blob: 946166e13953cca1d6c64d3feb9757d60690922d [file] [log] [blame]
Philipp Reisnerb411b362009-09-25 16:07:19 -07001/*
2 drbd_nl.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 drbd is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 drbd is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with drbd; see the file COPYING. If not, write to
22 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 */
25
Philipp Reisnerb411b362009-09-25 16:07:19 -070026#include <linux/module.h>
27#include <linux/drbd.h>
28#include <linux/in.h>
29#include <linux/fs.h>
30#include <linux/file.h>
31#include <linux/slab.h>
32#include <linux/connector.h>
33#include <linux/blkpg.h>
34#include <linux/cpumask.h>
35#include "drbd_int.h"
Philipp Reisner265be2d2010-05-31 10:14:17 +020036#include "drbd_req.h"
Philipp Reisnerb411b362009-09-25 16:07:19 -070037#include "drbd_wrappers.h"
38#include <asm/unaligned.h>
39#include <linux/drbd_tag_magic.h>
40#include <linux/drbd_limits.h>
Philipp Reisner87f7be42010-06-11 13:56:33 +020041#include <linux/compiler.h>
42#include <linux/kthread.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070043
44static unsigned short *tl_add_blob(unsigned short *, enum drbd_tags, const void *, int);
45static unsigned short *tl_add_str(unsigned short *, enum drbd_tags, const char *);
46static unsigned short *tl_add_int(unsigned short *, enum drbd_tags, const void *);
47
48/* see get_sb_bdev and bd_claim */
49static char *drbd_m_holder = "Hands off! this is DRBD's meta data device.";
50
51/* Generate the tag_list to struct functions */
52#define NL_PACKET(name, number, fields) \
53static int name ## _from_tags(struct drbd_conf *mdev, \
54 unsigned short *tags, struct name *arg) __attribute__ ((unused)); \
55static int name ## _from_tags(struct drbd_conf *mdev, \
56 unsigned short *tags, struct name *arg) \
57{ \
58 int tag; \
59 int dlen; \
60 \
61 while ((tag = get_unaligned(tags++)) != TT_END) { \
62 dlen = get_unaligned(tags++); \
63 switch (tag_number(tag)) { \
64 fields \
65 default: \
66 if (tag & T_MANDATORY) { \
67 dev_err(DEV, "Unknown tag: %d\n", tag_number(tag)); \
68 return 0; \
69 } \
70 } \
71 tags = (unsigned short *)((char *)tags + dlen); \
72 } \
73 return 1; \
74}
75#define NL_INTEGER(pn, pr, member) \
76 case pn: /* D_ASSERT( tag_type(tag) == TT_INTEGER ); */ \
77 arg->member = get_unaligned((int *)(tags)); \
78 break;
79#define NL_INT64(pn, pr, member) \
80 case pn: /* D_ASSERT( tag_type(tag) == TT_INT64 ); */ \
81 arg->member = get_unaligned((u64 *)(tags)); \
82 break;
83#define NL_BIT(pn, pr, member) \
84 case pn: /* D_ASSERT( tag_type(tag) == TT_BIT ); */ \
85 arg->member = *(char *)(tags) ? 1 : 0; \
86 break;
87#define NL_STRING(pn, pr, member, len) \
88 case pn: /* D_ASSERT( tag_type(tag) == TT_STRING ); */ \
89 if (dlen > len) { \
90 dev_err(DEV, "arg too long: %s (%u wanted, max len: %u bytes)\n", \
91 #member, dlen, (unsigned int)len); \
92 return 0; \
93 } \
94 arg->member ## _len = dlen; \
95 memcpy(arg->member, tags, min_t(size_t, dlen, len)); \
96 break;
Joe Perches1d273b92011-06-03 02:28:46 -070097#include <linux/drbd_nl.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070098
99/* Generate the struct to tag_list functions */
100#define NL_PACKET(name, number, fields) \
101static unsigned short* \
102name ## _to_tags(struct drbd_conf *mdev, \
103 struct name *arg, unsigned short *tags) __attribute__ ((unused)); \
104static unsigned short* \
105name ## _to_tags(struct drbd_conf *mdev, \
106 struct name *arg, unsigned short *tags) \
107{ \
108 fields \
109 return tags; \
110}
111
112#define NL_INTEGER(pn, pr, member) \
113 put_unaligned(pn | pr | TT_INTEGER, tags++); \
114 put_unaligned(sizeof(int), tags++); \
115 put_unaligned(arg->member, (int *)tags); \
116 tags = (unsigned short *)((char *)tags+sizeof(int));
117#define NL_INT64(pn, pr, member) \
118 put_unaligned(pn | pr | TT_INT64, tags++); \
119 put_unaligned(sizeof(u64), tags++); \
120 put_unaligned(arg->member, (u64 *)tags); \
121 tags = (unsigned short *)((char *)tags+sizeof(u64));
122#define NL_BIT(pn, pr, member) \
123 put_unaligned(pn | pr | TT_BIT, tags++); \
124 put_unaligned(sizeof(char), tags++); \
125 *(char *)tags = arg->member; \
126 tags = (unsigned short *)((char *)tags+sizeof(char));
127#define NL_STRING(pn, pr, member, len) \
128 put_unaligned(pn | pr | TT_STRING, tags++); \
129 put_unaligned(arg->member ## _len, tags++); \
130 memcpy(tags, arg->member, arg->member ## _len); \
131 tags = (unsigned short *)((char *)tags + arg->member ## _len);
Joe Perches1d273b92011-06-03 02:28:46 -0700132#include <linux/drbd_nl.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -0700133
134void drbd_bcast_ev_helper(struct drbd_conf *mdev, char *helper_name);
135void drbd_nl_send_reply(struct cn_msg *, int);
136
137int drbd_khelper(struct drbd_conf *mdev, char *cmd)
138{
139 char *envp[] = { "HOME=/",
140 "TERM=linux",
141 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
142 NULL, /* Will be set to address family */
143 NULL, /* Will be set to address */
144 NULL };
145
146 char mb[12], af[20], ad[60], *afs;
147 char *argv[] = {usermode_helper, cmd, mb, NULL };
148 int ret;
149
150 snprintf(mb, 12, "minor-%d", mdev_to_minor(mdev));
151
152 if (get_net_conf(mdev)) {
153 switch (((struct sockaddr *)mdev->net_conf->peer_addr)->sa_family) {
154 case AF_INET6:
155 afs = "ipv6";
156 snprintf(ad, 60, "DRBD_PEER_ADDRESS=%pI6",
157 &((struct sockaddr_in6 *)mdev->net_conf->peer_addr)->sin6_addr);
158 break;
159 case AF_INET:
160 afs = "ipv4";
161 snprintf(ad, 60, "DRBD_PEER_ADDRESS=%pI4",
162 &((struct sockaddr_in *)mdev->net_conf->peer_addr)->sin_addr);
163 break;
164 default:
165 afs = "ssocks";
166 snprintf(ad, 60, "DRBD_PEER_ADDRESS=%pI4",
167 &((struct sockaddr_in *)mdev->net_conf->peer_addr)->sin_addr);
168 }
169 snprintf(af, 20, "DRBD_PEER_AF=%s", afs);
170 envp[3]=af;
171 envp[4]=ad;
172 put_net_conf(mdev);
173 }
174
Lars Ellenberg1090c052010-07-19 17:41:04 +0200175 /* The helper may take some time.
176 * write out any unsynced meta data changes now */
177 drbd_md_sync(mdev);
178
Philipp Reisnerb411b362009-09-25 16:07:19 -0700179 dev_info(DEV, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
180
181 drbd_bcast_ev_helper(mdev, cmd);
Oleg Nesterov70834d32012-03-23 15:02:46 -0700182 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700183 if (ret)
184 dev_warn(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
185 usermode_helper, cmd, mb,
186 (ret >> 8) & 0xff, ret);
187 else
188 dev_info(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
189 usermode_helper, cmd, mb,
190 (ret >> 8) & 0xff, ret);
191
192 if (ret < 0) /* Ignore any ERRNOs we got. */
193 ret = 0;
194
195 return ret;
196}
197
198enum drbd_disk_state drbd_try_outdate_peer(struct drbd_conf *mdev)
199{
200 char *ex_to_string;
201 int r;
202 enum drbd_disk_state nps;
203 enum drbd_fencing_p fp;
204
205 D_ASSERT(mdev->state.pdsk == D_UNKNOWN);
206
207 if (get_ldev_if_state(mdev, D_CONSISTENT)) {
208 fp = mdev->ldev->dc.fencing;
209 put_ldev(mdev);
210 } else {
211 dev_warn(DEV, "Not fencing peer, I'm not even Consistent myself.\n");
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200212 nps = mdev->state.pdsk;
213 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700214 }
215
Philipp Reisnerb411b362009-09-25 16:07:19 -0700216 r = drbd_khelper(mdev, "fence-peer");
217
218 switch ((r>>8) & 0xff) {
219 case 3: /* peer is inconsistent */
220 ex_to_string = "peer is inconsistent or worse";
221 nps = D_INCONSISTENT;
222 break;
223 case 4: /* peer got outdated, or was already outdated */
224 ex_to_string = "peer was fenced";
225 nps = D_OUTDATED;
226 break;
227 case 5: /* peer was down */
228 if (mdev->state.disk == D_UP_TO_DATE) {
229 /* we will(have) create(d) a new UUID anyways... */
230 ex_to_string = "peer is unreachable, assumed to be dead";
231 nps = D_OUTDATED;
232 } else {
233 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
234 nps = mdev->state.pdsk;
235 }
236 break;
237 case 6: /* Peer is primary, voluntarily outdate myself.
238 * This is useful when an unconnected R_SECONDARY is asked to
239 * become R_PRIMARY, but finds the other peer being active. */
240 ex_to_string = "peer is active";
241 dev_warn(DEV, "Peer is primary, outdating myself.\n");
242 nps = D_UNKNOWN;
243 _drbd_request_state(mdev, NS(disk, D_OUTDATED), CS_WAIT_COMPLETE);
244 break;
245 case 7:
246 if (fp != FP_STONITH)
247 dev_err(DEV, "fence-peer() = 7 && fencing != Stonith !!!\n");
248 ex_to_string = "peer was stonithed";
249 nps = D_OUTDATED;
250 break;
251 default:
252 /* The script is broken ... */
253 nps = D_UNKNOWN;
254 dev_err(DEV, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
255 return nps;
256 }
257
258 dev_info(DEV, "fence-peer helper returned %d (%s)\n",
259 (r>>8) & 0xff, ex_to_string);
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200260
261out:
262 if (mdev->state.susp_fen && nps >= D_UNKNOWN) {
263 /* The handler was not successful... unfreeze here, the
264 state engine can not unfreeze... */
265 _drbd_request_state(mdev, NS(susp_fen, 0), CS_VERBOSE);
266 }
267
Philipp Reisnerb411b362009-09-25 16:07:19 -0700268 return nps;
269}
270
Philipp Reisner87f7be42010-06-11 13:56:33 +0200271static int _try_outdate_peer_async(void *data)
272{
273 struct drbd_conf *mdev = (struct drbd_conf *)data;
274 enum drbd_disk_state nps;
Philipp Reisner21423fa2011-05-17 14:19:41 +0200275 union drbd_state ns;
Philipp Reisner87f7be42010-06-11 13:56:33 +0200276
277 nps = drbd_try_outdate_peer(mdev);
Philipp Reisner21423fa2011-05-17 14:19:41 +0200278
279 /* Not using
280 drbd_request_state(mdev, NS(pdsk, nps));
Philipp Reisner99432fc2011-05-20 16:39:13 +0200281 here, because we might were able to re-establish the connection
282 in the meantime. This can only partially be solved in the state's
283 engine is_valid_state() and is_valid_state_transition()
284 functions.
285
286 nps can be D_INCONSISTENT, D_OUTDATED or D_UNKNOWN.
287 pdsk == D_INCONSISTENT while conn >= C_CONNECTED is valid,
288 therefore we have to have the pre state change check here.
Philipp Reisner21423fa2011-05-17 14:19:41 +0200289 */
290 spin_lock_irq(&mdev->req_lock);
291 ns = mdev->state;
292 if (ns.conn < C_WF_REPORT_PARAMS) {
293 ns.pdsk = nps;
294 _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
295 }
296 spin_unlock_irq(&mdev->req_lock);
Philipp Reisner87f7be42010-06-11 13:56:33 +0200297
298 return 0;
299}
300
301void drbd_try_outdate_peer_async(struct drbd_conf *mdev)
302{
303 struct task_struct *opa;
304
305 opa = kthread_run(_try_outdate_peer_async, mdev, "drbd%d_a_helper", mdev_to_minor(mdev));
306 if (IS_ERR(opa))
307 dev_err(DEV, "out of mem, failed to invoke fence-peer helper\n");
308}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700309
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100310enum drbd_state_rv
311drbd_set_role(struct drbd_conf *mdev, enum drbd_role new_role, int force)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700312{
313 const int max_tries = 4;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100314 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700315 int try = 0;
316 int forced = 0;
317 union drbd_state mask, val;
318 enum drbd_disk_state nps;
319
320 if (new_role == R_PRIMARY)
321 request_ping(mdev); /* Detect a dead peer ASAP */
322
323 mutex_lock(&mdev->state_mutex);
324
325 mask.i = 0; mask.role = R_MASK;
326 val.i = 0; val.role = new_role;
327
328 while (try++ < max_tries) {
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100329 rv = _drbd_request_state(mdev, mask, val, CS_WAIT_COMPLETE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700330
331 /* in case we first succeeded to outdate,
332 * but now suddenly could establish a connection */
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100333 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700334 val.pdsk = 0;
335 mask.pdsk = 0;
336 continue;
337 }
338
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100339 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
Philipp Reisnerd10a33c2010-03-04 15:11:39 +0100340 (mdev->state.disk < D_UP_TO_DATE &&
341 mdev->state.disk >= D_INCONSISTENT)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700342 mask.disk = D_MASK;
343 val.disk = D_UP_TO_DATE;
344 forced = 1;
345 continue;
346 }
347
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100348 if (rv == SS_NO_UP_TO_DATE_DISK &&
Philipp Reisnerb411b362009-09-25 16:07:19 -0700349 mdev->state.disk == D_CONSISTENT && mask.pdsk == 0) {
350 D_ASSERT(mdev->state.pdsk == D_UNKNOWN);
351 nps = drbd_try_outdate_peer(mdev);
352
353 if (nps == D_OUTDATED || nps == D_INCONSISTENT) {
354 val.disk = D_UP_TO_DATE;
355 mask.disk = D_MASK;
356 }
357
358 val.pdsk = nps;
359 mask.pdsk = D_MASK;
360
361 continue;
362 }
363
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100364 if (rv == SS_NOTHING_TO_DO)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700365 goto fail;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100366 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700367 nps = drbd_try_outdate_peer(mdev);
368
369 if (force && nps > D_OUTDATED) {
370 dev_warn(DEV, "Forced into split brain situation!\n");
371 nps = D_OUTDATED;
372 }
373
374 mask.pdsk = D_MASK;
375 val.pdsk = nps;
376
377 continue;
378 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100379 if (rv == SS_TWO_PRIMARIES) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700380 /* Maybe the peer is detected as dead very soon...
381 retry at most once more in this case. */
Philipp Reisner20ee6392011-01-18 15:28:59 +0100382 schedule_timeout_interruptible((mdev->net_conf->ping_timeo+1)*HZ/10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700383 if (try < max_tries)
384 try = max_tries - 1;
385 continue;
386 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100387 if (rv < SS_SUCCESS) {
388 rv = _drbd_request_state(mdev, mask, val,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700389 CS_VERBOSE + CS_WAIT_COMPLETE);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100390 if (rv < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700391 goto fail;
392 }
393 break;
394 }
395
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100396 if (rv < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700397 goto fail;
398
399 if (forced)
400 dev_warn(DEV, "Forced to consider local data as UpToDate!\n");
401
402 /* Wait until nothing is on the fly :) */
403 wait_event(mdev->misc_wait, atomic_read(&mdev->ap_pending_cnt) == 0);
404
405 if (new_role == R_SECONDARY) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100406 set_disk_ro(mdev->vdisk, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700407 if (get_ldev(mdev)) {
408 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
409 put_ldev(mdev);
410 }
411 } else {
412 if (get_net_conf(mdev)) {
413 mdev->net_conf->want_lose = 0;
414 put_net_conf(mdev);
415 }
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100416 set_disk_ro(mdev->vdisk, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700417 if (get_ldev(mdev)) {
418 if (((mdev->state.conn < C_CONNECTED ||
419 mdev->state.pdsk <= D_FAILED)
420 && mdev->ldev->md.uuid[UI_BITMAP] == 0) || forced)
421 drbd_uuid_new_current(mdev);
422
423 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
424 put_ldev(mdev);
425 }
426 }
427
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100428 /* writeout of activity log covered areas of the bitmap
429 * to stable storage done in after state change already */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700430
431 if (mdev->state.conn >= C_WF_REPORT_PARAMS) {
432 /* if this was forced, we should consider sync */
433 if (forced)
434 drbd_send_uuids(mdev);
435 drbd_send_state(mdev);
436 }
437
438 drbd_md_sync(mdev);
439
440 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
441 fail:
442 mutex_unlock(&mdev->state_mutex);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100443 return rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700444}
445
Lars Ellenbergef50a3e2010-09-01 14:39:30 +0200446static struct drbd_conf *ensure_mdev(int minor, int create)
447{
448 struct drbd_conf *mdev;
449
450 if (minor >= minor_count)
451 return NULL;
452
453 mdev = minor_to_mdev(minor);
454
455 if (!mdev && create) {
456 struct gendisk *disk = NULL;
457 mdev = drbd_new_device(minor);
458
459 spin_lock_irq(&drbd_pp_lock);
460 if (minor_table[minor] == NULL) {
461 minor_table[minor] = mdev;
462 disk = mdev->vdisk;
463 mdev = NULL;
464 } /* else: we lost the race */
465 spin_unlock_irq(&drbd_pp_lock);
466
467 if (disk) /* we won the race above */
468 /* in case we ever add a drbd_delete_device(),
469 * don't forget the del_gendisk! */
470 add_disk(disk);
471 else /* we lost the race above */
472 drbd_free_mdev(mdev);
473
474 mdev = minor_to_mdev(minor);
475 }
476
477 return mdev;
478}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700479
480static int drbd_nl_primary(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
481 struct drbd_nl_cfg_reply *reply)
482{
483 struct primary primary_args;
484
485 memset(&primary_args, 0, sizeof(struct primary));
486 if (!primary_from_tags(mdev, nlp->tag_list, &primary_args)) {
487 reply->ret_code = ERR_MANDATORY_TAG;
488 return 0;
489 }
490
491 reply->ret_code =
Philipp Reisner1f552432010-03-04 15:51:01 +0100492 drbd_set_role(mdev, R_PRIMARY, primary_args.primary_force);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700493
494 return 0;
495}
496
497static int drbd_nl_secondary(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
498 struct drbd_nl_cfg_reply *reply)
499{
500 reply->ret_code = drbd_set_role(mdev, R_SECONDARY, 0);
501
502 return 0;
503}
504
505/* initializes the md.*_offset members, so we are able to find
506 * the on disk meta data */
507static void drbd_md_set_sector_offsets(struct drbd_conf *mdev,
508 struct drbd_backing_dev *bdev)
509{
510 sector_t md_size_sect = 0;
511 switch (bdev->dc.meta_dev_idx) {
512 default:
513 /* v07 style fixed size indexed meta data */
514 bdev->md.md_size_sect = MD_RESERVED_SECT;
515 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
516 bdev->md.al_offset = MD_AL_OFFSET;
517 bdev->md.bm_offset = MD_BM_OFFSET;
518 break;
519 case DRBD_MD_INDEX_FLEX_EXT:
520 /* just occupy the full device; unit: sectors */
521 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
522 bdev->md.md_offset = 0;
523 bdev->md.al_offset = MD_AL_OFFSET;
524 bdev->md.bm_offset = MD_BM_OFFSET;
525 break;
526 case DRBD_MD_INDEX_INTERNAL:
527 case DRBD_MD_INDEX_FLEX_INT:
528 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
529 /* al size is still fixed */
530 bdev->md.al_offset = -MD_AL_MAX_SIZE;
531 /* we need (slightly less than) ~ this much bitmap sectors: */
532 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
533 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
534 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
535 md_size_sect = ALIGN(md_size_sect, 8);
536
537 /* plus the "drbd meta data super block",
538 * and the activity log; */
539 md_size_sect += MD_BM_OFFSET;
540
541 bdev->md.md_size_sect = md_size_sect;
542 /* bitmap offset is adjusted by 'super' block size */
543 bdev->md.bm_offset = -md_size_sect + MD_AL_OFFSET;
544 break;
545 }
546}
547
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100548/* input size is expected to be in KB */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700549char *ppsize(char *buf, unsigned long long size)
550{
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100551 /* Needs 9 bytes at max including trailing NUL:
552 * -1ULL ==> "16384 EB" */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700553 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
554 int base = 0;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100555 while (size >= 10000 && base < sizeof(units)-1) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700556 /* shift + round */
557 size = (size >> 10) + !!(size & (1<<9));
558 base++;
559 }
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100560 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700561
562 return buf;
563}
564
565/* there is still a theoretical deadlock when called from receiver
566 * on an D_INCONSISTENT R_PRIMARY:
567 * remote READ does inc_ap_bio, receiver would need to receive answer
568 * packet from remote to dec_ap_bio again.
569 * receiver receive_sizes(), comes here,
570 * waits for ap_bio_cnt == 0. -> deadlock.
571 * but this cannot happen, actually, because:
572 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
573 * (not connected, or bad/no disk on peer):
574 * see drbd_fail_request_early, ap_bio_cnt is zero.
575 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
576 * peer may not initiate a resize.
577 */
578void drbd_suspend_io(struct drbd_conf *mdev)
579{
580 set_bit(SUSPEND_IO, &mdev->flags);
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200581 if (is_susp(mdev->state))
Philipp Reisner265be2d2010-05-31 10:14:17 +0200582 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700583 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
584}
585
586void drbd_resume_io(struct drbd_conf *mdev)
587{
588 clear_bit(SUSPEND_IO, &mdev->flags);
589 wake_up(&mdev->misc_wait);
590}
591
592/**
593 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
594 * @mdev: DRBD device.
595 *
596 * Returns 0 on success, negative return values indicate errors.
597 * You should call drbd_md_sync() after calling this function.
598 */
Bart Van Assche24c48302011-05-21 18:32:29 +0200599enum determine_dev_size drbd_determine_dev_size(struct drbd_conf *mdev, enum dds_flags flags) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700600{
601 sector_t prev_first_sect, prev_size; /* previous meta location */
602 sector_t la_size;
603 sector_t size;
604 char ppb[10];
605
606 int md_moved, la_size_changed;
607 enum determine_dev_size rv = unchanged;
608
609 /* race:
610 * application request passes inc_ap_bio,
611 * but then cannot get an AL-reference.
612 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
613 *
614 * to avoid that:
615 * Suspend IO right here.
616 * still lock the act_log to not trigger ASSERTs there.
617 */
618 drbd_suspend_io(mdev);
619
620 /* no wait necessary anymore, actually we could assert that */
621 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
622
623 prev_first_sect = drbd_md_first_sector(mdev->ldev);
624 prev_size = mdev->ldev->md.md_size_sect;
625 la_size = mdev->ldev->md.la_size_sect;
626
627 /* TODO: should only be some assert here, not (re)init... */
628 drbd_md_set_sector_offsets(mdev, mdev->ldev);
629
Philipp Reisnerd8450302010-03-24 15:51:26 +0100630 size = drbd_new_dev_size(mdev, mdev->ldev, flags & DDSF_FORCED);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700631
632 if (drbd_get_capacity(mdev->this_bdev) != size ||
633 drbd_bm_capacity(mdev) != size) {
634 int err;
Philipp Reisner02d9a942010-03-24 16:23:03 +0100635 err = drbd_bm_resize(mdev, size, !(flags & DDSF_NO_RESYNC));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700636 if (unlikely(err)) {
637 /* currently there is only one error: ENOMEM! */
638 size = drbd_bm_capacity(mdev)>>1;
639 if (size == 0) {
640 dev_err(DEV, "OUT OF MEMORY! "
641 "Could not allocate bitmap!\n");
642 } else {
643 dev_err(DEV, "BM resizing failed. "
644 "Leaving size unchanged at size = %lu KB\n",
645 (unsigned long)size);
646 }
647 rv = dev_size_error;
648 }
649 /* racy, see comments above. */
650 drbd_set_my_capacity(mdev, size);
651 mdev->ldev->md.la_size_sect = size;
652 dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
653 (unsigned long long)size>>1);
654 }
655 if (rv == dev_size_error)
656 goto out;
657
658 la_size_changed = (la_size != mdev->ldev->md.la_size_sect);
659
660 md_moved = prev_first_sect != drbd_md_first_sector(mdev->ldev)
661 || prev_size != mdev->ldev->md.md_size_sect;
662
663 if (la_size_changed || md_moved) {
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100664 int err;
665
Philipp Reisnerb411b362009-09-25 16:07:19 -0700666 drbd_al_shrink(mdev); /* All extents inactive. */
667 dev_info(DEV, "Writing the whole bitmap, %s\n",
668 la_size_changed && md_moved ? "size changed and md moved" :
669 la_size_changed ? "size changed" : "md moved");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100670 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
671 err = drbd_bitmap_io(mdev, &drbd_bm_write,
672 "size changed", BM_LOCKED_MASK);
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100673 if (err) {
674 rv = dev_size_error;
675 goto out;
676 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700677 drbd_md_mark_dirty(mdev);
678 }
679
680 if (size > la_size)
681 rv = grew;
682 if (size < la_size)
683 rv = shrunk;
684out:
685 lc_unlock(mdev->act_log);
686 wake_up(&mdev->al_wait);
687 drbd_resume_io(mdev);
688
689 return rv;
690}
691
692sector_t
Philipp Reisnera393db62009-12-22 13:35:52 +0100693drbd_new_dev_size(struct drbd_conf *mdev, struct drbd_backing_dev *bdev, int assume_peer_has_space)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700694{
695 sector_t p_size = mdev->p_size; /* partner's disk size. */
696 sector_t la_size = bdev->md.la_size_sect; /* last agreed size. */
697 sector_t m_size; /* my size */
698 sector_t u_size = bdev->dc.disk_size; /* size requested by user. */
699 sector_t size = 0;
700
701 m_size = drbd_get_max_capacity(bdev);
702
Philipp Reisnera393db62009-12-22 13:35:52 +0100703 if (mdev->state.conn < C_CONNECTED && assume_peer_has_space) {
704 dev_warn(DEV, "Resize while not connected was forced by the user!\n");
705 p_size = m_size;
706 }
707
Philipp Reisnerb411b362009-09-25 16:07:19 -0700708 if (p_size && m_size) {
709 size = min_t(sector_t, p_size, m_size);
710 } else {
711 if (la_size) {
712 size = la_size;
713 if (m_size && m_size < size)
714 size = m_size;
715 if (p_size && p_size < size)
716 size = p_size;
717 } else {
718 if (m_size)
719 size = m_size;
720 if (p_size)
721 size = p_size;
722 }
723 }
724
725 if (size == 0)
726 dev_err(DEV, "Both nodes diskless!\n");
727
728 if (u_size) {
729 if (u_size > size)
730 dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n",
731 (unsigned long)u_size>>1, (unsigned long)size>>1);
732 else
733 size = u_size;
734 }
735
736 return size;
737}
738
739/**
740 * drbd_check_al_size() - Ensures that the AL is of the right size
741 * @mdev: DRBD device.
742 *
743 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
744 * failed, and 0 on success. You should call drbd_md_sync() after you called
745 * this function.
746 */
747static int drbd_check_al_size(struct drbd_conf *mdev)
748{
749 struct lru_cache *n, *t;
750 struct lc_element *e;
751 unsigned int in_use;
752 int i;
753
754 ERR_IF(mdev->sync_conf.al_extents < 7)
755 mdev->sync_conf.al_extents = 127;
756
757 if (mdev->act_log &&
758 mdev->act_log->nr_elements == mdev->sync_conf.al_extents)
759 return 0;
760
761 in_use = 0;
762 t = mdev->act_log;
763 n = lc_create("act_log", drbd_al_ext_cache,
764 mdev->sync_conf.al_extents, sizeof(struct lc_element), 0);
765
766 if (n == NULL) {
767 dev_err(DEV, "Cannot allocate act_log lru!\n");
768 return -ENOMEM;
769 }
770 spin_lock_irq(&mdev->al_lock);
771 if (t) {
772 for (i = 0; i < t->nr_elements; i++) {
773 e = lc_element_by_index(t, i);
774 if (e->refcnt)
775 dev_err(DEV, "refcnt(%d)==%d\n",
776 e->lc_number, e->refcnt);
777 in_use += e->refcnt;
778 }
779 }
780 if (!in_use)
781 mdev->act_log = n;
782 spin_unlock_irq(&mdev->al_lock);
783 if (in_use) {
784 dev_err(DEV, "Activity log still in use!\n");
785 lc_destroy(n);
786 return -EBUSY;
787 } else {
788 if (t)
789 lc_destroy(t);
790 }
791 drbd_md_mark_dirty(mdev); /* we changed mdev->act_log->nr_elemens */
792 return 0;
793}
794
Philipp Reisner99432fc2011-05-20 16:39:13 +0200795static void drbd_setup_queue_param(struct drbd_conf *mdev, unsigned int max_bio_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700796{
797 struct request_queue * const q = mdev->rq_queue;
Philipp Reisner99432fc2011-05-20 16:39:13 +0200798 int max_hw_sectors = max_bio_size >> 9;
799 int max_segments = 0;
800
801 if (get_ldev_if_state(mdev, D_ATTACHING)) {
802 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
803
804 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
805 max_segments = mdev->ldev->dc.max_bio_bvecs;
806 put_ldev(mdev);
807 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700808
Philipp Reisnerb411b362009-09-25 16:07:19 -0700809 blk_queue_logical_block_size(q, 512);
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100810 blk_queue_max_hw_sectors(q, max_hw_sectors);
811 /* This is the workaround for "bio would need to, but cannot, be split" */
812 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
813 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700814
Philipp Reisner99432fc2011-05-20 16:39:13 +0200815 if (get_ldev_if_state(mdev, D_ATTACHING)) {
816 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700817
Philipp Reisner99432fc2011-05-20 16:39:13 +0200818 blk_queue_stack_limits(q, b);
819
820 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
821 dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
822 q->backing_dev_info.ra_pages,
823 b->backing_dev_info.ra_pages);
824 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
825 }
826 put_ldev(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700827 }
828}
829
Philipp Reisner99432fc2011-05-20 16:39:13 +0200830void drbd_reconsider_max_bio_size(struct drbd_conf *mdev)
831{
832 int now, new, local, peer;
833
834 now = queue_max_hw_sectors(mdev->rq_queue) << 9;
835 local = mdev->local_max_bio_size; /* Eventually last known value, from volatile memory */
836 peer = mdev->peer_max_bio_size; /* Eventually last known value, from meta data */
837
838 if (get_ldev_if_state(mdev, D_ATTACHING)) {
839 local = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
840 mdev->local_max_bio_size = local;
841 put_ldev(mdev);
842 }
843
844 /* We may ignore peer limits if the peer is modern enough.
845 Because new from 8.3.8 onwards the peer can use multiple
846 BIOs for a single peer_request */
847 if (mdev->state.conn >= C_CONNECTED) {
848 if (mdev->agreed_pro_version < 94)
849 peer = mdev->peer_max_bio_size;
850 else if (mdev->agreed_pro_version == 94)
851 peer = DRBD_MAX_SIZE_H80_PACKET;
852 else /* drbd 8.3.8 onwards */
853 peer = DRBD_MAX_BIO_SIZE;
854 }
855
856 new = min_t(int, local, peer);
857
858 if (mdev->state.role == R_PRIMARY && new < now)
859 dev_err(DEV, "ASSERT FAILED new < now; (%d < %d)\n", new, now);
860
861 if (new != now)
862 dev_info(DEV, "max BIO size = %u\n", new);
863
864 drbd_setup_queue_param(mdev, new);
865}
866
Philipp Reisnerb411b362009-09-25 16:07:19 -0700867/* serialize deconfig (worker exiting, doing cleanup)
868 * and reconfig (drbdsetup disk, drbdsetup net)
869 *
Lars Ellenbergc518d042010-09-01 09:50:23 +0200870 * Wait for a potentially exiting worker, then restart it,
871 * or start a new one. Flush any pending work, there may still be an
872 * after_state_change queued.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700873 */
874static void drbd_reconfig_start(struct drbd_conf *mdev)
875{
Lars Ellenberg6c6c7952009-11-16 15:48:54 +0100876 wait_event(mdev->state_wait, !test_and_set_bit(CONFIG_PENDING, &mdev->flags));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700877 wait_event(mdev->state_wait, !test_bit(DEVICE_DYING, &mdev->flags));
878 drbd_thread_start(&mdev->worker);
Lars Ellenbergc518d042010-09-01 09:50:23 +0200879 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700880}
881
882/* if still unconfigured, stops worker again.
883 * if configured now, clears CONFIG_PENDING.
884 * wakes potential waiters */
885static void drbd_reconfig_done(struct drbd_conf *mdev)
886{
887 spin_lock_irq(&mdev->req_lock);
888 if (mdev->state.disk == D_DISKLESS &&
889 mdev->state.conn == C_STANDALONE &&
890 mdev->state.role == R_SECONDARY) {
891 set_bit(DEVICE_DYING, &mdev->flags);
892 drbd_thread_stop_nowait(&mdev->worker);
893 } else
894 clear_bit(CONFIG_PENDING, &mdev->flags);
895 spin_unlock_irq(&mdev->req_lock);
896 wake_up(&mdev->state_wait);
897}
898
Philipp Reisner07782862010-08-31 12:00:50 +0200899/* Make sure IO is suspended before calling this function(). */
900static void drbd_suspend_al(struct drbd_conf *mdev)
901{
902 int s = 0;
903
904 if (lc_try_lock(mdev->act_log)) {
905 drbd_al_shrink(mdev);
906 lc_unlock(mdev->act_log);
907 } else {
908 dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n");
909 return;
910 }
911
912 spin_lock_irq(&mdev->req_lock);
913 if (mdev->state.conn < C_CONNECTED)
914 s = !test_and_set_bit(AL_SUSPENDED, &mdev->flags);
915
916 spin_unlock_irq(&mdev->req_lock);
917
918 if (s)
919 dev_info(DEV, "Suspended AL updates\n");
920}
921
Philipp Reisnerb411b362009-09-25 16:07:19 -0700922/* does always return 0;
923 * interesting return code is in reply->ret_code */
924static int drbd_nl_disk_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
925 struct drbd_nl_cfg_reply *reply)
926{
Andreas Gruenbacher116676c2010-12-08 13:33:11 +0100927 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700928 enum determine_dev_size dd;
929 sector_t max_possible_sectors;
930 sector_t min_md_device_sectors;
931 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
Tejun Heoe525fd82010-11-13 11:55:17 +0100932 struct block_device *bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700933 struct lru_cache *resync_lru = NULL;
934 union drbd_state ns, os;
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +0100935 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700936 int cp_discovered = 0;
937 int logical_block_size;
938
939 drbd_reconfig_start(mdev);
940
941 /* if you want to reconfigure, please tear down first */
942 if (mdev->state.disk > D_DISKLESS) {
943 retcode = ERR_DISK_CONFIGURED;
944 goto fail;
945 }
Lars Ellenberg82f59cc2010-10-16 12:13:47 +0200946 /* It may just now have detached because of IO error. Make sure
947 * drbd_ldev_destroy is done already, we may end up here very fast,
948 * e.g. if someone calls attach from the on-io-error handler,
949 * to realize a "hot spare" feature (not that I'd recommend that) */
950 wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700951
952 /* allocation not in the IO path, cqueue thread context */
953 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
954 if (!nbc) {
955 retcode = ERR_NOMEM;
956 goto fail;
957 }
958
959 nbc->dc.disk_size = DRBD_DISK_SIZE_SECT_DEF;
960 nbc->dc.on_io_error = DRBD_ON_IO_ERROR_DEF;
961 nbc->dc.fencing = DRBD_FENCING_DEF;
962 nbc->dc.max_bio_bvecs = DRBD_MAX_BIO_BVECS_DEF;
963
964 if (!disk_conf_from_tags(mdev, nlp->tag_list, &nbc->dc)) {
965 retcode = ERR_MANDATORY_TAG;
966 goto fail;
967 }
968
969 if (nbc->dc.meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
970 retcode = ERR_MD_IDX_INVALID;
971 goto fail;
972 }
973
Philipp Reisner47ff2d02010-06-18 13:56:57 +0200974 if (get_net_conf(mdev)) {
975 int prot = mdev->net_conf->wire_protocol;
976 put_net_conf(mdev);
977 if (nbc->dc.fencing == FP_STONITH && prot == DRBD_PROT_A) {
978 retcode = ERR_STONITH_AND_PROT_A;
979 goto fail;
980 }
981 }
982
Tejun Heod4d77622010-11-13 11:55:18 +0100983 bdev = blkdev_get_by_path(nbc->dc.backing_dev,
984 FMODE_READ | FMODE_WRITE | FMODE_EXCL, mdev);
Tejun Heoe525fd82010-11-13 11:55:17 +0100985 if (IS_ERR(bdev)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700986 dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.backing_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +0100987 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700988 retcode = ERR_OPEN_DISK;
989 goto fail;
990 }
Tejun Heoe525fd82010-11-13 11:55:17 +0100991 nbc->backing_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700992
Tejun Heoe525fd82010-11-13 11:55:17 +0100993 /*
994 * meta_dev_idx >= 0: external fixed size, possibly multiple
995 * drbd sharing one meta device. TODO in that case, paranoia
996 * check that [md_bdev, meta_dev_idx] is not yet used by some
997 * other drbd minor! (if you use drbd.conf + drbdadm, that
998 * should check it for you already; but if you don't, or
999 * someone fooled it, we need to double check here)
1000 */
Tejun Heod4d77622010-11-13 11:55:18 +01001001 bdev = blkdev_get_by_path(nbc->dc.meta_dev,
1002 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
1003 (nbc->dc.meta_dev_idx < 0) ?
1004 (void *)mdev : (void *)drbd_m_holder);
Tejun Heoe525fd82010-11-13 11:55:17 +01001005 if (IS_ERR(bdev)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001006 dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.meta_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001007 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001008 retcode = ERR_OPEN_MD_DISK;
1009 goto fail;
1010 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001011 nbc->md_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001012
Tejun Heoe525fd82010-11-13 11:55:17 +01001013 if ((nbc->backing_bdev == nbc->md_bdev) !=
1014 (nbc->dc.meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1015 nbc->dc.meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
1016 retcode = ERR_MD_IDX_INVALID;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001017 goto fail;
1018 }
1019
1020 resync_lru = lc_create("resync", drbd_bm_ext_cache,
1021 61, sizeof(struct bm_extent),
1022 offsetof(struct bm_extent, lce));
1023 if (!resync_lru) {
1024 retcode = ERR_NOMEM;
Tejun Heoe525fd82010-11-13 11:55:17 +01001025 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001026 }
1027
1028 /* RT - for drbd_get_max_capacity() DRBD_MD_INDEX_FLEX_INT */
1029 drbd_md_set_sector_offsets(mdev, nbc);
1030
1031 if (drbd_get_max_capacity(nbc) < nbc->dc.disk_size) {
1032 dev_err(DEV, "max capacity %llu smaller than disk size %llu\n",
1033 (unsigned long long) drbd_get_max_capacity(nbc),
1034 (unsigned long long) nbc->dc.disk_size);
1035 retcode = ERR_DISK_TO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001036 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001037 }
1038
1039 if (nbc->dc.meta_dev_idx < 0) {
1040 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1041 /* at least one MB, otherwise it does not make sense */
1042 min_md_device_sectors = (2<<10);
1043 } else {
1044 max_possible_sectors = DRBD_MAX_SECTORS;
1045 min_md_device_sectors = MD_RESERVED_SECT * (nbc->dc.meta_dev_idx + 1);
1046 }
1047
Philipp Reisnerb411b362009-09-25 16:07:19 -07001048 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
1049 retcode = ERR_MD_DISK_TO_SMALL;
1050 dev_warn(DEV, "refusing attach: md-device too small, "
1051 "at least %llu sectors needed for this meta-disk type\n",
1052 (unsigned long long) min_md_device_sectors);
Tejun Heoe525fd82010-11-13 11:55:17 +01001053 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001054 }
1055
1056 /* Make sure the new disk is big enough
1057 * (we may currently be R_PRIMARY with no local disk...) */
1058 if (drbd_get_max_capacity(nbc) <
1059 drbd_get_capacity(mdev->this_bdev)) {
1060 retcode = ERR_DISK_TO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001061 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001062 }
1063
1064 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1065
Lars Ellenberg13529942009-10-12 19:07:49 +02001066 if (nbc->known_size > max_possible_sectors) {
1067 dev_warn(DEV, "==> truncating very big lower level device "
1068 "to currently maximum possible %llu sectors <==\n",
1069 (unsigned long long) max_possible_sectors);
1070 if (nbc->dc.meta_dev_idx >= 0)
1071 dev_warn(DEV, "==>> using internal or flexible "
1072 "meta data may help <<==\n");
1073 }
1074
Philipp Reisnerb411b362009-09-25 16:07:19 -07001075 drbd_suspend_io(mdev);
1076 /* also wait for the last barrier ack. */
Philipp Reisnerfb22c402010-09-08 23:20:21 +02001077 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_pending_cnt) || is_susp(mdev->state));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001078 /* and for any other previously queued work */
1079 drbd_flush_workqueue(mdev);
1080
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001081 rv = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE);
1082 retcode = rv; /* FIXME: Type mismatch. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001083 drbd_resume_io(mdev);
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001084 if (rv < SS_SUCCESS)
Tejun Heoe525fd82010-11-13 11:55:17 +01001085 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001086
1087 if (!get_ldev_if_state(mdev, D_ATTACHING))
1088 goto force_diskless;
1089
1090 drbd_md_set_sector_offsets(mdev, nbc);
1091
Lars Ellenberg4aa83b72010-02-26 16:53:24 +01001092 /* allocate a second IO page if logical_block_size != 512 */
1093 logical_block_size = bdev_logical_block_size(nbc->md_bdev);
1094 if (logical_block_size == 0)
1095 logical_block_size = MD_SECTOR_SIZE;
1096
1097 if (logical_block_size != MD_SECTOR_SIZE) {
1098 if (!mdev->md_io_tmpp) {
1099 struct page *page = alloc_page(GFP_NOIO);
1100 if (!page)
1101 goto force_diskless_dec;
1102
1103 dev_warn(DEV, "Meta data's bdev logical_block_size = %d != %d\n",
1104 logical_block_size, MD_SECTOR_SIZE);
1105 dev_warn(DEV, "Workaround engaged (has performance impact).\n");
1106
1107 mdev->md_io_tmpp = page;
1108 }
1109 }
1110
Philipp Reisnerb411b362009-09-25 16:07:19 -07001111 if (!mdev->bitmap) {
1112 if (drbd_bm_init(mdev)) {
1113 retcode = ERR_NOMEM;
1114 goto force_diskless_dec;
1115 }
1116 }
1117
1118 retcode = drbd_md_read(mdev, nbc);
1119 if (retcode != NO_ERROR)
1120 goto force_diskless_dec;
1121
1122 if (mdev->state.conn < C_CONNECTED &&
1123 mdev->state.role == R_PRIMARY &&
1124 (mdev->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
1125 dev_err(DEV, "Can only attach to data with current UUID=%016llX\n",
1126 (unsigned long long)mdev->ed_uuid);
1127 retcode = ERR_DATA_NOT_CURRENT;
1128 goto force_diskless_dec;
1129 }
1130
1131 /* Since we are diskless, fix the activity log first... */
1132 if (drbd_check_al_size(mdev)) {
1133 retcode = ERR_NOMEM;
1134 goto force_diskless_dec;
1135 }
1136
1137 /* Prevent shrinking of consistent devices ! */
1138 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
Philipp Reisnera393db62009-12-22 13:35:52 +01001139 drbd_new_dev_size(mdev, nbc, 0) < nbc->md.la_size_sect) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001140 dev_warn(DEV, "refusing to truncate a consistent device\n");
1141 retcode = ERR_DISK_TO_SMALL;
1142 goto force_diskless_dec;
1143 }
1144
1145 if (!drbd_al_read_log(mdev, nbc)) {
1146 retcode = ERR_IO_MD_DISK;
1147 goto force_diskless_dec;
1148 }
1149
Philipp Reisnerb411b362009-09-25 16:07:19 -07001150 /* Reset the "barriers don't work" bits here, then force meta data to
1151 * be written, to ensure we determine if barriers are supported. */
1152 if (nbc->dc.no_md_flush)
Philipp Reisnera8a4e512010-08-25 10:21:04 +02001153 set_bit(MD_NO_FUA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001154 else
Philipp Reisnera8a4e512010-08-25 10:21:04 +02001155 clear_bit(MD_NO_FUA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001156
1157 /* Point of no return reached.
1158 * Devices and memory are no longer released by error cleanup below.
1159 * now mdev takes over responsibility, and the state engine should
1160 * clean it up somewhere. */
1161 D_ASSERT(mdev->ldev == NULL);
1162 mdev->ldev = nbc;
1163 mdev->resync = resync_lru;
1164 nbc = NULL;
1165 resync_lru = NULL;
1166
Philipp Reisner2451fc32010-08-24 13:43:11 +02001167 mdev->write_ordering = WO_bdev_flush;
1168 drbd_bump_write_ordering(mdev, WO_bdev_flush);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001169
1170 if (drbd_md_test_flag(mdev->ldev, MDF_CRASHED_PRIMARY))
1171 set_bit(CRASHED_PRIMARY, &mdev->flags);
1172 else
1173 clear_bit(CRASHED_PRIMARY, &mdev->flags);
1174
Philipp Reisner894c6a92010-06-18 16:03:20 +02001175 if (drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
Philipp Reisnerfb22c402010-09-08 23:20:21 +02001176 !(mdev->state.role == R_PRIMARY && mdev->state.susp_nod)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001177 set_bit(CRASHED_PRIMARY, &mdev->flags);
1178 cp_discovered = 1;
1179 }
1180
1181 mdev->send_cnt = 0;
1182 mdev->recv_cnt = 0;
1183 mdev->read_cnt = 0;
1184 mdev->writ_cnt = 0;
1185
Philipp Reisner99432fc2011-05-20 16:39:13 +02001186 drbd_reconsider_max_bio_size(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001187
1188 /* If I am currently not R_PRIMARY,
1189 * but meta data primary indicator is set,
1190 * I just now recover from a hard crash,
1191 * and have been R_PRIMARY before that crash.
1192 *
1193 * Now, if I had no connection before that crash
1194 * (have been degraded R_PRIMARY), chances are that
1195 * I won't find my peer now either.
1196 *
1197 * In that case, and _only_ in that case,
1198 * we use the degr-wfc-timeout instead of the default,
1199 * so we can automatically recover from a crash of a
1200 * degraded but active "cluster" after a certain timeout.
1201 */
1202 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
1203 if (mdev->state.role != R_PRIMARY &&
1204 drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
1205 !drbd_md_test_flag(mdev->ldev, MDF_CONNECTED_IND))
1206 set_bit(USE_DEGR_WFC_T, &mdev->flags);
1207
Bart Van Assche24c48302011-05-21 18:32:29 +02001208 dd = drbd_determine_dev_size(mdev, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001209 if (dd == dev_size_error) {
1210 retcode = ERR_NOMEM_BITMAP;
1211 goto force_diskless_dec;
1212 } else if (dd == grew)
1213 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
1214
1215 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
1216 dev_info(DEV, "Assuming that all blocks are out of sync "
1217 "(aka FullSync)\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001218 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write,
1219 "set_n_write from attaching", BM_LOCKED_MASK)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001220 retcode = ERR_IO_MD_DISK;
1221 goto force_diskless_dec;
1222 }
1223 } else {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001224 if (drbd_bitmap_io(mdev, &drbd_bm_read,
1225 "read from attaching", BM_LOCKED_MASK) < 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001226 retcode = ERR_IO_MD_DISK;
1227 goto force_diskless_dec;
1228 }
1229 }
1230
1231 if (cp_discovered) {
1232 drbd_al_apply_to_bm(mdev);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001233 if (drbd_bitmap_io(mdev, &drbd_bm_write,
1234 "crashed primary apply AL", BM_LOCKED_MASK)) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001235 retcode = ERR_IO_MD_DISK;
1236 goto force_diskless_dec;
1237 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001238 }
1239
Philipp Reisner07782862010-08-31 12:00:50 +02001240 if (_drbd_bm_total_weight(mdev) == drbd_bm_bits(mdev))
1241 drbd_suspend_al(mdev); /* IO is still suspended here... */
1242
Philipp Reisnerb411b362009-09-25 16:07:19 -07001243 spin_lock_irq(&mdev->req_lock);
1244 os = mdev->state;
1245 ns.i = os.i;
1246 /* If MDF_CONSISTENT is not set go into inconsistent state,
1247 otherwise investigate MDF_WasUpToDate...
1248 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1249 otherwise into D_CONSISTENT state.
1250 */
1251 if (drbd_md_test_flag(mdev->ldev, MDF_CONSISTENT)) {
1252 if (drbd_md_test_flag(mdev->ldev, MDF_WAS_UP_TO_DATE))
1253 ns.disk = D_CONSISTENT;
1254 else
1255 ns.disk = D_OUTDATED;
1256 } else {
1257 ns.disk = D_INCONSISTENT;
1258 }
1259
1260 if (drbd_md_test_flag(mdev->ldev, MDF_PEER_OUT_DATED))
1261 ns.pdsk = D_OUTDATED;
1262
1263 if ( ns.disk == D_CONSISTENT &&
1264 (ns.pdsk == D_OUTDATED || mdev->ldev->dc.fencing == FP_DONT_CARE))
1265 ns.disk = D_UP_TO_DATE;
1266
1267 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1268 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1269 this point, because drbd_request_state() modifies these
1270 flags. */
1271
1272 /* In case we are C_CONNECTED postpone any decision on the new disk
1273 state after the negotiation phase. */
1274 if (mdev->state.conn == C_CONNECTED) {
1275 mdev->new_state_tmp.i = ns.i;
1276 ns.i = os.i;
1277 ns.disk = D_NEGOTIATING;
Philipp Reisnerdc66c742010-06-02 14:31:29 +02001278
1279 /* We expect to receive up-to-date UUIDs soon.
1280 To avoid a race in receive_state, free p_uuid while
1281 holding req_lock. I.e. atomic with the state change */
1282 kfree(mdev->p_uuid);
1283 mdev->p_uuid = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001284 }
1285
1286 rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
1287 ns = mdev->state;
1288 spin_unlock_irq(&mdev->req_lock);
1289
1290 if (rv < SS_SUCCESS)
1291 goto force_diskless_dec;
1292
1293 if (mdev->state.role == R_PRIMARY)
1294 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
1295 else
1296 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
1297
1298 drbd_md_mark_dirty(mdev);
1299 drbd_md_sync(mdev);
1300
1301 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1302 put_ldev(mdev);
1303 reply->ret_code = retcode;
1304 drbd_reconfig_done(mdev);
1305 return 0;
1306
1307 force_diskless_dec:
1308 put_ldev(mdev);
1309 force_diskless:
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001310 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001311 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001312 fail:
1313 if (nbc) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001314 if (nbc->backing_bdev)
1315 blkdev_put(nbc->backing_bdev,
1316 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1317 if (nbc->md_bdev)
1318 blkdev_put(nbc->md_bdev,
1319 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001320 kfree(nbc);
1321 }
1322 lc_destroy(resync_lru);
1323
1324 reply->ret_code = retcode;
1325 drbd_reconfig_done(mdev);
1326 return 0;
1327}
1328
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001329/* Detaching the disk is a process in multiple stages. First we need to lock
1330 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1331 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1332 * internal references as well.
1333 * Only then we have finally detached. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001334static int drbd_nl_detach(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1335 struct drbd_nl_cfg_reply *reply)
1336{
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001337 enum drbd_ret_code retcode;
1338 int ret;
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001339 drbd_suspend_io(mdev); /* so no-one is stuck in drbd_al_begin_io */
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001340 retcode = drbd_request_state(mdev, NS(disk, D_FAILED));
1341 /* D_FAILED will transition to DISKLESS. */
1342 ret = wait_event_interruptible(mdev->misc_wait,
1343 mdev->state.disk != D_FAILED);
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001344 drbd_resume_io(mdev);
Philipp Reisner9b2f61a2011-05-24 10:27:38 +02001345 if ((int)retcode == (int)SS_IS_DISKLESS)
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001346 retcode = SS_NOTHING_TO_DO;
1347 if (ret)
1348 retcode = ERR_INTR;
1349 reply->ret_code = retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001350 return 0;
1351}
1352
1353static int drbd_nl_net_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1354 struct drbd_nl_cfg_reply *reply)
1355{
1356 int i, ns;
Andreas Gruenbacher116676c2010-12-08 13:33:11 +01001357 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001358 struct net_conf *new_conf = NULL;
1359 struct crypto_hash *tfm = NULL;
1360 struct crypto_hash *integrity_w_tfm = NULL;
1361 struct crypto_hash *integrity_r_tfm = NULL;
1362 struct hlist_head *new_tl_hash = NULL;
1363 struct hlist_head *new_ee_hash = NULL;
1364 struct drbd_conf *odev;
1365 char hmac_name[CRYPTO_MAX_ALG_NAME];
1366 void *int_dig_out = NULL;
1367 void *int_dig_in = NULL;
1368 void *int_dig_vv = NULL;
1369 struct sockaddr *new_my_addr, *new_peer_addr, *taken_addr;
1370
1371 drbd_reconfig_start(mdev);
1372
1373 if (mdev->state.conn > C_STANDALONE) {
1374 retcode = ERR_NET_CONFIGURED;
1375 goto fail;
1376 }
1377
1378 /* allocation not in the IO path, cqueue thread context */
Julia Lawall2db4e422010-05-13 22:02:21 +02001379 new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001380 if (!new_conf) {
1381 retcode = ERR_NOMEM;
1382 goto fail;
1383 }
1384
Philipp Reisnerb411b362009-09-25 16:07:19 -07001385 new_conf->timeout = DRBD_TIMEOUT_DEF;
1386 new_conf->try_connect_int = DRBD_CONNECT_INT_DEF;
1387 new_conf->ping_int = DRBD_PING_INT_DEF;
1388 new_conf->max_epoch_size = DRBD_MAX_EPOCH_SIZE_DEF;
1389 new_conf->max_buffers = DRBD_MAX_BUFFERS_DEF;
1390 new_conf->unplug_watermark = DRBD_UNPLUG_WATERMARK_DEF;
1391 new_conf->sndbuf_size = DRBD_SNDBUF_SIZE_DEF;
1392 new_conf->rcvbuf_size = DRBD_RCVBUF_SIZE_DEF;
1393 new_conf->ko_count = DRBD_KO_COUNT_DEF;
1394 new_conf->after_sb_0p = DRBD_AFTER_SB_0P_DEF;
1395 new_conf->after_sb_1p = DRBD_AFTER_SB_1P_DEF;
1396 new_conf->after_sb_2p = DRBD_AFTER_SB_2P_DEF;
1397 new_conf->want_lose = 0;
1398 new_conf->two_primaries = 0;
1399 new_conf->wire_protocol = DRBD_PROT_C;
1400 new_conf->ping_timeo = DRBD_PING_TIMEO_DEF;
1401 new_conf->rr_conflict = DRBD_RR_CONFLICT_DEF;
Philipp Reisner422028b2010-10-27 11:12:07 +02001402 new_conf->on_congestion = DRBD_ON_CONGESTION_DEF;
1403 new_conf->cong_extents = DRBD_CONG_EXTENTS_DEF;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001404
1405 if (!net_conf_from_tags(mdev, nlp->tag_list, new_conf)) {
1406 retcode = ERR_MANDATORY_TAG;
1407 goto fail;
1408 }
1409
1410 if (new_conf->two_primaries
1411 && (new_conf->wire_protocol != DRBD_PROT_C)) {
1412 retcode = ERR_NOT_PROTO_C;
1413 goto fail;
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001414 }
1415
1416 if (get_ldev(mdev)) {
1417 enum drbd_fencing_p fp = mdev->ldev->dc.fencing;
1418 put_ldev(mdev);
1419 if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH) {
1420 retcode = ERR_STONITH_AND_PROT_A;
1421 goto fail;
1422 }
1423 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001424
Philipp Reisner422028b2010-10-27 11:12:07 +02001425 if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A) {
1426 retcode = ERR_CONG_NOT_PROTO_A;
1427 goto fail;
1428 }
1429
Philipp Reisnerb411b362009-09-25 16:07:19 -07001430 if (mdev->state.role == R_PRIMARY && new_conf->want_lose) {
1431 retcode = ERR_DISCARD;
1432 goto fail;
1433 }
1434
1435 retcode = NO_ERROR;
1436
1437 new_my_addr = (struct sockaddr *)&new_conf->my_addr;
1438 new_peer_addr = (struct sockaddr *)&new_conf->peer_addr;
1439 for (i = 0; i < minor_count; i++) {
1440 odev = minor_to_mdev(i);
1441 if (!odev || odev == mdev)
1442 continue;
1443 if (get_net_conf(odev)) {
1444 taken_addr = (struct sockaddr *)&odev->net_conf->my_addr;
1445 if (new_conf->my_addr_len == odev->net_conf->my_addr_len &&
1446 !memcmp(new_my_addr, taken_addr, new_conf->my_addr_len))
1447 retcode = ERR_LOCAL_ADDR;
1448
1449 taken_addr = (struct sockaddr *)&odev->net_conf->peer_addr;
1450 if (new_conf->peer_addr_len == odev->net_conf->peer_addr_len &&
1451 !memcmp(new_peer_addr, taken_addr, new_conf->peer_addr_len))
1452 retcode = ERR_PEER_ADDR;
1453
1454 put_net_conf(odev);
1455 if (retcode != NO_ERROR)
1456 goto fail;
1457 }
1458 }
1459
1460 if (new_conf->cram_hmac_alg[0] != 0) {
1461 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
1462 new_conf->cram_hmac_alg);
1463 tfm = crypto_alloc_hash(hmac_name, 0, CRYPTO_ALG_ASYNC);
1464 if (IS_ERR(tfm)) {
1465 tfm = NULL;
1466 retcode = ERR_AUTH_ALG;
1467 goto fail;
1468 }
1469
Philipp Reisner07982192009-12-28 16:58:38 +01001470 if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001471 retcode = ERR_AUTH_ALG_ND;
1472 goto fail;
1473 }
1474 }
1475
1476 if (new_conf->integrity_alg[0]) {
1477 integrity_w_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC);
1478 if (IS_ERR(integrity_w_tfm)) {
1479 integrity_w_tfm = NULL;
1480 retcode=ERR_INTEGRITY_ALG;
1481 goto fail;
1482 }
1483
1484 if (!drbd_crypto_is_hash(crypto_hash_tfm(integrity_w_tfm))) {
1485 retcode=ERR_INTEGRITY_ALG_ND;
1486 goto fail;
1487 }
1488
1489 integrity_r_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC);
1490 if (IS_ERR(integrity_r_tfm)) {
1491 integrity_r_tfm = NULL;
1492 retcode=ERR_INTEGRITY_ALG;
1493 goto fail;
1494 }
1495 }
1496
1497 ns = new_conf->max_epoch_size/8;
1498 if (mdev->tl_hash_s != ns) {
1499 new_tl_hash = kzalloc(ns*sizeof(void *), GFP_KERNEL);
1500 if (!new_tl_hash) {
1501 retcode = ERR_NOMEM;
1502 goto fail;
1503 }
1504 }
1505
1506 ns = new_conf->max_buffers/8;
1507 if (new_conf->two_primaries && (mdev->ee_hash_s != ns)) {
1508 new_ee_hash = kzalloc(ns*sizeof(void *), GFP_KERNEL);
1509 if (!new_ee_hash) {
1510 retcode = ERR_NOMEM;
1511 goto fail;
1512 }
1513 }
1514
1515 ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
1516
1517 if (integrity_w_tfm) {
1518 i = crypto_hash_digestsize(integrity_w_tfm);
1519 int_dig_out = kmalloc(i, GFP_KERNEL);
1520 if (!int_dig_out) {
1521 retcode = ERR_NOMEM;
1522 goto fail;
1523 }
1524 int_dig_in = kmalloc(i, GFP_KERNEL);
1525 if (!int_dig_in) {
1526 retcode = ERR_NOMEM;
1527 goto fail;
1528 }
1529 int_dig_vv = kmalloc(i, GFP_KERNEL);
1530 if (!int_dig_vv) {
1531 retcode = ERR_NOMEM;
1532 goto fail;
1533 }
1534 }
1535
1536 if (!mdev->bitmap) {
1537 if(drbd_bm_init(mdev)) {
1538 retcode = ERR_NOMEM;
1539 goto fail;
1540 }
1541 }
1542
Philipp Reisnerf70b3512010-06-24 14:34:40 +02001543 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001544 spin_lock_irq(&mdev->req_lock);
1545 if (mdev->net_conf != NULL) {
1546 retcode = ERR_NET_CONFIGURED;
1547 spin_unlock_irq(&mdev->req_lock);
1548 goto fail;
1549 }
1550 mdev->net_conf = new_conf;
1551
1552 mdev->send_cnt = 0;
1553 mdev->recv_cnt = 0;
1554
1555 if (new_tl_hash) {
1556 kfree(mdev->tl_hash);
1557 mdev->tl_hash_s = mdev->net_conf->max_epoch_size/8;
1558 mdev->tl_hash = new_tl_hash;
1559 }
1560
1561 if (new_ee_hash) {
1562 kfree(mdev->ee_hash);
1563 mdev->ee_hash_s = mdev->net_conf->max_buffers/8;
1564 mdev->ee_hash = new_ee_hash;
1565 }
1566
1567 crypto_free_hash(mdev->cram_hmac_tfm);
1568 mdev->cram_hmac_tfm = tfm;
1569
1570 crypto_free_hash(mdev->integrity_w_tfm);
1571 mdev->integrity_w_tfm = integrity_w_tfm;
1572
1573 crypto_free_hash(mdev->integrity_r_tfm);
1574 mdev->integrity_r_tfm = integrity_r_tfm;
1575
1576 kfree(mdev->int_dig_out);
1577 kfree(mdev->int_dig_in);
1578 kfree(mdev->int_dig_vv);
1579 mdev->int_dig_out=int_dig_out;
1580 mdev->int_dig_in=int_dig_in;
1581 mdev->int_dig_vv=int_dig_vv;
Philipp Reisnerf70b3512010-06-24 14:34:40 +02001582 retcode = _drbd_set_state(_NS(mdev, conn, C_UNCONNECTED), CS_VERBOSE, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001583 spin_unlock_irq(&mdev->req_lock);
1584
Philipp Reisnerb411b362009-09-25 16:07:19 -07001585 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1586 reply->ret_code = retcode;
1587 drbd_reconfig_done(mdev);
1588 return 0;
1589
1590fail:
1591 kfree(int_dig_out);
1592 kfree(int_dig_in);
1593 kfree(int_dig_vv);
1594 crypto_free_hash(tfm);
1595 crypto_free_hash(integrity_w_tfm);
1596 crypto_free_hash(integrity_r_tfm);
1597 kfree(new_tl_hash);
1598 kfree(new_ee_hash);
1599 kfree(new_conf);
1600
1601 reply->ret_code = retcode;
1602 drbd_reconfig_done(mdev);
1603 return 0;
1604}
1605
1606static int drbd_nl_disconnect(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1607 struct drbd_nl_cfg_reply *reply)
1608{
1609 int retcode;
Philipp Reisner2561b9c2010-12-03 15:22:48 +01001610 struct disconnect dc;
1611
1612 memset(&dc, 0, sizeof(struct disconnect));
1613 if (!disconnect_from_tags(mdev, nlp->tag_list, &dc)) {
1614 retcode = ERR_MANDATORY_TAG;
1615 goto fail;
1616 }
1617
1618 if (dc.force) {
1619 spin_lock_irq(&mdev->req_lock);
1620 if (mdev->state.conn >= C_WF_CONNECTION)
1621 _drbd_set_state(_NS(mdev, conn, C_DISCONNECTING), CS_HARD, NULL);
1622 spin_unlock_irq(&mdev->req_lock);
1623 goto done;
1624 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001625
1626 retcode = _drbd_request_state(mdev, NS(conn, C_DISCONNECTING), CS_ORDERED);
1627
1628 if (retcode == SS_NOTHING_TO_DO)
1629 goto done;
1630 else if (retcode == SS_ALREADY_STANDALONE)
1631 goto done;
1632 else if (retcode == SS_PRIMARY_NOP) {
1633 /* Our statche checking code wants to see the peer outdated. */
1634 retcode = drbd_request_state(mdev, NS2(conn, C_DISCONNECTING,
1635 pdsk, D_OUTDATED));
1636 } else if (retcode == SS_CW_FAILED_BY_PEER) {
1637 /* The peer probably wants to see us outdated. */
1638 retcode = _drbd_request_state(mdev, NS2(conn, C_DISCONNECTING,
1639 disk, D_OUTDATED),
1640 CS_ORDERED);
1641 if (retcode == SS_IS_DISKLESS || retcode == SS_LOWER_THAN_OUTDATED) {
1642 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
1643 retcode = SS_SUCCESS;
1644 }
1645 }
1646
1647 if (retcode < SS_SUCCESS)
1648 goto fail;
1649
1650 if (wait_event_interruptible(mdev->state_wait,
1651 mdev->state.conn != C_DISCONNECTING)) {
1652 /* Do not test for mdev->state.conn == C_STANDALONE, since
1653 someone else might connect us in the mean time! */
1654 retcode = ERR_INTR;
1655 goto fail;
1656 }
1657
1658 done:
1659 retcode = NO_ERROR;
1660 fail:
1661 drbd_md_sync(mdev);
1662 reply->ret_code = retcode;
1663 return 0;
1664}
1665
1666void resync_after_online_grow(struct drbd_conf *mdev)
1667{
1668 int iass; /* I am sync source */
1669
1670 dev_info(DEV, "Resync of new storage after online grow\n");
1671 if (mdev->state.role != mdev->state.peer)
1672 iass = (mdev->state.role == R_PRIMARY);
1673 else
1674 iass = test_bit(DISCARD_CONCURRENT, &mdev->flags);
1675
1676 if (iass)
1677 drbd_start_resync(mdev, C_SYNC_SOURCE);
1678 else
1679 _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
1680}
1681
1682static int drbd_nl_resize(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1683 struct drbd_nl_cfg_reply *reply)
1684{
1685 struct resize rs;
1686 int retcode = NO_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001687 enum determine_dev_size dd;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01001688 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001689
1690 memset(&rs, 0, sizeof(struct resize));
1691 if (!resize_from_tags(mdev, nlp->tag_list, &rs)) {
1692 retcode = ERR_MANDATORY_TAG;
1693 goto fail;
1694 }
1695
1696 if (mdev->state.conn > C_CONNECTED) {
1697 retcode = ERR_RESIZE_RESYNC;
1698 goto fail;
1699 }
1700
1701 if (mdev->state.role == R_SECONDARY &&
1702 mdev->state.peer == R_SECONDARY) {
1703 retcode = ERR_NO_PRIMARY;
1704 goto fail;
1705 }
1706
1707 if (!get_ldev(mdev)) {
1708 retcode = ERR_NO_DISK;
1709 goto fail;
1710 }
1711
Philipp Reisner6495d2c2010-03-24 16:07:04 +01001712 if (rs.no_resync && mdev->agreed_pro_version < 93) {
1713 retcode = ERR_NEED_APV_93;
1714 goto fail;
1715 }
1716
Philipp Reisner087c2492010-03-26 13:49:56 +01001717 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001718 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001719
1720 mdev->ldev->dc.disk_size = (sector_t)rs.resize_size;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01001721 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
Bart Van Assche24c48302011-05-21 18:32:29 +02001722 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001723 drbd_md_sync(mdev);
1724 put_ldev(mdev);
1725 if (dd == dev_size_error) {
1726 retcode = ERR_NOMEM_BITMAP;
1727 goto fail;
1728 }
1729
Philipp Reisner087c2492010-03-26 13:49:56 +01001730 if (mdev->state.conn == C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001731 if (dd == grew)
1732 set_bit(RESIZE_PENDING, &mdev->flags);
1733
1734 drbd_send_uuids(mdev);
Philipp Reisner6495d2c2010-03-24 16:07:04 +01001735 drbd_send_sizes(mdev, 1, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001736 }
1737
1738 fail:
1739 reply->ret_code = retcode;
1740 return 0;
1741}
1742
1743static int drbd_nl_syncer_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1744 struct drbd_nl_cfg_reply *reply)
1745{
1746 int retcode = NO_ERROR;
1747 int err;
1748 int ovr; /* online verify running */
1749 int rsr; /* re-sync running */
1750 struct crypto_hash *verify_tfm = NULL;
1751 struct crypto_hash *csums_tfm = NULL;
1752 struct syncer_conf sc;
1753 cpumask_var_t new_cpu_mask;
Philipp Reisner778f2712010-07-06 11:14:00 +02001754 int *rs_plan_s = NULL;
1755 int fifo_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001756
1757 if (!zalloc_cpumask_var(&new_cpu_mask, GFP_KERNEL)) {
1758 retcode = ERR_NOMEM;
1759 goto fail;
1760 }
1761
1762 if (nlp->flags & DRBD_NL_SET_DEFAULTS) {
1763 memset(&sc, 0, sizeof(struct syncer_conf));
1764 sc.rate = DRBD_RATE_DEF;
1765 sc.after = DRBD_AFTER_DEF;
1766 sc.al_extents = DRBD_AL_EXTENTS_DEF;
Philipp Reisner265be2d2010-05-31 10:14:17 +02001767 sc.on_no_data = DRBD_ON_NO_DATA_DEF;
Philipp Reisner9a31d712010-07-05 13:42:03 +02001768 sc.c_plan_ahead = DRBD_C_PLAN_AHEAD_DEF;
1769 sc.c_delay_target = DRBD_C_DELAY_TARGET_DEF;
1770 sc.c_fill_target = DRBD_C_FILL_TARGET_DEF;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001771 sc.c_max_rate = DRBD_C_MAX_RATE_DEF;
1772 sc.c_min_rate = DRBD_C_MIN_RATE_DEF;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001773 } else
1774 memcpy(&sc, &mdev->sync_conf, sizeof(struct syncer_conf));
1775
1776 if (!syncer_conf_from_tags(mdev, nlp->tag_list, &sc)) {
1777 retcode = ERR_MANDATORY_TAG;
1778 goto fail;
1779 }
1780
1781 /* re-sync running */
1782 rsr = ( mdev->state.conn == C_SYNC_SOURCE ||
1783 mdev->state.conn == C_SYNC_TARGET ||
1784 mdev->state.conn == C_PAUSED_SYNC_S ||
1785 mdev->state.conn == C_PAUSED_SYNC_T );
1786
1787 if (rsr && strcmp(sc.csums_alg, mdev->sync_conf.csums_alg)) {
1788 retcode = ERR_CSUMS_RESYNC_RUNNING;
1789 goto fail;
1790 }
1791
1792 if (!rsr && sc.csums_alg[0]) {
1793 csums_tfm = crypto_alloc_hash(sc.csums_alg, 0, CRYPTO_ALG_ASYNC);
1794 if (IS_ERR(csums_tfm)) {
1795 csums_tfm = NULL;
1796 retcode = ERR_CSUMS_ALG;
1797 goto fail;
1798 }
1799
1800 if (!drbd_crypto_is_hash(crypto_hash_tfm(csums_tfm))) {
1801 retcode = ERR_CSUMS_ALG_ND;
1802 goto fail;
1803 }
1804 }
1805
1806 /* online verify running */
1807 ovr = (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T);
1808
1809 if (ovr) {
1810 if (strcmp(sc.verify_alg, mdev->sync_conf.verify_alg)) {
1811 retcode = ERR_VERIFY_RUNNING;
1812 goto fail;
1813 }
1814 }
1815
1816 if (!ovr && sc.verify_alg[0]) {
1817 verify_tfm = crypto_alloc_hash(sc.verify_alg, 0, CRYPTO_ALG_ASYNC);
1818 if (IS_ERR(verify_tfm)) {
1819 verify_tfm = NULL;
1820 retcode = ERR_VERIFY_ALG;
1821 goto fail;
1822 }
1823
1824 if (!drbd_crypto_is_hash(crypto_hash_tfm(verify_tfm))) {
1825 retcode = ERR_VERIFY_ALG_ND;
1826 goto fail;
1827 }
1828 }
1829
1830 /* silently ignore cpu mask on UP kernel */
1831 if (nr_cpu_ids > 1 && sc.cpu_mask[0] != 0) {
H Hartley Sweetenddad9ef2011-08-02 12:43:49 +02001832 err = bitmap_parse(sc.cpu_mask, 32,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001833 cpumask_bits(new_cpu_mask), nr_cpu_ids);
1834 if (err) {
H Hartley Sweetenddad9ef2011-08-02 12:43:49 +02001835 dev_warn(DEV, "bitmap_parse() failed with %d\n", err);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001836 retcode = ERR_CPU_MASK_PARSE;
1837 goto fail;
1838 }
1839 }
1840
1841 ERR_IF (sc.rate < 1) sc.rate = 1;
1842 ERR_IF (sc.al_extents < 7) sc.al_extents = 127; /* arbitrary minimum */
1843#define AL_MAX ((MD_AL_MAX_SIZE-1) * AL_EXTENTS_PT)
1844 if (sc.al_extents > AL_MAX) {
1845 dev_err(DEV, "sc.al_extents > %d\n", AL_MAX);
1846 sc.al_extents = AL_MAX;
1847 }
1848#undef AL_MAX
1849
Lars Ellenbergef50a3e2010-09-01 14:39:30 +02001850 /* to avoid spurious errors when configuring minors before configuring
1851 * the minors they depend on: if necessary, first create the minor we
1852 * depend on */
1853 if (sc.after >= 0)
1854 ensure_mdev(sc.after, 1);
1855
Philipp Reisnerb411b362009-09-25 16:07:19 -07001856 /* most sanity checks done, try to assign the new sync-after
1857 * dependency. need to hold the global lock in there,
1858 * to avoid a race in the dependency loop check. */
1859 retcode = drbd_alter_sa(mdev, sc.after);
1860 if (retcode != NO_ERROR)
1861 goto fail;
1862
Philipp Reisner778f2712010-07-06 11:14:00 +02001863 fifo_size = (sc.c_plan_ahead * 10 * SLEEP_TIME) / HZ;
1864 if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) {
1865 rs_plan_s = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL);
1866 if (!rs_plan_s) {
1867 dev_err(DEV, "kmalloc of fifo_buffer failed");
1868 retcode = ERR_NOMEM;
1869 goto fail;
1870 }
1871 }
1872
Philipp Reisnerb411b362009-09-25 16:07:19 -07001873 /* ok, assign the rest of it as well.
1874 * lock against receive_SyncParam() */
1875 spin_lock(&mdev->peer_seq_lock);
1876 mdev->sync_conf = sc;
1877
1878 if (!rsr) {
1879 crypto_free_hash(mdev->csums_tfm);
1880 mdev->csums_tfm = csums_tfm;
1881 csums_tfm = NULL;
1882 }
1883
1884 if (!ovr) {
1885 crypto_free_hash(mdev->verify_tfm);
1886 mdev->verify_tfm = verify_tfm;
1887 verify_tfm = NULL;
1888 }
Philipp Reisner778f2712010-07-06 11:14:00 +02001889
1890 if (fifo_size != mdev->rs_plan_s.size) {
1891 kfree(mdev->rs_plan_s.values);
1892 mdev->rs_plan_s.values = rs_plan_s;
1893 mdev->rs_plan_s.size = fifo_size;
1894 mdev->rs_planed = 0;
1895 rs_plan_s = NULL;
1896 }
1897
Philipp Reisnerb411b362009-09-25 16:07:19 -07001898 spin_unlock(&mdev->peer_seq_lock);
1899
1900 if (get_ldev(mdev)) {
1901 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
1902 drbd_al_shrink(mdev);
1903 err = drbd_check_al_size(mdev);
1904 lc_unlock(mdev->act_log);
1905 wake_up(&mdev->al_wait);
1906
1907 put_ldev(mdev);
1908 drbd_md_sync(mdev);
1909
1910 if (err) {
1911 retcode = ERR_NOMEM;
1912 goto fail;
1913 }
1914 }
1915
1916 if (mdev->state.conn >= C_CONNECTED)
1917 drbd_send_sync_param(mdev, &sc);
1918
1919 if (!cpumask_equal(mdev->cpu_mask, new_cpu_mask)) {
1920 cpumask_copy(mdev->cpu_mask, new_cpu_mask);
1921 drbd_calc_cpu_mask(mdev);
1922 mdev->receiver.reset_cpu_mask = 1;
1923 mdev->asender.reset_cpu_mask = 1;
1924 mdev->worker.reset_cpu_mask = 1;
1925 }
1926
1927 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1928fail:
Philipp Reisner778f2712010-07-06 11:14:00 +02001929 kfree(rs_plan_s);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001930 free_cpumask_var(new_cpu_mask);
1931 crypto_free_hash(csums_tfm);
1932 crypto_free_hash(verify_tfm);
1933 reply->ret_code = retcode;
1934 return 0;
1935}
1936
1937static int drbd_nl_invalidate(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1938 struct drbd_nl_cfg_reply *reply)
1939{
1940 int retcode;
1941
Lars Ellenberg194bfb32011-01-18 10:38:01 +01001942 /* If there is still bitmap IO pending, probably because of a previous
1943 * resync just being finished, wait for it before requesting a new resync. */
1944 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
1945
Philipp Reisnerb411b362009-09-25 16:07:19 -07001946 retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T), CS_ORDERED);
1947
1948 if (retcode < SS_SUCCESS && retcode != SS_NEED_CONNECTION)
1949 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
1950
1951 while (retcode == SS_NEED_CONNECTION) {
1952 spin_lock_irq(&mdev->req_lock);
1953 if (mdev->state.conn < C_CONNECTED)
1954 retcode = _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_VERBOSE, NULL);
1955 spin_unlock_irq(&mdev->req_lock);
1956
1957 if (retcode != SS_NEED_CONNECTION)
1958 break;
1959
1960 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
1961 }
1962
1963 reply->ret_code = retcode;
1964 return 0;
1965}
1966
Philipp Reisner07782862010-08-31 12:00:50 +02001967static int drbd_bmio_set_susp_al(struct drbd_conf *mdev)
1968{
1969 int rv;
1970
1971 rv = drbd_bmio_set_n_write(mdev);
1972 drbd_suspend_al(mdev);
1973 return rv;
1974}
1975
Philipp Reisnerb411b362009-09-25 16:07:19 -07001976static int drbd_nl_invalidate_peer(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1977 struct drbd_nl_cfg_reply *reply)
1978{
Philipp Reisner07782862010-08-31 12:00:50 +02001979 int retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001980
Lars Ellenberg194bfb32011-01-18 10:38:01 +01001981 /* If there is still bitmap IO pending, probably because of a previous
1982 * resync just being finished, wait for it before requesting a new resync. */
1983 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
1984
Philipp Reisner07782862010-08-31 12:00:50 +02001985 retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S), CS_ORDERED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001986
Philipp Reisner07782862010-08-31 12:00:50 +02001987 if (retcode < SS_SUCCESS) {
1988 if (retcode == SS_NEED_CONNECTION && mdev->state.role == R_PRIMARY) {
1989 /* The peer will get a resync upon connect anyways. Just make that
1990 into a full resync. */
1991 retcode = drbd_request_state(mdev, NS(pdsk, D_INCONSISTENT));
1992 if (retcode >= SS_SUCCESS) {
Philipp Reisner07782862010-08-31 12:00:50 +02001993 if (drbd_bitmap_io(mdev, &drbd_bmio_set_susp_al,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001994 "set_n_write from invalidate_peer",
1995 BM_LOCKED_SET_ALLOWED))
Philipp Reisner07782862010-08-31 12:00:50 +02001996 retcode = ERR_IO_MD_DISK;
1997 }
1998 } else
1999 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S));
2000 }
2001
2002 reply->ret_code = retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002003 return 0;
2004}
2005
2006static int drbd_nl_pause_sync(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2007 struct drbd_nl_cfg_reply *reply)
2008{
2009 int retcode = NO_ERROR;
2010
2011 if (drbd_request_state(mdev, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
2012 retcode = ERR_PAUSE_IS_SET;
2013
2014 reply->ret_code = retcode;
2015 return 0;
2016}
2017
2018static int drbd_nl_resume_sync(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2019 struct drbd_nl_cfg_reply *reply)
2020{
2021 int retcode = NO_ERROR;
Philipp Reisnercd88d032011-01-20 11:46:41 +01002022 union drbd_state s;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002023
Philipp Reisnercd88d032011-01-20 11:46:41 +01002024 if (drbd_request_state(mdev, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2025 s = mdev->state;
2026 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2027 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2028 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2029 } else {
2030 retcode = ERR_PAUSE_IS_CLEAR;
2031 }
2032 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002033
2034 reply->ret_code = retcode;
2035 return 0;
2036}
2037
2038static int drbd_nl_suspend_io(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2039 struct drbd_nl_cfg_reply *reply)
2040{
2041 reply->ret_code = drbd_request_state(mdev, NS(susp, 1));
2042
2043 return 0;
2044}
2045
2046static int drbd_nl_resume_io(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2047 struct drbd_nl_cfg_reply *reply)
2048{
Philipp Reisner43a51822010-06-11 11:26:34 +02002049 if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
2050 drbd_uuid_new_current(mdev);
2051 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner43a51822010-06-11 11:26:34 +02002052 }
Philipp Reisner265be2d2010-05-31 10:14:17 +02002053 drbd_suspend_io(mdev);
Philipp Reisnerfb22c402010-09-08 23:20:21 +02002054 reply->ret_code = drbd_request_state(mdev, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
Philipp Reisner265be2d2010-05-31 10:14:17 +02002055 if (reply->ret_code == SS_SUCCESS) {
2056 if (mdev->state.conn < C_CONNECTED)
2057 tl_clear(mdev);
2058 if (mdev->state.disk == D_DISKLESS || mdev->state.disk == D_FAILED)
2059 tl_restart(mdev, fail_frozen_disk_io);
2060 }
2061 drbd_resume_io(mdev);
2062
Philipp Reisnerb411b362009-09-25 16:07:19 -07002063 return 0;
2064}
2065
2066static int drbd_nl_outdate(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2067 struct drbd_nl_cfg_reply *reply)
2068{
2069 reply->ret_code = drbd_request_state(mdev, NS(disk, D_OUTDATED));
2070 return 0;
2071}
2072
2073static int drbd_nl_get_config(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2074 struct drbd_nl_cfg_reply *reply)
2075{
2076 unsigned short *tl;
2077
2078 tl = reply->tag_list;
2079
2080 if (get_ldev(mdev)) {
2081 tl = disk_conf_to_tags(mdev, &mdev->ldev->dc, tl);
2082 put_ldev(mdev);
2083 }
2084
2085 if (get_net_conf(mdev)) {
2086 tl = net_conf_to_tags(mdev, mdev->net_conf, tl);
2087 put_net_conf(mdev);
2088 }
2089 tl = syncer_conf_to_tags(mdev, &mdev->sync_conf, tl);
2090
2091 put_unaligned(TT_END, tl++); /* Close the tag list */
2092
2093 return (int)((char *)tl - (char *)reply->tag_list);
2094}
2095
2096static int drbd_nl_get_state(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2097 struct drbd_nl_cfg_reply *reply)
2098{
2099 unsigned short *tl = reply->tag_list;
2100 union drbd_state s = mdev->state;
2101 unsigned long rs_left;
2102 unsigned int res;
2103
2104 tl = get_state_to_tags(mdev, (struct get_state *)&s, tl);
2105
2106 /* no local ref, no bitmap, no syncer progress. */
2107 if (s.conn >= C_SYNC_SOURCE && s.conn <= C_PAUSED_SYNC_T) {
2108 if (get_ldev(mdev)) {
2109 drbd_get_syncer_progress(mdev, &rs_left, &res);
2110 tl = tl_add_int(tl, T_sync_progress, &res);
2111 put_ldev(mdev);
2112 }
2113 }
2114 put_unaligned(TT_END, tl++); /* Close the tag list */
2115
2116 return (int)((char *)tl - (char *)reply->tag_list);
2117}
2118
2119static int drbd_nl_get_uuids(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2120 struct drbd_nl_cfg_reply *reply)
2121{
2122 unsigned short *tl;
2123
2124 tl = reply->tag_list;
2125
2126 if (get_ldev(mdev)) {
2127 tl = tl_add_blob(tl, T_uuids, mdev->ldev->md.uuid, UI_SIZE*sizeof(u64));
2128 tl = tl_add_int(tl, T_uuids_flags, &mdev->ldev->md.flags);
2129 put_ldev(mdev);
2130 }
2131 put_unaligned(TT_END, tl++); /* Close the tag list */
2132
2133 return (int)((char *)tl - (char *)reply->tag_list);
2134}
2135
2136/**
2137 * drbd_nl_get_timeout_flag() - Used by drbdsetup to find out which timeout value to use
2138 * @mdev: DRBD device.
2139 * @nlp: Netlink/connector packet from drbdsetup
2140 * @reply: Reply packet for drbdsetup
2141 */
2142static int drbd_nl_get_timeout_flag(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2143 struct drbd_nl_cfg_reply *reply)
2144{
2145 unsigned short *tl;
2146 char rv;
2147
2148 tl = reply->tag_list;
2149
2150 rv = mdev->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
2151 test_bit(USE_DEGR_WFC_T, &mdev->flags) ? UT_DEGRADED : UT_DEFAULT;
2152
2153 tl = tl_add_blob(tl, T_use_degraded, &rv, sizeof(rv));
2154 put_unaligned(TT_END, tl++); /* Close the tag list */
2155
2156 return (int)((char *)tl - (char *)reply->tag_list);
2157}
2158
2159static int drbd_nl_start_ov(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2160 struct drbd_nl_cfg_reply *reply)
2161{
2162 /* default to resume from last known position, if possible */
2163 struct start_ov args =
2164 { .start_sector = mdev->ov_start_sector };
2165
2166 if (!start_ov_from_tags(mdev, nlp->tag_list, &args)) {
2167 reply->ret_code = ERR_MANDATORY_TAG;
2168 return 0;
2169 }
Lars Ellenberg873b0d52011-01-21 22:53:48 +01002170
2171 /* If there is still bitmap IO pending, e.g. previous resync or verify
2172 * just being finished, wait for it before requesting a new resync. */
2173 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
2174
Philipp Reisnerb411b362009-09-25 16:07:19 -07002175 /* w_make_ov_request expects position to be aligned */
2176 mdev->ov_start_sector = args.start_sector & ~BM_SECT_PER_BIT;
2177 reply->ret_code = drbd_request_state(mdev,NS(conn,C_VERIFY_S));
2178 return 0;
2179}
2180
2181
2182static int drbd_nl_new_c_uuid(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2183 struct drbd_nl_cfg_reply *reply)
2184{
2185 int retcode = NO_ERROR;
2186 int skip_initial_sync = 0;
2187 int err;
2188
2189 struct new_c_uuid args;
2190
2191 memset(&args, 0, sizeof(struct new_c_uuid));
2192 if (!new_c_uuid_from_tags(mdev, nlp->tag_list, &args)) {
2193 reply->ret_code = ERR_MANDATORY_TAG;
2194 return 0;
2195 }
2196
2197 mutex_lock(&mdev->state_mutex); /* Protects us against serialized state changes. */
2198
2199 if (!get_ldev(mdev)) {
2200 retcode = ERR_NO_DISK;
2201 goto out;
2202 }
2203
2204 /* this is "skip initial sync", assume to be clean */
2205 if (mdev->state.conn == C_CONNECTED && mdev->agreed_pro_version >= 90 &&
2206 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
2207 dev_info(DEV, "Preparing to skip initial sync\n");
2208 skip_initial_sync = 1;
2209 } else if (mdev->state.conn != C_STANDALONE) {
2210 retcode = ERR_CONNECTED;
2211 goto out_dec;
2212 }
2213
2214 drbd_uuid_set(mdev, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
2215 drbd_uuid_new_current(mdev); /* New current, previous to UI_BITMAP */
2216
2217 if (args.clear_bm) {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002218 err = drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
2219 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002220 if (err) {
2221 dev_err(DEV, "Writing bitmap failed with %d\n",err);
2222 retcode = ERR_IO_MD_DISK;
2223 }
2224 if (skip_initial_sync) {
2225 drbd_send_uuids_skip_initial_sync(mdev);
2226 _drbd_uuid_set(mdev, UI_BITMAP, 0);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002227 drbd_print_uuids(mdev, "cleared bitmap UUID");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002228 spin_lock_irq(&mdev->req_lock);
2229 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
2230 CS_VERBOSE, NULL);
2231 spin_unlock_irq(&mdev->req_lock);
2232 }
2233 }
2234
2235 drbd_md_sync(mdev);
2236out_dec:
2237 put_ldev(mdev);
2238out:
2239 mutex_unlock(&mdev->state_mutex);
2240
2241 reply->ret_code = retcode;
2242 return 0;
2243}
2244
Philipp Reisnerb411b362009-09-25 16:07:19 -07002245struct cn_handler_struct {
2246 int (*function)(struct drbd_conf *,
2247 struct drbd_nl_cfg_req *,
2248 struct drbd_nl_cfg_reply *);
2249 int reply_body_size;
2250};
2251
2252static struct cn_handler_struct cnd_table[] = {
2253 [ P_primary ] = { &drbd_nl_primary, 0 },
2254 [ P_secondary ] = { &drbd_nl_secondary, 0 },
2255 [ P_disk_conf ] = { &drbd_nl_disk_conf, 0 },
2256 [ P_detach ] = { &drbd_nl_detach, 0 },
2257 [ P_net_conf ] = { &drbd_nl_net_conf, 0 },
2258 [ P_disconnect ] = { &drbd_nl_disconnect, 0 },
2259 [ P_resize ] = { &drbd_nl_resize, 0 },
2260 [ P_syncer_conf ] = { &drbd_nl_syncer_conf, 0 },
2261 [ P_invalidate ] = { &drbd_nl_invalidate, 0 },
2262 [ P_invalidate_peer ] = { &drbd_nl_invalidate_peer, 0 },
2263 [ P_pause_sync ] = { &drbd_nl_pause_sync, 0 },
2264 [ P_resume_sync ] = { &drbd_nl_resume_sync, 0 },
2265 [ P_suspend_io ] = { &drbd_nl_suspend_io, 0 },
2266 [ P_resume_io ] = { &drbd_nl_resume_io, 0 },
2267 [ P_outdate ] = { &drbd_nl_outdate, 0 },
2268 [ P_get_config ] = { &drbd_nl_get_config,
2269 sizeof(struct syncer_conf_tag_len_struct) +
2270 sizeof(struct disk_conf_tag_len_struct) +
2271 sizeof(struct net_conf_tag_len_struct) },
2272 [ P_get_state ] = { &drbd_nl_get_state,
2273 sizeof(struct get_state_tag_len_struct) +
2274 sizeof(struct sync_progress_tag_len_struct) },
2275 [ P_get_uuids ] = { &drbd_nl_get_uuids,
2276 sizeof(struct get_uuids_tag_len_struct) },
2277 [ P_get_timeout_flag ] = { &drbd_nl_get_timeout_flag,
2278 sizeof(struct get_timeout_flag_tag_len_struct)},
2279 [ P_start_ov ] = { &drbd_nl_start_ov, 0 },
2280 [ P_new_c_uuid ] = { &drbd_nl_new_c_uuid, 0 },
2281};
2282
Philipp Reisner9f5180e2009-10-06 09:30:14 +02002283static void drbd_connector_callback(struct cn_msg *req, struct netlink_skb_parms *nsp)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002284{
2285 struct drbd_nl_cfg_req *nlp = (struct drbd_nl_cfg_req *)req->data;
2286 struct cn_handler_struct *cm;
2287 struct cn_msg *cn_reply;
2288 struct drbd_nl_cfg_reply *reply;
2289 struct drbd_conf *mdev;
2290 int retcode, rr;
2291 int reply_size = sizeof(struct cn_msg)
2292 + sizeof(struct drbd_nl_cfg_reply)
2293 + sizeof(short int);
2294
2295 if (!try_module_get(THIS_MODULE)) {
2296 printk(KERN_ERR "drbd: try_module_get() failed!\n");
2297 return;
2298 }
2299
Eric W. Biederman38bf1952012-05-04 11:34:03 +00002300 if (!capable(CAP_SYS_ADMIN)) {
Philipp Reisner9f5180e2009-10-06 09:30:14 +02002301 retcode = ERR_PERM;
2302 goto fail;
2303 }
2304
Lars Ellenbergef50a3e2010-09-01 14:39:30 +02002305 mdev = ensure_mdev(nlp->drbd_minor,
2306 (nlp->flags & DRBD_NL_CREATE_DEVICE));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002307 if (!mdev) {
2308 retcode = ERR_MINOR_INVALID;
2309 goto fail;
2310 }
2311
Lars Ellenberg42ff2692010-11-24 10:11:14 +01002312 if (nlp->packet_type >= P_nl_after_last_packet ||
2313 nlp->packet_type == P_return_code_only) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002314 retcode = ERR_PACKET_NR;
2315 goto fail;
2316 }
2317
2318 cm = cnd_table + nlp->packet_type;
2319
2320 /* This may happen if packet number is 0: */
2321 if (cm->function == NULL) {
2322 retcode = ERR_PACKET_NR;
2323 goto fail;
2324 }
2325
2326 reply_size += cm->reply_body_size;
2327
2328 /* allocation not in the IO path, cqueue thread context */
Lars Ellenberg3e3a7762010-11-24 10:41:45 +01002329 cn_reply = kzalloc(reply_size, GFP_KERNEL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002330 if (!cn_reply) {
2331 retcode = ERR_NOMEM;
2332 goto fail;
2333 }
2334 reply = (struct drbd_nl_cfg_reply *) cn_reply->data;
2335
2336 reply->packet_type =
Lars Ellenberg42ff2692010-11-24 10:11:14 +01002337 cm->reply_body_size ? nlp->packet_type : P_return_code_only;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002338 reply->minor = nlp->drbd_minor;
2339 reply->ret_code = NO_ERROR; /* Might by modified by cm->function. */
2340 /* reply->tag_list; might be modified by cm->function. */
2341
2342 rr = cm->function(mdev, nlp, reply);
2343
2344 cn_reply->id = req->id;
2345 cn_reply->seq = req->seq;
2346 cn_reply->ack = req->ack + 1;
2347 cn_reply->len = sizeof(struct drbd_nl_cfg_reply) + rr;
2348 cn_reply->flags = 0;
2349
Philipp Reisnerb411b362009-09-25 16:07:19 -07002350 rr = cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_KERNEL);
2351 if (rr && rr != -ESRCH)
2352 printk(KERN_INFO "drbd: cn_netlink_send()=%d\n", rr);
2353
2354 kfree(cn_reply);
2355 module_put(THIS_MODULE);
2356 return;
2357 fail:
2358 drbd_nl_send_reply(req, retcode);
2359 module_put(THIS_MODULE);
2360}
2361
2362static atomic_t drbd_nl_seq = ATOMIC_INIT(2); /* two. */
2363
2364static unsigned short *
2365__tl_add_blob(unsigned short *tl, enum drbd_tags tag, const void *data,
2366 unsigned short len, int nul_terminated)
2367{
2368 unsigned short l = tag_descriptions[tag_number(tag)].max_len;
2369 len = (len < l) ? len : l;
2370 put_unaligned(tag, tl++);
2371 put_unaligned(len, tl++);
2372 memcpy(tl, data, len);
2373 tl = (unsigned short*)((char*)tl + len);
2374 if (nul_terminated)
2375 *((char*)tl - 1) = 0;
2376 return tl;
2377}
2378
2379static unsigned short *
2380tl_add_blob(unsigned short *tl, enum drbd_tags tag, const void *data, int len)
2381{
2382 return __tl_add_blob(tl, tag, data, len, 0);
2383}
2384
2385static unsigned short *
2386tl_add_str(unsigned short *tl, enum drbd_tags tag, const char *str)
2387{
2388 return __tl_add_blob(tl, tag, str, strlen(str)+1, 0);
2389}
2390
2391static unsigned short *
2392tl_add_int(unsigned short *tl, enum drbd_tags tag, const void *val)
2393{
2394 put_unaligned(tag, tl++);
2395 switch(tag_type(tag)) {
2396 case TT_INTEGER:
2397 put_unaligned(sizeof(int), tl++);
2398 put_unaligned(*(int *)val, (int *)tl);
2399 tl = (unsigned short*)((char*)tl+sizeof(int));
2400 break;
2401 case TT_INT64:
2402 put_unaligned(sizeof(u64), tl++);
2403 put_unaligned(*(u64 *)val, (u64 *)tl);
2404 tl = (unsigned short*)((char*)tl+sizeof(u64));
2405 break;
2406 default:
2407 /* someone did something stupid. */
2408 ;
2409 }
2410 return tl;
2411}
2412
2413void drbd_bcast_state(struct drbd_conf *mdev, union drbd_state state)
2414{
2415 char buffer[sizeof(struct cn_msg)+
2416 sizeof(struct drbd_nl_cfg_reply)+
2417 sizeof(struct get_state_tag_len_struct)+
2418 sizeof(short int)];
2419 struct cn_msg *cn_reply = (struct cn_msg *) buffer;
2420 struct drbd_nl_cfg_reply *reply =
2421 (struct drbd_nl_cfg_reply *)cn_reply->data;
2422 unsigned short *tl = reply->tag_list;
2423
2424 /* dev_warn(DEV, "drbd_bcast_state() got called\n"); */
2425
2426 tl = get_state_to_tags(mdev, (struct get_state *)&state, tl);
2427
2428 put_unaligned(TT_END, tl++); /* Close the tag list */
2429
2430 cn_reply->id.idx = CN_IDX_DRBD;
2431 cn_reply->id.val = CN_VAL_DRBD;
2432
2433 cn_reply->seq = atomic_add_return(1, &drbd_nl_seq);
2434 cn_reply->ack = 0; /* not used here. */
2435 cn_reply->len = sizeof(struct drbd_nl_cfg_reply) +
2436 (int)((char *)tl - (char *)reply->tag_list);
2437 cn_reply->flags = 0;
2438
2439 reply->packet_type = P_get_state;
2440 reply->minor = mdev_to_minor(mdev);
2441 reply->ret_code = NO_ERROR;
2442
Philipp Reisnerb411b362009-09-25 16:07:19 -07002443 cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO);
2444}
2445
2446void drbd_bcast_ev_helper(struct drbd_conf *mdev, char *helper_name)
2447{
2448 char buffer[sizeof(struct cn_msg)+
2449 sizeof(struct drbd_nl_cfg_reply)+
2450 sizeof(struct call_helper_tag_len_struct)+
2451 sizeof(short int)];
2452 struct cn_msg *cn_reply = (struct cn_msg *) buffer;
2453 struct drbd_nl_cfg_reply *reply =
2454 (struct drbd_nl_cfg_reply *)cn_reply->data;
2455 unsigned short *tl = reply->tag_list;
2456
2457 /* dev_warn(DEV, "drbd_bcast_state() got called\n"); */
2458
2459 tl = tl_add_str(tl, T_helper, helper_name);
2460 put_unaligned(TT_END, tl++); /* Close the tag list */
2461
2462 cn_reply->id.idx = CN_IDX_DRBD;
2463 cn_reply->id.val = CN_VAL_DRBD;
2464
2465 cn_reply->seq = atomic_add_return(1, &drbd_nl_seq);
2466 cn_reply->ack = 0; /* not used here. */
2467 cn_reply->len = sizeof(struct drbd_nl_cfg_reply) +
2468 (int)((char *)tl - (char *)reply->tag_list);
2469 cn_reply->flags = 0;
2470
2471 reply->packet_type = P_call_helper;
2472 reply->minor = mdev_to_minor(mdev);
2473 reply->ret_code = NO_ERROR;
2474
Philipp Reisnerb411b362009-09-25 16:07:19 -07002475 cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO);
2476}
2477
2478void drbd_bcast_ee(struct drbd_conf *mdev,
2479 const char *reason, const int dgs,
2480 const char* seen_hash, const char* calc_hash,
2481 const struct drbd_epoch_entry* e)
2482{
2483 struct cn_msg *cn_reply;
2484 struct drbd_nl_cfg_reply *reply;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002485 unsigned short *tl;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002486 struct page *page;
2487 unsigned len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002488
2489 if (!e)
2490 return;
2491 if (!reason || !reason[0])
2492 return;
2493
2494 /* apparently we have to memcpy twice, first to prepare the data for the
2495 * struct cn_msg, then within cn_netlink_send from the cn_msg to the
2496 * netlink skb. */
2497 /* receiver thread context, which is not in the writeout path (of this node),
2498 * but may be in the writeout path of the _other_ node.
2499 * GFP_NOIO to avoid potential "distributed deadlock". */
Lars Ellenberg3e3a7762010-11-24 10:41:45 +01002500 cn_reply = kzalloc(
Philipp Reisnerb411b362009-09-25 16:07:19 -07002501 sizeof(struct cn_msg)+
2502 sizeof(struct drbd_nl_cfg_reply)+
2503 sizeof(struct dump_ee_tag_len_struct)+
2504 sizeof(short int),
2505 GFP_NOIO);
2506
2507 if (!cn_reply) {
2508 dev_err(DEV, "could not kmalloc buffer for drbd_bcast_ee, sector %llu, size %u\n",
2509 (unsigned long long)e->sector, e->size);
2510 return;
2511 }
2512
2513 reply = (struct drbd_nl_cfg_reply*)cn_reply->data;
2514 tl = reply->tag_list;
2515
2516 tl = tl_add_str(tl, T_dump_ee_reason, reason);
2517 tl = tl_add_blob(tl, T_seen_digest, seen_hash, dgs);
2518 tl = tl_add_blob(tl, T_calc_digest, calc_hash, dgs);
2519 tl = tl_add_int(tl, T_ee_sector, &e->sector);
2520 tl = tl_add_int(tl, T_ee_block_id, &e->block_id);
2521
Lars Ellenberg3129b1b2010-11-11 10:47:05 +01002522 /* dump the first 32k */
2523 len = min_t(unsigned, e->size, 32 << 10);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002524 put_unaligned(T_ee_data, tl++);
Lars Ellenberg3129b1b2010-11-11 10:47:05 +01002525 put_unaligned(len, tl++);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002526
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002527 page = e->pages;
2528 page_chain_for_each(page) {
Cong Wangcfd80052011-11-25 23:14:18 +08002529 void *d = kmap_atomic(page);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002530 unsigned l = min_t(unsigned, len, PAGE_SIZE);
2531 memcpy(tl, d, l);
Cong Wangcfd80052011-11-25 23:14:18 +08002532 kunmap_atomic(d);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002533 tl = (unsigned short*)((char*)tl + l);
2534 len -= l;
Lars Ellenberg3129b1b2010-11-11 10:47:05 +01002535 if (len == 0)
2536 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002537 }
2538 put_unaligned(TT_END, tl++); /* Close the tag list */
2539
2540 cn_reply->id.idx = CN_IDX_DRBD;
2541 cn_reply->id.val = CN_VAL_DRBD;
2542
2543 cn_reply->seq = atomic_add_return(1,&drbd_nl_seq);
2544 cn_reply->ack = 0; // not used here.
2545 cn_reply->len = sizeof(struct drbd_nl_cfg_reply) +
2546 (int)((char*)tl - (char*)reply->tag_list);
2547 cn_reply->flags = 0;
2548
2549 reply->packet_type = P_dump_ee;
2550 reply->minor = mdev_to_minor(mdev);
2551 reply->ret_code = NO_ERROR;
2552
Philipp Reisnerb411b362009-09-25 16:07:19 -07002553 cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO);
2554 kfree(cn_reply);
2555}
2556
2557void drbd_bcast_sync_progress(struct drbd_conf *mdev)
2558{
2559 char buffer[sizeof(struct cn_msg)+
2560 sizeof(struct drbd_nl_cfg_reply)+
2561 sizeof(struct sync_progress_tag_len_struct)+
2562 sizeof(short int)];
2563 struct cn_msg *cn_reply = (struct cn_msg *) buffer;
2564 struct drbd_nl_cfg_reply *reply =
2565 (struct drbd_nl_cfg_reply *)cn_reply->data;
2566 unsigned short *tl = reply->tag_list;
2567 unsigned long rs_left;
2568 unsigned int res;
2569
2570 /* no local ref, no bitmap, no syncer progress, no broadcast. */
2571 if (!get_ldev(mdev))
2572 return;
2573 drbd_get_syncer_progress(mdev, &rs_left, &res);
2574 put_ldev(mdev);
2575
2576 tl = tl_add_int(tl, T_sync_progress, &res);
2577 put_unaligned(TT_END, tl++); /* Close the tag list */
2578
2579 cn_reply->id.idx = CN_IDX_DRBD;
2580 cn_reply->id.val = CN_VAL_DRBD;
2581
2582 cn_reply->seq = atomic_add_return(1, &drbd_nl_seq);
2583 cn_reply->ack = 0; /* not used here. */
2584 cn_reply->len = sizeof(struct drbd_nl_cfg_reply) +
2585 (int)((char *)tl - (char *)reply->tag_list);
2586 cn_reply->flags = 0;
2587
2588 reply->packet_type = P_sync_progress;
2589 reply->minor = mdev_to_minor(mdev);
2590 reply->ret_code = NO_ERROR;
2591
Philipp Reisnerb411b362009-09-25 16:07:19 -07002592 cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO);
2593}
2594
2595int __init drbd_nl_init(void)
2596{
2597 static struct cb_id cn_id_drbd;
2598 int err, try=10;
2599
2600 cn_id_drbd.val = CN_VAL_DRBD;
2601 do {
2602 cn_id_drbd.idx = cn_idx;
2603 err = cn_add_callback(&cn_id_drbd, "cn_drbd", &drbd_connector_callback);
2604 if (!err)
2605 break;
2606 cn_idx = (cn_idx + CN_IDX_STEP);
2607 } while (try--);
2608
2609 if (err) {
2610 printk(KERN_ERR "drbd: cn_drbd failed to register\n");
2611 return err;
2612 }
2613
2614 return 0;
2615}
2616
2617void drbd_nl_cleanup(void)
2618{
2619 static struct cb_id cn_id_drbd;
2620
2621 cn_id_drbd.idx = cn_idx;
2622 cn_id_drbd.val = CN_VAL_DRBD;
2623
2624 cn_del_callback(&cn_id_drbd);
2625}
2626
2627void drbd_nl_send_reply(struct cn_msg *req, int ret_code)
2628{
2629 char buffer[sizeof(struct cn_msg)+sizeof(struct drbd_nl_cfg_reply)];
2630 struct cn_msg *cn_reply = (struct cn_msg *) buffer;
2631 struct drbd_nl_cfg_reply *reply =
2632 (struct drbd_nl_cfg_reply *)cn_reply->data;
2633 int rr;
2634
Lars Ellenberg3e3a7762010-11-24 10:41:45 +01002635 memset(buffer, 0, sizeof(buffer));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002636 cn_reply->id = req->id;
2637
2638 cn_reply->seq = req->seq;
2639 cn_reply->ack = req->ack + 1;
2640 cn_reply->len = sizeof(struct drbd_nl_cfg_reply);
2641 cn_reply->flags = 0;
2642
Lars Ellenberg42ff2692010-11-24 10:11:14 +01002643 reply->packet_type = P_return_code_only;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002644 reply->minor = ((struct drbd_nl_cfg_req *)req->data)->drbd_minor;
2645 reply->ret_code = ret_code;
2646
Philipp Reisnerb411b362009-09-25 16:07:19 -07002647 rr = cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO);
2648 if (rr && rr != -ESRCH)
2649 printk(KERN_INFO "drbd: cn_netlink_send()=%d\n", rr);
2650}
2651