blob: cc3b451d465faa3647bac61e1194d8e710665958 [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>
Lars Ellenberg113fef92013-03-22 18:14:40 -060048#include <linux/workqueue.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070049#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"
Andreas Gruenbachera3603a62011-05-30 11:47:37 +020055#include "drbd_protocol.h"
Philipp Reisnerb411b362009-09-25 16:07:19 -070056#include "drbd_req.h" /* only for _req_mod in tl_release and tl_clear */
57
58#include "drbd_vli.h"
59
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020060static DEFINE_MUTEX(drbd_main_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -070061int drbd_worker(struct drbd_thread *);
Philipp Reisnerb411b362009-09-25 16:07:19 -070062
63int drbd_init(void);
64static int drbd_open(struct block_device *bdev, fmode_t mode);
Al Virodb2a1442013-05-05 21:52:57 -040065static void drbd_release(struct gendisk *gd, fmode_t mode);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +010066static int w_md_sync(struct drbd_work *w, int unused);
Philipp Reisnerb411b362009-09-25 16:07:19 -070067static void md_sync_timer_fn(unsigned long data);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +010068static int w_bitmap_io(struct drbd_work *w, int unused);
69static int w_go_diskless(struct drbd_work *w, int unused);
Philipp Reisnerb411b362009-09-25 16:07:19 -070070
Philipp Reisnerb411b362009-09-25 16:07:19 -070071MODULE_AUTHOR("Philipp Reisner <phil@linbit.com>, "
72 "Lars Ellenberg <lars@linbit.com>");
73MODULE_DESCRIPTION("drbd - Distributed Replicated Block Device v" REL_VERSION);
74MODULE_VERSION(REL_VERSION);
75MODULE_LICENSE("GPL");
Philipp Reisner81a5d602011-02-22 19:53:16 -050076MODULE_PARM_DESC(minor_count, "Approximate number of drbd devices ("
Philipp Reisner2b8a90b2011-01-10 11:15:17 +010077 __stringify(DRBD_MINOR_COUNT_MIN) "-" __stringify(DRBD_MINOR_COUNT_MAX) ")");
Philipp Reisnerb411b362009-09-25 16:07:19 -070078MODULE_ALIAS_BLOCKDEV_MAJOR(DRBD_MAJOR);
79
80#include <linux/moduleparam.h>
81/* allow_open_on_secondary */
82MODULE_PARM_DESC(allow_oos, "DONT USE!");
83/* thanks to these macros, if compiled into the kernel (not-module),
84 * this becomes the boot parameter drbd.minor_count */
85module_param(minor_count, uint, 0444);
86module_param(disable_sendpage, bool, 0644);
87module_param(allow_oos, bool, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -070088module_param(proc_details, int, 0644);
89
90#ifdef CONFIG_DRBD_FAULT_INJECTION
91int enable_faults;
92int fault_rate;
93static int fault_count;
94int fault_devs;
95/* bitmap of enabled faults */
96module_param(enable_faults, int, 0664);
97/* fault rate % value - applies to all enabled faults */
98module_param(fault_rate, int, 0664);
99/* count of faults inserted */
100module_param(fault_count, int, 0664);
101/* bitmap of devices to insert faults on */
102module_param(fault_devs, int, 0644);
103#endif
104
105/* module parameter, defined */
Philipp Reisner2b8a90b2011-01-10 11:15:17 +0100106unsigned int minor_count = DRBD_MINOR_COUNT_DEF;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030107bool disable_sendpage;
108bool allow_oos;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700109int proc_details; /* Detail level in proc drbd*/
110
111/* Module parameter for setting the user mode helper program
112 * to run. Default is /sbin/drbdadm */
113char usermode_helper[80] = "/sbin/drbdadm";
114
115module_param_string(usermode_helper, usermode_helper, sizeof(usermode_helper), 0644);
116
117/* in 2.6.x, our device mapping and config info contains our virtual gendisks
118 * as member "struct gendisk *vdisk;"
119 */
Philipp Reisner81a5d602011-02-22 19:53:16 -0500120struct idr minors;
Philipp Reisner21114382011-01-19 12:26:59 +0100121struct list_head drbd_tconns; /* list of struct drbd_tconn */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700122
123struct kmem_cache *drbd_request_cache;
Andreas Gruenbacher6c852be2011-02-04 15:38:52 +0100124struct kmem_cache *drbd_ee_cache; /* peer requests */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700125struct kmem_cache *drbd_bm_ext_cache; /* bitmap extents */
126struct kmem_cache *drbd_al_ext_cache; /* activity log extents */
127mempool_t *drbd_request_mempool;
128mempool_t *drbd_ee_mempool;
Lars Ellenberg42818082011-02-23 12:39:46 +0100129mempool_t *drbd_md_io_page_pool;
Lars Ellenberg9476f392011-02-23 17:02:01 +0100130struct bio_set *drbd_md_io_bio_set;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700131
132/* I do not use a standard mempool, because:
133 1) I want to hand out the pre-allocated objects first.
134 2) I want to be able to interrupt sleeping allocation with a signal.
135 Note: This is a single linked list, the next pointer is the private
136 member of struct page.
137 */
138struct page *drbd_pp_pool;
139spinlock_t drbd_pp_lock;
140int drbd_pp_vacant;
141wait_queue_head_t drbd_pp_wait;
142
143DEFINE_RATELIMIT_STATE(drbd_ratelimit_state, 5 * HZ, 5);
144
Emese Revfy7d4e9d02009-12-14 00:59:30 +0100145static const struct block_device_operations drbd_ops = {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700146 .owner = THIS_MODULE,
147 .open = drbd_open,
148 .release = drbd_release,
149};
150
Lars Ellenberg9476f392011-02-23 17:02:01 +0100151struct bio *bio_alloc_drbd(gfp_t gfp_mask)
152{
Lars Ellenbergda4a75d2011-02-23 17:02:01 +0100153 struct bio *bio;
154
Lars Ellenberg9476f392011-02-23 17:02:01 +0100155 if (!drbd_md_io_bio_set)
156 return bio_alloc(gfp_mask, 1);
157
Lars Ellenbergda4a75d2011-02-23 17:02:01 +0100158 bio = bio_alloc_bioset(gfp_mask, 1, drbd_md_io_bio_set);
159 if (!bio)
160 return NULL;
Lars Ellenbergda4a75d2011-02-23 17:02:01 +0100161 return bio;
Lars Ellenberg9476f392011-02-23 17:02:01 +0100162}
163
Philipp Reisnerb411b362009-09-25 16:07:19 -0700164#ifdef __CHECKER__
165/* When checking with sparse, and this is an inline function, sparse will
166 give tons of false positives. When this is a real functions sparse works.
167 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200168int _get_ldev_if_state(struct drbd_device *device, enum drbd_disk_state mins)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700169{
170 int io_allowed;
171
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200172 atomic_inc(&device->local_cnt);
173 io_allowed = (device->state.disk >= mins);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700174 if (!io_allowed) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200175 if (atomic_dec_and_test(&device->local_cnt))
176 wake_up(&device->misc_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700177 }
178 return io_allowed;
179}
180
181#endif
182
183/**
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100184 * tl_release() - mark as BARRIER_ACKED all requests in the corresponding transfer log epoch
185 * @tconn: DRBD connection.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700186 * @barrier_nr: Expected identifier of the DRBD write barrier packet.
187 * @set_size: Expected number of requests before that barrier.
188 *
189 * In case the passed barrier_nr or set_size does not match the oldest
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100190 * epoch of not yet barrier-acked requests, this function will cause a
191 * termination of the connection.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700192 */
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100193void tl_release(struct drbd_tconn *tconn, unsigned int barrier_nr,
194 unsigned int set_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700195{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700196 struct drbd_request *r;
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100197 struct drbd_request *req = NULL;
198 int expect_epoch = 0;
199 int expect_size = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700200
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100201 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700202
Philipp Reisner98683652012-11-09 14:18:43 +0100203 /* find oldest not yet barrier-acked write request,
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100204 * count writes in its epoch. */
205 list_for_each_entry(r, &tconn->transfer_log, tl_requests) {
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100206 const unsigned s = r->rq_state;
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100207 if (!req) {
208 if (!(s & RQ_WRITE))
209 continue;
210 if (!(s & RQ_NET_MASK))
211 continue;
212 if (s & RQ_NET_DONE)
213 continue;
214 req = r;
215 expect_epoch = req->epoch;
216 expect_size ++;
217 } else {
218 if (r->epoch != expect_epoch)
219 break;
220 if (!(s & RQ_WRITE))
221 continue;
222 /* if (s & RQ_DONE): not expected */
223 /* if (!(s & RQ_NET_MASK)): not expected */
224 expect_size++;
225 }
226 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700227
228 /* first some paranoia code */
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100229 if (req == NULL) {
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100230 conn_err(tconn, "BAD! BarrierAck #%u received, but no epoch in tl!?\n",
231 barrier_nr);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700232 goto bail;
233 }
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100234 if (expect_epoch != barrier_nr) {
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100235 conn_err(tconn, "BAD! BarrierAck #%u received, expected #%u!\n",
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100236 barrier_nr, expect_epoch);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700237 goto bail;
238 }
239
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100240 if (expect_size != set_size) {
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100241 conn_err(tconn, "BAD! BarrierAck #%u received with n_writes=%u, expected n_writes=%u!\n",
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100242 barrier_nr, set_size, expect_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700243 goto bail;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700244 }
245
Philipp Reisner98683652012-11-09 14:18:43 +0100246 /* Clean up list of requests processed during current epoch. */
247 /* this extra list walk restart is paranoia,
248 * to catch requests being barrier-acked "unexpectedly".
249 * It usually should find the same req again, or some READ preceding it. */
250 list_for_each_entry(req, &tconn->transfer_log, tl_requests)
251 if (req->epoch == expect_epoch)
252 break;
253 list_for_each_entry_safe_from(req, r, &tconn->transfer_log, tl_requests) {
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100254 if (req->epoch != expect_epoch)
255 break;
256 _req_mod(req, BARRIER_ACKED);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700257 }
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100258 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700259
260 return;
261
262bail:
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100263 spin_unlock_irq(&tconn->req_lock);
264 conn_request_state(tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700265}
266
Philipp Reisner617049a2010-12-22 12:48:31 +0100267
Philipp Reisner11b58e72010-05-12 17:08:26 +0200268/**
269 * _tl_restart() - Walks the transfer log, and applies an action to all requests
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200270 * @device: DRBD device.
Philipp Reisner11b58e72010-05-12 17:08:26 +0200271 * @what: The action/event to perform with all request objects
272 *
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100273 * @what might be one of CONNECTION_LOST_WHILE_PENDING, RESEND, FAIL_FROZEN_DISK_IO,
274 * RESTART_FROZEN_DISK_IO.
Philipp Reisner11b58e72010-05-12 17:08:26 +0200275 */
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100276/* must hold resource->req_lock */
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100277void _tl_restart(struct drbd_tconn *tconn, enum drbd_req_event what)
Philipp Reisner11b58e72010-05-12 17:08:26 +0200278{
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100279 struct drbd_request *req, *r;
Philipp Reisner11b58e72010-05-12 17:08:26 +0200280
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100281 list_for_each_entry_safe(req, r, &tconn->transfer_log, tl_requests)
Philipp Reisner509fc012012-07-31 11:22:58 +0200282 _req_mod(req, what);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200283}
284
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100285void tl_restart(struct drbd_tconn *tconn, enum drbd_req_event what)
286{
287 spin_lock_irq(&tconn->req_lock);
288 _tl_restart(tconn, what);
289 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnercdfda632011-07-05 15:38:59 +0200290}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700291
292/**
293 * tl_clear() - Clears all requests and &struct drbd_tl_epoch objects out of the TL
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200294 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700295 *
296 * This is called after the connection to the peer was lost. The storage covered
297 * by the requests on the transfer gets marked as our of sync. Called from the
298 * receiver thread and the worker thread.
299 */
Philipp Reisner2f5cdd02011-02-21 14:29:27 +0100300void tl_clear(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700301{
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100302 tl_restart(tconn, CONNECTION_LOST_WHILE_PENDING);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700303}
304
305/**
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200306 * tl_abort_disk_io() - Abort disk I/O for all requests for a certain device in the TL
307 * @device: DRBD device.
Philipp Reisnerfd2491f2011-07-18 16:25:15 +0200308 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200309void tl_abort_disk_io(struct drbd_device *device)
Philipp Reisnerfd2491f2011-07-18 16:25:15 +0200310{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200311 struct drbd_tconn *tconn = device->tconn;
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100312 struct drbd_request *req, *r;
Philipp Reisnerfd2491f2011-07-18 16:25:15 +0200313
Philipp Reisnercdfda632011-07-05 15:38:59 +0200314 spin_lock_irq(&tconn->req_lock);
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100315 list_for_each_entry_safe(req, r, &tconn->transfer_log, tl_requests) {
Philipp Reisnerfd2491f2011-07-18 16:25:15 +0200316 if (!(req->rq_state & RQ_LOCAL_PENDING))
317 continue;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200318 if (req->w.device != device)
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100319 continue;
320 _req_mod(req, ABORT_DISK_IO);
Philipp Reisnerfd2491f2011-07-18 16:25:15 +0200321 }
Philipp Reisnercdfda632011-07-05 15:38:59 +0200322 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnerfd2491f2011-07-18 16:25:15 +0200323}
324
Philipp Reisnerb411b362009-09-25 16:07:19 -0700325static int drbd_thread_setup(void *arg)
326{
327 struct drbd_thread *thi = (struct drbd_thread *) arg;
Philipp Reisner392c8802011-02-09 10:33:31 +0100328 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700329 unsigned long flags;
330 int retval;
331
Philipp Reisnerf1b3a6e2011-02-08 15:35:58 +0100332 snprintf(current->comm, sizeof(current->comm), "drbd_%c_%s",
Philipp Reisner392c8802011-02-09 10:33:31 +0100333 thi->name[0], thi->tconn->name);
Philipp Reisnerf1b3a6e2011-02-08 15:35:58 +0100334
Philipp Reisnerb411b362009-09-25 16:07:19 -0700335restart:
336 retval = thi->function(thi);
337
338 spin_lock_irqsave(&thi->t_lock, flags);
339
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100340 /* if the receiver has been "EXITING", the last thing it did
Philipp Reisnerb411b362009-09-25 16:07:19 -0700341 * was set the conn state to "StandAlone",
342 * if now a re-connect request comes in, conn state goes C_UNCONNECTED,
343 * and receiver thread will be "started".
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100344 * drbd_thread_start needs to set "RESTARTING" in that case.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700345 * t_state check and assignment needs to be within the same spinlock,
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100346 * so either thread_start sees EXITING, and can remap to RESTARTING,
347 * or thread_start see NONE, and can proceed as normal.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700348 */
349
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100350 if (thi->t_state == RESTARTING) {
Philipp Reisner392c8802011-02-09 10:33:31 +0100351 conn_info(tconn, "Restarting %s thread\n", thi->name);
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100352 thi->t_state = RUNNING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700353 spin_unlock_irqrestore(&thi->t_lock, flags);
354 goto restart;
355 }
356
357 thi->task = NULL;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100358 thi->t_state = NONE;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700359 smp_mb();
Lars Ellenberg992d6e92011-05-02 11:47:18 +0200360 complete_all(&thi->stop);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700361 spin_unlock_irqrestore(&thi->t_lock, flags);
362
Philipp Reisner392c8802011-02-09 10:33:31 +0100363 conn_info(tconn, "Terminating %s\n", current->comm);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700364
365 /* Release mod reference taken when thread was started */
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200366
367 kref_put(&tconn->kref, &conn_destroy);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700368 module_put(THIS_MODULE);
369 return retval;
370}
371
Philipp Reisner392c8802011-02-09 10:33:31 +0100372static void drbd_thread_init(struct drbd_tconn *tconn, struct drbd_thread *thi,
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100373 int (*func) (struct drbd_thread *), char *name)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700374{
375 spin_lock_init(&thi->t_lock);
376 thi->task = NULL;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100377 thi->t_state = NONE;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700378 thi->function = func;
Philipp Reisner392c8802011-02-09 10:33:31 +0100379 thi->tconn = tconn;
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100380 strncpy(thi->name, name, ARRAY_SIZE(thi->name));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700381}
382
383int drbd_thread_start(struct drbd_thread *thi)
384{
Philipp Reisner392c8802011-02-09 10:33:31 +0100385 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700386 struct task_struct *nt;
387 unsigned long flags;
388
Philipp Reisnerb411b362009-09-25 16:07:19 -0700389 /* is used from state engine doing drbd_thread_stop_nowait,
390 * while holding the req lock irqsave */
391 spin_lock_irqsave(&thi->t_lock, flags);
392
393 switch (thi->t_state) {
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100394 case NONE:
Philipp Reisner392c8802011-02-09 10:33:31 +0100395 conn_info(tconn, "Starting %s thread (from %s [%d])\n",
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100396 thi->name, current->comm, current->pid);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700397
398 /* Get ref on module for thread - this is released when thread exits */
399 if (!try_module_get(THIS_MODULE)) {
Philipp Reisner392c8802011-02-09 10:33:31 +0100400 conn_err(tconn, "Failed to get module reference in drbd_thread_start\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700401 spin_unlock_irqrestore(&thi->t_lock, flags);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100402 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700403 }
404
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200405 kref_get(&thi->tconn->kref);
406
Philipp Reisnerb411b362009-09-25 16:07:19 -0700407 init_completion(&thi->stop);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700408 thi->reset_cpu_mask = 1;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100409 thi->t_state = RUNNING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700410 spin_unlock_irqrestore(&thi->t_lock, flags);
411 flush_signals(current); /* otherw. may get -ERESTARTNOINTR */
412
413 nt = kthread_create(drbd_thread_setup, (void *) thi,
Philipp Reisner392c8802011-02-09 10:33:31 +0100414 "drbd_%c_%s", thi->name[0], thi->tconn->name);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700415
416 if (IS_ERR(nt)) {
Philipp Reisner392c8802011-02-09 10:33:31 +0100417 conn_err(tconn, "Couldn't start thread\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700418
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200419 kref_put(&tconn->kref, &conn_destroy);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700420 module_put(THIS_MODULE);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100421 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700422 }
423 spin_lock_irqsave(&thi->t_lock, flags);
424 thi->task = nt;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100425 thi->t_state = RUNNING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700426 spin_unlock_irqrestore(&thi->t_lock, flags);
427 wake_up_process(nt);
428 break;
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100429 case EXITING:
430 thi->t_state = RESTARTING;
Philipp Reisner392c8802011-02-09 10:33:31 +0100431 conn_info(tconn, "Restarting %s thread (from %s [%d])\n",
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100432 thi->name, current->comm, current->pid);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700433 /* fall through */
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100434 case RUNNING:
435 case RESTARTING:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700436 default:
437 spin_unlock_irqrestore(&thi->t_lock, flags);
438 break;
439 }
440
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100441 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700442}
443
444
445void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait)
446{
447 unsigned long flags;
448
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100449 enum drbd_thread_state ns = restart ? RESTARTING : EXITING;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700450
451 /* may be called from state engine, holding the req lock irqsave */
452 spin_lock_irqsave(&thi->t_lock, flags);
453
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +0100454 if (thi->t_state == NONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700455 spin_unlock_irqrestore(&thi->t_lock, flags);
456 if (restart)
457 drbd_thread_start(thi);
458 return;
459 }
460
461 if (thi->t_state != ns) {
462 if (thi->task == NULL) {
463 spin_unlock_irqrestore(&thi->t_lock, flags);
464 return;
465 }
466
467 thi->t_state = ns;
468 smp_mb();
469 init_completion(&thi->stop);
470 if (thi->task != current)
471 force_sig(DRBD_SIGKILL, thi->task);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700472 }
473
474 spin_unlock_irqrestore(&thi->t_lock, flags);
475
476 if (wait)
477 wait_for_completion(&thi->stop);
478}
479
Philipp Reisner392c8802011-02-09 10:33:31 +0100480static struct drbd_thread *drbd_task_to_thread(struct drbd_tconn *tconn, struct task_struct *task)
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100481{
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100482 struct drbd_thread *thi =
483 task == tconn->receiver.task ? &tconn->receiver :
484 task == tconn->asender.task ? &tconn->asender :
485 task == tconn->worker.task ? &tconn->worker : NULL;
486
487 return thi;
488}
489
Philipp Reisner392c8802011-02-09 10:33:31 +0100490char *drbd_task_to_thread_name(struct drbd_tconn *tconn, struct task_struct *task)
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100491{
Philipp Reisner392c8802011-02-09 10:33:31 +0100492 struct drbd_thread *thi = drbd_task_to_thread(tconn, task);
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100493 return thi ? thi->name : task->comm;
494}
495
Philipp Reisner80883192011-02-18 14:56:45 +0100496int conn_lowest_minor(struct drbd_tconn *tconn)
Philipp Reisner80822282011-02-08 12:46:30 +0100497{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200498 struct drbd_device *device;
Philipp Reisner695d08f2011-04-11 22:53:32 -0700499 int vnr = 0, m;
Philipp Reisner774b3052011-02-22 02:07:03 -0500500
Philipp Reisner695d08f2011-04-11 22:53:32 -0700501 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200502 device = idr_get_next(&tconn->volumes, &vnr);
503 m = device ? device_to_minor(device) : -1;
Philipp Reisner695d08f2011-04-11 22:53:32 -0700504 rcu_read_unlock();
505
506 return m;
Philipp Reisner80822282011-02-08 12:46:30 +0100507}
Philipp Reisner774b3052011-02-22 02:07:03 -0500508
Philipp Reisnerb411b362009-09-25 16:07:19 -0700509#ifdef CONFIG_SMP
510/**
511 * drbd_calc_cpu_mask() - Generate CPU masks, spread over all CPUs
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200512 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700513 *
514 * Forces all threads of a device onto the same CPU. This is beneficial for
515 * DRBD's performance. May be overwritten by user's configuration.
516 */
Philipp Reisner80822282011-02-08 12:46:30 +0100517void drbd_calc_cpu_mask(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700518{
519 int ord, cpu;
520
521 /* user override. */
Philipp Reisner80822282011-02-08 12:46:30 +0100522 if (cpumask_weight(tconn->cpu_mask))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700523 return;
524
Philipp Reisner80822282011-02-08 12:46:30 +0100525 ord = conn_lowest_minor(tconn) % cpumask_weight(cpu_online_mask);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700526 for_each_online_cpu(cpu) {
527 if (ord-- == 0) {
Philipp Reisner80822282011-02-08 12:46:30 +0100528 cpumask_set_cpu(cpu, tconn->cpu_mask);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700529 return;
530 }
531 }
532 /* should not be reached */
Philipp Reisner80822282011-02-08 12:46:30 +0100533 cpumask_setall(tconn->cpu_mask);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700534}
535
536/**
537 * drbd_thread_current_set_cpu() - modifies the cpu mask of the _current_ thread
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200538 * @device: DRBD device.
Philipp Reisnerbc31fe32011-02-07 11:14:38 +0100539 * @thi: drbd_thread object
Philipp Reisnerb411b362009-09-25 16:07:19 -0700540 *
541 * call in the "main loop" of _all_ threads, no need for any mutex, current won't die
542 * prematurely.
543 */
Philipp Reisner80822282011-02-08 12:46:30 +0100544void drbd_thread_current_set_cpu(struct drbd_thread *thi)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700545{
546 struct task_struct *p = current;
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100547
Philipp Reisnerb411b362009-09-25 16:07:19 -0700548 if (!thi->reset_cpu_mask)
549 return;
550 thi->reset_cpu_mask = 0;
Philipp Reisner392c8802011-02-09 10:33:31 +0100551 set_cpus_allowed_ptr(p, thi->tconn->cpu_mask);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700552}
553#endif
554
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +0200555/**
556 * drbd_header_size - size of a packet header
557 *
558 * The header size is a multiple of 8, so any payload following the header is
559 * word aligned on 64-bit architectures. (The bitmap send and receive code
560 * relies on this.)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700561 */
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +0200562unsigned int drbd_header_size(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700563{
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +0200564 if (tconn->agreed_pro_version >= 100) {
565 BUILD_BUG_ON(!IS_ALIGNED(sizeof(struct p_header100), 8));
566 return sizeof(struct p_header100);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700567 } else {
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +0200568 BUILD_BUG_ON(sizeof(struct p_header80) !=
569 sizeof(struct p_header95));
570 BUILD_BUG_ON(!IS_ALIGNED(sizeof(struct p_header80), 8));
571 return sizeof(struct p_header80);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700572 }
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +0200573}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700574
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200575static unsigned int prepare_header80(struct p_header80 *h, enum drbd_packet cmd, int size)
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100576{
577 h->magic = cpu_to_be32(DRBD_MAGIC);
578 h->command = cpu_to_be16(cmd);
579 h->length = cpu_to_be16(size);
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200580 return sizeof(struct p_header80);
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100581}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700582
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200583static unsigned int prepare_header95(struct p_header95 *h, enum drbd_packet cmd, int size)
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100584{
585 h->magic = cpu_to_be16(DRBD_MAGIC_BIG);
586 h->command = cpu_to_be16(cmd);
Andreas Gruenbacherb55d84b2011-03-22 13:17:47 +0100587 h->length = cpu_to_be32(size);
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200588 return sizeof(struct p_header95);
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100589}
590
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +0200591static unsigned int prepare_header100(struct p_header100 *h, enum drbd_packet cmd,
592 int size, int vnr)
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100593{
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +0200594 h->magic = cpu_to_be32(DRBD_MAGIC_100);
595 h->volume = cpu_to_be16(vnr);
596 h->command = cpu_to_be16(cmd);
597 h->length = cpu_to_be32(size);
598 h->pad = 0;
599 return sizeof(struct p_header100);
600}
601
602static unsigned int prepare_header(struct drbd_tconn *tconn, int vnr,
603 void *buffer, enum drbd_packet cmd, int size)
604{
605 if (tconn->agreed_pro_version >= 100)
606 return prepare_header100(buffer, cmd, size, vnr);
607 else if (tconn->agreed_pro_version >= 95 &&
608 size > DRBD_MAX_SIZE_H80_PACKET)
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200609 return prepare_header95(buffer, cmd, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700610 else
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200611 return prepare_header80(buffer, cmd, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700612}
613
Andreas Gruenbachera7eb7bd2011-04-29 13:19:58 +0200614static void *__conn_prepare_command(struct drbd_tconn *tconn,
615 struct drbd_socket *sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700616{
Andreas Gruenbachera7eb7bd2011-04-29 13:19:58 +0200617 if (!sock->socket)
618 return NULL;
619 return sock->sbuf + drbd_header_size(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700620}
621
Andreas Gruenbacherdba58582011-03-29 16:55:40 +0200622void *conn_prepare_command(struct drbd_tconn *tconn, struct drbd_socket *sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700623{
Andreas Gruenbachera7eb7bd2011-04-29 13:19:58 +0200624 void *p;
625
Andreas Gruenbacherdba58582011-03-29 16:55:40 +0200626 mutex_lock(&sock->mutex);
Andreas Gruenbachera7eb7bd2011-04-29 13:19:58 +0200627 p = __conn_prepare_command(tconn, sock);
628 if (!p)
Andreas Gruenbacherdba58582011-03-29 16:55:40 +0200629 mutex_unlock(&sock->mutex);
Andreas Gruenbachera7eb7bd2011-04-29 13:19:58 +0200630
631 return p;
Andreas Gruenbacherdba58582011-03-29 16:55:40 +0200632}
633
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200634void *drbd_prepare_command(struct drbd_device *device, struct drbd_socket *sock)
Andreas Gruenbacherdba58582011-03-29 16:55:40 +0200635{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200636 return conn_prepare_command(device->tconn, sock);
Andreas Gruenbacherdba58582011-03-29 16:55:40 +0200637}
638
639static int __send_command(struct drbd_tconn *tconn, int vnr,
640 struct drbd_socket *sock, enum drbd_packet cmd,
641 unsigned int header_size, void *data,
642 unsigned int size)
643{
644 int msg_flags;
645 int err;
646
647 /*
648 * Called with @data == NULL and the size of the data blocks in @size
649 * for commands that send data blocks. For those commands, omit the
650 * MSG_MORE flag: this will increase the likelihood that data blocks
651 * which are page aligned on the sender will end up page aligned on the
652 * receiver.
653 */
654 msg_flags = data ? MSG_MORE : 0;
655
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200656 header_size += prepare_header(tconn, vnr, sock->sbuf, cmd,
657 header_size + size);
Andreas Gruenbacherdba58582011-03-29 16:55:40 +0200658 err = drbd_send_all(tconn, sock->socket, sock->sbuf, header_size,
659 msg_flags);
660 if (data && !err)
661 err = drbd_send_all(tconn, sock->socket, data, size, 0);
662 return err;
663}
664
Andreas Gruenbachera7eb7bd2011-04-29 13:19:58 +0200665static int __conn_send_command(struct drbd_tconn *tconn, struct drbd_socket *sock,
666 enum drbd_packet cmd, unsigned int header_size,
667 void *data, unsigned int size)
668{
669 return __send_command(tconn, 0, sock, cmd, header_size, data, size);
670}
671
Andreas Gruenbacherdba58582011-03-29 16:55:40 +0200672int conn_send_command(struct drbd_tconn *tconn, struct drbd_socket *sock,
673 enum drbd_packet cmd, unsigned int header_size,
674 void *data, unsigned int size)
675{
676 int err;
677
Andreas Gruenbachera7eb7bd2011-04-29 13:19:58 +0200678 err = __conn_send_command(tconn, sock, cmd, header_size, data, size);
Andreas Gruenbacherdba58582011-03-29 16:55:40 +0200679 mutex_unlock(&sock->mutex);
680 return err;
681}
682
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200683int drbd_send_command(struct drbd_device *device, struct drbd_socket *sock,
Andreas Gruenbacherdba58582011-03-29 16:55:40 +0200684 enum drbd_packet cmd, unsigned int header_size,
685 void *data, unsigned int size)
686{
687 int err;
688
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200689 err = __send_command(device->tconn, device->vnr, sock, cmd, header_size,
Andreas Gruenbacherdba58582011-03-29 16:55:40 +0200690 data, size);
691 mutex_unlock(&sock->mutex);
692 return err;
693}
694
Andreas Gruenbachere307f352011-03-22 10:55:48 +0100695int drbd_send_ping(struct drbd_tconn *tconn)
696{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200697 struct drbd_socket *sock;
698
699 sock = &tconn->meta;
700 if (!conn_prepare_command(tconn, sock))
701 return -EIO;
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200702 return conn_send_command(tconn, sock, P_PING, 0, NULL, 0);
Andreas Gruenbachere307f352011-03-22 10:55:48 +0100703}
704
705int drbd_send_ping_ack(struct drbd_tconn *tconn)
706{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200707 struct drbd_socket *sock;
708
709 sock = &tconn->meta;
710 if (!conn_prepare_command(tconn, sock))
711 return -EIO;
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200712 return conn_send_command(tconn, sock, P_PING_ACK, 0, NULL, 0);
Andreas Gruenbachere307f352011-03-22 10:55:48 +0100713}
714
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200715int drbd_send_sync_param(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700716{
Andreas Gruenbacher7c967152011-03-22 00:49:36 +0100717 struct drbd_socket *sock;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +0200718 struct p_rs_param_95 *p;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200719 int size;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200720 const int apv = device->tconn->agreed_pro_version;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200721 enum drbd_packet cmd;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200722 struct net_conf *nc;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200723 struct disk_conf *dc;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200724
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200725 sock = &device->tconn->data;
726 p = drbd_prepare_command(device, sock);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200727 if (!p)
728 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700729
Philipp Reisner44ed1672011-04-19 17:10:19 +0200730 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200731 nc = rcu_dereference(device->tconn->net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700732
733 size = apv <= 87 ? sizeof(struct p_rs_param)
734 : apv == 88 ? sizeof(struct p_rs_param)
Philipp Reisner44ed1672011-04-19 17:10:19 +0200735 + strlen(nc->verify_alg) + 1
Philipp Reisner8e26f9c2010-07-06 17:25:54 +0200736 : apv <= 94 ? sizeof(struct p_rs_param_89)
737 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700738
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200739 cmd = apv >= 89 ? P_SYNC_PARAM89 : P_SYNC_PARAM;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700740
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200741 /* initialize verify_alg and csums_alg */
742 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700743
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200744 if (get_ldev(device)) {
745 dc = rcu_dereference(device->ldev->disk_conf);
Andreas Gruenbacher6394b932011-05-11 14:29:52 +0200746 p->resync_rate = cpu_to_be32(dc->resync_rate);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200747 p->c_plan_ahead = cpu_to_be32(dc->c_plan_ahead);
748 p->c_delay_target = cpu_to_be32(dc->c_delay_target);
749 p->c_fill_target = cpu_to_be32(dc->c_fill_target);
750 p->c_max_rate = cpu_to_be32(dc->c_max_rate);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200751 put_ldev(device);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200752 } else {
Andreas Gruenbacher6394b932011-05-11 14:29:52 +0200753 p->resync_rate = cpu_to_be32(DRBD_RESYNC_RATE_DEF);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200754 p->c_plan_ahead = cpu_to_be32(DRBD_C_PLAN_AHEAD_DEF);
755 p->c_delay_target = cpu_to_be32(DRBD_C_DELAY_TARGET_DEF);
756 p->c_fill_target = cpu_to_be32(DRBD_C_FILL_TARGET_DEF);
757 p->c_max_rate = cpu_to_be32(DRBD_C_MAX_RATE_DEF);
758 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700759
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200760 if (apv >= 88)
Philipp Reisner44ed1672011-04-19 17:10:19 +0200761 strcpy(p->verify_alg, nc->verify_alg);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200762 if (apv >= 89)
Philipp Reisner44ed1672011-04-19 17:10:19 +0200763 strcpy(p->csums_alg, nc->csums_alg);
764 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700765
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200766 return drbd_send_command(device, sock, cmd, size, NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700767}
768
Philipp Reisnerd659f2a2011-05-16 17:38:45 +0200769int __drbd_send_protocol(struct drbd_tconn *tconn, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700770{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200771 struct drbd_socket *sock;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700772 struct p_protocol *p;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200773 struct net_conf *nc;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200774 int size, cf;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700775
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200776 sock = &tconn->data;
Andreas Gruenbachera7eb7bd2011-04-29 13:19:58 +0200777 p = __conn_prepare_command(tconn, sock);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200778 if (!p)
779 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700780
Philipp Reisner44ed1672011-04-19 17:10:19 +0200781 rcu_read_lock();
782 nc = rcu_dereference(tconn->net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700783
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +0200784 if (nc->tentative && tconn->agreed_pro_version < 92) {
Philipp Reisner44ed1672011-04-19 17:10:19 +0200785 rcu_read_unlock();
786 mutex_unlock(&sock->mutex);
787 conn_err(tconn, "--dry-run is not supported by peer");
788 return -EOPNOTSUPP;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100789 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200790
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200791 size = sizeof(*p);
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100792 if (tconn->agreed_pro_version >= 87)
Philipp Reisner44ed1672011-04-19 17:10:19 +0200793 size += strlen(nc->integrity_alg) + 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700794
Philipp Reisner44ed1672011-04-19 17:10:19 +0200795 p->protocol = cpu_to_be32(nc->wire_protocol);
796 p->after_sb_0p = cpu_to_be32(nc->after_sb_0p);
797 p->after_sb_1p = cpu_to_be32(nc->after_sb_1p);
798 p->after_sb_2p = cpu_to_be32(nc->after_sb_2p);
799 p->two_primaries = cpu_to_be32(nc->two_primaries);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100800 cf = 0;
Andreas Gruenbacher6139f602011-05-06 20:00:02 +0200801 if (nc->discard_my_data)
802 cf |= CF_DISCARD_MY_DATA;
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +0200803 if (nc->tentative)
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200804 cf |= CF_DRY_RUN;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +0100805 p->conn_flags = cpu_to_be32(cf);
806
Philipp Reisnerdc8228d2011-02-08 10:13:15 +0100807 if (tconn->agreed_pro_version >= 87)
Philipp Reisner44ed1672011-04-19 17:10:19 +0200808 strcpy(p->integrity_alg, nc->integrity_alg);
809 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700810
Philipp Reisnerd659f2a2011-05-16 17:38:45 +0200811 return __conn_send_command(tconn, sock, cmd, size, NULL, 0);
Andreas Gruenbachera7eb7bd2011-04-29 13:19:58 +0200812}
813
814int drbd_send_protocol(struct drbd_tconn *tconn)
815{
816 int err;
817
818 mutex_lock(&tconn->data.mutex);
Philipp Reisnerd659f2a2011-05-16 17:38:45 +0200819 err = __drbd_send_protocol(tconn, P_PROTOCOL);
Andreas Gruenbachera7eb7bd2011-04-29 13:19:58 +0200820 mutex_unlock(&tconn->data.mutex);
821
822 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700823}
824
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200825static int _drbd_send_uuids(struct drbd_device *device, u64 uuid_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700826{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200827 struct drbd_socket *sock;
828 struct p_uuids *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700829 int i;
830
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200831 if (!get_ldev_if_state(device, D_NEGOTIATING))
Andreas Gruenbacher2ae5f952011-03-16 01:07:20 +0100832 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700833
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200834 sock = &device->tconn->data;
835 p = drbd_prepare_command(device, sock);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200836 if (!p) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200837 put_ldev(device);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200838 return -EIO;
839 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200840 spin_lock_irq(&device->ldev->md.uuid_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700841 for (i = UI_CURRENT; i < UI_SIZE; i++)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200842 p->uuid[i] = cpu_to_be64(device->ldev->md.uuid[i]);
843 spin_unlock_irq(&device->ldev->md.uuid_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700844
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200845 device->comm_bm_set = drbd_bm_total_weight(device);
846 p->uuid[UI_SIZE] = cpu_to_be64(device->comm_bm_set);
Philipp Reisner44ed1672011-04-19 17:10:19 +0200847 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200848 uuid_flags |= rcu_dereference(device->tconn->net_conf)->discard_my_data ? 1 : 0;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200849 rcu_read_unlock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200850 uuid_flags |= test_bit(CRASHED_PRIMARY, &device->flags) ? 2 : 0;
851 uuid_flags |= device->new_state_tmp.disk == D_INCONSISTENT ? 4 : 0;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200852 p->uuid[UI_FLAGS] = cpu_to_be64(uuid_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700853
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200854 put_ldev(device);
855 return drbd_send_command(device, sock, P_UUIDS, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700856}
857
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200858int drbd_send_uuids(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700859{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200860 return _drbd_send_uuids(device, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700861}
862
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200863int drbd_send_uuids_skip_initial_sync(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700864{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200865 return _drbd_send_uuids(device, 8);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700866}
867
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200868void drbd_print_uuids(struct drbd_device *device, const char *text)
Lars Ellenberg62b0da32011-01-20 13:25:21 +0100869{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200870 if (get_ldev_if_state(device, D_NEGOTIATING)) {
871 u64 *uuid = device->ldev->md.uuid;
Lars Ellenberg62b0da32011-01-20 13:25:21 +0100872 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX\n",
873 text,
874 (unsigned long long)uuid[UI_CURRENT],
875 (unsigned long long)uuid[UI_BITMAP],
876 (unsigned long long)uuid[UI_HISTORY_START],
877 (unsigned long long)uuid[UI_HISTORY_END]);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200878 put_ldev(device);
Lars Ellenberg62b0da32011-01-20 13:25:21 +0100879 } else {
880 dev_info(DEV, "%s effective data uuid: %016llX\n",
881 text,
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200882 (unsigned long long)device->ed_uuid);
Lars Ellenberg62b0da32011-01-20 13:25:21 +0100883 }
884}
885
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200886void drbd_gen_and_send_sync_uuid(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700887{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200888 struct drbd_socket *sock;
889 struct p_rs_uuid *p;
Lars Ellenberg5a22db82010-12-17 21:14:23 +0100890 u64 uuid;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700891
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200892 D_ASSERT(device->state.disk == D_UP_TO_DATE);
Lars Ellenberg5a22db82010-12-17 21:14:23 +0100893
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200894 uuid = device->ldev->md.uuid[UI_BITMAP];
Philipp Reisner5ba3dac2011-10-05 15:54:18 +0200895 if (uuid && uuid != UUID_JUST_CREATED)
896 uuid = uuid + UUID_NEW_BM_OFFSET;
897 else
898 get_random_bytes(&uuid, sizeof(u64));
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200899 drbd_uuid_set(device, UI_BITMAP, uuid);
900 drbd_print_uuids(device, "updated sync UUID");
901 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700902
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200903 sock = &device->tconn->data;
904 p = drbd_prepare_command(device, sock);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200905 if (p) {
906 p->uuid = cpu_to_be64(uuid);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200907 drbd_send_command(device, sock, P_SYNC_UUID, sizeof(*p), NULL, 0);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200908 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700909}
910
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200911int drbd_send_sizes(struct drbd_device *device, int trigger_reply, enum dds_flags flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700912{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200913 struct drbd_socket *sock;
914 struct p_sizes *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700915 sector_t d_size, u_size;
Lars Ellenbergdb141b22012-06-25 19:15:58 +0200916 int q_order_type;
917 unsigned int max_bio_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700918
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200919 if (get_ldev_if_state(device, D_NEGOTIATING)) {
920 D_ASSERT(device->ldev->backing_bdev);
921 d_size = drbd_get_max_capacity(device->ldev);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200922 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200923 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200924 rcu_read_unlock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200925 q_order_type = drbd_queue_order_type(device);
926 max_bio_size = queue_max_hw_sectors(device->ldev->backing_bdev->bd_disk->queue) << 9;
Lars Ellenbergdb141b22012-06-25 19:15:58 +0200927 max_bio_size = min(max_bio_size, DRBD_MAX_BIO_SIZE);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200928 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700929 } else {
930 d_size = 0;
931 u_size = 0;
932 q_order_type = QUEUE_ORDERED_NONE;
Philipp Reisner99432fc2011-05-20 16:39:13 +0200933 max_bio_size = DRBD_MAX_BIO_SIZE; /* ... multiple BIOs per peer_request */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700934 }
935
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200936 sock = &device->tconn->data;
937 p = drbd_prepare_command(device, sock);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200938 if (!p)
939 return -EIO;
Philipp Reisner2ffca4f2011-06-30 15:43:06 +0200940
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200941 if (device->tconn->agreed_pro_version <= 94)
Lars Ellenbergdb141b22012-06-25 19:15:58 +0200942 max_bio_size = min(max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200943 else if (device->tconn->agreed_pro_version < 100)
Philipp Reisner98683652012-11-09 14:18:43 +0100944 max_bio_size = min(max_bio_size, DRBD_MAX_BIO_SIZE_P95);
Philipp Reisner68093842011-06-30 15:43:06 +0200945
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200946 p->d_size = cpu_to_be64(d_size);
947 p->u_size = cpu_to_be64(u_size);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200948 p->c_size = cpu_to_be64(trigger_reply ? 0 : drbd_get_capacity(device->this_bdev));
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200949 p->max_bio_size = cpu_to_be32(max_bio_size);
950 p->queue_order_type = cpu_to_be16(q_order_type);
951 p->dds_flags = cpu_to_be16(flags);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200952 return drbd_send_command(device, sock, P_SIZES, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700953}
954
955/**
Lars Ellenbergf479ea02011-10-27 16:52:30 +0200956 * drbd_send_current_state() - Sends the drbd state to the peer
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200957 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700958 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200959int drbd_send_current_state(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700960{
Andreas Gruenbacher7c967152011-03-22 00:49:36 +0100961 struct drbd_socket *sock;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200962 struct p_state *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700963
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200964 sock = &device->tconn->data;
965 p = drbd_prepare_command(device, sock);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200966 if (!p)
967 return -EIO;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200968 p->state = cpu_to_be32(device->state.i); /* Within the send mutex */
969 return drbd_send_command(device, sock, P_STATE, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700970}
971
Lars Ellenbergf479ea02011-10-27 16:52:30 +0200972/**
973 * drbd_send_state() - After a state change, sends the new state to the peer
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200974 * @device: DRBD device.
Philipp Reisner43de7c82011-11-10 13:16:13 +0100975 * @state: the state to send, not necessarily the current state.
Lars Ellenbergf479ea02011-10-27 16:52:30 +0200976 *
977 * Each state change queues an "after_state_ch" work, which will eventually
978 * send the resulting new state to the peer. If more state changes happen
979 * between queuing and processing of the after_state_ch work, we still
980 * want to send each intermediary state in the order it occurred.
981 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200982int drbd_send_state(struct drbd_device *device, union drbd_state state)
Lars Ellenbergf479ea02011-10-27 16:52:30 +0200983{
Philipp Reisner43de7c82011-11-10 13:16:13 +0100984 struct drbd_socket *sock;
985 struct p_state *p;
Lars Ellenbergf479ea02011-10-27 16:52:30 +0200986
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200987 sock = &device->tconn->data;
988 p = drbd_prepare_command(device, sock);
Philipp Reisner43de7c82011-11-10 13:16:13 +0100989 if (!p)
990 return -EIO;
991 p->state = cpu_to_be32(state.i); /* Within the send mutex */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200992 return drbd_send_command(device, sock, P_STATE, sizeof(*p), NULL, 0);
Philipp Reisner43de7c82011-11-10 13:16:13 +0100993}
Lars Ellenbergf479ea02011-10-27 16:52:30 +0200994
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200995int drbd_send_state_req(struct drbd_device *device, union drbd_state mask, union drbd_state val)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700996{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200997 struct drbd_socket *sock;
998 struct p_req_state *p;
Lars Ellenbergf479ea02011-10-27 16:52:30 +0200999
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001000 sock = &device->tconn->data;
1001 p = drbd_prepare_command(device, sock);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001002 if (!p)
1003 return -EIO;
1004 p->mask = cpu_to_be32(mask.i);
1005 p->val = cpu_to_be32(val.i);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001006 return drbd_send_command(device, sock, P_STATE_CHG_REQ, sizeof(*p), NULL, 0);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001007}
1008
1009int conn_send_state_req(struct drbd_tconn *tconn, union drbd_state mask, union drbd_state val)
1010{
1011 enum drbd_packet cmd;
1012 struct drbd_socket *sock;
1013 struct p_req_state *p;
1014
1015 cmd = tconn->agreed_pro_version < 100 ? P_STATE_CHG_REQ : P_CONN_ST_CHG_REQ;
1016 sock = &tconn->data;
1017 p = conn_prepare_command(tconn, sock);
1018 if (!p)
1019 return -EIO;
1020 p->mask = cpu_to_be32(mask.i);
1021 p->val = cpu_to_be32(val.i);
1022 return conn_send_command(tconn, sock, cmd, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001023}
1024
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001025void drbd_send_sr_reply(struct drbd_device *device, enum drbd_state_rv retcode)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001026{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001027 struct drbd_socket *sock;
1028 struct p_req_state_reply *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001029
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001030 sock = &device->tconn->meta;
1031 p = drbd_prepare_command(device, sock);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001032 if (p) {
1033 p->retcode = cpu_to_be32(retcode);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001034 drbd_send_command(device, sock, P_STATE_CHG_REPLY, sizeof(*p), NULL, 0);
Lars Ellenbergf479ea02011-10-27 16:52:30 +02001035 }
Lars Ellenbergf479ea02011-10-27 16:52:30 +02001036}
1037
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001038void conn_send_sr_reply(struct drbd_tconn *tconn, enum drbd_state_rv retcode)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001039{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001040 struct drbd_socket *sock;
1041 struct p_req_state_reply *p;
Philipp Reisner047cd4a2011-02-15 11:09:33 +01001042 enum drbd_packet cmd = tconn->agreed_pro_version < 100 ? P_STATE_CHG_REPLY : P_CONN_ST_CHG_REPLY;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001043
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001044 sock = &tconn->meta;
1045 p = conn_prepare_command(tconn, sock);
1046 if (p) {
1047 p->retcode = cpu_to_be32(retcode);
1048 conn_send_command(tconn, sock, cmd, sizeof(*p), NULL, 0);
1049 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001050}
1051
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01001052static void dcbp_set_code(struct p_compressed_bm *p, enum drbd_bitmap_code code)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001053{
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01001054 BUG_ON(code & ~0xf);
1055 p->encoding = (p->encoding & ~0xf) | code;
1056}
Philipp Reisnerb411b362009-09-25 16:07:19 -07001057
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01001058static void dcbp_set_start(struct p_compressed_bm *p, int set)
1059{
1060 p->encoding = (p->encoding & ~0x80) | (set ? 0x80 : 0);
1061}
Philipp Reisnerb411b362009-09-25 16:07:19 -07001062
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01001063static void dcbp_set_pad_bits(struct p_compressed_bm *p, int n)
1064{
1065 BUG_ON(n & ~0x7);
1066 p->encoding = (p->encoding & (~0x7 << 4)) | (n << 4);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001067}
1068
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001069static int fill_bitmap_rle_bits(struct drbd_device *device,
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02001070 struct p_compressed_bm *p,
1071 unsigned int size,
1072 struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001073{
1074 struct bitstream bs;
1075 unsigned long plain_bits;
1076 unsigned long tmp;
1077 unsigned long rl;
1078 unsigned len;
1079 unsigned toggle;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001080 int bits, use_rle;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001081
1082 /* may we use this feature? */
Philipp Reisner44ed1672011-04-19 17:10:19 +02001083 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001084 use_rle = rcu_dereference(device->tconn->net_conf)->use_rle;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001085 rcu_read_unlock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001086 if (!use_rle || device->tconn->agreed_pro_version < 90)
Philipp Reisner44ed1672011-04-19 17:10:19 +02001087 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001088
1089 if (c->bit_offset >= c->bm_bits)
1090 return 0; /* nothing to do. */
1091
1092 /* use at most thus many bytes */
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02001093 bitstream_init(&bs, p->code, size, 0);
1094 memset(p->code, 0, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001095 /* plain bits covered in this code string */
1096 plain_bits = 0;
1097
1098 /* p->encoding & 0x80 stores whether the first run length is set.
1099 * bit offset is implicit.
1100 * start with toggle == 2 to be able to tell the first iteration */
1101 toggle = 2;
1102
1103 /* see how much plain bits we can stuff into one packet
1104 * using RLE and VLI. */
1105 do {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001106 tmp = (toggle == 0) ? _drbd_bm_find_next_zero(device, c->bit_offset)
1107 : _drbd_bm_find_next(device, c->bit_offset);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001108 if (tmp == -1UL)
1109 tmp = c->bm_bits;
1110 rl = tmp - c->bit_offset;
1111
1112 if (toggle == 2) { /* first iteration */
1113 if (rl == 0) {
1114 /* the first checked bit was set,
1115 * store start value, */
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01001116 dcbp_set_start(p, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001117 /* but skip encoding of zero run length */
1118 toggle = !toggle;
1119 continue;
1120 }
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01001121 dcbp_set_start(p, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001122 }
1123
1124 /* paranoia: catch zero runlength.
1125 * can only happen if bitmap is modified while we scan it. */
1126 if (rl == 0) {
1127 dev_err(DEV, "unexpected zero runlength while encoding bitmap "
1128 "t:%u bo:%lu\n", toggle, c->bit_offset);
1129 return -1;
1130 }
1131
1132 bits = vli_encode_bits(&bs, rl);
1133 if (bits == -ENOBUFS) /* buffer full */
1134 break;
1135 if (bits <= 0) {
1136 dev_err(DEV, "error while encoding bitmap: %d\n", bits);
1137 return 0;
1138 }
1139
1140 toggle = !toggle;
1141 plain_bits += rl;
1142 c->bit_offset = tmp;
1143 } while (c->bit_offset < c->bm_bits);
1144
1145 len = bs.cur.b - p->code + !!bs.cur.bit;
1146
1147 if (plain_bits < (len << 3)) {
1148 /* incompressible with this method.
1149 * we need to rewind both word and bit position. */
1150 c->bit_offset -= plain_bits;
1151 bm_xfer_ctx_bit_to_word_offset(c);
1152 c->bit_offset = c->word_offset * BITS_PER_LONG;
1153 return 0;
1154 }
1155
1156 /* RLE + VLI was able to compress it just fine.
1157 * update c->word_offset. */
1158 bm_xfer_ctx_bit_to_word_offset(c);
1159
1160 /* store pad_bits */
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01001161 dcbp_set_pad_bits(p, (8 - bs.cur.bit) & 0x7);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001162
1163 return len;
1164}
1165
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001166/**
1167 * send_bitmap_rle_or_plain
1168 *
1169 * Return 0 when done, 1 when another iteration is needed, and a negative error
1170 * code upon failure.
1171 */
1172static int
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001173send_bitmap_rle_or_plain(struct drbd_device *device, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001174{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001175 struct drbd_socket *sock = &device->tconn->data;
1176 unsigned int header_size = drbd_header_size(device->tconn);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001177 struct p_compressed_bm *p = sock->sbuf + header_size;
Andreas Gruenbachera982dd52010-12-10 00:45:25 +01001178 int len, err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001179
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001180 len = fill_bitmap_rle_bits(device, p,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001181 DRBD_SOCKET_BUFFER_SIZE - header_size - sizeof(*p), c);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001182 if (len < 0)
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001183 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001184
1185 if (len) {
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01001186 dcbp_set_code(p, RLE_VLI_Bits);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001187 err = __send_command(device->tconn, device->vnr, sock,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001188 P_COMPRESSED_BITMAP, sizeof(*p) + len,
1189 NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001190 c->packets[0]++;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001191 c->bytes[0] += header_size + sizeof(*p) + len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001192
1193 if (c->bit_offset >= c->bm_bits)
1194 len = 0; /* DONE */
1195 } else {
1196 /* was not compressible.
1197 * send a buffer full of plain text bits instead. */
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02001198 unsigned int data_size;
1199 unsigned long num_words;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001200 unsigned long *p = sock->sbuf + header_size;
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02001201
1202 data_size = DRBD_SOCKET_BUFFER_SIZE - header_size;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001203 num_words = min_t(size_t, data_size / sizeof(*p),
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02001204 c->bm_words - c->word_offset);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001205 len = num_words * sizeof(*p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001206 if (len)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001207 drbd_bm_get_lel(device, c->word_offset, num_words, p);
1208 err = __send_command(device->tconn, device->vnr, sock, P_BITMAP, len, NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001209 c->word_offset += num_words;
1210 c->bit_offset = c->word_offset * BITS_PER_LONG;
1211
1212 c->packets[1]++;
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02001213 c->bytes[1] += header_size + len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001214
1215 if (c->bit_offset > c->bm_bits)
1216 c->bit_offset = c->bm_bits;
1217 }
Andreas Gruenbachera982dd52010-12-10 00:45:25 +01001218 if (!err) {
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001219 if (len == 0) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001220 INFO_bm_xfer_stats(device, "send", c);
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001221 return 0;
1222 } else
1223 return 1;
1224 }
1225 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001226}
1227
1228/* See the comment at receive_bitmap() */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001229static int _drbd_send_bitmap(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001230{
1231 struct bm_xfer_ctx c;
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001232 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001233
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001234 if (!expect(device->bitmap))
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001235 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001236
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001237 if (get_ldev(device)) {
1238 if (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001239 dev_info(DEV, "Writing the whole bitmap, MDF_FullSync was set.\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001240 drbd_bm_set_all(device);
1241 if (drbd_bm_write(device)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001242 /* write_bm did fail! Leave full sync flag set in Meta P_DATA
1243 * but otherwise process as per normal - need to tell other
1244 * side that a full resync is required! */
1245 dev_err(DEV, "Failed to write bitmap to disk!\n");
1246 } else {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001247 drbd_md_clear_flag(device, MDF_FULL_SYNC);
1248 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001249 }
1250 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001251 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001252 }
1253
1254 c = (struct bm_xfer_ctx) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001255 .bm_bits = drbd_bm_bits(device),
1256 .bm_words = drbd_bm_words(device),
Philipp Reisnerb411b362009-09-25 16:07:19 -07001257 };
1258
1259 do {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001260 err = send_bitmap_rle_or_plain(device, &c);
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001261 } while (err > 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001262
Andreas Gruenbacherf70af112010-12-11 18:51:50 +01001263 return err == 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001264}
1265
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001266int drbd_send_bitmap(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001267{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001268 struct drbd_socket *sock = &device->tconn->data;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001269 int err = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001270
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001271 mutex_lock(&sock->mutex);
1272 if (sock->socket)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001273 err = !_drbd_send_bitmap(device);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001274 mutex_unlock(&sock->mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001275 return err;
1276}
1277
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001278void drbd_send_b_ack(struct drbd_tconn *tconn, u32 barrier_nr, u32 set_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001279{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001280 struct drbd_socket *sock;
1281 struct p_barrier_ack *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001282
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001283 if (tconn->cstate < C_WF_REPORT_PARAMS)
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001284 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001285
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001286 sock = &tconn->meta;
1287 p = conn_prepare_command(tconn, sock);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001288 if (!p)
1289 return;
1290 p->barrier = barrier_nr;
1291 p->set_size = cpu_to_be32(set_size);
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001292 conn_send_command(tconn, sock, P_BARRIER_ACK, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001293}
1294
1295/**
1296 * _drbd_send_ack() - Sends an ack packet
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001297 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001298 * @cmd: Packet command code.
1299 * @sector: sector, needs to be in big endian byte order
1300 * @blksize: size in byte, needs to be in big endian byte order
1301 * @block_id: Id, big endian byte order
1302 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001303static int _drbd_send_ack(struct drbd_device *device, enum drbd_packet cmd,
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001304 u64 sector, u32 blksize, u64 block_id)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001305{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001306 struct drbd_socket *sock;
1307 struct p_block_ack *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001308
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001309 if (device->state.conn < C_CONNECTED)
Andreas Gruenbachera8c32aa2011-03-16 01:27:22 +01001310 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001311
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001312 sock = &device->tconn->meta;
1313 p = drbd_prepare_command(device, sock);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001314 if (!p)
1315 return -EIO;
1316 p->sector = sector;
1317 p->block_id = block_id;
1318 p->blksize = blksize;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001319 p->seq_num = cpu_to_be32(atomic_inc_return(&device->packet_seq));
1320 return drbd_send_command(device, sock, cmd, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001321}
1322
Lars Ellenberg2b2bf212010-10-06 11:46:55 +02001323/* dp->sector and dp->block_id already/still in network byte order,
1324 * data_size is payload size according to dp->head,
1325 * and may need to be corrected for digest size. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001326void drbd_send_ack_dp(struct drbd_device *device, enum drbd_packet cmd,
Andreas Gruenbachera9a99942011-03-16 01:30:14 +01001327 struct p_data *dp, int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001328{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001329 if (device->tconn->peer_integrity_tfm)
1330 data_size -= crypto_hash_digestsize(device->tconn->peer_integrity_tfm);
1331 _drbd_send_ack(device, cmd, dp->sector, cpu_to_be32(data_size),
Andreas Gruenbachera9a99942011-03-16 01:30:14 +01001332 dp->block_id);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001333}
1334
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001335void drbd_send_ack_rp(struct drbd_device *device, enum drbd_packet cmd,
Andreas Gruenbachera9a99942011-03-16 01:30:14 +01001336 struct p_block_req *rp)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001337{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001338 _drbd_send_ack(device, cmd, rp->sector, rp->blksize, rp->block_id);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001339}
1340
1341/**
1342 * drbd_send_ack() - Sends an ack packet
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001343 * @device: DRBD device
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001344 * @cmd: packet command code
1345 * @peer_req: peer request
Philipp Reisnerb411b362009-09-25 16:07:19 -07001346 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001347int drbd_send_ack(struct drbd_device *device, enum drbd_packet cmd,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001348 struct drbd_peer_request *peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001349{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001350 return _drbd_send_ack(device, cmd,
Andreas Gruenbacherdd516122011-03-16 15:39:08 +01001351 cpu_to_be64(peer_req->i.sector),
1352 cpu_to_be32(peer_req->i.size),
1353 peer_req->block_id);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001354}
1355
1356/* This function misuses the block_id field to signal if the blocks
1357 * are is sync or not. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001358int drbd_send_ack_ex(struct drbd_device *device, enum drbd_packet cmd,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001359 sector_t sector, int blksize, u64 block_id)
1360{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001361 return _drbd_send_ack(device, cmd,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001362 cpu_to_be64(sector),
1363 cpu_to_be32(blksize),
1364 cpu_to_be64(block_id));
1365}
1366
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001367int drbd_send_drequest(struct drbd_device *device, int cmd,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001368 sector_t sector, int size, u64 block_id)
1369{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001370 struct drbd_socket *sock;
1371 struct p_block_req *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001372
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001373 sock = &device->tconn->data;
1374 p = drbd_prepare_command(device, sock);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001375 if (!p)
1376 return -EIO;
1377 p->sector = cpu_to_be64(sector);
1378 p->block_id = block_id;
1379 p->blksize = cpu_to_be32(size);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001380 return drbd_send_command(device, sock, cmd, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001381}
1382
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001383int drbd_send_drequest_csum(struct drbd_device *device, sector_t sector, int size,
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001384 void *digest, int digest_size, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001385{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001386 struct drbd_socket *sock;
1387 struct p_block_req *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001388
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001389 /* FIXME: Put the digest into the preallocated socket buffer. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001390
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001391 sock = &device->tconn->data;
1392 p = drbd_prepare_command(device, sock);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001393 if (!p)
1394 return -EIO;
1395 p->sector = cpu_to_be64(sector);
1396 p->block_id = ID_SYNCER /* unused */;
1397 p->blksize = cpu_to_be32(size);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001398 return drbd_send_command(device, sock, cmd, sizeof(*p),
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001399 digest, digest_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001400}
1401
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001402int drbd_send_ov_request(struct drbd_device *device, sector_t sector, int size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001403{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001404 struct drbd_socket *sock;
1405 struct p_block_req *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001406
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001407 sock = &device->tconn->data;
1408 p = drbd_prepare_command(device, sock);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001409 if (!p)
1410 return -EIO;
1411 p->sector = cpu_to_be64(sector);
1412 p->block_id = ID_SYNCER /* unused */;
1413 p->blksize = cpu_to_be32(size);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001414 return drbd_send_command(device, sock, P_OV_REQUEST, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001415}
1416
1417/* called on sndtimeo
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001418 * returns false if we should retry,
1419 * true if we think connection is dead
Philipp Reisnerb411b362009-09-25 16:07:19 -07001420 */
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001421static int we_should_drop_the_connection(struct drbd_tconn *tconn, struct socket *sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001422{
1423 int drop_it;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001424 /* long elapsed = (long)(jiffies - device->last_received); */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001425
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001426 drop_it = tconn->meta.socket == sock
1427 || !tconn->asender.task
1428 || get_t_state(&tconn->asender) != RUNNING
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001429 || tconn->cstate < C_WF_REPORT_PARAMS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001430
1431 if (drop_it)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001432 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001433
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001434 drop_it = !--tconn->ko_count;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001435 if (!drop_it) {
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001436 conn_err(tconn, "[%s/%d] sock_sendmsg time expired, ko = %u\n",
1437 current->comm, current->pid, tconn->ko_count);
1438 request_ping(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001439 }
1440
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001441 return drop_it; /* && (device->state == R_PRIMARY) */;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001442}
1443
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001444static void drbd_update_congested(struct drbd_tconn *tconn)
Andreas Gruenbacher9e204cd2011-01-26 18:45:11 +01001445{
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001446 struct sock *sk = tconn->data.socket->sk;
Andreas Gruenbacher9e204cd2011-01-26 18:45:11 +01001447 if (sk->sk_wmem_queued > sk->sk_sndbuf * 4 / 5)
Philipp Reisner1a7ba642011-02-07 14:56:02 +01001448 set_bit(NET_CONGESTED, &tconn->flags);
Andreas Gruenbacher9e204cd2011-01-26 18:45:11 +01001449}
1450
Philipp Reisnerb411b362009-09-25 16:07:19 -07001451/* The idea of sendpage seems to be to put some kind of reference
1452 * to the page into the skb, and to hand it over to the NIC. In
1453 * this process get_page() gets called.
1454 *
1455 * As soon as the page was really sent over the network put_page()
1456 * gets called by some part of the network layer. [ NIC driver? ]
1457 *
1458 * [ get_page() / put_page() increment/decrement the count. If count
1459 * reaches 0 the page will be freed. ]
1460 *
1461 * This works nicely with pages from FSs.
1462 * But this means that in protocol A we might signal IO completion too early!
1463 *
1464 * In order not to corrupt data during a resync we must make sure
1465 * that we do not reuse our own buffer pages (EEs) to early, therefore
1466 * we have the net_ee list.
1467 *
1468 * XFS seems to have problems, still, it submits pages with page_count == 0!
1469 * As a workaround, we disable sendpage on pages
1470 * with page_count == 0 or PageSlab.
1471 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001472static int _drbd_no_send_page(struct drbd_device *device, struct page *page,
Andreas Gruenbacherb9874272011-03-16 09:41:10 +01001473 int offset, size_t size, unsigned msg_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001474{
Andreas Gruenbacherb9874272011-03-16 09:41:10 +01001475 struct socket *socket;
1476 void *addr;
1477 int err;
1478
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001479 socket = device->tconn->data.socket;
Andreas Gruenbacherb9874272011-03-16 09:41:10 +01001480 addr = kmap(page) + offset;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001481 err = drbd_send_all(device->tconn, socket, addr, size, msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001482 kunmap(page);
Andreas Gruenbacherb9874272011-03-16 09:41:10 +01001483 if (!err)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001484 device->send_cnt += size >> 9;
Andreas Gruenbacherb9874272011-03-16 09:41:10 +01001485 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001486}
1487
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001488static int _drbd_send_page(struct drbd_device *device, struct page *page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001489 int offset, size_t size, unsigned msg_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001490{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001491 struct socket *socket = device->tconn->data.socket;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001492 mm_segment_t oldfs = get_fs();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001493 int len = size;
Andreas Gruenbacher88b390f2011-03-16 10:44:16 +01001494 int err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001495
1496 /* e.g. XFS meta- & log-data is in slab pages, which have a
1497 * page_count of 0 and/or have PageSlab() set.
1498 * we cannot use send_page for those, as that does get_page();
1499 * put_page(); and would cause either a VM_BUG directly, or
1500 * __page_cache_release a page that would actually still be referenced
1501 * by someone, leading to some obscure delayed Oops somewhere else. */
1502 if (disable_sendpage || (page_count(page) < 1) || PageSlab(page))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001503 return _drbd_no_send_page(device, page, offset, size, msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001504
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001505 msg_flags |= MSG_NOSIGNAL;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001506 drbd_update_congested(device->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001507 set_fs(KERNEL_DS);
1508 do {
Andreas Gruenbacher88b390f2011-03-16 10:44:16 +01001509 int sent;
1510
1511 sent = socket->ops->sendpage(socket, page, offset, len, msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001512 if (sent <= 0) {
Andreas Gruenbacher88b390f2011-03-16 10:44:16 +01001513 if (sent == -EAGAIN) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001514 if (we_should_drop_the_connection(device->tconn, socket))
Andreas Gruenbacher88b390f2011-03-16 10:44:16 +01001515 break;
1516 continue;
1517 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001518 dev_warn(DEV, "%s: size=%d len=%d sent=%d\n",
1519 __func__, (int)size, len, sent);
Andreas Gruenbacher88b390f2011-03-16 10:44:16 +01001520 if (sent < 0)
1521 err = sent;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001522 break;
1523 }
1524 len -= sent;
1525 offset += sent;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001526 } while (len > 0 /* THINK && device->cstate >= C_CONNECTED*/);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001527 set_fs(oldfs);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001528 clear_bit(NET_CONGESTED, &device->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001529
Andreas Gruenbacher88b390f2011-03-16 10:44:16 +01001530 if (len == 0) {
1531 err = 0;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001532 device->send_cnt += size >> 9;
Andreas Gruenbacher88b390f2011-03-16 10:44:16 +01001533 }
1534 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001535}
1536
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001537static int _drbd_send_bio(struct drbd_device *device, struct bio *bio)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001538{
Kent Overstreet79886132013-11-23 17:19:00 -08001539 struct bio_vec bvec;
1540 struct bvec_iter iter;
1541
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001542 /* hint all but last page with MSG_MORE */
Kent Overstreet79886132013-11-23 17:19:00 -08001543 bio_for_each_segment(bvec, bio, iter) {
Andreas Gruenbacher7fae55d2011-03-16 11:46:33 +01001544 int err;
1545
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001546 err = _drbd_no_send_page(device, bvec.bv_page,
Kent Overstreet79886132013-11-23 17:19:00 -08001547 bvec.bv_offset, bvec.bv_len,
Kent Overstreet4550dd62013-08-07 14:26:21 -07001548 bio_iter_last(bvec, iter)
Kent Overstreet79886132013-11-23 17:19:00 -08001549 ? 0 : MSG_MORE);
Andreas Gruenbacher7fae55d2011-03-16 11:46:33 +01001550 if (err)
1551 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001552 }
Andreas Gruenbacher7fae55d2011-03-16 11:46:33 +01001553 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001554}
1555
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001556static int _drbd_send_zc_bio(struct drbd_device *device, struct bio *bio)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001557{
Kent Overstreet79886132013-11-23 17:19:00 -08001558 struct bio_vec bvec;
1559 struct bvec_iter iter;
1560
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001561 /* hint all but last page with MSG_MORE */
Kent Overstreet79886132013-11-23 17:19:00 -08001562 bio_for_each_segment(bvec, bio, iter) {
Andreas Gruenbacher7fae55d2011-03-16 11:46:33 +01001563 int err;
1564
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001565 err = _drbd_send_page(device, bvec.bv_page,
Kent Overstreet79886132013-11-23 17:19:00 -08001566 bvec.bv_offset, bvec.bv_len,
Kent Overstreet4550dd62013-08-07 14:26:21 -07001567 bio_iter_last(bvec, iter) ? 0 : MSG_MORE);
Andreas Gruenbacher7fae55d2011-03-16 11:46:33 +01001568 if (err)
1569 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001570 }
Andreas Gruenbacher7fae55d2011-03-16 11:46:33 +01001571 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001572}
1573
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001574static int _drbd_send_zc_ee(struct drbd_device *device,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001575 struct drbd_peer_request *peer_req)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001576{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001577 struct page *page = peer_req->pages;
1578 unsigned len = peer_req->i.size;
Andreas Gruenbacher9f692302011-03-16 10:49:09 +01001579 int err;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001580
Lars Ellenbergba11ad92010-05-25 16:26:16 +02001581 /* hint all but last page with MSG_MORE */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001582 page_chain_for_each(page) {
1583 unsigned l = min_t(unsigned, len, PAGE_SIZE);
Andreas Gruenbacher9f692302011-03-16 10:49:09 +01001584
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001585 err = _drbd_send_page(device, page, 0, l,
Andreas Gruenbacher9f692302011-03-16 10:49:09 +01001586 page_chain_next(page) ? MSG_MORE : 0);
1587 if (err)
1588 return err;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001589 len -= l;
1590 }
Andreas Gruenbacher9f692302011-03-16 10:49:09 +01001591 return 0;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001592}
1593
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001594static u32 bio_flags_to_wire(struct drbd_device *device, unsigned long bi_rw)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001595{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001596 if (device->tconn->agreed_pro_version >= 95)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001597 return (bi_rw & REQ_SYNC ? DP_RW_SYNC : 0) |
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001598 (bi_rw & REQ_FUA ? DP_FUA : 0) |
1599 (bi_rw & REQ_FLUSH ? DP_FLUSH : 0) |
1600 (bi_rw & REQ_DISCARD ? DP_DISCARD : 0);
1601 else
Jens Axboe721a9602011-03-09 11:56:30 +01001602 return bi_rw & REQ_SYNC ? DP_RW_SYNC : 0;
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001603}
1604
Philipp Reisnerb411b362009-09-25 16:07:19 -07001605/* Used to send write requests
1606 * R_PRIMARY -> Peer (P_DATA)
1607 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001608int drbd_send_dblock(struct drbd_device *device, struct drbd_request *req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001609{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001610 struct drbd_socket *sock;
1611 struct p_data *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001612 unsigned int dp_flags = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001613 int dgs;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001614 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001615
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001616 sock = &device->tconn->data;
1617 p = drbd_prepare_command(device, sock);
1618 dgs = device->tconn->integrity_tfm ? crypto_hash_digestsize(device->tconn->integrity_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001619
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001620 if (!p)
1621 return -EIO;
1622 p->sector = cpu_to_be64(req->i.sector);
1623 p->block_id = (unsigned long)req;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001624 p->seq_num = cpu_to_be32(atomic_inc_return(&device->packet_seq));
1625 dp_flags = bio_flags_to_wire(device, req->master_bio->bi_rw);
1626 if (device->state.conn >= C_SYNC_SOURCE &&
1627 device->state.conn <= C_PAUSED_SYNC_T)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001628 dp_flags |= DP_MAY_SET_IN_SYNC;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001629 if (device->tconn->agreed_pro_version >= 100) {
Philipp Reisner303d1442011-04-13 16:24:47 -07001630 if (req->rq_state & RQ_EXP_RECEIVE_ACK)
1631 dp_flags |= DP_SEND_RECEIVE_ACK;
1632 if (req->rq_state & RQ_EXP_WRITE_ACK)
1633 dp_flags |= DP_SEND_WRITE_ACK;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001634 }
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001635 p->dp_flags = cpu_to_be32(dp_flags);
1636 if (dgs)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001637 drbd_csum_bio(device, device->tconn->integrity_tfm, req->master_bio, p + 1);
1638 err = __send_command(device->tconn, device->vnr, sock, P_DATA, sizeof(*p) + dgs, NULL, req->i.size);
Andreas Gruenbacher6bdb9b02011-03-16 11:52:58 +01001639 if (!err) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001640 /* For protocol A, we have to memcpy the payload into
1641 * socket buffers, as we may complete right away
1642 * as soon as we handed it over to tcp, at which point the data
1643 * pages may become invalid.
1644 *
1645 * For data-integrity enabled, we copy it as well, so we can be
1646 * sure that even if the bio pages may still be modified, it
1647 * won't change the data on the wire, thus if the digest checks
1648 * out ok after sending on this side, but does not fit on the
1649 * receiving side, we sure have detected corruption elsewhere.
1650 */
Philipp Reisner303d1442011-04-13 16:24:47 -07001651 if (!(req->rq_state & (RQ_EXP_RECEIVE_ACK | RQ_EXP_WRITE_ACK)) || dgs)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001652 err = _drbd_send_bio(device, req->master_bio);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001653 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001654 err = _drbd_send_zc_bio(device, req->master_bio);
Lars Ellenberg470be442010-11-10 10:36:52 +01001655
1656 /* double check digest, sometimes buffers have been modified in flight. */
1657 if (dgs > 0 && dgs <= 64) {
Bart Van Assche24c48302011-05-21 18:32:29 +02001658 /* 64 byte, 512 bit, is the largest digest size
Lars Ellenberg470be442010-11-10 10:36:52 +01001659 * currently supported in kernel crypto. */
1660 unsigned char digest[64];
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001661 drbd_csum_bio(device, device->tconn->integrity_tfm, req->master_bio, digest);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001662 if (memcmp(p + 1, digest, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001663 dev_warn(DEV,
1664 "Digest mismatch, buffer modified by upper layers during write: %llus +%u\n",
Andreas Gruenbacherace652a2011-01-03 17:09:58 +01001665 (unsigned long long)req->i.sector, req->i.size);
Lars Ellenberg470be442010-11-10 10:36:52 +01001666 }
1667 } /* else if (dgs > 64) {
1668 ... Be noisy about digest too large ...
1669 } */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001670 }
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001671 mutex_unlock(&sock->mutex); /* locked by drbd_prepare_command() */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001672
Andreas Gruenbacher6bdb9b02011-03-16 11:52:58 +01001673 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001674}
1675
1676/* answer packet, used to send data back for read requests:
1677 * Peer -> (diskless) R_PRIMARY (P_DATA_REPLY)
1678 * C_SYNC_SOURCE -> C_SYNC_TARGET (P_RS_DATA_REPLY)
1679 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001680int drbd_send_block(struct drbd_device *device, enum drbd_packet cmd,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001681 struct drbd_peer_request *peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001682{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001683 struct drbd_socket *sock;
1684 struct p_data *p;
Andreas Gruenbacher7b57b89d2011-03-16 11:35:20 +01001685 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001686 int dgs;
1687
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001688 sock = &device->tconn->data;
1689 p = drbd_prepare_command(device, sock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001690
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001691 dgs = device->tconn->integrity_tfm ? crypto_hash_digestsize(device->tconn->integrity_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001692
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001693 if (!p)
1694 return -EIO;
1695 p->sector = cpu_to_be64(peer_req->i.sector);
1696 p->block_id = peer_req->block_id;
1697 p->seq_num = 0; /* unused */
Lars Ellenbergb17f33c2012-02-08 15:32:51 +01001698 p->dp_flags = 0;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001699 if (dgs)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001700 drbd_csum_ee(device, device->tconn->integrity_tfm, peer_req, p + 1);
1701 err = __send_command(device->tconn, device->vnr, sock, cmd, sizeof(*p) + dgs, NULL, peer_req->i.size);
Andreas Gruenbacher7b57b89d2011-03-16 11:35:20 +01001702 if (!err)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001703 err = _drbd_send_zc_ee(device, peer_req);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001704 mutex_unlock(&sock->mutex); /* locked by drbd_prepare_command() */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001705
Andreas Gruenbacher7b57b89d2011-03-16 11:35:20 +01001706 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001707}
1708
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001709int drbd_send_out_of_sync(struct drbd_device *device, struct drbd_request *req)
Philipp Reisner73a01a12010-10-27 14:33:00 +02001710{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001711 struct drbd_socket *sock;
1712 struct p_block_desc *p;
Philipp Reisner73a01a12010-10-27 14:33:00 +02001713
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001714 sock = &device->tconn->data;
1715 p = drbd_prepare_command(device, sock);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001716 if (!p)
1717 return -EIO;
1718 p->sector = cpu_to_be64(req->i.sector);
1719 p->blksize = cpu_to_be32(req->i.size);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001720 return drbd_send_command(device, sock, P_OUT_OF_SYNC, sizeof(*p), NULL, 0);
Philipp Reisner73a01a12010-10-27 14:33:00 +02001721}
1722
Philipp Reisnerb411b362009-09-25 16:07:19 -07001723/*
1724 drbd_send distinguishes two cases:
1725
1726 Packets sent via the data socket "sock"
1727 and packets sent via the meta data socket "msock"
1728
1729 sock msock
1730 -----------------+-------------------------+------------------------------
1731 timeout conf.timeout / 2 conf.timeout / 2
1732 timeout action send a ping via msock Abort communication
1733 and close all sockets
1734*/
1735
1736/*
1737 * you must have down()ed the appropriate [m]sock_mutex elsewhere!
1738 */
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001739int drbd_send(struct drbd_tconn *tconn, struct socket *sock,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001740 void *buf, size_t size, unsigned msg_flags)
1741{
1742 struct kvec iov;
1743 struct msghdr msg;
1744 int rv, sent = 0;
1745
1746 if (!sock)
Andreas Gruenbacherc0d42c82010-12-09 23:52:22 +01001747 return -EBADR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001748
1749 /* THINK if (signal_pending) return ... ? */
1750
1751 iov.iov_base = buf;
1752 iov.iov_len = size;
1753
1754 msg.msg_name = NULL;
1755 msg.msg_namelen = 0;
1756 msg.msg_control = NULL;
1757 msg.msg_controllen = 0;
1758 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
1759
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001760 if (sock == tconn->data.socket) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02001761 rcu_read_lock();
1762 tconn->ko_count = rcu_dereference(tconn->net_conf)->ko_count;
1763 rcu_read_unlock();
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001764 drbd_update_congested(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001765 }
1766 do {
1767 /* STRANGE
1768 * tcp_sendmsg does _not_ use its size parameter at all ?
1769 *
1770 * -EAGAIN on timeout, -EINTR on signal.
1771 */
1772/* THINK
1773 * do we need to block DRBD_SIG if sock == &meta.socket ??
1774 * otherwise wake_asender() might interrupt some send_*Ack !
1775 */
1776 rv = kernel_sendmsg(sock, &msg, &iov, 1, size);
1777 if (rv == -EAGAIN) {
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001778 if (we_should_drop_the_connection(tconn, sock))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001779 break;
1780 else
1781 continue;
1782 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001783 if (rv == -EINTR) {
1784 flush_signals(current);
1785 rv = 0;
1786 }
1787 if (rv < 0)
1788 break;
1789 sent += rv;
1790 iov.iov_base += rv;
1791 iov.iov_len -= rv;
1792 } while (sent < size);
1793
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001794 if (sock == tconn->data.socket)
1795 clear_bit(NET_CONGESTED, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001796
1797 if (rv <= 0) {
1798 if (rv != -EAGAIN) {
Philipp Reisnerbedbd2a2011-02-07 15:08:48 +01001799 conn_err(tconn, "%s_sendmsg returned %d\n",
1800 sock == tconn->meta.socket ? "msock" : "sock",
1801 rv);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001802 conn_request_state(tconn, NS(conn, C_BROKEN_PIPE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001803 } else
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001804 conn_request_state(tconn, NS(conn, C_TIMEOUT), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001805 }
1806
1807 return sent;
1808}
1809
Andreas Gruenbacherfb708e42010-12-15 17:04:36 +01001810/**
1811 * drbd_send_all - Send an entire buffer
1812 *
1813 * Returns 0 upon success and a negative error value otherwise.
1814 */
1815int drbd_send_all(struct drbd_tconn *tconn, struct socket *sock, void *buffer,
1816 size_t size, unsigned msg_flags)
1817{
1818 int err;
1819
1820 err = drbd_send(tconn, sock, buffer, size, msg_flags);
1821 if (err < 0)
1822 return err;
1823 if (err != size)
1824 return -EIO;
1825 return 0;
1826}
1827
Philipp Reisnerb411b362009-09-25 16:07:19 -07001828static int drbd_open(struct block_device *bdev, fmode_t mode)
1829{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001830 struct drbd_device *device = bdev->bd_disk->private_data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001831 unsigned long flags;
1832 int rv = 0;
1833
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001834 mutex_lock(&drbd_main_mutex);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001835 spin_lock_irqsave(&device->tconn->req_lock, flags);
1836 /* to have a stable device->state.role
Philipp Reisnerb411b362009-09-25 16:07:19 -07001837 * and no race with updating open_cnt */
1838
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001839 if (device->state.role != R_PRIMARY) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001840 if (mode & FMODE_WRITE)
1841 rv = -EROFS;
1842 else if (!allow_oos)
1843 rv = -EMEDIUMTYPE;
1844 }
1845
1846 if (!rv)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001847 device->open_cnt++;
1848 spin_unlock_irqrestore(&device->tconn->req_lock, flags);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001849 mutex_unlock(&drbd_main_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001850
1851 return rv;
1852}
1853
Al Virodb2a1442013-05-05 21:52:57 -04001854static void drbd_release(struct gendisk *gd, fmode_t mode)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001855{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001856 struct drbd_device *device = gd->private_data;
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001857 mutex_lock(&drbd_main_mutex);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001858 device->open_cnt--;
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001859 mutex_unlock(&drbd_main_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001860}
1861
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001862static void drbd_set_defaults(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001863{
Lars Ellenbergf3990022011-03-23 14:31:09 +01001864 /* Beware! The actual layout differs
1865 * between big endian and little endian */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001866 device->state = (union drbd_dev_state) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001867 { .role = R_SECONDARY,
1868 .peer = R_UNKNOWN,
1869 .conn = C_STANDALONE,
1870 .disk = D_DISKLESS,
1871 .pdsk = D_UNKNOWN,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001872 } };
1873}
1874
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001875void drbd_init_set_defaults(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001876{
1877 /* the memset(,0,) did most of this.
1878 * note: only assignments, no allocation in here */
1879
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001880 drbd_set_defaults(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001881
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001882 atomic_set(&device->ap_bio_cnt, 0);
1883 atomic_set(&device->ap_pending_cnt, 0);
1884 atomic_set(&device->rs_pending_cnt, 0);
1885 atomic_set(&device->unacked_cnt, 0);
1886 atomic_set(&device->local_cnt, 0);
1887 atomic_set(&device->pp_in_use_by_net, 0);
1888 atomic_set(&device->rs_sect_in, 0);
1889 atomic_set(&device->rs_sect_ev, 0);
1890 atomic_set(&device->ap_in_flight, 0);
1891 atomic_set(&device->md_io_in_use, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001892
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001893 mutex_init(&device->own_state_mutex);
1894 device->state_mutex = &device->own_state_mutex;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001895
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001896 spin_lock_init(&device->al_lock);
1897 spin_lock_init(&device->peer_seq_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001898
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001899 INIT_LIST_HEAD(&device->active_ee);
1900 INIT_LIST_HEAD(&device->sync_ee);
1901 INIT_LIST_HEAD(&device->done_ee);
1902 INIT_LIST_HEAD(&device->read_ee);
1903 INIT_LIST_HEAD(&device->net_ee);
1904 INIT_LIST_HEAD(&device->resync_reads);
1905 INIT_LIST_HEAD(&device->resync_work.list);
1906 INIT_LIST_HEAD(&device->unplug_work.list);
1907 INIT_LIST_HEAD(&device->go_diskless.list);
1908 INIT_LIST_HEAD(&device->md_sync_work.list);
1909 INIT_LIST_HEAD(&device->start_resync_work.list);
1910 INIT_LIST_HEAD(&device->bm_io_work.w.list);
Philipp Reisner0ced55a2010-04-30 15:26:20 +02001911
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001912 device->resync_work.cb = w_resync_timer;
1913 device->unplug_work.cb = w_send_write_hint;
1914 device->go_diskless.cb = w_go_diskless;
1915 device->md_sync_work.cb = w_md_sync;
1916 device->bm_io_work.w.cb = w_bitmap_io;
1917 device->start_resync_work.cb = w_start_resync;
Philipp Reisnera21e9292011-02-08 15:08:49 +01001918
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001919 device->resync_work.device = device;
1920 device->unplug_work.device = device;
1921 device->go_diskless.device = device;
1922 device->md_sync_work.device = device;
1923 device->bm_io_work.w.device = device;
1924 device->start_resync_work.device = device;
Philipp Reisnera21e9292011-02-08 15:08:49 +01001925
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001926 init_timer(&device->resync_timer);
1927 init_timer(&device->md_sync_timer);
1928 init_timer(&device->start_resync_timer);
1929 init_timer(&device->request_timer);
1930 device->resync_timer.function = resync_timer_fn;
1931 device->resync_timer.data = (unsigned long) device;
1932 device->md_sync_timer.function = md_sync_timer_fn;
1933 device->md_sync_timer.data = (unsigned long) device;
1934 device->start_resync_timer.function = start_resync_timer_fn;
1935 device->start_resync_timer.data = (unsigned long) device;
1936 device->request_timer.function = request_timer_fn;
1937 device->request_timer.data = (unsigned long) device;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001938
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001939 init_waitqueue_head(&device->misc_wait);
1940 init_waitqueue_head(&device->state_wait);
1941 init_waitqueue_head(&device->ee_wait);
1942 init_waitqueue_head(&device->al_wait);
1943 init_waitqueue_head(&device->seq_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001944
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001945 device->resync_wenr = LC_FREE;
1946 device->peer_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
1947 device->local_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001948}
1949
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001950void drbd_device_cleanup(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001951{
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001952 int i;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001953 if (device->tconn->receiver.t_state != NONE)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001954 dev_err(DEV, "ASSERT FAILED: receiver t_state == %d expected 0.\n",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001955 device->tconn->receiver.t_state);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001956
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001957 device->al_writ_cnt =
1958 device->bm_writ_cnt =
1959 device->read_cnt =
1960 device->recv_cnt =
1961 device->send_cnt =
1962 device->writ_cnt =
1963 device->p_size =
1964 device->rs_start =
1965 device->rs_total =
1966 device->rs_failed = 0;
1967 device->rs_last_events = 0;
1968 device->rs_last_sect_ev = 0;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001969 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001970 device->rs_mark_left[i] = 0;
1971 device->rs_mark_time[i] = 0;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001972 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001973 D_ASSERT(device->tconn->net_conf == NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001974
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001975 drbd_set_my_capacity(device, 0);
1976 if (device->bitmap) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001977 /* maybe never allocated. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001978 drbd_bm_resize(device, 0, 1);
1979 drbd_bm_cleanup(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001980 }
1981
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001982 drbd_free_bc(device->ldev);
1983 device->ldev = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001984
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001985 clear_bit(AL_SUSPENDED, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001986
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001987 D_ASSERT(list_empty(&device->active_ee));
1988 D_ASSERT(list_empty(&device->sync_ee));
1989 D_ASSERT(list_empty(&device->done_ee));
1990 D_ASSERT(list_empty(&device->read_ee));
1991 D_ASSERT(list_empty(&device->net_ee));
1992 D_ASSERT(list_empty(&device->resync_reads));
1993 D_ASSERT(list_empty(&device->tconn->sender_work.q));
1994 D_ASSERT(list_empty(&device->resync_work.list));
1995 D_ASSERT(list_empty(&device->unplug_work.list));
1996 D_ASSERT(list_empty(&device->go_diskless.list));
Lars Ellenberg2265b472010-12-16 15:41:26 +01001997
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001998 drbd_set_defaults(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001999}
2000
2001
2002static void drbd_destroy_mempools(void)
2003{
2004 struct page *page;
2005
2006 while (drbd_pp_pool) {
2007 page = drbd_pp_pool;
2008 drbd_pp_pool = (struct page *)page_private(page);
2009 __free_page(page);
2010 drbd_pp_vacant--;
2011 }
2012
2013 /* D_ASSERT(atomic_read(&drbd_pp_vacant)==0); */
2014
Lars Ellenberg9476f392011-02-23 17:02:01 +01002015 if (drbd_md_io_bio_set)
2016 bioset_free(drbd_md_io_bio_set);
Lars Ellenberg42818082011-02-23 12:39:46 +01002017 if (drbd_md_io_page_pool)
2018 mempool_destroy(drbd_md_io_page_pool);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002019 if (drbd_ee_mempool)
2020 mempool_destroy(drbd_ee_mempool);
2021 if (drbd_request_mempool)
2022 mempool_destroy(drbd_request_mempool);
2023 if (drbd_ee_cache)
2024 kmem_cache_destroy(drbd_ee_cache);
2025 if (drbd_request_cache)
2026 kmem_cache_destroy(drbd_request_cache);
2027 if (drbd_bm_ext_cache)
2028 kmem_cache_destroy(drbd_bm_ext_cache);
2029 if (drbd_al_ext_cache)
2030 kmem_cache_destroy(drbd_al_ext_cache);
2031
Lars Ellenberg9476f392011-02-23 17:02:01 +01002032 drbd_md_io_bio_set = NULL;
Lars Ellenberg42818082011-02-23 12:39:46 +01002033 drbd_md_io_page_pool = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002034 drbd_ee_mempool = NULL;
2035 drbd_request_mempool = NULL;
2036 drbd_ee_cache = NULL;
2037 drbd_request_cache = NULL;
2038 drbd_bm_ext_cache = NULL;
2039 drbd_al_ext_cache = NULL;
2040
2041 return;
2042}
2043
2044static int drbd_create_mempools(void)
2045{
2046 struct page *page;
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01002047 const int number = (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * minor_count;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002048 int i;
2049
2050 /* prepare our caches and mempools */
2051 drbd_request_mempool = NULL;
2052 drbd_ee_cache = NULL;
2053 drbd_request_cache = NULL;
2054 drbd_bm_ext_cache = NULL;
2055 drbd_al_ext_cache = NULL;
2056 drbd_pp_pool = NULL;
Lars Ellenberg42818082011-02-23 12:39:46 +01002057 drbd_md_io_page_pool = NULL;
Lars Ellenberg9476f392011-02-23 17:02:01 +01002058 drbd_md_io_bio_set = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002059
2060 /* caches */
2061 drbd_request_cache = kmem_cache_create(
2062 "drbd_req", sizeof(struct drbd_request), 0, 0, NULL);
2063 if (drbd_request_cache == NULL)
2064 goto Enomem;
2065
2066 drbd_ee_cache = kmem_cache_create(
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01002067 "drbd_ee", sizeof(struct drbd_peer_request), 0, 0, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002068 if (drbd_ee_cache == NULL)
2069 goto Enomem;
2070
2071 drbd_bm_ext_cache = kmem_cache_create(
2072 "drbd_bm", sizeof(struct bm_extent), 0, 0, NULL);
2073 if (drbd_bm_ext_cache == NULL)
2074 goto Enomem;
2075
2076 drbd_al_ext_cache = kmem_cache_create(
2077 "drbd_al", sizeof(struct lc_element), 0, 0, NULL);
2078 if (drbd_al_ext_cache == NULL)
2079 goto Enomem;
2080
2081 /* mempools */
Lars Ellenberg9476f392011-02-23 17:02:01 +01002082 drbd_md_io_bio_set = bioset_create(DRBD_MIN_POOL_PAGES, 0);
2083 if (drbd_md_io_bio_set == NULL)
2084 goto Enomem;
Lars Ellenberg9476f392011-02-23 17:02:01 +01002085
Lars Ellenberg42818082011-02-23 12:39:46 +01002086 drbd_md_io_page_pool = mempool_create_page_pool(DRBD_MIN_POOL_PAGES, 0);
2087 if (drbd_md_io_page_pool == NULL)
2088 goto Enomem;
2089
Philipp Reisnerb411b362009-09-25 16:07:19 -07002090 drbd_request_mempool = mempool_create(number,
2091 mempool_alloc_slab, mempool_free_slab, drbd_request_cache);
2092 if (drbd_request_mempool == NULL)
2093 goto Enomem;
2094
2095 drbd_ee_mempool = mempool_create(number,
2096 mempool_alloc_slab, mempool_free_slab, drbd_ee_cache);
Nicolas Kaiser2027ae12010-10-28 06:15:26 -06002097 if (drbd_ee_mempool == NULL)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002098 goto Enomem;
2099
2100 /* drbd's page pool */
2101 spin_lock_init(&drbd_pp_lock);
2102
2103 for (i = 0; i < number; i++) {
2104 page = alloc_page(GFP_HIGHUSER);
2105 if (!page)
2106 goto Enomem;
2107 set_page_private(page, (unsigned long)drbd_pp_pool);
2108 drbd_pp_pool = page;
2109 }
2110 drbd_pp_vacant = number;
2111
2112 return 0;
2113
2114Enomem:
2115 drbd_destroy_mempools(); /* in case we allocated some */
2116 return -ENOMEM;
2117}
2118
2119static int drbd_notify_sys(struct notifier_block *this, unsigned long code,
2120 void *unused)
2121{
2122 /* just so we have it. you never know what interesting things we
2123 * might want to do here some day...
2124 */
2125
2126 return NOTIFY_DONE;
2127}
2128
2129static struct notifier_block drbd_notifier = {
2130 .notifier_call = drbd_notify_sys,
2131};
2132
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002133static void drbd_release_all_peer_reqs(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002134{
2135 int rr;
2136
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002137 rr = drbd_free_peer_reqs(device, &device->active_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002138 if (rr)
2139 dev_err(DEV, "%d EEs in active list found!\n", rr);
2140
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002141 rr = drbd_free_peer_reqs(device, &device->sync_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002142 if (rr)
2143 dev_err(DEV, "%d EEs in sync list found!\n", rr);
2144
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002145 rr = drbd_free_peer_reqs(device, &device->read_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002146 if (rr)
2147 dev_err(DEV, "%d EEs in read list found!\n", rr);
2148
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002149 rr = drbd_free_peer_reqs(device, &device->done_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002150 if (rr)
2151 dev_err(DEV, "%d EEs in done list found!\n", rr);
2152
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002153 rr = drbd_free_peer_reqs(device, &device->net_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002154 if (rr)
2155 dev_err(DEV, "%d EEs in net list found!\n", rr);
2156}
2157
Philipp Reisner774b3052011-02-22 02:07:03 -05002158/* caution. no locking. */
Philipp Reisner81fa2e62011-05-04 15:10:30 +02002159void drbd_minor_destroy(struct kref *kref)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002160{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002161 struct drbd_device *device = container_of(kref, struct drbd_device, kref);
2162 struct drbd_tconn *tconn = device->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002163
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002164 del_timer_sync(&device->request_timer);
Philipp Reisnerdfa8bed2011-06-29 14:06:08 +02002165
Philipp Reisnerb411b362009-09-25 16:07:19 -07002166 /* paranoia asserts */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002167 D_ASSERT(device->open_cnt == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002168 /* end paranoia asserts */
2169
Philipp Reisnerb411b362009-09-25 16:07:19 -07002170 /* cleanup stuff that may have been allocated during
2171 * device (re-)configuration or state changes */
2172
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002173 if (device->this_bdev)
2174 bdput(device->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002175
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002176 drbd_free_bc(device->ldev);
2177 device->ldev = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002178
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002179 drbd_release_all_peer_reqs(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002180
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002181 lc_destroy(device->act_log);
2182 lc_destroy(device->resync);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002183
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002184 kfree(device->p_uuid);
2185 /* device->p_uuid = NULL; */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002186
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002187 if (device->bitmap) /* should no longer be there. */
2188 drbd_bm_cleanup(device);
2189 __free_page(device->md_io_page);
2190 put_disk(device->vdisk);
2191 blk_cleanup_queue(device->rq_queue);
2192 kfree(device->rs_plan_s);
2193 kfree(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002194
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02002195 kref_put(&tconn->kref, &conn_destroy);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002196}
2197
Lars Ellenberg2312f0b32011-11-24 10:36:25 +01002198/* One global retry thread, if we need to push back some bio and have it
2199 * reinserted through our make request function.
2200 */
2201static struct retry_worker {
2202 struct workqueue_struct *wq;
2203 struct work_struct worker;
2204
2205 spinlock_t lock;
2206 struct list_head writes;
2207} retry;
2208
2209static void do_retry(struct work_struct *ws)
2210{
2211 struct retry_worker *retry = container_of(ws, struct retry_worker, worker);
2212 LIST_HEAD(writes);
2213 struct drbd_request *req, *tmp;
2214
2215 spin_lock_irq(&retry->lock);
2216 list_splice_init(&retry->writes, &writes);
2217 spin_unlock_irq(&retry->lock);
2218
2219 list_for_each_entry_safe(req, tmp, &writes, tl_requests) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002220 struct drbd_device *device = req->w.device;
Lars Ellenberg2312f0b32011-11-24 10:36:25 +01002221 struct bio *bio = req->master_bio;
2222 unsigned long start_time = req->start_time;
Lars Ellenberg9a278a72012-07-24 10:12:36 +02002223 bool expected;
Lars Ellenberg2312f0b32011-11-24 10:36:25 +01002224
Lars Ellenberg9a278a72012-07-24 10:12:36 +02002225 expected =
2226 expect(atomic_read(&req->completion_ref) == 0) &&
2227 expect(req->rq_state & RQ_POSTPONED) &&
2228 expect((req->rq_state & RQ_LOCAL_PENDING) == 0 ||
2229 (req->rq_state & RQ_LOCAL_ABORTED) != 0);
2230
2231 if (!expected)
2232 dev_err(DEV, "req=%p completion_ref=%d rq_state=%x\n",
2233 req, atomic_read(&req->completion_ref),
2234 req->rq_state);
2235
2236 /* We still need to put one kref associated with the
2237 * "completion_ref" going zero in the code path that queued it
2238 * here. The request object may still be referenced by a
2239 * frozen local req->private_bio, in case we force-detached.
Lars Ellenberg2312f0b32011-11-24 10:36:25 +01002240 */
Lars Ellenberg9a278a72012-07-24 10:12:36 +02002241 kref_put(&req->kref, drbd_req_destroy);
Lars Ellenberg2312f0b32011-11-24 10:36:25 +01002242
2243 /* A single suspended or otherwise blocking device may stall
2244 * all others as well. Fortunately, this code path is to
2245 * recover from a situation that "should not happen":
2246 * concurrent writes in multi-primary setup.
2247 * In a "normal" lifecycle, this workqueue is supposed to be
2248 * destroyed without ever doing anything.
2249 * If it turns out to be an issue anyways, we can do per
2250 * resource (replication group) or per device (minor) retry
2251 * workqueues instead.
2252 */
2253
2254 /* We are not just doing generic_make_request(),
2255 * as we want to keep the start_time information. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002256 inc_ap_bio(device);
2257 __drbd_make_request(device, bio, start_time);
Lars Ellenberg2312f0b32011-11-24 10:36:25 +01002258 }
2259}
2260
Lars Ellenberg9d05e7c2012-07-17 10:05:04 +02002261void drbd_restart_request(struct drbd_request *req)
Lars Ellenberg2312f0b32011-11-24 10:36:25 +01002262{
2263 unsigned long flags;
2264 spin_lock_irqsave(&retry.lock, flags);
2265 list_move_tail(&req->tl_requests, &retry.writes);
2266 spin_unlock_irqrestore(&retry.lock, flags);
2267
2268 /* Drop the extra reference that would otherwise
2269 * have been dropped by complete_master_bio.
2270 * do_retry() needs to grab a new one. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002271 dec_ap_bio(req->w.device);
Lars Ellenberg2312f0b32011-11-24 10:36:25 +01002272
2273 queue_work(retry.wq, &retry.worker);
2274}
2275
2276
Philipp Reisnerb411b362009-09-25 16:07:19 -07002277static void drbd_cleanup(void)
2278{
2279 unsigned int i;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002280 struct drbd_device *device;
Philipp Reisner81fa2e62011-05-04 15:10:30 +02002281 struct drbd_tconn *tconn, *tmp;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002282
2283 unregister_reboot_notifier(&drbd_notifier);
2284
Lars Ellenberg17a93f32010-11-24 10:37:35 +01002285 /* first remove proc,
2286 * drbdsetup uses it's presence to detect
2287 * whether DRBD is loaded.
2288 * If we would get stuck in proc removal,
2289 * but have netlink already deregistered,
2290 * some drbdsetup commands may wait forever
2291 * for an answer.
2292 */
2293 if (drbd_proc)
2294 remove_proc_entry("drbd", NULL);
2295
Lars Ellenberg2312f0b32011-11-24 10:36:25 +01002296 if (retry.wq)
2297 destroy_workqueue(retry.wq);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002298
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002299 drbd_genl_unregister();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002300
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002301 idr_for_each_entry(&minors, device, i) {
2302 idr_remove(&minors, device_to_minor(device));
2303 idr_remove(&device->tconn->volumes, device->vnr);
2304 destroy_workqueue(device->submit.wq);
2305 del_gendisk(device->vdisk);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02002306 /* synchronize_rcu(); No other threads running at this point */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002307 kref_put(&device->kref, &drbd_minor_destroy);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002308 }
2309
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02002310 /* not _rcu since, no other updater anymore. Genl already unregistered */
Philipp Reisner81fa2e62011-05-04 15:10:30 +02002311 list_for_each_entry_safe(tconn, tmp, &drbd_tconns, all_tconn) {
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02002312 list_del(&tconn->all_tconn); /* not _rcu no proc, not other threads */
2313 /* synchronize_rcu(); */
Philipp Reisner81fa2e62011-05-04 15:10:30 +02002314 kref_put(&tconn->kref, &conn_destroy);
2315 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002316
Philipp Reisner81a5d602011-02-22 19:53:16 -05002317 drbd_destroy_mempools();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002318 unregister_blkdev(DRBD_MAJOR, "drbd");
2319
Philipp Reisner81a5d602011-02-22 19:53:16 -05002320 idr_destroy(&minors);
2321
Philipp Reisnerb411b362009-09-25 16:07:19 -07002322 printk(KERN_INFO "drbd: module cleanup done.\n");
2323}
2324
2325/**
Artem Bityutskiyd97482e2012-07-25 18:12:12 +03002326 * drbd_congested() - Callback for the flusher thread
Philipp Reisnerb411b362009-09-25 16:07:19 -07002327 * @congested_data: User data
Artem Bityutskiyd97482e2012-07-25 18:12:12 +03002328 * @bdi_bits: Bits the BDI flusher thread is currently interested in
Philipp Reisnerb411b362009-09-25 16:07:19 -07002329 *
2330 * Returns 1<<BDI_async_congested and/or 1<<BDI_sync_congested if we are congested.
2331 */
2332static int drbd_congested(void *congested_data, int bdi_bits)
2333{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002334 struct drbd_device *device = congested_data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002335 struct request_queue *q;
2336 char reason = '-';
2337 int r = 0;
2338
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002339 if (!may_inc_ap_bio(device)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002340 /* DRBD has frozen IO */
2341 r = bdi_bits;
2342 reason = 'd';
2343 goto out;
2344 }
2345
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002346 if (test_bit(CALLBACK_PENDING, &device->tconn->flags)) {
Lars Ellenbergc2ba6862012-06-14 15:14:06 +02002347 r |= (1 << BDI_async_congested);
2348 /* Without good local data, we would need to read from remote,
2349 * and that would need the worker thread as well, which is
2350 * currently blocked waiting for that usermode helper to
2351 * finish.
2352 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002353 if (!get_ldev_if_state(device, D_UP_TO_DATE))
Lars Ellenbergc2ba6862012-06-14 15:14:06 +02002354 r |= (1 << BDI_sync_congested);
2355 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002356 put_ldev(device);
Lars Ellenbergc2ba6862012-06-14 15:14:06 +02002357 r &= bdi_bits;
2358 reason = 'c';
2359 goto out;
2360 }
2361
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002362 if (get_ldev(device)) {
2363 q = bdev_get_queue(device->ldev->backing_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002364 r = bdi_congested(&q->backing_dev_info, bdi_bits);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002365 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002366 if (r)
2367 reason = 'b';
2368 }
2369
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002370 if (bdi_bits & (1 << BDI_async_congested) && test_bit(NET_CONGESTED, &device->tconn->flags)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002371 r |= (1 << BDI_async_congested);
2372 reason = reason == 'b' ? 'a' : 'n';
2373 }
2374
2375out:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002376 device->congestion_reason = reason;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002377 return r;
2378}
2379
Philipp Reisner6699b652011-02-09 11:10:24 +01002380static void drbd_init_workqueue(struct drbd_work_queue* wq)
2381{
Philipp Reisner6699b652011-02-09 11:10:24 +01002382 spin_lock_init(&wq->q_lock);
2383 INIT_LIST_HEAD(&wq->q);
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02002384 init_waitqueue_head(&wq->q_wait);
Philipp Reisner6699b652011-02-09 11:10:24 +01002385}
2386
Philipp Reisner0ace9df2011-04-24 10:53:19 +02002387struct drbd_tconn *conn_get_by_name(const char *name)
Philipp Reisner1aba4d72011-02-21 15:38:08 +01002388{
2389 struct drbd_tconn *tconn;
2390
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002391 if (!name || !name[0])
2392 return NULL;
2393
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02002394 rcu_read_lock();
Philipp Reisnerec0bddb2011-05-04 15:47:01 +02002395 list_for_each_entry_rcu(tconn, &drbd_tconns, all_tconn) {
Philipp Reisner0ace9df2011-04-24 10:53:19 +02002396 if (!strcmp(tconn->name, name)) {
2397 kref_get(&tconn->kref);
Philipp Reisner1aba4d72011-02-21 15:38:08 +01002398 goto found;
Philipp Reisner0ace9df2011-04-24 10:53:19 +02002399 }
Philipp Reisner1aba4d72011-02-21 15:38:08 +01002400 }
2401 tconn = NULL;
2402found:
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02002403 rcu_read_unlock();
Philipp Reisner1aba4d72011-02-21 15:38:08 +01002404 return tconn;
2405}
2406
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002407struct drbd_tconn *conn_get_by_addrs(void *my_addr, int my_addr_len,
2408 void *peer_addr, int peer_addr_len)
2409{
2410 struct drbd_tconn *tconn;
2411
2412 rcu_read_lock();
2413 list_for_each_entry_rcu(tconn, &drbd_tconns, all_tconn) {
2414 if (tconn->my_addr_len == my_addr_len &&
2415 tconn->peer_addr_len == peer_addr_len &&
2416 !memcmp(&tconn->my_addr, my_addr, my_addr_len) &&
2417 !memcmp(&tconn->peer_addr, peer_addr, peer_addr_len)) {
2418 kref_get(&tconn->kref);
2419 goto found;
2420 }
2421 }
2422 tconn = NULL;
2423found:
2424 rcu_read_unlock();
2425 return tconn;
2426}
2427
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01002428static int drbd_alloc_socket(struct drbd_socket *socket)
2429{
2430 socket->rbuf = (void *) __get_free_page(GFP_KERNEL);
2431 if (!socket->rbuf)
2432 return -ENOMEM;
Andreas Gruenbacher5a87d922011-03-24 21:17:52 +01002433 socket->sbuf = (void *) __get_free_page(GFP_KERNEL);
2434 if (!socket->sbuf)
2435 return -ENOMEM;
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01002436 return 0;
2437}
2438
2439static void drbd_free_socket(struct drbd_socket *socket)
2440{
Andreas Gruenbacher5a87d922011-03-24 21:17:52 +01002441 free_page((unsigned long) socket->sbuf);
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01002442 free_page((unsigned long) socket->rbuf);
2443}
2444
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002445void conn_free_crypto(struct drbd_tconn *tconn)
2446{
Philipp Reisner1d041222011-04-22 15:20:23 +02002447 drbd_free_sock(tconn);
2448
2449 crypto_free_hash(tconn->csums_tfm);
2450 crypto_free_hash(tconn->verify_tfm);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002451 crypto_free_hash(tconn->cram_hmac_tfm);
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02002452 crypto_free_hash(tconn->integrity_tfm);
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02002453 crypto_free_hash(tconn->peer_integrity_tfm);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002454 kfree(tconn->int_dig_in);
2455 kfree(tconn->int_dig_vv);
Philipp Reisner1d041222011-04-22 15:20:23 +02002456
2457 tconn->csums_tfm = NULL;
2458 tconn->verify_tfm = NULL;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002459 tconn->cram_hmac_tfm = NULL;
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02002460 tconn->integrity_tfm = NULL;
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02002461 tconn->peer_integrity_tfm = NULL;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002462 tconn->int_dig_in = NULL;
2463 tconn->int_dig_vv = NULL;
2464}
2465
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02002466int set_resource_options(struct drbd_tconn *tconn, struct res_opts *res_opts)
2467{
2468 cpumask_var_t new_cpu_mask;
2469 int err;
2470
2471 if (!zalloc_cpumask_var(&new_cpu_mask, GFP_KERNEL))
2472 return -ENOMEM;
2473 /*
2474 retcode = ERR_NOMEM;
2475 drbd_msg_put_info("unable to allocate cpumask");
2476 */
2477
2478 /* silently ignore cpu mask on UP kernel */
2479 if (nr_cpu_ids > 1 && res_opts->cpu_mask[0] != 0) {
2480 /* FIXME: Get rid of constant 32 here */
Philipp Reisnerc5b005a2012-04-30 12:53:52 +02002481 err = bitmap_parse(res_opts->cpu_mask, 32,
2482 cpumask_bits(new_cpu_mask), nr_cpu_ids);
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02002483 if (err) {
Philipp Reisnerc5b005a2012-04-30 12:53:52 +02002484 conn_warn(tconn, "bitmap_parse() failed with %d\n", err);
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02002485 /* retcode = ERR_CPU_MASK_PARSE; */
2486 goto fail;
2487 }
2488 }
2489 tconn->res_opts = *res_opts;
2490 if (!cpumask_equal(tconn->cpu_mask, new_cpu_mask)) {
2491 cpumask_copy(tconn->cpu_mask, new_cpu_mask);
2492 drbd_calc_cpu_mask(tconn);
2493 tconn->receiver.reset_cpu_mask = 1;
2494 tconn->asender.reset_cpu_mask = 1;
2495 tconn->worker.reset_cpu_mask = 1;
2496 }
2497 err = 0;
2498
2499fail:
2500 free_cpumask_var(new_cpu_mask);
2501 return err;
2502
2503}
2504
Philipp Reisnerec0bddb2011-05-04 15:47:01 +02002505/* caller must be under genl_lock() */
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02002506struct drbd_tconn *conn_create(const char *name, struct res_opts *res_opts)
Philipp Reisner21114382011-01-19 12:26:59 +01002507{
2508 struct drbd_tconn *tconn;
2509
2510 tconn = kzalloc(sizeof(struct drbd_tconn), GFP_KERNEL);
2511 if (!tconn)
2512 return NULL;
2513
2514 tconn->name = kstrdup(name, GFP_KERNEL);
2515 if (!tconn->name)
2516 goto fail;
2517
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01002518 if (drbd_alloc_socket(&tconn->data))
2519 goto fail;
2520 if (drbd_alloc_socket(&tconn->meta))
2521 goto fail;
2522
Philipp Reisner774b3052011-02-22 02:07:03 -05002523 if (!zalloc_cpumask_var(&tconn->cpu_mask, GFP_KERNEL))
2524 goto fail;
2525
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02002526 if (set_resource_options(tconn, res_opts))
2527 goto fail;
2528
Philipp Reisner12038a32011-11-09 19:18:00 +01002529 tconn->current_epoch = kzalloc(sizeof(struct drbd_epoch), GFP_KERNEL);
2530 if (!tconn->current_epoch)
2531 goto fail;
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01002532
2533 INIT_LIST_HEAD(&tconn->transfer_log);
2534
Philipp Reisner12038a32011-11-09 19:18:00 +01002535 INIT_LIST_HEAD(&tconn->current_epoch->list);
2536 tconn->epochs = 1;
2537 spin_lock_init(&tconn->epoch_lock);
Philipp Reisner4b0007c2011-11-09 20:12:34 +01002538 tconn->write_ordering = WO_bdev_flush;
2539
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01002540 tconn->send.seen_any_write_yet = false;
2541 tconn->send.current_epoch_nr = 0;
2542 tconn->send.current_epoch_writes = 0;
2543
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002544 tconn->cstate = C_STANDALONE;
Philipp Reisner8410da8f02011-02-11 20:11:10 +01002545 mutex_init(&tconn->cstate_mutex);
Philipp Reisner6699b652011-02-09 11:10:24 +01002546 spin_lock_init(&tconn->req_lock);
Philipp Reisnera0095502011-05-03 13:14:15 +02002547 mutex_init(&tconn->conf_update);
Philipp Reisner2a67d8b2011-02-09 14:10:32 +01002548 init_waitqueue_head(&tconn->ping_wait);
Philipp Reisner062e8792011-02-08 11:09:18 +01002549 idr_init(&tconn->volumes);
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01002550
Lars Ellenbergd5b27b02011-11-14 15:42:37 +01002551 drbd_init_workqueue(&tconn->sender_work);
Philipp Reisner6699b652011-02-09 11:10:24 +01002552 mutex_init(&tconn->data.mutex);
Philipp Reisner6699b652011-02-09 11:10:24 +01002553 mutex_init(&tconn->meta.mutex);
2554
Philipp Reisner392c8802011-02-09 10:33:31 +01002555 drbd_thread_init(tconn, &tconn->receiver, drbdd_init, "receiver");
2556 drbd_thread_init(tconn, &tconn->worker, drbd_worker, "worker");
2557 drbd_thread_init(tconn, &tconn->asender, drbd_asender, "asender");
2558
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02002559 kref_init(&tconn->kref);
Philipp Reisnerec0bddb2011-05-04 15:47:01 +02002560 list_add_tail_rcu(&tconn->all_tconn, &drbd_tconns);
Philipp Reisner21114382011-01-19 12:26:59 +01002561
2562 return tconn;
2563
2564fail:
Philipp Reisner12038a32011-11-09 19:18:00 +01002565 kfree(tconn->current_epoch);
Philipp Reisner774b3052011-02-22 02:07:03 -05002566 free_cpumask_var(tconn->cpu_mask);
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01002567 drbd_free_socket(&tconn->meta);
2568 drbd_free_socket(&tconn->data);
Philipp Reisner21114382011-01-19 12:26:59 +01002569 kfree(tconn->name);
2570 kfree(tconn);
2571
2572 return NULL;
2573}
2574
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02002575void conn_destroy(struct kref *kref)
Philipp Reisner21114382011-01-19 12:26:59 +01002576{
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02002577 struct drbd_tconn *tconn = container_of(kref, struct drbd_tconn, kref);
2578
Philipp Reisner12038a32011-11-09 19:18:00 +01002579 if (atomic_read(&tconn->current_epoch->epoch_size) != 0)
2580 conn_err(tconn, "epoch_size:%d\n", atomic_read(&tconn->current_epoch->epoch_size));
2581 kfree(tconn->current_epoch);
2582
Philipp Reisner062e8792011-02-08 11:09:18 +01002583 idr_destroy(&tconn->volumes);
Philipp Reisner21114382011-01-19 12:26:59 +01002584
Philipp Reisner774b3052011-02-22 02:07:03 -05002585 free_cpumask_var(tconn->cpu_mask);
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01002586 drbd_free_socket(&tconn->meta);
2587 drbd_free_socket(&tconn->data);
Philipp Reisner21114382011-01-19 12:26:59 +01002588 kfree(tconn->name);
Philipp Reisnerb42a70a2011-01-27 10:55:20 +01002589 kfree(tconn->int_dig_in);
2590 kfree(tconn->int_dig_vv);
Philipp Reisner21114382011-01-19 12:26:59 +01002591 kfree(tconn);
2592}
2593
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002594static int init_submitter(struct drbd_device *device)
Lars Ellenberg113fef92013-03-22 18:14:40 -06002595{
2596 /* opencoded create_singlethread_workqueue(),
2597 * to be able to say "drbd%d", ..., minor */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002598 device->submit.wq = alloc_workqueue("drbd%u_submit",
2599 WQ_UNBOUND | WQ_MEM_RECLAIM, 1, device->minor);
2600 if (!device->submit.wq)
Lars Ellenberg113fef92013-03-22 18:14:40 -06002601 return -ENOMEM;
2602
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002603 INIT_WORK(&device->submit.worker, do_submit);
2604 spin_lock_init(&device->submit.lock);
2605 INIT_LIST_HEAD(&device->submit.writes);
Lars Ellenberg113fef92013-03-22 18:14:40 -06002606 return 0;
2607}
2608
Philipp Reisner774b3052011-02-22 02:07:03 -05002609enum drbd_ret_code conn_new_minor(struct drbd_tconn *tconn, unsigned int minor, int vnr)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002610{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002611 struct drbd_device *device;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002612 struct gendisk *disk;
2613 struct request_queue *q;
Philipp Reisner774b3052011-02-22 02:07:03 -05002614 int vnr_got = vnr;
Philipp Reisner81a5d602011-02-22 19:53:16 -05002615 int minor_got = minor;
Lars Ellenberg8432b312011-03-08 16:11:16 +01002616 enum drbd_ret_code err = ERR_NOMEM;
Philipp Reisner774b3052011-02-22 02:07:03 -05002617
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002618 device = minor_to_device(minor);
2619 if (device)
Philipp Reisner774b3052011-02-22 02:07:03 -05002620 return ERR_MINOR_EXISTS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002621
2622 /* GFP_KERNEL, we are outside of all write-out paths */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002623 device = kzalloc(sizeof(struct drbd_device), GFP_KERNEL);
2624 if (!device)
Philipp Reisner774b3052011-02-22 02:07:03 -05002625 return ERR_NOMEM;
2626
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02002627 kref_get(&tconn->kref);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002628 device->tconn = tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002629
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002630 device->minor = minor;
2631 device->vnr = vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002632
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002633 drbd_init_set_defaults(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002634
2635 q = blk_alloc_queue(GFP_KERNEL);
2636 if (!q)
2637 goto out_no_q;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002638 device->rq_queue = q;
2639 q->queuedata = device;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002640
2641 disk = alloc_disk(1);
2642 if (!disk)
2643 goto out_no_disk;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002644 device->vdisk = disk;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002645
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002646 set_disk_ro(disk, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002647
2648 disk->queue = q;
2649 disk->major = DRBD_MAJOR;
2650 disk->first_minor = minor;
2651 disk->fops = &drbd_ops;
2652 sprintf(disk->disk_name, "drbd%d", minor);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002653 disk->private_data = device;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002654
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002655 device->this_bdev = bdget(MKDEV(DRBD_MAJOR, minor));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002656 /* we have no partitions. we contain only ourselves. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002657 device->this_bdev->bd_contains = device->this_bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002658
2659 q->backing_dev_info.congested_fn = drbd_congested;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002660 q->backing_dev_info.congested_data = device;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002661
Andreas Gruenbacher2f58dcf2010-12-13 17:48:19 +01002662 blk_queue_make_request(q, drbd_make_request);
Lars Ellenberga73ff322012-06-25 19:15:38 +02002663 blk_queue_flush(q, REQ_FLUSH | REQ_FUA);
Philipp Reisner99432fc2011-05-20 16:39:13 +02002664 /* Setting the max_hw_sectors to an odd value of 8kibyte here
2665 This triggers a max_bio_size message upon first attach or connect */
2666 blk_queue_max_hw_sectors(q, DRBD_MAX_BIO_SIZE_SAFE >> 8);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002667 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
2668 blk_queue_merge_bvec(q, drbd_merge_bvec);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002669 q->queue_lock = &device->tconn->req_lock; /* needed since we use */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002670
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002671 device->md_io_page = alloc_page(GFP_KERNEL);
2672 if (!device->md_io_page)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002673 goto out_no_io_page;
2674
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002675 if (drbd_bm_init(device))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002676 goto out_no_bitmap;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002677 device->read_requests = RB_ROOT;
2678 device->write_requests = RB_ROOT;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002679
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002680 minor_got = idr_alloc(&minors, device, minor, minor + 1, GFP_KERNEL);
Tejun Heo56de2102013-02-27 17:04:01 -08002681 if (minor_got < 0) {
2682 if (minor_got == -ENOSPC) {
2683 err = ERR_MINOR_EXISTS;
2684 drbd_msg_put_info("requested minor exists already");
2685 }
Lars Ellenberg8432b312011-03-08 16:11:16 +01002686 goto out_no_minor_idr;
Tejun Heo56de2102013-02-27 17:04:01 -08002687 }
2688
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002689 vnr_got = idr_alloc(&tconn->volumes, device, vnr, vnr + 1, GFP_KERNEL);
Tejun Heo56de2102013-02-27 17:04:01 -08002690 if (vnr_got < 0) {
2691 if (vnr_got == -ENOSPC) {
2692 err = ERR_INVALID_REQUEST;
2693 drbd_msg_put_info("requested volume exists already");
2694 }
Lars Ellenberg8432b312011-03-08 16:11:16 +01002695 goto out_idr_remove_minor;
Lars Ellenberg569083c2011-03-07 09:49:02 +01002696 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002697
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002698 if (init_submitter(device)) {
Lars Ellenberg113fef92013-03-22 18:14:40 -06002699 err = ERR_NOMEM;
2700 drbd_msg_put_info("unable to create submit workqueue");
2701 goto out_idr_remove_vol;
2702 }
2703
Philipp Reisner774b3052011-02-22 02:07:03 -05002704 add_disk(disk);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002705 kref_init(&device->kref); /* one ref for both idrs and the the add_disk */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002706
Philipp Reisner2325eb62011-03-15 16:56:18 +01002707 /* inherit the connection state */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002708 device->state.conn = tconn->cstate;
2709 if (device->state.conn == C_WF_REPORT_PARAMS)
2710 drbd_connected(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002711
Philipp Reisner774b3052011-02-22 02:07:03 -05002712 return NO_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002713
Lars Ellenberg113fef92013-03-22 18:14:40 -06002714out_idr_remove_vol:
2715 idr_remove(&tconn->volumes, vnr_got);
Lars Ellenberg8432b312011-03-08 16:11:16 +01002716out_idr_remove_minor:
2717 idr_remove(&minors, minor_got);
Lars Ellenberg569083c2011-03-07 09:49:02 +01002718 synchronize_rcu();
Lars Ellenberg8432b312011-03-08 16:11:16 +01002719out_no_minor_idr:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002720 drbd_bm_cleanup(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002721out_no_bitmap:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002722 __free_page(device->md_io_page);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002723out_no_io_page:
2724 put_disk(disk);
2725out_no_disk:
2726 blk_cleanup_queue(q);
2727out_no_q:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002728 kfree(device);
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02002729 kref_put(&tconn->kref, &conn_destroy);
Lars Ellenberg8432b312011-03-08 16:11:16 +01002730 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002731}
2732
Philipp Reisnerb411b362009-09-25 16:07:19 -07002733int __init drbd_init(void)
2734{
2735 int err;
2736
Philipp Reisner2b8a90b2011-01-10 11:15:17 +01002737 if (minor_count < DRBD_MINOR_COUNT_MIN || minor_count > DRBD_MINOR_COUNT_MAX) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002738 printk(KERN_ERR
Philipp Reisner81a5d602011-02-22 19:53:16 -05002739 "drbd: invalid minor_count (%d)\n", minor_count);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002740#ifdef MODULE
2741 return -EINVAL;
2742#else
Andreas Gruenbacher46530e82011-05-31 13:08:53 +02002743 minor_count = DRBD_MINOR_COUNT_DEF;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002744#endif
2745 }
2746
Philipp Reisnerb411b362009-09-25 16:07:19 -07002747 err = register_blkdev(DRBD_MAJOR, "drbd");
2748 if (err) {
2749 printk(KERN_ERR
2750 "drbd: unable to register block device major %d\n",
2751 DRBD_MAJOR);
2752 return err;
2753 }
2754
2755 register_reboot_notifier(&drbd_notifier);
2756
2757 /*
2758 * allocate all necessary structs
2759 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002760 init_waitqueue_head(&drbd_pp_wait);
2761
2762 drbd_proc = NULL; /* play safe for drbd_cleanup */
Philipp Reisner81a5d602011-02-22 19:53:16 -05002763 idr_init(&minors);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002764
Lars Ellenberg69babf02013-10-23 10:59:15 +02002765 rwlock_init(&global_state_lock);
2766 INIT_LIST_HEAD(&drbd_tconns);
2767
2768 err = drbd_genl_register();
2769 if (err) {
2770 printk(KERN_ERR "drbd: unable to register generic netlink family\n");
2771 goto fail;
2772 }
2773
Philipp Reisnerb411b362009-09-25 16:07:19 -07002774 err = drbd_create_mempools();
2775 if (err)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002776 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002777
Wei Yongjun6110d702013-06-25 16:50:04 +02002778 err = -ENOMEM;
Lars Ellenberg8c484ee2010-03-11 16:47:58 +01002779 drbd_proc = proc_create_data("drbd", S_IFREG | S_IRUGO , NULL, &drbd_proc_fops, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002780 if (!drbd_proc) {
2781 printk(KERN_ERR "drbd: unable to register proc file\n");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002782 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002783 }
2784
Lars Ellenberg2312f0b32011-11-24 10:36:25 +01002785 retry.wq = create_singlethread_workqueue("drbd-reissue");
2786 if (!retry.wq) {
2787 printk(KERN_ERR "drbd: unable to create retry workqueue\n");
2788 goto fail;
2789 }
2790 INIT_WORK(&retry.worker, do_retry);
2791 spin_lock_init(&retry.lock);
2792 INIT_LIST_HEAD(&retry.writes);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002793
2794 printk(KERN_INFO "drbd: initialized. "
2795 "Version: " REL_VERSION " (api:%d/proto:%d-%d)\n",
2796 API_VERSION, PRO_VERSION_MIN, PRO_VERSION_MAX);
2797 printk(KERN_INFO "drbd: %s\n", drbd_buildtag());
2798 printk(KERN_INFO "drbd: registered as block device major %d\n",
2799 DRBD_MAJOR);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002800
2801 return 0; /* Success! */
2802
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002803fail:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002804 drbd_cleanup();
2805 if (err == -ENOMEM)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002806 printk(KERN_ERR "drbd: ran out of memory\n");
2807 else
2808 printk(KERN_ERR "drbd: initialization failure\n");
2809 return err;
2810}
2811
2812void drbd_free_bc(struct drbd_backing_dev *ldev)
2813{
2814 if (ldev == NULL)
2815 return;
2816
Tejun Heoe525fd82010-11-13 11:55:17 +01002817 blkdev_put(ldev->backing_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
2818 blkdev_put(ldev->md_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002819
Lars Ellenberg94ad0a12013-03-27 14:08:42 +01002820 kfree(ldev->disk_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002821 kfree(ldev);
2822}
2823
Philipp Reisner360cc742011-02-08 14:29:53 +01002824void drbd_free_sock(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002825{
Philipp Reisner360cc742011-02-08 14:29:53 +01002826 if (tconn->data.socket) {
2827 mutex_lock(&tconn->data.mutex);
2828 kernel_sock_shutdown(tconn->data.socket, SHUT_RDWR);
2829 sock_release(tconn->data.socket);
2830 tconn->data.socket = NULL;
2831 mutex_unlock(&tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002832 }
Philipp Reisner360cc742011-02-08 14:29:53 +01002833 if (tconn->meta.socket) {
2834 mutex_lock(&tconn->meta.mutex);
2835 kernel_sock_shutdown(tconn->meta.socket, SHUT_RDWR);
2836 sock_release(tconn->meta.socket);
2837 tconn->meta.socket = NULL;
2838 mutex_unlock(&tconn->meta.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002839 }
2840}
2841
Philipp Reisnerb411b362009-09-25 16:07:19 -07002842/* meta data management */
2843
Philipp Reisner19fffd72012-08-28 16:48:03 +02002844void conn_md_sync(struct drbd_tconn *tconn)
2845{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002846 struct drbd_device *device;
Philipp Reisner19fffd72012-08-28 16:48:03 +02002847 int vnr;
2848
2849 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002850 idr_for_each_entry(&tconn->volumes, device, vnr) {
2851 kref_get(&device->kref);
Philipp Reisner19fffd72012-08-28 16:48:03 +02002852 rcu_read_unlock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002853 drbd_md_sync(device);
2854 kref_put(&device->kref, &drbd_minor_destroy);
Philipp Reisner19fffd72012-08-28 16:48:03 +02002855 rcu_read_lock();
2856 }
2857 rcu_read_unlock();
2858}
2859
Lars Ellenbergae8bf312013-03-19 18:16:43 +01002860/* aligned 4kByte */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002861struct meta_data_on_disk {
Lars Ellenbergcccac982013-03-19 18:16:46 +01002862 u64 la_size_sect; /* last agreed size. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002863 u64 uuid[UI_SIZE]; /* UUIDs. */
2864 u64 device_uuid;
2865 u64 reserved_u64_1;
2866 u32 flags; /* MDF */
2867 u32 magic;
2868 u32 md_size_sect;
2869 u32 al_offset; /* offset to this block */
Lars Ellenbergae8bf312013-03-19 18:16:43 +01002870 u32 al_nr_extents; /* important for restoring the AL (userspace) */
Lars Ellenbergf3990022011-03-23 14:31:09 +01002871 /* `-- act_log->nr_elements <-- ldev->dc.al_extents */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002872 u32 bm_offset; /* offset to the bitmap, from here */
2873 u32 bm_bytes_per_bit; /* BM_BLOCK_SIZE */
Philipp Reisner99432fc2011-05-20 16:39:13 +02002874 u32 la_peer_max_bio_size; /* last peer max_bio_size */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002875
Lars Ellenberg3a4d4eb2013-03-19 18:16:44 +01002876 /* see al_tr_number_to_on_disk_sector() */
2877 u32 al_stripes;
2878 u32 al_stripe_size_4k;
2879
2880 u8 reserved_u8[4096 - (7*8 + 10*4)];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002881} __packed;
2882
Philipp Reisnerd752b262013-06-25 16:50:08 +02002883
2884
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002885void drbd_md_write(struct drbd_device *device, void *b)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002886{
Philipp Reisnerd752b262013-06-25 16:50:08 +02002887 struct meta_data_on_disk *buffer = b;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002888 sector_t sector;
2889 int i;
2890
Lars Ellenbergae8bf312013-03-19 18:16:43 +01002891 memset(buffer, 0, sizeof(*buffer));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002892
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002893 buffer->la_size_sect = cpu_to_be64(drbd_get_capacity(device->this_bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002894 for (i = UI_CURRENT; i < UI_SIZE; i++)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002895 buffer->uuid[i] = cpu_to_be64(device->ldev->md.uuid[i]);
2896 buffer->flags = cpu_to_be32(device->ldev->md.flags);
Lars Ellenbergd5d7ebd2011-07-05 20:59:26 +02002897 buffer->magic = cpu_to_be32(DRBD_MD_MAGIC_84_UNCLEAN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002898
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002899 buffer->md_size_sect = cpu_to_be32(device->ldev->md.md_size_sect);
2900 buffer->al_offset = cpu_to_be32(device->ldev->md.al_offset);
2901 buffer->al_nr_extents = cpu_to_be32(device->act_log->nr_elements);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002902 buffer->bm_bytes_per_bit = cpu_to_be32(BM_BLOCK_SIZE);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002903 buffer->device_uuid = cpu_to_be64(device->ldev->md.device_uuid);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002904
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002905 buffer->bm_offset = cpu_to_be32(device->ldev->md.bm_offset);
2906 buffer->la_peer_max_bio_size = cpu_to_be32(device->peer_max_bio_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002907
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002908 buffer->al_stripes = cpu_to_be32(device->ldev->md.al_stripes);
2909 buffer->al_stripe_size_4k = cpu_to_be32(device->ldev->md.al_stripe_size_4k);
Lars Ellenberg3a4d4eb2013-03-19 18:16:44 +01002910
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002911 D_ASSERT(drbd_md_ss(device->ldev) == device->ldev->md.md_offset);
2912 sector = device->ldev->md.md_offset;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002913
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002914 if (drbd_md_sync_page_io(device, device->ldev, sector, WRITE)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002915 /* this was a try anyways ... */
2916 dev_err(DEV, "meta data update failed!\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002917 drbd_chk_io_error(device, 1, DRBD_META_IO_ERROR);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002918 }
Philipp Reisnerd752b262013-06-25 16:50:08 +02002919}
2920
2921/**
2922 * drbd_md_sync() - Writes the meta data super block if the MD_DIRTY flag bit is set
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002923 * @device: DRBD device.
Philipp Reisnerd752b262013-06-25 16:50:08 +02002924 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002925void drbd_md_sync(struct drbd_device *device)
Philipp Reisnerd752b262013-06-25 16:50:08 +02002926{
2927 struct meta_data_on_disk *buffer;
2928
2929 /* Don't accidentally change the DRBD meta data layout. */
2930 BUILD_BUG_ON(UI_SIZE != 4);
2931 BUILD_BUG_ON(sizeof(struct meta_data_on_disk) != 4096);
2932
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002933 del_timer(&device->md_sync_timer);
Philipp Reisnerd752b262013-06-25 16:50:08 +02002934 /* timer may be rearmed by drbd_md_mark_dirty() now. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002935 if (!test_and_clear_bit(MD_DIRTY, &device->flags))
Philipp Reisnerd752b262013-06-25 16:50:08 +02002936 return;
2937
2938 /* We use here D_FAILED and not D_ATTACHING because we try to write
2939 * metadata even if we detach due to a disk failure! */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002940 if (!get_ldev_if_state(device, D_FAILED))
Philipp Reisnerd752b262013-06-25 16:50:08 +02002941 return;
2942
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002943 buffer = drbd_md_get_buffer(device);
Philipp Reisnerd752b262013-06-25 16:50:08 +02002944 if (!buffer)
2945 goto out;
2946
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002947 drbd_md_write(device, buffer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002948
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002949 /* Update device->ldev->md.la_size_sect,
Philipp Reisnerb411b362009-09-25 16:07:19 -07002950 * since we updated it on metadata. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002951 device->ldev->md.la_size_sect = drbd_get_capacity(device->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002952
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002953 drbd_md_put_buffer(device);
Philipp Reisnere1711732011-06-27 11:51:46 +02002954out:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002955 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002956}
2957
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002958static int check_activity_log_stripe_size(struct drbd_device *device,
Lars Ellenberg3a4d4eb2013-03-19 18:16:44 +01002959 struct meta_data_on_disk *on_disk,
2960 struct drbd_md *in_core)
2961{
2962 u32 al_stripes = be32_to_cpu(on_disk->al_stripes);
2963 u32 al_stripe_size_4k = be32_to_cpu(on_disk->al_stripe_size_4k);
2964 u64 al_size_4k;
2965
2966 /* both not set: default to old fixed size activity log */
2967 if (al_stripes == 0 && al_stripe_size_4k == 0) {
2968 al_stripes = 1;
2969 al_stripe_size_4k = MD_32kB_SECT/8;
2970 }
2971
2972 /* some paranoia plausibility checks */
2973
2974 /* we need both values to be set */
2975 if (al_stripes == 0 || al_stripe_size_4k == 0)
2976 goto err;
2977
2978 al_size_4k = (u64)al_stripes * al_stripe_size_4k;
2979
2980 /* Upper limit of activity log area, to avoid potential overflow
2981 * problems in al_tr_number_to_on_disk_sector(). As right now, more
2982 * than 72 * 4k blocks total only increases the amount of history,
2983 * limiting this arbitrarily to 16 GB is not a real limitation ;-) */
2984 if (al_size_4k > (16 * 1024 * 1024/4))
2985 goto err;
2986
2987 /* Lower limit: we need at least 8 transaction slots (32kB)
2988 * to not break existing setups */
2989 if (al_size_4k < MD_32kB_SECT/8)
2990 goto err;
2991
2992 in_core->al_stripe_size_4k = al_stripe_size_4k;
2993 in_core->al_stripes = al_stripes;
2994 in_core->al_size_4k = al_size_4k;
2995
2996 return 0;
2997err:
2998 dev_err(DEV, "invalid activity log striping: al_stripes=%u, al_stripe_size_4k=%u\n",
2999 al_stripes, al_stripe_size_4k);
3000 return -EINVAL;
3001}
3002
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003003static int check_offsets_and_sizes(struct drbd_device *device, struct drbd_backing_dev *bdev)
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +01003004{
3005 sector_t capacity = drbd_get_capacity(bdev->md_bdev);
3006 struct drbd_md *in_core = &bdev->md;
3007 s32 on_disk_al_sect;
3008 s32 on_disk_bm_sect;
3009
3010 /* The on-disk size of the activity log, calculated from offsets, and
3011 * the size of the activity log calculated from the stripe settings,
3012 * should match.
3013 * Though we could relax this a bit: it is ok, if the striped activity log
3014 * fits in the available on-disk activity log size.
3015 * Right now, that would break how resize is implemented.
3016 * TODO: make drbd_determine_dev_size() (and the drbdmeta tool) aware
3017 * of possible unused padding space in the on disk layout. */
3018 if (in_core->al_offset < 0) {
3019 if (in_core->bm_offset > in_core->al_offset)
3020 goto err;
3021 on_disk_al_sect = -in_core->al_offset;
3022 on_disk_bm_sect = in_core->al_offset - in_core->bm_offset;
3023 } else {
3024 if (in_core->al_offset != MD_4kB_SECT)
3025 goto err;
3026 if (in_core->bm_offset < in_core->al_offset + in_core->al_size_4k * MD_4kB_SECT)
3027 goto err;
3028
3029 on_disk_al_sect = in_core->bm_offset - MD_4kB_SECT;
3030 on_disk_bm_sect = in_core->md_size_sect - in_core->bm_offset;
3031 }
3032
3033 /* old fixed size meta data is exactly that: fixed. */
3034 if (in_core->meta_dev_idx >= 0) {
3035 if (in_core->md_size_sect != MD_128MB_SECT
3036 || in_core->al_offset != MD_4kB_SECT
3037 || in_core->bm_offset != MD_4kB_SECT + MD_32kB_SECT
3038 || in_core->al_stripes != 1
3039 || in_core->al_stripe_size_4k != MD_32kB_SECT/8)
3040 goto err;
3041 }
3042
3043 if (capacity < in_core->md_size_sect)
3044 goto err;
3045 if (capacity - in_core->md_size_sect < drbd_md_first_sector(bdev))
3046 goto err;
3047
3048 /* should be aligned, and at least 32k */
3049 if ((on_disk_al_sect & 7) || (on_disk_al_sect < MD_32kB_SECT))
3050 goto err;
3051
3052 /* should fit (for now: exactly) into the available on-disk space;
3053 * overflow prevention is in check_activity_log_stripe_size() above. */
3054 if (on_disk_al_sect != in_core->al_size_4k * MD_4kB_SECT)
3055 goto err;
3056
3057 /* again, should be aligned */
3058 if (in_core->bm_offset & 7)
3059 goto err;
3060
3061 /* FIXME check for device grow with flex external meta data? */
3062
3063 /* can the available bitmap space cover the last agreed device size? */
3064 if (on_disk_bm_sect < (in_core->la_size_sect+7)/MD_4kB_SECT/8/512)
3065 goto err;
3066
3067 return 0;
3068
3069err:
3070 dev_err(DEV, "meta data offsets don't make sense: idx=%d "
3071 "al_s=%u, al_sz4k=%u, al_offset=%d, bm_offset=%d, "
3072 "md_size_sect=%u, la_size=%llu, md_capacity=%llu\n",
3073 in_core->meta_dev_idx,
3074 in_core->al_stripes, in_core->al_stripe_size_4k,
3075 in_core->al_offset, in_core->bm_offset, in_core->md_size_sect,
3076 (unsigned long long)in_core->la_size_sect,
3077 (unsigned long long)capacity);
3078
3079 return -EINVAL;
3080}
3081
3082
Philipp Reisnerb411b362009-09-25 16:07:19 -07003083/**
3084 * drbd_md_read() - Reads in the meta data super block
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003085 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -07003086 * @bdev: Device from which the meta data should be read in.
3087 *
Lars Ellenberg3a4d4eb2013-03-19 18:16:44 +01003088 * Return NO_ERROR on success, and an enum drbd_ret_code in case
Lars Ellenbergd5d7ebd2011-07-05 20:59:26 +02003089 * something goes wrong.
Lars Ellenberg3a4d4eb2013-03-19 18:16:44 +01003090 *
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +01003091 * Called exactly once during drbd_adm_attach(), while still being D_DISKLESS,
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003092 * even before @bdev is assigned to @device->ldev.
Philipp Reisnerb411b362009-09-25 16:07:19 -07003093 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003094int drbd_md_read(struct drbd_device *device, struct drbd_backing_dev *bdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003095{
3096 struct meta_data_on_disk *buffer;
Lars Ellenbergd5d7ebd2011-07-05 20:59:26 +02003097 u32 magic, flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003098 int i, rv = NO_ERROR;
3099
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003100 if (device->state.disk != D_DISKLESS)
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +01003101 return ERR_DISK_CONFIGURED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003102
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003103 buffer = drbd_md_get_buffer(device);
Philipp Reisnere1711732011-06-27 11:51:46 +02003104 if (!buffer)
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +01003105 return ERR_NOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003106
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +01003107 /* First, figure out where our meta data superblock is located,
3108 * and read it. */
Lars Ellenberg3a4d4eb2013-03-19 18:16:44 +01003109 bdev->md.meta_dev_idx = bdev->disk_conf->meta_dev_idx;
3110 bdev->md.md_offset = drbd_md_ss(bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003111
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003112 if (drbd_md_sync_page_io(device, bdev, bdev->md.md_offset, READ)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003113 /* NOTE: can't do normal error processing here as this is
Philipp Reisnerb411b362009-09-25 16:07:19 -07003114 called BEFORE disk is attached */
3115 dev_err(DEV, "Error while reading metadata.\n");
3116 rv = ERR_IO_MD_DISK;
3117 goto err;
3118 }
3119
Lars Ellenbergd5d7ebd2011-07-05 20:59:26 +02003120 magic = be32_to_cpu(buffer->magic);
3121 flags = be32_to_cpu(buffer->flags);
3122 if (magic == DRBD_MD_MAGIC_84_UNCLEAN ||
3123 (magic == DRBD_MD_MAGIC_08 && !(flags & MDF_AL_CLEAN))) {
3124 /* btw: that's Activity Log clean, not "all" clean. */
3125 dev_err(DEV, "Found unclean meta data. Did you \"drbdadm apply-al\"?\n");
3126 rv = ERR_MD_UNCLEAN;
3127 goto err;
3128 }
Lars Ellenberg3a4d4eb2013-03-19 18:16:44 +01003129
3130 rv = ERR_MD_INVALID;
Lars Ellenbergd5d7ebd2011-07-05 20:59:26 +02003131 if (magic != DRBD_MD_MAGIC_08) {
Philipp Reisner43de7c82011-11-10 13:16:13 +01003132 if (magic == DRBD_MD_MAGIC_07)
Lars Ellenbergd5d7ebd2011-07-05 20:59:26 +02003133 dev_err(DEV, "Found old (0.7) meta data magic. Did you \"drbdadm create-md\"?\n");
3134 else
3135 dev_err(DEV, "Meta data magic not found. Did you \"drbdadm create-md\"?\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003136 goto err;
3137 }
3138
3139 if (be32_to_cpu(buffer->bm_bytes_per_bit) != BM_BLOCK_SIZE) {
3140 dev_err(DEV, "unexpected bm_bytes_per_bit: %u (expected %u)\n",
3141 be32_to_cpu(buffer->bm_bytes_per_bit), BM_BLOCK_SIZE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003142 goto err;
3143 }
3144
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +01003145
3146 /* convert to in_core endian */
3147 bdev->md.la_size_sect = be64_to_cpu(buffer->la_size_sect);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003148 for (i = UI_CURRENT; i < UI_SIZE; i++)
3149 bdev->md.uuid[i] = be64_to_cpu(buffer->uuid[i]);
3150 bdev->md.flags = be32_to_cpu(buffer->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003151 bdev->md.device_uuid = be64_to_cpu(buffer->device_uuid);
3152
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +01003153 bdev->md.md_size_sect = be32_to_cpu(buffer->md_size_sect);
3154 bdev->md.al_offset = be32_to_cpu(buffer->al_offset);
3155 bdev->md.bm_offset = be32_to_cpu(buffer->bm_offset);
3156
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003157 if (check_activity_log_stripe_size(device, buffer, &bdev->md))
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +01003158 goto err;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003159 if (check_offsets_and_sizes(device, bdev))
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +01003160 goto err;
3161
Philipp Reisnerb411b362009-09-25 16:07:19 -07003162 if (be32_to_cpu(buffer->bm_offset) != bdev->md.bm_offset) {
3163 dev_err(DEV, "unexpected bm_offset: %d (expected %d)\n",
3164 be32_to_cpu(buffer->bm_offset), bdev->md.bm_offset);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003165 goto err;
3166 }
3167 if (be32_to_cpu(buffer->md_size_sect) != bdev->md.md_size_sect) {
3168 dev_err(DEV, "unexpected md_size: %u (expected %u)\n",
3169 be32_to_cpu(buffer->md_size_sect), bdev->md.md_size_sect);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003170 goto err;
3171 }
3172
Lars Ellenberg3a4d4eb2013-03-19 18:16:44 +01003173 rv = NO_ERROR;
3174
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003175 spin_lock_irq(&device->tconn->req_lock);
3176 if (device->state.conn < C_CONNECTED) {
Lars Ellenbergdb141b22012-06-25 19:15:58 +02003177 unsigned int peer;
Philipp Reisner99432fc2011-05-20 16:39:13 +02003178 peer = be32_to_cpu(buffer->la_peer_max_bio_size);
Lars Ellenbergdb141b22012-06-25 19:15:58 +02003179 peer = max(peer, DRBD_MAX_BIO_SIZE_SAFE);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003180 device->peer_max_bio_size = peer;
Philipp Reisner99432fc2011-05-20 16:39:13 +02003181 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003182 spin_unlock_irq(&device->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003183
3184 err:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003185 drbd_md_put_buffer(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003186
3187 return rv;
3188}
3189
3190/**
3191 * drbd_md_mark_dirty() - Mark meta data super block as dirty
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003192 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -07003193 *
3194 * Call this function if you change anything that should be written to
3195 * the meta-data super block. This function sets MD_DIRTY, and starts a
3196 * timer that ensures that within five seconds you have to call drbd_md_sync().
3197 */
Lars Ellenbergca0e6092010-10-14 15:01:21 +02003198#ifdef DEBUG
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003199void drbd_md_mark_dirty_(struct drbd_device *device, unsigned int line, const char *func)
Lars Ellenbergee15b032010-09-03 10:00:09 +02003200{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003201 if (!test_and_set_bit(MD_DIRTY, &device->flags)) {
3202 mod_timer(&device->md_sync_timer, jiffies + HZ);
3203 device->last_md_mark_dirty.line = line;
3204 device->last_md_mark_dirty.func = func;
Lars Ellenbergee15b032010-09-03 10:00:09 +02003205 }
3206}
3207#else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003208void drbd_md_mark_dirty(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003209{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003210 if (!test_and_set_bit(MD_DIRTY, &device->flags))
3211 mod_timer(&device->md_sync_timer, jiffies + 5*HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003212}
Lars Ellenbergee15b032010-09-03 10:00:09 +02003213#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -07003214
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003215void drbd_uuid_move_history(struct drbd_device *device) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003216{
3217 int i;
3218
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003219 for (i = UI_HISTORY_START; i < UI_HISTORY_END; i++)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003220 device->ldev->md.uuid[i+1] = device->ldev->md.uuid[i];
Philipp Reisnerb411b362009-09-25 16:07:19 -07003221}
3222
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003223void __drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003224{
3225 if (idx == UI_CURRENT) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003226 if (device->state.role == R_PRIMARY)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003227 val |= 1;
3228 else
3229 val &= ~((u64)1);
3230
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003231 drbd_set_ed_uuid(device, val);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003232 }
3233
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003234 device->ldev->md.uuid[idx] = val;
3235 drbd_md_mark_dirty(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003236}
3237
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003238void _drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local)
Philipp Reisner9f2247b2012-08-16 14:25:58 +02003239{
3240 unsigned long flags;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003241 spin_lock_irqsave(&device->ldev->md.uuid_lock, flags);
3242 __drbd_uuid_set(device, idx, val);
3243 spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags);
Philipp Reisner9f2247b2012-08-16 14:25:58 +02003244}
Philipp Reisnerb411b362009-09-25 16:07:19 -07003245
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003246void drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003247{
Philipp Reisner9f2247b2012-08-16 14:25:58 +02003248 unsigned long flags;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003249 spin_lock_irqsave(&device->ldev->md.uuid_lock, flags);
3250 if (device->ldev->md.uuid[idx]) {
3251 drbd_uuid_move_history(device);
3252 device->ldev->md.uuid[UI_HISTORY_START] = device->ldev->md.uuid[idx];
Philipp Reisnerb411b362009-09-25 16:07:19 -07003253 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003254 __drbd_uuid_set(device, idx, val);
3255 spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003256}
3257
3258/**
3259 * drbd_uuid_new_current() - Creates a new current UUID
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003260 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -07003261 *
3262 * Creates a new current UUID, and rotates the old current UUID into
3263 * the bitmap slot. Causes an incremental resync upon next connect.
3264 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003265void drbd_uuid_new_current(struct drbd_device *device) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003266{
3267 u64 val;
Philipp Reisner9f2247b2012-08-16 14:25:58 +02003268 unsigned long long bm_uuid;
3269
3270 get_random_bytes(&val, sizeof(u64));
3271
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003272 spin_lock_irq(&device->ldev->md.uuid_lock);
3273 bm_uuid = device->ldev->md.uuid[UI_BITMAP];
Philipp Reisnerb411b362009-09-25 16:07:19 -07003274
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003275 if (bm_uuid)
3276 dev_warn(DEV, "bm UUID was already set: %llX\n", bm_uuid);
3277
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003278 device->ldev->md.uuid[UI_BITMAP] = device->ldev->md.uuid[UI_CURRENT];
3279 __drbd_uuid_set(device, UI_CURRENT, val);
3280 spin_unlock_irq(&device->ldev->md.uuid_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003281
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003282 drbd_print_uuids(device, "new current UUID");
Lars Ellenbergaaa8e2b2010-10-15 13:16:53 +02003283 /* get it to stable storage _now_ */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003284 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003285}
3286
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003287void drbd_uuid_set_bm(struct drbd_device *device, u64 val) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003288{
Philipp Reisner9f2247b2012-08-16 14:25:58 +02003289 unsigned long flags;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003290 if (device->ldev->md.uuid[UI_BITMAP] == 0 && val == 0)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003291 return;
3292
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003293 spin_lock_irqsave(&device->ldev->md.uuid_lock, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003294 if (val == 0) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003295 drbd_uuid_move_history(device);
3296 device->ldev->md.uuid[UI_HISTORY_START] = device->ldev->md.uuid[UI_BITMAP];
3297 device->ldev->md.uuid[UI_BITMAP] = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003298 } else {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003299 unsigned long long bm_uuid = device->ldev->md.uuid[UI_BITMAP];
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003300 if (bm_uuid)
3301 dev_warn(DEV, "bm UUID was already set: %llX\n", bm_uuid);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003302
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003303 device->ldev->md.uuid[UI_BITMAP] = val & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003304 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003305 spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags);
Philipp Reisner9f2247b2012-08-16 14:25:58 +02003306
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003307 drbd_md_mark_dirty(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003308}
3309
3310/**
3311 * drbd_bmio_set_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003312 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -07003313 *
3314 * Sets all bits in the bitmap and writes the whole bitmap to stable storage.
3315 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003316int drbd_bmio_set_n_write(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003317{
3318 int rv = -EIO;
3319
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003320 if (get_ldev_if_state(device, D_ATTACHING)) {
3321 drbd_md_set_flag(device, MDF_FULL_SYNC);
3322 drbd_md_sync(device);
3323 drbd_bm_set_all(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003324
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003325 rv = drbd_bm_write(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003326
3327 if (!rv) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003328 drbd_md_clear_flag(device, MDF_FULL_SYNC);
3329 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003330 }
3331
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003332 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003333 }
3334
3335 return rv;
3336}
3337
3338/**
3339 * drbd_bmio_clear_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003340 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -07003341 *
3342 * Clears all bits in the bitmap and writes the whole bitmap to stable storage.
3343 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003344int drbd_bmio_clear_n_write(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003345{
3346 int rv = -EIO;
3347
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003348 drbd_resume_al(device);
3349 if (get_ldev_if_state(device, D_ATTACHING)) {
3350 drbd_bm_clear_all(device);
3351 rv = drbd_bm_write(device);
3352 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003353 }
3354
3355 return rv;
3356}
3357
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01003358static int w_bitmap_io(struct drbd_work *w, int unused)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003359{
3360 struct bm_io_work *work = container_of(w, struct bm_io_work, w);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003361 struct drbd_device *device = w->device;
Lars Ellenberg02851e92010-12-16 14:47:39 +01003362 int rv = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003363
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003364 D_ASSERT(atomic_read(&device->ap_bio_cnt) == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003365
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003366 if (get_ldev(device)) {
3367 drbd_bm_lock(device, work->why, work->flags);
3368 rv = work->io_fn(device);
3369 drbd_bm_unlock(device);
3370 put_ldev(device);
Lars Ellenberg02851e92010-12-16 14:47:39 +01003371 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003372
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003373 clear_bit_unlock(BITMAP_IO, &device->flags);
3374 wake_up(&device->misc_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003375
3376 if (work->done)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003377 work->done(device, rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003378
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003379 clear_bit(BITMAP_IO_QUEUED, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003380 work->why = NULL;
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003381 work->flags = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003382
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01003383 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003384}
3385
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003386void drbd_ldev_destroy(struct drbd_device *device)
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02003387{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003388 lc_destroy(device->resync);
3389 device->resync = NULL;
3390 lc_destroy(device->act_log);
3391 device->act_log = NULL;
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02003392 __no_warn(local,
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003393 drbd_free_bc(device->ldev);
3394 device->ldev = NULL;);
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02003395
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003396 clear_bit(GO_DISKLESS, &device->flags);
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02003397}
3398
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01003399static int w_go_diskless(struct drbd_work *w, int unused)
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02003400{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003401 struct drbd_device *device = w->device;
Philipp Reisner00d56942011-02-09 18:09:48 +01003402
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003403 D_ASSERT(device->state.disk == D_FAILED);
Lars Ellenberg9d282872010-10-14 13:57:07 +02003404 /* we cannot assert local_cnt == 0 here, as get_ldev_if_state will
3405 * inc/dec it frequently. Once we are D_DISKLESS, no one will touch
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02003406 * the protected members anymore, though, so once put_ldev reaches zero
3407 * again, it will be safe to free them. */
Lars Ellenberga2a3c74f2012-09-22 12:26:57 +02003408
3409 /* Try to write changed bitmap pages, read errors may have just
3410 * set some bits outside the area covered by the activity log.
3411 *
3412 * If we have an IO error during the bitmap writeout,
3413 * we will want a full sync next time, just in case.
3414 * (Do we want a specific meta data flag for this?)
3415 *
3416 * If that does not make it to stable storage either,
Philipp Reisnerfd0017c2012-10-19 14:19:23 +02003417 * we cannot do anything about that anymore.
3418 *
3419 * We still need to check if both bitmap and ldev are present, we may
3420 * end up here after a failed attach, before ldev was even assigned.
3421 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003422 if (device->bitmap && device->ldev) {
Philipp Reisnerbb451852013-03-27 14:08:39 +01003423 /* An interrupted resync or similar is allowed to recounts bits
3424 * while we detach.
3425 * Any modifications would not be expected anymore, though.
3426 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003427 if (drbd_bitmap_io_from_worker(device, drbd_bm_write,
Philipp Reisnerbb451852013-03-27 14:08:39 +01003428 "detach", BM_LOCKED_TEST_ALLOWED)) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003429 if (test_bit(WAS_READ_ERROR, &device->flags)) {
3430 drbd_md_set_flag(device, MDF_FULL_SYNC);
3431 drbd_md_sync(device);
Lars Ellenberga2a3c74f2012-09-22 12:26:57 +02003432 }
3433 }
3434 }
3435
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003436 drbd_force_state(device, NS(disk, D_DISKLESS));
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01003437 return 0;
Lars Ellenberge9e6f3e2010-09-14 20:26:27 +02003438}
3439
Philipp Reisnerb411b362009-09-25 16:07:19 -07003440/**
3441 * drbd_queue_bitmap_io() - Queues an IO operation on the whole bitmap
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003442 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -07003443 * @io_fn: IO callback to be called when bitmap IO is possible
3444 * @done: callback to be called after the bitmap IO was performed
3445 * @why: Descriptive text of the reason for doing the IO
3446 *
3447 * While IO on the bitmap happens we freeze application IO thus we ensure
3448 * that drbd_set_out_of_sync() can not be called. This function MAY ONLY be
3449 * called from worker context. It MUST NOT be used while a previous such
3450 * work is still pending!
3451 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003452void drbd_queue_bitmap_io(struct drbd_device *device,
Andreas Gruenbacher54761692011-05-30 16:15:21 +02003453 int (*io_fn)(struct drbd_device *),
3454 void (*done)(struct drbd_device *, int),
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003455 char *why, enum bm_flag flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003456{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003457 D_ASSERT(current == device->tconn->worker.task);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003458
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003459 D_ASSERT(!test_bit(BITMAP_IO_QUEUED, &device->flags));
3460 D_ASSERT(!test_bit(BITMAP_IO, &device->flags));
3461 D_ASSERT(list_empty(&device->bm_io_work.w.list));
3462 if (device->bm_io_work.why)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003463 dev_err(DEV, "FIXME going to queue '%s' but '%s' still pending?\n",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003464 why, device->bm_io_work.why);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003465
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003466 device->bm_io_work.io_fn = io_fn;
3467 device->bm_io_work.done = done;
3468 device->bm_io_work.why = why;
3469 device->bm_io_work.flags = flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003470
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003471 spin_lock_irq(&device->tconn->req_lock);
3472 set_bit(BITMAP_IO, &device->flags);
3473 if (atomic_read(&device->ap_bio_cnt) == 0) {
3474 if (!test_and_set_bit(BITMAP_IO_QUEUED, &device->flags))
3475 drbd_queue_work(&device->tconn->sender_work, &device->bm_io_work.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003476 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003477 spin_unlock_irq(&device->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003478}
3479
3480/**
3481 * drbd_bitmap_io() - Does an IO operation on the whole bitmap
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003482 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -07003483 * @io_fn: IO callback to be called when bitmap IO is possible
3484 * @why: Descriptive text of the reason for doing the IO
3485 *
3486 * freezes application IO while that the actual IO operations runs. This
3487 * functions MAY NOT be called from worker context.
3488 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003489int drbd_bitmap_io(struct drbd_device *device, int (*io_fn)(struct drbd_device *),
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003490 char *why, enum bm_flag flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003491{
3492 int rv;
3493
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003494 D_ASSERT(current != device->tconn->worker.task);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003495
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003496 if ((flags & BM_LOCKED_SET_ALLOWED) == 0)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003497 drbd_suspend_io(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003498
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003499 drbd_bm_lock(device, why, flags);
3500 rv = io_fn(device);
3501 drbd_bm_unlock(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003502
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003503 if ((flags & BM_LOCKED_SET_ALLOWED) == 0)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003504 drbd_resume_io(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003505
3506 return rv;
3507}
3508
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003509void drbd_md_set_flag(struct drbd_device *device, int flag) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003510{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003511 if ((device->ldev->md.flags & flag) != flag) {
3512 drbd_md_mark_dirty(device);
3513 device->ldev->md.flags |= flag;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003514 }
3515}
3516
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003517void drbd_md_clear_flag(struct drbd_device *device, int flag) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003518{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003519 if ((device->ldev->md.flags & flag) != 0) {
3520 drbd_md_mark_dirty(device);
3521 device->ldev->md.flags &= ~flag;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003522 }
3523}
3524int drbd_md_test_flag(struct drbd_backing_dev *bdev, int flag)
3525{
3526 return (bdev->md.flags & flag) != 0;
3527}
3528
3529static void md_sync_timer_fn(unsigned long data)
3530{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003531 struct drbd_device *device = (struct drbd_device *) data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003532
Lars Ellenbergb792b652012-08-22 14:59:06 +02003533 /* must not double-queue! */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003534 if (list_empty(&device->md_sync_work.list))
3535 drbd_queue_work_front(&device->tconn->sender_work, &device->md_sync_work);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003536}
3537
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01003538static int w_md_sync(struct drbd_work *w, int unused)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003539{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003540 struct drbd_device *device = w->device;
Philipp Reisner00d56942011-02-09 18:09:48 +01003541
Philipp Reisnerb411b362009-09-25 16:07:19 -07003542 dev_warn(DEV, "md_sync_timer expired! Worker calls drbd_md_sync().\n");
Lars Ellenbergee15b032010-09-03 10:00:09 +02003543#ifdef DEBUG
3544 dev_warn(DEV, "last md_mark_dirty: %s:%u\n",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003545 device->last_md_mark_dirty.func, device->last_md_mark_dirty.line);
Lars Ellenbergee15b032010-09-03 10:00:09 +02003546#endif
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003547 drbd_md_sync(device);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01003548 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003549}
3550
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003551const char *cmdname(enum drbd_packet cmd)
Andreas Gruenbacherf2ad9062011-01-26 17:13:25 +01003552{
3553 /* THINK may need to become several global tables
3554 * when we want to support more than
3555 * one PRO_VERSION */
3556 static const char *cmdnames[] = {
3557 [P_DATA] = "Data",
3558 [P_DATA_REPLY] = "DataReply",
3559 [P_RS_DATA_REPLY] = "RSDataReply",
3560 [P_BARRIER] = "Barrier",
3561 [P_BITMAP] = "ReportBitMap",
3562 [P_BECOME_SYNC_TARGET] = "BecomeSyncTarget",
3563 [P_BECOME_SYNC_SOURCE] = "BecomeSyncSource",
3564 [P_UNPLUG_REMOTE] = "UnplugRemote",
3565 [P_DATA_REQUEST] = "DataRequest",
3566 [P_RS_DATA_REQUEST] = "RSDataRequest",
3567 [P_SYNC_PARAM] = "SyncParam",
3568 [P_SYNC_PARAM89] = "SyncParam89",
3569 [P_PROTOCOL] = "ReportProtocol",
3570 [P_UUIDS] = "ReportUUIDs",
3571 [P_SIZES] = "ReportSizes",
3572 [P_STATE] = "ReportState",
3573 [P_SYNC_UUID] = "ReportSyncUUID",
3574 [P_AUTH_CHALLENGE] = "AuthChallenge",
3575 [P_AUTH_RESPONSE] = "AuthResponse",
3576 [P_PING] = "Ping",
3577 [P_PING_ACK] = "PingAck",
3578 [P_RECV_ACK] = "RecvAck",
3579 [P_WRITE_ACK] = "WriteAck",
3580 [P_RS_WRITE_ACK] = "RSWriteAck",
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02003581 [P_SUPERSEDED] = "Superseded",
Andreas Gruenbacherf2ad9062011-01-26 17:13:25 +01003582 [P_NEG_ACK] = "NegAck",
3583 [P_NEG_DREPLY] = "NegDReply",
3584 [P_NEG_RS_DREPLY] = "NegRSDReply",
3585 [P_BARRIER_ACK] = "BarrierAck",
3586 [P_STATE_CHG_REQ] = "StateChgRequest",
3587 [P_STATE_CHG_REPLY] = "StateChgReply",
3588 [P_OV_REQUEST] = "OVRequest",
3589 [P_OV_REPLY] = "OVReply",
3590 [P_OV_RESULT] = "OVResult",
3591 [P_CSUM_RS_REQUEST] = "CsumRSRequest",
3592 [P_RS_IS_IN_SYNC] = "CsumRSIsInSync",
3593 [P_COMPRESSED_BITMAP] = "CBitmap",
3594 [P_DELAY_PROBE] = "DelayProbe",
3595 [P_OUT_OF_SYNC] = "OutOfSync",
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01003596 [P_RETRY_WRITE] = "RetryWrite",
Lars Ellenbergae25b332011-04-24 00:01:16 +02003597 [P_RS_CANCEL] = "RSCancel",
3598 [P_CONN_ST_CHG_REQ] = "conn_st_chg_req",
3599 [P_CONN_ST_CHG_REPLY] = "conn_st_chg_reply",
Philipp Reisner036b17e2011-05-16 17:38:11 +02003600 [P_RETRY_WRITE] = "retry_write",
3601 [P_PROTOCOL_UPDATE] = "protocol_update",
Lars Ellenbergae25b332011-04-24 00:01:16 +02003602
3603 /* enum drbd_packet, but not commands - obsoleted flags:
3604 * P_MAY_IGNORE
3605 * P_MAX_OPT_CMD
3606 */
Andreas Gruenbacherf2ad9062011-01-26 17:13:25 +01003607 };
3608
Lars Ellenbergae25b332011-04-24 00:01:16 +02003609 /* too big for the array: 0xfffX */
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +02003610 if (cmd == P_INITIAL_META)
3611 return "InitialMeta";
3612 if (cmd == P_INITIAL_DATA)
3613 return "InitialData";
Andreas Gruenbacher60381782011-03-28 17:05:50 +02003614 if (cmd == P_CONNECTION_FEATURES)
3615 return "ConnectionFeatures";
Andreas Gruenbacher6e849ce2011-03-14 17:27:45 +01003616 if (cmd >= ARRAY_SIZE(cmdnames))
Andreas Gruenbacherf2ad9062011-01-26 17:13:25 +01003617 return "Unknown";
3618 return cmdnames[cmd];
3619}
3620
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01003621/**
3622 * drbd_wait_misc - wait for a request to make progress
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003623 * @device: device associated with the request
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01003624 * @i: the struct drbd_interval embedded in struct drbd_request or
3625 * struct drbd_peer_request
3626 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003627int drbd_wait_misc(struct drbd_device *device, struct drbd_interval *i)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01003628{
Philipp Reisner44ed1672011-04-19 17:10:19 +02003629 struct net_conf *nc;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01003630 DEFINE_WAIT(wait);
3631 long timeout;
3632
Philipp Reisner44ed1672011-04-19 17:10:19 +02003633 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003634 nc = rcu_dereference(device->tconn->net_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +02003635 if (!nc) {
3636 rcu_read_unlock();
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01003637 return -ETIMEDOUT;
Philipp Reisner44ed1672011-04-19 17:10:19 +02003638 }
3639 timeout = nc->ko_count ? nc->timeout * HZ / 10 * nc->ko_count : MAX_SCHEDULE_TIMEOUT;
3640 rcu_read_unlock();
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01003641
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003642 /* Indicate to wake up device->misc_wait on progress. */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01003643 i->waiting = true;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003644 prepare_to_wait(&device->misc_wait, &wait, TASK_INTERRUPTIBLE);
3645 spin_unlock_irq(&device->tconn->req_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01003646 timeout = schedule_timeout(timeout);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003647 finish_wait(&device->misc_wait, &wait);
3648 spin_lock_irq(&device->tconn->req_lock);
3649 if (!timeout || device->state.conn < C_CONNECTED)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01003650 return -ETIMEDOUT;
3651 if (signal_pending(current))
3652 return -ERESTARTSYS;
3653 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003654}
3655
3656#ifdef CONFIG_DRBD_FAULT_INJECTION
3657/* Fault insertion support including random number generator shamelessly
3658 * stolen from kernel/rcutorture.c */
3659struct fault_random_state {
3660 unsigned long state;
3661 unsigned long count;
3662};
3663
3664#define FAULT_RANDOM_MULT 39916801 /* prime */
3665#define FAULT_RANDOM_ADD 479001701 /* prime */
3666#define FAULT_RANDOM_REFRESH 10000
3667
3668/*
3669 * Crude but fast random-number generator. Uses a linear congruential
3670 * generator, with occasional help from get_random_bytes().
3671 */
3672static unsigned long
3673_drbd_fault_random(struct fault_random_state *rsp)
3674{
3675 long refresh;
3676
Roel Kluin49829ea2009-12-15 22:55:44 +01003677 if (!rsp->count--) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003678 get_random_bytes(&refresh, sizeof(refresh));
3679 rsp->state += refresh;
3680 rsp->count = FAULT_RANDOM_REFRESH;
3681 }
3682 rsp->state = rsp->state * FAULT_RANDOM_MULT + FAULT_RANDOM_ADD;
3683 return swahw32(rsp->state);
3684}
3685
3686static char *
3687_drbd_fault_str(unsigned int type) {
3688 static char *_faults[] = {
3689 [DRBD_FAULT_MD_WR] = "Meta-data write",
3690 [DRBD_FAULT_MD_RD] = "Meta-data read",
3691 [DRBD_FAULT_RS_WR] = "Resync write",
3692 [DRBD_FAULT_RS_RD] = "Resync read",
3693 [DRBD_FAULT_DT_WR] = "Data write",
3694 [DRBD_FAULT_DT_RD] = "Data read",
3695 [DRBD_FAULT_DT_RA] = "Data read ahead",
3696 [DRBD_FAULT_BM_ALLOC] = "BM allocation",
Philipp Reisner6b4388a2010-04-26 14:11:45 +02003697 [DRBD_FAULT_AL_EE] = "EE allocation",
3698 [DRBD_FAULT_RECEIVE] = "receive data corruption",
Philipp Reisnerb411b362009-09-25 16:07:19 -07003699 };
3700
3701 return (type < DRBD_FAULT_MAX) ? _faults[type] : "**Unknown**";
3702}
3703
3704unsigned int
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003705_drbd_insert_fault(struct drbd_device *device, unsigned int type)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003706{
3707 static struct fault_random_state rrs = {0, 0};
3708
3709 unsigned int ret = (
3710 (fault_devs == 0 ||
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003711 ((1 << device_to_minor(device)) & fault_devs) != 0) &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003712 (((_drbd_fault_random(&rrs) % 100) + 1) <= fault_rate));
3713
3714 if (ret) {
3715 fault_count++;
3716
Lars Ellenberg73835062010-05-27 11:51:56 +02003717 if (__ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003718 dev_warn(DEV, "***Simulating %s failure\n",
3719 _drbd_fault_str(type));
3720 }
3721
3722 return ret;
3723}
3724#endif
3725
3726const char *drbd_buildtag(void)
3727{
3728 /* DRBD built from external sources has here a reference to the
3729 git hash of the source code. */
3730
3731 static char buildtag[38] = "\0uilt-in";
3732
3733 if (buildtag[0] == 0) {
Cong Wangbc4854b2012-04-03 14:13:36 +08003734#ifdef MODULE
3735 sprintf(buildtag, "srcversion: %-24s", THIS_MODULE->srcversion);
3736#else
3737 buildtag[0] = 'b';
Philipp Reisnerb411b362009-09-25 16:07:19 -07003738#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -07003739 }
3740
3741 return buildtag;
3742}
3743
3744module_init(drbd_init)
3745module_exit(drbd_cleanup)
3746
Philipp Reisnerb411b362009-09-25 16:07:19 -07003747EXPORT_SYMBOL(drbd_conn_str);
3748EXPORT_SYMBOL(drbd_role_str);
3749EXPORT_SYMBOL(drbd_disk_str);
3750EXPORT_SYMBOL(drbd_set_st_err_str);