blob: 1bbbad302ae7e8df715e061b958c36918b640c9b [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);
Lars Ellenbergf479ea02011-10-27 16:52:30 +0200435 drbd_send_current_state(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700436 }
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) {
Philipp Reisner68093842011-06-30 15:43:06 +0200848 if (mdev->agreed_pro_version < 94) {
849 peer = min_t(int, mdev->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
850 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
851 } else if (mdev->agreed_pro_version == 94)
Philipp Reisner99432fc2011-05-20 16:39:13 +0200852 peer = DRBD_MAX_SIZE_H80_PACKET;
853 else /* drbd 8.3.8 onwards */
854 peer = DRBD_MAX_BIO_SIZE;
855 }
856
857 new = min_t(int, local, peer);
858
859 if (mdev->state.role == R_PRIMARY && new < now)
860 dev_err(DEV, "ASSERT FAILED new < now; (%d < %d)\n", new, now);
861
862 if (new != now)
863 dev_info(DEV, "max BIO size = %u\n", new);
864
865 drbd_setup_queue_param(mdev, new);
866}
867
Philipp Reisnerb411b362009-09-25 16:07:19 -0700868/* serialize deconfig (worker exiting, doing cleanup)
869 * and reconfig (drbdsetup disk, drbdsetup net)
870 *
Lars Ellenbergc518d042010-09-01 09:50:23 +0200871 * Wait for a potentially exiting worker, then restart it,
872 * or start a new one. Flush any pending work, there may still be an
873 * after_state_change queued.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700874 */
875static void drbd_reconfig_start(struct drbd_conf *mdev)
876{
Lars Ellenberg6c6c7952009-11-16 15:48:54 +0100877 wait_event(mdev->state_wait, !test_and_set_bit(CONFIG_PENDING, &mdev->flags));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700878 wait_event(mdev->state_wait, !test_bit(DEVICE_DYING, &mdev->flags));
879 drbd_thread_start(&mdev->worker);
Lars Ellenbergc518d042010-09-01 09:50:23 +0200880 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700881}
882
883/* if still unconfigured, stops worker again.
884 * if configured now, clears CONFIG_PENDING.
885 * wakes potential waiters */
886static void drbd_reconfig_done(struct drbd_conf *mdev)
887{
888 spin_lock_irq(&mdev->req_lock);
889 if (mdev->state.disk == D_DISKLESS &&
890 mdev->state.conn == C_STANDALONE &&
891 mdev->state.role == R_SECONDARY) {
892 set_bit(DEVICE_DYING, &mdev->flags);
893 drbd_thread_stop_nowait(&mdev->worker);
894 } else
895 clear_bit(CONFIG_PENDING, &mdev->flags);
896 spin_unlock_irq(&mdev->req_lock);
897 wake_up(&mdev->state_wait);
898}
899
Philipp Reisner07782862010-08-31 12:00:50 +0200900/* Make sure IO is suspended before calling this function(). */
901static void drbd_suspend_al(struct drbd_conf *mdev)
902{
903 int s = 0;
904
905 if (lc_try_lock(mdev->act_log)) {
906 drbd_al_shrink(mdev);
907 lc_unlock(mdev->act_log);
908 } else {
909 dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n");
910 return;
911 }
912
913 spin_lock_irq(&mdev->req_lock);
914 if (mdev->state.conn < C_CONNECTED)
915 s = !test_and_set_bit(AL_SUSPENDED, &mdev->flags);
916
917 spin_unlock_irq(&mdev->req_lock);
918
919 if (s)
920 dev_info(DEV, "Suspended AL updates\n");
921}
922
Philipp Reisnerb411b362009-09-25 16:07:19 -0700923/* does always return 0;
924 * interesting return code is in reply->ret_code */
925static int drbd_nl_disk_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
926 struct drbd_nl_cfg_reply *reply)
927{
Andreas Gruenbacher116676c2010-12-08 13:33:11 +0100928 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700929 enum determine_dev_size dd;
930 sector_t max_possible_sectors;
931 sector_t min_md_device_sectors;
932 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
Tejun Heoe525fd82010-11-13 11:55:17 +0100933 struct block_device *bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700934 struct lru_cache *resync_lru = NULL;
935 union drbd_state ns, os;
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +0100936 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700937 int cp_discovered = 0;
938 int logical_block_size;
939
940 drbd_reconfig_start(mdev);
941
942 /* if you want to reconfigure, please tear down first */
943 if (mdev->state.disk > D_DISKLESS) {
944 retcode = ERR_DISK_CONFIGURED;
945 goto fail;
946 }
Lars Ellenberg82f59cc2010-10-16 12:13:47 +0200947 /* It may just now have detached because of IO error. Make sure
948 * drbd_ldev_destroy is done already, we may end up here very fast,
949 * e.g. if someone calls attach from the on-io-error handler,
950 * to realize a "hot spare" feature (not that I'd recommend that) */
951 wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700952
953 /* allocation not in the IO path, cqueue thread context */
954 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
955 if (!nbc) {
956 retcode = ERR_NOMEM;
957 goto fail;
958 }
959
960 nbc->dc.disk_size = DRBD_DISK_SIZE_SECT_DEF;
961 nbc->dc.on_io_error = DRBD_ON_IO_ERROR_DEF;
962 nbc->dc.fencing = DRBD_FENCING_DEF;
963 nbc->dc.max_bio_bvecs = DRBD_MAX_BIO_BVECS_DEF;
964
965 if (!disk_conf_from_tags(mdev, nlp->tag_list, &nbc->dc)) {
966 retcode = ERR_MANDATORY_TAG;
967 goto fail;
968 }
969
970 if (nbc->dc.meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
971 retcode = ERR_MD_IDX_INVALID;
972 goto fail;
973 }
974
Philipp Reisner47ff2d02010-06-18 13:56:57 +0200975 if (get_net_conf(mdev)) {
976 int prot = mdev->net_conf->wire_protocol;
977 put_net_conf(mdev);
978 if (nbc->dc.fencing == FP_STONITH && prot == DRBD_PROT_A) {
979 retcode = ERR_STONITH_AND_PROT_A;
980 goto fail;
981 }
982 }
983
Tejun Heod4d77622010-11-13 11:55:18 +0100984 bdev = blkdev_get_by_path(nbc->dc.backing_dev,
985 FMODE_READ | FMODE_WRITE | FMODE_EXCL, mdev);
Tejun Heoe525fd82010-11-13 11:55:17 +0100986 if (IS_ERR(bdev)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700987 dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.backing_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +0100988 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700989 retcode = ERR_OPEN_DISK;
990 goto fail;
991 }
Tejun Heoe525fd82010-11-13 11:55:17 +0100992 nbc->backing_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700993
Tejun Heoe525fd82010-11-13 11:55:17 +0100994 /*
995 * meta_dev_idx >= 0: external fixed size, possibly multiple
996 * drbd sharing one meta device. TODO in that case, paranoia
997 * check that [md_bdev, meta_dev_idx] is not yet used by some
998 * other drbd minor! (if you use drbd.conf + drbdadm, that
999 * should check it for you already; but if you don't, or
1000 * someone fooled it, we need to double check here)
1001 */
Tejun Heod4d77622010-11-13 11:55:18 +01001002 bdev = blkdev_get_by_path(nbc->dc.meta_dev,
1003 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
1004 (nbc->dc.meta_dev_idx < 0) ?
1005 (void *)mdev : (void *)drbd_m_holder);
Tejun Heoe525fd82010-11-13 11:55:17 +01001006 if (IS_ERR(bdev)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001007 dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.meta_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001008 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001009 retcode = ERR_OPEN_MD_DISK;
1010 goto fail;
1011 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001012 nbc->md_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001013
Tejun Heoe525fd82010-11-13 11:55:17 +01001014 if ((nbc->backing_bdev == nbc->md_bdev) !=
1015 (nbc->dc.meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1016 nbc->dc.meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
1017 retcode = ERR_MD_IDX_INVALID;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001018 goto fail;
1019 }
1020
1021 resync_lru = lc_create("resync", drbd_bm_ext_cache,
1022 61, sizeof(struct bm_extent),
1023 offsetof(struct bm_extent, lce));
1024 if (!resync_lru) {
1025 retcode = ERR_NOMEM;
Tejun Heoe525fd82010-11-13 11:55:17 +01001026 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001027 }
1028
1029 /* RT - for drbd_get_max_capacity() DRBD_MD_INDEX_FLEX_INT */
1030 drbd_md_set_sector_offsets(mdev, nbc);
1031
1032 if (drbd_get_max_capacity(nbc) < nbc->dc.disk_size) {
1033 dev_err(DEV, "max capacity %llu smaller than disk size %llu\n",
1034 (unsigned long long) drbd_get_max_capacity(nbc),
1035 (unsigned long long) nbc->dc.disk_size);
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001036 retcode = ERR_DISK_TOO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001037 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001038 }
1039
1040 if (nbc->dc.meta_dev_idx < 0) {
1041 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1042 /* at least one MB, otherwise it does not make sense */
1043 min_md_device_sectors = (2<<10);
1044 } else {
1045 max_possible_sectors = DRBD_MAX_SECTORS;
1046 min_md_device_sectors = MD_RESERVED_SECT * (nbc->dc.meta_dev_idx + 1);
1047 }
1048
Philipp Reisnerb411b362009-09-25 16:07:19 -07001049 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001050 retcode = ERR_MD_DISK_TOO_SMALL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001051 dev_warn(DEV, "refusing attach: md-device too small, "
1052 "at least %llu sectors needed for this meta-disk type\n",
1053 (unsigned long long) min_md_device_sectors);
Tejun Heoe525fd82010-11-13 11:55:17 +01001054 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001055 }
1056
1057 /* Make sure the new disk is big enough
1058 * (we may currently be R_PRIMARY with no local disk...) */
1059 if (drbd_get_max_capacity(nbc) <
1060 drbd_get_capacity(mdev->this_bdev)) {
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001061 retcode = ERR_DISK_TOO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001062 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001063 }
1064
1065 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1066
Lars Ellenberg13529942009-10-12 19:07:49 +02001067 if (nbc->known_size > max_possible_sectors) {
1068 dev_warn(DEV, "==> truncating very big lower level device "
1069 "to currently maximum possible %llu sectors <==\n",
1070 (unsigned long long) max_possible_sectors);
1071 if (nbc->dc.meta_dev_idx >= 0)
1072 dev_warn(DEV, "==>> using internal or flexible "
1073 "meta data may help <<==\n");
1074 }
1075
Philipp Reisnerb411b362009-09-25 16:07:19 -07001076 drbd_suspend_io(mdev);
1077 /* also wait for the last barrier ack. */
Philipp Reisnerfb22c402010-09-08 23:20:21 +02001078 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_pending_cnt) || is_susp(mdev->state));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001079 /* and for any other previously queued work */
1080 drbd_flush_workqueue(mdev);
1081
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001082 rv = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE);
1083 retcode = rv; /* FIXME: Type mismatch. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001084 drbd_resume_io(mdev);
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001085 if (rv < SS_SUCCESS)
Tejun Heoe525fd82010-11-13 11:55:17 +01001086 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001087
1088 if (!get_ldev_if_state(mdev, D_ATTACHING))
1089 goto force_diskless;
1090
1091 drbd_md_set_sector_offsets(mdev, nbc);
1092
Lars Ellenberg4aa83b72010-02-26 16:53:24 +01001093 /* allocate a second IO page if logical_block_size != 512 */
1094 logical_block_size = bdev_logical_block_size(nbc->md_bdev);
1095 if (logical_block_size == 0)
1096 logical_block_size = MD_SECTOR_SIZE;
1097
1098 if (logical_block_size != MD_SECTOR_SIZE) {
1099 if (!mdev->md_io_tmpp) {
1100 struct page *page = alloc_page(GFP_NOIO);
1101 if (!page)
1102 goto force_diskless_dec;
1103
1104 dev_warn(DEV, "Meta data's bdev logical_block_size = %d != %d\n",
1105 logical_block_size, MD_SECTOR_SIZE);
1106 dev_warn(DEV, "Workaround engaged (has performance impact).\n");
1107
1108 mdev->md_io_tmpp = page;
1109 }
1110 }
1111
Philipp Reisnerb411b362009-09-25 16:07:19 -07001112 if (!mdev->bitmap) {
1113 if (drbd_bm_init(mdev)) {
1114 retcode = ERR_NOMEM;
1115 goto force_diskless_dec;
1116 }
1117 }
1118
1119 retcode = drbd_md_read(mdev, nbc);
1120 if (retcode != NO_ERROR)
1121 goto force_diskless_dec;
1122
1123 if (mdev->state.conn < C_CONNECTED &&
1124 mdev->state.role == R_PRIMARY &&
1125 (mdev->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
1126 dev_err(DEV, "Can only attach to data with current UUID=%016llX\n",
1127 (unsigned long long)mdev->ed_uuid);
1128 retcode = ERR_DATA_NOT_CURRENT;
1129 goto force_diskless_dec;
1130 }
1131
1132 /* Since we are diskless, fix the activity log first... */
1133 if (drbd_check_al_size(mdev)) {
1134 retcode = ERR_NOMEM;
1135 goto force_diskless_dec;
1136 }
1137
1138 /* Prevent shrinking of consistent devices ! */
1139 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
Philipp Reisnera393db62009-12-22 13:35:52 +01001140 drbd_new_dev_size(mdev, nbc, 0) < nbc->md.la_size_sect) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001141 dev_warn(DEV, "refusing to truncate a consistent device\n");
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001142 retcode = ERR_DISK_TOO_SMALL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001143 goto force_diskless_dec;
1144 }
1145
1146 if (!drbd_al_read_log(mdev, nbc)) {
1147 retcode = ERR_IO_MD_DISK;
1148 goto force_diskless_dec;
1149 }
1150
Philipp Reisnerb411b362009-09-25 16:07:19 -07001151 /* Reset the "barriers don't work" bits here, then force meta data to
1152 * be written, to ensure we determine if barriers are supported. */
1153 if (nbc->dc.no_md_flush)
Philipp Reisnera8a4e512010-08-25 10:21:04 +02001154 set_bit(MD_NO_FUA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001155 else
Philipp Reisnera8a4e512010-08-25 10:21:04 +02001156 clear_bit(MD_NO_FUA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001157
1158 /* Point of no return reached.
1159 * Devices and memory are no longer released by error cleanup below.
1160 * now mdev takes over responsibility, and the state engine should
1161 * clean it up somewhere. */
1162 D_ASSERT(mdev->ldev == NULL);
1163 mdev->ldev = nbc;
1164 mdev->resync = resync_lru;
1165 nbc = NULL;
1166 resync_lru = NULL;
1167
Philipp Reisner2451fc32010-08-24 13:43:11 +02001168 mdev->write_ordering = WO_bdev_flush;
1169 drbd_bump_write_ordering(mdev, WO_bdev_flush);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001170
1171 if (drbd_md_test_flag(mdev->ldev, MDF_CRASHED_PRIMARY))
1172 set_bit(CRASHED_PRIMARY, &mdev->flags);
1173 else
1174 clear_bit(CRASHED_PRIMARY, &mdev->flags);
1175
Philipp Reisner894c6a92010-06-18 16:03:20 +02001176 if (drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
Philipp Reisnerfb22c402010-09-08 23:20:21 +02001177 !(mdev->state.role == R_PRIMARY && mdev->state.susp_nod)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001178 set_bit(CRASHED_PRIMARY, &mdev->flags);
1179 cp_discovered = 1;
1180 }
1181
1182 mdev->send_cnt = 0;
1183 mdev->recv_cnt = 0;
1184 mdev->read_cnt = 0;
1185 mdev->writ_cnt = 0;
1186
Philipp Reisner99432fc2011-05-20 16:39:13 +02001187 drbd_reconsider_max_bio_size(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001188
1189 /* If I am currently not R_PRIMARY,
1190 * but meta data primary indicator is set,
1191 * I just now recover from a hard crash,
1192 * and have been R_PRIMARY before that crash.
1193 *
1194 * Now, if I had no connection before that crash
1195 * (have been degraded R_PRIMARY), chances are that
1196 * I won't find my peer now either.
1197 *
1198 * In that case, and _only_ in that case,
1199 * we use the degr-wfc-timeout instead of the default,
1200 * so we can automatically recover from a crash of a
1201 * degraded but active "cluster" after a certain timeout.
1202 */
1203 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
1204 if (mdev->state.role != R_PRIMARY &&
1205 drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
1206 !drbd_md_test_flag(mdev->ldev, MDF_CONNECTED_IND))
1207 set_bit(USE_DEGR_WFC_T, &mdev->flags);
1208
Bart Van Assche24c48302011-05-21 18:32:29 +02001209 dd = drbd_determine_dev_size(mdev, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001210 if (dd == dev_size_error) {
1211 retcode = ERR_NOMEM_BITMAP;
1212 goto force_diskless_dec;
1213 } else if (dd == grew)
1214 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
1215
1216 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
1217 dev_info(DEV, "Assuming that all blocks are out of sync "
1218 "(aka FullSync)\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001219 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write,
1220 "set_n_write from attaching", BM_LOCKED_MASK)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001221 retcode = ERR_IO_MD_DISK;
1222 goto force_diskless_dec;
1223 }
1224 } else {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001225 if (drbd_bitmap_io(mdev, &drbd_bm_read,
1226 "read from attaching", BM_LOCKED_MASK) < 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001227 retcode = ERR_IO_MD_DISK;
1228 goto force_diskless_dec;
1229 }
1230 }
1231
1232 if (cp_discovered) {
1233 drbd_al_apply_to_bm(mdev);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001234 if (drbd_bitmap_io(mdev, &drbd_bm_write,
1235 "crashed primary apply AL", BM_LOCKED_MASK)) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001236 retcode = ERR_IO_MD_DISK;
1237 goto force_diskless_dec;
1238 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001239 }
1240
Philipp Reisner07782862010-08-31 12:00:50 +02001241 if (_drbd_bm_total_weight(mdev) == drbd_bm_bits(mdev))
1242 drbd_suspend_al(mdev); /* IO is still suspended here... */
1243
Philipp Reisnerb411b362009-09-25 16:07:19 -07001244 spin_lock_irq(&mdev->req_lock);
1245 os = mdev->state;
1246 ns.i = os.i;
1247 /* If MDF_CONSISTENT is not set go into inconsistent state,
1248 otherwise investigate MDF_WasUpToDate...
1249 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1250 otherwise into D_CONSISTENT state.
1251 */
1252 if (drbd_md_test_flag(mdev->ldev, MDF_CONSISTENT)) {
1253 if (drbd_md_test_flag(mdev->ldev, MDF_WAS_UP_TO_DATE))
1254 ns.disk = D_CONSISTENT;
1255 else
1256 ns.disk = D_OUTDATED;
1257 } else {
1258 ns.disk = D_INCONSISTENT;
1259 }
1260
1261 if (drbd_md_test_flag(mdev->ldev, MDF_PEER_OUT_DATED))
1262 ns.pdsk = D_OUTDATED;
1263
1264 if ( ns.disk == D_CONSISTENT &&
1265 (ns.pdsk == D_OUTDATED || mdev->ldev->dc.fencing == FP_DONT_CARE))
1266 ns.disk = D_UP_TO_DATE;
1267
1268 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1269 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1270 this point, because drbd_request_state() modifies these
1271 flags. */
1272
1273 /* In case we are C_CONNECTED postpone any decision on the new disk
1274 state after the negotiation phase. */
1275 if (mdev->state.conn == C_CONNECTED) {
1276 mdev->new_state_tmp.i = ns.i;
1277 ns.i = os.i;
1278 ns.disk = D_NEGOTIATING;
Philipp Reisnerdc66c742010-06-02 14:31:29 +02001279
1280 /* We expect to receive up-to-date UUIDs soon.
1281 To avoid a race in receive_state, free p_uuid while
1282 holding req_lock. I.e. atomic with the state change */
1283 kfree(mdev->p_uuid);
1284 mdev->p_uuid = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001285 }
1286
1287 rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
1288 ns = mdev->state;
1289 spin_unlock_irq(&mdev->req_lock);
1290
1291 if (rv < SS_SUCCESS)
1292 goto force_diskless_dec;
1293
1294 if (mdev->state.role == R_PRIMARY)
1295 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
1296 else
1297 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
1298
1299 drbd_md_mark_dirty(mdev);
1300 drbd_md_sync(mdev);
1301
1302 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1303 put_ldev(mdev);
1304 reply->ret_code = retcode;
1305 drbd_reconfig_done(mdev);
1306 return 0;
1307
1308 force_diskless_dec:
1309 put_ldev(mdev);
1310 force_diskless:
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001311 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001312 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001313 fail:
1314 if (nbc) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001315 if (nbc->backing_bdev)
1316 blkdev_put(nbc->backing_bdev,
1317 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1318 if (nbc->md_bdev)
1319 blkdev_put(nbc->md_bdev,
1320 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001321 kfree(nbc);
1322 }
1323 lc_destroy(resync_lru);
1324
1325 reply->ret_code = retcode;
1326 drbd_reconfig_done(mdev);
1327 return 0;
1328}
1329
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001330/* Detaching the disk is a process in multiple stages. First we need to lock
1331 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1332 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1333 * internal references as well.
1334 * Only then we have finally detached. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001335static int drbd_nl_detach(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1336 struct drbd_nl_cfg_reply *reply)
1337{
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001338 enum drbd_ret_code retcode;
1339 int ret;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001340 struct detach dt = {};
1341
1342 if (!detach_from_tags(mdev, nlp->tag_list, &dt)) {
1343 reply->ret_code = ERR_MANDATORY_TAG;
1344 goto out;
1345 }
1346
1347 if (dt.detach_force) {
1348 drbd_force_state(mdev, NS(disk, D_FAILED));
1349 reply->ret_code = SS_SUCCESS;
1350 goto out;
1351 }
1352
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001353 drbd_suspend_io(mdev); /* so no-one is stuck in drbd_al_begin_io */
Lars Ellenberga2e91382011-10-06 17:30:26 +02001354 drbd_md_get_buffer(mdev); /* make sure there is no in-flight meta-data IO */
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001355 retcode = drbd_request_state(mdev, NS(disk, D_FAILED));
Lars Ellenberga2e91382011-10-06 17:30:26 +02001356 drbd_md_put_buffer(mdev);
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001357 /* D_FAILED will transition to DISKLESS. */
1358 ret = wait_event_interruptible(mdev->misc_wait,
1359 mdev->state.disk != D_FAILED);
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001360 drbd_resume_io(mdev);
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001361
Philipp Reisner9b2f61a2011-05-24 10:27:38 +02001362 if ((int)retcode == (int)SS_IS_DISKLESS)
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001363 retcode = SS_NOTHING_TO_DO;
1364 if (ret)
1365 retcode = ERR_INTR;
1366 reply->ret_code = retcode;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001367out:
Philipp Reisnerb411b362009-09-25 16:07:19 -07001368 return 0;
1369}
1370
1371static int drbd_nl_net_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1372 struct drbd_nl_cfg_reply *reply)
1373{
1374 int i, ns;
Andreas Gruenbacher116676c2010-12-08 13:33:11 +01001375 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001376 struct net_conf *new_conf = NULL;
1377 struct crypto_hash *tfm = NULL;
1378 struct crypto_hash *integrity_w_tfm = NULL;
1379 struct crypto_hash *integrity_r_tfm = NULL;
1380 struct hlist_head *new_tl_hash = NULL;
1381 struct hlist_head *new_ee_hash = NULL;
1382 struct drbd_conf *odev;
1383 char hmac_name[CRYPTO_MAX_ALG_NAME];
1384 void *int_dig_out = NULL;
1385 void *int_dig_in = NULL;
1386 void *int_dig_vv = NULL;
1387 struct sockaddr *new_my_addr, *new_peer_addr, *taken_addr;
1388
1389 drbd_reconfig_start(mdev);
1390
1391 if (mdev->state.conn > C_STANDALONE) {
1392 retcode = ERR_NET_CONFIGURED;
1393 goto fail;
1394 }
1395
1396 /* allocation not in the IO path, cqueue thread context */
Julia Lawall2db4e422010-05-13 22:02:21 +02001397 new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001398 if (!new_conf) {
1399 retcode = ERR_NOMEM;
1400 goto fail;
1401 }
1402
Philipp Reisnerb411b362009-09-25 16:07:19 -07001403 new_conf->timeout = DRBD_TIMEOUT_DEF;
1404 new_conf->try_connect_int = DRBD_CONNECT_INT_DEF;
1405 new_conf->ping_int = DRBD_PING_INT_DEF;
1406 new_conf->max_epoch_size = DRBD_MAX_EPOCH_SIZE_DEF;
1407 new_conf->max_buffers = DRBD_MAX_BUFFERS_DEF;
1408 new_conf->unplug_watermark = DRBD_UNPLUG_WATERMARK_DEF;
1409 new_conf->sndbuf_size = DRBD_SNDBUF_SIZE_DEF;
1410 new_conf->rcvbuf_size = DRBD_RCVBUF_SIZE_DEF;
1411 new_conf->ko_count = DRBD_KO_COUNT_DEF;
1412 new_conf->after_sb_0p = DRBD_AFTER_SB_0P_DEF;
1413 new_conf->after_sb_1p = DRBD_AFTER_SB_1P_DEF;
1414 new_conf->after_sb_2p = DRBD_AFTER_SB_2P_DEF;
1415 new_conf->want_lose = 0;
1416 new_conf->two_primaries = 0;
1417 new_conf->wire_protocol = DRBD_PROT_C;
1418 new_conf->ping_timeo = DRBD_PING_TIMEO_DEF;
1419 new_conf->rr_conflict = DRBD_RR_CONFLICT_DEF;
Philipp Reisner422028b2010-10-27 11:12:07 +02001420 new_conf->on_congestion = DRBD_ON_CONGESTION_DEF;
1421 new_conf->cong_extents = DRBD_CONG_EXTENTS_DEF;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001422
1423 if (!net_conf_from_tags(mdev, nlp->tag_list, new_conf)) {
1424 retcode = ERR_MANDATORY_TAG;
1425 goto fail;
1426 }
1427
1428 if (new_conf->two_primaries
1429 && (new_conf->wire_protocol != DRBD_PROT_C)) {
1430 retcode = ERR_NOT_PROTO_C;
1431 goto fail;
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001432 }
1433
1434 if (get_ldev(mdev)) {
1435 enum drbd_fencing_p fp = mdev->ldev->dc.fencing;
1436 put_ldev(mdev);
1437 if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH) {
1438 retcode = ERR_STONITH_AND_PROT_A;
1439 goto fail;
1440 }
1441 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001442
Philipp Reisner422028b2010-10-27 11:12:07 +02001443 if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A) {
1444 retcode = ERR_CONG_NOT_PROTO_A;
1445 goto fail;
1446 }
1447
Philipp Reisnerb411b362009-09-25 16:07:19 -07001448 if (mdev->state.role == R_PRIMARY && new_conf->want_lose) {
1449 retcode = ERR_DISCARD;
1450 goto fail;
1451 }
1452
1453 retcode = NO_ERROR;
1454
1455 new_my_addr = (struct sockaddr *)&new_conf->my_addr;
1456 new_peer_addr = (struct sockaddr *)&new_conf->peer_addr;
1457 for (i = 0; i < minor_count; i++) {
1458 odev = minor_to_mdev(i);
1459 if (!odev || odev == mdev)
1460 continue;
1461 if (get_net_conf(odev)) {
1462 taken_addr = (struct sockaddr *)&odev->net_conf->my_addr;
1463 if (new_conf->my_addr_len == odev->net_conf->my_addr_len &&
1464 !memcmp(new_my_addr, taken_addr, new_conf->my_addr_len))
1465 retcode = ERR_LOCAL_ADDR;
1466
1467 taken_addr = (struct sockaddr *)&odev->net_conf->peer_addr;
1468 if (new_conf->peer_addr_len == odev->net_conf->peer_addr_len &&
1469 !memcmp(new_peer_addr, taken_addr, new_conf->peer_addr_len))
1470 retcode = ERR_PEER_ADDR;
1471
1472 put_net_conf(odev);
1473 if (retcode != NO_ERROR)
1474 goto fail;
1475 }
1476 }
1477
1478 if (new_conf->cram_hmac_alg[0] != 0) {
1479 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
1480 new_conf->cram_hmac_alg);
1481 tfm = crypto_alloc_hash(hmac_name, 0, CRYPTO_ALG_ASYNC);
1482 if (IS_ERR(tfm)) {
1483 tfm = NULL;
1484 retcode = ERR_AUTH_ALG;
1485 goto fail;
1486 }
1487
Philipp Reisner07982192009-12-28 16:58:38 +01001488 if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001489 retcode = ERR_AUTH_ALG_ND;
1490 goto fail;
1491 }
1492 }
1493
1494 if (new_conf->integrity_alg[0]) {
1495 integrity_w_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC);
1496 if (IS_ERR(integrity_w_tfm)) {
1497 integrity_w_tfm = NULL;
1498 retcode=ERR_INTEGRITY_ALG;
1499 goto fail;
1500 }
1501
1502 if (!drbd_crypto_is_hash(crypto_hash_tfm(integrity_w_tfm))) {
1503 retcode=ERR_INTEGRITY_ALG_ND;
1504 goto fail;
1505 }
1506
1507 integrity_r_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC);
1508 if (IS_ERR(integrity_r_tfm)) {
1509 integrity_r_tfm = NULL;
1510 retcode=ERR_INTEGRITY_ALG;
1511 goto fail;
1512 }
1513 }
1514
1515 ns = new_conf->max_epoch_size/8;
1516 if (mdev->tl_hash_s != ns) {
1517 new_tl_hash = kzalloc(ns*sizeof(void *), GFP_KERNEL);
1518 if (!new_tl_hash) {
1519 retcode = ERR_NOMEM;
1520 goto fail;
1521 }
1522 }
1523
1524 ns = new_conf->max_buffers/8;
1525 if (new_conf->two_primaries && (mdev->ee_hash_s != ns)) {
1526 new_ee_hash = kzalloc(ns*sizeof(void *), GFP_KERNEL);
1527 if (!new_ee_hash) {
1528 retcode = ERR_NOMEM;
1529 goto fail;
1530 }
1531 }
1532
1533 ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
1534
1535 if (integrity_w_tfm) {
1536 i = crypto_hash_digestsize(integrity_w_tfm);
1537 int_dig_out = kmalloc(i, GFP_KERNEL);
1538 if (!int_dig_out) {
1539 retcode = ERR_NOMEM;
1540 goto fail;
1541 }
1542 int_dig_in = kmalloc(i, GFP_KERNEL);
1543 if (!int_dig_in) {
1544 retcode = ERR_NOMEM;
1545 goto fail;
1546 }
1547 int_dig_vv = kmalloc(i, GFP_KERNEL);
1548 if (!int_dig_vv) {
1549 retcode = ERR_NOMEM;
1550 goto fail;
1551 }
1552 }
1553
1554 if (!mdev->bitmap) {
1555 if(drbd_bm_init(mdev)) {
1556 retcode = ERR_NOMEM;
1557 goto fail;
1558 }
1559 }
1560
Philipp Reisnerf70b35112010-06-24 14:34:40 +02001561 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001562 spin_lock_irq(&mdev->req_lock);
1563 if (mdev->net_conf != NULL) {
1564 retcode = ERR_NET_CONFIGURED;
1565 spin_unlock_irq(&mdev->req_lock);
1566 goto fail;
1567 }
1568 mdev->net_conf = new_conf;
1569
1570 mdev->send_cnt = 0;
1571 mdev->recv_cnt = 0;
1572
1573 if (new_tl_hash) {
1574 kfree(mdev->tl_hash);
1575 mdev->tl_hash_s = mdev->net_conf->max_epoch_size/8;
1576 mdev->tl_hash = new_tl_hash;
1577 }
1578
1579 if (new_ee_hash) {
1580 kfree(mdev->ee_hash);
1581 mdev->ee_hash_s = mdev->net_conf->max_buffers/8;
1582 mdev->ee_hash = new_ee_hash;
1583 }
1584
1585 crypto_free_hash(mdev->cram_hmac_tfm);
1586 mdev->cram_hmac_tfm = tfm;
1587
1588 crypto_free_hash(mdev->integrity_w_tfm);
1589 mdev->integrity_w_tfm = integrity_w_tfm;
1590
1591 crypto_free_hash(mdev->integrity_r_tfm);
1592 mdev->integrity_r_tfm = integrity_r_tfm;
1593
1594 kfree(mdev->int_dig_out);
1595 kfree(mdev->int_dig_in);
1596 kfree(mdev->int_dig_vv);
1597 mdev->int_dig_out=int_dig_out;
1598 mdev->int_dig_in=int_dig_in;
1599 mdev->int_dig_vv=int_dig_vv;
Philipp Reisnerf70b35112010-06-24 14:34:40 +02001600 retcode = _drbd_set_state(_NS(mdev, conn, C_UNCONNECTED), CS_VERBOSE, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001601 spin_unlock_irq(&mdev->req_lock);
1602
Philipp Reisnerb411b362009-09-25 16:07:19 -07001603 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1604 reply->ret_code = retcode;
1605 drbd_reconfig_done(mdev);
1606 return 0;
1607
1608fail:
1609 kfree(int_dig_out);
1610 kfree(int_dig_in);
1611 kfree(int_dig_vv);
1612 crypto_free_hash(tfm);
1613 crypto_free_hash(integrity_w_tfm);
1614 crypto_free_hash(integrity_r_tfm);
1615 kfree(new_tl_hash);
1616 kfree(new_ee_hash);
1617 kfree(new_conf);
1618
1619 reply->ret_code = retcode;
1620 drbd_reconfig_done(mdev);
1621 return 0;
1622}
1623
1624static int drbd_nl_disconnect(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1625 struct drbd_nl_cfg_reply *reply)
1626{
1627 int retcode;
Philipp Reisner2561b9c2010-12-03 15:22:48 +01001628 struct disconnect dc;
1629
1630 memset(&dc, 0, sizeof(struct disconnect));
1631 if (!disconnect_from_tags(mdev, nlp->tag_list, &dc)) {
1632 retcode = ERR_MANDATORY_TAG;
1633 goto fail;
1634 }
1635
1636 if (dc.force) {
1637 spin_lock_irq(&mdev->req_lock);
1638 if (mdev->state.conn >= C_WF_CONNECTION)
1639 _drbd_set_state(_NS(mdev, conn, C_DISCONNECTING), CS_HARD, NULL);
1640 spin_unlock_irq(&mdev->req_lock);
1641 goto done;
1642 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001643
1644 retcode = _drbd_request_state(mdev, NS(conn, C_DISCONNECTING), CS_ORDERED);
1645
1646 if (retcode == SS_NOTHING_TO_DO)
1647 goto done;
1648 else if (retcode == SS_ALREADY_STANDALONE)
1649 goto done;
1650 else if (retcode == SS_PRIMARY_NOP) {
1651 /* Our statche checking code wants to see the peer outdated. */
1652 retcode = drbd_request_state(mdev, NS2(conn, C_DISCONNECTING,
1653 pdsk, D_OUTDATED));
1654 } else if (retcode == SS_CW_FAILED_BY_PEER) {
1655 /* The peer probably wants to see us outdated. */
1656 retcode = _drbd_request_state(mdev, NS2(conn, C_DISCONNECTING,
1657 disk, D_OUTDATED),
1658 CS_ORDERED);
1659 if (retcode == SS_IS_DISKLESS || retcode == SS_LOWER_THAN_OUTDATED) {
1660 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
1661 retcode = SS_SUCCESS;
1662 }
1663 }
1664
1665 if (retcode < SS_SUCCESS)
1666 goto fail;
1667
1668 if (wait_event_interruptible(mdev->state_wait,
1669 mdev->state.conn != C_DISCONNECTING)) {
1670 /* Do not test for mdev->state.conn == C_STANDALONE, since
1671 someone else might connect us in the mean time! */
1672 retcode = ERR_INTR;
1673 goto fail;
1674 }
1675
1676 done:
1677 retcode = NO_ERROR;
1678 fail:
1679 drbd_md_sync(mdev);
1680 reply->ret_code = retcode;
1681 return 0;
1682}
1683
1684void resync_after_online_grow(struct drbd_conf *mdev)
1685{
1686 int iass; /* I am sync source */
1687
1688 dev_info(DEV, "Resync of new storage after online grow\n");
1689 if (mdev->state.role != mdev->state.peer)
1690 iass = (mdev->state.role == R_PRIMARY);
1691 else
1692 iass = test_bit(DISCARD_CONCURRENT, &mdev->flags);
1693
1694 if (iass)
1695 drbd_start_resync(mdev, C_SYNC_SOURCE);
1696 else
1697 _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
1698}
1699
1700static int drbd_nl_resize(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1701 struct drbd_nl_cfg_reply *reply)
1702{
1703 struct resize rs;
1704 int retcode = NO_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001705 enum determine_dev_size dd;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01001706 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001707
1708 memset(&rs, 0, sizeof(struct resize));
1709 if (!resize_from_tags(mdev, nlp->tag_list, &rs)) {
1710 retcode = ERR_MANDATORY_TAG;
1711 goto fail;
1712 }
1713
1714 if (mdev->state.conn > C_CONNECTED) {
1715 retcode = ERR_RESIZE_RESYNC;
1716 goto fail;
1717 }
1718
1719 if (mdev->state.role == R_SECONDARY &&
1720 mdev->state.peer == R_SECONDARY) {
1721 retcode = ERR_NO_PRIMARY;
1722 goto fail;
1723 }
1724
1725 if (!get_ldev(mdev)) {
1726 retcode = ERR_NO_DISK;
1727 goto fail;
1728 }
1729
Philipp Reisner6495d2c2010-03-24 16:07:04 +01001730 if (rs.no_resync && mdev->agreed_pro_version < 93) {
1731 retcode = ERR_NEED_APV_93;
Andreas Gruenbacher7b4e4d32011-09-28 22:15:04 +02001732 goto fail_ldev;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01001733 }
1734
Philipp Reisner087c2492010-03-26 13:49:56 +01001735 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001736 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001737
1738 mdev->ldev->dc.disk_size = (sector_t)rs.resize_size;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01001739 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
Bart Van Assche24c48302011-05-21 18:32:29 +02001740 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001741 drbd_md_sync(mdev);
1742 put_ldev(mdev);
1743 if (dd == dev_size_error) {
1744 retcode = ERR_NOMEM_BITMAP;
1745 goto fail;
1746 }
1747
Philipp Reisner087c2492010-03-26 13:49:56 +01001748 if (mdev->state.conn == C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001749 if (dd == grew)
1750 set_bit(RESIZE_PENDING, &mdev->flags);
1751
1752 drbd_send_uuids(mdev);
Philipp Reisner6495d2c2010-03-24 16:07:04 +01001753 drbd_send_sizes(mdev, 1, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001754 }
1755
1756 fail:
1757 reply->ret_code = retcode;
1758 return 0;
Andreas Gruenbacher7b4e4d32011-09-28 22:15:04 +02001759
1760 fail_ldev:
1761 put_ldev(mdev);
1762 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001763}
1764
1765static int drbd_nl_syncer_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1766 struct drbd_nl_cfg_reply *reply)
1767{
1768 int retcode = NO_ERROR;
1769 int err;
1770 int ovr; /* online verify running */
1771 int rsr; /* re-sync running */
1772 struct crypto_hash *verify_tfm = NULL;
1773 struct crypto_hash *csums_tfm = NULL;
1774 struct syncer_conf sc;
1775 cpumask_var_t new_cpu_mask;
Philipp Reisner778f2712010-07-06 11:14:00 +02001776 int *rs_plan_s = NULL;
1777 int fifo_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001778
1779 if (!zalloc_cpumask_var(&new_cpu_mask, GFP_KERNEL)) {
1780 retcode = ERR_NOMEM;
1781 goto fail;
1782 }
1783
1784 if (nlp->flags & DRBD_NL_SET_DEFAULTS) {
1785 memset(&sc, 0, sizeof(struct syncer_conf));
1786 sc.rate = DRBD_RATE_DEF;
1787 sc.after = DRBD_AFTER_DEF;
1788 sc.al_extents = DRBD_AL_EXTENTS_DEF;
Philipp Reisner265be2d2010-05-31 10:14:17 +02001789 sc.on_no_data = DRBD_ON_NO_DATA_DEF;
Philipp Reisner9a31d712010-07-05 13:42:03 +02001790 sc.c_plan_ahead = DRBD_C_PLAN_AHEAD_DEF;
1791 sc.c_delay_target = DRBD_C_DELAY_TARGET_DEF;
1792 sc.c_fill_target = DRBD_C_FILL_TARGET_DEF;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001793 sc.c_max_rate = DRBD_C_MAX_RATE_DEF;
1794 sc.c_min_rate = DRBD_C_MIN_RATE_DEF;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001795 } else
1796 memcpy(&sc, &mdev->sync_conf, sizeof(struct syncer_conf));
1797
1798 if (!syncer_conf_from_tags(mdev, nlp->tag_list, &sc)) {
1799 retcode = ERR_MANDATORY_TAG;
1800 goto fail;
1801 }
1802
1803 /* re-sync running */
1804 rsr = ( mdev->state.conn == C_SYNC_SOURCE ||
1805 mdev->state.conn == C_SYNC_TARGET ||
1806 mdev->state.conn == C_PAUSED_SYNC_S ||
1807 mdev->state.conn == C_PAUSED_SYNC_T );
1808
1809 if (rsr && strcmp(sc.csums_alg, mdev->sync_conf.csums_alg)) {
1810 retcode = ERR_CSUMS_RESYNC_RUNNING;
1811 goto fail;
1812 }
1813
1814 if (!rsr && sc.csums_alg[0]) {
1815 csums_tfm = crypto_alloc_hash(sc.csums_alg, 0, CRYPTO_ALG_ASYNC);
1816 if (IS_ERR(csums_tfm)) {
1817 csums_tfm = NULL;
1818 retcode = ERR_CSUMS_ALG;
1819 goto fail;
1820 }
1821
1822 if (!drbd_crypto_is_hash(crypto_hash_tfm(csums_tfm))) {
1823 retcode = ERR_CSUMS_ALG_ND;
1824 goto fail;
1825 }
1826 }
1827
1828 /* online verify running */
1829 ovr = (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T);
1830
1831 if (ovr) {
1832 if (strcmp(sc.verify_alg, mdev->sync_conf.verify_alg)) {
1833 retcode = ERR_VERIFY_RUNNING;
1834 goto fail;
1835 }
1836 }
1837
1838 if (!ovr && sc.verify_alg[0]) {
1839 verify_tfm = crypto_alloc_hash(sc.verify_alg, 0, CRYPTO_ALG_ASYNC);
1840 if (IS_ERR(verify_tfm)) {
1841 verify_tfm = NULL;
1842 retcode = ERR_VERIFY_ALG;
1843 goto fail;
1844 }
1845
1846 if (!drbd_crypto_is_hash(crypto_hash_tfm(verify_tfm))) {
1847 retcode = ERR_VERIFY_ALG_ND;
1848 goto fail;
1849 }
1850 }
1851
1852 /* silently ignore cpu mask on UP kernel */
1853 if (nr_cpu_ids > 1 && sc.cpu_mask[0] != 0) {
H Hartley Sweetenddad9ef2011-08-02 12:43:49 +02001854 err = bitmap_parse(sc.cpu_mask, 32,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001855 cpumask_bits(new_cpu_mask), nr_cpu_ids);
1856 if (err) {
H Hartley Sweetenddad9ef2011-08-02 12:43:49 +02001857 dev_warn(DEV, "bitmap_parse() failed with %d\n", err);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001858 retcode = ERR_CPU_MASK_PARSE;
1859 goto fail;
1860 }
1861 }
1862
1863 ERR_IF (sc.rate < 1) sc.rate = 1;
1864 ERR_IF (sc.al_extents < 7) sc.al_extents = 127; /* arbitrary minimum */
1865#define AL_MAX ((MD_AL_MAX_SIZE-1) * AL_EXTENTS_PT)
1866 if (sc.al_extents > AL_MAX) {
1867 dev_err(DEV, "sc.al_extents > %d\n", AL_MAX);
1868 sc.al_extents = AL_MAX;
1869 }
1870#undef AL_MAX
1871
Lars Ellenbergef50a3e2010-09-01 14:39:30 +02001872 /* to avoid spurious errors when configuring minors before configuring
1873 * the minors they depend on: if necessary, first create the minor we
1874 * depend on */
1875 if (sc.after >= 0)
1876 ensure_mdev(sc.after, 1);
1877
Philipp Reisnerb411b362009-09-25 16:07:19 -07001878 /* most sanity checks done, try to assign the new sync-after
1879 * dependency. need to hold the global lock in there,
1880 * to avoid a race in the dependency loop check. */
1881 retcode = drbd_alter_sa(mdev, sc.after);
1882 if (retcode != NO_ERROR)
1883 goto fail;
1884
Philipp Reisner778f2712010-07-06 11:14:00 +02001885 fifo_size = (sc.c_plan_ahead * 10 * SLEEP_TIME) / HZ;
1886 if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) {
1887 rs_plan_s = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL);
1888 if (!rs_plan_s) {
1889 dev_err(DEV, "kmalloc of fifo_buffer failed");
1890 retcode = ERR_NOMEM;
1891 goto fail;
1892 }
1893 }
1894
Philipp Reisnerb411b362009-09-25 16:07:19 -07001895 /* ok, assign the rest of it as well.
1896 * lock against receive_SyncParam() */
1897 spin_lock(&mdev->peer_seq_lock);
1898 mdev->sync_conf = sc;
1899
1900 if (!rsr) {
1901 crypto_free_hash(mdev->csums_tfm);
1902 mdev->csums_tfm = csums_tfm;
1903 csums_tfm = NULL;
1904 }
1905
1906 if (!ovr) {
1907 crypto_free_hash(mdev->verify_tfm);
1908 mdev->verify_tfm = verify_tfm;
1909 verify_tfm = NULL;
1910 }
Philipp Reisner778f2712010-07-06 11:14:00 +02001911
1912 if (fifo_size != mdev->rs_plan_s.size) {
1913 kfree(mdev->rs_plan_s.values);
1914 mdev->rs_plan_s.values = rs_plan_s;
1915 mdev->rs_plan_s.size = fifo_size;
1916 mdev->rs_planed = 0;
1917 rs_plan_s = NULL;
1918 }
1919
Philipp Reisnerb411b362009-09-25 16:07:19 -07001920 spin_unlock(&mdev->peer_seq_lock);
1921
1922 if (get_ldev(mdev)) {
1923 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
1924 drbd_al_shrink(mdev);
1925 err = drbd_check_al_size(mdev);
1926 lc_unlock(mdev->act_log);
1927 wake_up(&mdev->al_wait);
1928
1929 put_ldev(mdev);
1930 drbd_md_sync(mdev);
1931
1932 if (err) {
1933 retcode = ERR_NOMEM;
1934 goto fail;
1935 }
1936 }
1937
1938 if (mdev->state.conn >= C_CONNECTED)
1939 drbd_send_sync_param(mdev, &sc);
1940
1941 if (!cpumask_equal(mdev->cpu_mask, new_cpu_mask)) {
1942 cpumask_copy(mdev->cpu_mask, new_cpu_mask);
1943 drbd_calc_cpu_mask(mdev);
1944 mdev->receiver.reset_cpu_mask = 1;
1945 mdev->asender.reset_cpu_mask = 1;
1946 mdev->worker.reset_cpu_mask = 1;
1947 }
1948
1949 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1950fail:
Philipp Reisner778f2712010-07-06 11:14:00 +02001951 kfree(rs_plan_s);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001952 free_cpumask_var(new_cpu_mask);
1953 crypto_free_hash(csums_tfm);
1954 crypto_free_hash(verify_tfm);
1955 reply->ret_code = retcode;
1956 return 0;
1957}
1958
1959static int drbd_nl_invalidate(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1960 struct drbd_nl_cfg_reply *reply)
1961{
1962 int retcode;
1963
Lars Ellenberg194bfb32011-01-18 10:38:01 +01001964 /* If there is still bitmap IO pending, probably because of a previous
1965 * resync just being finished, wait for it before requesting a new resync. */
1966 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
1967
Philipp Reisnerb411b362009-09-25 16:07:19 -07001968 retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T), CS_ORDERED);
1969
1970 if (retcode < SS_SUCCESS && retcode != SS_NEED_CONNECTION)
1971 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
1972
1973 while (retcode == SS_NEED_CONNECTION) {
1974 spin_lock_irq(&mdev->req_lock);
1975 if (mdev->state.conn < C_CONNECTED)
1976 retcode = _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_VERBOSE, NULL);
1977 spin_unlock_irq(&mdev->req_lock);
1978
1979 if (retcode != SS_NEED_CONNECTION)
1980 break;
1981
1982 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
1983 }
1984
1985 reply->ret_code = retcode;
1986 return 0;
1987}
1988
Philipp Reisner07782862010-08-31 12:00:50 +02001989static int drbd_bmio_set_susp_al(struct drbd_conf *mdev)
1990{
1991 int rv;
1992
1993 rv = drbd_bmio_set_n_write(mdev);
1994 drbd_suspend_al(mdev);
1995 return rv;
1996}
1997
Philipp Reisnerb411b362009-09-25 16:07:19 -07001998static int drbd_nl_invalidate_peer(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1999 struct drbd_nl_cfg_reply *reply)
2000{
Philipp Reisner07782862010-08-31 12:00:50 +02002001 int retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002002
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002003 /* If there is still bitmap IO pending, probably because of a previous
2004 * resync just being finished, wait for it before requesting a new resync. */
2005 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
2006
Philipp Reisner07782862010-08-31 12:00:50 +02002007 retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S), CS_ORDERED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002008
Philipp Reisner07782862010-08-31 12:00:50 +02002009 if (retcode < SS_SUCCESS) {
2010 if (retcode == SS_NEED_CONNECTION && mdev->state.role == R_PRIMARY) {
2011 /* The peer will get a resync upon connect anyways. Just make that
2012 into a full resync. */
2013 retcode = drbd_request_state(mdev, NS(pdsk, D_INCONSISTENT));
2014 if (retcode >= SS_SUCCESS) {
Philipp Reisner07782862010-08-31 12:00:50 +02002015 if (drbd_bitmap_io(mdev, &drbd_bmio_set_susp_al,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002016 "set_n_write from invalidate_peer",
2017 BM_LOCKED_SET_ALLOWED))
Philipp Reisner07782862010-08-31 12:00:50 +02002018 retcode = ERR_IO_MD_DISK;
2019 }
2020 } else
2021 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S));
2022 }
2023
2024 reply->ret_code = retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002025 return 0;
2026}
2027
2028static int drbd_nl_pause_sync(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2029 struct drbd_nl_cfg_reply *reply)
2030{
2031 int retcode = NO_ERROR;
2032
2033 if (drbd_request_state(mdev, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
2034 retcode = ERR_PAUSE_IS_SET;
2035
2036 reply->ret_code = retcode;
2037 return 0;
2038}
2039
2040static int drbd_nl_resume_sync(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2041 struct drbd_nl_cfg_reply *reply)
2042{
2043 int retcode = NO_ERROR;
Philipp Reisnercd88d032011-01-20 11:46:41 +01002044 union drbd_state s;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002045
Philipp Reisnercd88d032011-01-20 11:46:41 +01002046 if (drbd_request_state(mdev, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2047 s = mdev->state;
2048 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2049 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2050 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2051 } else {
2052 retcode = ERR_PAUSE_IS_CLEAR;
2053 }
2054 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002055
2056 reply->ret_code = retcode;
2057 return 0;
2058}
2059
2060static int drbd_nl_suspend_io(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2061 struct drbd_nl_cfg_reply *reply)
2062{
2063 reply->ret_code = drbd_request_state(mdev, NS(susp, 1));
2064
2065 return 0;
2066}
2067
2068static int drbd_nl_resume_io(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2069 struct drbd_nl_cfg_reply *reply)
2070{
Philipp Reisner43a51822010-06-11 11:26:34 +02002071 if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
2072 drbd_uuid_new_current(mdev);
2073 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner43a51822010-06-11 11:26:34 +02002074 }
Philipp Reisner265be2d2010-05-31 10:14:17 +02002075 drbd_suspend_io(mdev);
Philipp Reisnerfb22c402010-09-08 23:20:21 +02002076 reply->ret_code = drbd_request_state(mdev, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
Philipp Reisner265be2d2010-05-31 10:14:17 +02002077 if (reply->ret_code == SS_SUCCESS) {
2078 if (mdev->state.conn < C_CONNECTED)
2079 tl_clear(mdev);
2080 if (mdev->state.disk == D_DISKLESS || mdev->state.disk == D_FAILED)
2081 tl_restart(mdev, fail_frozen_disk_io);
2082 }
2083 drbd_resume_io(mdev);
2084
Philipp Reisnerb411b362009-09-25 16:07:19 -07002085 return 0;
2086}
2087
2088static int drbd_nl_outdate(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2089 struct drbd_nl_cfg_reply *reply)
2090{
2091 reply->ret_code = drbd_request_state(mdev, NS(disk, D_OUTDATED));
2092 return 0;
2093}
2094
2095static int drbd_nl_get_config(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2096 struct drbd_nl_cfg_reply *reply)
2097{
2098 unsigned short *tl;
2099
2100 tl = reply->tag_list;
2101
2102 if (get_ldev(mdev)) {
2103 tl = disk_conf_to_tags(mdev, &mdev->ldev->dc, tl);
2104 put_ldev(mdev);
2105 }
2106
2107 if (get_net_conf(mdev)) {
2108 tl = net_conf_to_tags(mdev, mdev->net_conf, tl);
2109 put_net_conf(mdev);
2110 }
2111 tl = syncer_conf_to_tags(mdev, &mdev->sync_conf, tl);
2112
2113 put_unaligned(TT_END, tl++); /* Close the tag list */
2114
2115 return (int)((char *)tl - (char *)reply->tag_list);
2116}
2117
2118static int drbd_nl_get_state(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2119 struct drbd_nl_cfg_reply *reply)
2120{
2121 unsigned short *tl = reply->tag_list;
2122 union drbd_state s = mdev->state;
2123 unsigned long rs_left;
2124 unsigned int res;
2125
2126 tl = get_state_to_tags(mdev, (struct get_state *)&s, tl);
2127
2128 /* no local ref, no bitmap, no syncer progress. */
2129 if (s.conn >= C_SYNC_SOURCE && s.conn <= C_PAUSED_SYNC_T) {
2130 if (get_ldev(mdev)) {
2131 drbd_get_syncer_progress(mdev, &rs_left, &res);
2132 tl = tl_add_int(tl, T_sync_progress, &res);
2133 put_ldev(mdev);
2134 }
2135 }
2136 put_unaligned(TT_END, tl++); /* Close the tag list */
2137
2138 return (int)((char *)tl - (char *)reply->tag_list);
2139}
2140
2141static int drbd_nl_get_uuids(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2142 struct drbd_nl_cfg_reply *reply)
2143{
2144 unsigned short *tl;
2145
2146 tl = reply->tag_list;
2147
2148 if (get_ldev(mdev)) {
2149 tl = tl_add_blob(tl, T_uuids, mdev->ldev->md.uuid, UI_SIZE*sizeof(u64));
2150 tl = tl_add_int(tl, T_uuids_flags, &mdev->ldev->md.flags);
2151 put_ldev(mdev);
2152 }
2153 put_unaligned(TT_END, tl++); /* Close the tag list */
2154
2155 return (int)((char *)tl - (char *)reply->tag_list);
2156}
2157
2158/**
2159 * drbd_nl_get_timeout_flag() - Used by drbdsetup to find out which timeout value to use
2160 * @mdev: DRBD device.
2161 * @nlp: Netlink/connector packet from drbdsetup
2162 * @reply: Reply packet for drbdsetup
2163 */
2164static int drbd_nl_get_timeout_flag(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2165 struct drbd_nl_cfg_reply *reply)
2166{
2167 unsigned short *tl;
2168 char rv;
2169
2170 tl = reply->tag_list;
2171
2172 rv = mdev->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
2173 test_bit(USE_DEGR_WFC_T, &mdev->flags) ? UT_DEGRADED : UT_DEFAULT;
2174
2175 tl = tl_add_blob(tl, T_use_degraded, &rv, sizeof(rv));
2176 put_unaligned(TT_END, tl++); /* Close the tag list */
2177
2178 return (int)((char *)tl - (char *)reply->tag_list);
2179}
2180
2181static int drbd_nl_start_ov(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2182 struct drbd_nl_cfg_reply *reply)
2183{
2184 /* default to resume from last known position, if possible */
2185 struct start_ov args =
2186 { .start_sector = mdev->ov_start_sector };
2187
2188 if (!start_ov_from_tags(mdev, nlp->tag_list, &args)) {
2189 reply->ret_code = ERR_MANDATORY_TAG;
2190 return 0;
2191 }
Lars Ellenberg873b0d52011-01-21 22:53:48 +01002192
2193 /* If there is still bitmap IO pending, e.g. previous resync or verify
2194 * just being finished, wait for it before requesting a new resync. */
2195 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
2196
Philipp Reisnerb411b362009-09-25 16:07:19 -07002197 /* w_make_ov_request expects position to be aligned */
2198 mdev->ov_start_sector = args.start_sector & ~BM_SECT_PER_BIT;
2199 reply->ret_code = drbd_request_state(mdev,NS(conn,C_VERIFY_S));
2200 return 0;
2201}
2202
2203
2204static int drbd_nl_new_c_uuid(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2205 struct drbd_nl_cfg_reply *reply)
2206{
2207 int retcode = NO_ERROR;
2208 int skip_initial_sync = 0;
2209 int err;
2210
2211 struct new_c_uuid args;
2212
2213 memset(&args, 0, sizeof(struct new_c_uuid));
2214 if (!new_c_uuid_from_tags(mdev, nlp->tag_list, &args)) {
2215 reply->ret_code = ERR_MANDATORY_TAG;
2216 return 0;
2217 }
2218
2219 mutex_lock(&mdev->state_mutex); /* Protects us against serialized state changes. */
2220
2221 if (!get_ldev(mdev)) {
2222 retcode = ERR_NO_DISK;
2223 goto out;
2224 }
2225
2226 /* this is "skip initial sync", assume to be clean */
2227 if (mdev->state.conn == C_CONNECTED && mdev->agreed_pro_version >= 90 &&
2228 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
2229 dev_info(DEV, "Preparing to skip initial sync\n");
2230 skip_initial_sync = 1;
2231 } else if (mdev->state.conn != C_STANDALONE) {
2232 retcode = ERR_CONNECTED;
2233 goto out_dec;
2234 }
2235
2236 drbd_uuid_set(mdev, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
2237 drbd_uuid_new_current(mdev); /* New current, previous to UI_BITMAP */
2238
2239 if (args.clear_bm) {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002240 err = drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
2241 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002242 if (err) {
2243 dev_err(DEV, "Writing bitmap failed with %d\n",err);
2244 retcode = ERR_IO_MD_DISK;
2245 }
2246 if (skip_initial_sync) {
2247 drbd_send_uuids_skip_initial_sync(mdev);
2248 _drbd_uuid_set(mdev, UI_BITMAP, 0);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002249 drbd_print_uuids(mdev, "cleared bitmap UUID");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002250 spin_lock_irq(&mdev->req_lock);
2251 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
2252 CS_VERBOSE, NULL);
2253 spin_unlock_irq(&mdev->req_lock);
2254 }
2255 }
2256
2257 drbd_md_sync(mdev);
2258out_dec:
2259 put_ldev(mdev);
2260out:
2261 mutex_unlock(&mdev->state_mutex);
2262
2263 reply->ret_code = retcode;
2264 return 0;
2265}
2266
Philipp Reisnerb411b362009-09-25 16:07:19 -07002267struct cn_handler_struct {
2268 int (*function)(struct drbd_conf *,
2269 struct drbd_nl_cfg_req *,
2270 struct drbd_nl_cfg_reply *);
2271 int reply_body_size;
2272};
2273
2274static struct cn_handler_struct cnd_table[] = {
2275 [ P_primary ] = { &drbd_nl_primary, 0 },
2276 [ P_secondary ] = { &drbd_nl_secondary, 0 },
2277 [ P_disk_conf ] = { &drbd_nl_disk_conf, 0 },
2278 [ P_detach ] = { &drbd_nl_detach, 0 },
2279 [ P_net_conf ] = { &drbd_nl_net_conf, 0 },
2280 [ P_disconnect ] = { &drbd_nl_disconnect, 0 },
2281 [ P_resize ] = { &drbd_nl_resize, 0 },
2282 [ P_syncer_conf ] = { &drbd_nl_syncer_conf, 0 },
2283 [ P_invalidate ] = { &drbd_nl_invalidate, 0 },
2284 [ P_invalidate_peer ] = { &drbd_nl_invalidate_peer, 0 },
2285 [ P_pause_sync ] = { &drbd_nl_pause_sync, 0 },
2286 [ P_resume_sync ] = { &drbd_nl_resume_sync, 0 },
2287 [ P_suspend_io ] = { &drbd_nl_suspend_io, 0 },
2288 [ P_resume_io ] = { &drbd_nl_resume_io, 0 },
2289 [ P_outdate ] = { &drbd_nl_outdate, 0 },
2290 [ P_get_config ] = { &drbd_nl_get_config,
2291 sizeof(struct syncer_conf_tag_len_struct) +
2292 sizeof(struct disk_conf_tag_len_struct) +
2293 sizeof(struct net_conf_tag_len_struct) },
2294 [ P_get_state ] = { &drbd_nl_get_state,
2295 sizeof(struct get_state_tag_len_struct) +
2296 sizeof(struct sync_progress_tag_len_struct) },
2297 [ P_get_uuids ] = { &drbd_nl_get_uuids,
2298 sizeof(struct get_uuids_tag_len_struct) },
2299 [ P_get_timeout_flag ] = { &drbd_nl_get_timeout_flag,
2300 sizeof(struct get_timeout_flag_tag_len_struct)},
2301 [ P_start_ov ] = { &drbd_nl_start_ov, 0 },
2302 [ P_new_c_uuid ] = { &drbd_nl_new_c_uuid, 0 },
2303};
2304
Philipp Reisner9f5180e2009-10-06 09:30:14 +02002305static void drbd_connector_callback(struct cn_msg *req, struct netlink_skb_parms *nsp)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002306{
2307 struct drbd_nl_cfg_req *nlp = (struct drbd_nl_cfg_req *)req->data;
2308 struct cn_handler_struct *cm;
2309 struct cn_msg *cn_reply;
2310 struct drbd_nl_cfg_reply *reply;
2311 struct drbd_conf *mdev;
2312 int retcode, rr;
2313 int reply_size = sizeof(struct cn_msg)
2314 + sizeof(struct drbd_nl_cfg_reply)
2315 + sizeof(short int);
2316
2317 if (!try_module_get(THIS_MODULE)) {
2318 printk(KERN_ERR "drbd: try_module_get() failed!\n");
2319 return;
2320 }
2321
Patrick McHardy01a16b22011-03-03 13:32:07 -08002322 if (!cap_raised(current_cap(), CAP_SYS_ADMIN)) {
Philipp Reisner9f5180e2009-10-06 09:30:14 +02002323 retcode = ERR_PERM;
2324 goto fail;
2325 }
2326
Lars Ellenbergef50a3e2010-09-01 14:39:30 +02002327 mdev = ensure_mdev(nlp->drbd_minor,
2328 (nlp->flags & DRBD_NL_CREATE_DEVICE));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002329 if (!mdev) {
2330 retcode = ERR_MINOR_INVALID;
2331 goto fail;
2332 }
2333
Lars Ellenberg42ff2692010-11-24 10:11:14 +01002334 if (nlp->packet_type >= P_nl_after_last_packet ||
2335 nlp->packet_type == P_return_code_only) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002336 retcode = ERR_PACKET_NR;
2337 goto fail;
2338 }
2339
2340 cm = cnd_table + nlp->packet_type;
2341
2342 /* This may happen if packet number is 0: */
2343 if (cm->function == NULL) {
2344 retcode = ERR_PACKET_NR;
2345 goto fail;
2346 }
2347
2348 reply_size += cm->reply_body_size;
2349
2350 /* allocation not in the IO path, cqueue thread context */
Lars Ellenberg3e3a7762010-11-24 10:41:45 +01002351 cn_reply = kzalloc(reply_size, GFP_KERNEL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002352 if (!cn_reply) {
2353 retcode = ERR_NOMEM;
2354 goto fail;
2355 }
2356 reply = (struct drbd_nl_cfg_reply *) cn_reply->data;
2357
2358 reply->packet_type =
Lars Ellenberg42ff2692010-11-24 10:11:14 +01002359 cm->reply_body_size ? nlp->packet_type : P_return_code_only;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002360 reply->minor = nlp->drbd_minor;
2361 reply->ret_code = NO_ERROR; /* Might by modified by cm->function. */
2362 /* reply->tag_list; might be modified by cm->function. */
2363
2364 rr = cm->function(mdev, nlp, reply);
2365
2366 cn_reply->id = req->id;
2367 cn_reply->seq = req->seq;
2368 cn_reply->ack = req->ack + 1;
2369 cn_reply->len = sizeof(struct drbd_nl_cfg_reply) + rr;
2370 cn_reply->flags = 0;
2371
Philipp Reisnerb411b362009-09-25 16:07:19 -07002372 rr = cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_KERNEL);
2373 if (rr && rr != -ESRCH)
2374 printk(KERN_INFO "drbd: cn_netlink_send()=%d\n", rr);
2375
2376 kfree(cn_reply);
2377 module_put(THIS_MODULE);
2378 return;
2379 fail:
2380 drbd_nl_send_reply(req, retcode);
2381 module_put(THIS_MODULE);
2382}
2383
2384static atomic_t drbd_nl_seq = ATOMIC_INIT(2); /* two. */
2385
2386static unsigned short *
2387__tl_add_blob(unsigned short *tl, enum drbd_tags tag, const void *data,
2388 unsigned short len, int nul_terminated)
2389{
2390 unsigned short l = tag_descriptions[tag_number(tag)].max_len;
2391 len = (len < l) ? len : l;
2392 put_unaligned(tag, tl++);
2393 put_unaligned(len, tl++);
2394 memcpy(tl, data, len);
2395 tl = (unsigned short*)((char*)tl + len);
2396 if (nul_terminated)
2397 *((char*)tl - 1) = 0;
2398 return tl;
2399}
2400
2401static unsigned short *
2402tl_add_blob(unsigned short *tl, enum drbd_tags tag, const void *data, int len)
2403{
2404 return __tl_add_blob(tl, tag, data, len, 0);
2405}
2406
2407static unsigned short *
2408tl_add_str(unsigned short *tl, enum drbd_tags tag, const char *str)
2409{
2410 return __tl_add_blob(tl, tag, str, strlen(str)+1, 0);
2411}
2412
2413static unsigned short *
2414tl_add_int(unsigned short *tl, enum drbd_tags tag, const void *val)
2415{
2416 put_unaligned(tag, tl++);
2417 switch(tag_type(tag)) {
2418 case TT_INTEGER:
2419 put_unaligned(sizeof(int), tl++);
2420 put_unaligned(*(int *)val, (int *)tl);
2421 tl = (unsigned short*)((char*)tl+sizeof(int));
2422 break;
2423 case TT_INT64:
2424 put_unaligned(sizeof(u64), tl++);
2425 put_unaligned(*(u64 *)val, (u64 *)tl);
2426 tl = (unsigned short*)((char*)tl+sizeof(u64));
2427 break;
2428 default:
2429 /* someone did something stupid. */
2430 ;
2431 }
2432 return tl;
2433}
2434
2435void drbd_bcast_state(struct drbd_conf *mdev, union drbd_state state)
2436{
2437 char buffer[sizeof(struct cn_msg)+
2438 sizeof(struct drbd_nl_cfg_reply)+
2439 sizeof(struct get_state_tag_len_struct)+
2440 sizeof(short int)];
2441 struct cn_msg *cn_reply = (struct cn_msg *) buffer;
2442 struct drbd_nl_cfg_reply *reply =
2443 (struct drbd_nl_cfg_reply *)cn_reply->data;
2444 unsigned short *tl = reply->tag_list;
2445
2446 /* dev_warn(DEV, "drbd_bcast_state() got called\n"); */
2447
2448 tl = get_state_to_tags(mdev, (struct get_state *)&state, tl);
2449
2450 put_unaligned(TT_END, tl++); /* Close the tag list */
2451
2452 cn_reply->id.idx = CN_IDX_DRBD;
2453 cn_reply->id.val = CN_VAL_DRBD;
2454
2455 cn_reply->seq = atomic_add_return(1, &drbd_nl_seq);
2456 cn_reply->ack = 0; /* not used here. */
2457 cn_reply->len = sizeof(struct drbd_nl_cfg_reply) +
2458 (int)((char *)tl - (char *)reply->tag_list);
2459 cn_reply->flags = 0;
2460
2461 reply->packet_type = P_get_state;
2462 reply->minor = mdev_to_minor(mdev);
2463 reply->ret_code = NO_ERROR;
2464
Philipp Reisnerb411b362009-09-25 16:07:19 -07002465 cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO);
2466}
2467
2468void drbd_bcast_ev_helper(struct drbd_conf *mdev, char *helper_name)
2469{
2470 char buffer[sizeof(struct cn_msg)+
2471 sizeof(struct drbd_nl_cfg_reply)+
2472 sizeof(struct call_helper_tag_len_struct)+
2473 sizeof(short int)];
2474 struct cn_msg *cn_reply = (struct cn_msg *) buffer;
2475 struct drbd_nl_cfg_reply *reply =
2476 (struct drbd_nl_cfg_reply *)cn_reply->data;
2477 unsigned short *tl = reply->tag_list;
2478
2479 /* dev_warn(DEV, "drbd_bcast_state() got called\n"); */
2480
2481 tl = tl_add_str(tl, T_helper, helper_name);
2482 put_unaligned(TT_END, tl++); /* Close the tag list */
2483
2484 cn_reply->id.idx = CN_IDX_DRBD;
2485 cn_reply->id.val = CN_VAL_DRBD;
2486
2487 cn_reply->seq = atomic_add_return(1, &drbd_nl_seq);
2488 cn_reply->ack = 0; /* not used here. */
2489 cn_reply->len = sizeof(struct drbd_nl_cfg_reply) +
2490 (int)((char *)tl - (char *)reply->tag_list);
2491 cn_reply->flags = 0;
2492
2493 reply->packet_type = P_call_helper;
2494 reply->minor = mdev_to_minor(mdev);
2495 reply->ret_code = NO_ERROR;
2496
Philipp Reisnerb411b362009-09-25 16:07:19 -07002497 cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO);
2498}
2499
2500void drbd_bcast_ee(struct drbd_conf *mdev,
2501 const char *reason, const int dgs,
2502 const char* seen_hash, const char* calc_hash,
2503 const struct drbd_epoch_entry* e)
2504{
2505 struct cn_msg *cn_reply;
2506 struct drbd_nl_cfg_reply *reply;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002507 unsigned short *tl;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002508 struct page *page;
2509 unsigned len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002510
2511 if (!e)
2512 return;
2513 if (!reason || !reason[0])
2514 return;
2515
2516 /* apparently we have to memcpy twice, first to prepare the data for the
2517 * struct cn_msg, then within cn_netlink_send from the cn_msg to the
2518 * netlink skb. */
2519 /* receiver thread context, which is not in the writeout path (of this node),
2520 * but may be in the writeout path of the _other_ node.
2521 * GFP_NOIO to avoid potential "distributed deadlock". */
Lars Ellenberg3e3a7762010-11-24 10:41:45 +01002522 cn_reply = kzalloc(
Philipp Reisnerb411b362009-09-25 16:07:19 -07002523 sizeof(struct cn_msg)+
2524 sizeof(struct drbd_nl_cfg_reply)+
2525 sizeof(struct dump_ee_tag_len_struct)+
2526 sizeof(short int),
2527 GFP_NOIO);
2528
2529 if (!cn_reply) {
2530 dev_err(DEV, "could not kmalloc buffer for drbd_bcast_ee, sector %llu, size %u\n",
2531 (unsigned long long)e->sector, e->size);
2532 return;
2533 }
2534
2535 reply = (struct drbd_nl_cfg_reply*)cn_reply->data;
2536 tl = reply->tag_list;
2537
2538 tl = tl_add_str(tl, T_dump_ee_reason, reason);
2539 tl = tl_add_blob(tl, T_seen_digest, seen_hash, dgs);
2540 tl = tl_add_blob(tl, T_calc_digest, calc_hash, dgs);
2541 tl = tl_add_int(tl, T_ee_sector, &e->sector);
2542 tl = tl_add_int(tl, T_ee_block_id, &e->block_id);
2543
Lars Ellenberg3129b1b2010-11-11 10:47:05 +01002544 /* dump the first 32k */
2545 len = min_t(unsigned, e->size, 32 << 10);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002546 put_unaligned(T_ee_data, tl++);
Lars Ellenberg3129b1b2010-11-11 10:47:05 +01002547 put_unaligned(len, tl++);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002548
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002549 page = e->pages;
2550 page_chain_for_each(page) {
Cong Wangcfd80052011-11-25 23:14:18 +08002551 void *d = kmap_atomic(page);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002552 unsigned l = min_t(unsigned, len, PAGE_SIZE);
2553 memcpy(tl, d, l);
Cong Wangcfd80052011-11-25 23:14:18 +08002554 kunmap_atomic(d);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002555 tl = (unsigned short*)((char*)tl + l);
2556 len -= l;
Lars Ellenberg3129b1b2010-11-11 10:47:05 +01002557 if (len == 0)
2558 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002559 }
2560 put_unaligned(TT_END, tl++); /* Close the tag list */
2561
2562 cn_reply->id.idx = CN_IDX_DRBD;
2563 cn_reply->id.val = CN_VAL_DRBD;
2564
2565 cn_reply->seq = atomic_add_return(1,&drbd_nl_seq);
2566 cn_reply->ack = 0; // not used here.
2567 cn_reply->len = sizeof(struct drbd_nl_cfg_reply) +
2568 (int)((char*)tl - (char*)reply->tag_list);
2569 cn_reply->flags = 0;
2570
2571 reply->packet_type = P_dump_ee;
2572 reply->minor = mdev_to_minor(mdev);
2573 reply->ret_code = NO_ERROR;
2574
Philipp Reisnerb411b362009-09-25 16:07:19 -07002575 cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO);
2576 kfree(cn_reply);
2577}
2578
2579void drbd_bcast_sync_progress(struct drbd_conf *mdev)
2580{
2581 char buffer[sizeof(struct cn_msg)+
2582 sizeof(struct drbd_nl_cfg_reply)+
2583 sizeof(struct sync_progress_tag_len_struct)+
2584 sizeof(short int)];
2585 struct cn_msg *cn_reply = (struct cn_msg *) buffer;
2586 struct drbd_nl_cfg_reply *reply =
2587 (struct drbd_nl_cfg_reply *)cn_reply->data;
2588 unsigned short *tl = reply->tag_list;
2589 unsigned long rs_left;
2590 unsigned int res;
2591
2592 /* no local ref, no bitmap, no syncer progress, no broadcast. */
2593 if (!get_ldev(mdev))
2594 return;
2595 drbd_get_syncer_progress(mdev, &rs_left, &res);
2596 put_ldev(mdev);
2597
2598 tl = tl_add_int(tl, T_sync_progress, &res);
2599 put_unaligned(TT_END, tl++); /* Close the tag list */
2600
2601 cn_reply->id.idx = CN_IDX_DRBD;
2602 cn_reply->id.val = CN_VAL_DRBD;
2603
2604 cn_reply->seq = atomic_add_return(1, &drbd_nl_seq);
2605 cn_reply->ack = 0; /* not used here. */
2606 cn_reply->len = sizeof(struct drbd_nl_cfg_reply) +
2607 (int)((char *)tl - (char *)reply->tag_list);
2608 cn_reply->flags = 0;
2609
2610 reply->packet_type = P_sync_progress;
2611 reply->minor = mdev_to_minor(mdev);
2612 reply->ret_code = NO_ERROR;
2613
Philipp Reisnerb411b362009-09-25 16:07:19 -07002614 cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO);
2615}
2616
2617int __init drbd_nl_init(void)
2618{
2619 static struct cb_id cn_id_drbd;
2620 int err, try=10;
2621
2622 cn_id_drbd.val = CN_VAL_DRBD;
2623 do {
2624 cn_id_drbd.idx = cn_idx;
2625 err = cn_add_callback(&cn_id_drbd, "cn_drbd", &drbd_connector_callback);
2626 if (!err)
2627 break;
2628 cn_idx = (cn_idx + CN_IDX_STEP);
2629 } while (try--);
2630
2631 if (err) {
2632 printk(KERN_ERR "drbd: cn_drbd failed to register\n");
2633 return err;
2634 }
2635
2636 return 0;
2637}
2638
2639void drbd_nl_cleanup(void)
2640{
2641 static struct cb_id cn_id_drbd;
2642
2643 cn_id_drbd.idx = cn_idx;
2644 cn_id_drbd.val = CN_VAL_DRBD;
2645
2646 cn_del_callback(&cn_id_drbd);
2647}
2648
2649void drbd_nl_send_reply(struct cn_msg *req, int ret_code)
2650{
2651 char buffer[sizeof(struct cn_msg)+sizeof(struct drbd_nl_cfg_reply)];
2652 struct cn_msg *cn_reply = (struct cn_msg *) buffer;
2653 struct drbd_nl_cfg_reply *reply =
2654 (struct drbd_nl_cfg_reply *)cn_reply->data;
2655 int rr;
2656
Lars Ellenberg3e3a7762010-11-24 10:41:45 +01002657 memset(buffer, 0, sizeof(buffer));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002658 cn_reply->id = req->id;
2659
2660 cn_reply->seq = req->seq;
2661 cn_reply->ack = req->ack + 1;
2662 cn_reply->len = sizeof(struct drbd_nl_cfg_reply);
2663 cn_reply->flags = 0;
2664
Lars Ellenberg42ff2692010-11-24 10:11:14 +01002665 reply->packet_type = P_return_code_only;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002666 reply->minor = ((struct drbd_nl_cfg_req *)req->data)->drbd_minor;
2667 reply->ret_code = ret_code;
2668
Philipp Reisnerb411b362009-09-25 16:07:19 -07002669 rr = cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO);
2670 if (rr && rr != -ESRCH)
2671 printk(KERN_INFO "drbd: cn_netlink_send()=%d\n", rr);
2672}
2673