]>
Commit | Line | Data |
---|---|---|
b411b363 PR |
1 | /* |
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 | ||
b411b363 | 29 | #include <linux/module.h> |
b411b363 PR |
30 | #include <linux/drbd.h> |
31 | #include <asm/uaccess.h> | |
32 | #include <asm/types.h> | |
33 | #include <net/sock.h> | |
34 | #include <linux/ctype.h> | |
2a48fc0a | 35 | #include <linux/mutex.h> |
b411b363 PR |
36 | #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> | |
113fef9e | 48 | #include <linux/workqueue.h> |
b411b363 PR |
49 | #define __KERNEL_SYSCALLS__ |
50 | #include <linux/unistd.h> | |
51 | #include <linux/vmalloc.h> | |
52 | ||
53 | #include <linux/drbd_limits.h> | |
54 | #include "drbd_int.h" | |
a3603a6e | 55 | #include "drbd_protocol.h" |
b411b363 PR |
56 | #include "drbd_req.h" /* only for _req_mod in tl_release and tl_clear */ |
57 | ||
58 | #include "drbd_vli.h" | |
59 | ||
2a48fc0a | 60 | static DEFINE_MUTEX(drbd_main_mutex); |
b411b363 | 61 | static int drbd_open(struct block_device *bdev, fmode_t mode); |
db2a144b | 62 | static void drbd_release(struct gendisk *gd, fmode_t mode); |
99920dc5 | 63 | static int w_md_sync(struct drbd_work *w, int unused); |
b411b363 | 64 | static void md_sync_timer_fn(unsigned long data); |
99920dc5 | 65 | static int w_bitmap_io(struct drbd_work *w, int unused); |
b411b363 | 66 | |
b411b363 PR |
67 | MODULE_AUTHOR("Philipp Reisner <phil@linbit.com>, " |
68 | "Lars Ellenberg <lars@linbit.com>"); | |
69 | MODULE_DESCRIPTION("drbd - Distributed Replicated Block Device v" REL_VERSION); | |
70 | MODULE_VERSION(REL_VERSION); | |
71 | MODULE_LICENSE("GPL"); | |
81a5d60e | 72 | MODULE_PARM_DESC(minor_count, "Approximate number of drbd devices (" |
2b8a90b5 | 73 | __stringify(DRBD_MINOR_COUNT_MIN) "-" __stringify(DRBD_MINOR_COUNT_MAX) ")"); |
b411b363 PR |
74 | MODULE_ALIAS_BLOCKDEV_MAJOR(DRBD_MAJOR); |
75 | ||
76 | #include <linux/moduleparam.h> | |
77 | /* allow_open_on_secondary */ | |
78 | MODULE_PARM_DESC(allow_oos, "DONT USE!"); | |
79 | /* thanks to these macros, if compiled into the kernel (not-module), | |
80 | * this becomes the boot parameter drbd.minor_count */ | |
81 | module_param(minor_count, uint, 0444); | |
82 | module_param(disable_sendpage, bool, 0644); | |
83 | module_param(allow_oos, bool, 0); | |
b411b363 PR |
84 | module_param(proc_details, int, 0644); |
85 | ||
86 | #ifdef CONFIG_DRBD_FAULT_INJECTION | |
87 | int enable_faults; | |
88 | int fault_rate; | |
89 | static int fault_count; | |
90 | int fault_devs; | |
91 | /* bitmap of enabled faults */ | |
92 | module_param(enable_faults, int, 0664); | |
93 | /* fault rate % value - applies to all enabled faults */ | |
94 | module_param(fault_rate, int, 0664); | |
95 | /* count of faults inserted */ | |
96 | module_param(fault_count, int, 0664); | |
97 | /* bitmap of devices to insert faults on */ | |
98 | module_param(fault_devs, int, 0644); | |
99 | #endif | |
100 | ||
101 | /* module parameter, defined */ | |
2b8a90b5 | 102 | unsigned int minor_count = DRBD_MINOR_COUNT_DEF; |
90ab5ee9 RR |
103 | bool disable_sendpage; |
104 | bool allow_oos; | |
b411b363 PR |
105 | int proc_details; /* Detail level in proc drbd*/ |
106 | ||
107 | /* Module parameter for setting the user mode helper program | |
108 | * to run. Default is /sbin/drbdadm */ | |
109 | char usermode_helper[80] = "/sbin/drbdadm"; | |
110 | ||
111 | module_param_string(usermode_helper, usermode_helper, sizeof(usermode_helper), 0644); | |
112 | ||
113 | /* in 2.6.x, our device mapping and config info contains our virtual gendisks | |
114 | * as member "struct gendisk *vdisk;" | |
115 | */ | |
05a10ec7 | 116 | struct idr drbd_devices; |
77c556f6 | 117 | struct list_head drbd_resources; |
b411b363 PR |
118 | |
119 | struct kmem_cache *drbd_request_cache; | |
6c852bec | 120 | struct kmem_cache *drbd_ee_cache; /* peer requests */ |
b411b363 PR |
121 | struct kmem_cache *drbd_bm_ext_cache; /* bitmap extents */ |
122 | struct kmem_cache *drbd_al_ext_cache; /* activity log extents */ | |
123 | mempool_t *drbd_request_mempool; | |
124 | mempool_t *drbd_ee_mempool; | |
4281808f | 125 | mempool_t *drbd_md_io_page_pool; |
9476f39d | 126 | struct bio_set *drbd_md_io_bio_set; |
b411b363 PR |
127 | |
128 | /* I do not use a standard mempool, because: | |
129 | 1) I want to hand out the pre-allocated objects first. | |
130 | 2) I want to be able to interrupt sleeping allocation with a signal. | |
131 | Note: This is a single linked list, the next pointer is the private | |
132 | member of struct page. | |
133 | */ | |
134 | struct page *drbd_pp_pool; | |
135 | spinlock_t drbd_pp_lock; | |
136 | int drbd_pp_vacant; | |
137 | wait_queue_head_t drbd_pp_wait; | |
138 | ||
139 | DEFINE_RATELIMIT_STATE(drbd_ratelimit_state, 5 * HZ, 5); | |
140 | ||
7d4e9d09 | 141 | static const struct block_device_operations drbd_ops = { |
b411b363 PR |
142 | .owner = THIS_MODULE, |
143 | .open = drbd_open, | |
144 | .release = drbd_release, | |
145 | }; | |
19f843aa | 146 | |
da4a75d2 LE |
147 | struct bio *bio_alloc_drbd(gfp_t gfp_mask) |
148 | { | |
149 | struct bio *bio; | |
19f843aa | 150 | |
da4a75d2 LE |
151 | if (!drbd_md_io_bio_set) |
152 | return bio_alloc(gfp_mask, 1); | |
19f843aa | 153 | |
da4a75d2 LE |
154 | bio = bio_alloc_bioset(gfp_mask, 1, drbd_md_io_bio_set); |
155 | if (!bio) | |
156 | return NULL; | |
da4a75d2 | 157 | return bio; |
19f843aa LE |
158 | } |
159 | ||
b411b363 PR |
160 | #ifdef __CHECKER__ |
161 | /* When checking with sparse, and this is an inline function, sparse will | |
162 | give tons of false positives. When this is a real functions sparse works. | |
b411b363 | 163 | */ |
b30ab791 | 164 | int _get_ldev_if_state(struct drbd_device *device, enum drbd_disk_state mins) |
b411b363 | 165 | { |
b411b363 | 166 | int io_allowed; |
b411b363 | 167 | |
b30ab791 AG |
168 | atomic_inc(&device->local_cnt); |
169 | io_allowed = (device->state.disk >= mins); | |
b411b363 | 170 | if (!io_allowed) { |
b30ab791 AG |
171 | if (atomic_dec_and_test(&device->local_cnt)) |
172 | wake_up(&device->misc_wait); | |
b411b363 | 173 | } |
b411b363 PR |
174 | return io_allowed; |
175 | } | |
b411b363 | 176 | |
b411b363 | 177 | #endif |
265be2d0 | 178 | |
b411b363 | 179 | /** |
b6dd1a89 | 180 | * tl_release() - mark as BARRIER_ACKED all requests in the corresponding transfer log epoch |
bde89a9e | 181 | * @connection: DRBD connection. |
b411b363 PR |
182 | * @barrier_nr: Expected identifier of the DRBD write barrier packet. |
183 | * @set_size: Expected number of requests before that barrier. | |
184 | * | |
185 | * In case the passed barrier_nr or set_size does not match the oldest | |
b6dd1a89 LE |
186 | * epoch of not yet barrier-acked requests, this function will cause a |
187 | * termination of the connection. | |
b411b363 | 188 | */ |
bde89a9e | 189 | void tl_release(struct drbd_connection *connection, unsigned int barrier_nr, |
2f5cdd0b | 190 | unsigned int set_size) |
b411b363 | 191 | { |
b411b363 | 192 | struct drbd_request *r; |
b6dd1a89 LE |
193 | struct drbd_request *req = NULL; |
194 | int expect_epoch = 0; | |
195 | int expect_size = 0; | |
b411b363 | 196 | |
0500813f | 197 | spin_lock_irq(&connection->resource->req_lock); |
b411b363 | 198 | |
98683650 | 199 | /* find oldest not yet barrier-acked write request, |
b6dd1a89 | 200 | * count writes in its epoch. */ |
bde89a9e | 201 | list_for_each_entry(r, &connection->transfer_log, tl_requests) { |
a0d856df | 202 | const unsigned s = r->rq_state; |
b6dd1a89 LE |
203 | if (!req) { |
204 | if (!(s & RQ_WRITE)) | |
205 | continue; | |
206 | if (!(s & RQ_NET_MASK)) | |
207 | continue; | |
208 | if (s & RQ_NET_DONE) | |
209 | continue; | |
210 | req = r; | |
211 | expect_epoch = req->epoch; | |
212 | expect_size ++; | |
213 | } else { | |
214 | if (r->epoch != expect_epoch) | |
215 | break; | |
216 | if (!(s & RQ_WRITE)) | |
217 | continue; | |
218 | /* if (s & RQ_DONE): not expected */ | |
219 | /* if (!(s & RQ_NET_MASK)): not expected */ | |
220 | expect_size++; | |
43a5182c | 221 | } |
b411b363 | 222 | } |
67098930 | 223 | |
b411b363 | 224 | /* first some paranoia code */ |
b6dd1a89 | 225 | if (req == NULL) { |
1ec861eb | 226 | drbd_err(connection, "BAD! BarrierAck #%u received, but no epoch in tl!?\n", |
2f5cdd0b | 227 | barrier_nr); |
b411b363 | 228 | goto bail; |
b411b363 | 229 | } |
b6dd1a89 | 230 | if (expect_epoch != barrier_nr) { |
1ec861eb | 231 | drbd_err(connection, "BAD! BarrierAck #%u received, expected #%u!\n", |
b6dd1a89 | 232 | barrier_nr, expect_epoch); |
b411b363 | 233 | goto bail; |
5a22db89 LE |
234 | } |
235 | ||
b6dd1a89 | 236 | if (expect_size != set_size) { |
1ec861eb | 237 | drbd_err(connection, "BAD! BarrierAck #%u received with n_writes=%u, expected n_writes=%u!\n", |
b6dd1a89 | 238 | barrier_nr, set_size, expect_size); |
b411b363 | 239 | goto bail; |
b411b363 PR |
240 | } |
241 | ||
98683650 PR |
242 | /* Clean up list of requests processed during current epoch. */ |
243 | /* this extra list walk restart is paranoia, | |
244 | * to catch requests being barrier-acked "unexpectedly". | |
245 | * It usually should find the same req again, or some READ preceding it. */ | |
bde89a9e | 246 | list_for_each_entry(req, &connection->transfer_log, tl_requests) |
98683650 PR |
247 | if (req->epoch == expect_epoch) |
248 | break; | |
bde89a9e | 249 | list_for_each_entry_safe_from(req, r, &connection->transfer_log, tl_requests) { |
b6dd1a89 LE |
250 | if (req->epoch != expect_epoch) |
251 | break; | |
252 | _req_mod(req, BARRIER_ACKED); | |
19f843aa | 253 | } |
0500813f | 254 | spin_unlock_irq(&connection->resource->req_lock); |
19f843aa | 255 | |
b411b363 | 256 | return; |
b411b363 | 257 | |
b411b363 | 258 | bail: |
0500813f | 259 | spin_unlock_irq(&connection->resource->req_lock); |
bde89a9e | 260 | conn_request_state(connection, NS(conn, C_PROTOCOL_ERROR), CS_HARD); |
b411b363 | 261 | } |
b411b363 | 262 | |
b411b363 | 263 | |
b411b363 | 264 | /** |
11b58e73 | 265 | * _tl_restart() - Walks the transfer log, and applies an action to all requests |
b30ab791 | 266 | * @device: DRBD device. |
11b58e73 | 267 | * @what: The action/event to perform with all request objects |
b411b363 | 268 | * |
8554df1c AG |
269 | * @what might be one of CONNECTION_LOST_WHILE_PENDING, RESEND, FAIL_FROZEN_DISK_IO, |
270 | * RESTART_FROZEN_DISK_IO. | |
b411b363 | 271 | */ |
b6dd1a89 | 272 | /* must hold resource->req_lock */ |
bde89a9e | 273 | void _tl_restart(struct drbd_connection *connection, enum drbd_req_event what) |
b411b363 | 274 | { |
b6dd1a89 | 275 | struct drbd_request *req, *r; |
b411b363 | 276 | |
bde89a9e | 277 | list_for_each_entry_safe(req, r, &connection->transfer_log, tl_requests) |
b6dd1a89 LE |
278 | _req_mod(req, what); |
279 | } | |
738a84b2 | 280 | |
bde89a9e | 281 | void tl_restart(struct drbd_connection *connection, enum drbd_req_event what) |
b6dd1a89 | 282 | { |
0500813f | 283 | spin_lock_irq(&connection->resource->req_lock); |
bde89a9e | 284 | _tl_restart(connection, what); |
0500813f | 285 | spin_unlock_irq(&connection->resource->req_lock); |
cdfda633 | 286 | } |
b411b363 | 287 | |
b411b363 PR |
288 | /** |
289 | * tl_clear() - Clears all requests and &struct drbd_tl_epoch objects out of the TL | |
b30ab791 | 290 | * @device: DRBD device. |
b411b363 PR |
291 | * |
292 | * This is called after the connection to the peer was lost. The storage covered | |
293 | * by the requests on the transfer gets marked as our of sync. Called from the | |
294 | * receiver thread and the worker thread. | |
295 | */ | |
bde89a9e | 296 | void tl_clear(struct drbd_connection *connection) |
b411b363 | 297 | { |
bde89a9e | 298 | tl_restart(connection, CONNECTION_LOST_WHILE_PENDING); |
b411b363 | 299 | } |
197296ff | 300 | |
cdfda633 | 301 | /** |
b30ab791 AG |
302 | * tl_abort_disk_io() - Abort disk I/O for all requests for a certain device in the TL |
303 | * @device: DRBD device. | |
cdfda633 | 304 | */ |
b30ab791 | 305 | void tl_abort_disk_io(struct drbd_device *device) |
cdfda633 | 306 | { |
a6b32bc3 | 307 | struct drbd_connection *connection = first_peer_device(device)->connection; |
b6dd1a89 | 308 | struct drbd_request *req, *r; |
02851e9f | 309 | |
0500813f | 310 | spin_lock_irq(&connection->resource->req_lock); |
bde89a9e | 311 | list_for_each_entry_safe(req, r, &connection->transfer_log, tl_requests) { |
97ddb687 LE |
312 | if (!(req->rq_state & RQ_LOCAL_PENDING)) |
313 | continue; | |
84b8c06b | 314 | if (req->device != device) |
b6dd1a89 LE |
315 | continue; |
316 | _req_mod(req, ABORT_DISK_IO); | |
b411b363 | 317 | } |
0500813f | 318 | spin_unlock_irq(&connection->resource->req_lock); |
b411b363 PR |
319 | } |
320 | ||
b411b363 PR |
321 | static int drbd_thread_setup(void *arg) |
322 | { | |
323 | struct drbd_thread *thi = (struct drbd_thread *) arg; | |
2457b6d5 | 324 | struct drbd_resource *resource = thi->resource; |
b411b363 PR |
325 | unsigned long flags; |
326 | int retval; | |
327 | ||
f1b3a6ec | 328 | snprintf(current->comm, sizeof(current->comm), "drbd_%c_%s", |
77c556f6 | 329 | thi->name[0], |
2457b6d5 | 330 | resource->name); |
f1b3a6ec | 331 | |
b411b363 PR |
332 | restart: |
333 | retval = thi->function(thi); | |
334 | ||
335 | spin_lock_irqsave(&thi->t_lock, flags); | |
336 | ||
e77a0a5c | 337 | /* if the receiver has been "EXITING", the last thing it did |
b411b363 PR |
338 | * was set the conn state to "StandAlone", |
339 | * if now a re-connect request comes in, conn state goes C_UNCONNECTED, | |
340 | * and receiver thread will be "started". | |
e77a0a5c | 341 | * drbd_thread_start needs to set "RESTARTING" in that case. |
b411b363 | 342 | * t_state check and assignment needs to be within the same spinlock, |
e77a0a5c AG |
343 | * so either thread_start sees EXITING, and can remap to RESTARTING, |
344 | * or thread_start see NONE, and can proceed as normal. | |
b411b363 PR |
345 | */ |
346 | ||
e77a0a5c | 347 | if (thi->t_state == RESTARTING) { |
2457b6d5 | 348 | drbd_info(resource, "Restarting %s thread\n", thi->name); |
e77a0a5c | 349 | thi->t_state = RUNNING; |
b411b363 PR |
350 | spin_unlock_irqrestore(&thi->t_lock, flags); |
351 | goto restart; | |
352 | } | |
353 | ||
354 | thi->task = NULL; | |
e77a0a5c | 355 | thi->t_state = NONE; |
b411b363 | 356 | smp_mb(); |
992d6e91 | 357 | complete_all(&thi->stop); |
b411b363 PR |
358 | spin_unlock_irqrestore(&thi->t_lock, flags); |
359 | ||
2457b6d5 | 360 | drbd_info(resource, "Terminating %s\n", current->comm); |
b411b363 PR |
361 | |
362 | /* Release mod reference taken when thread was started */ | |
9dc9fbb3 | 363 | |
2457b6d5 AG |
364 | if (thi->connection) |
365 | kref_put(&thi->connection->kref, drbd_destroy_connection); | |
366 | kref_put(&resource->kref, drbd_destroy_resource); | |
b411b363 PR |
367 | module_put(THIS_MODULE); |
368 | return retval; | |
369 | } | |
370 | ||
2457b6d5 | 371 | static void drbd_thread_init(struct drbd_resource *resource, struct drbd_thread *thi, |
c60b0251 | 372 | int (*func) (struct drbd_thread *), const char *name) |
b411b363 PR |
373 | { |
374 | spin_lock_init(&thi->t_lock); | |
375 | thi->task = NULL; | |
e77a0a5c | 376 | thi->t_state = NONE; |
b411b363 | 377 | thi->function = func; |
2457b6d5 AG |
378 | thi->resource = resource; |
379 | thi->connection = NULL; | |
c60b0251 | 380 | thi->name = name; |
b411b363 PR |
381 | } |
382 | ||
383 | int drbd_thread_start(struct drbd_thread *thi) | |
384 | { | |
2457b6d5 | 385 | struct drbd_resource *resource = thi->resource; |
b411b363 PR |
386 | struct task_struct *nt; |
387 | unsigned long flags; | |
388 | ||
b411b363 PR |
389 | /* 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) { | |
e77a0a5c | 394 | case NONE: |
2457b6d5 | 395 | drbd_info(resource, "Starting %s thread (from %s [%d])\n", |
bed879ae | 396 | thi->name, current->comm, current->pid); |
b411b363 PR |
397 | |
398 | /* Get ref on module for thread - this is released when thread exits */ | |
399 | if (!try_module_get(THIS_MODULE)) { | |
2457b6d5 | 400 | drbd_err(resource, "Failed to get module reference in drbd_thread_start\n"); |
b411b363 | 401 | spin_unlock_irqrestore(&thi->t_lock, flags); |
81e84650 | 402 | return false; |
b411b363 PR |
403 | } |
404 | ||
2457b6d5 AG |
405 | kref_get(&resource->kref); |
406 | if (thi->connection) | |
407 | kref_get(&thi->connection->kref); | |
9dc9fbb3 | 408 | |
b411b363 | 409 | init_completion(&thi->stop); |
b411b363 | 410 | thi->reset_cpu_mask = 1; |
e77a0a5c | 411 | thi->t_state = RUNNING; |
b411b363 PR |
412 | spin_unlock_irqrestore(&thi->t_lock, flags); |
413 | flush_signals(current); /* otherw. may get -ERESTARTNOINTR */ | |
414 | ||
415 | nt = kthread_create(drbd_thread_setup, (void *) thi, | |
2457b6d5 | 416 | "drbd_%c_%s", thi->name[0], thi->resource->name); |
b411b363 PR |
417 | |
418 | if (IS_ERR(nt)) { | |
2457b6d5 | 419 | drbd_err(resource, "Couldn't start thread\n"); |
b411b363 | 420 | |
2457b6d5 AG |
421 | if (thi->connection) |
422 | kref_put(&thi->connection->kref, drbd_destroy_connection); | |
423 | kref_put(&resource->kref, drbd_destroy_resource); | |
b411b363 | 424 | module_put(THIS_MODULE); |
81e84650 | 425 | return false; |
b411b363 PR |
426 | } |
427 | spin_lock_irqsave(&thi->t_lock, flags); | |
428 | thi->task = nt; | |
e77a0a5c | 429 | thi->t_state = RUNNING; |
b411b363 PR |
430 | spin_unlock_irqrestore(&thi->t_lock, flags); |
431 | wake_up_process(nt); | |
432 | break; | |
e77a0a5c AG |
433 | case EXITING: |
434 | thi->t_state = RESTARTING; | |
2457b6d5 | 435 | drbd_info(resource, "Restarting %s thread (from %s [%d])\n", |
bed879ae | 436 | thi->name, current->comm, current->pid); |
b411b363 | 437 | /* fall through */ |
e77a0a5c AG |
438 | case RUNNING: |
439 | case RESTARTING: | |
b411b363 PR |
440 | default: |
441 | spin_unlock_irqrestore(&thi->t_lock, flags); | |
442 | break; | |
443 | } | |
444 | ||
81e84650 | 445 | return true; |
b411b363 PR |
446 | } |
447 | ||
448 | ||
449 | void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait) | |
450 | { | |
451 | unsigned long flags; | |
452 | ||
e77a0a5c | 453 | enum drbd_thread_state ns = restart ? RESTARTING : EXITING; |
b411b363 PR |
454 | |
455 | /* may be called from state engine, holding the req lock irqsave */ | |
456 | spin_lock_irqsave(&thi->t_lock, flags); | |
457 | ||
e77a0a5c | 458 | if (thi->t_state == NONE) { |
b411b363 PR |
459 | spin_unlock_irqrestore(&thi->t_lock, flags); |
460 | if (restart) | |
461 | drbd_thread_start(thi); | |
462 | return; | |
463 | } | |
464 | ||
465 | if (thi->t_state != ns) { | |
466 | if (thi->task == NULL) { | |
467 | spin_unlock_irqrestore(&thi->t_lock, flags); | |
468 | return; | |
469 | } | |
470 | ||
471 | thi->t_state = ns; | |
472 | smp_mb(); | |
473 | init_completion(&thi->stop); | |
474 | if (thi->task != current) | |
475 | force_sig(DRBD_SIGKILL, thi->task); | |
b411b363 PR |
476 | } |
477 | ||
478 | spin_unlock_irqrestore(&thi->t_lock, flags); | |
479 | ||
480 | if (wait) | |
481 | wait_for_completion(&thi->stop); | |
482 | } | |
483 | ||
bde89a9e | 484 | int conn_lowest_minor(struct drbd_connection *connection) |
80822284 | 485 | { |
c06ece6b AG |
486 | struct drbd_peer_device *peer_device; |
487 | int vnr = 0, minor = -1; | |
774b3055 | 488 | |
695d08fa | 489 | rcu_read_lock(); |
c06ece6b AG |
490 | peer_device = idr_get_next(&connection->peer_devices, &vnr); |
491 | if (peer_device) | |
492 | minor = device_to_minor(peer_device->device); | |
695d08fa PR |
493 | rcu_read_unlock(); |
494 | ||
c06ece6b | 495 | return minor; |
80822284 | 496 | } |
774b3055 | 497 | |
b411b363 PR |
498 | #ifdef CONFIG_SMP |
499 | /** | |
500 | * drbd_calc_cpu_mask() - Generate CPU masks, spread over all CPUs | |
b411b363 | 501 | * |
625a6ba2 | 502 | * Forces all threads of a resource onto the same CPU. This is beneficial for |
b411b363 PR |
503 | * DRBD's performance. May be overwritten by user's configuration. |
504 | */ | |
625a6ba2 | 505 | static void drbd_calc_cpu_mask(cpumask_var_t *cpu_mask) |
b411b363 | 506 | { |
625a6ba2 | 507 | unsigned int *resources_per_cpu, min_index = ~0; |
b411b363 | 508 | |
625a6ba2 AG |
509 | resources_per_cpu = kzalloc(nr_cpu_ids * sizeof(*resources_per_cpu), GFP_KERNEL); |
510 | if (resources_per_cpu) { | |
511 | struct drbd_resource *resource; | |
512 | unsigned int cpu, min = ~0; | |
b411b363 | 513 | |
625a6ba2 AG |
514 | rcu_read_lock(); |
515 | for_each_resource_rcu(resource, &drbd_resources) { | |
516 | for_each_cpu(cpu, resource->cpu_mask) | |
517 | resources_per_cpu[cpu]++; | |
b411b363 | 518 | } |
625a6ba2 AG |
519 | rcu_read_unlock(); |
520 | for_each_online_cpu(cpu) { | |
521 | if (resources_per_cpu[cpu] < min) { | |
522 | min = resources_per_cpu[cpu]; | |
523 | min_index = cpu; | |
524 | } | |
525 | } | |
526 | kfree(resources_per_cpu); | |
527 | } | |
528 | if (min_index == ~0) { | |
529 | cpumask_setall(*cpu_mask); | |
530 | return; | |
b411b363 | 531 | } |
625a6ba2 | 532 | cpumask_set_cpu(min_index, *cpu_mask); |
b411b363 PR |
533 | } |
534 | ||
535 | /** | |
536 | * drbd_thread_current_set_cpu() - modifies the cpu mask of the _current_ thread | |
b30ab791 | 537 | * @device: DRBD device. |
bc31fe33 | 538 | * @thi: drbd_thread object |
b411b363 PR |
539 | * |
540 | * call in the "main loop" of _all_ threads, no need for any mutex, current won't die | |
541 | * prematurely. | |
542 | */ | |
80822284 | 543 | void drbd_thread_current_set_cpu(struct drbd_thread *thi) |
b411b363 | 544 | { |
2457b6d5 | 545 | struct drbd_resource *resource = thi->resource; |
b411b363 | 546 | struct task_struct *p = current; |
bed879ae | 547 | |
b411b363 PR |
548 | if (!thi->reset_cpu_mask) |
549 | return; | |
550 | thi->reset_cpu_mask = 0; | |
2457b6d5 | 551 | set_cpus_allowed_ptr(p, resource->cpu_mask); |
b411b363 | 552 | } |
625a6ba2 AG |
553 | #else |
554 | #define drbd_calc_cpu_mask(A) ({}) | |
b411b363 PR |
555 | #endif |
556 | ||
52b061a4 AG |
557 | /** |
558 | * drbd_header_size - size of a packet header | |
559 | * | |
560 | * The header size is a multiple of 8, so any payload following the header is | |
561 | * word aligned on 64-bit architectures. (The bitmap send and receive code | |
562 | * relies on this.) | |
563 | */ | |
bde89a9e | 564 | unsigned int drbd_header_size(struct drbd_connection *connection) |
b411b363 | 565 | { |
bde89a9e | 566 | if (connection->agreed_pro_version >= 100) { |
0c8e36d9 AG |
567 | BUILD_BUG_ON(!IS_ALIGNED(sizeof(struct p_header100), 8)); |
568 | return sizeof(struct p_header100); | |
569 | } else { | |
570 | BUILD_BUG_ON(sizeof(struct p_header80) != | |
571 | sizeof(struct p_header95)); | |
572 | BUILD_BUG_ON(!IS_ALIGNED(sizeof(struct p_header80), 8)); | |
573 | return sizeof(struct p_header80); | |
574 | } | |
52b061a4 | 575 | } |
b411b363 | 576 | |
e658983a | 577 | static unsigned int prepare_header80(struct p_header80 *h, enum drbd_packet cmd, int size) |
fd340c12 PR |
578 | { |
579 | h->magic = cpu_to_be32(DRBD_MAGIC); | |
580 | h->command = cpu_to_be16(cmd); | |
581 | h->length = cpu_to_be16(size); | |
e658983a | 582 | return sizeof(struct p_header80); |
fd340c12 | 583 | } |
b411b363 | 584 | |
e658983a | 585 | static unsigned int prepare_header95(struct p_header95 *h, enum drbd_packet cmd, int size) |
fd340c12 PR |
586 | { |
587 | h->magic = cpu_to_be16(DRBD_MAGIC_BIG); | |
b411b363 | 588 | h->command = cpu_to_be16(cmd); |
b55d84ba | 589 | h->length = cpu_to_be32(size); |
e658983a | 590 | return sizeof(struct p_header95); |
fd340c12 | 591 | } |
b411b363 | 592 | |
0c8e36d9 AG |
593 | static unsigned int prepare_header100(struct p_header100 *h, enum drbd_packet cmd, |
594 | int size, int vnr) | |
595 | { | |
596 | h->magic = cpu_to_be32(DRBD_MAGIC_100); | |
597 | h->volume = cpu_to_be16(vnr); | |
598 | h->command = cpu_to_be16(cmd); | |
599 | h->length = cpu_to_be32(size); | |
600 | h->pad = 0; | |
601 | return sizeof(struct p_header100); | |
602 | } | |
b411b363 | 603 | |
bde89a9e | 604 | static unsigned int prepare_header(struct drbd_connection *connection, int vnr, |
0c8e36d9 | 605 | void *buffer, enum drbd_packet cmd, int size) |
d38e787e | 606 | { |
bde89a9e | 607 | if (connection->agreed_pro_version >= 100) |
0c8e36d9 | 608 | return prepare_header100(buffer, cmd, size, vnr); |
bde89a9e | 609 | else if (connection->agreed_pro_version >= 95 && |
0c8e36d9 | 610 | size > DRBD_MAX_SIZE_H80_PACKET) |
e658983a | 611 | return prepare_header95(buffer, cmd, size); |
d38e787e | 612 | else |
e658983a | 613 | return prepare_header80(buffer, cmd, size); |
b411b363 PR |
614 | } |
615 | ||
bde89a9e | 616 | static void *__conn_prepare_command(struct drbd_connection *connection, |
a7eb7bdf | 617 | struct drbd_socket *sock) |
b411b363 | 618 | { |
a7eb7bdf AG |
619 | if (!sock->socket) |
620 | return NULL; | |
bde89a9e | 621 | return sock->sbuf + drbd_header_size(connection); |
a7eb7bdf | 622 | } |
b411b363 | 623 | |
bde89a9e | 624 | void *conn_prepare_command(struct drbd_connection *connection, struct drbd_socket *sock) |
dba58587 | 625 | { |
a7eb7bdf | 626 | void *p; |
b411b363 | 627 | |
dba58587 | 628 | mutex_lock(&sock->mutex); |
bde89a9e | 629 | p = __conn_prepare_command(connection, sock); |
a7eb7bdf | 630 | if (!p) |
dba58587 | 631 | mutex_unlock(&sock->mutex); |
b411b363 | 632 | |
a7eb7bdf | 633 | return p; |
b411b363 PR |
634 | } |
635 | ||
69a22773 | 636 | void *drbd_prepare_command(struct drbd_peer_device *peer_device, struct drbd_socket *sock) |
b411b363 | 637 | { |
69a22773 | 638 | return conn_prepare_command(peer_device->connection, sock); |
dba58587 | 639 | } |
b411b363 | 640 | |
bde89a9e | 641 | static int __send_command(struct drbd_connection *connection, int vnr, |
dba58587 AG |
642 | struct drbd_socket *sock, enum drbd_packet cmd, |
643 | unsigned int header_size, void *data, | |
644 | unsigned int size) | |
645 | { | |
646 | int msg_flags; | |
647 | int err; | |
b411b363 | 648 | |
dba58587 AG |
649 | /* |
650 | * Called with @data == NULL and the size of the data blocks in @size | |
651 | * for commands that send data blocks. For those commands, omit the | |
652 | * MSG_MORE flag: this will increase the likelihood that data blocks | |
653 | * which are page aligned on the sender will end up page aligned on the | |
654 | * receiver. | |
655 | */ | |
656 | msg_flags = data ? MSG_MORE : 0; | |
657 | ||
bde89a9e | 658 | header_size += prepare_header(connection, vnr, sock->sbuf, cmd, |
e658983a | 659 | header_size + size); |
bde89a9e | 660 | err = drbd_send_all(connection, sock->socket, sock->sbuf, header_size, |
dba58587 AG |
661 | msg_flags); |
662 | if (data && !err) | |
bde89a9e | 663 | err = drbd_send_all(connection, sock->socket, data, size, 0); |
123ff122 LE |
664 | /* DRBD protocol "pings" are latency critical. |
665 | * This is supposed to trigger tcp_push_pending_frames() */ | |
666 | if (!err && (cmd == P_PING || cmd == P_PING_ACK)) | |
667 | drbd_tcp_nodelay(sock->socket); | |
668 | ||
dba58587 AG |
669 | return err; |
670 | } | |
671 | ||
bde89a9e | 672 | static int __conn_send_command(struct drbd_connection *connection, struct drbd_socket *sock, |
a7eb7bdf AG |
673 | enum drbd_packet cmd, unsigned int header_size, |
674 | void *data, unsigned int size) | |
675 | { | |
bde89a9e | 676 | return __send_command(connection, 0, sock, cmd, header_size, data, size); |
a7eb7bdf AG |
677 | } |
678 | ||
bde89a9e | 679 | int conn_send_command(struct drbd_connection *connection, struct drbd_socket *sock, |
dba58587 AG |
680 | enum drbd_packet cmd, unsigned int header_size, |
681 | void *data, unsigned int size) | |
682 | { | |
683 | int err; | |
b411b363 | 684 | |
bde89a9e | 685 | err = __conn_send_command(connection, sock, cmd, header_size, data, size); |
dba58587 AG |
686 | mutex_unlock(&sock->mutex); |
687 | return err; | |
688 | } | |
689 | ||
69a22773 | 690 | int drbd_send_command(struct drbd_peer_device *peer_device, struct drbd_socket *sock, |
dba58587 AG |
691 | enum drbd_packet cmd, unsigned int header_size, |
692 | void *data, unsigned int size) | |
693 | { | |
694 | int err; | |
695 | ||
69a22773 AG |
696 | err = __send_command(peer_device->connection, peer_device->device->vnr, |
697 | sock, cmd, header_size, data, size); | |
dba58587 AG |
698 | mutex_unlock(&sock->mutex); |
699 | return err; | |
700 | } | |
b411b363 | 701 | |
bde89a9e | 702 | int drbd_send_ping(struct drbd_connection *connection) |
e307f352 | 703 | { |
9f5bdc33 AG |
704 | struct drbd_socket *sock; |
705 | ||
bde89a9e AG |
706 | sock = &connection->meta; |
707 | if (!conn_prepare_command(connection, sock)) | |
9f5bdc33 | 708 | return -EIO; |
bde89a9e | 709 | return conn_send_command(connection, sock, P_PING, 0, NULL, 0); |
e307f352 | 710 | } |
b411b363 | 711 | |
bde89a9e | 712 | int drbd_send_ping_ack(struct drbd_connection *connection) |
e307f352 | 713 | { |
9f5bdc33 AG |
714 | struct drbd_socket *sock; |
715 | ||
bde89a9e AG |
716 | sock = &connection->meta; |
717 | if (!conn_prepare_command(connection, sock)) | |
9f5bdc33 | 718 | return -EIO; |
bde89a9e | 719 | return conn_send_command(connection, sock, P_PING_ACK, 0, NULL, 0); |
b411b363 PR |
720 | } |
721 | ||
69a22773 | 722 | int drbd_send_sync_param(struct drbd_peer_device *peer_device) |
b411b363 | 723 | { |
7c96715a | 724 | struct drbd_socket *sock; |
8e26f9cc | 725 | struct p_rs_param_95 *p; |
9f5bdc33 | 726 | int size; |
69a22773 | 727 | const int apv = peer_device->connection->agreed_pro_version; |
9f5bdc33 | 728 | enum drbd_packet cmd; |
44ed167d | 729 | struct net_conf *nc; |
daeda1cc | 730 | struct disk_conf *dc; |
9f5bdc33 | 731 | |
69a22773 AG |
732 | sock = &peer_device->connection->data; |
733 | p = drbd_prepare_command(peer_device, sock); | |
9f5bdc33 AG |
734 | if (!p) |
735 | return -EIO; | |
b411b363 | 736 | |
44ed167d | 737 | rcu_read_lock(); |
69a22773 | 738 | nc = rcu_dereference(peer_device->connection->net_conf); |
b411b363 PR |
739 | |
740 | size = apv <= 87 ? sizeof(struct p_rs_param) | |
741 | : apv == 88 ? sizeof(struct p_rs_param) | |
44ed167d | 742 | + strlen(nc->verify_alg) + 1 |
8e26f9cc PR |
743 | : apv <= 94 ? sizeof(struct p_rs_param_89) |
744 | : /* apv >= 95 */ sizeof(struct p_rs_param_95); | |
b411b363 | 745 | |
9f5bdc33 | 746 | cmd = apv >= 89 ? P_SYNC_PARAM89 : P_SYNC_PARAM; |
b411b363 | 747 | |
9f5bdc33 AG |
748 | /* initialize verify_alg and csums_alg */ |
749 | memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX); | |
b411b363 | 750 | |
69a22773 AG |
751 | if (get_ldev(peer_device->device)) { |
752 | dc = rcu_dereference(peer_device->device->ldev->disk_conf); | |
6394b935 | 753 | p->resync_rate = cpu_to_be32(dc->resync_rate); |
daeda1cc PR |
754 | p->c_plan_ahead = cpu_to_be32(dc->c_plan_ahead); |
755 | p->c_delay_target = cpu_to_be32(dc->c_delay_target); | |
756 | p->c_fill_target = cpu_to_be32(dc->c_fill_target); | |
757 | p->c_max_rate = cpu_to_be32(dc->c_max_rate); | |
69a22773 | 758 | put_ldev(peer_device->device); |
9f5bdc33 | 759 | } else { |
6394b935 | 760 | p->resync_rate = cpu_to_be32(DRBD_RESYNC_RATE_DEF); |
9f5bdc33 AG |
761 | p->c_plan_ahead = cpu_to_be32(DRBD_C_PLAN_AHEAD_DEF); |
762 | p->c_delay_target = cpu_to_be32(DRBD_C_DELAY_TARGET_DEF); | |
763 | p->c_fill_target = cpu_to_be32(DRBD_C_FILL_TARGET_DEF); | |
764 | p->c_max_rate = cpu_to_be32(DRBD_C_MAX_RATE_DEF); | |
765 | } | |
b411b363 | 766 | |
9f5bdc33 | 767 | if (apv >= 88) |
44ed167d | 768 | strcpy(p->verify_alg, nc->verify_alg); |
9f5bdc33 | 769 | if (apv >= 89) |
44ed167d PR |
770 | strcpy(p->csums_alg, nc->csums_alg); |
771 | rcu_read_unlock(); | |
b411b363 | 772 | |
69a22773 | 773 | return drbd_send_command(peer_device, sock, cmd, size, NULL, 0); |
b411b363 PR |
774 | } |
775 | ||
bde89a9e | 776 | int __drbd_send_protocol(struct drbd_connection *connection, enum drbd_packet cmd) |
b411b363 | 777 | { |
9f5bdc33 | 778 | struct drbd_socket *sock; |
b411b363 | 779 | struct p_protocol *p; |
44ed167d | 780 | struct net_conf *nc; |
9f5bdc33 | 781 | int size, cf; |
b411b363 | 782 | |
bde89a9e AG |
783 | sock = &connection->data; |
784 | p = __conn_prepare_command(connection, sock); | |
9f5bdc33 AG |
785 | if (!p) |
786 | return -EIO; | |
b411b363 | 787 | |
44ed167d | 788 | rcu_read_lock(); |
bde89a9e | 789 | nc = rcu_dereference(connection->net_conf); |
b411b363 | 790 | |
bde89a9e | 791 | if (nc->tentative && connection->agreed_pro_version < 92) { |
44ed167d PR |
792 | rcu_read_unlock(); |
793 | mutex_unlock(&sock->mutex); | |
1ec861eb | 794 | drbd_err(connection, "--dry-run is not supported by peer"); |
44ed167d PR |
795 | return -EOPNOTSUPP; |
796 | } | |
b411b363 | 797 | |
9f5bdc33 | 798 | size = sizeof(*p); |
bde89a9e | 799 | if (connection->agreed_pro_version >= 87) |
44ed167d | 800 | size += strlen(nc->integrity_alg) + 1; |
b411b363 | 801 | |
44ed167d PR |
802 | p->protocol = cpu_to_be32(nc->wire_protocol); |
803 | p->after_sb_0p = cpu_to_be32(nc->after_sb_0p); | |
804 | p->after_sb_1p = cpu_to_be32(nc->after_sb_1p); | |
805 | p->after_sb_2p = cpu_to_be32(nc->after_sb_2p); | |
806 | p->two_primaries = cpu_to_be32(nc->two_primaries); | |
cf14c2e9 | 807 | cf = 0; |
6139f60d AG |
808 | if (nc->discard_my_data) |
809 | cf |= CF_DISCARD_MY_DATA; | |
6dff2902 | 810 | if (nc->tentative) |
9f5bdc33 | 811 | cf |= CF_DRY_RUN; |
cf14c2e9 PR |
812 | p->conn_flags = cpu_to_be32(cf); |
813 | ||
bde89a9e | 814 | if (connection->agreed_pro_version >= 87) |
44ed167d PR |
815 | strcpy(p->integrity_alg, nc->integrity_alg); |
816 | rcu_read_unlock(); | |
b411b363 | 817 | |
bde89a9e | 818 | return __conn_send_command(connection, sock, cmd, size, NULL, 0); |
a7eb7bdf AG |
819 | } |
820 | ||
bde89a9e | 821 | int drbd_send_protocol(struct drbd_connection *connection) |
a7eb7bdf AG |
822 | { |
823 | int err; | |
824 | ||
bde89a9e AG |
825 | mutex_lock(&connection->data.mutex); |
826 | err = __drbd_send_protocol(connection, P_PROTOCOL); | |
827 | mutex_unlock(&connection->data.mutex); | |
a7eb7bdf AG |
828 | |
829 | return err; | |
b411b363 PR |
830 | } |
831 | ||
69a22773 | 832 | static int _drbd_send_uuids(struct drbd_peer_device *peer_device, u64 uuid_flags) |
b411b363 | 833 | { |
69a22773 | 834 | struct drbd_device *device = peer_device->device; |
9f5bdc33 AG |
835 | struct drbd_socket *sock; |
836 | struct p_uuids *p; | |
b411b363 PR |
837 | int i; |
838 | ||
b30ab791 | 839 | if (!get_ldev_if_state(device, D_NEGOTIATING)) |
2ae5f95b | 840 | return 0; |
b411b363 | 841 | |
69a22773 AG |
842 | sock = &peer_device->connection->data; |
843 | p = drbd_prepare_command(peer_device, sock); | |
9f5bdc33 | 844 | if (!p) { |
b30ab791 | 845 | put_ldev(device); |
9f5bdc33 AG |
846 | return -EIO; |
847 | } | |
b30ab791 | 848 | spin_lock_irq(&device->ldev->md.uuid_lock); |
b411b363 | 849 | for (i = UI_CURRENT; i < UI_SIZE; i++) |
b30ab791 AG |
850 | p->uuid[i] = cpu_to_be64(device->ldev->md.uuid[i]); |
851 | spin_unlock_irq(&device->ldev->md.uuid_lock); | |
b411b363 | 852 | |
b30ab791 AG |
853 | device->comm_bm_set = drbd_bm_total_weight(device); |
854 | p->uuid[UI_SIZE] = cpu_to_be64(device->comm_bm_set); | |
44ed167d | 855 | rcu_read_lock(); |
69a22773 | 856 | uuid_flags |= rcu_dereference(peer_device->connection->net_conf)->discard_my_data ? 1 : 0; |
44ed167d | 857 | rcu_read_unlock(); |
b30ab791 AG |
858 | uuid_flags |= test_bit(CRASHED_PRIMARY, &device->flags) ? 2 : 0; |
859 | uuid_flags |= device->new_state_tmp.disk == D_INCONSISTENT ? 4 : 0; | |
9f5bdc33 | 860 | p->uuid[UI_FLAGS] = cpu_to_be64(uuid_flags); |
b411b363 | 861 | |
b30ab791 | 862 | put_ldev(device); |
69a22773 | 863 | return drbd_send_command(peer_device, sock, P_UUIDS, sizeof(*p), NULL, 0); |
b411b363 PR |
864 | } |
865 | ||
69a22773 | 866 | int drbd_send_uuids(struct drbd_peer_device *peer_device) |
b411b363 | 867 | { |
69a22773 | 868 | return _drbd_send_uuids(peer_device, 0); |
b411b363 PR |
869 | } |
870 | ||
69a22773 | 871 | int drbd_send_uuids_skip_initial_sync(struct drbd_peer_device *peer_device) |
b411b363 | 872 | { |
69a22773 | 873 | return _drbd_send_uuids(peer_device, 8); |
b411b363 PR |
874 | } |
875 | ||
b30ab791 | 876 | void drbd_print_uuids(struct drbd_device *device, const char *text) |
62b0da3a | 877 | { |
b30ab791 AG |
878 | if (get_ldev_if_state(device, D_NEGOTIATING)) { |
879 | u64 *uuid = device->ldev->md.uuid; | |
d0180171 | 880 | drbd_info(device, "%s %016llX:%016llX:%016llX:%016llX\n", |
62b0da3a LE |
881 | text, |
882 | (unsigned long long)uuid[UI_CURRENT], | |
883 | (unsigned long long)uuid[UI_BITMAP], | |
884 | (unsigned long long)uuid[UI_HISTORY_START], | |
885 | (unsigned long long)uuid[UI_HISTORY_END]); | |
b30ab791 | 886 | put_ldev(device); |
62b0da3a | 887 | } else { |
d0180171 | 888 | drbd_info(device, "%s effective data uuid: %016llX\n", |
62b0da3a | 889 | text, |
b30ab791 | 890 | (unsigned long long)device->ed_uuid); |
62b0da3a LE |
891 | } |
892 | } | |
893 | ||
69a22773 | 894 | void drbd_gen_and_send_sync_uuid(struct drbd_peer_device *peer_device) |
b411b363 | 895 | { |
69a22773 | 896 | struct drbd_device *device = peer_device->device; |
9f5bdc33 AG |
897 | struct drbd_socket *sock; |
898 | struct p_rs_uuid *p; | |
5a22db89 LE |
899 | u64 uuid; |
900 | ||
0b0ba1ef | 901 | D_ASSERT(device, device->state.disk == D_UP_TO_DATE); |
b411b363 | 902 | |
b30ab791 | 903 | uuid = device->ldev->md.uuid[UI_BITMAP]; |
5ba3dac5 PR |
904 | if (uuid && uuid != UUID_JUST_CREATED) |
905 | uuid = uuid + UUID_NEW_BM_OFFSET; | |
906 | else | |
907 | get_random_bytes(&uuid, sizeof(u64)); | |
b30ab791 AG |
908 | drbd_uuid_set(device, UI_BITMAP, uuid); |
909 | drbd_print_uuids(device, "updated sync UUID"); | |
910 | drbd_md_sync(device); | |
b411b363 | 911 | |
69a22773 AG |
912 | sock = &peer_device->connection->data; |
913 | p = drbd_prepare_command(peer_device, sock); | |
9f5bdc33 AG |
914 | if (p) { |
915 | p->uuid = cpu_to_be64(uuid); | |
69a22773 | 916 | drbd_send_command(peer_device, sock, P_SYNC_UUID, sizeof(*p), NULL, 0); |
9f5bdc33 | 917 | } |
b411b363 PR |
918 | } |
919 | ||
69a22773 | 920 | int drbd_send_sizes(struct drbd_peer_device *peer_device, int trigger_reply, enum dds_flags flags) |
b411b363 | 921 | { |
69a22773 | 922 | struct drbd_device *device = peer_device->device; |
9f5bdc33 AG |
923 | struct drbd_socket *sock; |
924 | struct p_sizes *p; | |
b411b363 | 925 | sector_t d_size, u_size; |
db141b2f LE |
926 | int q_order_type; |
927 | unsigned int max_bio_size; | |
b411b363 | 928 | |
b30ab791 | 929 | if (get_ldev_if_state(device, D_NEGOTIATING)) { |
0b0ba1ef | 930 | D_ASSERT(device, device->ldev->backing_bdev); |
b30ab791 | 931 | d_size = drbd_get_max_capacity(device->ldev); |
daeda1cc | 932 | rcu_read_lock(); |
b30ab791 | 933 | u_size = rcu_dereference(device->ldev->disk_conf)->disk_size; |
daeda1cc | 934 | rcu_read_unlock(); |
b30ab791 AG |
935 | q_order_type = drbd_queue_order_type(device); |
936 | max_bio_size = queue_max_hw_sectors(device->ldev->backing_bdev->bd_disk->queue) << 9; | |
db141b2f | 937 | max_bio_size = min(max_bio_size, DRBD_MAX_BIO_SIZE); |
b30ab791 | 938 | put_ldev(device); |
b411b363 PR |
939 | } else { |
940 | d_size = 0; | |
941 | u_size = 0; | |
942 | q_order_type = QUEUE_ORDERED_NONE; | |
99432fcc | 943 | max_bio_size = DRBD_MAX_BIO_SIZE; /* ... multiple BIOs per peer_request */ |
b411b363 PR |
944 | } |
945 | ||
69a22773 AG |
946 | sock = &peer_device->connection->data; |
947 | p = drbd_prepare_command(peer_device, sock); | |
9f5bdc33 AG |
948 | if (!p) |
949 | return -EIO; | |
6809384c | 950 | |
69a22773 | 951 | if (peer_device->connection->agreed_pro_version <= 94) |
98683650 | 952 | max_bio_size = min(max_bio_size, DRBD_MAX_SIZE_H80_PACKET); |
69a22773 | 953 | else if (peer_device->connection->agreed_pro_version < 100) |
98683650 | 954 | max_bio_size = min(max_bio_size, DRBD_MAX_BIO_SIZE_P95); |
b411b363 | 955 | |
9f5bdc33 AG |
956 | p->d_size = cpu_to_be64(d_size); |
957 | p->u_size = cpu_to_be64(u_size); | |
b30ab791 | 958 | p->c_size = cpu_to_be64(trigger_reply ? 0 : drbd_get_capacity(device->this_bdev)); |
9f5bdc33 AG |
959 | p->max_bio_size = cpu_to_be32(max_bio_size); |
960 | p->queue_order_type = cpu_to_be16(q_order_type); | |
961 | p->dds_flags = cpu_to_be16(flags); | |
69a22773 | 962 | return drbd_send_command(peer_device, sock, P_SIZES, sizeof(*p), NULL, 0); |
b411b363 PR |
963 | } |
964 | ||
965 | /** | |
f479ea06 | 966 | * drbd_send_current_state() - Sends the drbd state to the peer |
69a22773 | 967 | * @peer_device: DRBD peer device. |
b411b363 | 968 | */ |
69a22773 | 969 | int drbd_send_current_state(struct drbd_peer_device *peer_device) |
b411b363 | 970 | { |
7c96715a | 971 | struct drbd_socket *sock; |
9f5bdc33 | 972 | struct p_state *p; |
b411b363 | 973 | |
69a22773 AG |
974 | sock = &peer_device->connection->data; |
975 | p = drbd_prepare_command(peer_device, sock); | |
9f5bdc33 AG |
976 | if (!p) |
977 | return -EIO; | |
69a22773 AG |
978 | p->state = cpu_to_be32(peer_device->device->state.i); /* Within the send mutex */ |
979 | return drbd_send_command(peer_device, sock, P_STATE, sizeof(*p), NULL, 0); | |
b411b363 PR |
980 | } |
981 | ||
f479ea06 LE |
982 | /** |
983 | * drbd_send_state() - After a state change, sends the new state to the peer | |
69a22773 | 984 | * @peer_device: DRBD peer device. |
43de7c85 | 985 | * @state: the state to send, not necessarily the current state. |
f479ea06 LE |
986 | * |
987 | * Each state change queues an "after_state_ch" work, which will eventually | |
988 | * send the resulting new state to the peer. If more state changes happen | |
989 | * between queuing and processing of the after_state_ch work, we still | |
990 | * want to send each intermediary state in the order it occurred. | |
991 | */ | |
69a22773 | 992 | int drbd_send_state(struct drbd_peer_device *peer_device, union drbd_state state) |
f479ea06 | 993 | { |
43de7c85 PR |
994 | struct drbd_socket *sock; |
995 | struct p_state *p; | |
f479ea06 | 996 | |
69a22773 AG |
997 | sock = &peer_device->connection->data; |
998 | p = drbd_prepare_command(peer_device, sock); | |
43de7c85 PR |
999 | if (!p) |
1000 | return -EIO; | |
1001 | p->state = cpu_to_be32(state.i); /* Within the send mutex */ | |
69a22773 | 1002 | return drbd_send_command(peer_device, sock, P_STATE, sizeof(*p), NULL, 0); |
43de7c85 | 1003 | } |
f479ea06 | 1004 | |
69a22773 | 1005 | int drbd_send_state_req(struct drbd_peer_device *peer_device, union drbd_state mask, union drbd_state val) |
9f5bdc33 AG |
1006 | { |
1007 | struct drbd_socket *sock; | |
1008 | struct p_req_state *p; | |
f479ea06 | 1009 | |
69a22773 AG |
1010 | sock = &peer_device->connection->data; |
1011 | p = drbd_prepare_command(peer_device, sock); | |
9f5bdc33 AG |
1012 | if (!p) |
1013 | return -EIO; | |
1014 | p->mask = cpu_to_be32(mask.i); | |
1015 | p->val = cpu_to_be32(val.i); | |
69a22773 | 1016 | return drbd_send_command(peer_device, sock, P_STATE_CHG_REQ, sizeof(*p), NULL, 0); |
b411b363 | 1017 | } |
f479ea06 | 1018 | |
bde89a9e | 1019 | int conn_send_state_req(struct drbd_connection *connection, union drbd_state mask, union drbd_state val) |
b411b363 | 1020 | { |
9f5bdc33 AG |
1021 | enum drbd_packet cmd; |
1022 | struct drbd_socket *sock; | |
1023 | struct p_req_state *p; | |
f479ea06 | 1024 | |
bde89a9e AG |
1025 | cmd = connection->agreed_pro_version < 100 ? P_STATE_CHG_REQ : P_CONN_ST_CHG_REQ; |
1026 | sock = &connection->data; | |
1027 | p = conn_prepare_command(connection, sock); | |
9f5bdc33 AG |
1028 | if (!p) |
1029 | return -EIO; | |
1030 | p->mask = cpu_to_be32(mask.i); | |
1031 | p->val = cpu_to_be32(val.i); | |
bde89a9e | 1032 | return conn_send_command(connection, sock, cmd, sizeof(*p), NULL, 0); |
f479ea06 LE |
1033 | } |
1034 | ||
69a22773 | 1035 | void drbd_send_sr_reply(struct drbd_peer_device *peer_device, enum drbd_state_rv retcode) |
b411b363 | 1036 | { |
9f5bdc33 AG |
1037 | struct drbd_socket *sock; |
1038 | struct p_req_state_reply *p; | |
b411b363 | 1039 | |
69a22773 AG |
1040 | sock = &peer_device->connection->meta; |
1041 | p = drbd_prepare_command(peer_device, sock); | |
9f5bdc33 AG |
1042 | if (p) { |
1043 | p->retcode = cpu_to_be32(retcode); | |
69a22773 | 1044 | drbd_send_command(peer_device, sock, P_STATE_CHG_REPLY, sizeof(*p), NULL, 0); |
9f5bdc33 | 1045 | } |
b411b363 | 1046 | } |
b411b363 | 1047 | |
bde89a9e | 1048 | void conn_send_sr_reply(struct drbd_connection *connection, enum drbd_state_rv retcode) |
047cd4a6 | 1049 | { |
9f5bdc33 AG |
1050 | struct drbd_socket *sock; |
1051 | struct p_req_state_reply *p; | |
bde89a9e | 1052 | enum drbd_packet cmd = connection->agreed_pro_version < 100 ? P_STATE_CHG_REPLY : P_CONN_ST_CHG_REPLY; |
b411b363 | 1053 | |
bde89a9e AG |
1054 | sock = &connection->meta; |
1055 | p = conn_prepare_command(connection, sock); | |
9f5bdc33 AG |
1056 | if (p) { |
1057 | p->retcode = cpu_to_be32(retcode); | |
bde89a9e | 1058 | conn_send_command(connection, sock, cmd, sizeof(*p), NULL, 0); |
9f5bdc33 | 1059 | } |
b411b363 PR |
1060 | } |
1061 | ||
a02d1240 | 1062 | static void dcbp_set_code(struct p_compressed_bm *p, enum drbd_bitmap_code code) |
b411b363 | 1063 | { |
a02d1240 AG |
1064 | BUG_ON(code & ~0xf); |
1065 | p->encoding = (p->encoding & ~0xf) | code; | |
1066 | } | |
b411b363 | 1067 | |
a02d1240 AG |
1068 | static void dcbp_set_start(struct p_compressed_bm *p, int set) |
1069 | { | |
1070 | p->encoding = (p->encoding & ~0x80) | (set ? 0x80 : 0); | |
1071 | } | |
b411b363 | 1072 | |
a02d1240 AG |
1073 | static void dcbp_set_pad_bits(struct p_compressed_bm *p, int n) |
1074 | { | |
1075 | BUG_ON(n & ~0x7); | |
1076 | p->encoding = (p->encoding & (~0x7 << 4)) | (n << 4); | |
b411b363 PR |
1077 | } |
1078 | ||
b30ab791 | 1079 | static int fill_bitmap_rle_bits(struct drbd_device *device, |
50d0b1ad AG |
1080 | struct p_compressed_bm *p, |
1081 | unsigned int size, | |
1082 | struct bm_xfer_ctx *c) | |
b411b363 PR |
1083 | { |
1084 | struct bitstream bs; | |
1085 | unsigned long plain_bits; | |
1086 | unsigned long tmp; | |
1087 | unsigned long rl; | |
1088 | unsigned len; | |
1089 | unsigned toggle; | |
44ed167d | 1090 | int bits, use_rle; |
b411b363 PR |
1091 | |
1092 | /* may we use this feature? */ | |
44ed167d | 1093 | rcu_read_lock(); |
a6b32bc3 | 1094 | use_rle = rcu_dereference(first_peer_device(device)->connection->net_conf)->use_rle; |
44ed167d | 1095 | rcu_read_unlock(); |
a6b32bc3 | 1096 | if (!use_rle || first_peer_device(device)->connection->agreed_pro_version < 90) |
44ed167d | 1097 | return 0; |
b411b363 PR |
1098 | |
1099 | if (c->bit_offset >= c->bm_bits) | |
1100 | return 0; /* nothing to do. */ | |
1101 | ||
1102 | /* use at most thus many bytes */ | |
50d0b1ad AG |
1103 | bitstream_init(&bs, p->code, size, 0); |
1104 | memset(p->code, 0, size); | |
b411b363 PR |
1105 | /* plain bits covered in this code string */ |
1106 | plain_bits = 0; | |
1107 | ||
1108 | /* p->encoding & 0x80 stores whether the first run length is set. | |
1109 | * bit offset is implicit. | |
1110 | * start with toggle == 2 to be able to tell the first iteration */ | |
1111 | toggle = 2; | |
1112 | ||
1113 | /* see how much plain bits we can stuff into one packet | |
1114 | * using RLE and VLI. */ | |
1115 | do { | |
b30ab791 AG |
1116 | tmp = (toggle == 0) ? _drbd_bm_find_next_zero(device, c->bit_offset) |
1117 | : _drbd_bm_find_next(device, c->bit_offset); | |
b411b363 PR |
1118 | if (tmp == -1UL) |
1119 | tmp = c->bm_bits; | |
1120 | rl = tmp - c->bit_offset; | |
1121 | ||
1122 | if (toggle == 2) { /* first iteration */ | |
1123 | if (rl == 0) { | |
1124 | /* the first checked bit was set, | |
1125 | * store start value, */ | |
a02d1240 | 1126 | dcbp_set_start(p, 1); |
b411b363 PR |
1127 | /* but skip encoding of zero run length */ |
1128 | toggle = !toggle; | |
1129 | continue; | |
1130 | } | |
a02d1240 | 1131 | dcbp_set_start(p, 0); |
b411b363 PR |
1132 | } |
1133 | ||
1134 | /* paranoia: catch zero runlength. | |
1135 | * can only happen if bitmap is modified while we scan it. */ | |
1136 | if (rl == 0) { | |
d0180171 | 1137 | drbd_err(device, "unexpected zero runlength while encoding bitmap " |
b411b363 PR |
1138 | "t:%u bo:%lu\n", toggle, c->bit_offset); |
1139 | return -1; | |
1140 | } | |
1141 | ||
1142 | bits = vli_encode_bits(&bs, rl); | |
1143 | if (bits == -ENOBUFS) /* buffer full */ | |
1144 | break; | |
1145 | if (bits <= 0) { | |
d0180171 | 1146 | drbd_err(device, "error while encoding bitmap: %d\n", bits); |
b411b363 PR |
1147 | return 0; |
1148 | } | |
1149 | ||
1150 | toggle = !toggle; | |
1151 | plain_bits += rl; | |
1152 | c->bit_offset = tmp; | |
1153 | } while (c->bit_offset < c->bm_bits); | |
1154 | ||
1155 | len = bs.cur.b - p->code + !!bs.cur.bit; | |
1156 | ||
1157 | if (plain_bits < (len << 3)) { | |
1158 | /* incompressible with this method. | |
1159 | * we need to rewind both word and bit position. */ | |
1160 | c->bit_offset -= plain_bits; | |
1161 | bm_xfer_ctx_bit_to_word_offset(c); | |
1162 | c->bit_offset = c->word_offset * BITS_PER_LONG; | |
1163 | return 0; | |
1164 | } | |
1165 | ||
1166 | /* RLE + VLI was able to compress it just fine. | |
1167 | * update c->word_offset. */ | |
1168 | bm_xfer_ctx_bit_to_word_offset(c); | |
1169 | ||
1170 | /* store pad_bits */ | |
a02d1240 | 1171 | dcbp_set_pad_bits(p, (8 - bs.cur.bit) & 0x7); |
b411b363 PR |
1172 | |
1173 | return len; | |
1174 | } | |
1175 | ||
f70af118 AG |
1176 | /** |
1177 | * send_bitmap_rle_or_plain | |
1178 | * | |
1179 | * Return 0 when done, 1 when another iteration is needed, and a negative error | |
1180 | * code upon failure. | |
1181 | */ | |
1182 | static int | |
b30ab791 | 1183 | send_bitmap_rle_or_plain(struct drbd_device *device, struct bm_xfer_ctx *c) |
b411b363 | 1184 | { |
a6b32bc3 AG |
1185 | struct drbd_socket *sock = &first_peer_device(device)->connection->data; |
1186 | unsigned int header_size = drbd_header_size(first_peer_device(device)->connection); | |
e658983a | 1187 | struct p_compressed_bm *p = sock->sbuf + header_size; |
a982dd57 | 1188 | int len, err; |
b411b363 | 1189 | |
b30ab791 | 1190 | len = fill_bitmap_rle_bits(device, p, |
e658983a | 1191 | DRBD_SOCKET_BUFFER_SIZE - header_size - sizeof(*p), c); |
b411b363 | 1192 | if (len < 0) |
f70af118 | 1193 | return -EIO; |
b411b363 PR |
1194 | |
1195 | if (len) { | |
a02d1240 | 1196 | dcbp_set_code(p, RLE_VLI_Bits); |
a6b32bc3 | 1197 | err = __send_command(first_peer_device(device)->connection, device->vnr, sock, |
9f5bdc33 AG |
1198 | P_COMPRESSED_BITMAP, sizeof(*p) + len, |
1199 | NULL, 0); | |
b411b363 | 1200 | c->packets[0]++; |
e658983a | 1201 | c->bytes[0] += header_size + sizeof(*p) + len; |
b411b363 PR |
1202 | |
1203 | if (c->bit_offset >= c->bm_bits) | |
1204 | len = 0; /* DONE */ | |
1205 | } else { | |
1206 | /* was not compressible. | |
1207 | * send a buffer full of plain text bits instead. */ | |
50d0b1ad AG |
1208 | unsigned int data_size; |
1209 | unsigned long num_words; | |
e658983a | 1210 | unsigned long *p = sock->sbuf + header_size; |
50d0b1ad AG |
1211 | |
1212 | data_size = DRBD_SOCKET_BUFFER_SIZE - header_size; | |
e658983a | 1213 | num_words = min_t(size_t, data_size / sizeof(*p), |
50d0b1ad | 1214 | c->bm_words - c->word_offset); |
e658983a | 1215 | len = num_words * sizeof(*p); |
b411b363 | 1216 | if (len) |
b30ab791 | 1217 | drbd_bm_get_lel(device, c->word_offset, num_words, p); |
a6b32bc3 | 1218 | err = __send_command(first_peer_device(device)->connection, device->vnr, sock, P_BITMAP, len, NULL, 0); |
b411b363 PR |
1219 | c->word_offset += num_words; |
1220 | c->bit_offset = c->word_offset * BITS_PER_LONG; | |
1221 | ||
1222 | c->packets[1]++; | |
50d0b1ad | 1223 | c->bytes[1] += header_size + len; |
b411b363 PR |
1224 | |
1225 | if (c->bit_offset > c->bm_bits) | |
1226 | c->bit_offset = c->bm_bits; | |
1227 | } | |
a982dd57 | 1228 | if (!err) { |
f70af118 | 1229 | if (len == 0) { |
b30ab791 | 1230 | INFO_bm_xfer_stats(device, "send", c); |
f70af118 AG |
1231 | return 0; |
1232 | } else | |
1233 | return 1; | |
1234 | } | |
1235 | return -EIO; | |
b411b363 PR |
1236 | } |
1237 | ||
1238 | /* See the comment at receive_bitmap() */ | |
b30ab791 | 1239 | static int _drbd_send_bitmap(struct drbd_device *device) |
b411b363 PR |
1240 | { |
1241 | struct bm_xfer_ctx c; | |
f70af118 | 1242 | int err; |
b411b363 | 1243 | |
b30ab791 | 1244 | if (!expect(device->bitmap)) |
81e84650 | 1245 | return false; |
b411b363 | 1246 | |
b30ab791 AG |
1247 | if (get_ldev(device)) { |
1248 | if (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC)) { | |
d0180171 | 1249 | drbd_info(device, "Writing the whole bitmap, MDF_FullSync was set.\n"); |
b30ab791 AG |
1250 | drbd_bm_set_all(device); |
1251 | if (drbd_bm_write(device)) { | |
b411b363 PR |
1252 | /* write_bm did fail! Leave full sync flag set in Meta P_DATA |
1253 | * but otherwise process as per normal - need to tell other | |
1254 | * side that a full resync is required! */ | |
d0180171 | 1255 | drbd_err(device, "Failed to write bitmap to disk!\n"); |
b411b363 | 1256 | } else { |
b30ab791 AG |
1257 | drbd_md_clear_flag(device, MDF_FULL_SYNC); |
1258 | drbd_md_sync(device); | |
b411b363 PR |
1259 | } |
1260 | } | |
b30ab791 | 1261 | put_ldev(device); |
b411b363 PR |
1262 | } |
1263 | ||
1264 | c = (struct bm_xfer_ctx) { | |
b30ab791 AG |
1265 | .bm_bits = drbd_bm_bits(device), |
1266 | .bm_words = drbd_bm_words(device), | |
b411b363 PR |
1267 | }; |
1268 | ||
1269 | do { | |
b30ab791 | 1270 | err = send_bitmap_rle_or_plain(device, &c); |
f70af118 | 1271 | } while (err > 0); |
b411b363 | 1272 | |
f70af118 | 1273 | return err == 0; |
b411b363 PR |
1274 | } |
1275 | ||
b30ab791 | 1276 | int drbd_send_bitmap(struct drbd_device *device) |
b411b363 | 1277 | { |
a6b32bc3 | 1278 | struct drbd_socket *sock = &first_peer_device(device)->connection->data; |
9f5bdc33 | 1279 | int err = -1; |
b411b363 | 1280 | |
9f5bdc33 AG |
1281 | mutex_lock(&sock->mutex); |
1282 | if (sock->socket) | |
b30ab791 | 1283 | err = !_drbd_send_bitmap(device); |
9f5bdc33 | 1284 | mutex_unlock(&sock->mutex); |
b411b363 PR |
1285 | return err; |
1286 | } | |
1287 | ||
bde89a9e | 1288 | void drbd_send_b_ack(struct drbd_connection *connection, u32 barrier_nr, u32 set_size) |
b411b363 | 1289 | { |
9f5bdc33 AG |
1290 | struct drbd_socket *sock; |
1291 | struct p_barrier_ack *p; | |
b411b363 | 1292 | |
bde89a9e | 1293 | if (connection->cstate < C_WF_REPORT_PARAMS) |
9f5bdc33 | 1294 | return; |
b411b363 | 1295 | |
bde89a9e AG |
1296 | sock = &connection->meta; |
1297 | p = conn_prepare_command(connection, sock); | |
9f5bdc33 AG |
1298 | if (!p) |
1299 | return; | |
1300 | p->barrier = barrier_nr; | |
1301 | p->set_size = cpu_to_be32(set_size); | |
bde89a9e | 1302 | conn_send_command(connection, sock, P_BARRIER_ACK, sizeof(*p), NULL, 0); |
b411b363 PR |
1303 | } |
1304 | ||
1305 | /** | |
1306 | * _drbd_send_ack() - Sends an ack packet | |
b30ab791 | 1307 | * @device: DRBD device. |
b411b363 PR |
1308 | * @cmd: Packet command code. |
1309 | * @sector: sector, needs to be in big endian byte order | |
1310 | * @blksize: size in byte, needs to be in big endian byte order | |
1311 | * @block_id: Id, big endian byte order | |
1312 | */ | |
69a22773 | 1313 | static int _drbd_send_ack(struct drbd_peer_device *peer_device, enum drbd_packet cmd, |
d8763023 | 1314 | u64 sector, u32 blksize, u64 block_id) |
b411b363 | 1315 | { |
9f5bdc33 AG |
1316 | struct drbd_socket *sock; |
1317 | struct p_block_ack *p; | |
b411b363 | 1318 | |
69a22773 | 1319 | if (peer_device->device->state.conn < C_CONNECTED) |
9f5bdc33 | 1320 | return -EIO; |
b411b363 | 1321 | |
69a22773 AG |
1322 | sock = &peer_device->connection->meta; |
1323 | p = drbd_prepare_command(peer_device, sock); | |
9f5bdc33 | 1324 | if (!p) |
a8c32aa8 | 1325 | return -EIO; |
9f5bdc33 AG |
1326 | p->sector = sector; |
1327 | p->block_id = block_id; | |
1328 | p->blksize = blksize; | |
69a22773 AG |
1329 | p->seq_num = cpu_to_be32(atomic_inc_return(&peer_device->device->packet_seq)); |
1330 | return drbd_send_command(peer_device, sock, cmd, sizeof(*p), NULL, 0); | |
b411b363 PR |
1331 | } |
1332 | ||
2b2bf214 LE |
1333 | /* dp->sector and dp->block_id already/still in network byte order, |
1334 | * data_size is payload size according to dp->head, | |
1335 | * and may need to be corrected for digest size. */ | |
69a22773 | 1336 | void drbd_send_ack_dp(struct drbd_peer_device *peer_device, enum drbd_packet cmd, |
a9a9994d | 1337 | struct p_data *dp, int data_size) |
b411b363 | 1338 | { |
69a22773 AG |
1339 | if (peer_device->connection->peer_integrity_tfm) |
1340 | data_size -= crypto_hash_digestsize(peer_device->connection->peer_integrity_tfm); | |
1341 | _drbd_send_ack(peer_device, cmd, dp->sector, cpu_to_be32(data_size), | |
a9a9994d | 1342 | dp->block_id); |
b411b363 PR |
1343 | } |
1344 | ||
69a22773 | 1345 | void drbd_send_ack_rp(struct drbd_peer_device *peer_device, enum drbd_packet cmd, |
a9a9994d | 1346 | struct p_block_req *rp) |
b411b363 | 1347 | { |
69a22773 | 1348 | _drbd_send_ack(peer_device, cmd, rp->sector, rp->blksize, rp->block_id); |
b411b363 PR |
1349 | } |
1350 | ||
1351 | /** | |
1352 | * drbd_send_ack() - Sends an ack packet | |
b30ab791 | 1353 | * @device: DRBD device |
db830c46 AG |
1354 | * @cmd: packet command code |
1355 | * @peer_req: peer request | |
b411b363 | 1356 | */ |
69a22773 | 1357 | int drbd_send_ack(struct drbd_peer_device *peer_device, enum drbd_packet cmd, |
db830c46 | 1358 | struct drbd_peer_request *peer_req) |
b411b363 | 1359 | { |
69a22773 | 1360 | return _drbd_send_ack(peer_device, cmd, |
dd516121 AG |
1361 | cpu_to_be64(peer_req->i.sector), |
1362 | cpu_to_be32(peer_req->i.size), | |
1363 | peer_req->block_id); | |
b411b363 PR |
1364 | } |
1365 | ||
1366 | /* This function misuses the block_id field to signal if the blocks | |
1367 | * are is sync or not. */ | |
69a22773 | 1368 | int drbd_send_ack_ex(struct drbd_peer_device *peer_device, enum drbd_packet cmd, |
b411b363 PR |
1369 | sector_t sector, int blksize, u64 block_id) |
1370 | { | |
69a22773 | 1371 | return _drbd_send_ack(peer_device, cmd, |
b411b363 PR |
1372 | cpu_to_be64(sector), |
1373 | cpu_to_be32(blksize), | |
1374 | cpu_to_be64(block_id)); | |
1375 | } | |
1376 | ||
69a22773 | 1377 | int drbd_send_drequest(struct drbd_peer_device *peer_device, int cmd, |
b411b363 PR |
1378 | sector_t sector, int size, u64 block_id) |
1379 | { | |
9f5bdc33 AG |
1380 | struct drbd_socket *sock; |
1381 | struct p_block_req *p; | |
b411b363 | 1382 | |
69a22773 AG |
1383 | sock = &peer_device->connection->data; |
1384 | p = drbd_prepare_command(peer_device, sock); | |
9f5bdc33 AG |
1385 | if (!p) |
1386 | return -EIO; | |
1387 | p->sector = cpu_to_be64(sector); | |
1388 | p->block_id = block_id; | |
1389 | p->blksize = cpu_to_be32(size); | |
69a22773 | 1390 | return drbd_send_command(peer_device, sock, cmd, sizeof(*p), NULL, 0); |
b411b363 PR |
1391 | } |
1392 | ||
69a22773 | 1393 | int drbd_send_drequest_csum(struct drbd_peer_device *peer_device, sector_t sector, int size, |
d8763023 | 1394 | void *digest, int digest_size, enum drbd_packet cmd) |
b411b363 | 1395 | { |
9f5bdc33 AG |
1396 | struct drbd_socket *sock; |
1397 | struct p_block_req *p; | |
b411b363 | 1398 | |
9f5bdc33 | 1399 | /* FIXME: Put the digest into the preallocated socket buffer. */ |
b411b363 | 1400 | |
69a22773 AG |
1401 | sock = &peer_device->connection->data; |
1402 | p = drbd_prepare_command(peer_device, sock); | |
9f5bdc33 AG |
1403 | if (!p) |
1404 | return -EIO; | |
1405 | p->sector = cpu_to_be64(sector); | |
1406 | p->block_id = ID_SYNCER /* unused */; | |
1407 | p->blksize = cpu_to_be32(size); | |
69a22773 | 1408 | return drbd_send_command(peer_device, sock, cmd, sizeof(*p), digest, digest_size); |
b411b363 PR |
1409 | } |
1410 | ||
69a22773 | 1411 | int drbd_send_ov_request(struct drbd_peer_device *peer_device, sector_t sector, int size) |
b411b363 | 1412 | { |
9f5bdc33 AG |
1413 | struct drbd_socket *sock; |
1414 | struct p_block_req *p; | |
b411b363 | 1415 | |
69a22773 AG |
1416 | sock = &peer_device->connection->data; |
1417 | p = drbd_prepare_command(peer_device, sock); | |
9f5bdc33 AG |
1418 | if (!p) |
1419 | return -EIO; | |
1420 | p->sector = cpu_to_be64(sector); | |
1421 | p->block_id = ID_SYNCER /* unused */; | |
1422 | p->blksize = cpu_to_be32(size); | |
69a22773 | 1423 | return drbd_send_command(peer_device, sock, P_OV_REQUEST, sizeof(*p), NULL, 0); |
b411b363 PR |
1424 | } |
1425 | ||
1426 | /* called on sndtimeo | |
81e84650 AG |
1427 | * returns false if we should retry, |
1428 | * true if we think connection is dead | |
b411b363 | 1429 | */ |
bde89a9e | 1430 | static int we_should_drop_the_connection(struct drbd_connection *connection, struct socket *sock) |
b411b363 PR |
1431 | { |
1432 | int drop_it; | |
b30ab791 | 1433 | /* long elapsed = (long)(jiffies - device->last_received); */ |
b411b363 | 1434 | |
bde89a9e AG |
1435 | drop_it = connection->meta.socket == sock |
1436 | || !connection->asender.task | |
1437 | || get_t_state(&connection->asender) != RUNNING | |
1438 | || connection->cstate < C_WF_REPORT_PARAMS; | |
b411b363 PR |
1439 | |
1440 | if (drop_it) | |
81e84650 | 1441 | return true; |
b411b363 | 1442 | |
bde89a9e | 1443 | drop_it = !--connection->ko_count; |
b411b363 | 1444 | if (!drop_it) { |
1ec861eb | 1445 | drbd_err(connection, "[%s/%d] sock_sendmsg time expired, ko = %u\n", |
bde89a9e AG |
1446 | current->comm, current->pid, connection->ko_count); |
1447 | request_ping(connection); | |
b411b363 PR |
1448 | } |
1449 | ||
b30ab791 | 1450 | return drop_it; /* && (device->state == R_PRIMARY) */; |
b411b363 PR |
1451 | } |
1452 | ||
bde89a9e | 1453 | static void drbd_update_congested(struct drbd_connection *connection) |
9e204cdd | 1454 | { |
bde89a9e | 1455 | struct sock *sk = connection->data.socket->sk; |
9e204cdd | 1456 | if (sk->sk_wmem_queued > sk->sk_sndbuf * 4 / 5) |
bde89a9e | 1457 | set_bit(NET_CONGESTED, &connection->flags); |
9e204cdd AG |
1458 | } |
1459 | ||
b411b363 PR |
1460 | /* The idea of sendpage seems to be to put some kind of reference |
1461 | * to the page into the skb, and to hand it over to the NIC. In | |
1462 | * this process get_page() gets called. | |
1463 | * | |
1464 | * As soon as the page was really sent over the network put_page() | |
1465 | * gets called by some part of the network layer. [ NIC driver? ] | |
1466 | * | |
1467 | * [ get_page() / put_page() increment/decrement the count. If count | |
1468 | * reaches 0 the page will be freed. ] | |
1469 | * | |
1470 | * This works nicely with pages from FSs. | |
1471 | * But this means that in protocol A we might signal IO completion too early! | |
1472 | * | |
1473 | * In order not to corrupt data during a resync we must make sure | |
1474 | * that we do not reuse our own buffer pages (EEs) to early, therefore | |
1475 | * we have the net_ee list. | |
1476 | * | |
1477 | * XFS seems to have problems, still, it submits pages with page_count == 0! | |
1478 | * As a workaround, we disable sendpage on pages | |
1479 | * with page_count == 0 or PageSlab. | |
1480 | */ | |
69a22773 | 1481 | static int _drbd_no_send_page(struct drbd_peer_device *peer_device, struct page *page, |
b987427b | 1482 | int offset, size_t size, unsigned msg_flags) |
b411b363 | 1483 | { |
b987427b AG |
1484 | struct socket *socket; |
1485 | void *addr; | |
1486 | int err; | |
1487 | ||
69a22773 | 1488 | socket = peer_device->connection->data.socket; |
b987427b | 1489 | addr = kmap(page) + offset; |
69a22773 | 1490 | err = drbd_send_all(peer_device->connection, socket, addr, size, msg_flags); |
b411b363 | 1491 | kunmap(page); |
b987427b | 1492 | if (!err) |
69a22773 | 1493 | peer_device->device->send_cnt += size >> 9; |
b987427b | 1494 | return err; |
b411b363 PR |
1495 | } |
1496 | ||
69a22773 | 1497 | static int _drbd_send_page(struct drbd_peer_device *peer_device, struct page *page, |
ba11ad9a | 1498 | int offset, size_t size, unsigned msg_flags) |
b411b363 | 1499 | { |
69a22773 | 1500 | struct socket *socket = peer_device->connection->data.socket; |
b411b363 | 1501 | mm_segment_t oldfs = get_fs(); |
b411b363 | 1502 | int len = size; |
88b390ff | 1503 | int err = -EIO; |
b411b363 PR |
1504 | |
1505 | /* e.g. XFS meta- & log-data is in slab pages, which have a | |
1506 | * page_count of 0 and/or have PageSlab() set. | |
1507 | * we cannot use send_page for those, as that does get_page(); | |
1508 | * put_page(); and would cause either a VM_BUG directly, or | |
1509 | * __page_cache_release a page that would actually still be referenced | |
1510 | * by someone, leading to some obscure delayed Oops somewhere else. */ | |
1511 | if (disable_sendpage || (page_count(page) < 1) || PageSlab(page)) | |
69a22773 | 1512 | return _drbd_no_send_page(peer_device, page, offset, size, msg_flags); |
b411b363 | 1513 | |
ba11ad9a | 1514 | msg_flags |= MSG_NOSIGNAL; |
69a22773 | 1515 | drbd_update_congested(peer_device->connection); |
b411b363 PR |
1516 | set_fs(KERNEL_DS); |
1517 | do { | |
88b390ff AG |
1518 | int sent; |
1519 | ||
1520 | sent = socket->ops->sendpage(socket, page, offset, len, msg_flags); | |
b411b363 | 1521 | if (sent <= 0) { |
88b390ff | 1522 | if (sent == -EAGAIN) { |
69a22773 | 1523 | if (we_should_drop_the_connection(peer_device->connection, socket)) |
88b390ff AG |
1524 | break; |
1525 | continue; | |
1526 | } | |
69a22773 | 1527 | drbd_warn(peer_device->device, "%s: size=%d len=%d sent=%d\n", |
b411b363 | 1528 | __func__, (int)size, len, sent); |
88b390ff AG |
1529 | if (sent < 0) |
1530 | err = sent; | |
b411b363 PR |
1531 | break; |
1532 | } | |
1533 | len -= sent; | |
1534 | offset += sent; | |
b30ab791 | 1535 | } while (len > 0 /* THINK && device->cstate >= C_CONNECTED*/); |
b411b363 | 1536 | set_fs(oldfs); |
69a22773 | 1537 | clear_bit(NET_CONGESTED, &peer_device->connection->flags); |
b411b363 | 1538 | |
88b390ff AG |
1539 | if (len == 0) { |
1540 | err = 0; | |
69a22773 | 1541 | peer_device->device->send_cnt += size >> 9; |
88b390ff AG |
1542 | } |
1543 | return err; | |
b411b363 PR |
1544 | } |
1545 | ||
69a22773 | 1546 | static int _drbd_send_bio(struct drbd_peer_device *peer_device, struct bio *bio) |
b411b363 | 1547 | { |
7988613b KO |
1548 | struct bio_vec bvec; |
1549 | struct bvec_iter iter; | |
1550 | ||
ba11ad9a | 1551 | /* hint all but last page with MSG_MORE */ |
7988613b | 1552 | bio_for_each_segment(bvec, bio, iter) { |
7fae55da AG |
1553 | int err; |
1554 | ||
69a22773 | 1555 | err = _drbd_no_send_page(peer_device, bvec.bv_page, |
7988613b | 1556 | bvec.bv_offset, bvec.bv_len, |
4550dd6c | 1557 | bio_iter_last(bvec, iter) |
7988613b | 1558 | ? 0 : MSG_MORE); |
7fae55da AG |
1559 | if (err) |
1560 | return err; | |
b411b363 | 1561 | } |
7fae55da | 1562 | return 0; |
b411b363 PR |
1563 | } |
1564 | ||
69a22773 | 1565 | static int _drbd_send_zc_bio(struct drbd_peer_device *peer_device, struct bio *bio) |
b411b363 | 1566 | { |
7988613b KO |
1567 | struct bio_vec bvec; |
1568 | struct bvec_iter iter; | |
1569 | ||
ba11ad9a | 1570 | /* hint all but last page with MSG_MORE */ |
7988613b | 1571 | bio_for_each_segment(bvec, bio, iter) { |
7fae55da AG |
1572 | int err; |
1573 | ||
69a22773 | 1574 | err = _drbd_send_page(peer_device, bvec.bv_page, |
7988613b | 1575 | bvec.bv_offset, bvec.bv_len, |
4550dd6c | 1576 | bio_iter_last(bvec, iter) ? 0 : MSG_MORE); |
7fae55da AG |
1577 | if (err) |
1578 | return err; | |
b411b363 | 1579 | } |
7fae55da | 1580 | return 0; |
b411b363 PR |
1581 | } |
1582 | ||
69a22773 | 1583 | static int _drbd_send_zc_ee(struct drbd_peer_device *peer_device, |
db830c46 | 1584 | struct drbd_peer_request *peer_req) |
45bb912b | 1585 | { |
db830c46 AG |
1586 | struct page *page = peer_req->pages; |
1587 | unsigned len = peer_req->i.size; | |
9f69230c | 1588 | int err; |
db830c46 | 1589 | |
ba11ad9a | 1590 | /* hint all but last page with MSG_MORE */ |
45bb912b LE |
1591 | page_chain_for_each(page) { |
1592 | unsigned l = min_t(unsigned, len, PAGE_SIZE); | |
9f69230c | 1593 | |
69a22773 | 1594 | err = _drbd_send_page(peer_device, page, 0, l, |
9f69230c AG |
1595 | page_chain_next(page) ? MSG_MORE : 0); |
1596 | if (err) | |
1597 | return err; | |
45bb912b LE |
1598 | len -= l; |
1599 | } | |
9f69230c | 1600 | return 0; |
45bb912b LE |
1601 | } |
1602 | ||
69a22773 | 1603 | static u32 bio_flags_to_wire(struct drbd_connection *connection, unsigned long bi_rw) |
76d2e7ec | 1604 | { |
69a22773 | 1605 | if (connection->agreed_pro_version >= 95) |
76d2e7ec | 1606 | return (bi_rw & REQ_SYNC ? DP_RW_SYNC : 0) | |
76d2e7ec PR |
1607 | (bi_rw & REQ_FUA ? DP_FUA : 0) | |
1608 | (bi_rw & REQ_FLUSH ? DP_FLUSH : 0) | | |
1609 | (bi_rw & REQ_DISCARD ? DP_DISCARD : 0); | |
1610 | else | |
721a9602 | 1611 | return bi_rw & REQ_SYNC ? DP_RW_SYNC : 0; |
76d2e7ec PR |
1612 | } |
1613 | ||
2f632aeb LE |
1614 | /* Used to send write or TRIM aka REQ_DISCARD requests |
1615 | * R_PRIMARY -> Peer (P_DATA, P_TRIM) | |
b411b363 | 1616 | */ |
69a22773 | 1617 | int drbd_send_dblock(struct drbd_peer_device *peer_device, struct drbd_request *req) |
b411b363 | 1618 | { |
69a22773 | 1619 | struct drbd_device *device = peer_device->device; |
9f5bdc33 AG |
1620 | struct drbd_socket *sock; |
1621 | struct p_data *p; | |
b411b363 | 1622 | unsigned int dp_flags = 0; |
b411b363 | 1623 | int dgs; |
9f5bdc33 | 1624 | int err; |
b411b363 | 1625 | |
69a22773 AG |
1626 | sock = &peer_device->connection->data; |
1627 | p = drbd_prepare_command(peer_device, sock); | |
1628 | dgs = peer_device->connection->integrity_tfm ? | |
1629 | crypto_hash_digestsize(peer_device->connection->integrity_tfm) : 0; | |
b411b363 | 1630 | |
9f5bdc33 AG |
1631 | if (!p) |
1632 | return -EIO; | |
1633 | p->sector = cpu_to_be64(req->i.sector); | |
1634 | p->block_id = (unsigned long)req; | |
b30ab791 | 1635 | p->seq_num = cpu_to_be32(atomic_inc_return(&device->packet_seq)); |
69a22773 | 1636 | dp_flags = bio_flags_to_wire(peer_device->connection, req->master_bio->bi_rw); |
b30ab791 AG |
1637 | if (device->state.conn >= C_SYNC_SOURCE && |
1638 | device->state.conn <= C_PAUSED_SYNC_T) | |
b411b363 | 1639 | dp_flags |= DP_MAY_SET_IN_SYNC; |
69a22773 | 1640 | if (peer_device->connection->agreed_pro_version >= 100) { |
303d1448 PR |
1641 | if (req->rq_state & RQ_EXP_RECEIVE_ACK) |
1642 | dp_flags |= DP_SEND_RECEIVE_ACK; | |
1643 | if (req->rq_state & RQ_EXP_WRITE_ACK) | |
1644 | dp_flags |= DP_SEND_WRITE_ACK; | |
1645 | } | |
9f5bdc33 | 1646 | p->dp_flags = cpu_to_be32(dp_flags); |
2f632aeb LE |
1647 | |
1648 | if (dp_flags & DP_DISCARD) { | |
1649 | struct p_trim *t = (struct p_trim*)p; | |
1650 | t->size = cpu_to_be32(req->i.size); | |
1651 | err = __send_command(peer_device->connection, device->vnr, sock, P_TRIM, sizeof(*t), NULL, 0); | |
1652 | goto out; | |
1653 | } | |
1654 | ||
1655 | /* our digest is still only over the payload. | |
1656 | * TRIM does not carry any payload. */ | |
9f5bdc33 | 1657 | if (dgs) |
69a22773 AG |
1658 | drbd_csum_bio(peer_device->connection->integrity_tfm, req->master_bio, p + 1); |
1659 | err = __send_command(peer_device->connection, device->vnr, sock, P_DATA, sizeof(*p) + dgs, NULL, req->i.size); | |
6bdb9b0e | 1660 | if (!err) { |
470be44a LE |
1661 | /* For protocol A, we have to memcpy the payload into |
1662 | * socket buffers, as we may complete right away | |
1663 | * as soon as we handed it over to tcp, at which point the data | |
1664 | * pages may become invalid. | |
1665 | * | |
1666 | * For data-integrity enabled, we copy it as well, so we can be | |
1667 | * sure that even if the bio pages may still be modified, it | |
1668 | * won't change the data on the wire, thus if the digest checks | |
1669 | * out ok after sending on this side, but does not fit on the | |
1670 | * receiving side, we sure have detected corruption elsewhere. | |
1671 | */ | |
303d1448 | 1672 | if (!(req->rq_state & (RQ_EXP_RECEIVE_ACK | RQ_EXP_WRITE_ACK)) || dgs) |
69a22773 | 1673 | err = _drbd_send_bio(peer_device, req->master_bio); |
b411b363 | 1674 | else |
69a22773 | 1675 | err = _drbd_send_zc_bio(peer_device, req->master_bio); |
470be44a LE |
1676 | |
1677 | /* double check digest, sometimes buffers have been modified in flight. */ | |
1678 | if (dgs > 0 && dgs <= 64) { | |
24c4830c | 1679 | /* 64 byte, 512 bit, is the largest digest size |
470be44a LE |
1680 | * currently supported in kernel crypto. */ |
1681 | unsigned char digest[64]; | |
69a22773 | 1682 | drbd_csum_bio(peer_device->connection->integrity_tfm, req->master_bio, digest); |
9f5bdc33 | 1683 | if (memcmp(p + 1, digest, dgs)) { |
d0180171 | 1684 | drbd_warn(device, |
470be44a | 1685 | "Digest mismatch, buffer modified by upper layers during write: %llus +%u\n", |
ace652ac | 1686 | (unsigned long long)req->i.sector, req->i.size); |
470be44a LE |
1687 | } |
1688 | } /* else if (dgs > 64) { | |
1689 | ... Be noisy about digest too large ... | |
1690 | } */ | |
b411b363 | 1691 | } |
2f632aeb | 1692 | out: |
9f5bdc33 | 1693 | mutex_unlock(&sock->mutex); /* locked by drbd_prepare_command() */ |
b411b363 | 1694 | |
6bdb9b0e | 1695 | return err; |
b411b363 PR |
1696 | } |
1697 | ||
1698 | /* answer packet, used to send data back for read requests: | |
1699 | * Peer -> (diskless) R_PRIMARY (P_DATA_REPLY) | |
1700 | * C_SYNC_SOURCE -> C_SYNC_TARGET (P_RS_DATA_REPLY) | |
1701 | */ | |
69a22773 | 1702 | int drbd_send_block(struct drbd_peer_device *peer_device, enum drbd_packet cmd, |
db830c46 | 1703 | struct drbd_peer_request *peer_req) |
b411b363 | 1704 | { |
69a22773 | 1705 | struct drbd_device *device = peer_device->device; |
9f5bdc33 AG |
1706 | struct drbd_socket *sock; |
1707 | struct p_data *p; | |
7b57b89d | 1708 | int err; |
b411b363 PR |
1709 | int dgs; |
1710 | ||
69a22773 AG |
1711 | sock = &peer_device->connection->data; |
1712 | p = drbd_prepare_command(peer_device, sock); | |
b411b363 | 1713 | |
69a22773 AG |
1714 | dgs = peer_device->connection->integrity_tfm ? |
1715 | crypto_hash_digestsize(peer_device->connection->integrity_tfm) : 0; | |
b411b363 | 1716 | |
9f5bdc33 AG |
1717 | if (!p) |
1718 | return -EIO; | |
1719 | p->sector = cpu_to_be64(peer_req->i.sector); | |
1720 | p->block_id = peer_req->block_id; | |
1721 | p->seq_num = 0; /* unused */ | |
b17f33cb | 1722 | p->dp_flags = 0; |
9f5bdc33 | 1723 | if (dgs) |
69a22773 AG |
1724 | drbd_csum_ee(peer_device->connection->integrity_tfm, peer_req, p + 1); |
1725 | err = __send_command(peer_device->connection, device->vnr, sock, cmd, sizeof(*p) + dgs, NULL, peer_req->i.size); | |
7b57b89d | 1726 | if (!err) |
69a22773 | 1727 | err = _drbd_send_zc_ee(peer_device, peer_req); |
9f5bdc33 | 1728 | mutex_unlock(&sock->mutex); /* locked by drbd_prepare_command() */ |
bd26bfc5 | 1729 | |
7b57b89d | 1730 | return err; |
b411b363 PR |
1731 | } |
1732 | ||
69a22773 | 1733 | int drbd_send_out_of_sync(struct drbd_peer_device *peer_device, struct drbd_request *req) |
73a01a18 | 1734 | { |
9f5bdc33 AG |
1735 | struct drbd_socket *sock; |
1736 | struct p_block_desc *p; | |
73a01a18 | 1737 | |
69a22773 AG |
1738 | sock = &peer_device->connection->data; |
1739 | p = drbd_prepare_command(peer_device, sock); | |
9f5bdc33 AG |
1740 | if (!p) |
1741 | return -EIO; | |
1742 | p->sector = cpu_to_be64(req->i.sector); | |
1743 | p->blksize = cpu_to_be32(req->i.size); | |
69a22773 | 1744 | return drbd_send_command(peer_device, sock, P_OUT_OF_SYNC, sizeof(*p), NULL, 0); |
73a01a18 PR |
1745 | } |
1746 | ||
b411b363 PR |
1747 | /* |
1748 | drbd_send distinguishes two cases: | |
1749 | ||
1750 | Packets sent via the data socket "sock" | |
1751 | and packets sent via the meta data socket "msock" | |
1752 | ||
1753 | sock msock | |
1754 | -----------------+-------------------------+------------------------------ | |
1755 | timeout conf.timeout / 2 conf.timeout / 2 | |
1756 | timeout action send a ping via msock Abort communication | |
1757 | and close all sockets | |
1758 | */ | |
1759 | ||
1760 | /* | |
1761 | * you must have down()ed the appropriate [m]sock_mutex elsewhere! | |
1762 | */ | |
bde89a9e | 1763 | int drbd_send(struct drbd_connection *connection, struct socket *sock, |
b411b363 PR |
1764 | void *buf, size_t size, unsigned msg_flags) |
1765 | { | |
1766 | struct kvec iov; | |
1767 | struct msghdr msg; | |
1768 | int rv, sent = 0; | |
1769 | ||
1770 | if (!sock) | |
c0d42c8e | 1771 | return -EBADR; |
b411b363 PR |
1772 | |
1773 | /* THINK if (signal_pending) return ... ? */ | |
1774 | ||
1775 | iov.iov_base = buf; | |
1776 | iov.iov_len = size; | |
1777 | ||
1778 | msg.msg_name = NULL; | |
1779 | msg.msg_namelen = 0; | |
1780 | msg.msg_control = NULL; | |
1781 | msg.msg_controllen = 0; | |
1782 | msg.msg_flags = msg_flags | MSG_NOSIGNAL; | |
1783 | ||
bde89a9e | 1784 | if (sock == connection->data.socket) { |
44ed167d | 1785 | rcu_read_lock(); |
bde89a9e | 1786 | connection->ko_count = rcu_dereference(connection->net_conf)->ko_count; |
44ed167d | 1787 | rcu_read_unlock(); |
bde89a9e | 1788 | drbd_update_congested(connection); |
b411b363 PR |
1789 | } |
1790 | do { | |
1791 | /* STRANGE | |
1792 | * tcp_sendmsg does _not_ use its size parameter at all ? | |
1793 | * | |
1794 | * -EAGAIN on timeout, -EINTR on signal. | |
1795 | */ | |
1796 | /* THINK | |
1797 | * do we need to block DRBD_SIG if sock == &meta.socket ?? | |
1798 | * otherwise wake_asender() might interrupt some send_*Ack ! | |
1799 | */ | |
1800 | rv = kernel_sendmsg(sock, &msg, &iov, 1, size); | |
1801 | if (rv == -EAGAIN) { | |
bde89a9e | 1802 | if (we_should_drop_the_connection(connection, sock)) |
b411b363 PR |
1803 | break; |
1804 | else | |
1805 | continue; | |
1806 | } | |
b411b363 PR |
1807 | if (rv == -EINTR) { |
1808 | flush_signals(current); | |
1809 | rv = 0; | |
1810 | } | |
1811 | if (rv < 0) | |
1812 | break; | |
1813 | sent += rv; | |
1814 | iov.iov_base += rv; | |
1815 | iov.iov_len -= rv; | |
1816 | } while (sent < size); | |
1817 | ||
bde89a9e AG |
1818 | if (sock == connection->data.socket) |
1819 | clear_bit(NET_CONGESTED, &connection->flags); | |
b411b363 PR |
1820 | |
1821 | if (rv <= 0) { | |
1822 | if (rv != -EAGAIN) { | |
1ec861eb | 1823 | drbd_err(connection, "%s_sendmsg returned %d\n", |
bde89a9e | 1824 | sock == connection->meta.socket ? "msock" : "sock", |
bedbd2a5 | 1825 | rv); |
bde89a9e | 1826 | conn_request_state(connection, NS(conn, C_BROKEN_PIPE), CS_HARD); |
b411b363 | 1827 | } else |
bde89a9e | 1828 | conn_request_state(connection, NS(conn, C_TIMEOUT), CS_HARD); |
b411b363 PR |
1829 | } |
1830 | ||
1831 | return sent; | |
1832 | } | |
1833 | ||
fb708e40 AG |
1834 | /** |
1835 | * drbd_send_all - Send an entire buffer | |
1836 | * | |
1837 | * Returns 0 upon success and a negative error value otherwise. | |
1838 | */ | |
bde89a9e | 1839 | int drbd_send_all(struct drbd_connection *connection, struct socket *sock, void *buffer, |
fb708e40 AG |
1840 | size_t size, unsigned msg_flags) |
1841 | { | |
1842 | int err; | |
1843 | ||
bde89a9e | 1844 | err = drbd_send(connection, sock, buffer, size, msg_flags); |
fb708e40 AG |
1845 | if (err < 0) |
1846 | return err; | |
1847 | if (err != size) | |
1848 | return -EIO; | |
1849 | return 0; | |
1850 | } | |
1851 | ||
b411b363 PR |
1852 | static int drbd_open(struct block_device *bdev, fmode_t mode) |
1853 | { | |
b30ab791 | 1854 | struct drbd_device *device = bdev->bd_disk->private_data; |
b411b363 PR |
1855 | unsigned long flags; |
1856 | int rv = 0; | |
1857 | ||
2a48fc0a | 1858 | mutex_lock(&drbd_main_mutex); |
0500813f | 1859 | spin_lock_irqsave(&device->resource->req_lock, flags); |
b30ab791 | 1860 | /* to have a stable device->state.role |
b411b363 PR |
1861 | * and no race with updating open_cnt */ |
1862 | ||
b30ab791 | 1863 | if (device->state.role != R_PRIMARY) { |
b411b363 PR |
1864 | if (mode & FMODE_WRITE) |
1865 | rv = -EROFS; | |
1866 | else if (!allow_oos) | |
1867 | rv = -EMEDIUMTYPE; | |
1868 | } | |
1869 | ||
1870 | if (!rv) | |
b30ab791 | 1871 | device->open_cnt++; |
0500813f | 1872 | spin_unlock_irqrestore(&device->resource->req_lock, flags); |
2a48fc0a | 1873 | mutex_unlock(&drbd_main_mutex); |
b411b363 PR |
1874 | |
1875 | return rv; | |
1876 | } | |
1877 | ||
db2a144b | 1878 | static void drbd_release(struct gendisk *gd, fmode_t mode) |
b411b363 | 1879 | { |
b30ab791 | 1880 | struct drbd_device *device = gd->private_data; |
2a48fc0a | 1881 | mutex_lock(&drbd_main_mutex); |
b30ab791 | 1882 | device->open_cnt--; |
2a48fc0a | 1883 | mutex_unlock(&drbd_main_mutex); |
b411b363 PR |
1884 | } |
1885 | ||
b30ab791 | 1886 | static void drbd_set_defaults(struct drbd_device *device) |
b411b363 | 1887 | { |
f399002e LE |
1888 | /* Beware! The actual layout differs |
1889 | * between big endian and little endian */ | |
b30ab791 | 1890 | device->state = (union drbd_dev_state) { |
b411b363 PR |
1891 | { .role = R_SECONDARY, |
1892 | .peer = R_UNKNOWN, | |
1893 | .conn = C_STANDALONE, | |
1894 | .disk = D_DISKLESS, | |
1895 | .pdsk = D_UNKNOWN, | |
b411b363 PR |
1896 | } }; |
1897 | } | |
1898 | ||
b30ab791 | 1899 | void drbd_init_set_defaults(struct drbd_device *device) |
b411b363 PR |
1900 | { |
1901 | /* the memset(,0,) did most of this. | |
1902 | * note: only assignments, no allocation in here */ | |
1903 | ||
b30ab791 AG |
1904 | drbd_set_defaults(device); |
1905 | ||
1906 | atomic_set(&device->ap_bio_cnt, 0); | |
1907 | atomic_set(&device->ap_pending_cnt, 0); | |
1908 | atomic_set(&device->rs_pending_cnt, 0); | |
1909 | atomic_set(&device->unacked_cnt, 0); | |
1910 | atomic_set(&device->local_cnt, 0); | |
1911 | atomic_set(&device->pp_in_use_by_net, 0); | |
1912 | atomic_set(&device->rs_sect_in, 0); | |
1913 | atomic_set(&device->rs_sect_ev, 0); | |
1914 | atomic_set(&device->ap_in_flight, 0); | |
1915 | atomic_set(&device->md_io_in_use, 0); | |
1916 | ||
1917 | mutex_init(&device->own_state_mutex); | |
1918 | device->state_mutex = &device->own_state_mutex; | |
1919 | ||
1920 | spin_lock_init(&device->al_lock); | |
1921 | spin_lock_init(&device->peer_seq_lock); | |
1922 | ||
1923 | INIT_LIST_HEAD(&device->active_ee); | |
1924 | INIT_LIST_HEAD(&device->sync_ee); | |
1925 | INIT_LIST_HEAD(&device->done_ee); | |
1926 | INIT_LIST_HEAD(&device->read_ee); | |
1927 | INIT_LIST_HEAD(&device->net_ee); | |
1928 | INIT_LIST_HEAD(&device->resync_reads); | |
1929 | INIT_LIST_HEAD(&device->resync_work.list); | |
1930 | INIT_LIST_HEAD(&device->unplug_work.list); | |
b30ab791 AG |
1931 | INIT_LIST_HEAD(&device->md_sync_work.list); |
1932 | INIT_LIST_HEAD(&device->start_resync_work.list); | |
1933 | INIT_LIST_HEAD(&device->bm_io_work.w.list); | |
1934 | ||
1935 | device->resync_work.cb = w_resync_timer; | |
1936 | device->unplug_work.cb = w_send_write_hint; | |
b30ab791 AG |
1937 | device->md_sync_work.cb = w_md_sync; |
1938 | device->bm_io_work.w.cb = w_bitmap_io; | |
1939 | device->start_resync_work.cb = w_start_resync; | |
1940 | ||
b30ab791 AG |
1941 | init_timer(&device->resync_timer); |
1942 | init_timer(&device->md_sync_timer); | |
1943 | init_timer(&device->start_resync_timer); | |
1944 | init_timer(&device->request_timer); | |
1945 | device->resync_timer.function = resync_timer_fn; | |
1946 | device->resync_timer.data = (unsigned long) device; | |
1947 | device->md_sync_timer.function = md_sync_timer_fn; | |
1948 | device->md_sync_timer.data = (unsigned long) device; | |
1949 | device->start_resync_timer.function = start_resync_timer_fn; | |
1950 | device->start_resync_timer.data = (unsigned long) device; | |
1951 | device->request_timer.function = request_timer_fn; | |
1952 | device->request_timer.data = (unsigned long) device; | |
1953 | ||
1954 | init_waitqueue_head(&device->misc_wait); | |
1955 | init_waitqueue_head(&device->state_wait); | |
1956 | init_waitqueue_head(&device->ee_wait); | |
1957 | init_waitqueue_head(&device->al_wait); | |
1958 | init_waitqueue_head(&device->seq_wait); | |
1959 | ||
1960 | device->resync_wenr = LC_FREE; | |
1961 | device->peer_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE; | |
1962 | device->local_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE; | |
1963 | } | |
1964 | ||
1965 | void drbd_device_cleanup(struct drbd_device *device) | |
b411b363 | 1966 | { |
1d7734a0 | 1967 | int i; |
a6b32bc3 | 1968 | if (first_peer_device(device)->connection->receiver.t_state != NONE) |
d0180171 | 1969 | drbd_err(device, "ASSERT FAILED: receiver t_state == %d expected 0.\n", |
a6b32bc3 | 1970 | first_peer_device(device)->connection->receiver.t_state); |
b30ab791 AG |
1971 | |
1972 | device->al_writ_cnt = | |
1973 | device->bm_writ_cnt = | |
1974 | device->read_cnt = | |
1975 | device->recv_cnt = | |
1976 | device->send_cnt = | |
1977 | device->writ_cnt = | |
1978 | device->p_size = | |
1979 | device->rs_start = | |
1980 | device->rs_total = | |
1981 | device->rs_failed = 0; | |
1982 | device->rs_last_events = 0; | |
1983 | device->rs_last_sect_ev = 0; | |
1d7734a0 | 1984 | for (i = 0; i < DRBD_SYNC_MARKS; i++) { |
b30ab791 AG |
1985 | device->rs_mark_left[i] = 0; |
1986 | device->rs_mark_time[i] = 0; | |
1d7734a0 | 1987 | } |
0b0ba1ef | 1988 | D_ASSERT(device, first_peer_device(device)->connection->net_conf == NULL); |
b411b363 | 1989 | |
b30ab791 AG |
1990 | drbd_set_my_capacity(device, 0); |
1991 | if (device->bitmap) { | |
b411b363 | 1992 | /* maybe never allocated. */ |
b30ab791 AG |
1993 | drbd_bm_resize(device, 0, 1); |
1994 | drbd_bm_cleanup(device); | |
b411b363 PR |
1995 | } |
1996 | ||
28995af5 | 1997 | drbd_free_ldev(device->ldev); |
b30ab791 | 1998 | device->ldev = NULL; |
1d041225 | 1999 | |
b30ab791 | 2000 | clear_bit(AL_SUSPENDED, &device->flags); |
b411b363 | 2001 | |
0b0ba1ef AG |
2002 | D_ASSERT(device, list_empty(&device->active_ee)); |
2003 | D_ASSERT(device, list_empty(&device->sync_ee)); | |
2004 | D_ASSERT(device, list_empty(&device->done_ee)); | |
2005 | D_ASSERT(device, list_empty(&device->read_ee)); | |
2006 | D_ASSERT(device, list_empty(&device->net_ee)); | |
2007 | D_ASSERT(device, list_empty(&device->resync_reads)); | |
2008 | D_ASSERT(device, list_empty(&first_peer_device(device)->connection->sender_work.q)); | |
2009 | D_ASSERT(device, list_empty(&device->resync_work.list)); | |
2010 | D_ASSERT(device, list_empty(&device->unplug_work.list)); | |
2265b473 | 2011 | |
b30ab791 | 2012 | drbd_set_defaults(device); |
b411b363 PR |
2013 | } |
2014 | ||
2015 | ||
2016 | static void drbd_destroy_mempools(void) | |
2017 | { | |
2018 | struct page *page; | |
2019 | ||
2020 | while (drbd_pp_pool) { | |
2021 | page = drbd_pp_pool; | |
2022 | drbd_pp_pool = (struct page *)page_private(page); | |
2023 | __free_page(page); | |
2024 | drbd_pp_vacant--; | |
2025 | } | |
2026 | ||
0b0ba1ef | 2027 | /* D_ASSERT(device, atomic_read(&drbd_pp_vacant)==0); */ |
b411b363 | 2028 | |
9476f39d LE |
2029 | if (drbd_md_io_bio_set) |
2030 | bioset_free(drbd_md_io_bio_set); | |
4281808f LE |
2031 | if (drbd_md_io_page_pool) |
2032 | mempool_destroy(drbd_md_io_page_pool); | |
b411b363 PR |
2033 | if (drbd_ee_mempool) |
2034 | mempool_destroy(drbd_ee_mempool); | |
2035 | if (drbd_request_mempool) | |
2036 | mempool_destroy(drbd_request_mempool); | |
2037 | if (drbd_ee_cache) | |
2038 | kmem_cache_destroy(drbd_ee_cache); | |
2039 | if (drbd_request_cache) | |
2040 | kmem_cache_destroy(drbd_request_cache); | |
2041 | if (drbd_bm_ext_cache) | |
2042 | kmem_cache_destroy(drbd_bm_ext_cache); | |
2043 | if (drbd_al_ext_cache) | |
2044 | kmem_cache_destroy(drbd_al_ext_cache); | |
2045 | ||
9476f39d | 2046 | drbd_md_io_bio_set = NULL; |
4281808f | 2047 | drbd_md_io_page_pool = NULL; |
b411b363 PR |
2048 | drbd_ee_mempool = NULL; |
2049 | drbd_request_mempool = NULL; | |
2050 | drbd_ee_cache = NULL; | |
2051 | drbd_request_cache = NULL; | |
2052 | drbd_bm_ext_cache = NULL; | |
2053 | drbd_al_ext_cache = NULL; | |
2054 | ||
2055 | return; | |
2056 | } | |
2057 | ||
2058 | static int drbd_create_mempools(void) | |
2059 | { | |
2060 | struct page *page; | |
1816a2b4 | 2061 | const int number = (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * minor_count; |
b411b363 PR |
2062 | int i; |
2063 | ||
2064 | /* prepare our caches and mempools */ | |
2065 | drbd_request_mempool = NULL; | |
2066 | drbd_ee_cache = NULL; | |
2067 | drbd_request_cache = NULL; | |
2068 | drbd_bm_ext_cache = NULL; | |
2069 | drbd_al_ext_cache = NULL; | |
2070 | drbd_pp_pool = NULL; | |
4281808f | 2071 | drbd_md_io_page_pool = NULL; |
9476f39d | 2072 | drbd_md_io_bio_set = NULL; |
b411b363 PR |
2073 | |
2074 | /* caches */ | |
2075 | drbd_request_cache = kmem_cache_create( | |
2076 | "drbd_req", sizeof(struct drbd_request), 0, 0, NULL); | |
2077 | if (drbd_request_cache == NULL) | |
2078 | goto Enomem; | |
2079 | ||
2080 | drbd_ee_cache = kmem_cache_create( | |
f6ffca9f | 2081 | "drbd_ee", sizeof(struct drbd_peer_request), 0, 0, NULL); |
b411b363 PR |
2082 | if (drbd_ee_cache == NULL) |
2083 | goto Enomem; | |
2084 | ||
2085 | drbd_bm_ext_cache = kmem_cache_create( | |
2086 | "drbd_bm", sizeof(struct bm_extent), 0, 0, NULL); | |
2087 | if (drbd_bm_ext_cache == NULL) | |
2088 | goto Enomem; | |
2089 | ||
2090 | drbd_al_ext_cache = kmem_cache_create( | |
2091 | "drbd_al", sizeof(struct lc_element), 0, 0, NULL); | |
2092 | if (drbd_al_ext_cache == NULL) | |
2093 | goto Enomem; | |
2094 | ||
2095 | /* mempools */ | |
9476f39d LE |
2096 | drbd_md_io_bio_set = bioset_create(DRBD_MIN_POOL_PAGES, 0); |
2097 | if (drbd_md_io_bio_set == NULL) | |
2098 | goto Enomem; | |
9476f39d | 2099 | |
4281808f LE |
2100 | drbd_md_io_page_pool = mempool_create_page_pool(DRBD_MIN_POOL_PAGES, 0); |
2101 | if (drbd_md_io_page_pool == NULL) | |
2102 | goto Enomem; | |
2103 | ||
b411b363 PR |
2104 | drbd_request_mempool = mempool_create(number, |
2105 | mempool_alloc_slab, mempool_free_slab, drbd_request_cache); | |
2106 | if (drbd_request_mempool == NULL) | |
2107 | goto Enomem; | |
2108 | ||
2109 | drbd_ee_mempool = mempool_create(number, | |
2110 | mempool_alloc_slab, mempool_free_slab, drbd_ee_cache); | |
2027ae1f | 2111 | if (drbd_ee_mempool == NULL) |
b411b363 PR |
2112 | goto Enomem; |
2113 | ||
2114 | /* drbd's page pool */ | |
2115 | spin_lock_init(&drbd_pp_lock); | |
2116 | ||
2117 | for (i = 0; i < number; i++) { | |
2118 | page = alloc_page(GFP_HIGHUSER); | |
2119 | if (!page) | |
2120 | goto Enomem; | |
2121 | set_page_private(page, (unsigned long)drbd_pp_pool); | |
2122 | drbd_pp_pool = page; | |
2123 | } | |
2124 | drbd_pp_vacant = number; | |
2125 | ||
2126 | return 0; | |
2127 | ||
2128 | Enomem: | |
2129 | drbd_destroy_mempools(); /* in case we allocated some */ | |
2130 | return -ENOMEM; | |
2131 | } | |
2132 | ||
2133 | static int drbd_notify_sys(struct notifier_block *this, unsigned long code, | |
2134 | void *unused) | |
2135 | { | |
2136 | /* just so we have it. you never know what interesting things we | |
2137 | * might want to do here some day... | |
2138 | */ | |
2139 | ||
2140 | return NOTIFY_DONE; | |
2141 | } | |
2142 | ||
2143 | static struct notifier_block drbd_notifier = { | |
2144 | .notifier_call = drbd_notify_sys, | |
2145 | }; | |
2146 | ||
b30ab791 | 2147 | static void drbd_release_all_peer_reqs(struct drbd_device *device) |
b411b363 PR |
2148 | { |
2149 | int rr; | |
2150 | ||
b30ab791 | 2151 | rr = drbd_free_peer_reqs(device, &device->active_ee); |
b411b363 | 2152 | if (rr) |
d0180171 | 2153 | drbd_err(device, "%d EEs in active list found!\n", rr); |
b411b363 | 2154 | |
b30ab791 | 2155 | rr = drbd_free_peer_reqs(device, &device->sync_ee); |
b411b363 | 2156 | if (rr) |
d0180171 | 2157 | drbd_err(device, "%d EEs in sync list found!\n", rr); |
b411b363 | 2158 | |
b30ab791 | 2159 | rr = drbd_free_peer_reqs(device, &device->read_ee); |
b411b363 | 2160 | if (rr) |
d0180171 | 2161 | drbd_err(device, "%d EEs in read list found!\n", rr); |
b411b363 | 2162 | |
b30ab791 | 2163 | rr = drbd_free_peer_reqs(device, &device->done_ee); |
b411b363 | 2164 | if (rr) |
d0180171 | 2165 | drbd_err(device, "%d EEs in done list found!\n", rr); |
b411b363 | 2166 | |
b30ab791 | 2167 | rr = drbd_free_peer_reqs(device, &device->net_ee); |
b411b363 | 2168 | if (rr) |
d0180171 | 2169 | drbd_err(device, "%d EEs in net list found!\n", rr); |
b411b363 PR |
2170 | } |
2171 | ||
774b3055 | 2172 | /* caution. no locking. */ |
05a10ec7 | 2173 | void drbd_destroy_device(struct kref *kref) |
b411b363 | 2174 | { |
b30ab791 | 2175 | struct drbd_device *device = container_of(kref, struct drbd_device, kref); |
803ea134 AG |
2176 | struct drbd_resource *resource = device->resource; |
2177 | struct drbd_connection *connection; | |
b411b363 | 2178 | |
b30ab791 | 2179 | del_timer_sync(&device->request_timer); |
dfa8bedb | 2180 | |
b411b363 | 2181 | /* paranoia asserts */ |
0b0ba1ef | 2182 | D_ASSERT(device, device->open_cnt == 0); |
b411b363 PR |
2183 | /* end paranoia asserts */ |
2184 | ||
b411b363 PR |
2185 | /* cleanup stuff that may have been allocated during |
2186 | * device (re-)configuration or state changes */ | |
2187 | ||
b30ab791 AG |
2188 | if (device->this_bdev) |
2189 | bdput(device->this_bdev); | |
b411b363 | 2190 | |
28995af5 | 2191 | drbd_free_ldev(device->ldev); |
b30ab791 | 2192 | device->ldev = NULL; |
b411b363 | 2193 | |
b30ab791 | 2194 | drbd_release_all_peer_reqs(device); |
b411b363 | 2195 | |
b30ab791 AG |
2196 | lc_destroy(device->act_log); |
2197 | lc_destroy(device->resync); | |
b411b363 | 2198 | |
b30ab791 AG |
2199 | kfree(device->p_uuid); |
2200 | /* device->p_uuid = NULL; */ | |
b411b363 | 2201 | |
b30ab791 AG |
2202 | if (device->bitmap) /* should no longer be there. */ |
2203 | drbd_bm_cleanup(device); | |
2204 | __free_page(device->md_io_page); | |
2205 | put_disk(device->vdisk); | |
2206 | blk_cleanup_queue(device->rq_queue); | |
2207 | kfree(device->rs_plan_s); | |
a6b32bc3 | 2208 | kfree(first_peer_device(device)); |
b30ab791 | 2209 | kfree(device); |
9dc9fbb3 | 2210 | |
803ea134 AG |
2211 | for_each_connection(connection, resource) |
2212 | kref_put(&connection->kref, drbd_destroy_connection); | |
2213 | kref_put(&resource->kref, drbd_destroy_resource); | |
b411b363 PR |
2214 | } |
2215 | ||
2312f0b3 LE |
2216 | /* One global retry thread, if we need to push back some bio and have it |
2217 | * reinserted through our make request function. | |
2218 | */ | |
2219 | static struct retry_worker { | |
2220 | struct workqueue_struct *wq; | |
2221 | struct work_struct worker; | |
2222 | ||
2223 | spinlock_t lock; | |
2224 | struct list_head writes; | |
2225 | } retry; | |
2226 | ||
2227 | static void do_retry(struct work_struct *ws) | |
2228 | { | |
2229 | struct retry_worker *retry = container_of(ws, struct retry_worker, worker); | |
2230 | LIST_HEAD(writes); | |
2231 | struct drbd_request *req, *tmp; | |
2232 | ||
2233 | spin_lock_irq(&retry->lock); | |
2234 | list_splice_init(&retry->writes, &writes); | |
2235 | spin_unlock_irq(&retry->lock); | |
2236 | ||
2237 | list_for_each_entry_safe(req, tmp, &writes, tl_requests) { | |
84b8c06b | 2238 | struct drbd_device *device = req->device; |
2312f0b3 LE |
2239 | struct bio *bio = req->master_bio; |
2240 | unsigned long start_time = req->start_time; | |
9a278a79 LE |
2241 | bool expected; |
2242 | ||
84b8c06b | 2243 | expected = |
9a278a79 LE |
2244 | expect(atomic_read(&req->completion_ref) == 0) && |
2245 | expect(req->rq_state & RQ_POSTPONED) && | |
2246 | expect((req->rq_state & RQ_LOCAL_PENDING) == 0 || | |
2247 | (req->rq_state & RQ_LOCAL_ABORTED) != 0); | |
2248 | ||
2249 | if (!expected) | |
d0180171 | 2250 | drbd_err(device, "req=%p completion_ref=%d rq_state=%x\n", |
9a278a79 LE |
2251 | req, atomic_read(&req->completion_ref), |
2252 | req->rq_state); | |
2253 | ||
2254 | /* We still need to put one kref associated with the | |
2255 | * "completion_ref" going zero in the code path that queued it | |
2256 | * here. The request object may still be referenced by a | |
2257 | * frozen local req->private_bio, in case we force-detached. | |
2312f0b3 | 2258 | */ |
9a278a79 | 2259 | kref_put(&req->kref, drbd_req_destroy); |
2312f0b3 LE |
2260 | |
2261 | /* A single suspended or otherwise blocking device may stall | |
2262 | * all others as well. Fortunately, this code path is to | |
2263 | * recover from a situation that "should not happen": | |
2264 | * concurrent writes in multi-primary setup. | |
2265 | * In a "normal" lifecycle, this workqueue is supposed to be | |
2266 | * destroyed without ever doing anything. | |
2267 | * If it turns out to be an issue anyways, we can do per | |
2268 | * resource (replication group) or per device (minor) retry | |
2269 | * workqueues instead. | |
2270 | */ | |
2271 | ||
2272 | /* We are not just doing generic_make_request(), | |
2273 | * as we want to keep the start_time information. */ | |
b30ab791 AG |
2274 | inc_ap_bio(device); |
2275 | __drbd_make_request(device, bio, start_time); | |
2312f0b3 LE |
2276 | } |
2277 | } | |
2278 | ||
9d05e7c4 | 2279 | void drbd_restart_request(struct drbd_request *req) |
2312f0b3 LE |
2280 | { |
2281 | unsigned long flags; | |
2282 | spin_lock_irqsave(&retry.lock, flags); | |
2283 | list_move_tail(&req->tl_requests, &retry.writes); | |
2284 | spin_unlock_irqrestore(&retry.lock, flags); | |
2285 | ||
2286 | /* Drop the extra reference that would otherwise | |
2287 | * have been dropped by complete_master_bio. | |
2288 | * do_retry() needs to grab a new one. */ | |
84b8c06b | 2289 | dec_ap_bio(req->device); |
b411b363 | 2290 | |
2312f0b3 | 2291 | queue_work(retry.wq, &retry.worker); |
b411b363 PR |
2292 | } |
2293 | ||
77c556f6 AG |
2294 | void drbd_destroy_resource(struct kref *kref) |
2295 | { | |
2296 | struct drbd_resource *resource = | |
2297 | container_of(kref, struct drbd_resource, kref); | |
2298 | ||
803ea134 | 2299 | idr_destroy(&resource->devices); |
625a6ba2 | 2300 | free_cpumask_var(resource->cpu_mask); |
77c556f6 AG |
2301 | kfree(resource->name); |
2302 | kfree(resource); | |
2303 | } | |
2304 | ||
2305 | void drbd_free_resource(struct drbd_resource *resource) | |
2306 | { | |
2307 | struct drbd_connection *connection, *tmp; | |
2308 | ||
2309 | for_each_connection_safe(connection, tmp, resource) { | |
2310 | list_del(&connection->connections); | |
2311 | kref_put(&connection->kref, drbd_destroy_connection); | |
2312 | } | |
2313 | kref_put(&resource->kref, drbd_destroy_resource); | |
2314 | } | |
2312f0b3 | 2315 | |
b411b363 PR |
2316 | static void drbd_cleanup(void) |
2317 | { | |
2318 | unsigned int i; | |
b30ab791 | 2319 | struct drbd_device *device; |
77c556f6 | 2320 | struct drbd_resource *resource, *tmp; |
b411b363 PR |
2321 | |
2322 | unregister_reboot_notifier(&drbd_notifier); | |
2323 | ||
17a93f30 LE |
2324 | /* first remove proc, |
2325 | * drbdsetup uses it's presence to detect | |
2326 | * whether DRBD is loaded. | |
2327 | * If we would get stuck in proc removal, | |
2328 | * but have netlink already deregistered, | |
2329 | * some drbdsetup commands may wait forever | |
2330 | * for an answer. | |
2331 | */ | |
2332 | if (drbd_proc) | |
2333 | remove_proc_entry("drbd", NULL); | |
2334 | ||
2312f0b3 LE |
2335 | if (retry.wq) |
2336 | destroy_workqueue(retry.wq); | |
b411b363 | 2337 | |
3b98c0c2 | 2338 | drbd_genl_unregister(); |
b411b363 | 2339 | |
803ea134 | 2340 | idr_for_each_entry(&drbd_devices, device, i) |
f82795d6 | 2341 | drbd_delete_device(device); |
b411b363 | 2342 | |
c141ebda | 2343 | /* not _rcu since, no other updater anymore. Genl already unregistered */ |
77c556f6 AG |
2344 | for_each_resource_safe(resource, tmp, &drbd_resources) { |
2345 | list_del(&resource->resources); | |
2346 | drbd_free_resource(resource); | |
81fa2e67 | 2347 | } |
b411b363 | 2348 | |
81a5d60e | 2349 | drbd_destroy_mempools(); |
b411b363 PR |
2350 | unregister_blkdev(DRBD_MAJOR, "drbd"); |
2351 | ||
05a10ec7 | 2352 | idr_destroy(&drbd_devices); |
81a5d60e | 2353 | |
b411b363 PR |
2354 | printk(KERN_INFO "drbd: module cleanup done.\n"); |
2355 | } | |
2356 | ||
2357 | /** | |
d97482ed | 2358 | * drbd_congested() - Callback for the flusher thread |
b411b363 | 2359 | * @congested_data: User data |
d97482ed | 2360 | * @bdi_bits: Bits the BDI flusher thread is currently interested in |
b411b363 PR |
2361 | * |
2362 | * Returns 1<<BDI_async_congested and/or 1<<BDI_sync_congested if we are congested. | |
2363 | */ | |
2364 | static int drbd_congested(void *congested_data, int bdi_bits) | |
2365 | { | |
b30ab791 | 2366 | struct drbd_device *device = congested_data; |
b411b363 PR |
2367 | struct request_queue *q; |
2368 | char reason = '-'; | |
2369 | int r = 0; | |
2370 | ||
b30ab791 | 2371 | if (!may_inc_ap_bio(device)) { |
b411b363 PR |
2372 | /* DRBD has frozen IO */ |
2373 | r = bdi_bits; | |
2374 | reason = 'd'; | |
2375 | goto out; | |
2376 | } | |
2377 | ||
a6b32bc3 | 2378 | if (test_bit(CALLBACK_PENDING, &first_peer_device(device)->connection->flags)) { |
c2ba686f LE |
2379 | r |= (1 << BDI_async_congested); |
2380 | /* Without good local data, we would need to read from remote, | |
2381 | * and that would need the worker thread as well, which is | |
2382 | * currently blocked waiting for that usermode helper to | |
2383 | * finish. | |
2384 | */ | |
b30ab791 | 2385 | if (!get_ldev_if_state(device, D_UP_TO_DATE)) |
c2ba686f LE |
2386 | r |= (1 << BDI_sync_congested); |
2387 | else | |
b30ab791 | 2388 | put_ldev(device); |
c2ba686f LE |
2389 | r &= bdi_bits; |
2390 | reason = 'c'; | |
2391 | goto out; | |
2392 | } | |
2393 | ||
b30ab791 AG |
2394 | if (get_ldev(device)) { |
2395 | q = bdev_get_queue(device->ldev->backing_bdev); | |
b411b363 | 2396 | r = bdi_congested(&q->backing_dev_info, bdi_bits); |
b30ab791 | 2397 | put_ldev(device); |
b411b363 PR |
2398 | if (r) |
2399 | reason = 'b'; | |
2400 | } | |
2401 | ||
a6b32bc3 AG |
2402 | if (bdi_bits & (1 << BDI_async_congested) && |
2403 | test_bit(NET_CONGESTED, &first_peer_device(device)->connection->flags)) { | |
b411b363 PR |
2404 | r |= (1 << BDI_async_congested); |
2405 | reason = reason == 'b' ? 'a' : 'n'; | |
2406 | } | |
2407 | ||
2408 | out: | |
b30ab791 | 2409 | device->congestion_reason = reason; |
b411b363 PR |
2410 | return r; |
2411 | } | |
2412 | ||
6699b655 PR |
2413 | static void drbd_init_workqueue(struct drbd_work_queue* wq) |
2414 | { | |
6699b655 PR |
2415 | spin_lock_init(&wq->q_lock); |
2416 | INIT_LIST_HEAD(&wq->q); | |
8c0785a5 | 2417 | init_waitqueue_head(&wq->q_wait); |
6699b655 PR |
2418 | } |
2419 | ||
b5043c5e AG |
2420 | struct completion_work { |
2421 | struct drbd_work w; | |
2422 | struct completion done; | |
2423 | }; | |
2424 | ||
2425 | static int w_complete(struct drbd_work *w, int cancel) | |
2426 | { | |
2427 | struct completion_work *completion_work = | |
2428 | container_of(w, struct completion_work, w); | |
2429 | ||
2430 | complete(&completion_work->done); | |
2431 | return 0; | |
2432 | } | |
2433 | ||
2434 | void drbd_flush_workqueue(struct drbd_work_queue *work_queue) | |
2435 | { | |
2436 | struct completion_work completion_work; | |
2437 | ||
2438 | completion_work.w.cb = w_complete; | |
2439 | init_completion(&completion_work.done); | |
2440 | drbd_queue_work(work_queue, &completion_work.w); | |
2441 | wait_for_completion(&completion_work.done); | |
2442 | } | |
2443 | ||
4bc76048 | 2444 | struct drbd_resource *drbd_find_resource(const char *name) |
1aba4d7f | 2445 | { |
77c556f6 | 2446 | struct drbd_resource *resource; |
1aba4d7f | 2447 | |
3b98c0c2 LE |
2448 | if (!name || !name[0]) |
2449 | return NULL; | |
2450 | ||
c141ebda | 2451 | rcu_read_lock(); |
77c556f6 AG |
2452 | for_each_resource_rcu(resource, &drbd_resources) { |
2453 | if (!strcmp(resource->name, name)) { | |
4bc76048 | 2454 | kref_get(&resource->kref); |
1aba4d7f | 2455 | goto found; |
0ace9dfa | 2456 | } |
1aba4d7f | 2457 | } |
4bc76048 | 2458 | resource = NULL; |
1aba4d7f | 2459 | found: |
c141ebda | 2460 | rcu_read_unlock(); |
4bc76048 | 2461 | return resource; |
1aba4d7f PR |
2462 | } |
2463 | ||
bde89a9e | 2464 | struct drbd_connection *conn_get_by_addrs(void *my_addr, int my_addr_len, |
089c075d AG |
2465 | void *peer_addr, int peer_addr_len) |
2466 | { | |
77c556f6 | 2467 | struct drbd_resource *resource; |
bde89a9e | 2468 | struct drbd_connection *connection; |
089c075d AG |
2469 | |
2470 | rcu_read_lock(); | |
77c556f6 AG |
2471 | for_each_resource_rcu(resource, &drbd_resources) { |
2472 | for_each_connection_rcu(connection, resource) { | |
2473 | if (connection->my_addr_len == my_addr_len && | |
2474 | connection->peer_addr_len == peer_addr_len && | |
2475 | !memcmp(&connection->my_addr, my_addr, my_addr_len) && | |
2476 | !memcmp(&connection->peer_addr, peer_addr, peer_addr_len)) { | |
2477 | kref_get(&connection->kref); | |
2478 | goto found; | |
2479 | } | |
089c075d AG |
2480 | } |
2481 | } | |
bde89a9e | 2482 | connection = NULL; |
089c075d AG |
2483 | found: |
2484 | rcu_read_unlock(); | |
bde89a9e | 2485 | return connection; |
089c075d AG |
2486 | } |
2487 | ||
e6ef8a5c AG |
2488 | static int drbd_alloc_socket(struct drbd_socket *socket) |
2489 | { | |
2490 | socket->rbuf = (void *) __get_free_page(GFP_KERNEL); | |
2491 | if (!socket->rbuf) | |
2492 | return -ENOMEM; | |
5a87d920 AG |
2493 | socket->sbuf = (void *) __get_free_page(GFP_KERNEL); |
2494 | if (!socket->sbuf) | |
2495 | return -ENOMEM; | |
e6ef8a5c AG |
2496 | return 0; |
2497 | } | |
2498 | ||
2499 | static void drbd_free_socket(struct drbd_socket *socket) | |
2500 | { | |
5a87d920 | 2501 | free_page((unsigned long) socket->sbuf); |
e6ef8a5c AG |
2502 | free_page((unsigned long) socket->rbuf); |
2503 | } | |
2504 | ||
bde89a9e | 2505 | void conn_free_crypto(struct drbd_connection *connection) |
91fd4dad | 2506 | { |
bde89a9e | 2507 | drbd_free_sock(connection); |
1d041225 | 2508 | |
bde89a9e AG |
2509 | crypto_free_hash(connection->csums_tfm); |
2510 | crypto_free_hash(connection->verify_tfm); | |
2511 | crypto_free_hash(connection->cram_hmac_tfm); | |
2512 | crypto_free_hash(connection->integrity_tfm); | |
2513 | crypto_free_hash(connection->peer_integrity_tfm); | |
2514 | kfree(connection->int_dig_in); | |
2515 | kfree(connection->int_dig_vv); | |
1d041225 | 2516 | |
bde89a9e AG |
2517 | connection->csums_tfm = NULL; |
2518 | connection->verify_tfm = NULL; | |
2519 | connection->cram_hmac_tfm = NULL; | |
2520 | connection->integrity_tfm = NULL; | |
2521 | connection->peer_integrity_tfm = NULL; | |
2522 | connection->int_dig_in = NULL; | |
2523 | connection->int_dig_vv = NULL; | |
91fd4dad PR |
2524 | } |
2525 | ||
eb6bea67 | 2526 | int set_resource_options(struct drbd_resource *resource, struct res_opts *res_opts) |
afbbfa88 | 2527 | { |
eb6bea67 | 2528 | struct drbd_connection *connection; |
afbbfa88 AG |
2529 | cpumask_var_t new_cpu_mask; |
2530 | int err; | |
2531 | ||
2532 | if (!zalloc_cpumask_var(&new_cpu_mask, GFP_KERNEL)) | |
2533 | return -ENOMEM; | |
2534 | /* | |
2535 | retcode = ERR_NOMEM; | |
2536 | drbd_msg_put_info("unable to allocate cpumask"); | |
2537 | */ | |
2538 | ||
2539 | /* silently ignore cpu mask on UP kernel */ | |
2540 | if (nr_cpu_ids > 1 && res_opts->cpu_mask[0] != 0) { | |
f44d0436 | 2541 | err = bitmap_parse(res_opts->cpu_mask, DRBD_CPU_MASK_SIZE, |
c5b005ab | 2542 | cpumask_bits(new_cpu_mask), nr_cpu_ids); |
afbbfa88 | 2543 | if (err) { |
1ec861eb | 2544 | drbd_warn(resource, "bitmap_parse() failed with %d\n", err); |
afbbfa88 AG |
2545 | /* retcode = ERR_CPU_MASK_PARSE; */ |
2546 | goto fail; | |
2547 | } | |
2548 | } | |
eb6bea67 | 2549 | resource->res_opts = *res_opts; |
625a6ba2 AG |
2550 | if (cpumask_empty(new_cpu_mask)) |
2551 | drbd_calc_cpu_mask(&new_cpu_mask); | |
2552 | if (!cpumask_equal(resource->cpu_mask, new_cpu_mask)) { | |
2553 | cpumask_copy(resource->cpu_mask, new_cpu_mask); | |
2554 | for_each_connection_rcu(connection, resource) { | |
eb6bea67 AG |
2555 | connection->receiver.reset_cpu_mask = 1; |
2556 | connection->asender.reset_cpu_mask = 1; | |
2557 | connection->worker.reset_cpu_mask = 1; | |
2558 | } | |
afbbfa88 AG |
2559 | } |
2560 | err = 0; | |
2561 | ||
2562 | fail: | |
2563 | free_cpumask_var(new_cpu_mask); | |
2564 | return err; | |
2565 | ||
2566 | } | |
2567 | ||
77c556f6 AG |
2568 | struct drbd_resource *drbd_create_resource(const char *name) |
2569 | { | |
2570 | struct drbd_resource *resource; | |
2571 | ||
6bbf53ca | 2572 | resource = kzalloc(sizeof(struct drbd_resource), GFP_KERNEL); |
77c556f6 | 2573 | if (!resource) |
625a6ba2 | 2574 | goto fail; |
77c556f6 | 2575 | resource->name = kstrdup(name, GFP_KERNEL); |
625a6ba2 AG |
2576 | if (!resource->name) |
2577 | goto fail_free_resource; | |
2578 | if (!zalloc_cpumask_var(&resource->cpu_mask, GFP_KERNEL)) | |
2579 | goto fail_free_name; | |
77c556f6 | 2580 | kref_init(&resource->kref); |
803ea134 | 2581 | idr_init(&resource->devices); |
77c556f6 | 2582 | INIT_LIST_HEAD(&resource->connections); |
e9526580 | 2583 | resource->write_ordering = WO_bdev_flush; |
77c556f6 | 2584 | list_add_tail_rcu(&resource->resources, &drbd_resources); |
0500813f | 2585 | mutex_init(&resource->conf_update); |
9e276872 | 2586 | mutex_init(&resource->adm_mutex); |
0500813f | 2587 | spin_lock_init(&resource->req_lock); |
77c556f6 | 2588 | return resource; |
625a6ba2 AG |
2589 | |
2590 | fail_free_name: | |
2591 | kfree(resource->name); | |
2592 | fail_free_resource: | |
2593 | kfree(resource); | |
2594 | fail: | |
2595 | return NULL; | |
77c556f6 AG |
2596 | } |
2597 | ||
ec0bddbc | 2598 | /* caller must be under genl_lock() */ |
bde89a9e | 2599 | struct drbd_connection *conn_create(const char *name, struct res_opts *res_opts) |
2111438b | 2600 | { |
77c556f6 | 2601 | struct drbd_resource *resource; |
bde89a9e | 2602 | struct drbd_connection *connection; |
2111438b | 2603 | |
bde89a9e AG |
2604 | connection = kzalloc(sizeof(struct drbd_connection), GFP_KERNEL); |
2605 | if (!connection) | |
2111438b PR |
2606 | return NULL; |
2607 | ||
bde89a9e | 2608 | if (drbd_alloc_socket(&connection->data)) |
e6ef8a5c | 2609 | goto fail; |
bde89a9e | 2610 | if (drbd_alloc_socket(&connection->meta)) |
e6ef8a5c AG |
2611 | goto fail; |
2612 | ||
bde89a9e AG |
2613 | connection->current_epoch = kzalloc(sizeof(struct drbd_epoch), GFP_KERNEL); |
2614 | if (!connection->current_epoch) | |
12038a3a | 2615 | goto fail; |
b6dd1a89 | 2616 | |
bde89a9e | 2617 | INIT_LIST_HEAD(&connection->transfer_log); |
b6dd1a89 | 2618 | |
bde89a9e AG |
2619 | INIT_LIST_HEAD(&connection->current_epoch->list); |
2620 | connection->epochs = 1; | |
2621 | spin_lock_init(&connection->epoch_lock); | |
4b0007c0 | 2622 | |
bde89a9e AG |
2623 | connection->send.seen_any_write_yet = false; |
2624 | connection->send.current_epoch_nr = 0; | |
2625 | connection->send.current_epoch_writes = 0; | |
b6dd1a89 | 2626 | |
77c556f6 AG |
2627 | resource = drbd_create_resource(name); |
2628 | if (!resource) | |
2629 | goto fail; | |
2630 | ||
bde89a9e AG |
2631 | connection->cstate = C_STANDALONE; |
2632 | mutex_init(&connection->cstate_mutex); | |
bde89a9e | 2633 | init_waitqueue_head(&connection->ping_wait); |
c06ece6b | 2634 | idr_init(&connection->peer_devices); |
b2fb6dbe | 2635 | |
bde89a9e AG |
2636 | drbd_init_workqueue(&connection->sender_work); |
2637 | mutex_init(&connection->data.mutex); | |
2638 | mutex_init(&connection->meta.mutex); | |
6699b655 | 2639 | |
2457b6d5 AG |
2640 | drbd_thread_init(resource, &connection->receiver, drbd_receiver, "receiver"); |
2641 | connection->receiver.connection = connection; | |
2642 | drbd_thread_init(resource, &connection->worker, drbd_worker, "worker"); | |
2643 | connection->worker.connection = connection; | |
2644 | drbd_thread_init(resource, &connection->asender, drbd_asender, "asender"); | |
2645 | connection->asender.connection = connection; | |
392c8801 | 2646 | |
bde89a9e | 2647 | kref_init(&connection->kref); |
77c556f6 | 2648 | |
77c556f6 | 2649 | connection->resource = resource; |
2111438b | 2650 | |
eb6bea67 AG |
2651 | if (set_resource_options(resource, res_opts)) |
2652 | goto fail_resource; | |
2653 | ||
2654 | kref_get(&resource->kref); | |
2655 | list_add_tail_rcu(&connection->connections, &resource->connections); | |
bde89a9e | 2656 | return connection; |
2111438b | 2657 | |
eb6bea67 AG |
2658 | fail_resource: |
2659 | list_del(&resource->resources); | |
2660 | drbd_free_resource(resource); | |
2111438b | 2661 | fail: |
bde89a9e | 2662 | kfree(connection->current_epoch); |
bde89a9e AG |
2663 | drbd_free_socket(&connection->meta); |
2664 | drbd_free_socket(&connection->data); | |
bde89a9e | 2665 | kfree(connection); |
2111438b PR |
2666 | return NULL; |
2667 | } | |
2668 | ||
05a10ec7 | 2669 | void drbd_destroy_connection(struct kref *kref) |
2111438b | 2670 | { |
bde89a9e | 2671 | struct drbd_connection *connection = container_of(kref, struct drbd_connection, kref); |
77c556f6 | 2672 | struct drbd_resource *resource = connection->resource; |
9dc9fbb3 | 2673 | |
bde89a9e | 2674 | if (atomic_read(&connection->current_epoch->epoch_size) != 0) |
1ec861eb | 2675 | drbd_err(connection, "epoch_size:%d\n", atomic_read(&connection->current_epoch->epoch_size)); |
bde89a9e | 2676 | kfree(connection->current_epoch); |
12038a3a | 2677 | |
c06ece6b | 2678 | idr_destroy(&connection->peer_devices); |
2111438b | 2679 | |
bde89a9e AG |
2680 | drbd_free_socket(&connection->meta); |
2681 | drbd_free_socket(&connection->data); | |
bde89a9e AG |
2682 | kfree(connection->int_dig_in); |
2683 | kfree(connection->int_dig_vv); | |
2684 | kfree(connection); | |
77c556f6 | 2685 | kref_put(&resource->kref, drbd_destroy_resource); |
2111438b PR |
2686 | } |
2687 | ||
b30ab791 | 2688 | static int init_submitter(struct drbd_device *device) |
113fef9e LE |
2689 | { |
2690 | /* opencoded create_singlethread_workqueue(), | |
2691 | * to be able to say "drbd%d", ..., minor */ | |
b30ab791 AG |
2692 | device->submit.wq = alloc_workqueue("drbd%u_submit", |
2693 | WQ_UNBOUND | WQ_MEM_RECLAIM, 1, device->minor); | |
2694 | if (!device->submit.wq) | |
113fef9e LE |
2695 | return -ENOMEM; |
2696 | ||
b30ab791 AG |
2697 | INIT_WORK(&device->submit.worker, do_submit); |
2698 | spin_lock_init(&device->submit.lock); | |
2699 | INIT_LIST_HEAD(&device->submit.writes); | |
113fef9e LE |
2700 | return 0; |
2701 | } | |
2702 | ||
a910b123 | 2703 | enum drbd_ret_code drbd_create_device(struct drbd_config_context *adm_ctx, unsigned int minor) |
b411b363 | 2704 | { |
a910b123 | 2705 | struct drbd_resource *resource = adm_ctx->resource; |
b6f85ef9 | 2706 | struct drbd_connection *connection; |
b30ab791 | 2707 | struct drbd_device *device; |
b6f85ef9 | 2708 | struct drbd_peer_device *peer_device, *tmp_peer_device; |
b411b363 PR |
2709 | struct gendisk *disk; |
2710 | struct request_queue *q; | |
93e4bf7a | 2711 | int id; |
a910b123 | 2712 | int vnr = adm_ctx->volume; |
8432b314 | 2713 | enum drbd_ret_code err = ERR_NOMEM; |
774b3055 | 2714 | |
b30ab791 AG |
2715 | device = minor_to_device(minor); |
2716 | if (device) | |
774b3055 | 2717 | return ERR_MINOR_EXISTS; |
b411b363 PR |
2718 | |
2719 | /* GFP_KERNEL, we are outside of all write-out paths */ | |
b30ab791 AG |
2720 | device = kzalloc(sizeof(struct drbd_device), GFP_KERNEL); |
2721 | if (!device) | |
774b3055 | 2722 | return ERR_NOMEM; |
803ea134 AG |
2723 | kref_init(&device->kref); |
2724 | ||
803ea134 AG |
2725 | kref_get(&resource->kref); |
2726 | device->resource = resource; | |
b30ab791 AG |
2727 | device->minor = minor; |
2728 | device->vnr = vnr; | |
b411b363 | 2729 | |
b30ab791 | 2730 | drbd_init_set_defaults(device); |
b411b363 PR |
2731 | |
2732 | q = blk_alloc_queue(GFP_KERNEL); | |
2733 | if (!q) | |
2734 | goto out_no_q; | |
b30ab791 AG |
2735 | device->rq_queue = q; |
2736 | q->queuedata = device; | |
b411b363 PR |
2737 | |
2738 | disk = alloc_disk(1); | |
2739 | if (!disk) | |
2740 | goto out_no_disk; | |
b30ab791 | 2741 | device->vdisk = disk; |
b411b363 | 2742 | |
81e84650 | 2743 | set_disk_ro(disk, true); |
b411b363 PR |
2744 | |
2745 | disk->queue = q; | |
2746 | disk->major = DRBD_MAJOR; | |
2747 | disk->first_minor = minor; | |
2748 | disk->fops = &drbd_ops; | |
2749 | sprintf(disk->disk_name, "drbd%d", minor); | |
b30ab791 | 2750 | disk->private_data = device; |
b411b363 | 2751 | |
b30ab791 | 2752 | device->this_bdev = bdget(MKDEV(DRBD_MAJOR, minor)); |
b411b363 | 2753 | /* we have no partitions. we contain only ourselves. */ |
b30ab791 | 2754 | device->this_bdev->bd_contains = device->this_bdev; |
b411b363 PR |
2755 | |
2756 | q->backing_dev_info.congested_fn = drbd_congested; | |
b30ab791 | 2757 | q->backing_dev_info.congested_data = device; |
b411b363 | 2758 | |
2f58dcfc | 2759 | blk_queue_make_request(q, drbd_make_request); |
a73ff323 | 2760 | blk_queue_flush(q, REQ_FLUSH | REQ_FUA); |
99432fcc PR |
2761 | /* Setting the max_hw_sectors to an odd value of 8kibyte here |
2762 | This triggers a max_bio_size message upon first attach or connect */ | |
2763 | blk_queue_max_hw_sectors(q, DRBD_MAX_BIO_SIZE_SAFE >> 8); | |
b411b363 PR |
2764 | blk_queue_bounce_limit(q, BLK_BOUNCE_ANY); |
2765 | blk_queue_merge_bvec(q, drbd_merge_bvec); | |
0500813f | 2766 | q->queue_lock = &resource->req_lock; |
b411b363 | 2767 | |
b30ab791 AG |
2768 | device->md_io_page = alloc_page(GFP_KERNEL); |
2769 | if (!device->md_io_page) | |
b411b363 PR |
2770 | goto out_no_io_page; |
2771 | ||
b30ab791 | 2772 | if (drbd_bm_init(device)) |
b411b363 | 2773 | goto out_no_bitmap; |
b30ab791 AG |
2774 | device->read_requests = RB_ROOT; |
2775 | device->write_requests = RB_ROOT; | |
b411b363 | 2776 | |
93e4bf7a AG |
2777 | id = idr_alloc(&drbd_devices, device, minor, minor + 1, GFP_KERNEL); |
2778 | if (id < 0) { | |
2779 | if (id == -ENOSPC) { | |
56de2102 | 2780 | err = ERR_MINOR_EXISTS; |
a910b123 | 2781 | drbd_msg_put_info(adm_ctx->reply_skb, "requested minor exists already"); |
56de2102 | 2782 | } |
8432b314 | 2783 | goto out_no_minor_idr; |
81a5d60e | 2784 | } |
803ea134 AG |
2785 | kref_get(&device->kref); |
2786 | ||
2787 | id = idr_alloc(&resource->devices, device, vnr, vnr + 1, GFP_KERNEL); | |
2788 | if (id < 0) { | |
2789 | if (id == -ENOSPC) { | |
2790 | err = ERR_MINOR_EXISTS; | |
a910b123 | 2791 | drbd_msg_put_info(adm_ctx->reply_skb, "requested minor exists already"); |
803ea134 AG |
2792 | } |
2793 | goto out_idr_remove_minor; | |
2794 | } | |
2795 | kref_get(&device->kref); | |
8432b314 | 2796 | |
b6f85ef9 AG |
2797 | INIT_LIST_HEAD(&device->peer_devices); |
2798 | for_each_connection(connection, resource) { | |
2799 | peer_device = kzalloc(sizeof(struct drbd_peer_device), GFP_KERNEL); | |
2800 | if (!peer_device) | |
2801 | goto out_idr_remove_from_resource; | |
2802 | peer_device->connection = connection; | |
2803 | peer_device->device = device; | |
2804 | ||
2805 | list_add(&peer_device->peer_devices, &device->peer_devices); | |
2806 | kref_get(&device->kref); | |
2807 | ||
2808 | id = idr_alloc(&connection->peer_devices, peer_device, vnr, vnr + 1, GFP_KERNEL); | |
2809 | if (id < 0) { | |
2810 | if (id == -ENOSPC) { | |
2811 | err = ERR_INVALID_REQUEST; | |
a910b123 | 2812 | drbd_msg_put_info(adm_ctx->reply_skb, "requested volume exists already"); |
b6f85ef9 AG |
2813 | } |
2814 | goto out_idr_remove_from_resource; | |
56de2102 | 2815 | } |
b6f85ef9 | 2816 | kref_get(&connection->kref); |
8432b314 | 2817 | } |
56de2102 | 2818 | |
b30ab791 | 2819 | if (init_submitter(device)) { |
113fef9e | 2820 | err = ERR_NOMEM; |
a910b123 | 2821 | drbd_msg_put_info(adm_ctx->reply_skb, "unable to create submit workqueue"); |
113fef9e LE |
2822 | goto out_idr_remove_vol; |
2823 | } | |
2824 | ||
774b3055 PR |
2825 | add_disk(disk); |
2826 | ||
2325eb66 | 2827 | /* inherit the connection state */ |
b6f85ef9 | 2828 | device->state.conn = first_connection(resource)->cstate; |
69a22773 AG |
2829 | if (device->state.conn == C_WF_REPORT_PARAMS) { |
2830 | for_each_peer_device(peer_device, device) | |
2831 | drbd_connected(peer_device); | |
2832 | } | |
2325eb66 | 2833 | |
774b3055 | 2834 | return NO_ERROR; |
b411b363 | 2835 | |
113fef9e | 2836 | out_idr_remove_vol: |
c06ece6b | 2837 | idr_remove(&connection->peer_devices, vnr); |
803ea134 | 2838 | out_idr_remove_from_resource: |
b6f85ef9 AG |
2839 | for_each_connection(connection, resource) { |
2840 | peer_device = idr_find(&connection->peer_devices, vnr); | |
2841 | if (peer_device) { | |
2842 | idr_remove(&connection->peer_devices, vnr); | |
2843 | kref_put(&connection->kref, drbd_destroy_connection); | |
2844 | } | |
2845 | } | |
2846 | for_each_peer_device_safe(peer_device, tmp_peer_device, device) { | |
2847 | list_del(&peer_device->peer_devices); | |
2848 | kfree(peer_device); | |
2849 | } | |
803ea134 | 2850 | idr_remove(&resource->devices, vnr); |
8432b314 | 2851 | out_idr_remove_minor: |
93e4bf7a | 2852 | idr_remove(&drbd_devices, minor); |
569083c0 | 2853 | synchronize_rcu(); |
8432b314 | 2854 | out_no_minor_idr: |
b30ab791 | 2855 | drbd_bm_cleanup(device); |
b411b363 | 2856 | out_no_bitmap: |
b30ab791 | 2857 | __free_page(device->md_io_page); |
b411b363 PR |
2858 | out_no_io_page: |
2859 | put_disk(disk); | |
2860 | out_no_disk: | |
2861 | blk_cleanup_queue(q); | |
2862 | out_no_q: | |
803ea134 | 2863 | kref_put(&resource->kref, drbd_destroy_resource); |
a6b32bc3 | 2864 | kfree(device); |
8432b314 | 2865 | return err; |
b411b363 PR |
2866 | } |
2867 | ||
f82795d6 | 2868 | void drbd_delete_device(struct drbd_device *device) |
803ea134 AG |
2869 | { |
2870 | struct drbd_resource *resource = device->resource; | |
2871 | struct drbd_connection *connection; | |
2872 | int refs = 3; | |
2873 | ||
2874 | for_each_connection(connection, resource) { | |
c06ece6b | 2875 | idr_remove(&connection->peer_devices, device->vnr); |
803ea134 AG |
2876 | refs++; |
2877 | } | |
2878 | idr_remove(&resource->devices, device->vnr); | |
2879 | idr_remove(&drbd_devices, device_to_minor(device)); | |
2880 | del_gendisk(device->vdisk); | |
2881 | synchronize_rcu(); | |
2882 | kref_sub(&device->kref, refs, drbd_destroy_device); | |
2883 | } | |
2884 | ||
b411b363 PR |
2885 | int __init drbd_init(void) |
2886 | { | |
2887 | int err; | |
2888 | ||
2b8a90b5 | 2889 | if (minor_count < DRBD_MINOR_COUNT_MIN || minor_count > DRBD_MINOR_COUNT_MAX) { |
b411b363 | 2890 | printk(KERN_ERR |
81a5d60e | 2891 | "drbd: invalid minor_count (%d)\n", minor_count); |
b411b363 PR |
2892 | #ifdef MODULE |
2893 | return -EINVAL; | |
2894 | #else | |
46530e85 | 2895 | minor_count = DRBD_MINOR_COUNT_DEF; |
b411b363 PR |
2896 | #endif |
2897 | } | |
2898 | ||
b411b363 PR |
2899 | err = register_blkdev(DRBD_MAJOR, "drbd"); |
2900 | if (err) { | |
2901 | printk(KERN_ERR | |
2902 | "drbd: unable to register block device major %d\n", | |
2903 | DRBD_MAJOR); | |
2904 | return err; | |
2905 | } | |
2906 | ||
2907 | register_reboot_notifier(&drbd_notifier); | |
2908 | ||
2909 | /* | |
2910 | * allocate all necessary structs | |
2911 | */ | |
b411b363 PR |
2912 | init_waitqueue_head(&drbd_pp_wait); |
2913 | ||
2914 | drbd_proc = NULL; /* play safe for drbd_cleanup */ | |
05a10ec7 | 2915 | idr_init(&drbd_devices); |
b411b363 | 2916 | |
69babf05 | 2917 | rwlock_init(&global_state_lock); |
77c556f6 | 2918 | INIT_LIST_HEAD(&drbd_resources); |
69babf05 LE |
2919 | |
2920 | err = drbd_genl_register(); | |
2921 | if (err) { | |
2922 | printk(KERN_ERR "drbd: unable to register generic netlink family\n"); | |
2923 | goto fail; | |
2924 | } | |
2925 | ||
b411b363 PR |
2926 | err = drbd_create_mempools(); |
2927 | if (err) | |
3b98c0c2 | 2928 | goto fail; |
b411b363 | 2929 | |
6110d70b | 2930 | err = -ENOMEM; |
8c484ee4 | 2931 | drbd_proc = proc_create_data("drbd", S_IFREG | S_IRUGO , NULL, &drbd_proc_fops, NULL); |
b411b363 PR |
2932 | if (!drbd_proc) { |
2933 | printk(KERN_ERR "drbd: unable to register proc file\n"); | |
3b98c0c2 | 2934 | goto fail; |
b411b363 PR |
2935 | } |
2936 | ||
2312f0b3 LE |
2937 | retry.wq = create_singlethread_workqueue("drbd-reissue"); |
2938 | if (!retry.wq) { | |
2939 | printk(KERN_ERR "drbd: unable to create retry workqueue\n"); | |
2940 | goto fail; | |
2941 | } | |
2942 | INIT_WORK(&retry.worker, do_retry); | |
2943 | spin_lock_init(&retry.lock); | |
2944 | INIT_LIST_HEAD(&retry.writes); | |
b411b363 PR |
2945 | |
2946 | printk(KERN_INFO "drbd: initialized. " | |
2947 | "Version: " REL_VERSION " (api:%d/proto:%d-%d)\n", | |
2948 | API_VERSION, PRO_VERSION_MIN, PRO_VERSION_MAX); | |
2949 | printk(KERN_INFO "drbd: %s\n", drbd_buildtag()); | |
2950 | printk(KERN_INFO "drbd: registered as block device major %d\n", | |
2951 | DRBD_MAJOR); | |
b411b363 PR |
2952 | |
2953 | return 0; /* Success! */ | |
2954 | ||
3b98c0c2 | 2955 | fail: |
b411b363 PR |
2956 | drbd_cleanup(); |
2957 | if (err == -ENOMEM) | |
b411b363 PR |
2958 | printk(KERN_ERR "drbd: ran out of memory\n"); |
2959 | else | |
2960 | printk(KERN_ERR "drbd: initialization failure\n"); | |
2961 | return err; | |
2962 | } | |
2963 | ||
28995af5 | 2964 | void drbd_free_ldev(struct drbd_backing_dev *ldev) |
b411b363 PR |
2965 | { |
2966 | if (ldev == NULL) | |
2967 | return; | |
2968 | ||
e525fd89 TH |
2969 | blkdev_put(ldev->backing_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); |
2970 | blkdev_put(ldev->md_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); | |
b411b363 | 2971 | |
94ad0a10 | 2972 | kfree(ldev->disk_conf); |
b411b363 PR |
2973 | kfree(ldev); |
2974 | } | |
2975 | ||
bde89a9e | 2976 | void drbd_free_sock(struct drbd_connection *connection) |
b411b363 | 2977 | { |
bde89a9e AG |
2978 | if (connection->data.socket) { |
2979 | mutex_lock(&connection->data.mutex); | |
2980 | kernel_sock_shutdown(connection->data.socket, SHUT_RDWR); | |
2981 | sock_release(connection->data.socket); | |
2982 | connection->data.socket = NULL; | |
2983 | mutex_unlock(&connection->data.mutex); | |
b411b363 | 2984 | } |
bde89a9e AG |
2985 | if (connection->meta.socket) { |
2986 | mutex_lock(&connection->meta.mutex); | |
2987 | kernel_sock_shutdown(connection->meta.socket, SHUT_RDWR); | |
2988 | sock_release(connection->meta.socket); | |
2989 | connection->meta.socket = NULL; | |
2990 | mutex_unlock(&connection->meta.mutex); | |
b411b363 PR |
2991 | } |
2992 | } | |
2993 | ||
b411b363 | 2994 | /* meta data management */ |
b411b363 | 2995 | |
bde89a9e | 2996 | void conn_md_sync(struct drbd_connection *connection) |
b411b363 | 2997 | { |
c06ece6b | 2998 | struct drbd_peer_device *peer_device; |
19fffd7b | 2999 | int vnr; |
b411b363 | 3000 | |
19fffd7b | 3001 | rcu_read_lock(); |
c06ece6b AG |
3002 | idr_for_each_entry(&connection->peer_devices, peer_device, vnr) { |
3003 | struct drbd_device *device = peer_device->device; | |
3004 | ||
b30ab791 | 3005 | kref_get(&device->kref); |
19fffd7b | 3006 | rcu_read_unlock(); |
b30ab791 | 3007 | drbd_md_sync(device); |
05a10ec7 | 3008 | kref_put(&device->kref, drbd_destroy_device); |
19fffd7b PR |
3009 | rcu_read_lock(); |
3010 | } | |
3011 | rcu_read_unlock(); | |
b411b363 PR |
3012 | } |
3013 | ||
ae8bf312 | 3014 | /* aligned 4kByte */ |
b411b363 | 3015 | struct meta_data_on_disk { |
cccac985 | 3016 | u64 la_size_sect; /* last agreed size. */ |
b411b363 PR |
3017 | u64 uuid[UI_SIZE]; /* UUIDs. */ |
3018 | u64 device_uuid; | |
3019 | u64 reserved_u64_1; | |
3020 | u32 flags; /* MDF */ | |
3021 | u32 magic; | |
3022 | u32 md_size_sect; | |
3023 | u32 al_offset; /* offset to this block */ | |
ae8bf312 | 3024 | u32 al_nr_extents; /* important for restoring the AL (userspace) */ |
f399002e | 3025 | /* `-- act_log->nr_elements <-- ldev->dc.al_extents */ |
b411b363 PR |
3026 | u32 bm_offset; /* offset to the bitmap, from here */ |
3027 | u32 bm_bytes_per_bit; /* BM_BLOCK_SIZE */ | |
99432fcc | 3028 | u32 la_peer_max_bio_size; /* last peer max_bio_size */ |
b411b363 | 3029 | |
3a4d4eb3 LE |
3030 | /* see al_tr_number_to_on_disk_sector() */ |
3031 | u32 al_stripes; | |
3032 | u32 al_stripe_size_4k; | |
3033 | ||
3034 | u8 reserved_u8[4096 - (7*8 + 10*4)]; | |
b411b363 PR |
3035 | } __packed; |
3036 | ||
d752b269 PR |
3037 | |
3038 | ||
b30ab791 | 3039 | void drbd_md_write(struct drbd_device *device, void *b) |
b411b363 | 3040 | { |
d752b269 | 3041 | struct meta_data_on_disk *buffer = b; |
b411b363 PR |
3042 | sector_t sector; |
3043 | int i; | |
3044 | ||
ae8bf312 | 3045 | memset(buffer, 0, sizeof(*buffer)); |
b411b363 | 3046 | |
b30ab791 | 3047 | buffer->la_size_sect = cpu_to_be64(drbd_get_capacity(device->this_bdev)); |
b411b363 | 3048 | for (i = UI_CURRENT; i < UI_SIZE; i++) |
b30ab791 AG |
3049 | buffer->uuid[i] = cpu_to_be64(device->ldev->md.uuid[i]); |
3050 | buffer->flags = cpu_to_be32(device->ldev->md.flags); | |
d5d7ebd4 | 3051 | buffer->magic = cpu_to_be32(DRBD_MD_MAGIC_84_UNCLEAN); |
b411b363 | 3052 | |
b30ab791 AG |
3053 | buffer->md_size_sect = cpu_to_be32(device->ldev->md.md_size_sect); |
3054 | buffer->al_offset = cpu_to_be32(device->ldev->md.al_offset); | |
3055 | buffer->al_nr_extents = cpu_to_be32(device->act_log->nr_elements); | |
b411b363 | 3056 | buffer->bm_bytes_per_bit = cpu_to_be32(BM_BLOCK_SIZE); |
b30ab791 | 3057 | buffer->device_uuid = cpu_to_be64(device->ldev->md.device_uuid); |
b411b363 | 3058 | |
b30ab791 AG |
3059 | buffer->bm_offset = cpu_to_be32(device->ldev->md.bm_offset); |
3060 | buffer->la_peer_max_bio_size = cpu_to_be32(device->peer_max_bio_size); | |
b411b363 | 3061 | |
b30ab791 AG |
3062 | buffer->al_stripes = cpu_to_be32(device->ldev->md.al_stripes); |
3063 | buffer->al_stripe_size_4k = cpu_to_be32(device->ldev->md.al_stripe_size_4k); | |
3a4d4eb3 | 3064 | |
0b0ba1ef | 3065 | D_ASSERT(device, drbd_md_ss(device->ldev) == device->ldev->md.md_offset); |
b30ab791 | 3066 | sector = device->ldev->md.md_offset; |
b411b363 | 3067 | |
b30ab791 | 3068 | if (drbd_md_sync_page_io(device, device->ldev, sector, WRITE)) { |
b411b363 | 3069 | /* this was a try anyways ... */ |
d0180171 | 3070 | drbd_err(device, "meta data update failed!\n"); |
b30ab791 | 3071 | drbd_chk_io_error(device, 1, DRBD_META_IO_ERROR); |
b411b363 | 3072 | } |
d752b269 PR |
3073 | } |
3074 | ||
3075 | /** | |
3076 | * drbd_md_sync() - Writes the meta data super block if the MD_DIRTY flag bit is set | |
b30ab791 | 3077 | * @device: DRBD device. |
d752b269 | 3078 | */ |
b30ab791 | 3079 | void drbd_md_sync(struct drbd_device *device) |
d752b269 PR |
3080 | { |
3081 | struct meta_data_on_disk *buffer; | |
3082 | ||
3083 | /* Don't accidentally change the DRBD meta data layout. */ | |
3084 | BUILD_BUG_ON(UI_SIZE != 4); | |
3085 | BUILD_BUG_ON(sizeof(struct meta_data_on_disk) != 4096); | |
3086 | ||
b30ab791 | 3087 | del_timer(&device->md_sync_timer); |
d752b269 | 3088 | /* timer may be rearmed by drbd_md_mark_dirty() now. */ |
b30ab791 | 3089 | if (!test_and_clear_bit(MD_DIRTY, &device->flags)) |
d752b269 PR |
3090 | return; |
3091 | ||
3092 | /* We use here D_FAILED and not D_ATTACHING because we try to write | |
3093 | * metadata even if we detach due to a disk failure! */ | |
b30ab791 | 3094 | if (!get_ldev_if_state(device, D_FAILED)) |
d752b269 PR |
3095 | return; |
3096 | ||
b30ab791 | 3097 | buffer = drbd_md_get_buffer(device); |
d752b269 PR |
3098 | if (!buffer) |
3099 | goto out; | |
3100 | ||
b30ab791 | 3101 | drbd_md_write(device, buffer); |
b411b363 | 3102 | |
b30ab791 | 3103 | /* Update device->ldev->md.la_size_sect, |
b411b363 | 3104 | * since we updated it on metadata. */ |
b30ab791 | 3105 | device->ldev->md.la_size_sect = drbd_get_capacity(device->this_bdev); |
b411b363 | 3106 | |
b30ab791 | 3107 | drbd_md_put_buffer(device); |
e1711731 | 3108 | out: |
b30ab791 | 3109 | put_ldev(device); |
b411b363 PR |
3110 | } |
3111 | ||
b30ab791 | 3112 | static int check_activity_log_stripe_size(struct drbd_device *device, |
3a4d4eb3 LE |
3113 | struct meta_data_on_disk *on_disk, |
3114 | struct drbd_md *in_core) | |
3115 | { | |
3116 | u32 al_stripes = be32_to_cpu(on_disk->al_stripes); | |
3117 | u32 al_stripe_size_4k = be32_to_cpu(on_disk->al_stripe_size_4k); | |
3118 | u64 al_size_4k; | |
3119 | ||
3120 | /* both not set: default to old fixed size activity log */ | |
3121 | if (al_stripes == 0 && al_stripe_size_4k == 0) { | |
3122 | al_stripes = 1; | |
3123 | al_stripe_size_4k = MD_32kB_SECT/8; | |
3124 | } | |
3125 | ||
3126 | /* some paranoia plausibility checks */ | |
3127 | ||
3128 | /* we need both values to be set */ | |
3129 | if (al_stripes == 0 || al_stripe_size_4k == 0) | |
3130 | goto err; | |
3131 | ||
3132 | al_size_4k = (u64)al_stripes * al_stripe_size_4k; | |
3133 | ||
3134 | /* Upper limit of activity log area, to avoid potential overflow | |
3135 | * problems in al_tr_number_to_on_disk_sector(). As right now, more | |
3136 | * than 72 * 4k blocks total only increases the amount of history, | |
3137 | * limiting this arbitrarily to 16 GB is not a real limitation ;-) */ | |
3138 | if (al_size_4k > (16 * 1024 * 1024/4)) | |
3139 | goto err; | |
3140 | ||
3141 | /* Lower limit: we need at least 8 transaction slots (32kB) | |
3142 | * to not break existing setups */ | |
3143 | if (al_size_4k < MD_32kB_SECT/8) | |
3144 | goto err; | |
3145 | ||
3146 | in_core->al_stripe_size_4k = al_stripe_size_4k; | |
3147 | in_core->al_stripes = al_stripes; | |
3148 | in_core->al_size_4k = al_size_4k; | |
3149 | ||
3150 | return 0; | |
3151 | err: | |
d0180171 | 3152 | drbd_err(device, "invalid activity log striping: al_stripes=%u, al_stripe_size_4k=%u\n", |
3a4d4eb3 LE |
3153 | al_stripes, al_stripe_size_4k); |
3154 | return -EINVAL; | |
3155 | } | |
3156 | ||
b30ab791 | 3157 | static int check_offsets_and_sizes(struct drbd_device *device, struct drbd_backing_dev *bdev) |
c04ccaa6 LE |
3158 | { |
3159 | sector_t capacity = drbd_get_capacity(bdev->md_bdev); | |
3160 | struct drbd_md *in_core = &bdev->md; | |
3161 | s32 on_disk_al_sect; | |
3162 | s32 on_disk_bm_sect; | |
3163 | ||
3164 | /* The on-disk size of the activity log, calculated from offsets, and | |
3165 | * the size of the activity log calculated from the stripe settings, | |
3166 | * should match. | |
3167 | * Though we could relax this a bit: it is ok, if the striped activity log | |
3168 | * fits in the available on-disk activity log size. | |
3169 | * Right now, that would break how resize is implemented. | |
3170 | * TODO: make drbd_determine_dev_size() (and the drbdmeta tool) aware | |
3171 | * of possible unused padding space in the on disk layout. */ | |
3172 | if (in_core->al_offset < 0) { | |
3173 | if (in_core->bm_offset > in_core->al_offset) | |
3174 | goto err; | |
3175 | on_disk_al_sect = -in_core->al_offset; | |
3176 | on_disk_bm_sect = in_core->al_offset - in_core->bm_offset; | |
3177 | } else { | |
3178 | if (in_core->al_offset != MD_4kB_SECT) | |
3179 | goto err; | |
3180 | if (in_core->bm_offset < in_core->al_offset + in_core->al_size_4k * MD_4kB_SECT) | |
3181 | goto err; | |
3182 | ||
3183 | on_disk_al_sect = in_core->bm_offset - MD_4kB_SECT; | |
3184 | on_disk_bm_sect = in_core->md_size_sect - in_core->bm_offset; | |
3185 | } | |
3186 | ||
3187 | /* old fixed size meta data is exactly that: fixed. */ | |
3188 | if (in_core->meta_dev_idx >= 0) { | |
3189 | if (in_core->md_size_sect != MD_128MB_SECT | |
3190 | || in_core->al_offset != MD_4kB_SECT | |
3191 | || in_core->bm_offset != MD_4kB_SECT + MD_32kB_SECT | |
3192 | || in_core->al_stripes != 1 | |
3193 | || in_core->al_stripe_size_4k != MD_32kB_SECT/8) | |
3194 | goto err; | |
3195 | } | |
3196 | ||
3197 | if (capacity < in_core->md_size_sect) | |
3198 | goto err; | |
3199 | if (capacity - in_core->md_size_sect < drbd_md_first_sector(bdev)) | |
3200 | goto err; | |
3201 | ||
3202 | /* should be aligned, and at least 32k */ | |
3203 | if ((on_disk_al_sect & 7) || (on_disk_al_sect < MD_32kB_SECT)) | |
3204 | goto err; | |
3205 | ||
3206 | /* should fit (for now: exactly) into the available on-disk space; | |
3207 | * overflow prevention is in check_activity_log_stripe_size() above. */ | |
3208 | if (on_disk_al_sect != in_core->al_size_4k * MD_4kB_SECT) | |
3209 | goto err; | |
3210 | ||
3211 | /* again, should be aligned */ | |
3212 | if (in_core->bm_offset & 7) | |
3213 | goto err; | |
3214 | ||
3215 | /* FIXME check for device grow with flex external meta data? */ | |
3216 | ||
3217 | /* can the available bitmap space cover the last agreed device size? */ | |
3218 | if (on_disk_bm_sect < (in_core->la_size_sect+7)/MD_4kB_SECT/8/512) | |
3219 | goto err; | |
3220 | ||
3221 | return 0; | |
3222 | ||
3223 | err: | |
d0180171 | 3224 | drbd_err(device, "meta data offsets don't make sense: idx=%d " |
c04ccaa6 LE |
3225 | "al_s=%u, al_sz4k=%u, al_offset=%d, bm_offset=%d, " |
3226 | "md_size_sect=%u, la_size=%llu, md_capacity=%llu\n", | |
3227 | in_core->meta_dev_idx, | |
3228 | in_core->al_stripes, in_core->al_stripe_size_4k, | |
3229 | in_core->al_offset, in_core->bm_offset, in_core->md_size_sect, | |
3230 | (unsigned long long)in_core->la_size_sect, | |
3231 | (unsigned long long)capacity); | |
3232 | ||
3233 | return -EINVAL; | |
3234 | } | |
3235 | ||
3236 | ||
b411b363 PR |
3237 | /** |
3238 | * drbd_md_read() - Reads in the meta data super block | |
b30ab791 | 3239 | * @device: DRBD device. |
b411b363 PR |
3240 | * @bdev: Device from which the meta data should be read in. |
3241 | * | |
3a4d4eb3 | 3242 | * Return NO_ERROR on success, and an enum drbd_ret_code in case |
d5d7ebd4 | 3243 | * something goes wrong. |
3a4d4eb3 | 3244 | * |
c04ccaa6 | 3245 | * Called exactly once during drbd_adm_attach(), while still being D_DISKLESS, |
b30ab791 | 3246 | * even before @bdev is assigned to @device->ldev. |
b411b363 | 3247 | */ |
b30ab791 | 3248 | int drbd_md_read(struct drbd_device *device, struct drbd_backing_dev *bdev) |
b411b363 PR |
3249 | { |
3250 | struct meta_data_on_disk *buffer; | |
d5d7ebd4 | 3251 | u32 magic, flags; |
b411b363 PR |
3252 | int i, rv = NO_ERROR; |
3253 | ||
b30ab791 | 3254 | if (device->state.disk != D_DISKLESS) |
c04ccaa6 | 3255 | return ERR_DISK_CONFIGURED; |
b411b363 | 3256 | |
b30ab791 | 3257 | buffer = drbd_md_get_buffer(device); |
e1711731 | 3258 | if (!buffer) |
c04ccaa6 | 3259 | return ERR_NOMEM; |
b411b363 | 3260 | |
c04ccaa6 LE |
3261 | /* First, figure out where our meta data superblock is located, |
3262 | * and read it. */ | |
3a4d4eb3 LE |
3263 | bdev->md.meta_dev_idx = bdev->disk_conf->meta_dev_idx; |
3264 | bdev->md.md_offset = drbd_md_ss(bdev); | |
b411b363 | 3265 | |
b30ab791 | 3266 | if (drbd_md_sync_page_io(device, bdev, bdev->md.md_offset, READ)) { |
25985edc | 3267 | /* NOTE: can't do normal error processing here as this is |
b411b363 | 3268 | called BEFORE disk is attached */ |
d0180171 | 3269 | drbd_err(device, "Error while reading metadata.\n"); |
b411b363 PR |
3270 | rv = ERR_IO_MD_DISK; |
3271 | goto err; | |
3272 | } | |
3273 | ||
d5d7ebd4 LE |
3274 | magic = be32_to_cpu(buffer->magic); |
3275 | flags = be32_to_cpu(buffer->flags); | |
3276 | if (magic == DRBD_MD_MAGIC_84_UNCLEAN || | |
3277 | (magic == DRBD_MD_MAGIC_08 && !(flags & MDF_AL_CLEAN))) { | |
3278 | /* btw: that's Activity Log clean, not "all" clean. */ | |
d0180171 | 3279 | drbd_err(device, "Found unclean meta data. Did you \"drbdadm apply-al\"?\n"); |
d5d7ebd4 LE |
3280 | rv = ERR_MD_UNCLEAN; |
3281 | goto err; | |
3282 | } | |
3a4d4eb3 LE |
3283 | |
3284 | rv = ERR_MD_INVALID; | |
d5d7ebd4 | 3285 | if (magic != DRBD_MD_MAGIC_08) { |
43de7c85 | 3286 | if (magic == DRBD_MD_MAGIC_07) |
d0180171 | 3287 | drbd_err(device, "Found old (0.7) meta data magic. Did you \"drbdadm create-md\"?\n"); |
d5d7ebd4 | 3288 | else |
d0180171 | 3289 | drbd_err(device, "Meta data magic not found. Did you \"drbdadm create-md\"?\n"); |
b411b363 PR |
3290 | goto err; |
3291 | } | |
3a4d4eb3 | 3292 | |
c04ccaa6 | 3293 | if (be32_to_cpu(buffer->bm_bytes_per_bit) != BM_BLOCK_SIZE) { |
d0180171 | 3294 | drbd_err(device, "unexpected bm_bytes_per_bit: %u (expected %u)\n", |
c04ccaa6 | 3295 | be32_to_cpu(buffer->bm_bytes_per_bit), BM_BLOCK_SIZE); |
b411b363 PR |
3296 | goto err; |
3297 | } | |
3a4d4eb3 | 3298 | |
c04ccaa6 LE |
3299 | |
3300 | /* convert to in_core endian */ | |
3301 | bdev->md.la_size_sect = be64_to_cpu(buffer->la_size_sect); | |
3302 | for (i = UI_CURRENT; i < UI_SIZE; i++) | |
3303 | bdev->md.uuid[i] = be64_to_cpu(buffer->uuid[i]); | |
3304 | bdev->md.flags = be32_to_cpu(buffer->flags); | |
3305 | bdev->md.device_uuid = be64_to_cpu(buffer->device_uuid); | |
3306 | ||
3307 | bdev->md.md_size_sect = be32_to_cpu(buffer->md_size_sect); | |
3308 | bdev->md.al_offset = be32_to_cpu(buffer->al_offset); | |
3309 | bdev->md.bm_offset = be32_to_cpu(buffer->bm_offset); | |
3310 | ||
b30ab791 | 3311 | if (check_activity_log_stripe_size(device, buffer, &bdev->md)) |
b411b363 | 3312 | goto err; |
b30ab791 | 3313 | if (check_offsets_and_sizes(device, bdev)) |
c04ccaa6 LE |
3314 | goto err; |
3315 | ||
b411b363 | 3316 | if (be32_to_cpu(buffer->bm_offset) != bdev->md.bm_offset) { |
d0180171 | 3317 | drbd_err(device, "unexpected bm_offset: %d (expected %d)\n", |
b411b363 | 3318 | be32_to_cpu(buffer->bm_offset), bdev->md.bm_offset); |
b411b363 PR |
3319 | goto err; |
3320 | } | |
3321 | if (be32_to_cpu(buffer->md_size_sect) != bdev->md.md_size_sect) { | |
d0180171 | 3322 | drbd_err(device, "unexpected md_size: %u (expected %u)\n", |
b411b363 | 3323 | be32_to_cpu(buffer->md_size_sect), bdev->md.md_size_sect); |
b411b363 PR |
3324 | goto err; |
3325 | } | |
3326 | ||
3a4d4eb3 | 3327 | rv = NO_ERROR; |
b411b363 | 3328 | |
0500813f | 3329 | spin_lock_irq(&device->resource->req_lock); |
b30ab791 | 3330 | if (device->state.conn < C_CONNECTED) { |
db141b2f | 3331 | unsigned int peer; |
99432fcc | 3332 | peer = be32_to_cpu(buffer->la_peer_max_bio_size); |
db141b2f | 3333 | peer = max(peer, DRBD_MAX_BIO_SIZE_SAFE); |
b30ab791 | 3334 | device->peer_max_bio_size = peer; |
99432fcc | 3335 | } |
0500813f | 3336 | spin_unlock_irq(&device->resource->req_lock); |
b411b363 PR |
3337 | |
3338 | err: | |
b30ab791 | 3339 | drbd_md_put_buffer(device); |
b411b363 PR |
3340 | |
3341 | return rv; | |
3342 | } | |
3343 | ||
3344 | /** | |
3345 | * drbd_md_mark_dirty() - Mark meta data super block as dirty | |
b30ab791 | 3346 | * @device: DRBD device. |
b411b363 PR |
3347 | * |
3348 | * Call this function if you change anything that should be written to | |
3349 | * the meta-data super block. This function sets MD_DIRTY, and starts a | |
3350 | * timer that ensures that within five seconds you have to call drbd_md_sync(). | |
3351 | */ | |
ca0e6098 | 3352 | #ifdef DEBUG |
b30ab791 | 3353 | void drbd_md_mark_dirty_(struct drbd_device *device, unsigned int line, const char *func) |
ee15b038 | 3354 | { |
b30ab791 AG |
3355 | if (!test_and_set_bit(MD_DIRTY, &device->flags)) { |
3356 | mod_timer(&device->md_sync_timer, jiffies + HZ); | |
3357 | device->last_md_mark_dirty.line = line; | |
3358 | device->last_md_mark_dirty.func = func; | |
ee15b038 LE |
3359 | } |
3360 | } | |
3361 | #else | |
b30ab791 | 3362 | void drbd_md_mark_dirty(struct drbd_device *device) |
b411b363 | 3363 | { |
b30ab791 AG |
3364 | if (!test_and_set_bit(MD_DIRTY, &device->flags)) |
3365 | mod_timer(&device->md_sync_timer, jiffies + 5*HZ); | |
b411b363 | 3366 | } |
ee15b038 | 3367 | #endif |
b411b363 | 3368 | |
b30ab791 | 3369 | void drbd_uuid_move_history(struct drbd_device *device) __must_hold(local) |
b411b363 PR |
3370 | { |
3371 | int i; | |
3372 | ||
62b0da3a | 3373 | for (i = UI_HISTORY_START; i < UI_HISTORY_END; i++) |
b30ab791 | 3374 | device->ldev->md.uuid[i+1] = device->ldev->md.uuid[i]; |
b411b363 PR |
3375 | } |
3376 | ||
b30ab791 | 3377 | void __drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local) |
b411b363 PR |
3378 | { |
3379 | if (idx == UI_CURRENT) { | |
b30ab791 | 3380 | if (device->state.role == R_PRIMARY) |
b411b363 PR |
3381 | val |= 1; |
3382 | else | |
3383 | val &= ~((u64)1); | |
3384 | ||
b30ab791 | 3385 | drbd_set_ed_uuid(device, val); |
b411b363 PR |
3386 | } |
3387 | ||
b30ab791 AG |
3388 | device->ldev->md.uuid[idx] = val; |
3389 | drbd_md_mark_dirty(device); | |
b411b363 PR |
3390 | } |
3391 | ||
b30ab791 | 3392 | void _drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local) |
9f2247bb PR |
3393 | { |
3394 | unsigned long flags; | |
b30ab791 AG |
3395 | spin_lock_irqsave(&device->ldev->md.uuid_lock, flags); |
3396 | __drbd_uuid_set(device, idx, val); | |
3397 | spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags); | |
9f2247bb | 3398 | } |
b411b363 | 3399 | |
b30ab791 | 3400 | void drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local) |
b411b363 | 3401 | { |
9f2247bb | 3402 | unsigned long flags; |
b30ab791 AG |
3403 | spin_lock_irqsave(&device->ldev->md.uuid_lock, flags); |
3404 | if (device->ldev->md.uuid[idx]) { | |
3405 | drbd_uuid_move_history(device); | |
3406 | device->ldev->md.uuid[UI_HISTORY_START] = device->ldev->md.uuid[idx]; | |
b411b363 | 3407 | } |
b30ab791 AG |
3408 | __drbd_uuid_set(device, idx, val); |
3409 | spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags); | |
b411b363 PR |
3410 | } |
3411 | ||
3412 | /** | |
3413 | * drbd_uuid_new_current() - Creates a new current UUID | |
b30ab791 | 3414 | * @device: DRBD device. |
b411b363 PR |
3415 | * |
3416 | * Creates a new current UUID, and rotates the old current UUID into | |
3417 | * the bitmap slot. Causes an incremental resync upon next connect. | |
3418 | */ | |
b30ab791 | 3419 | void drbd_uuid_new_current(struct drbd_device *device) __must_hold(local) |
b411b363 PR |
3420 | { |
3421 | u64 val; | |
9f2247bb PR |
3422 | unsigned long long bm_uuid; |
3423 | ||
3424 | get_random_bytes(&val, sizeof(u64)); | |
3425 | ||
b30ab791 AG |
3426 | spin_lock_irq(&device->ldev->md.uuid_lock); |
3427 | bm_uuid = device->ldev->md.uuid[UI_BITMAP]; | |
62b0da3a LE |
3428 | |
3429 | if (bm_uuid) | |
d0180171 | 3430 | drbd_warn(device, "bm UUID was already set: %llX\n", bm_uuid); |
b411b363 | 3431 | |
b30ab791 AG |
3432 | device->ldev->md.uuid[UI_BITMAP] = device->ldev->md.uuid[UI_CURRENT]; |
3433 | __drbd_uuid_set(device, UI_CURRENT, val); | |
3434 | spin_unlock_irq(&device->ldev->md.uuid_lock); | |
b411b363 | 3435 | |
b30ab791 | 3436 | drbd_print_uuids(device, "new current UUID"); |
aaa8e2b3 | 3437 | /* get it to stable storage _now_ */ |
b30ab791 | 3438 | drbd_md_sync(device); |
b411b363 PR |
3439 | } |
3440 | ||
b30ab791 | 3441 | void drbd_uuid_set_bm(struct drbd_device *device, u64 val) __must_hold(local) |
b411b363 | 3442 | { |
9f2247bb | 3443 | unsigned long flags; |
b30ab791 | 3444 | if (device->ldev->md.uuid[UI_BITMAP] == 0 && val == 0) |
b411b363 PR |
3445 | return; |
3446 | ||
b30ab791 | 3447 | spin_lock_irqsave(&device->ldev->md.uuid_lock, flags); |
b411b363 | 3448 | if (val == 0) { |
b30ab791 AG |
3449 | drbd_uuid_move_history(device); |
3450 | device->ldev->md.uuid[UI_HISTORY_START] = device->ldev->md.uuid[UI_BITMAP]; | |
3451 | device->ldev->md.uuid[UI_BITMAP] = 0; | |
b411b363 | 3452 | } else { |
b30ab791 | 3453 | unsigned long long bm_uuid = device->ldev->md.uuid[UI_BITMAP]; |
62b0da3a | 3454 | if (bm_uuid) |
d0180171 | 3455 | drbd_warn(device, "bm UUID was already set: %llX\n", bm_uuid); |
b411b363 | 3456 | |
b30ab791 | 3457 | device->ldev->md.uuid[UI_BITMAP] = val & ~((u64)1); |
b411b363 | 3458 | } |
b30ab791 | 3459 | spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags); |
9f2247bb | 3460 | |
b30ab791 | 3461 | drbd_md_mark_dirty(device); |
b411b363 PR |
3462 | } |
3463 | ||
3464 | /** | |
3465 | * drbd_bmio_set_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io() | |
b30ab791 | 3466 | * @device: DRBD device. |
b411b363 PR |
3467 | * |
3468 | * Sets all bits in the bitmap and writes the whole bitmap to stable storage. | |
3469 | */ | |
8fe39aac | 3470 | int drbd_bmio_set_n_write(struct drbd_device *device) __must_hold(local) |
b411b363 PR |
3471 | { |
3472 | int rv = -EIO; | |
3473 | ||
8fe39aac PR |
3474 | drbd_md_set_flag(device, MDF_FULL_SYNC); |
3475 | drbd_md_sync(device); | |
3476 | drbd_bm_set_all(device); | |
b411b363 | 3477 | |
8fe39aac | 3478 | rv = drbd_bm_write(device); |
b411b363 | 3479 | |
8fe39aac PR |
3480 | if (!rv) { |
3481 | drbd_md_clear_flag(device, MDF_FULL_SYNC); | |
3482 | drbd_md_sync(device); | |
b411b363 PR |
3483 | } |
3484 | ||
3485 | return rv; | |
3486 | } | |
3487 | ||
3488 | /** | |
3489 | * drbd_bmio_clear_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io() | |
b30ab791 | 3490 | * @device: DRBD device. |
b411b363 PR |
3491 | * |
3492 | * Clears all bits in the bitmap and writes the whole bitmap to stable storage. | |
3493 | */ | |
8fe39aac | 3494 | int drbd_bmio_clear_n_write(struct drbd_device *device) __must_hold(local) |
b411b363 | 3495 | { |
b30ab791 | 3496 | drbd_resume_al(device); |
8fe39aac PR |
3497 | drbd_bm_clear_all(device); |
3498 | return drbd_bm_write(device); | |
b411b363 PR |
3499 | } |
3500 | ||
99920dc5 | 3501 | static int w_bitmap_io(struct drbd_work *w, int unused) |
b411b363 | 3502 | { |
84b8c06b AG |
3503 | struct drbd_device *device = |
3504 | container_of(w, struct drbd_device, bm_io_work.w); | |
3505 | struct bm_io_work *work = &device->bm_io_work; | |
02851e9f | 3506 | int rv = -EIO; |
b411b363 | 3507 | |
0b0ba1ef | 3508 | D_ASSERT(device, atomic_read(&device->ap_bio_cnt) == 0); |
b411b363 | 3509 | |
b30ab791 AG |
3510 | if (get_ldev(device)) { |
3511 | drbd_bm_lock(device, work->why, work->flags); | |
3512 | rv = work->io_fn(device); | |
3513 | drbd_bm_unlock(device); | |
3514 | put_ldev(device); | |
02851e9f | 3515 | } |
b411b363 | 3516 | |
b30ab791 AG |
3517 | clear_bit_unlock(BITMAP_IO, &device->flags); |
3518 | wake_up(&device->misc_wait); | |
b411b363 PR |
3519 | |
3520 | if (work->done) | |
b30ab791 | 3521 | work->done(device, rv); |
b411b363 | 3522 | |
b30ab791 | 3523 | clear_bit(BITMAP_IO_QUEUED, &device->flags); |
b411b363 | 3524 | work->why = NULL; |
20ceb2b2 | 3525 | work->flags = 0; |
b411b363 | 3526 | |
99920dc5 | 3527 | return 0; |
b411b363 PR |
3528 | } |
3529 | ||
3530 | /** | |
3531 | * drbd_queue_bitmap_io() - Queues an IO operation on the whole bitmap | |
b30ab791 | 3532 | * @device: DRBD device. |
b411b363 PR |
3533 | * @io_fn: IO callback to be called when bitmap IO is possible |
3534 | * @done: callback to be called after the bitmap IO was performed | |
3535 | * @why: Descriptive text of the reason for doing the IO | |
3536 | * | |
3537 | * While IO on the bitmap happens we freeze application IO thus we ensure | |
3538 | * that drbd_set_out_of_sync() can not be called. This function MAY ONLY be | |
3539 | * called from worker context. It MUST NOT be used while a previous such | |
3540 | * work is still pending! | |
8fe39aac PR |
3541 | * |
3542 | * Its worker function encloses the call of io_fn() by get_ldev() and | |
3543 | * put_ldev(). | |
b411b363 | 3544 | */ |
b30ab791 | 3545 | void drbd_queue_bitmap_io(struct drbd_device *device, |
54761697 AG |
3546 | int (*io_fn)(struct drbd_device *), |
3547 | void (*done)(struct drbd_device *, int), | |
20ceb2b2 | 3548 | char *why, enum bm_flag flags) |
b411b363 | 3549 | { |
0b0ba1ef | 3550 | D_ASSERT(device, current == first_peer_device(device)->connection->worker.task); |
b411b363 | 3551 | |
0b0ba1ef AG |
3552 | D_ASSERT(device, !test_bit(BITMAP_IO_QUEUED, &device->flags)); |
3553 | D_ASSERT(device, !test_bit(BITMAP_IO, &device->flags)); | |
3554 | D_ASSERT(device, list_empty(&device->bm_io_work.w.list)); | |
b30ab791 | 3555 | if (device->bm_io_work.why) |
d0180171 | 3556 | drbd_err(device, "FIXME going to queue '%s' but '%s' still pending?\n", |
b30ab791 | 3557 | why, device->bm_io_work.why); |
b411b363 | 3558 | |
b30ab791 AG |
3559 | device->bm_io_work.io_fn = io_fn; |
3560 | device->bm_io_work.done = done; | |
3561 | device->bm_io_work.why = why; | |
3562 | device->bm_io_work.flags = flags; | |
b411b363 | 3563 | |
0500813f | 3564 | spin_lock_irq(&device->resource->req_lock); |
b30ab791 AG |
3565 | set_bit(BITMAP_IO, &device->flags); |
3566 | if (atomic_read(&device->ap_bio_cnt) == 0) { | |
3567 | if (!test_and_set_bit(BITMAP_IO_QUEUED, &device->flags)) | |
84b8c06b AG |
3568 | drbd_queue_work(&first_peer_device(device)->connection->sender_work, |
3569 | &device->bm_io_work.w); | |
b411b363 | 3570 | } |
0500813f | 3571 | spin_unlock_irq(&device->resource->req_lock); |
b411b363 PR |
3572 | } |
3573 | ||
3574 | /** | |
3575 | * drbd_bitmap_io() - Does an IO operation on the whole bitmap | |
b30ab791 | 3576 | * @device: DRBD device. |
b411b363 PR |
3577 | * @io_fn: IO callback to be called when bitmap IO is possible |
3578 | * @why: Descriptive text of the reason for doing the IO | |
3579 | * | |
3580 | * freezes application IO while that the actual IO operations runs. This | |
3581 | * functions MAY NOT be called from worker context. | |
3582 | */ | |
b30ab791 | 3583 | int drbd_bitmap_io(struct drbd_device *device, int (*io_fn)(struct drbd_device *), |
20ceb2b2 | 3584 | char *why, enum bm_flag flags) |
b411b363 PR |
3585 | { |
3586 | int rv; | |
3587 | ||
0b0ba1ef | 3588 | D_ASSERT(device, current != first_peer_device(device)->connection->worker.task); |
b411b363 | 3589 | |
20ceb2b2 | 3590 | if ((flags & BM_LOCKED_SET_ALLOWED) == 0) |
b30ab791 | 3591 | drbd_suspend_io(device); |
b411b363 | 3592 | |
b30ab791 AG |
3593 | drbd_bm_lock(device, why, flags); |
3594 | rv = io_fn(device); | |
3595 | drbd_bm_unlock(device); | |
b411b363 | 3596 | |
20ceb2b2 | 3597 | if ((flags & BM_LOCKED_SET_ALLOWED) == 0) |
b30ab791 | 3598 | drbd_resume_io(device); |
b411b363 PR |
3599 | |
3600 | return rv; | |
3601 | } | |
3602 | ||
b30ab791 | 3603 | void drbd_md_set_flag(struct drbd_device *device, int flag) __must_hold(local) |
b411b363 | 3604 | { |
b30ab791 AG |
3605 | if ((device->ldev->md.flags & flag) != flag) { |
3606 | drbd_md_mark_dirty(device); | |
3607 | device->ldev->md.flags |= flag; | |
b411b363 PR |
3608 | } |
3609 | } | |
3610 | ||
b30ab791 | 3611 | void drbd_md_clear_flag(struct drbd_device *device, int flag) __must_hold(local) |
b411b363 | 3612 | { |
b30ab791 AG |
3613 | if ((device->ldev->md.flags & flag) != 0) { |
3614 | drbd_md_mark_dirty(device); | |
3615 | device->ldev->md.flags &= ~flag; | |
b411b363 PR |
3616 | } |
3617 | } | |
3618 | int drbd_md_test_flag(struct drbd_backing_dev *bdev, int flag) | |
3619 | { | |
3620 | return (bdev->md.flags & flag) != 0; | |
3621 | } | |
3622 | ||
3623 | static void md_sync_timer_fn(unsigned long data) | |
3624 | { | |
b30ab791 | 3625 | struct drbd_device *device = (struct drbd_device *) data; |
b411b363 | 3626 | |
b792b655 | 3627 | /* must not double-queue! */ |
b30ab791 | 3628 | if (list_empty(&device->md_sync_work.list)) |
84b8c06b AG |
3629 | drbd_queue_work_front(&first_peer_device(device)->connection->sender_work, |
3630 | &device->md_sync_work); | |
b411b363 PR |
3631 | } |
3632 | ||
99920dc5 | 3633 | static int w_md_sync(struct drbd_work *w, int unused) |
b411b363 | 3634 | { |
84b8c06b AG |
3635 | struct drbd_device *device = |
3636 | container_of(w, struct drbd_device, md_sync_work); | |
00d56944 | 3637 | |
d0180171 | 3638 | drbd_warn(device, "md_sync_timer expired! Worker calls drbd_md_sync().\n"); |
ee15b038 | 3639 | #ifdef DEBUG |
d0180171 | 3640 | drbd_warn(device, "last md_mark_dirty: %s:%u\n", |
b30ab791 | 3641 | device->last_md_mark_dirty.func, device->last_md_mark_dirty.line); |
ee15b038 | 3642 | #endif |
b30ab791 | 3643 | drbd_md_sync(device); |
99920dc5 | 3644 | return 0; |
b411b363 PR |
3645 | } |
3646 | ||
d8763023 | 3647 | const char *cmdname(enum drbd_packet cmd) |
f2ad9063 AG |
3648 | { |
3649 | /* THINK may need to become several global tables | |
3650 | * when we want to support more than | |
3651 | * one PRO_VERSION */ | |
3652 | static const char *cmdnames[] = { | |
3653 | [P_DATA] = "Data", | |
3654 | [P_DATA_REPLY] = "DataReply", | |
3655 | [P_RS_DATA_REPLY] = "RSDataReply", | |
3656 | [P_BARRIER] = "Barrier", | |
3657 | [P_BITMAP] = "ReportBitMap", | |
3658 | [P_BECOME_SYNC_TARGET] = "BecomeSyncTarget", | |
3659 | [P_BECOME_SYNC_SOURCE] = "BecomeSyncSource", | |
3660 | [P_UNPLUG_REMOTE] = "UnplugRemote", | |
3661 | [P_DATA_REQUEST] = "DataRequest", | |
3662 | [P_RS_DATA_REQUEST] = "RSDataRequest", | |
3663 | [P_SYNC_PARAM] = "SyncParam", | |
3664 | [P_SYNC_PARAM89] = "SyncParam89", | |
3665 | [P_PROTOCOL] = "ReportProtocol", | |
3666 | [P_UUIDS] = "ReportUUIDs", | |
3667 | [P_SIZES] = "ReportSizes", | |
3668 | [P_STATE] = "ReportState", | |
3669 | [P_SYNC_UUID] = "ReportSyncUUID", | |
3670 | [P_AUTH_CHALLENGE] = "AuthChallenge", | |
3671 | [P_AUTH_RESPONSE] = "AuthResponse", | |
3672 | [P_PING] = "Ping", | |
3673 | [P_PING_ACK] = "PingAck", | |
3674 | [P_RECV_ACK] = "RecvAck", | |
3675 | [P_WRITE_ACK] = "WriteAck", | |
3676 | [P_RS_WRITE_ACK] = "RSWriteAck", | |
d4dabbe2 | 3677 | [P_SUPERSEDED] = "Superseded", |
f2ad9063 AG |
3678 | [P_NEG_ACK] = "NegAck", |
3679 | [P_NEG_DREPLY] = "NegDReply", | |
3680 | [P_NEG_RS_DREPLY] = "NegRSDReply", | |
3681 | [P_BARRIER_ACK] = "BarrierAck", | |
3682 | [P_STATE_CHG_REQ] = "StateChgRequest", | |
3683 | [P_STATE_CHG_REPLY] = "StateChgReply", | |
3684 | [P_OV_REQUEST] = "OVRequest", | |
3685 | [P_OV_REPLY] = "OVReply", | |
3686 | [P_OV_RESULT] = "OVResult", | |
3687 | [P_CSUM_RS_REQUEST] = "CsumRSRequest", | |
3688 | [P_RS_IS_IN_SYNC] = "CsumRSIsInSync", | |
3689 | [P_COMPRESSED_BITMAP] = "CBitmap", | |
3690 | [P_DELAY_PROBE] = "DelayProbe", | |
3691 | [P_OUT_OF_SYNC] = "OutOfSync", | |
7be8da07 | 3692 | [P_RETRY_WRITE] = "RetryWrite", |
ae25b336 LE |
3693 | [P_RS_CANCEL] = "RSCancel", |
3694 | [P_CONN_ST_CHG_REQ] = "conn_st_chg_req", | |
3695 | [P_CONN_ST_CHG_REPLY] = "conn_st_chg_reply", | |
036b17ea PR |
3696 | [P_RETRY_WRITE] = "retry_write", |
3697 | [P_PROTOCOL_UPDATE] = "protocol_update", | |
ae25b336 LE |
3698 | |
3699 | /* enum drbd_packet, but not commands - obsoleted flags: | |
3700 | * P_MAY_IGNORE | |
3701 | * P_MAX_OPT_CMD | |
3702 | */ | |
f2ad9063 AG |
3703 | }; |
3704 | ||
ae25b336 | 3705 | /* too big for the array: 0xfffX */ |
e5d6f33a AG |
3706 | if (cmd == P_INITIAL_META) |
3707 | return "InitialMeta"; | |
3708 | if (cmd == P_INITIAL_DATA) | |
3709 | return "InitialData"; | |
6038178e AG |
3710 | if (cmd == P_CONNECTION_FEATURES) |
3711 | return "ConnectionFeatures"; | |
6e849ce8 | 3712 | if (cmd >= ARRAY_SIZE(cmdnames)) |
f2ad9063 AG |
3713 | return "Unknown"; |
3714 | return cmdnames[cmd]; | |
3715 | } | |
3716 | ||
7be8da07 AG |
3717 | /** |
3718 | * drbd_wait_misc - wait for a request to make progress | |
b30ab791 | 3719 | * @device: device associated with the request |
7be8da07 AG |
3720 | * @i: the struct drbd_interval embedded in struct drbd_request or |
3721 | * struct drbd_peer_request | |
3722 | */ | |
b30ab791 | 3723 | int drbd_wait_misc(struct drbd_device *device, struct drbd_interval *i) |
7be8da07 | 3724 | { |
44ed167d | 3725 | struct net_conf *nc; |
7be8da07 AG |
3726 | DEFINE_WAIT(wait); |
3727 | long timeout; | |
3728 | ||
44ed167d | 3729 | rcu_read_lock(); |
a6b32bc3 | 3730 | nc = rcu_dereference(first_peer_device(device)->connection->net_conf); |
44ed167d PR |
3731 | if (!nc) { |
3732 | rcu_read_unlock(); | |
7be8da07 | 3733 | return -ETIMEDOUT; |
44ed167d PR |
3734 | } |
3735 | timeout = nc->ko_count ? nc->timeout * HZ / 10 * nc->ko_count : MAX_SCHEDULE_TIMEOUT; | |
3736 | rcu_read_unlock(); | |
7be8da07 | 3737 | |
b30ab791 | 3738 | /* Indicate to wake up device->misc_wait on progress. */ |
7be8da07 | 3739 | i->waiting = true; |
b30ab791 | 3740 | prepare_to_wait(&device->misc_wait, &wait, TASK_INTERRUPTIBLE); |
0500813f | 3741 | spin_unlock_irq(&device->resource->req_lock); |
7be8da07 | 3742 | timeout = schedule_timeout(timeout); |
b30ab791 | 3743 | finish_wait(&device->misc_wait, &wait); |
0500813f | 3744 | spin_lock_irq(&device->resource->req_lock); |
b30ab791 | 3745 | if (!timeout || device->state.conn < C_CONNECTED) |
7be8da07 AG |
3746 | return -ETIMEDOUT; |
3747 | if (signal_pending(current)) | |
3748 | return -ERESTARTSYS; | |
3749 | return 0; | |
b411b363 PR |
3750 | } |
3751 | ||
3752 | #ifdef CONFIG_DRBD_FAULT_INJECTION | |
3753 | /* Fault insertion support including random number generator shamelessly | |
3754 | * stolen from kernel/rcutorture.c */ | |
3755 | struct fault_random_state { | |
3756 | unsigned long state; | |
3757 | unsigned long count; | |
3758 | }; | |
3759 | ||
3760 | #define FAULT_RANDOM_MULT 39916801 /* prime */ | |
3761 | #define FAULT_RANDOM_ADD 479001701 /* prime */ | |
3762 | #define FAULT_RANDOM_REFRESH 10000 | |
3763 | ||
3764 | /* | |
3765 | * Crude but fast random-number generator. Uses a linear congruential | |
3766 | * generator, with occasional help from get_random_bytes(). | |
3767 | */ | |
3768 | static unsigned long | |
3769 | _drbd_fault_random(struct fault_random_state *rsp) | |
3770 | { | |
3771 | long refresh; | |
3772 | ||
49829ea7 | 3773 | if (!rsp->count--) { |
b411b363 PR |
3774 | get_random_bytes(&refresh, sizeof(refresh)); |
3775 | rsp->state += refresh; | |
3776 | rsp->count = FAULT_RANDOM_REFRESH; | |
3777 | } | |
3778 | rsp->state = rsp->state * FAULT_RANDOM_MULT + FAULT_RANDOM_ADD; | |
3779 | return swahw32(rsp->state); | |
3780 | } | |
3781 | ||
3782 | static char * | |
3783 | _drbd_fault_str(unsigned int type) { | |
3784 | static char *_faults[] = { | |
3785 | [DRBD_FAULT_MD_WR] = "Meta-data write", | |
3786 | [DRBD_FAULT_MD_RD] = "Meta-data read", | |
3787 | [DRBD_FAULT_RS_WR] = "Resync write", | |
3788 | [DRBD_FAULT_RS_RD] = "Resync read", | |
3789 | [DRBD_FAULT_DT_WR] = "Data write", | |
3790 | [DRBD_FAULT_DT_RD] = "Data read", | |
3791 | [DRBD_FAULT_DT_RA] = "Data read ahead", | |
3792 | [DRBD_FAULT_BM_ALLOC] = "BM allocation", | |
6b4388ac PR |
3793 | [DRBD_FAULT_AL_EE] = "EE allocation", |
3794 | [DRBD_FAULT_RECEIVE] = "receive data corruption", | |
b411b363 PR |
3795 | }; |
3796 | ||
3797 | return (type < DRBD_FAULT_MAX) ? _faults[type] : "**Unknown**"; | |
3798 | } | |
3799 | ||
3800 | unsigned int | |
b30ab791 | 3801 | _drbd_insert_fault(struct drbd_device *device, unsigned int type) |
b411b363 PR |
3802 | { |
3803 | static struct fault_random_state rrs = {0, 0}; | |
3804 | ||
3805 | unsigned int ret = ( | |
3806 | (fault_devs == 0 || | |
b30ab791 | 3807 | ((1 << device_to_minor(device)) & fault_devs) != 0) && |
b411b363 PR |
3808 | (((_drbd_fault_random(&rrs) % 100) + 1) <= fault_rate)); |
3809 | ||
3810 | if (ret) { | |
3811 | fault_count++; | |
3812 | ||
7383506c | 3813 | if (__ratelimit(&drbd_ratelimit_state)) |
d0180171 | 3814 | drbd_warn(device, "***Simulating %s failure\n", |
b411b363 PR |
3815 | _drbd_fault_str(type)); |
3816 | } | |
3817 | ||
3818 | return ret; | |
3819 | } | |
3820 | #endif | |
3821 | ||
3822 | const char *drbd_buildtag(void) | |
3823 | { | |
3824 | /* DRBD built from external sources has here a reference to the | |
3825 | git hash of the source code. */ | |
3826 | ||
3827 | static char buildtag[38] = "\0uilt-in"; | |
3828 | ||
3829 | if (buildtag[0] == 0) { | |
bc4854bc CW |
3830 | #ifdef MODULE |
3831 | sprintf(buildtag, "srcversion: %-24s", THIS_MODULE->srcversion); | |
3832 | #else | |
3833 | buildtag[0] = 'b'; | |
b411b363 | 3834 | #endif |
b411b363 PR |
3835 | } |
3836 | ||
3837 | return buildtag; | |
3838 | } | |
3839 | ||
3840 | module_init(drbd_init) | |
3841 | module_exit(drbd_cleanup) | |
3842 | ||
b411b363 PR |
3843 | EXPORT_SYMBOL(drbd_conn_str); |
3844 | EXPORT_SYMBOL(drbd_role_str); | |
3845 | EXPORT_SYMBOL(drbd_disk_str); | |
3846 | EXPORT_SYMBOL(drbd_set_st_err_str); |