blob: 997b2e214670f652e189df1483de41d6e64d32c2 [file] [log] [blame]
Philipp Reisnerb411b362009-09-25 16:07:19 -07001/*
2 drbd.c
3
4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5
6 Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
7 Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8 Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
9
10 Thanks to Carter Burden, Bart Grantham and Gennadiy Nerubayev
11 from Logicworks, Inc. for making SDP replication support possible.
12
13 drbd is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2, or (at your option)
16 any later version.
17
18 drbd is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with drbd; see the file COPYING. If not, write to
25 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26
27 */
28
Philipp Reisnerb411b362009-09-25 16:07:19 -070029#include <linux/module.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070030#include <linux/drbd.h>
31#include <asm/uaccess.h>
32#include <asm/types.h>
33#include <net/sock.h>
34#include <linux/ctype.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020035#include <linux/mutex.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070036#include <linux/fs.h>
37#include <linux/file.h>
38#include <linux/proc_fs.h>
39#include <linux/init.h>
40#include <linux/mm.h>
41#include <linux/memcontrol.h>
42#include <linux/mm_inline.h>
43#include <linux/slab.h>
44#include <linux/random.h>
45#include <linux/reboot.h>
46#include <linux/notifier.h>
47#include <linux/kthread.h>
48
49#define __KERNEL_SYSCALLS__
50#include <linux/unistd.h>
51#include <linux/vmalloc.h>
52
53#include <linux/drbd_limits.h>
54#include "drbd_int.h"
Philipp Reisnerb411b362009-09-25 16:07:19 -070055#include "drbd_req.h" /* only for _req_mod in tl_release and tl_clear */
56
57#include "drbd_vli.h"
58
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020059static DEFINE_MUTEX(drbd_main_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -070060int drbdd_init(struct drbd_thread *);
61int drbd_worker(struct drbd_thread *);
62int drbd_asender(struct drbd_thread *);
63
64int drbd_init(void);
65static int drbd_open(struct block_device *bdev, fmode_t mode);
66static int drbd_release(struct gendisk *gd, fmode_t mode);
Philipp Reisner00d56942011-02-09 18:09:48 +010067static int w_md_sync(struct drbd_work *w, int unused);
Philipp Reisnerb411b362009-09-25 16:07:19 -070068static void md_sync_timer_fn(unsigned long data);
Philipp Reisner00d56942011-02-09 18:09:48 +010069static int w_bitmap_io(struct drbd_work *w, int unused);
70static int w_go_diskless(struct drbd_work *w, int unused);
Philipp Reisnerb411b362009-09-25 16:07:19 -070071
Philipp Reisnerb411b362009-09-25 16:07:19 -070072MODULE_AUTHOR("Philipp Reisner <phil@linbit.com>, "
73 "Lars Ellenberg <lars@linbit.com>");
74MODULE_DESCRIPTION("drbd - Distributed Replicated Block Device v" REL_VERSION);
75MODULE_VERSION(REL_VERSION);
76MODULE_LICENSE("GPL");
Philipp Reisner81a5d602011-02-22 19:53:16 -050077MODULE_PARM_DESC(minor_count, "Approximate number of drbd devices ("
Philipp Reisner2b8a90b2011-01-10 11:15:17 +010078 __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 */
Philipp Reisner81a5d602011-02-22 19:53:16 -0500123struct idr minors;
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;
Lars Ellenberg35abf592011-02-23 12:39:46 +0100132mempool_t *drbd_md_io_page_pool;
Lars Ellenbergda4a75d2011-02-23 17:02:01 +0100133struct bio_set *drbd_md_io_bio_set;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700134
135/* I do not use a standard mempool, because:
136 1) I want to hand out the pre-allocated objects first.
137 2) I want to be able to interrupt sleeping allocation with a signal.
138 Note: This is a single linked list, the next pointer is the private
139 member of struct page.
140 */
141struct page *drbd_pp_pool;
142spinlock_t drbd_pp_lock;
143int drbd_pp_vacant;
144wait_queue_head_t drbd_pp_wait;
145
146DEFINE_RATELIMIT_STATE(drbd_ratelimit_state, 5 * HZ, 5);
147
Emese Revfy7d4e9d02009-12-14 00:59:30 +0100148static const struct block_device_operations drbd_ops = {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700149 .owner = THIS_MODULE,
150 .open = drbd_open,
151 .release = drbd_release,
152};
153
Lars Ellenbergda4a75d2011-02-23 17:02:01 +0100154static void bio_destructor_drbd(struct bio *bio)
155{
156 bio_free(bio, drbd_md_io_bio_set);
157}
158
159struct bio *bio_alloc_drbd(gfp_t gfp_mask)
160{
161 struct bio *bio;
162
163 if (!drbd_md_io_bio_set)
164 return bio_alloc(gfp_mask, 1);
165
166 bio = bio_alloc_bioset(gfp_mask, 1, drbd_md_io_bio_set);
167 if (!bio)
168 return NULL;
169 bio->bi_destructor = bio_destructor_drbd;
170 return bio;
171}
172
Philipp Reisnerb411b362009-09-25 16:07:19 -0700173#ifdef __CHECKER__
174/* When checking with sparse, and this is an inline function, sparse will
175 give tons of false positives. When this is a real functions sparse works.
176 */
177int _get_ldev_if_state(struct drbd_conf *mdev, enum drbd_disk_state mins)
178{
179 int io_allowed;
180
181 atomic_inc(&mdev->local_cnt);
182 io_allowed = (mdev->state.disk >= mins);
183 if (!io_allowed) {
184 if (atomic_dec_and_test(&mdev->local_cnt))
185 wake_up(&mdev->misc_wait);
186 }
187 return io_allowed;
188}
189
190#endif
191
192/**
193 * DOC: The transfer log
194 *
195 * The transfer log is a single linked list of &struct drbd_tl_epoch objects.
Philipp Reisner87eeee42011-01-19 14:16:30 +0100196 * mdev->tconn->newest_tle points to the head, mdev->tconn->oldest_tle points to the tail
Philipp Reisnerb411b362009-09-25 16:07:19 -0700197 * of the list. There is always at least one &struct drbd_tl_epoch object.
198 *
199 * Each &struct drbd_tl_epoch has a circular double linked list of requests
200 * attached.
201 */
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100202static int tl_init(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700203{
204 struct drbd_tl_epoch *b;
205
206 /* during device minor initialization, we may well use GFP_KERNEL */
207 b = kmalloc(sizeof(struct drbd_tl_epoch), GFP_KERNEL);
208 if (!b)
209 return 0;
210 INIT_LIST_HEAD(&b->requests);
211 INIT_LIST_HEAD(&b->w.list);
212 b->next = NULL;
213 b->br_number = 4711;
Philipp Reisner7e602c02010-05-27 14:49:27 +0200214 b->n_writes = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700215 b->w.cb = NULL; /* if this is != NULL, we need to dec_ap_pending in tl_clear */
216
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100217 tconn->oldest_tle = b;
218 tconn->newest_tle = b;
219 INIT_LIST_HEAD(&tconn->out_of_sequence_requests);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700220
Philipp Reisnerb411b362009-09-25 16:07:19 -0700221 return 1;
222}
223
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100224static void tl_cleanup(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700225{
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100226 if (tconn->oldest_tle != tconn->newest_tle)
227 conn_err(tconn, "ASSERT FAILED: oldest_tle == newest_tle\n");
228 if (!list_empty(&tconn->out_of_sequence_requests))
229 conn_err(tconn, "ASSERT FAILED: list_empty(out_of_sequence_requests)\n");
230 kfree(tconn->oldest_tle);
231 tconn->oldest_tle = NULL;
232 kfree(tconn->unused_spare_tle);
233 tconn->unused_spare_tle = NULL;
Andreas Gruenbacherd6287692011-01-13 23:05:39 +0100234}
235
Philipp Reisnerb411b362009-09-25 16:07:19 -0700236/**
237 * _tl_add_barrier() - Adds a barrier to the transfer log
238 * @mdev: DRBD device.
239 * @new: Barrier to be added before the current head of the TL.
240 *
241 * The caller must hold the req_lock.
242 */
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100243void _tl_add_barrier(struct drbd_tconn *tconn, struct drbd_tl_epoch *new)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700244{
245 struct drbd_tl_epoch *newest_before;
246
247 INIT_LIST_HEAD(&new->requests);
248 INIT_LIST_HEAD(&new->w.list);
249 new->w.cb = NULL; /* if this is != NULL, we need to dec_ap_pending in tl_clear */
250 new->next = NULL;
Philipp Reisner7e602c02010-05-27 14:49:27 +0200251 new->n_writes = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700252
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100253 newest_before = tconn->newest_tle;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700254 /* never send a barrier number == 0, because that is special-cased
255 * when using TCQ for our write ordering code */
256 new->br_number = (newest_before->br_number+1) ?: 1;
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100257 if (tconn->newest_tle != new) {
258 tconn->newest_tle->next = new;
259 tconn->newest_tle = new;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700260 }
261}
262
263/**
264 * tl_release() - Free or recycle the oldest &struct drbd_tl_epoch object of the TL
265 * @mdev: DRBD device.
266 * @barrier_nr: Expected identifier of the DRBD write barrier packet.
267 * @set_size: Expected number of requests before that barrier.
268 *
269 * In case the passed barrier_nr or set_size does not match the oldest
270 * &struct drbd_tl_epoch objects this function will cause a termination
271 * of the connection.
272 */
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100273void tl_release(struct drbd_tconn *tconn, unsigned int barrier_nr,
274 unsigned int set_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700275{
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100276 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700277 struct drbd_tl_epoch *b, *nob; /* next old barrier */
278 struct list_head *le, *tle;
279 struct drbd_request *r;
280
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100281 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700282
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100283 b = tconn->oldest_tle;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700284
285 /* first some paranoia code */
286 if (b == NULL) {
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100287 conn_err(tconn, "BAD! BarrierAck #%u received, but no epoch in tl!?\n",
288 barrier_nr);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700289 goto bail;
290 }
291 if (b->br_number != barrier_nr) {
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100292 conn_err(tconn, "BAD! BarrierAck #%u received, expected #%u!\n",
293 barrier_nr, b->br_number);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700294 goto bail;
295 }
Philipp Reisner7e602c02010-05-27 14:49:27 +0200296 if (b->n_writes != set_size) {
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100297 conn_err(tconn, "BAD! BarrierAck #%u received with n_writes=%u, expected n_writes=%u!\n",
298 barrier_nr, set_size, b->n_writes);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700299 goto bail;
300 }
301
302 /* Clean up list of requests processed during current epoch */
303 list_for_each_safe(le, tle, &b->requests) {
304 r = list_entry(le, struct drbd_request, tl_requests);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100305 _req_mod(r, BARRIER_ACKED);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700306 }
307 /* There could be requests on the list waiting for completion
308 of the write to the local disk. To avoid corruptions of
309 slab's data structures we have to remove the lists head.
310
311 Also there could have been a barrier ack out of sequence, overtaking
312 the write acks - which would be a bug and violating write ordering.
313 To not deadlock in case we lose connection while such requests are
314 still pending, we need some way to find them for the
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100315 _req_mode(CONNECTION_LOST_WHILE_PENDING).
Philipp Reisnerb411b362009-09-25 16:07:19 -0700316
317 These have been list_move'd to the out_of_sequence_requests list in
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100318 _req_mod(, BARRIER_ACKED) above.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700319 */
320 list_del_init(&b->requests);
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100321 mdev = b->w.mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700322
323 nob = b->next;
324 if (test_and_clear_bit(CREATE_BARRIER, &mdev->flags)) {
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100325 _tl_add_barrier(tconn, b);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700326 if (nob)
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100327 tconn->oldest_tle = nob;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700328 /* if nob == NULL b was the only barrier, and becomes the new
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100329 barrier. Therefore tconn->oldest_tle points already to b */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700330 } else {
331 D_ASSERT(nob != NULL);
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100332 tconn->oldest_tle = nob;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700333 kfree(b);
334 }
335
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100336 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700337 dec_ap_pending(mdev);
338
339 return;
340
341bail:
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100342 spin_unlock_irq(&tconn->req_lock);
343 conn_request_state(tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700344}
345
Philipp Reisner617049a2010-12-22 12:48:31 +0100346
Philipp Reisner11b58e72010-05-12 17:08:26 +0200347/**
348 * _tl_restart() - Walks the transfer log, and applies an action to all requests
349 * @mdev: DRBD device.
350 * @what: The action/event to perform with all request objects
351 *
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100352 * @what might be one of CONNECTION_LOST_WHILE_PENDING, RESEND, FAIL_FROZEN_DISK_IO,
353 * RESTART_FROZEN_DISK_IO.
Philipp Reisner11b58e72010-05-12 17:08:26 +0200354 */
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100355void _tl_restart(struct drbd_tconn *tconn, enum drbd_req_event what)
Philipp Reisner11b58e72010-05-12 17:08:26 +0200356{
357 struct drbd_tl_epoch *b, *tmp, **pn;
Philipp Reisnerb9b98712010-06-22 11:26:48 +0200358 struct list_head *le, *tle, carry_reads;
Philipp Reisner11b58e72010-05-12 17:08:26 +0200359 struct drbd_request *req;
360 int rv, n_writes, n_reads;
361
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100362 b = tconn->oldest_tle;
363 pn = &tconn->oldest_tle;
Philipp Reisner11b58e72010-05-12 17:08:26 +0200364 while (b) {
365 n_writes = 0;
366 n_reads = 0;
Philipp Reisnerb9b98712010-06-22 11:26:48 +0200367 INIT_LIST_HEAD(&carry_reads);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200368 list_for_each_safe(le, tle, &b->requests) {
369 req = list_entry(le, struct drbd_request, tl_requests);
370 rv = _req_mod(req, what);
371
372 n_writes += (rv & MR_WRITE) >> MR_WRITE_SHIFT;
373 n_reads += (rv & MR_READ) >> MR_READ_SHIFT;
374 }
375 tmp = b->next;
376
Philipp Reisnerb9b98712010-06-22 11:26:48 +0200377 if (n_writes) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100378 if (what == RESEND) {
Philipp Reisner11b58e72010-05-12 17:08:26 +0200379 b->n_writes = n_writes;
380 if (b->w.cb == NULL) {
381 b->w.cb = w_send_barrier;
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100382 inc_ap_pending(b->w.mdev);
383 set_bit(CREATE_BARRIER, &b->w.mdev->flags);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200384 }
385
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100386 drbd_queue_work(&tconn->data.work, &b->w);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200387 }
388 pn = &b->next;
389 } else {
Philipp Reisnerb9b98712010-06-22 11:26:48 +0200390 if (n_reads)
391 list_add(&carry_reads, &b->requests);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200392 /* there could still be requests on that ring list,
393 * in case local io is still pending */
394 list_del(&b->requests);
395
396 /* dec_ap_pending corresponding to queue_barrier.
397 * the newest barrier may not have been queued yet,
398 * in which case w.cb is still NULL. */
399 if (b->w.cb != NULL)
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100400 dec_ap_pending(b->w.mdev);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200401
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100402 if (b == tconn->newest_tle) {
Philipp Reisner11b58e72010-05-12 17:08:26 +0200403 /* recycle, but reinit! */
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100404 if (tmp != NULL)
405 conn_err(tconn, "ASSERT FAILED tmp == NULL");
Philipp Reisner11b58e72010-05-12 17:08:26 +0200406 INIT_LIST_HEAD(&b->requests);
Philipp Reisnerb9b98712010-06-22 11:26:48 +0200407 list_splice(&carry_reads, &b->requests);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200408 INIT_LIST_HEAD(&b->w.list);
409 b->w.cb = NULL;
410 b->br_number = net_random();
411 b->n_writes = 0;
412
413 *pn = b;
414 break;
415 }
416 *pn = tmp;
417 kfree(b);
418 }
419 b = tmp;
Philipp Reisnerb9b98712010-06-22 11:26:48 +0200420 list_splice(&carry_reads, &b->requests);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200421 }
422}
423
Philipp Reisnerb411b362009-09-25 16:07:19 -0700424
425/**
426 * tl_clear() - Clears all requests and &struct drbd_tl_epoch objects out of the TL
427 * @mdev: DRBD device.
428 *
429 * This is called after the connection to the peer was lost. The storage covered
430 * by the requests on the transfer gets marked as our of sync. Called from the
431 * receiver thread and the worker thread.
432 */
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100433void tl_clear(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700434{
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100435 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700436 struct list_head *le, *tle;
437 struct drbd_request *r;
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100438 int minor;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700439
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100440 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700441
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100442 _tl_restart(tconn, CONNECTION_LOST_WHILE_PENDING);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700443
444 /* we expect this list to be empty. */
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100445 if (!list_empty(&tconn->out_of_sequence_requests))
446 conn_err(tconn, "ASSERT FAILED list_empty(&out_of_sequence_requests)\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700447
448 /* but just in case, clean it up anyways! */
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100449 list_for_each_safe(le, tle, &tconn->out_of_sequence_requests) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700450 r = list_entry(le, struct drbd_request, tl_requests);
451 /* It would be nice to complete outside of spinlock.
452 * But this is easier for now. */
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100453 _req_mod(r, CONNECTION_LOST_WHILE_PENDING);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700454 }
455
456 /* ensure bit indicating barrier is required is clear */
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100457 idr_for_each_entry(&tconn->volumes, mdev, minor)
458 clear_bit(CREATE_BARRIER, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700459
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100460 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700461}
462
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100463void tl_restart(struct drbd_tconn *tconn, enum drbd_req_event what)
Philipp Reisner11b58e72010-05-12 17:08:26 +0200464{
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100465 spin_lock_irq(&tconn->req_lock);
466 _tl_restart(tconn, what);
467 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700468}
469
Philipp Reisnerb411b362009-09-25 16:07:19 -0700470static int drbd_thread_setup(void *arg)
471{
472 struct drbd_thread *thi = (struct drbd_thread *) arg;
Philipp Reisner392c8802011-02-09 10:33:31 +0100473 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700474 unsigned long flags;
475 int retval;
476
Philipp Reisnerf1b3a6e2011-02-08 15:35:58 +0100477 snprintf(current->comm, sizeof(current->comm), "drbd_%c_%s",
Philipp Reisner392c8802011-02-09 10:33:31 +0100478 thi->name[0], thi->tconn->name);
Philipp Reisnerf1b3a6e2011-02-08 15:35:58 +0100479
Philipp Reisnerb411b362009-09-25 16:07:19 -0700480restart:
481 retval = thi->function(thi);
482
483 spin_lock_irqsave(&thi->t_lock, flags);
484
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100485 /* if the receiver has been "EXITING", the last thing it did
Philipp Reisnerb411b362009-09-25 16:07:19 -0700486 * was set the conn state to "StandAlone",
487 * if now a re-connect request comes in, conn state goes C_UNCONNECTED,
488 * and receiver thread will be "started".
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100489 * drbd_thread_start needs to set "RESTARTING" in that case.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700490 * t_state check and assignment needs to be within the same spinlock,
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100491 * so either thread_start sees EXITING, and can remap to RESTARTING,
492 * or thread_start see NONE, and can proceed as normal.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700493 */
494
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100495 if (thi->t_state == RESTARTING) {
Philipp Reisner392c8802011-02-09 10:33:31 +0100496 conn_info(tconn, "Restarting %s thread\n", thi->name);
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100497 thi->t_state = RUNNING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700498 spin_unlock_irqrestore(&thi->t_lock, flags);
499 goto restart;
500 }
501
502 thi->task = NULL;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100503 thi->t_state = NONE;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700504 smp_mb();
505 complete(&thi->stop);
506 spin_unlock_irqrestore(&thi->t_lock, flags);
507
Philipp Reisner392c8802011-02-09 10:33:31 +0100508 conn_info(tconn, "Terminating %s\n", current->comm);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700509
510 /* Release mod reference taken when thread was started */
511 module_put(THIS_MODULE);
512 return retval;
513}
514
Philipp Reisner392c8802011-02-09 10:33:31 +0100515static void drbd_thread_init(struct drbd_tconn *tconn, struct drbd_thread *thi,
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100516 int (*func) (struct drbd_thread *), char *name)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700517{
518 spin_lock_init(&thi->t_lock);
519 thi->task = NULL;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100520 thi->t_state = NONE;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700521 thi->function = func;
Philipp Reisner392c8802011-02-09 10:33:31 +0100522 thi->tconn = tconn;
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100523 strncpy(thi->name, name, ARRAY_SIZE(thi->name));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700524}
525
526int drbd_thread_start(struct drbd_thread *thi)
527{
Philipp Reisner392c8802011-02-09 10:33:31 +0100528 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700529 struct task_struct *nt;
530 unsigned long flags;
531
Philipp Reisnerb411b362009-09-25 16:07:19 -0700532 /* is used from state engine doing drbd_thread_stop_nowait,
533 * while holding the req lock irqsave */
534 spin_lock_irqsave(&thi->t_lock, flags);
535
536 switch (thi->t_state) {
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100537 case NONE:
Philipp Reisner392c8802011-02-09 10:33:31 +0100538 conn_info(tconn, "Starting %s thread (from %s [%d])\n",
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100539 thi->name, current->comm, current->pid);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700540
541 /* Get ref on module for thread - this is released when thread exits */
542 if (!try_module_get(THIS_MODULE)) {
Philipp Reisner392c8802011-02-09 10:33:31 +0100543 conn_err(tconn, "Failed to get module reference in drbd_thread_start\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700544 spin_unlock_irqrestore(&thi->t_lock, flags);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100545 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700546 }
547
548 init_completion(&thi->stop);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700549 thi->reset_cpu_mask = 1;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100550 thi->t_state = RUNNING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700551 spin_unlock_irqrestore(&thi->t_lock, flags);
552 flush_signals(current); /* otherw. may get -ERESTARTNOINTR */
553
554 nt = kthread_create(drbd_thread_setup, (void *) thi,
Philipp Reisner392c8802011-02-09 10:33:31 +0100555 "drbd_%c_%s", thi->name[0], thi->tconn->name);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700556
557 if (IS_ERR(nt)) {
Philipp Reisner392c8802011-02-09 10:33:31 +0100558 conn_err(tconn, "Couldn't start thread\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700559
560 module_put(THIS_MODULE);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100561 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700562 }
563 spin_lock_irqsave(&thi->t_lock, flags);
564 thi->task = nt;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100565 thi->t_state = RUNNING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700566 spin_unlock_irqrestore(&thi->t_lock, flags);
567 wake_up_process(nt);
568 break;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100569 case EXITING:
570 thi->t_state = RESTARTING;
Philipp Reisner392c8802011-02-09 10:33:31 +0100571 conn_info(tconn, "Restarting %s thread (from %s [%d])\n",
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100572 thi->name, current->comm, current->pid);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700573 /* fall through */
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100574 case RUNNING:
575 case RESTARTING:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700576 default:
577 spin_unlock_irqrestore(&thi->t_lock, flags);
578 break;
579 }
580
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100581 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700582}
583
584
585void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait)
586{
587 unsigned long flags;
588
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100589 enum drbd_thread_state ns = restart ? RESTARTING : EXITING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700590
591 /* may be called from state engine, holding the req lock irqsave */
592 spin_lock_irqsave(&thi->t_lock, flags);
593
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100594 if (thi->t_state == NONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700595 spin_unlock_irqrestore(&thi->t_lock, flags);
596 if (restart)
597 drbd_thread_start(thi);
598 return;
599 }
600
601 if (thi->t_state != ns) {
602 if (thi->task == NULL) {
603 spin_unlock_irqrestore(&thi->t_lock, flags);
604 return;
605 }
606
607 thi->t_state = ns;
608 smp_mb();
609 init_completion(&thi->stop);
610 if (thi->task != current)
611 force_sig(DRBD_SIGKILL, thi->task);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700612 }
613
614 spin_unlock_irqrestore(&thi->t_lock, flags);
615
616 if (wait)
617 wait_for_completion(&thi->stop);
618}
619
Philipp Reisner392c8802011-02-09 10:33:31 +0100620static struct drbd_thread *drbd_task_to_thread(struct drbd_tconn *tconn, struct task_struct *task)
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100621{
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100622 struct drbd_thread *thi =
623 task == tconn->receiver.task ? &tconn->receiver :
624 task == tconn->asender.task ? &tconn->asender :
625 task == tconn->worker.task ? &tconn->worker : NULL;
626
627 return thi;
628}
629
Philipp Reisner392c8802011-02-09 10:33:31 +0100630char *drbd_task_to_thread_name(struct drbd_tconn *tconn, struct task_struct *task)
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100631{
Philipp Reisner392c8802011-02-09 10:33:31 +0100632 struct drbd_thread *thi = drbd_task_to_thread(tconn, task);
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100633 return thi ? thi->name : task->comm;
634}
635
Philipp Reisner80883192011-02-18 14:56:45 +0100636int conn_lowest_minor(struct drbd_tconn *tconn)
Philipp Reisner80822282011-02-08 12:46:30 +0100637{
638 int minor = 0;
Philipp Reisner774b3052011-02-22 02:07:03 -0500639
640 if (!idr_get_next(&tconn->volumes, &minor))
641 return -1;
Philipp Reisner80822282011-02-08 12:46:30 +0100642 return minor;
643}
Philipp Reisner774b3052011-02-22 02:07:03 -0500644
645#ifdef CONFIG_SMP
Philipp Reisnerb411b362009-09-25 16:07:19 -0700646/**
647 * drbd_calc_cpu_mask() - Generate CPU masks, spread over all CPUs
648 * @mdev: DRBD device.
649 *
650 * Forces all threads of a device onto the same CPU. This is beneficial for
651 * DRBD's performance. May be overwritten by user's configuration.
652 */
Philipp Reisner80822282011-02-08 12:46:30 +0100653void drbd_calc_cpu_mask(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700654{
655 int ord, cpu;
656
657 /* user override. */
Philipp Reisner80822282011-02-08 12:46:30 +0100658 if (cpumask_weight(tconn->cpu_mask))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700659 return;
660
Philipp Reisner80822282011-02-08 12:46:30 +0100661 ord = conn_lowest_minor(tconn) % cpumask_weight(cpu_online_mask);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700662 for_each_online_cpu(cpu) {
663 if (ord-- == 0) {
Philipp Reisner80822282011-02-08 12:46:30 +0100664 cpumask_set_cpu(cpu, tconn->cpu_mask);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700665 return;
666 }
667 }
668 /* should not be reached */
Philipp Reisner80822282011-02-08 12:46:30 +0100669 cpumask_setall(tconn->cpu_mask);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700670}
671
672/**
673 * drbd_thread_current_set_cpu() - modifies the cpu mask of the _current_ thread
674 * @mdev: DRBD device.
Philipp Reisnerbc31fe32011-02-07 11:14:38 +0100675 * @thi: drbd_thread object
Philipp Reisnerb411b362009-09-25 16:07:19 -0700676 *
677 * call in the "main loop" of _all_ threads, no need for any mutex, current won't die
678 * prematurely.
679 */
Philipp Reisner80822282011-02-08 12:46:30 +0100680void drbd_thread_current_set_cpu(struct drbd_thread *thi)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700681{
682 struct task_struct *p = current;
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100683
Philipp Reisnerb411b362009-09-25 16:07:19 -0700684 if (!thi->reset_cpu_mask)
685 return;
686 thi->reset_cpu_mask = 0;
Philipp Reisner392c8802011-02-09 10:33:31 +0100687 set_cpus_allowed_ptr(p, thi->tconn->cpu_mask);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700688}
689#endif
690
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100691static void prepare_header80(struct p_header80 *h, enum drbd_packet cmd, int size)
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100692{
693 h->magic = cpu_to_be32(DRBD_MAGIC);
694 h->command = cpu_to_be16(cmd);
695 h->length = cpu_to_be16(size);
696}
697
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100698static void prepare_header95(struct p_header95 *h, enum drbd_packet cmd, int size)
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100699{
700 h->magic = cpu_to_be16(DRBD_MAGIC_BIG);
701 h->command = cpu_to_be16(cmd);
702 h->length = cpu_to_be32(size);
703}
704
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100705static void _prepare_header(struct drbd_tconn *tconn, int vnr, struct p_header *h,
706 enum drbd_packet cmd, int size)
707{
708 if (tconn->agreed_pro_version >= 100 || size > DRBD_MAX_SIZE_H80_PACKET)
709 prepare_header95(&h->h95, cmd, size);
710 else
711 prepare_header80(&h->h80, cmd, size);
712}
713
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100714static void prepare_header(struct drbd_conf *mdev, struct p_header *h,
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100715 enum drbd_packet cmd, int size)
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100716{
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100717 _prepare_header(mdev->tconn, mdev->vnr, h, cmd, size);
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100718}
719
Philipp Reisnerb411b362009-09-25 16:07:19 -0700720/* the appropriate socket mutex must be held already */
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100721int _conn_send_cmd(struct drbd_tconn *tconn, int vnr, struct socket *sock,
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100722 enum drbd_packet cmd, struct p_header *h, size_t size,
723 unsigned msg_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700724{
725 int sent, ok;
726
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100727 _prepare_header(tconn, vnr, h, cmd, size - sizeof(struct p_header));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700728
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100729 sent = drbd_send(tconn, sock, h, size, msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700730
731 ok = (sent == size);
Lars Ellenberg0ddc5542011-01-21 12:35:15 +0100732 if (!ok && !signal_pending(current))
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100733 conn_warn(tconn, "short sent %s size=%d sent=%d\n",
734 cmdname(cmd), (int)size, sent);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700735 return ok;
736}
737
738/* don't pass the socket. we may only look at it
739 * when we hold the appropriate socket mutex.
740 */
Philipp Reisner2a67d8b2011-02-09 14:10:32 +0100741int conn_send_cmd(struct drbd_tconn *tconn, int vnr, int use_data_socket,
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100742 enum drbd_packet cmd, struct p_header *h, size_t size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700743{
744 int ok = 0;
745 struct socket *sock;
746
747 if (use_data_socket) {
Philipp Reisner2a67d8b2011-02-09 14:10:32 +0100748 mutex_lock(&tconn->data.mutex);
749 sock = tconn->data.socket;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700750 } else {
Philipp Reisner2a67d8b2011-02-09 14:10:32 +0100751 mutex_lock(&tconn->meta.mutex);
752 sock = tconn->meta.socket;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700753 }
754
755 /* drbd_disconnect() could have called drbd_free_sock()
756 * while we were waiting in down()... */
757 if (likely(sock != NULL))
Philipp Reisner2a67d8b2011-02-09 14:10:32 +0100758 ok = _conn_send_cmd(tconn, vnr, sock, cmd, h, size, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700759
760 if (use_data_socket)
Philipp Reisner2a67d8b2011-02-09 14:10:32 +0100761 mutex_unlock(&tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700762 else
Philipp Reisner2a67d8b2011-02-09 14:10:32 +0100763 mutex_unlock(&tconn->meta.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700764 return ok;
765}
766
Philipp Reisner61120872011-02-08 09:50:54 +0100767int conn_send_cmd2(struct drbd_tconn *tconn, enum drbd_packet cmd, char *data,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700768 size_t size)
769{
Philipp Reisner61120872011-02-08 09:50:54 +0100770 struct p_header80 h;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700771 int ok;
772
Philipp Reisner61120872011-02-08 09:50:54 +0100773 prepare_header80(&h, cmd, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700774
Philipp Reisner61120872011-02-08 09:50:54 +0100775 if (!drbd_get_data_sock(tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700776 return 0;
777
Philipp Reisnerb411b362009-09-25 16:07:19 -0700778 ok = (sizeof(h) ==
Philipp Reisner61120872011-02-08 09:50:54 +0100779 drbd_send(tconn, tconn->data.socket, &h, sizeof(h), 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700780 ok = ok && (size ==
Philipp Reisner61120872011-02-08 09:50:54 +0100781 drbd_send(tconn, tconn->data.socket, data, size, 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700782
Philipp Reisner61120872011-02-08 09:50:54 +0100783 drbd_put_data_sock(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700784
785 return ok;
786}
787
788int drbd_send_sync_param(struct drbd_conf *mdev, struct syncer_conf *sc)
789{
Philipp Reisner8e26f9c2010-07-06 17:25:54 +0200790 struct p_rs_param_95 *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700791 struct socket *sock;
792 int size, rv;
Philipp Reisner31890f42011-01-19 14:12:51 +0100793 const int apv = mdev->tconn->agreed_pro_version;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700794
795 size = apv <= 87 ? sizeof(struct p_rs_param)
796 : apv == 88 ? sizeof(struct p_rs_param)
797 + strlen(mdev->sync_conf.verify_alg) + 1
Philipp Reisner8e26f9c2010-07-06 17:25:54 +0200798 : apv <= 94 ? sizeof(struct p_rs_param_89)
799 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700800
801 /* used from admin command context and receiver/worker context.
802 * to avoid kmalloc, grab the socket right here,
803 * then use the pre-allocated sbuf there */
Philipp Reisnere42325a2011-01-19 13:55:45 +0100804 mutex_lock(&mdev->tconn->data.mutex);
805 sock = mdev->tconn->data.socket;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700806
807 if (likely(sock != NULL)) {
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100808 enum drbd_packet cmd =
809 apv >= 89 ? P_SYNC_PARAM89 : P_SYNC_PARAM;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700810
Philipp Reisnere42325a2011-01-19 13:55:45 +0100811 p = &mdev->tconn->data.sbuf.rs_param_95;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700812
813 /* initialize verify_alg and csums_alg */
814 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
815
816 p->rate = cpu_to_be32(sc->rate);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +0200817 p->c_plan_ahead = cpu_to_be32(sc->c_plan_ahead);
818 p->c_delay_target = cpu_to_be32(sc->c_delay_target);
819 p->c_fill_target = cpu_to_be32(sc->c_fill_target);
820 p->c_max_rate = cpu_to_be32(sc->c_max_rate);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700821
822 if (apv >= 88)
823 strcpy(p->verify_alg, mdev->sync_conf.verify_alg);
824 if (apv >= 89)
825 strcpy(p->csums_alg, mdev->sync_conf.csums_alg);
826
827 rv = _drbd_send_cmd(mdev, sock, cmd, &p->head, size, 0);
828 } else
829 rv = 0; /* not ok */
830
Philipp Reisnere42325a2011-01-19 13:55:45 +0100831 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700832
833 return rv;
834}
835
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100836int drbd_send_protocol(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700837{
838 struct p_protocol *p;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100839 int size, cf, rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700840
841 size = sizeof(struct p_protocol);
842
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100843 if (tconn->agreed_pro_version >= 87)
844 size += strlen(tconn->net_conf->integrity_alg) + 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700845
846 /* we must not recurse into our own queue,
847 * as that is blocked during handshake */
848 p = kmalloc(size, GFP_NOIO);
849 if (p == NULL)
850 return 0;
851
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100852 p->protocol = cpu_to_be32(tconn->net_conf->wire_protocol);
853 p->after_sb_0p = cpu_to_be32(tconn->net_conf->after_sb_0p);
854 p->after_sb_1p = cpu_to_be32(tconn->net_conf->after_sb_1p);
855 p->after_sb_2p = cpu_to_be32(tconn->net_conf->after_sb_2p);
856 p->two_primaries = cpu_to_be32(tconn->net_conf->two_primaries);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700857
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100858 cf = 0;
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100859 if (tconn->net_conf->want_lose)
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100860 cf |= CF_WANT_LOSE;
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100861 if (tconn->net_conf->dry_run) {
862 if (tconn->agreed_pro_version >= 92)
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100863 cf |= CF_DRY_RUN;
864 else {
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100865 conn_err(tconn, "--dry-run is not supported by peer");
Dan Carpenter7ac314c2010-04-22 14:27:23 +0200866 kfree(p);
Philipp Reisner148efa12011-01-15 00:21:15 +0100867 return -1;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100868 }
869 }
870 p->conn_flags = cpu_to_be32(cf);
871
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100872 if (tconn->agreed_pro_version >= 87)
873 strcpy(p->integrity_alg, tconn->net_conf->integrity_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700874
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100875 rv = conn_send_cmd2(tconn, P_PROTOCOL, p->head.payload, size - sizeof(struct p_header));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700876 kfree(p);
877 return rv;
878}
879
880int _drbd_send_uuids(struct drbd_conf *mdev, u64 uuid_flags)
881{
882 struct p_uuids p;
883 int i;
884
885 if (!get_ldev_if_state(mdev, D_NEGOTIATING))
886 return 1;
887
888 for (i = UI_CURRENT; i < UI_SIZE; i++)
889 p.uuid[i] = mdev->ldev ? cpu_to_be64(mdev->ldev->md.uuid[i]) : 0;
890
891 mdev->comm_bm_set = drbd_bm_total_weight(mdev);
892 p.uuid[UI_SIZE] = cpu_to_be64(mdev->comm_bm_set);
Philipp Reisner89e58e72011-01-19 13:12:45 +0100893 uuid_flags |= mdev->tconn->net_conf->want_lose ? 1 : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700894 uuid_flags |= test_bit(CRASHED_PRIMARY, &mdev->flags) ? 2 : 0;
895 uuid_flags |= mdev->new_state_tmp.disk == D_INCONSISTENT ? 4 : 0;
896 p.uuid[UI_FLAGS] = cpu_to_be64(uuid_flags);
897
898 put_ldev(mdev);
899
Philipp Reisnerc0129492011-01-19 16:58:16 +0100900 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_UUIDS, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700901}
902
903int drbd_send_uuids(struct drbd_conf *mdev)
904{
905 return _drbd_send_uuids(mdev, 0);
906}
907
908int drbd_send_uuids_skip_initial_sync(struct drbd_conf *mdev)
909{
910 return _drbd_send_uuids(mdev, 8);
911}
912
Lars Ellenberg62b0da32011-01-20 13:25:21 +0100913void drbd_print_uuids(struct drbd_conf *mdev, const char *text)
914{
915 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
916 u64 *uuid = mdev->ldev->md.uuid;
917 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX\n",
918 text,
919 (unsigned long long)uuid[UI_CURRENT],
920 (unsigned long long)uuid[UI_BITMAP],
921 (unsigned long long)uuid[UI_HISTORY_START],
922 (unsigned long long)uuid[UI_HISTORY_END]);
923 put_ldev(mdev);
924 } else {
925 dev_info(DEV, "%s effective data uuid: %016llX\n",
926 text,
927 (unsigned long long)mdev->ed_uuid);
928 }
929}
930
Lars Ellenberg5a22db82010-12-17 21:14:23 +0100931int drbd_gen_and_send_sync_uuid(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700932{
933 struct p_rs_uuid p;
Lars Ellenberg5a22db82010-12-17 21:14:23 +0100934 u64 uuid;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700935
Lars Ellenberg5a22db82010-12-17 21:14:23 +0100936 D_ASSERT(mdev->state.disk == D_UP_TO_DATE);
937
Philipp Reisner4a23f262011-01-11 17:42:17 +0100938 uuid = mdev->ldev->md.uuid[UI_BITMAP] + UUID_NEW_BM_OFFSET;
Lars Ellenberg5a22db82010-12-17 21:14:23 +0100939 drbd_uuid_set(mdev, UI_BITMAP, uuid);
Lars Ellenberg62b0da32011-01-20 13:25:21 +0100940 drbd_print_uuids(mdev, "updated sync UUID");
Lars Ellenberg5a22db82010-12-17 21:14:23 +0100941 drbd_md_sync(mdev);
942 p.uuid = cpu_to_be64(uuid);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700943
Philipp Reisnerc0129492011-01-19 16:58:16 +0100944 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_SYNC_UUID, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700945}
946
Philipp Reisnere89b5912010-03-24 17:11:33 +0100947int drbd_send_sizes(struct drbd_conf *mdev, int trigger_reply, enum dds_flags flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700948{
949 struct p_sizes p;
950 sector_t d_size, u_size;
Philipp Reisner99432fc2011-05-20 16:39:13 +0200951 int q_order_type, max_bio_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700952 int ok;
953
954 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
955 D_ASSERT(mdev->ldev->backing_bdev);
956 d_size = drbd_get_max_capacity(mdev->ldev);
957 u_size = mdev->ldev->dc.disk_size;
958 q_order_type = drbd_queue_order_type(mdev);
Philipp Reisner99432fc2011-05-20 16:39:13 +0200959 max_bio_size = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
960 max_bio_size = min_t(int, max_bio_size, DRBD_MAX_BIO_SIZE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700961 put_ldev(mdev);
962 } else {
963 d_size = 0;
964 u_size = 0;
965 q_order_type = QUEUE_ORDERED_NONE;
Philipp Reisner99432fc2011-05-20 16:39:13 +0200966 max_bio_size = DRBD_MAX_BIO_SIZE; /* ... multiple BIOs per peer_request */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700967 }
968
969 p.d_size = cpu_to_be64(d_size);
970 p.u_size = cpu_to_be64(u_size);
971 p.c_size = cpu_to_be64(trigger_reply ? 0 : drbd_get_capacity(mdev->this_bdev));
Philipp Reisner99432fc2011-05-20 16:39:13 +0200972 p.max_bio_size = cpu_to_be32(max_bio_size);
Philipp Reisnere89b5912010-03-24 17:11:33 +0100973 p.queue_order_type = cpu_to_be16(q_order_type);
974 p.dds_flags = cpu_to_be16(flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700975
Philipp Reisnerc0129492011-01-19 16:58:16 +0100976 ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_SIZES, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700977 return ok;
978}
979
980/**
981 * drbd_send_state() - Sends the drbd state to the peer
982 * @mdev: DRBD device.
983 */
984int drbd_send_state(struct drbd_conf *mdev)
985{
986 struct socket *sock;
987 struct p_state p;
988 int ok = 0;
989
Philipp Reisnere42325a2011-01-19 13:55:45 +0100990 mutex_lock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700991
992 p.state = cpu_to_be32(mdev->state.i); /* Within the send mutex */
Philipp Reisnere42325a2011-01-19 13:55:45 +0100993 sock = mdev->tconn->data.socket;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700994
995 if (likely(sock != NULL)) {
Philipp Reisnerc0129492011-01-19 16:58:16 +0100996 ok = _drbd_send_cmd(mdev, sock, P_STATE, &p.head, sizeof(p), 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700997 }
998
Philipp Reisnere42325a2011-01-19 13:55:45 +0100999 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001000
Philipp Reisnerb411b362009-09-25 16:07:19 -07001001 return ok;
1002}
1003
Philipp Reisnercf29c9d2011-02-11 15:11:24 +01001004int _conn_send_state_req(struct drbd_tconn *tconn, int vnr, enum drbd_packet cmd,
1005 union drbd_state mask, union drbd_state val)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001006{
1007 struct p_req_state p;
1008
1009 p.mask = cpu_to_be32(mask.i);
1010 p.val = cpu_to_be32(val.i);
1011
Philipp Reisnercf29c9d2011-02-11 15:11:24 +01001012 return conn_send_cmd(tconn, vnr, USE_DATA_SOCKET, cmd, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001013}
1014
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01001015int drbd_send_sr_reply(struct drbd_conf *mdev, enum drbd_state_rv retcode)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001016{
1017 struct p_req_state_reply p;
1018
1019 p.retcode = cpu_to_be32(retcode);
1020
Philipp Reisnerc0129492011-01-19 16:58:16 +01001021 return drbd_send_cmd(mdev, USE_META_SOCKET, P_STATE_CHG_REPLY, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001022}
1023
Philipp Reisner047cd4a2011-02-15 11:09:33 +01001024int conn_send_sr_reply(struct drbd_tconn *tconn, enum drbd_state_rv retcode)
1025{
1026 struct p_req_state_reply p;
1027 enum drbd_packet cmd = tconn->agreed_pro_version < 100 ? P_STATE_CHG_REPLY : P_CONN_ST_CHG_REPLY;
1028
1029 p.retcode = cpu_to_be32(retcode);
1030
1031 return conn_send_cmd(tconn, 0, USE_META_SOCKET, cmd, &p.head, sizeof(p));
1032}
1033
Philipp Reisnerb411b362009-09-25 16:07:19 -07001034int fill_bitmap_rle_bits(struct drbd_conf *mdev,
1035 struct p_compressed_bm *p,
1036 struct bm_xfer_ctx *c)
1037{
1038 struct bitstream bs;
1039 unsigned long plain_bits;
1040 unsigned long tmp;
1041 unsigned long rl;
1042 unsigned len;
1043 unsigned toggle;
1044 int bits;
1045
1046 /* may we use this feature? */
1047 if ((mdev->sync_conf.use_rle == 0) ||
Philipp Reisner31890f42011-01-19 14:12:51 +01001048 (mdev->tconn->agreed_pro_version < 90))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001049 return 0;
1050
1051 if (c->bit_offset >= c->bm_bits)
1052 return 0; /* nothing to do. */
1053
1054 /* use at most thus many bytes */
1055 bitstream_init(&bs, p->code, BM_PACKET_VLI_BYTES_MAX, 0);
1056 memset(p->code, 0, BM_PACKET_VLI_BYTES_MAX);
1057 /* plain bits covered in this code string */
1058 plain_bits = 0;
1059
1060 /* p->encoding & 0x80 stores whether the first run length is set.
1061 * bit offset is implicit.
1062 * start with toggle == 2 to be able to tell the first iteration */
1063 toggle = 2;
1064
1065 /* see how much plain bits we can stuff into one packet
1066 * using RLE and VLI. */
1067 do {
1068 tmp = (toggle == 0) ? _drbd_bm_find_next_zero(mdev, c->bit_offset)
1069 : _drbd_bm_find_next(mdev, c->bit_offset);
1070 if (tmp == -1UL)
1071 tmp = c->bm_bits;
1072 rl = tmp - c->bit_offset;
1073
1074 if (toggle == 2) { /* first iteration */
1075 if (rl == 0) {
1076 /* the first checked bit was set,
1077 * store start value, */
1078 DCBP_set_start(p, 1);
1079 /* but skip encoding of zero run length */
1080 toggle = !toggle;
1081 continue;
1082 }
1083 DCBP_set_start(p, 0);
1084 }
1085
1086 /* paranoia: catch zero runlength.
1087 * can only happen if bitmap is modified while we scan it. */
1088 if (rl == 0) {
1089 dev_err(DEV, "unexpected zero runlength while encoding bitmap "
1090 "t:%u bo:%lu\n", toggle, c->bit_offset);
1091 return -1;
1092 }
1093
1094 bits = vli_encode_bits(&bs, rl);
1095 if (bits == -ENOBUFS) /* buffer full */
1096 break;
1097 if (bits <= 0) {
1098 dev_err(DEV, "error while encoding bitmap: %d\n", bits);
1099 return 0;
1100 }
1101
1102 toggle = !toggle;
1103 plain_bits += rl;
1104 c->bit_offset = tmp;
1105 } while (c->bit_offset < c->bm_bits);
1106
1107 len = bs.cur.b - p->code + !!bs.cur.bit;
1108
1109 if (plain_bits < (len << 3)) {
1110 /* incompressible with this method.
1111 * we need to rewind both word and bit position. */
1112 c->bit_offset -= plain_bits;
1113 bm_xfer_ctx_bit_to_word_offset(c);
1114 c->bit_offset = c->word_offset * BITS_PER_LONG;
1115 return 0;
1116 }
1117
1118 /* RLE + VLI was able to compress it just fine.
1119 * update c->word_offset. */
1120 bm_xfer_ctx_bit_to_word_offset(c);
1121
1122 /* store pad_bits */
1123 DCBP_set_pad_bits(p, (8 - bs.cur.bit) & 0x7);
1124
1125 return len;
1126}
1127
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001128/**
1129 * send_bitmap_rle_or_plain
1130 *
1131 * Return 0 when done, 1 when another iteration is needed, and a negative error
1132 * code upon failure.
1133 */
1134static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07001135send_bitmap_rle_or_plain(struct drbd_conf *mdev,
Philipp Reisnerc0129492011-01-19 16:58:16 +01001136 struct p_header *h, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001137{
1138 struct p_compressed_bm *p = (void*)h;
1139 unsigned long num_words;
1140 int len;
1141 int ok;
1142
1143 len = fill_bitmap_rle_bits(mdev, p, c);
1144
1145 if (len < 0)
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001146 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001147
1148 if (len) {
1149 DCBP_set_code(p, RLE_VLI_Bits);
Philipp Reisnere42325a2011-01-19 13:55:45 +01001150 ok = _drbd_send_cmd(mdev, mdev->tconn->data.socket, P_COMPRESSED_BITMAP, h,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001151 sizeof(*p) + len, 0);
1152
1153 c->packets[0]++;
1154 c->bytes[0] += sizeof(*p) + len;
1155
1156 if (c->bit_offset >= c->bm_bits)
1157 len = 0; /* DONE */
1158 } else {
1159 /* was not compressible.
1160 * send a buffer full of plain text bits instead. */
1161 num_words = min_t(size_t, BM_PACKET_WORDS, c->bm_words - c->word_offset);
1162 len = num_words * sizeof(long);
1163 if (len)
1164 drbd_bm_get_lel(mdev, c->word_offset, num_words, (unsigned long*)h->payload);
Philipp Reisnere42325a2011-01-19 13:55:45 +01001165 ok = _drbd_send_cmd(mdev, mdev->tconn->data.socket, P_BITMAP,
Philipp Reisner0b70a132010-08-20 13:36:10 +02001166 h, sizeof(struct p_header80) + len, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001167 c->word_offset += num_words;
1168 c->bit_offset = c->word_offset * BITS_PER_LONG;
1169
1170 c->packets[1]++;
Philipp Reisner0b70a132010-08-20 13:36:10 +02001171 c->bytes[1] += sizeof(struct p_header80) + len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001172
1173 if (c->bit_offset > c->bm_bits)
1174 c->bit_offset = c->bm_bits;
1175 }
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001176 if (ok) {
1177 if (len == 0) {
1178 INFO_bm_xfer_stats(mdev, "send", c);
1179 return 0;
1180 } else
1181 return 1;
1182 }
1183 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001184}
1185
1186/* See the comment at receive_bitmap() */
1187int _drbd_send_bitmap(struct drbd_conf *mdev)
1188{
1189 struct bm_xfer_ctx c;
Philipp Reisnerc0129492011-01-19 16:58:16 +01001190 struct p_header *p;
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001191 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001192
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001193 if (!expect(mdev->bitmap))
1194 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001195
1196 /* maybe we should use some per thread scratch page,
1197 * and allocate that during initial device creation? */
Philipp Reisnerc0129492011-01-19 16:58:16 +01001198 p = (struct p_header *) __get_free_page(GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001199 if (!p) {
1200 dev_err(DEV, "failed to allocate one page buffer in %s\n", __func__);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001201 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001202 }
1203
1204 if (get_ldev(mdev)) {
1205 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
1206 dev_info(DEV, "Writing the whole bitmap, MDF_FullSync was set.\n");
1207 drbd_bm_set_all(mdev);
1208 if (drbd_bm_write(mdev)) {
1209 /* write_bm did fail! Leave full sync flag set in Meta P_DATA
1210 * but otherwise process as per normal - need to tell other
1211 * side that a full resync is required! */
1212 dev_err(DEV, "Failed to write bitmap to disk!\n");
1213 } else {
1214 drbd_md_clear_flag(mdev, MDF_FULL_SYNC);
1215 drbd_md_sync(mdev);
1216 }
1217 }
1218 put_ldev(mdev);
1219 }
1220
1221 c = (struct bm_xfer_ctx) {
1222 .bm_bits = drbd_bm_bits(mdev),
1223 .bm_words = drbd_bm_words(mdev),
1224 };
1225
1226 do {
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001227 err = send_bitmap_rle_or_plain(mdev, p, &c);
1228 } while (err > 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001229
1230 free_page((unsigned long) p);
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001231 return err == 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001232}
1233
1234int drbd_send_bitmap(struct drbd_conf *mdev)
1235{
1236 int err;
1237
Philipp Reisner61120872011-02-08 09:50:54 +01001238 if (!drbd_get_data_sock(mdev->tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001239 return -1;
1240 err = !_drbd_send_bitmap(mdev);
Philipp Reisner61120872011-02-08 09:50:54 +01001241 drbd_put_data_sock(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001242 return err;
1243}
1244
1245int drbd_send_b_ack(struct drbd_conf *mdev, u32 barrier_nr, u32 set_size)
1246{
1247 int ok;
1248 struct p_barrier_ack p;
1249
1250 p.barrier = barrier_nr;
1251 p.set_size = cpu_to_be32(set_size);
1252
1253 if (mdev->state.conn < C_CONNECTED)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001254 return false;
Philipp Reisnerc0129492011-01-19 16:58:16 +01001255 ok = drbd_send_cmd(mdev, USE_META_SOCKET, P_BARRIER_ACK, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001256 return ok;
1257}
1258
1259/**
1260 * _drbd_send_ack() - Sends an ack packet
1261 * @mdev: DRBD device.
1262 * @cmd: Packet command code.
1263 * @sector: sector, needs to be in big endian byte order
1264 * @blksize: size in byte, needs to be in big endian byte order
1265 * @block_id: Id, big endian byte order
1266 */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001267static int _drbd_send_ack(struct drbd_conf *mdev, enum drbd_packet cmd,
1268 u64 sector, u32 blksize, u64 block_id)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001269{
1270 int ok;
1271 struct p_block_ack p;
1272
1273 p.sector = sector;
1274 p.block_id = block_id;
1275 p.blksize = blksize;
Andreas Gruenbacher8ccf2182011-02-24 11:35:43 +01001276 p.seq_num = cpu_to_be32(atomic_inc_return(&mdev->packet_seq));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001277
Philipp Reisnere42325a2011-01-19 13:55:45 +01001278 if (!mdev->tconn->meta.socket || mdev->state.conn < C_CONNECTED)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001279 return false;
Philipp Reisnerc0129492011-01-19 16:58:16 +01001280 ok = drbd_send_cmd(mdev, USE_META_SOCKET, cmd, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001281 return ok;
1282}
1283
Lars Ellenberg2b2bf212010-10-06 11:46:55 +02001284/* dp->sector and dp->block_id already/still in network byte order,
1285 * data_size is payload size according to dp->head,
1286 * and may need to be corrected for digest size. */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001287int drbd_send_ack_dp(struct drbd_conf *mdev, enum drbd_packet cmd,
Lars Ellenberg2b2bf212010-10-06 11:46:55 +02001288 struct p_data *dp, int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001289{
Philipp Reisnera0638452011-01-19 14:31:32 +01001290 data_size -= (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_r_tfm) ?
1291 crypto_hash_digestsize(mdev->tconn->integrity_r_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001292 return _drbd_send_ack(mdev, cmd, dp->sector, cpu_to_be32(data_size),
1293 dp->block_id);
1294}
1295
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001296int drbd_send_ack_rp(struct drbd_conf *mdev, enum drbd_packet cmd,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001297 struct p_block_req *rp)
1298{
1299 return _drbd_send_ack(mdev, cmd, rp->sector, rp->blksize, rp->block_id);
1300}
1301
1302/**
1303 * drbd_send_ack() - Sends an ack packet
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001304 * @mdev: DRBD device
1305 * @cmd: packet command code
1306 * @peer_req: peer request
Philipp Reisnerb411b362009-09-25 16:07:19 -07001307 */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001308int drbd_send_ack(struct drbd_conf *mdev, enum drbd_packet cmd,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001309 struct drbd_peer_request *peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001310{
1311 return _drbd_send_ack(mdev, cmd,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001312 cpu_to_be64(peer_req->i.sector),
1313 cpu_to_be32(peer_req->i.size),
1314 peer_req->block_id);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001315}
1316
1317/* This function misuses the block_id field to signal if the blocks
1318 * are is sync or not. */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001319int drbd_send_ack_ex(struct drbd_conf *mdev, enum drbd_packet cmd,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001320 sector_t sector, int blksize, u64 block_id)
1321{
1322 return _drbd_send_ack(mdev, cmd,
1323 cpu_to_be64(sector),
1324 cpu_to_be32(blksize),
1325 cpu_to_be64(block_id));
1326}
1327
1328int drbd_send_drequest(struct drbd_conf *mdev, int cmd,
1329 sector_t sector, int size, u64 block_id)
1330{
1331 int ok;
1332 struct p_block_req p;
1333
1334 p.sector = cpu_to_be64(sector);
1335 p.block_id = block_id;
1336 p.blksize = cpu_to_be32(size);
1337
Philipp Reisnerc0129492011-01-19 16:58:16 +01001338 ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, cmd, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001339 return ok;
1340}
1341
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001342int drbd_send_drequest_csum(struct drbd_conf *mdev, sector_t sector, int size,
1343 void *digest, int digest_size, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001344{
1345 int ok;
1346 struct p_block_req p;
1347
Philipp Reisnerfd340c12011-01-19 16:57:39 +01001348 prepare_header(mdev, &p.head, cmd, sizeof(p) - sizeof(struct p_header) + digest_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001349 p.sector = cpu_to_be64(sector);
Andreas Gruenbacher9a8e7752011-01-11 14:04:09 +01001350 p.block_id = ID_SYNCER /* unused */;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001351 p.blksize = cpu_to_be32(size);
1352
Philipp Reisnere42325a2011-01-19 13:55:45 +01001353 mutex_lock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001354
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001355 ok = (sizeof(p) == drbd_send(mdev->tconn, mdev->tconn->data.socket, &p, sizeof(p), 0));
1356 ok = ok && (digest_size == drbd_send(mdev->tconn, mdev->tconn->data.socket, digest, digest_size, 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001357
Philipp Reisnere42325a2011-01-19 13:55:45 +01001358 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001359
1360 return ok;
1361}
1362
1363int drbd_send_ov_request(struct drbd_conf *mdev, sector_t sector, int size)
1364{
1365 int ok;
1366 struct p_block_req p;
1367
1368 p.sector = cpu_to_be64(sector);
Andreas Gruenbacher9a8e7752011-01-11 14:04:09 +01001369 p.block_id = ID_SYNCER /* unused */;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001370 p.blksize = cpu_to_be32(size);
1371
Philipp Reisnerc0129492011-01-19 16:58:16 +01001372 ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_OV_REQUEST, &p.head, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001373 return ok;
1374}
1375
1376/* called on sndtimeo
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001377 * returns false if we should retry,
1378 * true if we think connection is dead
Philipp Reisnerb411b362009-09-25 16:07:19 -07001379 */
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001380static int we_should_drop_the_connection(struct drbd_tconn *tconn, struct socket *sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001381{
1382 int drop_it;
1383 /* long elapsed = (long)(jiffies - mdev->last_received); */
1384
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001385 drop_it = tconn->meta.socket == sock
1386 || !tconn->asender.task
1387 || get_t_state(&tconn->asender) != RUNNING
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001388 || tconn->cstate < C_WF_REPORT_PARAMS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001389
1390 if (drop_it)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001391 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001392
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001393 drop_it = !--tconn->ko_count;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001394 if (!drop_it) {
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001395 conn_err(tconn, "[%s/%d] sock_sendmsg time expired, ko = %u\n",
1396 current->comm, current->pid, tconn->ko_count);
1397 request_ping(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001398 }
1399
1400 return drop_it; /* && (mdev->state == R_PRIMARY) */;
1401}
1402
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001403static void drbd_update_congested(struct drbd_tconn *tconn)
Andreas Gruenbacher9e204cd2011-01-26 18:45:11 +01001404{
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001405 struct sock *sk = tconn->data.socket->sk;
Andreas Gruenbacher9e204cd2011-01-26 18:45:11 +01001406 if (sk->sk_wmem_queued > sk->sk_sndbuf * 4 / 5)
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001407 set_bit(NET_CONGESTED, &tconn->flags);
Andreas Gruenbacher9e204cd2011-01-26 18:45:11 +01001408}
1409
Philipp Reisnerb411b362009-09-25 16:07:19 -07001410/* The idea of sendpage seems to be to put some kind of reference
1411 * to the page into the skb, and to hand it over to the NIC. In
1412 * this process get_page() gets called.
1413 *
1414 * As soon as the page was really sent over the network put_page()
1415 * gets called by some part of the network layer. [ NIC driver? ]
1416 *
1417 * [ get_page() / put_page() increment/decrement the count. If count
1418 * reaches 0 the page will be freed. ]
1419 *
1420 * This works nicely with pages from FSs.
1421 * But this means that in protocol A we might signal IO completion too early!
1422 *
1423 * In order not to corrupt data during a resync we must make sure
1424 * that we do not reuse our own buffer pages (EEs) to early, therefore
1425 * we have the net_ee list.
1426 *
1427 * XFS seems to have problems, still, it submits pages with page_count == 0!
1428 * As a workaround, we disable sendpage on pages
1429 * with page_count == 0 or PageSlab.
1430 */
1431static int _drbd_no_send_page(struct drbd_conf *mdev, struct page *page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001432 int offset, size_t size, unsigned msg_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001433{
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001434 int sent = drbd_send(mdev->tconn, mdev->tconn->data.socket, kmap(page) + offset, size, msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001435 kunmap(page);
1436 if (sent == size)
1437 mdev->send_cnt += size>>9;
1438 return sent == size;
1439}
1440
1441static int _drbd_send_page(struct drbd_conf *mdev, struct page *page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001442 int offset, size_t size, unsigned msg_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001443{
1444 mm_segment_t oldfs = get_fs();
1445 int sent, ok;
1446 int len = size;
1447
1448 /* e.g. XFS meta- & log-data is in slab pages, which have a
1449 * page_count of 0 and/or have PageSlab() set.
1450 * we cannot use send_page for those, as that does get_page();
1451 * put_page(); and would cause either a VM_BUG directly, or
1452 * __page_cache_release a page that would actually still be referenced
1453 * by someone, leading to some obscure delayed Oops somewhere else. */
1454 if (disable_sendpage || (page_count(page) < 1) || PageSlab(page))
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001455 return _drbd_no_send_page(mdev, page, offset, size, msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001456
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001457 msg_flags |= MSG_NOSIGNAL;
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001458 drbd_update_congested(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001459 set_fs(KERNEL_DS);
1460 do {
Philipp Reisnere42325a2011-01-19 13:55:45 +01001461 sent = mdev->tconn->data.socket->ops->sendpage(mdev->tconn->data.socket, page,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001462 offset, len,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001463 msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001464 if (sent == -EAGAIN) {
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001465 if (we_should_drop_the_connection(mdev->tconn,
Philipp Reisnere42325a2011-01-19 13:55:45 +01001466 mdev->tconn->data.socket))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001467 break;
1468 else
1469 continue;
1470 }
1471 if (sent <= 0) {
1472 dev_warn(DEV, "%s: size=%d len=%d sent=%d\n",
1473 __func__, (int)size, len, sent);
1474 break;
1475 }
1476 len -= sent;
1477 offset += sent;
1478 } while (len > 0 /* THINK && mdev->cstate >= C_CONNECTED*/);
1479 set_fs(oldfs);
Philipp Reisner01a311a2011-02-07 14:30:33 +01001480 clear_bit(NET_CONGESTED, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001481
1482 ok = (len == 0);
1483 if (likely(ok))
1484 mdev->send_cnt += size>>9;
1485 return ok;
1486}
1487
1488static int _drbd_send_bio(struct drbd_conf *mdev, struct bio *bio)
1489{
1490 struct bio_vec *bvec;
1491 int i;
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001492 /* hint all but last page with MSG_MORE */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001493 __bio_for_each_segment(bvec, bio, i, 0) {
1494 if (!_drbd_no_send_page(mdev, bvec->bv_page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001495 bvec->bv_offset, bvec->bv_len,
1496 i == bio->bi_vcnt -1 ? 0 : MSG_MORE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001497 return 0;
1498 }
1499 return 1;
1500}
1501
1502static int _drbd_send_zc_bio(struct drbd_conf *mdev, struct bio *bio)
1503{
1504 struct bio_vec *bvec;
1505 int i;
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001506 /* hint all but last page with MSG_MORE */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001507 __bio_for_each_segment(bvec, bio, i, 0) {
1508 if (!_drbd_send_page(mdev, bvec->bv_page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001509 bvec->bv_offset, bvec->bv_len,
1510 i == bio->bi_vcnt -1 ? 0 : MSG_MORE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001511 return 0;
1512 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001513 return 1;
1514}
1515
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001516static int _drbd_send_zc_ee(struct drbd_conf *mdev,
1517 struct drbd_peer_request *peer_req)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001518{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001519 struct page *page = peer_req->pages;
1520 unsigned len = peer_req->i.size;
1521
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001522 /* hint all but last page with MSG_MORE */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001523 page_chain_for_each(page) {
1524 unsigned l = min_t(unsigned, len, PAGE_SIZE);
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001525 if (!_drbd_send_page(mdev, page, 0, l,
1526 page_chain_next(page) ? MSG_MORE : 0))
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001527 return 0;
1528 len -= l;
1529 }
1530 return 1;
1531}
1532
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001533static u32 bio_flags_to_wire(struct drbd_conf *mdev, unsigned long bi_rw)
1534{
Philipp Reisner31890f42011-01-19 14:12:51 +01001535 if (mdev->tconn->agreed_pro_version >= 95)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001536 return (bi_rw & REQ_SYNC ? DP_RW_SYNC : 0) |
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001537 (bi_rw & REQ_FUA ? DP_FUA : 0) |
1538 (bi_rw & REQ_FLUSH ? DP_FLUSH : 0) |
1539 (bi_rw & REQ_DISCARD ? DP_DISCARD : 0);
1540 else
Jens Axboe721a9602011-03-09 11:56:30 +01001541 return bi_rw & REQ_SYNC ? DP_RW_SYNC : 0;
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001542}
1543
Philipp Reisnerb411b362009-09-25 16:07:19 -07001544/* Used to send write requests
1545 * R_PRIMARY -> Peer (P_DATA)
1546 */
1547int drbd_send_dblock(struct drbd_conf *mdev, struct drbd_request *req)
1548{
1549 int ok = 1;
1550 struct p_data p;
1551 unsigned int dp_flags = 0;
1552 void *dgb;
1553 int dgs;
1554
Philipp Reisner61120872011-02-08 09:50:54 +01001555 if (!drbd_get_data_sock(mdev->tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001556 return 0;
1557
Philipp Reisnera0638452011-01-19 14:31:32 +01001558 dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_w_tfm) ?
1559 crypto_hash_digestsize(mdev->tconn->integrity_w_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001560
Philipp Reisnerfd340c12011-01-19 16:57:39 +01001561 prepare_header(mdev, &p.head, P_DATA, sizeof(p) - sizeof(struct p_header) + dgs + req->i.size);
Andreas Gruenbacherace652a2011-01-03 17:09:58 +01001562 p.sector = cpu_to_be64(req->i.sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001563 p.block_id = (unsigned long)req;
Andreas Gruenbacher8ccf2182011-02-24 11:35:43 +01001564 p.seq_num = cpu_to_be32(req->seq_num = atomic_inc_return(&mdev->packet_seq));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001565
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001566 dp_flags = bio_flags_to_wire(mdev, req->master_bio->bi_rw);
1567
Philipp Reisnerb411b362009-09-25 16:07:19 -07001568 if (mdev->state.conn >= C_SYNC_SOURCE &&
1569 mdev->state.conn <= C_PAUSED_SYNC_T)
1570 dp_flags |= DP_MAY_SET_IN_SYNC;
1571
1572 p.dp_flags = cpu_to_be32(dp_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001573 set_bit(UNPLUG_REMOTE, &mdev->flags);
1574 ok = (sizeof(p) ==
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001575 drbd_send(mdev->tconn, mdev->tconn->data.socket, &p, sizeof(p), dgs ? MSG_MORE : 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001576 if (ok && dgs) {
Philipp Reisnera0638452011-01-19 14:31:32 +01001577 dgb = mdev->tconn->int_dig_out;
1578 drbd_csum_bio(mdev, mdev->tconn->integrity_w_tfm, req->master_bio, dgb);
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001579 ok = dgs == drbd_send(mdev->tconn, mdev->tconn->data.socket, dgb, dgs, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001580 }
1581 if (ok) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001582 /* For protocol A, we have to memcpy the payload into
1583 * socket buffers, as we may complete right away
1584 * as soon as we handed it over to tcp, at which point the data
1585 * pages may become invalid.
1586 *
1587 * For data-integrity enabled, we copy it as well, so we can be
1588 * sure that even if the bio pages may still be modified, it
1589 * won't change the data on the wire, thus if the digest checks
1590 * out ok after sending on this side, but does not fit on the
1591 * receiving side, we sure have detected corruption elsewhere.
1592 */
Philipp Reisner89e58e72011-01-19 13:12:45 +01001593 if (mdev->tconn->net_conf->wire_protocol == DRBD_PROT_A || dgs)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001594 ok = _drbd_send_bio(mdev, req->master_bio);
1595 else
1596 ok = _drbd_send_zc_bio(mdev, req->master_bio);
Lars Ellenberg470be442010-11-10 10:36:52 +01001597
1598 /* double check digest, sometimes buffers have been modified in flight. */
1599 if (dgs > 0 && dgs <= 64) {
Bart Van Assche24c48302011-05-21 18:32:29 +02001600 /* 64 byte, 512 bit, is the largest digest size
Lars Ellenberg470be442010-11-10 10:36:52 +01001601 * currently supported in kernel crypto. */
1602 unsigned char digest[64];
Philipp Reisnera0638452011-01-19 14:31:32 +01001603 drbd_csum_bio(mdev, mdev->tconn->integrity_w_tfm, req->master_bio, digest);
1604 if (memcmp(mdev->tconn->int_dig_out, digest, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001605 dev_warn(DEV,
1606 "Digest mismatch, buffer modified by upper layers during write: %llus +%u\n",
Andreas Gruenbacherace652a2011-01-03 17:09:58 +01001607 (unsigned long long)req->i.sector, req->i.size);
Lars Ellenberg470be442010-11-10 10:36:52 +01001608 }
1609 } /* else if (dgs > 64) {
1610 ... Be noisy about digest too large ...
1611 } */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001612 }
1613
Philipp Reisner61120872011-02-08 09:50:54 +01001614 drbd_put_data_sock(mdev->tconn);
Philipp Reisnerbd26bfc52010-05-04 12:33:58 +02001615
Philipp Reisnerb411b362009-09-25 16:07:19 -07001616 return ok;
1617}
1618
1619/* answer packet, used to send data back for read requests:
1620 * Peer -> (diskless) R_PRIMARY (P_DATA_REPLY)
1621 * C_SYNC_SOURCE -> C_SYNC_TARGET (P_RS_DATA_REPLY)
1622 */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001623int drbd_send_block(struct drbd_conf *mdev, enum drbd_packet cmd,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001624 struct drbd_peer_request *peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001625{
1626 int ok;
1627 struct p_data p;
1628 void *dgb;
1629 int dgs;
1630
Philipp Reisnera0638452011-01-19 14:31:32 +01001631 dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_w_tfm) ?
1632 crypto_hash_digestsize(mdev->tconn->integrity_w_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001633
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001634 prepare_header(mdev, &p.head, cmd, sizeof(p) -
1635 sizeof(struct p_header80) +
1636 dgs + peer_req->i.size);
1637 p.sector = cpu_to_be64(peer_req->i.sector);
1638 p.block_id = peer_req->block_id;
Andreas Gruenbachercc378272011-01-26 18:01:50 +01001639 p.seq_num = 0; /* unused */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001640
1641 /* Only called by our kernel thread.
1642 * This one may be interrupted by DRBD_SIG and/or DRBD_SIGKILL
1643 * in response to admin command or module unload.
1644 */
Philipp Reisner61120872011-02-08 09:50:54 +01001645 if (!drbd_get_data_sock(mdev->tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001646 return 0;
1647
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001648 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 -07001649 if (ok && dgs) {
Philipp Reisnera0638452011-01-19 14:31:32 +01001650 dgb = mdev->tconn->int_dig_out;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001651 drbd_csum_ee(mdev, mdev->tconn->integrity_w_tfm, peer_req, dgb);
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001652 ok = dgs == drbd_send(mdev->tconn, mdev->tconn->data.socket, dgb, dgs, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001653 }
1654 if (ok)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001655 ok = _drbd_send_zc_ee(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001656
Philipp Reisner61120872011-02-08 09:50:54 +01001657 drbd_put_data_sock(mdev->tconn);
Philipp Reisnerbd26bfc52010-05-04 12:33:58 +02001658
Philipp Reisnerb411b362009-09-25 16:07:19 -07001659 return ok;
1660}
1661
Philipp Reisner73a01a12010-10-27 14:33:00 +02001662int drbd_send_oos(struct drbd_conf *mdev, struct drbd_request *req)
1663{
1664 struct p_block_desc p;
1665
Andreas Gruenbacherace652a2011-01-03 17:09:58 +01001666 p.sector = cpu_to_be64(req->i.sector);
1667 p.blksize = cpu_to_be32(req->i.size);
Philipp Reisner73a01a12010-10-27 14:33:00 +02001668
1669 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_OUT_OF_SYNC, &p.head, sizeof(p));
1670}
1671
Philipp Reisnerb411b362009-09-25 16:07:19 -07001672/*
1673 drbd_send distinguishes two cases:
1674
1675 Packets sent via the data socket "sock"
1676 and packets sent via the meta data socket "msock"
1677
1678 sock msock
1679 -----------------+-------------------------+------------------------------
1680 timeout conf.timeout / 2 conf.timeout / 2
1681 timeout action send a ping via msock Abort communication
1682 and close all sockets
1683*/
1684
1685/*
1686 * you must have down()ed the appropriate [m]sock_mutex elsewhere!
1687 */
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001688int drbd_send(struct drbd_tconn *tconn, struct socket *sock,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001689 void *buf, size_t size, unsigned msg_flags)
1690{
1691 struct kvec iov;
1692 struct msghdr msg;
1693 int rv, sent = 0;
1694
1695 if (!sock)
1696 return -1000;
1697
1698 /* THINK if (signal_pending) return ... ? */
1699
1700 iov.iov_base = buf;
1701 iov.iov_len = size;
1702
1703 msg.msg_name = NULL;
1704 msg.msg_namelen = 0;
1705 msg.msg_control = NULL;
1706 msg.msg_controllen = 0;
1707 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
1708
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001709 if (sock == tconn->data.socket) {
1710 tconn->ko_count = tconn->net_conf->ko_count;
1711 drbd_update_congested(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001712 }
1713 do {
1714 /* STRANGE
1715 * tcp_sendmsg does _not_ use its size parameter at all ?
1716 *
1717 * -EAGAIN on timeout, -EINTR on signal.
1718 */
1719/* THINK
1720 * do we need to block DRBD_SIG if sock == &meta.socket ??
1721 * otherwise wake_asender() might interrupt some send_*Ack !
1722 */
1723 rv = kernel_sendmsg(sock, &msg, &iov, 1, size);
1724 if (rv == -EAGAIN) {
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001725 if (we_should_drop_the_connection(tconn, sock))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001726 break;
1727 else
1728 continue;
1729 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001730 if (rv == -EINTR) {
1731 flush_signals(current);
1732 rv = 0;
1733 }
1734 if (rv < 0)
1735 break;
1736 sent += rv;
1737 iov.iov_base += rv;
1738 iov.iov_len -= rv;
1739 } while (sent < size);
1740
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001741 if (sock == tconn->data.socket)
1742 clear_bit(NET_CONGESTED, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001743
1744 if (rv <= 0) {
1745 if (rv != -EAGAIN) {
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001746 conn_err(tconn, "%s_sendmsg returned %d\n",
1747 sock == tconn->meta.socket ? "msock" : "sock",
1748 rv);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001749 conn_request_state(tconn, NS(conn, C_BROKEN_PIPE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001750 } else
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001751 conn_request_state(tconn, NS(conn, C_TIMEOUT), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001752 }
1753
1754 return sent;
1755}
1756
1757static int drbd_open(struct block_device *bdev, fmode_t mode)
1758{
1759 struct drbd_conf *mdev = bdev->bd_disk->private_data;
1760 unsigned long flags;
1761 int rv = 0;
1762
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001763 mutex_lock(&drbd_main_mutex);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001764 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001765 /* to have a stable mdev->state.role
1766 * and no race with updating open_cnt */
1767
1768 if (mdev->state.role != R_PRIMARY) {
1769 if (mode & FMODE_WRITE)
1770 rv = -EROFS;
1771 else if (!allow_oos)
1772 rv = -EMEDIUMTYPE;
1773 }
1774
1775 if (!rv)
1776 mdev->open_cnt++;
Philipp Reisner87eeee42011-01-19 14:16:30 +01001777 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001778 mutex_unlock(&drbd_main_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001779
1780 return rv;
1781}
1782
1783static int drbd_release(struct gendisk *gd, fmode_t mode)
1784{
1785 struct drbd_conf *mdev = gd->private_data;
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001786 mutex_lock(&drbd_main_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001787 mdev->open_cnt--;
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001788 mutex_unlock(&drbd_main_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001789 return 0;
1790}
1791
Philipp Reisnerb411b362009-09-25 16:07:19 -07001792static void drbd_set_defaults(struct drbd_conf *mdev)
1793{
Philipp Reisner85f4cc12010-06-29 17:35:34 +02001794 /* This way we get a compile error when sync_conf grows,
1795 and we forgot to initialize it here */
1796 mdev->sync_conf = (struct syncer_conf) {
1797 /* .rate = */ DRBD_RATE_DEF,
1798 /* .after = */ DRBD_AFTER_DEF,
1799 /* .al_extents = */ DRBD_AL_EXTENTS_DEF,
Philipp Reisner85f4cc12010-06-29 17:35:34 +02001800 /* .verify_alg = */ {}, 0,
1801 /* .cpu_mask = */ {}, 0,
1802 /* .csums_alg = */ {}, 0,
Philipp Reisnere7564142010-06-29 17:35:34 +02001803 /* .use_rle = */ 0,
Philipp Reisner9a31d712010-07-05 13:42:03 +02001804 /* .on_no_data = */ DRBD_ON_NO_DATA_DEF,
1805 /* .c_plan_ahead = */ DRBD_C_PLAN_AHEAD_DEF,
1806 /* .c_delay_target = */ DRBD_C_DELAY_TARGET_DEF,
1807 /* .c_fill_target = */ DRBD_C_FILL_TARGET_DEF,
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001808 /* .c_max_rate = */ DRBD_C_MAX_RATE_DEF,
1809 /* .c_min_rate = */ DRBD_C_MIN_RATE_DEF
Philipp Reisner85f4cc12010-06-29 17:35:34 +02001810 };
1811
1812 /* Have to use that way, because the layout differs between
1813 big endian and little endian */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001814 mdev->state = (union drbd_state) {
1815 { .role = R_SECONDARY,
1816 .peer = R_UNKNOWN,
1817 .conn = C_STANDALONE,
1818 .disk = D_DISKLESS,
1819 .pdsk = D_UNKNOWN,
Philipp Reisnerfb22c402010-09-08 23:20:21 +02001820 .susp = 0,
1821 .susp_nod = 0,
1822 .susp_fen = 0
Philipp Reisnerb411b362009-09-25 16:07:19 -07001823 } };
1824}
1825
1826void drbd_init_set_defaults(struct drbd_conf *mdev)
1827{
1828 /* the memset(,0,) did most of this.
1829 * note: only assignments, no allocation in here */
1830
1831 drbd_set_defaults(mdev);
1832
Philipp Reisnerb411b362009-09-25 16:07:19 -07001833 atomic_set(&mdev->ap_bio_cnt, 0);
1834 atomic_set(&mdev->ap_pending_cnt, 0);
1835 atomic_set(&mdev->rs_pending_cnt, 0);
1836 atomic_set(&mdev->unacked_cnt, 0);
1837 atomic_set(&mdev->local_cnt, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001838 atomic_set(&mdev->pp_in_use, 0);
Lars Ellenberg435f0742010-09-06 12:30:25 +02001839 atomic_set(&mdev->pp_in_use_by_net, 0);
Philipp Reisner778f2712010-07-06 11:14:00 +02001840 atomic_set(&mdev->rs_sect_in, 0);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001841 atomic_set(&mdev->rs_sect_ev, 0);
Philipp Reisner759fbdf2010-10-26 16:02:27 +02001842 atomic_set(&mdev->ap_in_flight, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001843
1844 mutex_init(&mdev->md_io_mutex);
Philipp Reisner8410da8f02011-02-11 20:11:10 +01001845 mutex_init(&mdev->own_state_mutex);
1846 mdev->state_mutex = &mdev->own_state_mutex;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001847
Philipp Reisnerb411b362009-09-25 16:07:19 -07001848 spin_lock_init(&mdev->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001849 spin_lock_init(&mdev->peer_seq_lock);
1850 spin_lock_init(&mdev->epoch_lock);
1851
1852 INIT_LIST_HEAD(&mdev->active_ee);
1853 INIT_LIST_HEAD(&mdev->sync_ee);
1854 INIT_LIST_HEAD(&mdev->done_ee);
1855 INIT_LIST_HEAD(&mdev->read_ee);
1856 INIT_LIST_HEAD(&mdev->net_ee);
1857 INIT_LIST_HEAD(&mdev->resync_reads);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001858 INIT_LIST_HEAD(&mdev->resync_work.list);
1859 INIT_LIST_HEAD(&mdev->unplug_work.list);
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02001860 INIT_LIST_HEAD(&mdev->go_diskless.list);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001861 INIT_LIST_HEAD(&mdev->md_sync_work.list);
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02001862 INIT_LIST_HEAD(&mdev->start_resync_work.list);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001863 INIT_LIST_HEAD(&mdev->bm_io_work.w.list);
Philipp Reisner0ced55a2010-04-30 15:26:20 +02001864
Philipp Reisner794abb72010-12-27 11:51:23 +01001865 mdev->resync_work.cb = w_resync_timer;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001866 mdev->unplug_work.cb = w_send_write_hint;
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02001867 mdev->go_diskless.cb = w_go_diskless;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001868 mdev->md_sync_work.cb = w_md_sync;
1869 mdev->bm_io_work.w.cb = w_bitmap_io;
Philipp Reisner370a43e2011-01-14 16:03:11 +01001870 mdev->start_resync_work.cb = w_start_resync;
Philipp Reisnera21e9292011-02-08 15:08:49 +01001871
1872 mdev->resync_work.mdev = mdev;
1873 mdev->unplug_work.mdev = mdev;
1874 mdev->go_diskless.mdev = mdev;
1875 mdev->md_sync_work.mdev = mdev;
1876 mdev->bm_io_work.w.mdev = mdev;
1877 mdev->start_resync_work.mdev = mdev;
1878
Philipp Reisnerb411b362009-09-25 16:07:19 -07001879 init_timer(&mdev->resync_timer);
1880 init_timer(&mdev->md_sync_timer);
Philipp Reisner370a43e2011-01-14 16:03:11 +01001881 init_timer(&mdev->start_resync_timer);
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001882 init_timer(&mdev->request_timer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001883 mdev->resync_timer.function = resync_timer_fn;
1884 mdev->resync_timer.data = (unsigned long) mdev;
1885 mdev->md_sync_timer.function = md_sync_timer_fn;
1886 mdev->md_sync_timer.data = (unsigned long) mdev;
Philipp Reisner370a43e2011-01-14 16:03:11 +01001887 mdev->start_resync_timer.function = start_resync_timer_fn;
1888 mdev->start_resync_timer.data = (unsigned long) mdev;
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001889 mdev->request_timer.function = request_timer_fn;
1890 mdev->request_timer.data = (unsigned long) mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001891
1892 init_waitqueue_head(&mdev->misc_wait);
1893 init_waitqueue_head(&mdev->state_wait);
1894 init_waitqueue_head(&mdev->ee_wait);
1895 init_waitqueue_head(&mdev->al_wait);
1896 init_waitqueue_head(&mdev->seq_wait);
1897
Philipp Reisnerfd340c12011-01-19 16:57:39 +01001898 /* mdev->tconn->agreed_pro_version gets initialized in drbd_connect() */
Philipp Reisner2451fc32010-08-24 13:43:11 +02001899 mdev->write_ordering = WO_bdev_flush;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001900 mdev->resync_wenr = LC_FREE;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001901 mdev->peer_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
1902 mdev->local_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001903}
1904
1905void drbd_mdev_cleanup(struct drbd_conf *mdev)
1906{
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001907 int i;
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01001908 if (mdev->tconn->receiver.t_state != NONE)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001909 dev_err(DEV, "ASSERT FAILED: receiver t_state == %d expected 0.\n",
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01001910 mdev->tconn->receiver.t_state);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001911
1912 /* no need to lock it, I'm the only thread alive */
1913 if (atomic_read(&mdev->current_epoch->epoch_size) != 0)
1914 dev_err(DEV, "epoch_size:%d\n", atomic_read(&mdev->current_epoch->epoch_size));
1915 mdev->al_writ_cnt =
1916 mdev->bm_writ_cnt =
1917 mdev->read_cnt =
1918 mdev->recv_cnt =
1919 mdev->send_cnt =
1920 mdev->writ_cnt =
1921 mdev->p_size =
1922 mdev->rs_start =
1923 mdev->rs_total =
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001924 mdev->rs_failed = 0;
1925 mdev->rs_last_events = 0;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001926 mdev->rs_last_sect_ev = 0;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001927 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
1928 mdev->rs_mark_left[i] = 0;
1929 mdev->rs_mark_time[i] = 0;
1930 }
Philipp Reisner89e58e72011-01-19 13:12:45 +01001931 D_ASSERT(mdev->tconn->net_conf == NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001932
1933 drbd_set_my_capacity(mdev, 0);
1934 if (mdev->bitmap) {
1935 /* maybe never allocated. */
Philipp Reisner02d9a942010-03-24 16:23:03 +01001936 drbd_bm_resize(mdev, 0, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001937 drbd_bm_cleanup(mdev);
1938 }
1939
1940 drbd_free_resources(mdev);
Philipp Reisner07782862010-08-31 12:00:50 +02001941 clear_bit(AL_SUSPENDED, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001942
1943 /*
1944 * currently we drbd_init_ee only on module load, so
1945 * we may do drbd_release_ee only on module unload!
1946 */
1947 D_ASSERT(list_empty(&mdev->active_ee));
1948 D_ASSERT(list_empty(&mdev->sync_ee));
1949 D_ASSERT(list_empty(&mdev->done_ee));
1950 D_ASSERT(list_empty(&mdev->read_ee));
1951 D_ASSERT(list_empty(&mdev->net_ee));
1952 D_ASSERT(list_empty(&mdev->resync_reads));
Philipp Reisnere42325a2011-01-19 13:55:45 +01001953 D_ASSERT(list_empty(&mdev->tconn->data.work.q));
1954 D_ASSERT(list_empty(&mdev->tconn->meta.work.q));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001955 D_ASSERT(list_empty(&mdev->resync_work.list));
1956 D_ASSERT(list_empty(&mdev->unplug_work.list));
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02001957 D_ASSERT(list_empty(&mdev->go_diskless.list));
Lars Ellenberg2265b472010-12-16 15:41:26 +01001958
1959 drbd_set_defaults(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001960}
1961
1962
1963static void drbd_destroy_mempools(void)
1964{
1965 struct page *page;
1966
1967 while (drbd_pp_pool) {
1968 page = drbd_pp_pool;
1969 drbd_pp_pool = (struct page *)page_private(page);
1970 __free_page(page);
1971 drbd_pp_vacant--;
1972 }
1973
1974 /* D_ASSERT(atomic_read(&drbd_pp_vacant)==0); */
1975
Lars Ellenbergda4a75d2011-02-23 17:02:01 +01001976 if (drbd_md_io_bio_set)
1977 bioset_free(drbd_md_io_bio_set);
Lars Ellenberg35abf592011-02-23 12:39:46 +01001978 if (drbd_md_io_page_pool)
1979 mempool_destroy(drbd_md_io_page_pool);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001980 if (drbd_ee_mempool)
1981 mempool_destroy(drbd_ee_mempool);
1982 if (drbd_request_mempool)
1983 mempool_destroy(drbd_request_mempool);
1984 if (drbd_ee_cache)
1985 kmem_cache_destroy(drbd_ee_cache);
1986 if (drbd_request_cache)
1987 kmem_cache_destroy(drbd_request_cache);
1988 if (drbd_bm_ext_cache)
1989 kmem_cache_destroy(drbd_bm_ext_cache);
1990 if (drbd_al_ext_cache)
1991 kmem_cache_destroy(drbd_al_ext_cache);
1992
Lars Ellenbergda4a75d2011-02-23 17:02:01 +01001993 drbd_md_io_bio_set = NULL;
Lars Ellenberg35abf592011-02-23 12:39:46 +01001994 drbd_md_io_page_pool = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001995 drbd_ee_mempool = NULL;
1996 drbd_request_mempool = NULL;
1997 drbd_ee_cache = NULL;
1998 drbd_request_cache = NULL;
1999 drbd_bm_ext_cache = NULL;
2000 drbd_al_ext_cache = NULL;
2001
2002 return;
2003}
2004
2005static int drbd_create_mempools(void)
2006{
2007 struct page *page;
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01002008 const int number = (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * minor_count;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002009 int i;
2010
2011 /* prepare our caches and mempools */
2012 drbd_request_mempool = NULL;
2013 drbd_ee_cache = NULL;
2014 drbd_request_cache = NULL;
2015 drbd_bm_ext_cache = NULL;
2016 drbd_al_ext_cache = NULL;
2017 drbd_pp_pool = NULL;
Lars Ellenberg35abf592011-02-23 12:39:46 +01002018 drbd_md_io_page_pool = NULL;
Lars Ellenbergda4a75d2011-02-23 17:02:01 +01002019 drbd_md_io_bio_set = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002020
2021 /* caches */
2022 drbd_request_cache = kmem_cache_create(
2023 "drbd_req", sizeof(struct drbd_request), 0, 0, NULL);
2024 if (drbd_request_cache == NULL)
2025 goto Enomem;
2026
2027 drbd_ee_cache = kmem_cache_create(
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01002028 "drbd_ee", sizeof(struct drbd_peer_request), 0, 0, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002029 if (drbd_ee_cache == NULL)
2030 goto Enomem;
2031
2032 drbd_bm_ext_cache = kmem_cache_create(
2033 "drbd_bm", sizeof(struct bm_extent), 0, 0, NULL);
2034 if (drbd_bm_ext_cache == NULL)
2035 goto Enomem;
2036
2037 drbd_al_ext_cache = kmem_cache_create(
2038 "drbd_al", sizeof(struct lc_element), 0, 0, NULL);
2039 if (drbd_al_ext_cache == NULL)
2040 goto Enomem;
2041
2042 /* mempools */
Lars Ellenbergda4a75d2011-02-23 17:02:01 +01002043 drbd_md_io_bio_set = bioset_create(DRBD_MIN_POOL_PAGES, 0);
2044 if (drbd_md_io_bio_set == NULL)
2045 goto Enomem;
2046
Lars Ellenberg35abf592011-02-23 12:39:46 +01002047 drbd_md_io_page_pool = mempool_create_page_pool(DRBD_MIN_POOL_PAGES, 0);
2048 if (drbd_md_io_page_pool == NULL)
2049 goto Enomem;
2050
Philipp Reisnerb411b362009-09-25 16:07:19 -07002051 drbd_request_mempool = mempool_create(number,
2052 mempool_alloc_slab, mempool_free_slab, drbd_request_cache);
2053 if (drbd_request_mempool == NULL)
2054 goto Enomem;
2055
2056 drbd_ee_mempool = mempool_create(number,
2057 mempool_alloc_slab, mempool_free_slab, drbd_ee_cache);
Nicolas Kaiser2027ae12010-10-28 06:15:26 -06002058 if (drbd_ee_mempool == NULL)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002059 goto Enomem;
2060
2061 /* drbd's page pool */
2062 spin_lock_init(&drbd_pp_lock);
2063
2064 for (i = 0; i < number; i++) {
2065 page = alloc_page(GFP_HIGHUSER);
2066 if (!page)
2067 goto Enomem;
2068 set_page_private(page, (unsigned long)drbd_pp_pool);
2069 drbd_pp_pool = page;
2070 }
2071 drbd_pp_vacant = number;
2072
2073 return 0;
2074
2075Enomem:
2076 drbd_destroy_mempools(); /* in case we allocated some */
2077 return -ENOMEM;
2078}
2079
2080static int drbd_notify_sys(struct notifier_block *this, unsigned long code,
2081 void *unused)
2082{
2083 /* just so we have it. you never know what interesting things we
2084 * might want to do here some day...
2085 */
2086
2087 return NOTIFY_DONE;
2088}
2089
2090static struct notifier_block drbd_notifier = {
2091 .notifier_call = drbd_notify_sys,
2092};
2093
2094static void drbd_release_ee_lists(struct drbd_conf *mdev)
2095{
2096 int rr;
2097
2098 rr = drbd_release_ee(mdev, &mdev->active_ee);
2099 if (rr)
2100 dev_err(DEV, "%d EEs in active list found!\n", rr);
2101
2102 rr = drbd_release_ee(mdev, &mdev->sync_ee);
2103 if (rr)
2104 dev_err(DEV, "%d EEs in sync list found!\n", rr);
2105
2106 rr = drbd_release_ee(mdev, &mdev->read_ee);
2107 if (rr)
2108 dev_err(DEV, "%d EEs in read list found!\n", rr);
2109
2110 rr = drbd_release_ee(mdev, &mdev->done_ee);
2111 if (rr)
2112 dev_err(DEV, "%d EEs in done list found!\n", rr);
2113
2114 rr = drbd_release_ee(mdev, &mdev->net_ee);
2115 if (rr)
2116 dev_err(DEV, "%d EEs in net list found!\n", rr);
2117}
2118
Philipp Reisner774b3052011-02-22 02:07:03 -05002119/* caution. no locking. */
2120void drbd_delete_device(unsigned int minor)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002121{
2122 struct drbd_conf *mdev = minor_to_mdev(minor);
2123
2124 if (!mdev)
2125 return;
2126
Philipp Reisner774b3052011-02-22 02:07:03 -05002127 idr_remove(&mdev->tconn->volumes, minor);
2128
Philipp Reisnerb411b362009-09-25 16:07:19 -07002129 /* paranoia asserts */
Andreas Gruenbacher70dc65e2010-12-21 14:46:57 +01002130 D_ASSERT(mdev->open_cnt == 0);
Philipp Reisnere42325a2011-01-19 13:55:45 +01002131 D_ASSERT(list_empty(&mdev->tconn->data.work.q));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002132 /* end paranoia asserts */
2133
2134 del_gendisk(mdev->vdisk);
2135
2136 /* cleanup stuff that may have been allocated during
2137 * device (re-)configuration or state changes */
2138
2139 if (mdev->this_bdev)
2140 bdput(mdev->this_bdev);
2141
2142 drbd_free_resources(mdev);
2143
2144 drbd_release_ee_lists(mdev);
2145
Philipp Reisnerb411b362009-09-25 16:07:19 -07002146 lc_destroy(mdev->act_log);
2147 lc_destroy(mdev->resync);
2148
2149 kfree(mdev->p_uuid);
2150 /* mdev->p_uuid = NULL; */
2151
Philipp Reisnerb411b362009-09-25 16:07:19 -07002152 /* cleanup the rest that has been
2153 * allocated from drbd_new_device
2154 * and actually free the mdev itself */
2155 drbd_free_mdev(mdev);
Philipp Reisner81a5d602011-02-22 19:53:16 -05002156 idr_remove(&minors, minor);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002157}
2158
2159static void drbd_cleanup(void)
2160{
2161 unsigned int i;
Philipp Reisner81a5d602011-02-22 19:53:16 -05002162 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002163
2164 unregister_reboot_notifier(&drbd_notifier);
2165
Lars Ellenberg17a93f32010-11-24 10:37:35 +01002166 /* first remove proc,
2167 * drbdsetup uses it's presence to detect
2168 * whether DRBD is loaded.
2169 * If we would get stuck in proc removal,
2170 * but have netlink already deregistered,
2171 * some drbdsetup commands may wait forever
2172 * for an answer.
2173 */
2174 if (drbd_proc)
2175 remove_proc_entry("drbd", NULL);
2176
Philipp Reisnerb411b362009-09-25 16:07:19 -07002177 drbd_nl_cleanup();
2178
Philipp Reisner81a5d602011-02-22 19:53:16 -05002179 idr_for_each_entry(&minors, mdev, i)
2180 drbd_delete_device(i);
2181 drbd_destroy_mempools();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002182 unregister_blkdev(DRBD_MAJOR, "drbd");
2183
Philipp Reisner81a5d602011-02-22 19:53:16 -05002184 idr_destroy(&minors);
2185
Philipp Reisnerb411b362009-09-25 16:07:19 -07002186 printk(KERN_INFO "drbd: module cleanup done.\n");
2187}
2188
2189/**
2190 * drbd_congested() - Callback for pdflush
2191 * @congested_data: User data
2192 * @bdi_bits: Bits pdflush is currently interested in
2193 *
2194 * Returns 1<<BDI_async_congested and/or 1<<BDI_sync_congested if we are congested.
2195 */
2196static int drbd_congested(void *congested_data, int bdi_bits)
2197{
2198 struct drbd_conf *mdev = congested_data;
2199 struct request_queue *q;
2200 char reason = '-';
2201 int r = 0;
2202
Andreas Gruenbacher1b881ef2010-12-13 18:03:38 +01002203 if (!may_inc_ap_bio(mdev)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002204 /* DRBD has frozen IO */
2205 r = bdi_bits;
2206 reason = 'd';
2207 goto out;
2208 }
2209
2210 if (get_ldev(mdev)) {
2211 q = bdev_get_queue(mdev->ldev->backing_bdev);
2212 r = bdi_congested(&q->backing_dev_info, bdi_bits);
2213 put_ldev(mdev);
2214 if (r)
2215 reason = 'b';
2216 }
2217
Philipp Reisner01a311a2011-02-07 14:30:33 +01002218 if (bdi_bits & (1 << BDI_async_congested) && test_bit(NET_CONGESTED, &mdev->tconn->flags)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002219 r |= (1 << BDI_async_congested);
2220 reason = reason == 'b' ? 'a' : 'n';
2221 }
2222
2223out:
2224 mdev->congestion_reason = reason;
2225 return r;
2226}
2227
Philipp Reisner6699b652011-02-09 11:10:24 +01002228static void drbd_init_workqueue(struct drbd_work_queue* wq)
2229{
2230 sema_init(&wq->s, 0);
2231 spin_lock_init(&wq->q_lock);
2232 INIT_LIST_HEAD(&wq->q);
2233}
2234
Philipp Reisner1aba4d72011-02-21 15:38:08 +01002235struct drbd_tconn *conn_by_name(const char *name)
2236{
2237 struct drbd_tconn *tconn;
2238
2239 write_lock_irq(&global_state_lock);
2240 list_for_each_entry(tconn, &drbd_tconns, all_tconn) {
2241 if (!strcmp(tconn->name, name))
2242 goto found;
2243 }
2244 tconn = NULL;
2245found:
2246 write_unlock_irq(&global_state_lock);
2247 return tconn;
2248}
2249
Philipp Reisner21114382011-01-19 12:26:59 +01002250struct drbd_tconn *drbd_new_tconn(char *name)
2251{
2252 struct drbd_tconn *tconn;
2253
2254 tconn = kzalloc(sizeof(struct drbd_tconn), GFP_KERNEL);
2255 if (!tconn)
2256 return NULL;
2257
2258 tconn->name = kstrdup(name, GFP_KERNEL);
2259 if (!tconn->name)
2260 goto fail;
2261
Philipp Reisner774b3052011-02-22 02:07:03 -05002262 if (!zalloc_cpumask_var(&tconn->cpu_mask, GFP_KERNEL))
2263 goto fail;
2264
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01002265 if (!tl_init(tconn))
2266 goto fail;
2267
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002268 tconn->cstate = C_STANDALONE;
Philipp Reisner8410da8f02011-02-11 20:11:10 +01002269 mutex_init(&tconn->cstate_mutex);
Philipp Reisner6699b652011-02-09 11:10:24 +01002270 spin_lock_init(&tconn->req_lock);
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01002271 atomic_set(&tconn->net_cnt, 0);
2272 init_waitqueue_head(&tconn->net_cnt_wait);
Philipp Reisner2a67d8b2011-02-09 14:10:32 +01002273 init_waitqueue_head(&tconn->ping_wait);
Philipp Reisner062e8792011-02-08 11:09:18 +01002274 idr_init(&tconn->volumes);
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01002275
Philipp Reisner6699b652011-02-09 11:10:24 +01002276 drbd_init_workqueue(&tconn->data.work);
2277 mutex_init(&tconn->data.mutex);
2278
2279 drbd_init_workqueue(&tconn->meta.work);
2280 mutex_init(&tconn->meta.mutex);
2281
Philipp Reisner392c8802011-02-09 10:33:31 +01002282 drbd_thread_init(tconn, &tconn->receiver, drbdd_init, "receiver");
2283 drbd_thread_init(tconn, &tconn->worker, drbd_worker, "worker");
2284 drbd_thread_init(tconn, &tconn->asender, drbd_asender, "asender");
2285
Philipp Reisner21114382011-01-19 12:26:59 +01002286 write_lock_irq(&global_state_lock);
2287 list_add(&tconn->all_tconn, &drbd_tconns);
2288 write_unlock_irq(&global_state_lock);
2289
2290 return tconn;
2291
2292fail:
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01002293 tl_cleanup(tconn);
Philipp Reisner774b3052011-02-22 02:07:03 -05002294 free_cpumask_var(tconn->cpu_mask);
Philipp Reisner21114382011-01-19 12:26:59 +01002295 kfree(tconn->name);
2296 kfree(tconn);
2297
2298 return NULL;
2299}
2300
2301void drbd_free_tconn(struct drbd_tconn *tconn)
2302{
2303 write_lock_irq(&global_state_lock);
2304 list_del(&tconn->all_tconn);
2305 write_unlock_irq(&global_state_lock);
Philipp Reisner062e8792011-02-08 11:09:18 +01002306 idr_destroy(&tconn->volumes);
Philipp Reisner21114382011-01-19 12:26:59 +01002307
Philipp Reisner774b3052011-02-22 02:07:03 -05002308 free_cpumask_var(tconn->cpu_mask);
Philipp Reisner21114382011-01-19 12:26:59 +01002309 kfree(tconn->name);
Philipp Reisnerb42a70a2011-01-27 10:55:20 +01002310 kfree(tconn->int_dig_out);
2311 kfree(tconn->int_dig_in);
2312 kfree(tconn->int_dig_vv);
Philipp Reisner21114382011-01-19 12:26:59 +01002313 kfree(tconn);
2314}
2315
Philipp Reisner774b3052011-02-22 02:07:03 -05002316enum drbd_ret_code conn_new_minor(struct drbd_tconn *tconn, unsigned int minor, int vnr)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002317{
2318 struct drbd_conf *mdev;
2319 struct gendisk *disk;
2320 struct request_queue *q;
Philipp Reisner774b3052011-02-22 02:07:03 -05002321 int vnr_got = vnr;
Philipp Reisner81a5d602011-02-22 19:53:16 -05002322 int minor_got = minor;
Philipp Reisner774b3052011-02-22 02:07:03 -05002323
2324 mdev = minor_to_mdev(minor);
2325 if (mdev)
2326 return ERR_MINOR_EXISTS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002327
2328 /* GFP_KERNEL, we are outside of all write-out paths */
2329 mdev = kzalloc(sizeof(struct drbd_conf), GFP_KERNEL);
2330 if (!mdev)
Philipp Reisner774b3052011-02-22 02:07:03 -05002331 return ERR_NOMEM;
2332
2333 mdev->tconn = tconn;
2334 if (!idr_pre_get(&tconn->volumes, GFP_KERNEL))
2335 goto out_no_idr;
2336 if (idr_get_new(&tconn->volumes, mdev, &vnr_got))
2337 goto out_no_idr;
2338 if (vnr_got != vnr) {
2339 dev_err(DEV, "vnr_got (%d) != vnr (%d)\n", vnr_got, vnr);
2340 goto out_no_q;
Philipp Reisner062e8792011-02-08 11:09:18 +01002341 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002342
2343 mdev->minor = minor;
2344
2345 drbd_init_set_defaults(mdev);
2346
2347 q = blk_alloc_queue(GFP_KERNEL);
2348 if (!q)
2349 goto out_no_q;
2350 mdev->rq_queue = q;
2351 q->queuedata = mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002352
2353 disk = alloc_disk(1);
2354 if (!disk)
2355 goto out_no_disk;
2356 mdev->vdisk = disk;
2357
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002358 set_disk_ro(disk, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002359
2360 disk->queue = q;
2361 disk->major = DRBD_MAJOR;
2362 disk->first_minor = minor;
2363 disk->fops = &drbd_ops;
2364 sprintf(disk->disk_name, "drbd%d", minor);
2365 disk->private_data = mdev;
2366
2367 mdev->this_bdev = bdget(MKDEV(DRBD_MAJOR, minor));
2368 /* we have no partitions. we contain only ourselves. */
2369 mdev->this_bdev->bd_contains = mdev->this_bdev;
2370
2371 q->backing_dev_info.congested_fn = drbd_congested;
2372 q->backing_dev_info.congested_data = mdev;
2373
Andreas Gruenbacher2f58dcf2010-12-13 17:48:19 +01002374 blk_queue_make_request(q, drbd_make_request);
Philipp Reisner99432fc2011-05-20 16:39:13 +02002375 /* Setting the max_hw_sectors to an odd value of 8kibyte here
2376 This triggers a max_bio_size message upon first attach or connect */
2377 blk_queue_max_hw_sectors(q, DRBD_MAX_BIO_SIZE_SAFE >> 8);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002378 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
2379 blk_queue_merge_bvec(q, drbd_merge_bvec);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002380 q->queue_lock = &mdev->tconn->req_lock; /* needed since we use */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002381
2382 mdev->md_io_page = alloc_page(GFP_KERNEL);
2383 if (!mdev->md_io_page)
2384 goto out_no_io_page;
2385
2386 if (drbd_bm_init(mdev))
2387 goto out_no_bitmap;
Andreas Gruenbacherdac13892011-01-21 17:18:39 +01002388 mdev->read_requests = RB_ROOT;
Andreas Gruenbacherde696712011-01-20 15:00:24 +01002389 mdev->write_requests = RB_ROOT;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002390
Philipp Reisnerb411b362009-09-25 16:07:19 -07002391 mdev->current_epoch = kzalloc(sizeof(struct drbd_epoch), GFP_KERNEL);
2392 if (!mdev->current_epoch)
2393 goto out_no_epoch;
2394
2395 INIT_LIST_HEAD(&mdev->current_epoch->list);
2396 mdev->epochs = 1;
2397
Philipp Reisner81a5d602011-02-22 19:53:16 -05002398 if (!idr_pre_get(&minors, GFP_KERNEL))
2399 goto out_no_minor_idr;
2400 if (idr_get_new(&minors, mdev, &minor_got))
2401 goto out_no_minor_idr;
2402 if (minor_got != minor) {
2403 idr_remove(&minors, minor_got);
2404 goto out_no_minor_idr;
2405 }
Philipp Reisner774b3052011-02-22 02:07:03 -05002406 add_disk(disk);
2407
2408 return NO_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002409
Philipp Reisner81a5d602011-02-22 19:53:16 -05002410out_no_minor_idr:
2411 kfree(mdev->current_epoch);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002412out_no_epoch:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002413 drbd_bm_cleanup(mdev);
2414out_no_bitmap:
2415 __free_page(mdev->md_io_page);
2416out_no_io_page:
2417 put_disk(disk);
2418out_no_disk:
2419 blk_cleanup_queue(q);
2420out_no_q:
Philipp Reisner774b3052011-02-22 02:07:03 -05002421 idr_remove(&tconn->volumes, vnr_got);
2422out_no_idr:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002423 kfree(mdev);
Philipp Reisner774b3052011-02-22 02:07:03 -05002424 return ERR_NOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002425}
2426
2427/* counterpart of drbd_new_device.
2428 * last part of drbd_delete_device. */
2429void drbd_free_mdev(struct drbd_conf *mdev)
2430{
2431 kfree(mdev->current_epoch);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002432 if (mdev->bitmap) /* should no longer be there. */
2433 drbd_bm_cleanup(mdev);
2434 __free_page(mdev->md_io_page);
2435 put_disk(mdev->vdisk);
2436 blk_cleanup_queue(mdev->rq_queue);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002437 kfree(mdev);
2438}
2439
2440
2441int __init drbd_init(void)
2442{
2443 int err;
2444
Philipp Reisnerfd340c12011-01-19 16:57:39 +01002445 BUILD_BUG_ON(sizeof(struct p_header80) != sizeof(struct p_header95));
2446 BUILD_BUG_ON(sizeof(struct p_handshake) != 80);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002447
Philipp Reisner2b8a90b2011-01-10 11:15:17 +01002448 if (minor_count < DRBD_MINOR_COUNT_MIN || minor_count > DRBD_MINOR_COUNT_MAX) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002449 printk(KERN_ERR
Philipp Reisner81a5d602011-02-22 19:53:16 -05002450 "drbd: invalid minor_count (%d)\n", minor_count);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002451#ifdef MODULE
2452 return -EINVAL;
2453#else
2454 minor_count = 8;
2455#endif
2456 }
2457
2458 err = drbd_nl_init();
2459 if (err)
2460 return err;
2461
2462 err = register_blkdev(DRBD_MAJOR, "drbd");
2463 if (err) {
2464 printk(KERN_ERR
2465 "drbd: unable to register block device major %d\n",
2466 DRBD_MAJOR);
2467 return err;
2468 }
2469
2470 register_reboot_notifier(&drbd_notifier);
2471
2472 /*
2473 * allocate all necessary structs
2474 */
2475 err = -ENOMEM;
2476
2477 init_waitqueue_head(&drbd_pp_wait);
2478
2479 drbd_proc = NULL; /* play safe for drbd_cleanup */
Philipp Reisner81a5d602011-02-22 19:53:16 -05002480 idr_init(&minors);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002481
2482 err = drbd_create_mempools();
2483 if (err)
2484 goto Enomem;
2485
Lars Ellenberg8c484ee2010-03-11 16:47:58 +01002486 drbd_proc = proc_create_data("drbd", S_IFREG | S_IRUGO , NULL, &drbd_proc_fops, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002487 if (!drbd_proc) {
2488 printk(KERN_ERR "drbd: unable to register proc file\n");
2489 goto Enomem;
2490 }
2491
2492 rwlock_init(&global_state_lock);
Philipp Reisner21114382011-01-19 12:26:59 +01002493 INIT_LIST_HEAD(&drbd_tconns);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002494
2495 printk(KERN_INFO "drbd: initialized. "
2496 "Version: " REL_VERSION " (api:%d/proto:%d-%d)\n",
2497 API_VERSION, PRO_VERSION_MIN, PRO_VERSION_MAX);
2498 printk(KERN_INFO "drbd: %s\n", drbd_buildtag());
2499 printk(KERN_INFO "drbd: registered as block device major %d\n",
2500 DRBD_MAJOR);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002501
2502 return 0; /* Success! */
2503
2504Enomem:
2505 drbd_cleanup();
2506 if (err == -ENOMEM)
2507 /* currently always the case */
2508 printk(KERN_ERR "drbd: ran out of memory\n");
2509 else
2510 printk(KERN_ERR "drbd: initialization failure\n");
2511 return err;
2512}
2513
2514void drbd_free_bc(struct drbd_backing_dev *ldev)
2515{
2516 if (ldev == NULL)
2517 return;
2518
Tejun Heoe525fd82010-11-13 11:55:17 +01002519 blkdev_put(ldev->backing_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
2520 blkdev_put(ldev->md_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002521
2522 kfree(ldev);
2523}
2524
Philipp Reisner360cc742011-02-08 14:29:53 +01002525void drbd_free_sock(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002526{
Philipp Reisner360cc742011-02-08 14:29:53 +01002527 if (tconn->data.socket) {
2528 mutex_lock(&tconn->data.mutex);
2529 kernel_sock_shutdown(tconn->data.socket, SHUT_RDWR);
2530 sock_release(tconn->data.socket);
2531 tconn->data.socket = NULL;
2532 mutex_unlock(&tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002533 }
Philipp Reisner360cc742011-02-08 14:29:53 +01002534 if (tconn->meta.socket) {
2535 mutex_lock(&tconn->meta.mutex);
2536 kernel_sock_shutdown(tconn->meta.socket, SHUT_RDWR);
2537 sock_release(tconn->meta.socket);
2538 tconn->meta.socket = NULL;
2539 mutex_unlock(&tconn->meta.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002540 }
2541}
2542
2543
2544void drbd_free_resources(struct drbd_conf *mdev)
2545{
2546 crypto_free_hash(mdev->csums_tfm);
2547 mdev->csums_tfm = NULL;
2548 crypto_free_hash(mdev->verify_tfm);
2549 mdev->verify_tfm = NULL;
Philipp Reisnera0638452011-01-19 14:31:32 +01002550 crypto_free_hash(mdev->tconn->cram_hmac_tfm);
2551 mdev->tconn->cram_hmac_tfm = NULL;
2552 crypto_free_hash(mdev->tconn->integrity_w_tfm);
2553 mdev->tconn->integrity_w_tfm = NULL;
2554 crypto_free_hash(mdev->tconn->integrity_r_tfm);
2555 mdev->tconn->integrity_r_tfm = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002556
Philipp Reisner360cc742011-02-08 14:29:53 +01002557 drbd_free_sock(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002558
2559 __no_warn(local,
2560 drbd_free_bc(mdev->ldev);
2561 mdev->ldev = NULL;);
2562}
2563
2564/* meta data management */
2565
2566struct meta_data_on_disk {
2567 u64 la_size; /* last agreed size. */
2568 u64 uuid[UI_SIZE]; /* UUIDs. */
2569 u64 device_uuid;
2570 u64 reserved_u64_1;
2571 u32 flags; /* MDF */
2572 u32 magic;
2573 u32 md_size_sect;
2574 u32 al_offset; /* offset to this block */
2575 u32 al_nr_extents; /* important for restoring the AL */
2576 /* `-- act_log->nr_elements <-- sync_conf.al_extents */
2577 u32 bm_offset; /* offset to the bitmap, from here */
2578 u32 bm_bytes_per_bit; /* BM_BLOCK_SIZE */
Philipp Reisner99432fc2011-05-20 16:39:13 +02002579 u32 la_peer_max_bio_size; /* last peer max_bio_size */
2580 u32 reserved_u32[3];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002581
2582} __packed;
2583
2584/**
2585 * drbd_md_sync() - Writes the meta data super block if the MD_DIRTY flag bit is set
2586 * @mdev: DRBD device.
2587 */
2588void drbd_md_sync(struct drbd_conf *mdev)
2589{
2590 struct meta_data_on_disk *buffer;
2591 sector_t sector;
2592 int i;
2593
Lars Ellenbergee15b032010-09-03 10:00:09 +02002594 del_timer(&mdev->md_sync_timer);
2595 /* timer may be rearmed by drbd_md_mark_dirty() now. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002596 if (!test_and_clear_bit(MD_DIRTY, &mdev->flags))
2597 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002598
2599 /* We use here D_FAILED and not D_ATTACHING because we try to write
2600 * metadata even if we detach due to a disk failure! */
2601 if (!get_ldev_if_state(mdev, D_FAILED))
2602 return;
2603
Philipp Reisnerb411b362009-09-25 16:07:19 -07002604 mutex_lock(&mdev->md_io_mutex);
2605 buffer = (struct meta_data_on_disk *)page_address(mdev->md_io_page);
2606 memset(buffer, 0, 512);
2607
2608 buffer->la_size = cpu_to_be64(drbd_get_capacity(mdev->this_bdev));
2609 for (i = UI_CURRENT; i < UI_SIZE; i++)
2610 buffer->uuid[i] = cpu_to_be64(mdev->ldev->md.uuid[i]);
2611 buffer->flags = cpu_to_be32(mdev->ldev->md.flags);
2612 buffer->magic = cpu_to_be32(DRBD_MD_MAGIC);
2613
2614 buffer->md_size_sect = cpu_to_be32(mdev->ldev->md.md_size_sect);
2615 buffer->al_offset = cpu_to_be32(mdev->ldev->md.al_offset);
2616 buffer->al_nr_extents = cpu_to_be32(mdev->act_log->nr_elements);
2617 buffer->bm_bytes_per_bit = cpu_to_be32(BM_BLOCK_SIZE);
2618 buffer->device_uuid = cpu_to_be64(mdev->ldev->md.device_uuid);
2619
2620 buffer->bm_offset = cpu_to_be32(mdev->ldev->md.bm_offset);
Philipp Reisner99432fc2011-05-20 16:39:13 +02002621 buffer->la_peer_max_bio_size = cpu_to_be32(mdev->peer_max_bio_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002622
2623 D_ASSERT(drbd_md_ss__(mdev, mdev->ldev) == mdev->ldev->md.md_offset);
2624 sector = mdev->ldev->md.md_offset;
2625
Lars Ellenberg3f3a9b82010-09-01 15:12:12 +02002626 if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002627 /* this was a try anyways ... */
2628 dev_err(DEV, "meta data update failed!\n");
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002629 drbd_chk_io_error(mdev, 1, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002630 }
2631
2632 /* Update mdev->ldev->md.la_size_sect,
2633 * since we updated it on metadata. */
2634 mdev->ldev->md.la_size_sect = drbd_get_capacity(mdev->this_bdev);
2635
2636 mutex_unlock(&mdev->md_io_mutex);
2637 put_ldev(mdev);
2638}
2639
2640/**
2641 * drbd_md_read() - Reads in the meta data super block
2642 * @mdev: DRBD device.
2643 * @bdev: Device from which the meta data should be read in.
2644 *
Andreas Gruenbacher116676c2010-12-08 13:33:11 +01002645 * Return 0 (NO_ERROR) on success, and an enum drbd_ret_code in case
Philipp Reisnerb411b362009-09-25 16:07:19 -07002646 * something goes wrong. Currently only: ERR_IO_MD_DISK, ERR_MD_INVALID.
2647 */
2648int drbd_md_read(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
2649{
2650 struct meta_data_on_disk *buffer;
2651 int i, rv = NO_ERROR;
2652
2653 if (!get_ldev_if_state(mdev, D_ATTACHING))
2654 return ERR_IO_MD_DISK;
2655
Philipp Reisnerb411b362009-09-25 16:07:19 -07002656 mutex_lock(&mdev->md_io_mutex);
2657 buffer = (struct meta_data_on_disk *)page_address(mdev->md_io_page);
2658
2659 if (!drbd_md_sync_page_io(mdev, bdev, bdev->md.md_offset, READ)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002660 /* NOTE: can't do normal error processing here as this is
Philipp Reisnerb411b362009-09-25 16:07:19 -07002661 called BEFORE disk is attached */
2662 dev_err(DEV, "Error while reading metadata.\n");
2663 rv = ERR_IO_MD_DISK;
2664 goto err;
2665 }
2666
Andreas Gruenbachere7fad8a2011-01-11 13:54:02 +01002667 if (buffer->magic != cpu_to_be32(DRBD_MD_MAGIC)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002668 dev_err(DEV, "Error while reading metadata, magic not found.\n");
2669 rv = ERR_MD_INVALID;
2670 goto err;
2671 }
2672 if (be32_to_cpu(buffer->al_offset) != bdev->md.al_offset) {
2673 dev_err(DEV, "unexpected al_offset: %d (expected %d)\n",
2674 be32_to_cpu(buffer->al_offset), bdev->md.al_offset);
2675 rv = ERR_MD_INVALID;
2676 goto err;
2677 }
2678 if (be32_to_cpu(buffer->bm_offset) != bdev->md.bm_offset) {
2679 dev_err(DEV, "unexpected bm_offset: %d (expected %d)\n",
2680 be32_to_cpu(buffer->bm_offset), bdev->md.bm_offset);
2681 rv = ERR_MD_INVALID;
2682 goto err;
2683 }
2684 if (be32_to_cpu(buffer->md_size_sect) != bdev->md.md_size_sect) {
2685 dev_err(DEV, "unexpected md_size: %u (expected %u)\n",
2686 be32_to_cpu(buffer->md_size_sect), bdev->md.md_size_sect);
2687 rv = ERR_MD_INVALID;
2688 goto err;
2689 }
2690
2691 if (be32_to_cpu(buffer->bm_bytes_per_bit) != BM_BLOCK_SIZE) {
2692 dev_err(DEV, "unexpected bm_bytes_per_bit: %u (expected %u)\n",
2693 be32_to_cpu(buffer->bm_bytes_per_bit), BM_BLOCK_SIZE);
2694 rv = ERR_MD_INVALID;
2695 goto err;
2696 }
2697
2698 bdev->md.la_size_sect = be64_to_cpu(buffer->la_size);
2699 for (i = UI_CURRENT; i < UI_SIZE; i++)
2700 bdev->md.uuid[i] = be64_to_cpu(buffer->uuid[i]);
2701 bdev->md.flags = be32_to_cpu(buffer->flags);
2702 mdev->sync_conf.al_extents = be32_to_cpu(buffer->al_nr_extents);
2703 bdev->md.device_uuid = be64_to_cpu(buffer->device_uuid);
2704
Philipp Reisner87eeee42011-01-19 14:16:30 +01002705 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner99432fc2011-05-20 16:39:13 +02002706 if (mdev->state.conn < C_CONNECTED) {
2707 int peer;
2708 peer = be32_to_cpu(buffer->la_peer_max_bio_size);
2709 peer = max_t(int, peer, DRBD_MAX_BIO_SIZE_SAFE);
2710 mdev->peer_max_bio_size = peer;
2711 }
Philipp Reisner87eeee42011-01-19 14:16:30 +01002712 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner99432fc2011-05-20 16:39:13 +02002713
Philipp Reisnerb411b362009-09-25 16:07:19 -07002714 if (mdev->sync_conf.al_extents < 7)
2715 mdev->sync_conf.al_extents = 127;
2716
2717 err:
2718 mutex_unlock(&mdev->md_io_mutex);
2719 put_ldev(mdev);
2720
2721 return rv;
2722}
2723
2724/**
2725 * drbd_md_mark_dirty() - Mark meta data super block as dirty
2726 * @mdev: DRBD device.
2727 *
2728 * Call this function if you change anything that should be written to
2729 * the meta-data super block. This function sets MD_DIRTY, and starts a
2730 * timer that ensures that within five seconds you have to call drbd_md_sync().
2731 */
Lars Ellenbergca0e6092010-10-14 15:01:21 +02002732#ifdef DEBUG
Lars Ellenbergee15b032010-09-03 10:00:09 +02002733void drbd_md_mark_dirty_(struct drbd_conf *mdev, unsigned int line, const char *func)
2734{
2735 if (!test_and_set_bit(MD_DIRTY, &mdev->flags)) {
2736 mod_timer(&mdev->md_sync_timer, jiffies + HZ);
2737 mdev->last_md_mark_dirty.line = line;
2738 mdev->last_md_mark_dirty.func = func;
2739 }
2740}
2741#else
Philipp Reisnerb411b362009-09-25 16:07:19 -07002742void drbd_md_mark_dirty(struct drbd_conf *mdev)
2743{
Lars Ellenbergee15b032010-09-03 10:00:09 +02002744 if (!test_and_set_bit(MD_DIRTY, &mdev->flags))
Lars Ellenbergca0e6092010-10-14 15:01:21 +02002745 mod_timer(&mdev->md_sync_timer, jiffies + 5*HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002746}
Lars Ellenbergee15b032010-09-03 10:00:09 +02002747#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -07002748
2749static void drbd_uuid_move_history(struct drbd_conf *mdev) __must_hold(local)
2750{
2751 int i;
2752
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002753 for (i = UI_HISTORY_START; i < UI_HISTORY_END; i++)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002754 mdev->ldev->md.uuid[i+1] = mdev->ldev->md.uuid[i];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002755}
2756
2757void _drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local)
2758{
2759 if (idx == UI_CURRENT) {
2760 if (mdev->state.role == R_PRIMARY)
2761 val |= 1;
2762 else
2763 val &= ~((u64)1);
2764
2765 drbd_set_ed_uuid(mdev, val);
2766 }
2767
2768 mdev->ldev->md.uuid[idx] = val;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002769 drbd_md_mark_dirty(mdev);
2770}
2771
2772
2773void drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local)
2774{
2775 if (mdev->ldev->md.uuid[idx]) {
2776 drbd_uuid_move_history(mdev);
2777 mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[idx];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002778 }
2779 _drbd_uuid_set(mdev, idx, val);
2780}
2781
2782/**
2783 * drbd_uuid_new_current() - Creates a new current UUID
2784 * @mdev: DRBD device.
2785 *
2786 * Creates a new current UUID, and rotates the old current UUID into
2787 * the bitmap slot. Causes an incremental resync upon next connect.
2788 */
2789void drbd_uuid_new_current(struct drbd_conf *mdev) __must_hold(local)
2790{
2791 u64 val;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002792 unsigned long long bm_uuid = mdev->ldev->md.uuid[UI_BITMAP];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002793
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002794 if (bm_uuid)
2795 dev_warn(DEV, "bm UUID was already set: %llX\n", bm_uuid);
2796
Philipp Reisnerb411b362009-09-25 16:07:19 -07002797 mdev->ldev->md.uuid[UI_BITMAP] = mdev->ldev->md.uuid[UI_CURRENT];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002798
2799 get_random_bytes(&val, sizeof(u64));
2800 _drbd_uuid_set(mdev, UI_CURRENT, val);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002801 drbd_print_uuids(mdev, "new current UUID");
Lars Ellenbergaaa8e2b2010-10-15 13:16:53 +02002802 /* get it to stable storage _now_ */
2803 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002804}
2805
2806void drbd_uuid_set_bm(struct drbd_conf *mdev, u64 val) __must_hold(local)
2807{
2808 if (mdev->ldev->md.uuid[UI_BITMAP] == 0 && val == 0)
2809 return;
2810
2811 if (val == 0) {
2812 drbd_uuid_move_history(mdev);
2813 mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[UI_BITMAP];
2814 mdev->ldev->md.uuid[UI_BITMAP] = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002815 } else {
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002816 unsigned long long bm_uuid = mdev->ldev->md.uuid[UI_BITMAP];
2817 if (bm_uuid)
2818 dev_warn(DEV, "bm UUID was already set: %llX\n", bm_uuid);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002819
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002820 mdev->ldev->md.uuid[UI_BITMAP] = val & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002821 }
2822 drbd_md_mark_dirty(mdev);
2823}
2824
2825/**
2826 * drbd_bmio_set_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
2827 * @mdev: DRBD device.
2828 *
2829 * Sets all bits in the bitmap and writes the whole bitmap to stable storage.
2830 */
2831int drbd_bmio_set_n_write(struct drbd_conf *mdev)
2832{
2833 int rv = -EIO;
2834
2835 if (get_ldev_if_state(mdev, D_ATTACHING)) {
2836 drbd_md_set_flag(mdev, MDF_FULL_SYNC);
2837 drbd_md_sync(mdev);
2838 drbd_bm_set_all(mdev);
2839
2840 rv = drbd_bm_write(mdev);
2841
2842 if (!rv) {
2843 drbd_md_clear_flag(mdev, MDF_FULL_SYNC);
2844 drbd_md_sync(mdev);
2845 }
2846
2847 put_ldev(mdev);
2848 }
2849
2850 return rv;
2851}
2852
2853/**
2854 * drbd_bmio_clear_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
2855 * @mdev: DRBD device.
2856 *
2857 * Clears all bits in the bitmap and writes the whole bitmap to stable storage.
2858 */
2859int drbd_bmio_clear_n_write(struct drbd_conf *mdev)
2860{
2861 int rv = -EIO;
2862
Philipp Reisner07782862010-08-31 12:00:50 +02002863 drbd_resume_al(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002864 if (get_ldev_if_state(mdev, D_ATTACHING)) {
2865 drbd_bm_clear_all(mdev);
2866 rv = drbd_bm_write(mdev);
2867 put_ldev(mdev);
2868 }
2869
2870 return rv;
2871}
2872
Philipp Reisner00d56942011-02-09 18:09:48 +01002873static int w_bitmap_io(struct drbd_work *w, int unused)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002874{
2875 struct bm_io_work *work = container_of(w, struct bm_io_work, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01002876 struct drbd_conf *mdev = w->mdev;
Lars Ellenberg02851e92010-12-16 14:47:39 +01002877 int rv = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002878
2879 D_ASSERT(atomic_read(&mdev->ap_bio_cnt) == 0);
2880
Lars Ellenberg02851e92010-12-16 14:47:39 +01002881 if (get_ldev(mdev)) {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002882 drbd_bm_lock(mdev, work->why, work->flags);
Lars Ellenberg02851e92010-12-16 14:47:39 +01002883 rv = work->io_fn(mdev);
2884 drbd_bm_unlock(mdev);
2885 put_ldev(mdev);
2886 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002887
Lars Ellenberg4738fa12011-02-21 13:20:55 +01002888 clear_bit_unlock(BITMAP_IO, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002889 wake_up(&mdev->misc_wait);
2890
2891 if (work->done)
2892 work->done(mdev, rv);
2893
2894 clear_bit(BITMAP_IO_QUEUED, &mdev->flags);
2895 work->why = NULL;
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002896 work->flags = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002897
2898 return 1;
2899}
2900
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02002901void drbd_ldev_destroy(struct drbd_conf *mdev)
2902{
2903 lc_destroy(mdev->resync);
2904 mdev->resync = NULL;
2905 lc_destroy(mdev->act_log);
2906 mdev->act_log = NULL;
2907 __no_warn(local,
2908 drbd_free_bc(mdev->ldev);
2909 mdev->ldev = NULL;);
2910
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02002911 clear_bit(GO_DISKLESS, &mdev->flags);
2912}
2913
Philipp Reisner00d56942011-02-09 18:09:48 +01002914static int w_go_diskless(struct drbd_work *w, int unused)
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002915{
Philipp Reisner00d56942011-02-09 18:09:48 +01002916 struct drbd_conf *mdev = w->mdev;
2917
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002918 D_ASSERT(mdev->state.disk == D_FAILED);
Lars Ellenberg9d282872010-10-14 13:57:07 +02002919 /* we cannot assert local_cnt == 0 here, as get_ldev_if_state will
2920 * inc/dec it frequently. Once we are D_DISKLESS, no one will touch
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02002921 * the protected members anymore, though, so once put_ldev reaches zero
2922 * again, it will be safe to free them. */
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002923 drbd_force_state(mdev, NS(disk, D_DISKLESS));
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002924 return 1;
2925}
2926
2927void drbd_go_diskless(struct drbd_conf *mdev)
2928{
2929 D_ASSERT(mdev->state.disk == D_FAILED);
2930 if (!test_and_set_bit(GO_DISKLESS, &mdev->flags))
Philipp Reisnere42325a2011-01-19 13:55:45 +01002931 drbd_queue_work(&mdev->tconn->data.work, &mdev->go_diskless);
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02002932}
2933
Philipp Reisnerb411b362009-09-25 16:07:19 -07002934/**
2935 * drbd_queue_bitmap_io() - Queues an IO operation on the whole bitmap
2936 * @mdev: DRBD device.
2937 * @io_fn: IO callback to be called when bitmap IO is possible
2938 * @done: callback to be called after the bitmap IO was performed
2939 * @why: Descriptive text of the reason for doing the IO
2940 *
2941 * While IO on the bitmap happens we freeze application IO thus we ensure
2942 * that drbd_set_out_of_sync() can not be called. This function MAY ONLY be
2943 * called from worker context. It MUST NOT be used while a previous such
2944 * work is still pending!
2945 */
2946void drbd_queue_bitmap_io(struct drbd_conf *mdev,
2947 int (*io_fn)(struct drbd_conf *),
2948 void (*done)(struct drbd_conf *, int),
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002949 char *why, enum bm_flag flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002950{
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01002951 D_ASSERT(current == mdev->tconn->worker.task);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002952
2953 D_ASSERT(!test_bit(BITMAP_IO_QUEUED, &mdev->flags));
2954 D_ASSERT(!test_bit(BITMAP_IO, &mdev->flags));
2955 D_ASSERT(list_empty(&mdev->bm_io_work.w.list));
2956 if (mdev->bm_io_work.why)
2957 dev_err(DEV, "FIXME going to queue '%s' but '%s' still pending?\n",
2958 why, mdev->bm_io_work.why);
2959
2960 mdev->bm_io_work.io_fn = io_fn;
2961 mdev->bm_io_work.done = done;
2962 mdev->bm_io_work.why = why;
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002963 mdev->bm_io_work.flags = flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002964
Philipp Reisner87eeee42011-01-19 14:16:30 +01002965 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002966 set_bit(BITMAP_IO, &mdev->flags);
2967 if (atomic_read(&mdev->ap_bio_cnt) == 0) {
Philipp Reisner127b3172010-11-16 10:07:53 +01002968 if (!test_and_set_bit(BITMAP_IO_QUEUED, &mdev->flags))
Philipp Reisnere42325a2011-01-19 13:55:45 +01002969 drbd_queue_work(&mdev->tconn->data.work, &mdev->bm_io_work.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002970 }
Philipp Reisner87eeee42011-01-19 14:16:30 +01002971 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002972}
2973
2974/**
2975 * drbd_bitmap_io() - Does an IO operation on the whole bitmap
2976 * @mdev: DRBD device.
2977 * @io_fn: IO callback to be called when bitmap IO is possible
2978 * @why: Descriptive text of the reason for doing the IO
2979 *
2980 * freezes application IO while that the actual IO operations runs. This
2981 * functions MAY NOT be called from worker context.
2982 */
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002983int drbd_bitmap_io(struct drbd_conf *mdev, int (*io_fn)(struct drbd_conf *),
2984 char *why, enum bm_flag flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002985{
2986 int rv;
2987
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01002988 D_ASSERT(current != mdev->tconn->worker.task);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002989
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002990 if ((flags & BM_LOCKED_SET_ALLOWED) == 0)
2991 drbd_suspend_io(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002992
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002993 drbd_bm_lock(mdev, why, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002994 rv = io_fn(mdev);
2995 drbd_bm_unlock(mdev);
2996
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002997 if ((flags & BM_LOCKED_SET_ALLOWED) == 0)
2998 drbd_resume_io(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002999
3000 return rv;
3001}
3002
3003void drbd_md_set_flag(struct drbd_conf *mdev, int flag) __must_hold(local)
3004{
3005 if ((mdev->ldev->md.flags & flag) != flag) {
3006 drbd_md_mark_dirty(mdev);
3007 mdev->ldev->md.flags |= flag;
3008 }
3009}
3010
3011void drbd_md_clear_flag(struct drbd_conf *mdev, int flag) __must_hold(local)
3012{
3013 if ((mdev->ldev->md.flags & flag) != 0) {
3014 drbd_md_mark_dirty(mdev);
3015 mdev->ldev->md.flags &= ~flag;
3016 }
3017}
3018int drbd_md_test_flag(struct drbd_backing_dev *bdev, int flag)
3019{
3020 return (bdev->md.flags & flag) != 0;
3021}
3022
3023static void md_sync_timer_fn(unsigned long data)
3024{
3025 struct drbd_conf *mdev = (struct drbd_conf *) data;
3026
Philipp Reisnere42325a2011-01-19 13:55:45 +01003027 drbd_queue_work_front(&mdev->tconn->data.work, &mdev->md_sync_work);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003028}
3029
Philipp Reisner00d56942011-02-09 18:09:48 +01003030static int w_md_sync(struct drbd_work *w, int unused)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003031{
Philipp Reisner00d56942011-02-09 18:09:48 +01003032 struct drbd_conf *mdev = w->mdev;
3033
Philipp Reisnerb411b362009-09-25 16:07:19 -07003034 dev_warn(DEV, "md_sync_timer expired! Worker calls drbd_md_sync().\n");
Lars Ellenbergee15b032010-09-03 10:00:09 +02003035#ifdef DEBUG
3036 dev_warn(DEV, "last md_mark_dirty: %s:%u\n",
3037 mdev->last_md_mark_dirty.func, mdev->last_md_mark_dirty.line);
3038#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -07003039 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003040 return 1;
3041}
3042
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003043const char *cmdname(enum drbd_packet cmd)
Andreas Gruenbacherf2ad9062011-01-26 17:13:25 +01003044{
3045 /* THINK may need to become several global tables
3046 * when we want to support more than
3047 * one PRO_VERSION */
3048 static const char *cmdnames[] = {
3049 [P_DATA] = "Data",
3050 [P_DATA_REPLY] = "DataReply",
3051 [P_RS_DATA_REPLY] = "RSDataReply",
3052 [P_BARRIER] = "Barrier",
3053 [P_BITMAP] = "ReportBitMap",
3054 [P_BECOME_SYNC_TARGET] = "BecomeSyncTarget",
3055 [P_BECOME_SYNC_SOURCE] = "BecomeSyncSource",
3056 [P_UNPLUG_REMOTE] = "UnplugRemote",
3057 [P_DATA_REQUEST] = "DataRequest",
3058 [P_RS_DATA_REQUEST] = "RSDataRequest",
3059 [P_SYNC_PARAM] = "SyncParam",
3060 [P_SYNC_PARAM89] = "SyncParam89",
3061 [P_PROTOCOL] = "ReportProtocol",
3062 [P_UUIDS] = "ReportUUIDs",
3063 [P_SIZES] = "ReportSizes",
3064 [P_STATE] = "ReportState",
3065 [P_SYNC_UUID] = "ReportSyncUUID",
3066 [P_AUTH_CHALLENGE] = "AuthChallenge",
3067 [P_AUTH_RESPONSE] = "AuthResponse",
3068 [P_PING] = "Ping",
3069 [P_PING_ACK] = "PingAck",
3070 [P_RECV_ACK] = "RecvAck",
3071 [P_WRITE_ACK] = "WriteAck",
3072 [P_RS_WRITE_ACK] = "RSWriteAck",
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01003073 [P_DISCARD_WRITE] = "DiscardWrite",
Andreas Gruenbacherf2ad9062011-01-26 17:13:25 +01003074 [P_NEG_ACK] = "NegAck",
3075 [P_NEG_DREPLY] = "NegDReply",
3076 [P_NEG_RS_DREPLY] = "NegRSDReply",
3077 [P_BARRIER_ACK] = "BarrierAck",
3078 [P_STATE_CHG_REQ] = "StateChgRequest",
3079 [P_STATE_CHG_REPLY] = "StateChgReply",
3080 [P_OV_REQUEST] = "OVRequest",
3081 [P_OV_REPLY] = "OVReply",
3082 [P_OV_RESULT] = "OVResult",
3083 [P_CSUM_RS_REQUEST] = "CsumRSRequest",
3084 [P_RS_IS_IN_SYNC] = "CsumRSIsInSync",
3085 [P_COMPRESSED_BITMAP] = "CBitmap",
3086 [P_DELAY_PROBE] = "DelayProbe",
3087 [P_OUT_OF_SYNC] = "OutOfSync",
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01003088 [P_RETRY_WRITE] = "RetryWrite",
Andreas Gruenbacherf2ad9062011-01-26 17:13:25 +01003089 };
3090
3091 if (cmd == P_HAND_SHAKE_M)
3092 return "HandShakeM";
3093 if (cmd == P_HAND_SHAKE_S)
3094 return "HandShakeS";
3095 if (cmd == P_HAND_SHAKE)
3096 return "HandShake";
Andreas Gruenbacher6e849ce2011-03-14 17:27:45 +01003097 if (cmd >= ARRAY_SIZE(cmdnames))
Andreas Gruenbacherf2ad9062011-01-26 17:13:25 +01003098 return "Unknown";
3099 return cmdnames[cmd];
3100}
3101
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01003102/**
3103 * drbd_wait_misc - wait for a request to make progress
3104 * @mdev: device associated with the request
3105 * @i: the struct drbd_interval embedded in struct drbd_request or
3106 * struct drbd_peer_request
3107 */
3108int drbd_wait_misc(struct drbd_conf *mdev, struct drbd_interval *i)
3109{
3110 struct net_conf *net_conf = mdev->tconn->net_conf;
3111 DEFINE_WAIT(wait);
3112 long timeout;
3113
3114 if (!net_conf)
3115 return -ETIMEDOUT;
3116 timeout = MAX_SCHEDULE_TIMEOUT;
3117 if (net_conf->ko_count)
3118 timeout = net_conf->timeout * HZ / 10 * net_conf->ko_count;
3119
3120 /* Indicate to wake up mdev->misc_wait on progress. */
3121 i->waiting = true;
3122 prepare_to_wait(&mdev->misc_wait, &wait, TASK_INTERRUPTIBLE);
3123 spin_unlock_irq(&mdev->tconn->req_lock);
3124 timeout = schedule_timeout(timeout);
3125 finish_wait(&mdev->misc_wait, &wait);
3126 spin_lock_irq(&mdev->tconn->req_lock);
3127 if (!timeout || mdev->state.conn < C_CONNECTED)
3128 return -ETIMEDOUT;
3129 if (signal_pending(current))
3130 return -ERESTARTSYS;
3131 return 0;
3132}
3133
Philipp Reisnerb411b362009-09-25 16:07:19 -07003134#ifdef CONFIG_DRBD_FAULT_INJECTION
3135/* Fault insertion support including random number generator shamelessly
3136 * stolen from kernel/rcutorture.c */
3137struct fault_random_state {
3138 unsigned long state;
3139 unsigned long count;
3140};
3141
3142#define FAULT_RANDOM_MULT 39916801 /* prime */
3143#define FAULT_RANDOM_ADD 479001701 /* prime */
3144#define FAULT_RANDOM_REFRESH 10000
3145
3146/*
3147 * Crude but fast random-number generator. Uses a linear congruential
3148 * generator, with occasional help from get_random_bytes().
3149 */
3150static unsigned long
3151_drbd_fault_random(struct fault_random_state *rsp)
3152{
3153 long refresh;
3154
Roel Kluin49829ea2009-12-15 22:55:44 +01003155 if (!rsp->count--) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003156 get_random_bytes(&refresh, sizeof(refresh));
3157 rsp->state += refresh;
3158 rsp->count = FAULT_RANDOM_REFRESH;
3159 }
3160 rsp->state = rsp->state * FAULT_RANDOM_MULT + FAULT_RANDOM_ADD;
3161 return swahw32(rsp->state);
3162}
3163
3164static char *
3165_drbd_fault_str(unsigned int type) {
3166 static char *_faults[] = {
3167 [DRBD_FAULT_MD_WR] = "Meta-data write",
3168 [DRBD_FAULT_MD_RD] = "Meta-data read",
3169 [DRBD_FAULT_RS_WR] = "Resync write",
3170 [DRBD_FAULT_RS_RD] = "Resync read",
3171 [DRBD_FAULT_DT_WR] = "Data write",
3172 [DRBD_FAULT_DT_RD] = "Data read",
3173 [DRBD_FAULT_DT_RA] = "Data read ahead",
3174 [DRBD_FAULT_BM_ALLOC] = "BM allocation",
Philipp Reisner6b4388a2010-04-26 14:11:45 +02003175 [DRBD_FAULT_AL_EE] = "EE allocation",
3176 [DRBD_FAULT_RECEIVE] = "receive data corruption",
Philipp Reisnerb411b362009-09-25 16:07:19 -07003177 };
3178
3179 return (type < DRBD_FAULT_MAX) ? _faults[type] : "**Unknown**";
3180}
3181
3182unsigned int
3183_drbd_insert_fault(struct drbd_conf *mdev, unsigned int type)
3184{
3185 static struct fault_random_state rrs = {0, 0};
3186
3187 unsigned int ret = (
3188 (fault_devs == 0 ||
3189 ((1 << mdev_to_minor(mdev)) & fault_devs) != 0) &&
3190 (((_drbd_fault_random(&rrs) % 100) + 1) <= fault_rate));
3191
3192 if (ret) {
3193 fault_count++;
3194
Lars Ellenberg73835062010-05-27 11:51:56 +02003195 if (__ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003196 dev_warn(DEV, "***Simulating %s failure\n",
3197 _drbd_fault_str(type));
3198 }
3199
3200 return ret;
3201}
3202#endif
3203
3204const char *drbd_buildtag(void)
3205{
3206 /* DRBD built from external sources has here a reference to the
3207 git hash of the source code. */
3208
3209 static char buildtag[38] = "\0uilt-in";
3210
3211 if (buildtag[0] == 0) {
3212#ifdef CONFIG_MODULES
3213 if (THIS_MODULE != NULL)
3214 sprintf(buildtag, "srcversion: %-24s", THIS_MODULE->srcversion);
3215 else
3216#endif
3217 buildtag[0] = 'b';
3218 }
3219
3220 return buildtag;
3221}
3222
3223module_init(drbd_init)
3224module_exit(drbd_cleanup)
3225
Philipp Reisnerb411b362009-09-25 16:07:19 -07003226EXPORT_SYMBOL(drbd_conn_str);
3227EXPORT_SYMBOL(drbd_role_str);
3228EXPORT_SYMBOL(drbd_disk_str);
3229EXPORT_SYMBOL(drbd_set_st_err_str);