drbd: on attach, enforce clean meta data
[deliverable/linux.git] / drivers / block / drbd / drbd_nl.c
CommitLineData
b411b363
PR
1/*
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
b411b363
PR
26#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>
b411b363
PR
32#include <linux/blkpg.h>
33#include <linux/cpumask.h>
34#include "drbd_int.h"
265be2d0 35#include "drbd_req.h"
b411b363
PR
36#include "drbd_wrappers.h"
37#include <asm/unaligned.h>
b411b363 38#include <linux/drbd_limits.h>
87f7be4c 39#include <linux/kthread.h>
b411b363 40
3b98c0c2
LE
41#include <net/genetlink.h>
42
43/* .doit */
44// int drbd_adm_create_resource(struct sk_buff *skb, struct genl_info *info);
45// int drbd_adm_delete_resource(struct sk_buff *skb, struct genl_info *info);
46
47int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info);
48int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info);
49
789c1b62
AG
50int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info);
51int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info);
85f75dd7 52int drbd_adm_down(struct sk_buff *skb, struct genl_info *info);
3b98c0c2
LE
53
54int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info);
55int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info);
f399002e 56int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info);
3b98c0c2
LE
57int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info);
58int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info);
f399002e 59int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info);
3b98c0c2
LE
60int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info);
61int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info);
62int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info);
63int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info);
64int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info);
65int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info);
66int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info);
67int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info);
68int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info);
69int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info);
70int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info);
f399002e 71int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info);
3b98c0c2
LE
72int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info);
73int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info);
74/* .dumpit */
75int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb);
76
77#include <linux/drbd_genl_api.h>
01b39b50 78#include "drbd_nla.h"
3b98c0c2
LE
79#include <linux/genl_magic_func.h>
80
81/* used blkdev_get_by_path, to claim our meta data device(s) */
b411b363
PR
82static char *drbd_m_holder = "Hands off! this is DRBD's meta data device.";
83
3b98c0c2
LE
84/* Configuration is strictly serialized, because generic netlink message
85 * processing is strictly serialized by the genl_lock().
86 * Which means we can use one static global drbd_config_context struct.
87 */
88static struct drbd_config_context {
89 /* assigned from drbd_genlmsghdr */
90 unsigned int minor;
91 /* assigned from request attributes, if present */
92 unsigned int volume;
93#define VOLUME_UNSPECIFIED (-1U)
94 /* pointer into the request skb,
95 * limited lifetime! */
7c3063cc 96 char *resource_name;
089c075d
AG
97 struct nlattr *my_addr;
98 struct nlattr *peer_addr;
3b98c0c2
LE
99
100 /* reply buffer */
101 struct sk_buff *reply_skb;
102 /* pointer into reply buffer */
103 struct drbd_genlmsghdr *reply_dh;
104 /* resolved from attributes, if possible */
105 struct drbd_conf *mdev;
106 struct drbd_tconn *tconn;
107} adm_ctx;
108
109static void drbd_adm_send_reply(struct sk_buff *skb, struct genl_info *info)
110{
111 genlmsg_end(skb, genlmsg_data(nlmsg_data(nlmsg_hdr(skb))));
112 if (genlmsg_reply(skb, info))
113 printk(KERN_ERR "drbd: error sending genl reply\n");
b411b363 114}
3b98c0c2
LE
115
116/* Used on a fresh "drbd_adm_prepare"d reply_skb, this cannot fail: The only
117 * reason it could fail was no space in skb, and there are 4k available. */
8432b314 118int drbd_msg_put_info(const char *info)
3b98c0c2
LE
119{
120 struct sk_buff *skb = adm_ctx.reply_skb;
121 struct nlattr *nla;
122 int err = -EMSGSIZE;
123
124 if (!info || !info[0])
125 return 0;
126
127 nla = nla_nest_start(skb, DRBD_NLA_CFG_REPLY);
128 if (!nla)
129 return err;
130
131 err = nla_put_string(skb, T_info_text, info);
132 if (err) {
133 nla_nest_cancel(skb, nla);
134 return err;
135 } else
136 nla_nest_end(skb, nla);
137 return 0;
b411b363
PR
138}
139
3b98c0c2
LE
140/* This would be a good candidate for a "pre_doit" hook,
141 * and per-family private info->pointers.
142 * But we need to stay compatible with older kernels.
143 * If it returns successfully, adm_ctx members are valid.
144 */
145#define DRBD_ADM_NEED_MINOR 1
44e52cfa 146#define DRBD_ADM_NEED_RESOURCE 2
089c075d 147#define DRBD_ADM_NEED_CONNECTION 4
3b98c0c2
LE
148static int drbd_adm_prepare(struct sk_buff *skb, struct genl_info *info,
149 unsigned flags)
150{
151 struct drbd_genlmsghdr *d_in = info->userhdr;
152 const u8 cmd = info->genlhdr->cmd;
153 int err;
154
155 memset(&adm_ctx, 0, sizeof(adm_ctx));
156
157 /* genl_rcv_msg only checks for CAP_NET_ADMIN on "GENL_ADMIN_PERM" :( */
158 if (cmd != DRBD_ADM_GET_STATUS
159 && security_netlink_recv(skb, CAP_SYS_ADMIN))
160 return -EPERM;
161
162 adm_ctx.reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1e2a2551
AG
163 if (!adm_ctx.reply_skb) {
164 err = -ENOMEM;
3b98c0c2 165 goto fail;
1e2a2551 166 }
3b98c0c2
LE
167
168 adm_ctx.reply_dh = genlmsg_put_reply(adm_ctx.reply_skb,
169 info, &drbd_genl_family, 0, cmd);
170 /* put of a few bytes into a fresh skb of >= 4k will always succeed.
171 * but anyways */
1e2a2551
AG
172 if (!adm_ctx.reply_dh) {
173 err = -ENOMEM;
3b98c0c2 174 goto fail;
1e2a2551 175 }
3b98c0c2
LE
176
177 adm_ctx.reply_dh->minor = d_in->minor;
178 adm_ctx.reply_dh->ret_code = NO_ERROR;
179
089c075d 180 adm_ctx.volume = VOLUME_UNSPECIFIED;
3b98c0c2
LE
181 if (info->attrs[DRBD_NLA_CFG_CONTEXT]) {
182 struct nlattr *nla;
183 /* parse and validate only */
f399002e 184 err = drbd_cfg_context_from_attrs(NULL, info);
3b98c0c2
LE
185 if (err)
186 goto fail;
187
188 /* It was present, and valid,
189 * copy it over to the reply skb. */
190 err = nla_put_nohdr(adm_ctx.reply_skb,
191 info->attrs[DRBD_NLA_CFG_CONTEXT]->nla_len,
192 info->attrs[DRBD_NLA_CFG_CONTEXT]);
193 if (err)
194 goto fail;
195
196 /* and assign stuff to the global adm_ctx */
197 nla = nested_attr_tb[__nla_type(T_ctx_volume)];
089c075d
AG
198 if (nla)
199 adm_ctx.volume = nla_get_u32(nla);
7c3063cc 200 nla = nested_attr_tb[__nla_type(T_ctx_resource_name)];
3b98c0c2 201 if (nla)
7c3063cc 202 adm_ctx.resource_name = nla_data(nla);
089c075d
AG
203 adm_ctx.my_addr = nested_attr_tb[__nla_type(T_ctx_my_addr)];
204 adm_ctx.peer_addr = nested_attr_tb[__nla_type(T_ctx_peer_addr)];
205 if ((adm_ctx.my_addr &&
206 nla_len(adm_ctx.my_addr) > sizeof(adm_ctx.tconn->my_addr)) ||
207 (adm_ctx.peer_addr &&
208 nla_len(adm_ctx.peer_addr) > sizeof(adm_ctx.tconn->peer_addr))) {
209 err = -EINVAL;
210 goto fail;
211 }
212 }
3b98c0c2
LE
213
214 adm_ctx.minor = d_in->minor;
215 adm_ctx.mdev = minor_to_mdev(d_in->minor);
7c3063cc 216 adm_ctx.tconn = conn_get_by_name(adm_ctx.resource_name);
3b98c0c2
LE
217
218 if (!adm_ctx.mdev && (flags & DRBD_ADM_NEED_MINOR)) {
219 drbd_msg_put_info("unknown minor");
220 return ERR_MINOR_INVALID;
221 }
44e52cfa
AG
222 if (!adm_ctx.tconn && (flags & DRBD_ADM_NEED_RESOURCE)) {
223 drbd_msg_put_info("unknown resource");
3b98c0c2
LE
224 return ERR_INVALID_REQUEST;
225 }
226
089c075d
AG
227 if (flags & DRBD_ADM_NEED_CONNECTION) {
228 if (adm_ctx.tconn && !(flags & DRBD_ADM_NEED_RESOURCE)) {
229 drbd_msg_put_info("no resource name expected");
230 return ERR_INVALID_REQUEST;
231 }
232 if (adm_ctx.mdev) {
233 drbd_msg_put_info("no minor number expected");
234 return ERR_INVALID_REQUEST;
235 }
236 if (adm_ctx.my_addr && adm_ctx.peer_addr)
237 adm_ctx.tconn = conn_get_by_addrs(nla_data(adm_ctx.my_addr),
238 nla_len(adm_ctx.my_addr),
239 nla_data(adm_ctx.peer_addr),
240 nla_len(adm_ctx.peer_addr));
241 if (!adm_ctx.tconn) {
242 drbd_msg_put_info("unknown connection");
243 return ERR_INVALID_REQUEST;
244 }
245 }
246
3b98c0c2 247 /* some more paranoia, if the request was over-determined */
527f4b24
LE
248 if (adm_ctx.mdev && adm_ctx.tconn &&
249 adm_ctx.mdev->tconn != adm_ctx.tconn) {
44e52cfa 250 pr_warning("request: minor=%u, resource=%s; but that minor belongs to connection %s\n",
7c3063cc
AG
251 adm_ctx.minor, adm_ctx.resource_name,
252 adm_ctx.mdev->tconn->name);
44e52cfa 253 drbd_msg_put_info("minor exists in different resource");
527f4b24
LE
254 return ERR_INVALID_REQUEST;
255 }
3b98c0c2
LE
256 if (adm_ctx.mdev &&
257 adm_ctx.volume != VOLUME_UNSPECIFIED &&
258 adm_ctx.volume != adm_ctx.mdev->vnr) {
259 pr_warning("request: minor=%u, volume=%u; but that minor is volume %u in %s\n",
260 adm_ctx.minor, adm_ctx.volume,
261 adm_ctx.mdev->vnr, adm_ctx.mdev->tconn->name);
527f4b24 262 drbd_msg_put_info("minor exists as different volume");
3b98c0c2
LE
263 return ERR_INVALID_REQUEST;
264 }
0ace9dfa 265
3b98c0c2
LE
266 return NO_ERROR;
267
268fail:
269 nlmsg_free(adm_ctx.reply_skb);
270 adm_ctx.reply_skb = NULL;
1e2a2551 271 return err;
3b98c0c2
LE
272}
273
274static int drbd_adm_finish(struct genl_info *info, int retcode)
275{
0ace9dfa
PR
276 if (adm_ctx.tconn) {
277 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
278 adm_ctx.tconn = NULL;
279 }
280
3b98c0c2
LE
281 if (!adm_ctx.reply_skb)
282 return -ENOMEM;
283
284 adm_ctx.reply_dh->ret_code = retcode;
3b98c0c2
LE
285 drbd_adm_send_reply(adm_ctx.reply_skb, info);
286 return 0;
287}
b411b363 288
6b75dced 289static void setup_khelper_env(struct drbd_tconn *tconn, char **envp)
b411b363 290{
6b75dced 291 char *afs;
b411b363 292
089c075d
AG
293 /* FIXME: A future version will not allow this case. */
294 if (tconn->my_addr_len == 0 || tconn->peer_addr_len == 0)
295 return;
296
297 switch (((struct sockaddr *)&tconn->peer_addr)->sa_family) {
298 case AF_INET6:
299 afs = "ipv6";
300 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
301 &((struct sockaddr_in6 *)&tconn->peer_addr)->sin6_addr);
302 break;
303 case AF_INET:
304 afs = "ipv4";
305 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
306 &((struct sockaddr_in *)&tconn->peer_addr)->sin_addr);
307 break;
308 default:
309 afs = "ssocks";
310 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
311 &((struct sockaddr_in *)&tconn->peer_addr)->sin_addr);
b411b363 312 }
089c075d 313 snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
6b75dced
PR
314}
315
316int drbd_khelper(struct drbd_conf *mdev, char *cmd)
317{
318 char *envp[] = { "HOME=/",
319 "TERM=linux",
320 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
321 (char[20]) { }, /* address family */
322 (char[60]) { }, /* address */
323 NULL };
324 char mb[12];
325 char *argv[] = {usermode_helper, cmd, mb, NULL };
326 struct sib_info sib;
327 int ret;
328
329 snprintf(mb, 12, "minor-%d", mdev_to_minor(mdev));
330 setup_khelper_env(mdev->tconn, envp);
b411b363 331
1090c056
LE
332 /* The helper may take some time.
333 * write out any unsynced meta data changes now */
334 drbd_md_sync(mdev);
335
b411b363 336 dev_info(DEV, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
3b98c0c2
LE
337 sib.sib_reason = SIB_HELPER_PRE;
338 sib.helper_name = cmd;
339 drbd_bcast_event(mdev, &sib);
b411b363
PR
340 ret = call_usermodehelper(usermode_helper, argv, envp, 1);
341 if (ret)
342 dev_warn(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
343 usermode_helper, cmd, mb,
344 (ret >> 8) & 0xff, ret);
345 else
346 dev_info(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
347 usermode_helper, cmd, mb,
348 (ret >> 8) & 0xff, ret);
3b98c0c2
LE
349 sib.sib_reason = SIB_HELPER_POST;
350 sib.helper_exit_code = ret;
351 drbd_bcast_event(mdev, &sib);
b411b363
PR
352
353 if (ret < 0) /* Ignore any ERRNOs we got. */
354 ret = 0;
355
356 return ret;
357}
358
6b75dced
PR
359static void conn_md_sync(struct drbd_tconn *tconn)
360{
361 struct drbd_conf *mdev;
e90285e0 362 int vnr;
6b75dced 363
c141ebda
PR
364 rcu_read_lock();
365 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
366 kref_get(&mdev->kref);
367 rcu_read_unlock();
6b75dced 368 drbd_md_sync(mdev);
c141ebda
PR
369 kref_put(&mdev->kref, &drbd_minor_destroy);
370 rcu_read_lock();
371 }
372 rcu_read_unlock();
6b75dced
PR
373}
374
375int conn_khelper(struct drbd_tconn *tconn, char *cmd)
376{
377 char *envp[] = { "HOME=/",
378 "TERM=linux",
379 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
380 (char[20]) { }, /* address family */
381 (char[60]) { }, /* address */
382 NULL };
383 char *argv[] = {usermode_helper, cmd, tconn->name, NULL };
384 int ret;
385
386 setup_khelper_env(tconn, envp);
387 conn_md_sync(tconn);
388
389 conn_info(tconn, "helper command: %s %s %s\n", usermode_helper, cmd, tconn->name);
390 /* TODO: conn_bcast_event() ?? */
391
392 ret = call_usermodehelper(usermode_helper, argv, envp, 1);
393 if (ret)
394 conn_warn(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
395 usermode_helper, cmd, tconn->name,
396 (ret >> 8) & 0xff, ret);
397 else
398 conn_info(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
399 usermode_helper, cmd, tconn->name,
400 (ret >> 8) & 0xff, ret);
401 /* TODO: conn_bcast_event() ?? */
402
403 if (ret < 0) /* Ignore any ERRNOs we got. */
404 ret = 0;
405
406 return ret;
407}
408
cb703454 409static enum drbd_fencing_p highest_fencing_policy(struct drbd_tconn *tconn)
b411b363 410{
cb703454
PR
411 enum drbd_fencing_p fp = FP_NOT_AVAIL;
412 struct drbd_conf *mdev;
413 int vnr;
414
695d08fa 415 rcu_read_lock();
cb703454
PR
416 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
417 if (get_ldev_if_state(mdev, D_CONSISTENT)) {
daeda1cc
PR
418 fp = max_t(enum drbd_fencing_p, fp,
419 rcu_dereference(mdev->ldev->disk_conf)->fencing);
cb703454
PR
420 put_ldev(mdev);
421 }
422 }
695d08fa 423 rcu_read_unlock();
cb703454
PR
424
425 return fp;
426}
427
428bool conn_try_outdate_peer(struct drbd_tconn *tconn)
429{
430 union drbd_state mask = { };
431 union drbd_state val = { };
432 enum drbd_fencing_p fp;
b411b363
PR
433 char *ex_to_string;
434 int r;
b411b363 435
cb703454
PR
436 if (tconn->cstate >= C_WF_REPORT_PARAMS) {
437 conn_err(tconn, "Expected cstate < C_WF_REPORT_PARAMS\n");
438 return false;
439 }
b411b363 440
cb703454
PR
441 fp = highest_fencing_policy(tconn);
442 switch (fp) {
443 case FP_NOT_AVAIL:
444 conn_warn(tconn, "Not fencing peer, I'm not even Consistent myself.\n");
fb22c402 445 goto out;
cb703454
PR
446 case FP_DONT_CARE:
447 return true;
448 default: ;
b411b363
PR
449 }
450
cb703454 451 r = conn_khelper(tconn, "fence-peer");
b411b363
PR
452
453 switch ((r>>8) & 0xff) {
454 case 3: /* peer is inconsistent */
455 ex_to_string = "peer is inconsistent or worse";
cb703454
PR
456 mask.pdsk = D_MASK;
457 val.pdsk = D_INCONSISTENT;
b411b363
PR
458 break;
459 case 4: /* peer got outdated, or was already outdated */
460 ex_to_string = "peer was fenced";
cb703454
PR
461 mask.pdsk = D_MASK;
462 val.pdsk = D_OUTDATED;
b411b363
PR
463 break;
464 case 5: /* peer was down */
cb703454 465 if (conn_highest_disk(tconn) == D_UP_TO_DATE) {
b411b363
PR
466 /* we will(have) create(d) a new UUID anyways... */
467 ex_to_string = "peer is unreachable, assumed to be dead";
cb703454
PR
468 mask.pdsk = D_MASK;
469 val.pdsk = D_OUTDATED;
b411b363
PR
470 } else {
471 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
b411b363
PR
472 }
473 break;
474 case 6: /* Peer is primary, voluntarily outdate myself.
475 * This is useful when an unconnected R_SECONDARY is asked to
476 * become R_PRIMARY, but finds the other peer being active. */
477 ex_to_string = "peer is active";
cb703454
PR
478 conn_warn(tconn, "Peer is primary, outdating myself.\n");
479 mask.disk = D_MASK;
480 val.disk = D_OUTDATED;
b411b363
PR
481 break;
482 case 7:
483 if (fp != FP_STONITH)
cb703454 484 conn_err(tconn, "fence-peer() = 7 && fencing != Stonith !!!\n");
b411b363 485 ex_to_string = "peer was stonithed";
cb703454
PR
486 mask.pdsk = D_MASK;
487 val.pdsk = D_OUTDATED;
b411b363
PR
488 break;
489 default:
490 /* The script is broken ... */
cb703454
PR
491 conn_err(tconn, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
492 return false; /* Eventually leave IO frozen */
b411b363
PR
493 }
494
cb703454
PR
495 conn_info(tconn, "fence-peer helper returned %d (%s)\n",
496 (r>>8) & 0xff, ex_to_string);
fb22c402 497
cb703454 498 out:
fb22c402 499
cb703454
PR
500 /* Not using
501 conn_request_state(tconn, mask, val, CS_VERBOSE);
502 here, because we might were able to re-establish the connection in the
503 meantime. */
504 spin_lock_irq(&tconn->req_lock);
505 if (tconn->cstate < C_WF_REPORT_PARAMS)
506 _conn_request_state(tconn, mask, val, CS_VERBOSE);
507 spin_unlock_irq(&tconn->req_lock);
508
509 return conn_highest_pdsk(tconn) <= D_OUTDATED;
b411b363
PR
510}
511
87f7be4c
PR
512static int _try_outdate_peer_async(void *data)
513{
cb703454 514 struct drbd_tconn *tconn = (struct drbd_tconn *)data;
87f7be4c 515
cb703454 516 conn_try_outdate_peer(tconn);
87f7be4c 517
9dc9fbb3 518 kref_put(&tconn->kref, &conn_destroy);
87f7be4c
PR
519 return 0;
520}
521
cb703454 522void conn_try_outdate_peer_async(struct drbd_tconn *tconn)
87f7be4c
PR
523{
524 struct task_struct *opa;
525
9dc9fbb3 526 kref_get(&tconn->kref);
cb703454 527 opa = kthread_run(_try_outdate_peer_async, tconn, "drbd_async_h");
9dc9fbb3 528 if (IS_ERR(opa)) {
cb703454 529 conn_err(tconn, "out of mem, failed to invoke fence-peer helper\n");
9dc9fbb3
PR
530 kref_put(&tconn->kref, &conn_destroy);
531 }
87f7be4c 532}
b411b363 533
bf885f8a
AG
534enum drbd_state_rv
535drbd_set_role(struct drbd_conf *mdev, enum drbd_role new_role, int force)
b411b363
PR
536{
537 const int max_tries = 4;
bf885f8a 538 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
44ed167d 539 struct net_conf *nc;
b411b363
PR
540 int try = 0;
541 int forced = 0;
542 union drbd_state mask, val;
b411b363
PR
543
544 if (new_role == R_PRIMARY)
0625ac19 545 request_ping(mdev->tconn); /* Detect a dead peer ASAP */
b411b363 546
8410da8f 547 mutex_lock(mdev->state_mutex);
b411b363
PR
548
549 mask.i = 0; mask.role = R_MASK;
550 val.i = 0; val.role = new_role;
551
552 while (try++ < max_tries) {
bf885f8a 553 rv = _drbd_request_state(mdev, mask, val, CS_WAIT_COMPLETE);
b411b363
PR
554
555 /* in case we first succeeded to outdate,
556 * but now suddenly could establish a connection */
bf885f8a 557 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
b411b363
PR
558 val.pdsk = 0;
559 mask.pdsk = 0;
560 continue;
561 }
562
bf885f8a 563 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
d10a33c6
PR
564 (mdev->state.disk < D_UP_TO_DATE &&
565 mdev->state.disk >= D_INCONSISTENT)) {
b411b363
PR
566 mask.disk = D_MASK;
567 val.disk = D_UP_TO_DATE;
568 forced = 1;
569 continue;
570 }
571
bf885f8a 572 if (rv == SS_NO_UP_TO_DATE_DISK &&
b411b363
PR
573 mdev->state.disk == D_CONSISTENT && mask.pdsk == 0) {
574 D_ASSERT(mdev->state.pdsk == D_UNKNOWN);
b411b363 575
cb703454 576 if (conn_try_outdate_peer(mdev->tconn)) {
b411b363
PR
577 val.disk = D_UP_TO_DATE;
578 mask.disk = D_MASK;
579 }
b411b363
PR
580 continue;
581 }
582
bf885f8a 583 if (rv == SS_NOTHING_TO_DO)
3b98c0c2 584 goto out;
bf885f8a 585 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
cb703454 586 if (!conn_try_outdate_peer(mdev->tconn) && force) {
b411b363 587 dev_warn(DEV, "Forced into split brain situation!\n");
cb703454
PR
588 mask.pdsk = D_MASK;
589 val.pdsk = D_OUTDATED;
b411b363 590
cb703454 591 }
b411b363
PR
592 continue;
593 }
bf885f8a 594 if (rv == SS_TWO_PRIMARIES) {
b411b363
PR
595 /* Maybe the peer is detected as dead very soon...
596 retry at most once more in this case. */
44ed167d
PR
597 int timeo;
598 rcu_read_lock();
599 nc = rcu_dereference(mdev->tconn->net_conf);
600 timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
601 rcu_read_unlock();
602 schedule_timeout_interruptible(timeo);
b411b363
PR
603 if (try < max_tries)
604 try = max_tries - 1;
605 continue;
606 }
bf885f8a
AG
607 if (rv < SS_SUCCESS) {
608 rv = _drbd_request_state(mdev, mask, val,
b411b363 609 CS_VERBOSE + CS_WAIT_COMPLETE);
bf885f8a 610 if (rv < SS_SUCCESS)
3b98c0c2 611 goto out;
b411b363
PR
612 }
613 break;
614 }
615
bf885f8a 616 if (rv < SS_SUCCESS)
3b98c0c2 617 goto out;
b411b363
PR
618
619 if (forced)
620 dev_warn(DEV, "Forced to consider local data as UpToDate!\n");
621
622 /* Wait until nothing is on the fly :) */
623 wait_event(mdev->misc_wait, atomic_read(&mdev->ap_pending_cnt) == 0);
624
625 if (new_role == R_SECONDARY) {
81e84650 626 set_disk_ro(mdev->vdisk, true);
b411b363
PR
627 if (get_ldev(mdev)) {
628 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
629 put_ldev(mdev);
630 }
631 } else {
a0095508 632 mutex_lock(&mdev->tconn->conf_update);
91fd4dad 633 nc = mdev->tconn->net_conf;
44ed167d 634 if (nc)
6139f60d 635 nc->discard_my_data = 0; /* without copy; single bit op is atomic */
a0095508 636 mutex_unlock(&mdev->tconn->conf_update);
91fd4dad 637
81e84650 638 set_disk_ro(mdev->vdisk, false);
b411b363
PR
639 if (get_ldev(mdev)) {
640 if (((mdev->state.conn < C_CONNECTED ||
641 mdev->state.pdsk <= D_FAILED)
642 && mdev->ldev->md.uuid[UI_BITMAP] == 0) || forced)
643 drbd_uuid_new_current(mdev);
644
645 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
646 put_ldev(mdev);
647 }
648 }
649
19f843aa
LE
650 /* writeout of activity log covered areas of the bitmap
651 * to stable storage done in after state change already */
b411b363
PR
652
653 if (mdev->state.conn >= C_WF_REPORT_PARAMS) {
654 /* if this was forced, we should consider sync */
655 if (forced)
656 drbd_send_uuids(mdev);
657 drbd_send_state(mdev);
658 }
659
660 drbd_md_sync(mdev);
661
662 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
3b98c0c2 663out:
8410da8f 664 mutex_unlock(mdev->state_mutex);
bf885f8a 665 return rv;
b411b363
PR
666}
667
3b98c0c2 668static const char *from_attrs_err_to_txt(int err)
b411b363 669{
3b98c0c2
LE
670 return err == -ENOMSG ? "required attribute missing" :
671 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
f399002e 672 err == -EEXIST ? "can not change invariant setting" :
3b98c0c2 673 "invalid attribute value";
b411b363
PR
674}
675
3b98c0c2 676int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
b411b363 677{
3b98c0c2
LE
678 struct set_role_parms parms;
679 int err;
680 enum drbd_ret_code retcode;
b411b363 681
3b98c0c2
LE
682 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
683 if (!adm_ctx.reply_skb)
684 return retcode;
685 if (retcode != NO_ERROR)
686 goto out;
687
688 memset(&parms, 0, sizeof(parms));
689 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
f399002e 690 err = set_role_parms_from_attrs(&parms, info);
3b98c0c2
LE
691 if (err) {
692 retcode = ERR_MANDATORY_TAG;
693 drbd_msg_put_info(from_attrs_err_to_txt(err));
694 goto out;
695 }
696 }
697
698 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
699 retcode = drbd_set_role(adm_ctx.mdev, R_PRIMARY, parms.assume_uptodate);
700 else
701 retcode = drbd_set_role(adm_ctx.mdev, R_SECONDARY, 0);
702out:
703 drbd_adm_finish(info, retcode);
b411b363
PR
704 return 0;
705}
706
707/* initializes the md.*_offset members, so we are able to find
708 * the on disk meta data */
709static void drbd_md_set_sector_offsets(struct drbd_conf *mdev,
710 struct drbd_backing_dev *bdev)
711{
712 sector_t md_size_sect = 0;
daeda1cc
PR
713 int meta_dev_idx;
714
715 rcu_read_lock();
716 meta_dev_idx = rcu_dereference(bdev->disk_conf)->meta_dev_idx;
717
718 switch (meta_dev_idx) {
b411b363
PR
719 default:
720 /* v07 style fixed size indexed meta data */
721 bdev->md.md_size_sect = MD_RESERVED_SECT;
722 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
723 bdev->md.al_offset = MD_AL_OFFSET;
724 bdev->md.bm_offset = MD_BM_OFFSET;
725 break;
726 case DRBD_MD_INDEX_FLEX_EXT:
727 /* just occupy the full device; unit: sectors */
728 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
729 bdev->md.md_offset = 0;
730 bdev->md.al_offset = MD_AL_OFFSET;
731 bdev->md.bm_offset = MD_BM_OFFSET;
732 break;
733 case DRBD_MD_INDEX_INTERNAL:
734 case DRBD_MD_INDEX_FLEX_INT:
735 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
736 /* al size is still fixed */
7ad651b5 737 bdev->md.al_offset = -MD_AL_SECTORS;
b411b363
PR
738 /* we need (slightly less than) ~ this much bitmap sectors: */
739 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
740 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
741 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
742 md_size_sect = ALIGN(md_size_sect, 8);
743
744 /* plus the "drbd meta data super block",
745 * and the activity log; */
746 md_size_sect += MD_BM_OFFSET;
747
748 bdev->md.md_size_sect = md_size_sect;
749 /* bitmap offset is adjusted by 'super' block size */
750 bdev->md.bm_offset = -md_size_sect + MD_AL_OFFSET;
751 break;
752 }
daeda1cc 753 rcu_read_unlock();
b411b363
PR
754}
755
4b0715f0 756/* input size is expected to be in KB */
b411b363
PR
757char *ppsize(char *buf, unsigned long long size)
758{
4b0715f0
LE
759 /* Needs 9 bytes at max including trailing NUL:
760 * -1ULL ==> "16384 EB" */
b411b363
PR
761 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
762 int base = 0;
4b0715f0 763 while (size >= 10000 && base < sizeof(units)-1) {
b411b363
PR
764 /* shift + round */
765 size = (size >> 10) + !!(size & (1<<9));
766 base++;
767 }
4b0715f0 768 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
b411b363
PR
769
770 return buf;
771}
772
773/* there is still a theoretical deadlock when called from receiver
774 * on an D_INCONSISTENT R_PRIMARY:
775 * remote READ does inc_ap_bio, receiver would need to receive answer
776 * packet from remote to dec_ap_bio again.
777 * receiver receive_sizes(), comes here,
778 * waits for ap_bio_cnt == 0. -> deadlock.
779 * but this cannot happen, actually, because:
780 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
781 * (not connected, or bad/no disk on peer):
782 * see drbd_fail_request_early, ap_bio_cnt is zero.
783 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
784 * peer may not initiate a resize.
785 */
3b98c0c2
LE
786/* Note these are not to be confused with
787 * drbd_adm_suspend_io/drbd_adm_resume_io,
788 * which are (sub) state changes triggered by admin (drbdsetup),
789 * and can be long lived.
790 * This changes an mdev->flag, is triggered by drbd internals,
791 * and should be short-lived. */
b411b363
PR
792void drbd_suspend_io(struct drbd_conf *mdev)
793{
794 set_bit(SUSPEND_IO, &mdev->flags);
2aebfabb 795 if (drbd_suspended(mdev))
265be2d0 796 return;
b411b363
PR
797 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
798}
799
800void drbd_resume_io(struct drbd_conf *mdev)
801{
802 clear_bit(SUSPEND_IO, &mdev->flags);
803 wake_up(&mdev->misc_wait);
804}
805
806/**
807 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
808 * @mdev: DRBD device.
809 *
810 * Returns 0 on success, negative return values indicate errors.
811 * You should call drbd_md_sync() after calling this function.
812 */
24c4830c 813enum determine_dev_size drbd_determine_dev_size(struct drbd_conf *mdev, enum dds_flags flags) __must_hold(local)
b411b363
PR
814{
815 sector_t prev_first_sect, prev_size; /* previous meta location */
ef5e44a6 816 sector_t la_size, u_size;
b411b363
PR
817 sector_t size;
818 char ppb[10];
819
820 int md_moved, la_size_changed;
821 enum determine_dev_size rv = unchanged;
822
823 /* race:
824 * application request passes inc_ap_bio,
825 * but then cannot get an AL-reference.
826 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
827 *
828 * to avoid that:
829 * Suspend IO right here.
830 * still lock the act_log to not trigger ASSERTs there.
831 */
832 drbd_suspend_io(mdev);
833
834 /* no wait necessary anymore, actually we could assert that */
835 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
836
837 prev_first_sect = drbd_md_first_sector(mdev->ldev);
838 prev_size = mdev->ldev->md.md_size_sect;
839 la_size = mdev->ldev->md.la_size_sect;
840
841 /* TODO: should only be some assert here, not (re)init... */
842 drbd_md_set_sector_offsets(mdev, mdev->ldev);
843
daeda1cc
PR
844 rcu_read_lock();
845 u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
846 rcu_read_unlock();
ef5e44a6 847 size = drbd_new_dev_size(mdev, mdev->ldev, u_size, flags & DDSF_FORCED);
b411b363
PR
848
849 if (drbd_get_capacity(mdev->this_bdev) != size ||
850 drbd_bm_capacity(mdev) != size) {
851 int err;
02d9a94b 852 err = drbd_bm_resize(mdev, size, !(flags & DDSF_NO_RESYNC));
b411b363
PR
853 if (unlikely(err)) {
854 /* currently there is only one error: ENOMEM! */
855 size = drbd_bm_capacity(mdev)>>1;
856 if (size == 0) {
857 dev_err(DEV, "OUT OF MEMORY! "
858 "Could not allocate bitmap!\n");
859 } else {
860 dev_err(DEV, "BM resizing failed. "
861 "Leaving size unchanged at size = %lu KB\n",
862 (unsigned long)size);
863 }
864 rv = dev_size_error;
865 }
866 /* racy, see comments above. */
867 drbd_set_my_capacity(mdev, size);
868 mdev->ldev->md.la_size_sect = size;
869 dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
870 (unsigned long long)size>>1);
871 }
872 if (rv == dev_size_error)
873 goto out;
874
875 la_size_changed = (la_size != mdev->ldev->md.la_size_sect);
876
877 md_moved = prev_first_sect != drbd_md_first_sector(mdev->ldev)
878 || prev_size != mdev->ldev->md.md_size_sect;
879
880 if (la_size_changed || md_moved) {
24dccabb
AG
881 int err;
882
b411b363
PR
883 drbd_al_shrink(mdev); /* All extents inactive. */
884 dev_info(DEV, "Writing the whole bitmap, %s\n",
885 la_size_changed && md_moved ? "size changed and md moved" :
886 la_size_changed ? "size changed" : "md moved");
20ceb2b2
LE
887 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
888 err = drbd_bitmap_io(mdev, &drbd_bm_write,
889 "size changed", BM_LOCKED_MASK);
24dccabb
AG
890 if (err) {
891 rv = dev_size_error;
892 goto out;
893 }
b411b363
PR
894 drbd_md_mark_dirty(mdev);
895 }
896
897 if (size > la_size)
898 rv = grew;
899 if (size < la_size)
900 rv = shrunk;
901out:
902 lc_unlock(mdev->act_log);
903 wake_up(&mdev->al_wait);
904 drbd_resume_io(mdev);
905
906 return rv;
907}
908
909sector_t
ef5e44a6
PR
910drbd_new_dev_size(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
911 sector_t u_size, int assume_peer_has_space)
b411b363
PR
912{
913 sector_t p_size = mdev->p_size; /* partner's disk size. */
914 sector_t la_size = bdev->md.la_size_sect; /* last agreed size. */
915 sector_t m_size; /* my size */
b411b363
PR
916 sector_t size = 0;
917
918 m_size = drbd_get_max_capacity(bdev);
919
a393db6f
PR
920 if (mdev->state.conn < C_CONNECTED && assume_peer_has_space) {
921 dev_warn(DEV, "Resize while not connected was forced by the user!\n");
922 p_size = m_size;
923 }
924
b411b363
PR
925 if (p_size && m_size) {
926 size = min_t(sector_t, p_size, m_size);
927 } else {
928 if (la_size) {
929 size = la_size;
930 if (m_size && m_size < size)
931 size = m_size;
932 if (p_size && p_size < size)
933 size = p_size;
934 } else {
935 if (m_size)
936 size = m_size;
937 if (p_size)
938 size = p_size;
939 }
940 }
941
942 if (size == 0)
943 dev_err(DEV, "Both nodes diskless!\n");
944
945 if (u_size) {
946 if (u_size > size)
947 dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n",
948 (unsigned long)u_size>>1, (unsigned long)size>>1);
949 else
950 size = u_size;
951 }
952
953 return size;
954}
955
956/**
957 * drbd_check_al_size() - Ensures that the AL is of the right size
958 * @mdev: DRBD device.
959 *
960 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
961 * failed, and 0 on success. You should call drbd_md_sync() after you called
962 * this function.
963 */
f399002e 964static int drbd_check_al_size(struct drbd_conf *mdev, struct disk_conf *dc)
b411b363
PR
965{
966 struct lru_cache *n, *t;
967 struct lc_element *e;
968 unsigned int in_use;
969 int i;
970
b411b363 971 if (mdev->act_log &&
f399002e 972 mdev->act_log->nr_elements == dc->al_extents)
b411b363
PR
973 return 0;
974
975 in_use = 0;
976 t = mdev->act_log;
7ad651b5 977 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
f399002e 978 dc->al_extents, sizeof(struct lc_element), 0);
b411b363
PR
979
980 if (n == NULL) {
981 dev_err(DEV, "Cannot allocate act_log lru!\n");
982 return -ENOMEM;
983 }
984 spin_lock_irq(&mdev->al_lock);
985 if (t) {
986 for (i = 0; i < t->nr_elements; i++) {
987 e = lc_element_by_index(t, i);
988 if (e->refcnt)
989 dev_err(DEV, "refcnt(%d)==%d\n",
990 e->lc_number, e->refcnt);
991 in_use += e->refcnt;
992 }
993 }
994 if (!in_use)
995 mdev->act_log = n;
996 spin_unlock_irq(&mdev->al_lock);
997 if (in_use) {
998 dev_err(DEV, "Activity log still in use!\n");
999 lc_destroy(n);
1000 return -EBUSY;
1001 } else {
1002 if (t)
1003 lc_destroy(t);
1004 }
1005 drbd_md_mark_dirty(mdev); /* we changed mdev->act_log->nr_elemens */
1006 return 0;
1007}
1008
99432fcc 1009static void drbd_setup_queue_param(struct drbd_conf *mdev, unsigned int max_bio_size)
b411b363
PR
1010{
1011 struct request_queue * const q = mdev->rq_queue;
99432fcc
PR
1012 int max_hw_sectors = max_bio_size >> 9;
1013 int max_segments = 0;
1014
1015 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1016 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
1017
1018 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
daeda1cc
PR
1019 rcu_read_lock();
1020 max_segments = rcu_dereference(mdev->ldev->disk_conf)->max_bio_bvecs;
1021 rcu_read_unlock();
99432fcc
PR
1022 put_ldev(mdev);
1023 }
b411b363 1024
b411b363 1025 blk_queue_logical_block_size(q, 512);
1816a2b4
LE
1026 blk_queue_max_hw_sectors(q, max_hw_sectors);
1027 /* This is the workaround for "bio would need to, but cannot, be split" */
1028 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
1029 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
b411b363 1030
99432fcc
PR
1031 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1032 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
1033
1034 blk_queue_stack_limits(q, b);
1035
1036 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
1037 dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
1038 q->backing_dev_info.ra_pages,
1039 b->backing_dev_info.ra_pages);
1040 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1041 }
1042 put_ldev(mdev);
1043 }
1044}
1045
1046void drbd_reconsider_max_bio_size(struct drbd_conf *mdev)
1047{
1048 int now, new, local, peer;
1049
1050 now = queue_max_hw_sectors(mdev->rq_queue) << 9;
1051 local = mdev->local_max_bio_size; /* Eventually last known value, from volatile memory */
1052 peer = mdev->peer_max_bio_size; /* Eventually last known value, from meta data */
b411b363 1053
99432fcc
PR
1054 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1055 local = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
1056 mdev->local_max_bio_size = local;
1057 put_ldev(mdev);
b411b363 1058 }
99432fcc
PR
1059
1060 /* We may ignore peer limits if the peer is modern enough.
1061 Because new from 8.3.8 onwards the peer can use multiple
1062 BIOs for a single peer_request */
1063 if (mdev->state.conn >= C_CONNECTED) {
31890f4a 1064 if (mdev->tconn->agreed_pro_version < 94)
2ffca4f3
PR
1065 peer = min_t(int, mdev->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
1066 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
31890f4a 1067 else if (mdev->tconn->agreed_pro_version == 94)
99432fcc 1068 peer = DRBD_MAX_SIZE_H80_PACKET;
2ffca4f3
PR
1069 else if (mdev->tconn->agreed_pro_version < 100)
1070 peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
1071 else
99432fcc
PR
1072 peer = DRBD_MAX_BIO_SIZE;
1073 }
1074
1075 new = min_t(int, local, peer);
1076
1077 if (mdev->state.role == R_PRIMARY && new < now)
1078 dev_err(DEV, "ASSERT FAILED new < now; (%d < %d)\n", new, now);
1079
1080 if (new != now)
1081 dev_info(DEV, "max BIO size = %u\n", new);
1082
1083 drbd_setup_queue_param(mdev, new);
b411b363
PR
1084}
1085
a18e9d1e 1086/* Starts the worker thread */
0e29d163 1087static void conn_reconfig_start(struct drbd_tconn *tconn)
b411b363 1088{
0e29d163
PR
1089 drbd_thread_start(&tconn->worker);
1090 conn_flush_workqueue(tconn);
b411b363
PR
1091}
1092
a18e9d1e 1093/* if still unconfigured, stops worker again. */
0e29d163 1094static void conn_reconfig_done(struct drbd_tconn *tconn)
b411b363 1095{
992d6e91 1096 bool stop_threads;
0e29d163 1097 spin_lock_irq(&tconn->req_lock);
992d6e91 1098 stop_threads = conn_all_vols_unconf(tconn);
0e29d163 1099 spin_unlock_irq(&tconn->req_lock);
992d6e91
LE
1100 if (stop_threads) {
1101 /* asender is implicitly stopped by receiver
81fa2e67 1102 * in conn_disconnect() */
992d6e91
LE
1103 drbd_thread_stop(&tconn->receiver);
1104 drbd_thread_stop(&tconn->worker);
1105 }
b411b363
PR
1106}
1107
0778286a
PR
1108/* Make sure IO is suspended before calling this function(). */
1109static void drbd_suspend_al(struct drbd_conf *mdev)
1110{
1111 int s = 0;
1112
61610420 1113 if (!lc_try_lock(mdev->act_log)) {
0778286a
PR
1114 dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n");
1115 return;
1116 }
1117
61610420 1118 drbd_al_shrink(mdev);
87eeee41 1119 spin_lock_irq(&mdev->tconn->req_lock);
0778286a
PR
1120 if (mdev->state.conn < C_CONNECTED)
1121 s = !test_and_set_bit(AL_SUSPENDED, &mdev->flags);
87eeee41 1122 spin_unlock_irq(&mdev->tconn->req_lock);
61610420 1123 lc_unlock(mdev->act_log);
0778286a
PR
1124
1125 if (s)
1126 dev_info(DEV, "Suspended AL updates\n");
1127}
1128
5979e361
LE
1129
1130static bool should_set_defaults(struct genl_info *info)
1131{
1132 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1133 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1134}
1135
d589a21e
PR
1136static void enforce_disk_conf_limits(struct disk_conf *dc)
1137{
1138 if (dc->al_extents < DRBD_AL_EXTENTS_MIN)
1139 dc->al_extents = DRBD_AL_EXTENTS_MIN;
1140 if (dc->al_extents > DRBD_AL_EXTENTS_MAX)
1141 dc->al_extents = DRBD_AL_EXTENTS_MAX;
1142
1143 if (dc->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1144 dc->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
1145}
1146
f399002e
LE
1147int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1148{
1149 enum drbd_ret_code retcode;
1150 struct drbd_conf *mdev;
daeda1cc 1151 struct disk_conf *new_disk_conf, *old_disk_conf;
813472ce 1152 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
f399002e 1153 int err, fifo_size;
f399002e
LE
1154
1155 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1156 if (!adm_ctx.reply_skb)
1157 return retcode;
1158 if (retcode != NO_ERROR)
1159 goto out;
1160
1161 mdev = adm_ctx.mdev;
1162
1163 /* we also need a disk
1164 * to change the options on */
1165 if (!get_ldev(mdev)) {
1166 retcode = ERR_NO_DISK;
1167 goto out;
1168 }
1169
daeda1cc 1170 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
5ecc72c3 1171 if (!new_disk_conf) {
f399002e
LE
1172 retcode = ERR_NOMEM;
1173 goto fail;
1174 }
1175
daeda1cc
PR
1176 mutex_lock(&mdev->tconn->conf_update);
1177 old_disk_conf = mdev->ldev->disk_conf;
1178 *new_disk_conf = *old_disk_conf;
5979e361 1179 if (should_set_defaults(info))
b966b5dd 1180 set_disk_conf_defaults(new_disk_conf);
5979e361 1181
5ecc72c3 1182 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
c75b9b10 1183 if (err && err != -ENOMSG) {
f399002e
LE
1184 retcode = ERR_MANDATORY_TAG;
1185 drbd_msg_put_info(from_attrs_err_to_txt(err));
1186 }
1187
5ecc72c3
LE
1188 if (!expect(new_disk_conf->resync_rate >= 1))
1189 new_disk_conf->resync_rate = 1;
f399002e 1190
d589a21e 1191 enforce_disk_conf_limits(new_disk_conf);
f399002e 1192
5ecc72c3 1193 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
9958c857 1194 if (fifo_size != mdev->rs_plan_s->size) {
813472ce
PR
1195 new_plan = fifo_alloc(fifo_size);
1196 if (!new_plan) {
f399002e
LE
1197 dev_err(DEV, "kmalloc of fifo_buffer failed");
1198 retcode = ERR_NOMEM;
daeda1cc 1199 goto fail_unlock;
f399002e
LE
1200 }
1201 }
1202
f399002e
LE
1203 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
1204 drbd_al_shrink(mdev);
5ecc72c3 1205 err = drbd_check_al_size(mdev, new_disk_conf);
f399002e
LE
1206 lc_unlock(mdev->act_log);
1207 wake_up(&mdev->al_wait);
1208
1209 if (err) {
1210 retcode = ERR_NOMEM;
daeda1cc 1211 goto fail_unlock;
f399002e
LE
1212 }
1213
dc97b708 1214 write_lock_irq(&global_state_lock);
95f8efd0 1215 retcode = drbd_resync_after_valid(mdev, new_disk_conf->resync_after);
dc97b708 1216 if (retcode == NO_ERROR) {
daeda1cc 1217 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
95f8efd0 1218 drbd_resync_after_changed(mdev);
dc97b708
PR
1219 }
1220 write_unlock_irq(&global_state_lock);
f399002e 1221
daeda1cc
PR
1222 if (retcode != NO_ERROR)
1223 goto fail_unlock;
f399002e 1224
813472ce
PR
1225 if (new_plan) {
1226 old_plan = mdev->rs_plan_s;
1227 rcu_assign_pointer(mdev->rs_plan_s, new_plan);
9958c857 1228 }
9958c857 1229
c141ebda 1230 mutex_unlock(&mdev->tconn->conf_update);
daeda1cc 1231 drbd_md_sync(mdev);
f399002e
LE
1232
1233 if (mdev->state.conn >= C_CONNECTED)
1234 drbd_send_sync_param(mdev);
1235
daeda1cc
PR
1236 synchronize_rcu();
1237 kfree(old_disk_conf);
813472ce 1238 kfree(old_plan);
cdfda633 1239 mod_timer(&mdev->request_timer, jiffies + HZ);
daeda1cc
PR
1240 goto success;
1241
1242fail_unlock:
1243 mutex_unlock(&mdev->tconn->conf_update);
f399002e 1244 fail:
5ecc72c3 1245 kfree(new_disk_conf);
813472ce 1246 kfree(new_plan);
daeda1cc
PR
1247success:
1248 put_ldev(mdev);
f399002e
LE
1249 out:
1250 drbd_adm_finish(info, retcode);
1251 return 0;
1252}
1253
3b98c0c2 1254int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
b411b363 1255{
3b98c0c2
LE
1256 struct drbd_conf *mdev;
1257 int err;
116676ca 1258 enum drbd_ret_code retcode;
b411b363
PR
1259 enum determine_dev_size dd;
1260 sector_t max_possible_sectors;
1261 sector_t min_md_device_sectors;
1262 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
daeda1cc 1263 struct disk_conf *new_disk_conf = NULL;
e525fd89 1264 struct block_device *bdev;
b411b363 1265 struct lru_cache *resync_lru = NULL;
9958c857 1266 struct fifo_buffer *new_plan = NULL;
b411b363 1267 union drbd_state ns, os;
f2024e7c 1268 enum drbd_state_rv rv;
44ed167d 1269 struct net_conf *nc;
b411b363 1270
3b98c0c2
LE
1271 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1272 if (!adm_ctx.reply_skb)
1273 return retcode;
1274 if (retcode != NO_ERROR)
40cbf085 1275 goto finish;
3b98c0c2
LE
1276
1277 mdev = adm_ctx.mdev;
0e29d163 1278 conn_reconfig_start(mdev->tconn);
b411b363
PR
1279
1280 /* if you want to reconfigure, please tear down first */
1281 if (mdev->state.disk > D_DISKLESS) {
1282 retcode = ERR_DISK_CONFIGURED;
1283 goto fail;
1284 }
82f59cc6
LE
1285 /* It may just now have detached because of IO error. Make sure
1286 * drbd_ldev_destroy is done already, we may end up here very fast,
1287 * e.g. if someone calls attach from the on-io-error handler,
1288 * to realize a "hot spare" feature (not that I'd recommend that) */
1289 wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
b411b363 1290
3b98c0c2 1291 /* allocation not in the IO path, drbdsetup context */
b411b363
PR
1292 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1293 if (!nbc) {
1294 retcode = ERR_NOMEM;
1295 goto fail;
1296 }
daeda1cc
PR
1297 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
1298 if (!new_disk_conf) {
1299 retcode = ERR_NOMEM;
1300 goto fail;
1301 }
1302 nbc->disk_conf = new_disk_conf;
b411b363 1303
daeda1cc
PR
1304 set_disk_conf_defaults(new_disk_conf);
1305 err = disk_conf_from_attrs(new_disk_conf, info);
3b98c0c2 1306 if (err) {
b411b363 1307 retcode = ERR_MANDATORY_TAG;
3b98c0c2 1308 drbd_msg_put_info(from_attrs_err_to_txt(err));
b411b363
PR
1309 goto fail;
1310 }
1311
d589a21e
PR
1312 enforce_disk_conf_limits(new_disk_conf);
1313
9958c857
PR
1314 new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
1315 if (!new_plan) {
1316 retcode = ERR_NOMEM;
1317 goto fail;
1318 }
1319
daeda1cc 1320 if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
b411b363
PR
1321 retcode = ERR_MD_IDX_INVALID;
1322 goto fail;
1323 }
1324
44ed167d
PR
1325 rcu_read_lock();
1326 nc = rcu_dereference(mdev->tconn->net_conf);
1327 if (nc) {
daeda1cc 1328 if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
44ed167d 1329 rcu_read_unlock();
47ff2d0a
PR
1330 retcode = ERR_STONITH_AND_PROT_A;
1331 goto fail;
1332 }
1333 }
44ed167d 1334 rcu_read_unlock();
47ff2d0a 1335
daeda1cc 1336 bdev = blkdev_get_by_path(new_disk_conf->backing_dev,
d4d77629 1337 FMODE_READ | FMODE_WRITE | FMODE_EXCL, mdev);
e525fd89 1338 if (IS_ERR(bdev)) {
daeda1cc 1339 dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->backing_dev,
e525fd89 1340 PTR_ERR(bdev));
b411b363
PR
1341 retcode = ERR_OPEN_DISK;
1342 goto fail;
1343 }
e525fd89
TH
1344 nbc->backing_bdev = bdev;
1345
1346 /*
1347 * meta_dev_idx >= 0: external fixed size, possibly multiple
1348 * drbd sharing one meta device. TODO in that case, paranoia
1349 * check that [md_bdev, meta_dev_idx] is not yet used by some
1350 * other drbd minor! (if you use drbd.conf + drbdadm, that
1351 * should check it for you already; but if you don't, or
1352 * someone fooled it, we need to double check here)
1353 */
daeda1cc 1354 bdev = blkdev_get_by_path(new_disk_conf->meta_dev,
d4d77629 1355 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
daeda1cc 1356 (new_disk_conf->meta_dev_idx < 0) ?
d4d77629 1357 (void *)mdev : (void *)drbd_m_holder);
e525fd89 1358 if (IS_ERR(bdev)) {
daeda1cc 1359 dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->meta_dev,
e525fd89 1360 PTR_ERR(bdev));
b411b363
PR
1361 retcode = ERR_OPEN_MD_DISK;
1362 goto fail;
1363 }
e525fd89 1364 nbc->md_bdev = bdev;
b411b363 1365
e525fd89 1366 if ((nbc->backing_bdev == nbc->md_bdev) !=
daeda1cc
PR
1367 (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1368 new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
e525fd89 1369 retcode = ERR_MD_IDX_INVALID;
b411b363
PR
1370 goto fail;
1371 }
1372
1373 resync_lru = lc_create("resync", drbd_bm_ext_cache,
46a15bc3 1374 1, 61, sizeof(struct bm_extent),
b411b363
PR
1375 offsetof(struct bm_extent, lce));
1376 if (!resync_lru) {
1377 retcode = ERR_NOMEM;
e525fd89 1378 goto fail;
b411b363
PR
1379 }
1380
1381 /* RT - for drbd_get_max_capacity() DRBD_MD_INDEX_FLEX_INT */
1382 drbd_md_set_sector_offsets(mdev, nbc);
1383
daeda1cc 1384 if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
b411b363
PR
1385 dev_err(DEV, "max capacity %llu smaller than disk size %llu\n",
1386 (unsigned long long) drbd_get_max_capacity(nbc),
daeda1cc 1387 (unsigned long long) new_disk_conf->disk_size);
67b58bf7 1388 retcode = ERR_DISK_TOO_SMALL;
e525fd89 1389 goto fail;
b411b363
PR
1390 }
1391
daeda1cc 1392 if (new_disk_conf->meta_dev_idx < 0) {
b411b363
PR
1393 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1394 /* at least one MB, otherwise it does not make sense */
1395 min_md_device_sectors = (2<<10);
1396 } else {
1397 max_possible_sectors = DRBD_MAX_SECTORS;
daeda1cc 1398 min_md_device_sectors = MD_RESERVED_SECT * (new_disk_conf->meta_dev_idx + 1);
b411b363
PR
1399 }
1400
b411b363 1401 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
67b58bf7 1402 retcode = ERR_MD_DISK_TOO_SMALL;
b411b363
PR
1403 dev_warn(DEV, "refusing attach: md-device too small, "
1404 "at least %llu sectors needed for this meta-disk type\n",
1405 (unsigned long long) min_md_device_sectors);
e525fd89 1406 goto fail;
b411b363
PR
1407 }
1408
1409 /* Make sure the new disk is big enough
1410 * (we may currently be R_PRIMARY with no local disk...) */
1411 if (drbd_get_max_capacity(nbc) <
1412 drbd_get_capacity(mdev->this_bdev)) {
67b58bf7 1413 retcode = ERR_DISK_TOO_SMALL;
e525fd89 1414 goto fail;
b411b363
PR
1415 }
1416
1417 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1418
1352994b
LE
1419 if (nbc->known_size > max_possible_sectors) {
1420 dev_warn(DEV, "==> truncating very big lower level device "
1421 "to currently maximum possible %llu sectors <==\n",
1422 (unsigned long long) max_possible_sectors);
daeda1cc 1423 if (new_disk_conf->meta_dev_idx >= 0)
1352994b
LE
1424 dev_warn(DEV, "==>> using internal or flexible "
1425 "meta data may help <<==\n");
1426 }
1427
b411b363
PR
1428 drbd_suspend_io(mdev);
1429 /* also wait for the last barrier ack. */
2aebfabb 1430 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_pending_cnt) || drbd_suspended(mdev));
b411b363 1431 /* and for any other previously queued work */
a21e9298 1432 drbd_flush_workqueue(mdev);
b411b363 1433
f2024e7c
AG
1434 rv = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE);
1435 retcode = rv; /* FIXME: Type mismatch. */
b411b363 1436 drbd_resume_io(mdev);
f2024e7c 1437 if (rv < SS_SUCCESS)
e525fd89 1438 goto fail;
b411b363
PR
1439
1440 if (!get_ldev_if_state(mdev, D_ATTACHING))
1441 goto force_diskless;
1442
1443 drbd_md_set_sector_offsets(mdev, nbc);
1444
1445 if (!mdev->bitmap) {
1446 if (drbd_bm_init(mdev)) {
1447 retcode = ERR_NOMEM;
1448 goto force_diskless_dec;
1449 }
1450 }
1451
1452 retcode = drbd_md_read(mdev, nbc);
1453 if (retcode != NO_ERROR)
1454 goto force_diskless_dec;
1455
1456 if (mdev->state.conn < C_CONNECTED &&
1457 mdev->state.role == R_PRIMARY &&
1458 (mdev->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
1459 dev_err(DEV, "Can only attach to data with current UUID=%016llX\n",
1460 (unsigned long long)mdev->ed_uuid);
1461 retcode = ERR_DATA_NOT_CURRENT;
1462 goto force_diskless_dec;
1463 }
1464
1465 /* Since we are diskless, fix the activity log first... */
daeda1cc 1466 if (drbd_check_al_size(mdev, new_disk_conf)) {
b411b363
PR
1467 retcode = ERR_NOMEM;
1468 goto force_diskless_dec;
1469 }
1470
1471 /* Prevent shrinking of consistent devices ! */
1472 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
daeda1cc 1473 drbd_new_dev_size(mdev, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
b411b363 1474 dev_warn(DEV, "refusing to truncate a consistent device\n");
67b58bf7 1475 retcode = ERR_DISK_TOO_SMALL;
b411b363
PR
1476 goto force_diskless_dec;
1477 }
1478
b411b363
PR
1479 /* Reset the "barriers don't work" bits here, then force meta data to
1480 * be written, to ensure we determine if barriers are supported. */
e544046a 1481 if (new_disk_conf->md_flushes)
a8a4e51e 1482 clear_bit(MD_NO_FUA, &mdev->flags);
e544046a
AG
1483 else
1484 set_bit(MD_NO_FUA, &mdev->flags);
b411b363
PR
1485
1486 /* Point of no return reached.
1487 * Devices and memory are no longer released by error cleanup below.
1488 * now mdev takes over responsibility, and the state engine should
1489 * clean it up somewhere. */
1490 D_ASSERT(mdev->ldev == NULL);
1491 mdev->ldev = nbc;
1492 mdev->resync = resync_lru;
9958c857 1493 mdev->rs_plan_s = new_plan;
b411b363
PR
1494 nbc = NULL;
1495 resync_lru = NULL;
daeda1cc 1496 new_disk_conf = NULL;
9958c857 1497 new_plan = NULL;
b411b363 1498
2451fc3b
PR
1499 mdev->write_ordering = WO_bdev_flush;
1500 drbd_bump_write_ordering(mdev, WO_bdev_flush);
b411b363
PR
1501
1502 if (drbd_md_test_flag(mdev->ldev, MDF_CRASHED_PRIMARY))
1503 set_bit(CRASHED_PRIMARY, &mdev->flags);
1504 else
1505 clear_bit(CRASHED_PRIMARY, &mdev->flags);
1506
894c6a94 1507 if (drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
d5d7ebd4 1508 !(mdev->state.role == R_PRIMARY && mdev->tconn->susp_nod))
b411b363 1509 set_bit(CRASHED_PRIMARY, &mdev->flags);
b411b363
PR
1510
1511 mdev->send_cnt = 0;
1512 mdev->recv_cnt = 0;
1513 mdev->read_cnt = 0;
1514 mdev->writ_cnt = 0;
1515
99432fcc 1516 drbd_reconsider_max_bio_size(mdev);
b411b363
PR
1517
1518 /* If I am currently not R_PRIMARY,
1519 * but meta data primary indicator is set,
1520 * I just now recover from a hard crash,
1521 * and have been R_PRIMARY before that crash.
1522 *
1523 * Now, if I had no connection before that crash
1524 * (have been degraded R_PRIMARY), chances are that
1525 * I won't find my peer now either.
1526 *
1527 * In that case, and _only_ in that case,
1528 * we use the degr-wfc-timeout instead of the default,
1529 * so we can automatically recover from a crash of a
1530 * degraded but active "cluster" after a certain timeout.
1531 */
1532 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
1533 if (mdev->state.role != R_PRIMARY &&
1534 drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
1535 !drbd_md_test_flag(mdev->ldev, MDF_CONNECTED_IND))
1536 set_bit(USE_DEGR_WFC_T, &mdev->flags);
1537
24c4830c 1538 dd = drbd_determine_dev_size(mdev, 0);
b411b363
PR
1539 if (dd == dev_size_error) {
1540 retcode = ERR_NOMEM_BITMAP;
1541 goto force_diskless_dec;
1542 } else if (dd == grew)
1543 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
1544
1545 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
1546 dev_info(DEV, "Assuming that all blocks are out of sync "
1547 "(aka FullSync)\n");
20ceb2b2
LE
1548 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write,
1549 "set_n_write from attaching", BM_LOCKED_MASK)) {
b411b363
PR
1550 retcode = ERR_IO_MD_DISK;
1551 goto force_diskless_dec;
1552 }
1553 } else {
20ceb2b2 1554 if (drbd_bitmap_io(mdev, &drbd_bm_read,
22ab6a30 1555 "read from attaching", BM_LOCKED_MASK)) {
b411b363
PR
1556 retcode = ERR_IO_MD_DISK;
1557 goto force_diskless_dec;
1558 }
1559 }
1560
0778286a
PR
1561 if (_drbd_bm_total_weight(mdev) == drbd_bm_bits(mdev))
1562 drbd_suspend_al(mdev); /* IO is still suspended here... */
1563
87eeee41 1564 spin_lock_irq(&mdev->tconn->req_lock);
78bae59b
PR
1565 os = drbd_read_state(mdev);
1566 ns = os;
b411b363
PR
1567 /* If MDF_CONSISTENT is not set go into inconsistent state,
1568 otherwise investigate MDF_WasUpToDate...
1569 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1570 otherwise into D_CONSISTENT state.
1571 */
1572 if (drbd_md_test_flag(mdev->ldev, MDF_CONSISTENT)) {
1573 if (drbd_md_test_flag(mdev->ldev, MDF_WAS_UP_TO_DATE))
1574 ns.disk = D_CONSISTENT;
1575 else
1576 ns.disk = D_OUTDATED;
1577 } else {
1578 ns.disk = D_INCONSISTENT;
1579 }
1580
1581 if (drbd_md_test_flag(mdev->ldev, MDF_PEER_OUT_DATED))
1582 ns.pdsk = D_OUTDATED;
1583
daeda1cc
PR
1584 rcu_read_lock();
1585 if (ns.disk == D_CONSISTENT &&
1586 (ns.pdsk == D_OUTDATED || rcu_dereference(mdev->ldev->disk_conf)->fencing == FP_DONT_CARE))
b411b363 1587 ns.disk = D_UP_TO_DATE;
daeda1cc 1588 rcu_read_unlock();
b411b363
PR
1589
1590 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1591 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1592 this point, because drbd_request_state() modifies these
1593 flags. */
1594
1595 /* In case we are C_CONNECTED postpone any decision on the new disk
1596 state after the negotiation phase. */
1597 if (mdev->state.conn == C_CONNECTED) {
1598 mdev->new_state_tmp.i = ns.i;
1599 ns.i = os.i;
1600 ns.disk = D_NEGOTIATING;
dc66c74d
PR
1601
1602 /* We expect to receive up-to-date UUIDs soon.
1603 To avoid a race in receive_state, free p_uuid while
1604 holding req_lock. I.e. atomic with the state change */
1605 kfree(mdev->p_uuid);
1606 mdev->p_uuid = NULL;
b411b363
PR
1607 }
1608
1609 rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
87eeee41 1610 spin_unlock_irq(&mdev->tconn->req_lock);
b411b363
PR
1611
1612 if (rv < SS_SUCCESS)
1613 goto force_diskless_dec;
1614
cdfda633
PR
1615 mod_timer(&mdev->request_timer, jiffies + HZ);
1616
b411b363
PR
1617 if (mdev->state.role == R_PRIMARY)
1618 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
1619 else
1620 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
1621
1622 drbd_md_mark_dirty(mdev);
1623 drbd_md_sync(mdev);
1624
1625 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1626 put_ldev(mdev);
0e29d163 1627 conn_reconfig_done(mdev->tconn);
3b98c0c2 1628 drbd_adm_finish(info, retcode);
b411b363
PR
1629 return 0;
1630
1631 force_diskless_dec:
1632 put_ldev(mdev);
1633 force_diskless:
9510b241 1634 drbd_force_state(mdev, NS(disk, D_DISKLESS));
b411b363 1635 drbd_md_sync(mdev);
b411b363 1636 fail:
40cbf085 1637 conn_reconfig_done(mdev->tconn);
b411b363 1638 if (nbc) {
e525fd89
TH
1639 if (nbc->backing_bdev)
1640 blkdev_put(nbc->backing_bdev,
1641 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1642 if (nbc->md_bdev)
1643 blkdev_put(nbc->md_bdev,
1644 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
b411b363
PR
1645 kfree(nbc);
1646 }
daeda1cc 1647 kfree(new_disk_conf);
b411b363 1648 lc_destroy(resync_lru);
9958c857 1649 kfree(new_plan);
b411b363 1650
40cbf085 1651 finish:
3b98c0c2 1652 drbd_adm_finish(info, retcode);
b411b363
PR
1653 return 0;
1654}
1655
cdfda633 1656static int adm_detach(struct drbd_conf *mdev, int force)
85f75dd7 1657{
19f83c76 1658 enum drbd_state_rv retcode;
009ba89d 1659 int ret;
cdfda633
PR
1660
1661 if (force) {
1662 drbd_force_state(mdev, NS(disk, D_FAILED));
1663 retcode = SS_SUCCESS;
1664 goto out;
1665 }
1666
85f75dd7 1667 drbd_suspend_io(mdev); /* so no-one is stuck in drbd_al_begin_io */
009ba89d
LE
1668 retcode = drbd_request_state(mdev, NS(disk, D_FAILED));
1669 /* D_FAILED will transition to DISKLESS. */
1670 ret = wait_event_interruptible(mdev->misc_wait,
1671 mdev->state.disk != D_FAILED);
85f75dd7 1672 drbd_resume_io(mdev);
009ba89d
LE
1673 if ((int)retcode == (int)SS_IS_DISKLESS)
1674 retcode = SS_NOTHING_TO_DO;
1675 if (ret)
1676 retcode = ERR_INTR;
cdfda633 1677out:
85f75dd7
LE
1678 return retcode;
1679}
1680
82f59cc6
LE
1681/* Detaching the disk is a process in multiple stages. First we need to lock
1682 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1683 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1684 * internal references as well.
1685 * Only then we have finally detached. */
3b98c0c2 1686int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
b411b363 1687{
9a0d9d03 1688 enum drbd_ret_code retcode;
cdfda633
PR
1689 struct detach_parms parms = { };
1690 int err;
3b98c0c2
LE
1691
1692 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1693 if (!adm_ctx.reply_skb)
1694 return retcode;
1695 if (retcode != NO_ERROR)
1696 goto out;
1697
cdfda633
PR
1698 if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
1699 err = detach_parms_from_attrs(&parms, info);
1700 if (err) {
1701 retcode = ERR_MANDATORY_TAG;
1702 drbd_msg_put_info(from_attrs_err_to_txt(err));
1703 goto out;
1704 }
1705 }
1706
1707 retcode = adm_detach(adm_ctx.mdev, parms.force_detach);
3b98c0c2
LE
1708out:
1709 drbd_adm_finish(info, retcode);
b411b363
PR
1710 return 0;
1711}
1712
f399002e
LE
1713static bool conn_resync_running(struct drbd_tconn *tconn)
1714{
1715 struct drbd_conf *mdev;
695d08fa 1716 bool rv = false;
f399002e
LE
1717 int vnr;
1718
695d08fa 1719 rcu_read_lock();
f399002e
LE
1720 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1721 if (mdev->state.conn == C_SYNC_SOURCE ||
1722 mdev->state.conn == C_SYNC_TARGET ||
1723 mdev->state.conn == C_PAUSED_SYNC_S ||
695d08fa
PR
1724 mdev->state.conn == C_PAUSED_SYNC_T) {
1725 rv = true;
1726 break;
1727 }
f399002e 1728 }
695d08fa
PR
1729 rcu_read_unlock();
1730
1731 return rv;
f399002e
LE
1732}
1733
1734static bool conn_ov_running(struct drbd_tconn *tconn)
1735{
1736 struct drbd_conf *mdev;
695d08fa 1737 bool rv = false;
f399002e
LE
1738 int vnr;
1739
695d08fa 1740 rcu_read_lock();
f399002e
LE
1741 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1742 if (mdev->state.conn == C_VERIFY_S ||
695d08fa
PR
1743 mdev->state.conn == C_VERIFY_T) {
1744 rv = true;
1745 break;
1746 }
f399002e 1747 }
695d08fa
PR
1748 rcu_read_unlock();
1749
1750 return rv;
f399002e
LE
1751}
1752
cd64397c 1753static enum drbd_ret_code
44ed167d 1754_check_net_options(struct drbd_tconn *tconn, struct net_conf *old_conf, struct net_conf *new_conf)
cd64397c
PR
1755{
1756 struct drbd_conf *mdev;
1757 int i;
1758
dcb20d1a
PR
1759 if (old_conf && tconn->cstate == C_WF_REPORT_PARAMS && tconn->agreed_pro_version < 100) {
1760 if (new_conf->wire_protocol != old_conf->wire_protocol)
1761 return ERR_NEED_APV_100;
1762
1763 if (new_conf->two_primaries != old_conf->two_primaries)
1764 return ERR_NEED_APV_100;
1765
1766 if (!new_conf->integrity_alg != !old_conf->integrity_alg)
1767 return ERR_NEED_APV_100;
1768
1769 if (strcmp(new_conf->integrity_alg, old_conf->integrity_alg))
1770 return ERR_NEED_APV_100;
1771 }
1772
1773 if (!new_conf->two_primaries &&
1774 conn_highest_role(tconn) == R_PRIMARY &&
1775 conn_highest_peer(tconn) == R_PRIMARY)
1776 return ERR_NEED_ALLOW_TWO_PRI;
b032b6fa 1777
cd64397c
PR
1778 if (new_conf->two_primaries &&
1779 (new_conf->wire_protocol != DRBD_PROT_C))
1780 return ERR_NOT_PROTO_C;
1781
cd64397c
PR
1782 idr_for_each_entry(&tconn->volumes, mdev, i) {
1783 if (get_ldev(mdev)) {
daeda1cc 1784 enum drbd_fencing_p fp = rcu_dereference(mdev->ldev->disk_conf)->fencing;
cd64397c 1785 put_ldev(mdev);
44ed167d 1786 if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
cd64397c 1787 return ERR_STONITH_AND_PROT_A;
cd64397c 1788 }
6139f60d 1789 if (mdev->state.role == R_PRIMARY && new_conf->discard_my_data)
cd64397c 1790 return ERR_DISCARD;
cd64397c 1791 }
cd64397c
PR
1792
1793 if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A)
1794 return ERR_CONG_NOT_PROTO_A;
1795
1796 return NO_ERROR;
1797}
1798
44ed167d
PR
1799static enum drbd_ret_code
1800check_net_options(struct drbd_tconn *tconn, struct net_conf *new_conf)
1801{
1802 static enum drbd_ret_code rv;
1803 struct drbd_conf *mdev;
1804 int i;
1805
1806 rcu_read_lock();
1807 rv = _check_net_options(tconn, rcu_dereference(tconn->net_conf), new_conf);
1808 rcu_read_unlock();
1809
1810 /* tconn->volumes protected by genl_lock() here */
1811 idr_for_each_entry(&tconn->volumes, mdev, i) {
1812 if (!mdev->bitmap) {
1813 if(drbd_bm_init(mdev))
1814 return ERR_NOMEM;
1815 }
1816 }
1817
1818 return rv;
1819}
1820
0fd0ea06
PR
1821struct crypto {
1822 struct crypto_hash *verify_tfm;
1823 struct crypto_hash *csums_tfm;
1824 struct crypto_hash *cram_hmac_tfm;
8d412fc6 1825 struct crypto_hash *integrity_tfm;
0fd0ea06
PR
1826 void *int_dig_in;
1827 void *int_dig_vv;
1828};
1829
1830static int
4b6ad6d4 1831alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
0fd0ea06
PR
1832{
1833 if (!tfm_name[0])
1834 return NO_ERROR;
1835
1836 *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
1837 if (IS_ERR(*tfm)) {
1838 *tfm = NULL;
1839 return err_alg;
1840 }
1841
0fd0ea06
PR
1842 return NO_ERROR;
1843}
1844
1845static enum drbd_ret_code
1846alloc_crypto(struct crypto *crypto, struct net_conf *new_conf)
1847{
1848 char hmac_name[CRYPTO_MAX_ALG_NAME];
1849 enum drbd_ret_code rv;
1850 int hash_size;
1851
4b6ad6d4
AG
1852 rv = alloc_hash(&crypto->csums_tfm, new_conf->csums_alg,
1853 ERR_CSUMS_ALG);
0fd0ea06
PR
1854 if (rv != NO_ERROR)
1855 return rv;
4b6ad6d4
AG
1856 rv = alloc_hash(&crypto->verify_tfm, new_conf->verify_alg,
1857 ERR_VERIFY_ALG);
0fd0ea06
PR
1858 if (rv != NO_ERROR)
1859 return rv;
4b6ad6d4
AG
1860 rv = alloc_hash(&crypto->integrity_tfm, new_conf->integrity_alg,
1861 ERR_INTEGRITY_ALG);
0fd0ea06
PR
1862 if (rv != NO_ERROR)
1863 return rv;
0fd0ea06
PR
1864 if (new_conf->cram_hmac_alg[0] != 0) {
1865 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
1866 new_conf->cram_hmac_alg);
1867
4b6ad6d4
AG
1868 rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
1869 ERR_AUTH_ALG);
0fd0ea06 1870 }
8d412fc6
AG
1871 if (crypto->integrity_tfm) {
1872 hash_size = crypto_hash_digestsize(crypto->integrity_tfm);
0fd0ea06
PR
1873 crypto->int_dig_in = kmalloc(hash_size, GFP_KERNEL);
1874 if (!crypto->int_dig_in)
1875 return ERR_NOMEM;
1876 crypto->int_dig_vv = kmalloc(hash_size, GFP_KERNEL);
1877 if (!crypto->int_dig_vv)
1878 return ERR_NOMEM;
1879 }
1880
1881 return rv;
1882}
1883
1884static void free_crypto(struct crypto *crypto)
1885{
1886 kfree(crypto->int_dig_in);
1887 kfree(crypto->int_dig_vv);
1888 crypto_free_hash(crypto->cram_hmac_tfm);
8d412fc6 1889 crypto_free_hash(crypto->integrity_tfm);
0fd0ea06
PR
1890 crypto_free_hash(crypto->csums_tfm);
1891 crypto_free_hash(crypto->verify_tfm);
1892}
1893
f399002e
LE
1894int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
1895{
1896 enum drbd_ret_code retcode;
1897 struct drbd_tconn *tconn;
44ed167d 1898 struct net_conf *old_conf, *new_conf = NULL;
f399002e
LE
1899 int err;
1900 int ovr; /* online verify running */
1901 int rsr; /* re-sync running */
0fd0ea06 1902 struct crypto crypto = { };
f399002e 1903
089c075d 1904 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
f399002e
LE
1905 if (!adm_ctx.reply_skb)
1906 return retcode;
1907 if (retcode != NO_ERROR)
1908 goto out;
1909
1910 tconn = adm_ctx.tconn;
1911
1912 new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
1913 if (!new_conf) {
1914 retcode = ERR_NOMEM;
1915 goto out;
1916 }
1917
44ed167d
PR
1918 conn_reconfig_start(tconn);
1919
88104ca4 1920 mutex_lock(&tconn->data.mutex);
a0095508 1921 mutex_lock(&tconn->conf_update);
91fd4dad 1922 old_conf = tconn->net_conf;
44ed167d
PR
1923
1924 if (!old_conf) {
f399002e
LE
1925 drbd_msg_put_info("net conf missing, try connect");
1926 retcode = ERR_INVALID_REQUEST;
91fd4dad 1927 goto fail;
f399002e
LE
1928 }
1929
44ed167d 1930 *new_conf = *old_conf;
5979e361 1931 if (should_set_defaults(info))
b966b5dd 1932 set_net_conf_defaults(new_conf);
f399002e 1933
f399002e 1934 err = net_conf_from_attrs_for_change(new_conf, info);
c75b9b10 1935 if (err && err != -ENOMSG) {
f399002e
LE
1936 retcode = ERR_MANDATORY_TAG;
1937 drbd_msg_put_info(from_attrs_err_to_txt(err));
1938 goto fail;
1939 }
1940
cd64397c
PR
1941 retcode = check_net_options(tconn, new_conf);
1942 if (retcode != NO_ERROR)
1943 goto fail;
1944
f399002e
LE
1945 /* re-sync running */
1946 rsr = conn_resync_running(tconn);
0fd0ea06 1947 if (rsr && strcmp(new_conf->csums_alg, old_conf->csums_alg)) {
f399002e 1948 retcode = ERR_CSUMS_RESYNC_RUNNING;
91fd4dad 1949 goto fail;
f399002e
LE
1950 }
1951
f399002e
LE
1952 /* online verify running */
1953 ovr = conn_ov_running(tconn);
0fd0ea06
PR
1954 if (ovr && strcmp(new_conf->verify_alg, old_conf->verify_alg)) {
1955 retcode = ERR_VERIFY_RUNNING;
1956 goto fail;
f399002e
LE
1957 }
1958
0fd0ea06
PR
1959 retcode = alloc_crypto(&crypto, new_conf);
1960 if (retcode != NO_ERROR)
1961 goto fail;
f399002e 1962
44ed167d 1963 rcu_assign_pointer(tconn->net_conf, new_conf);
f399002e
LE
1964
1965 if (!rsr) {
1966 crypto_free_hash(tconn->csums_tfm);
0fd0ea06
PR
1967 tconn->csums_tfm = crypto.csums_tfm;
1968 crypto.csums_tfm = NULL;
f399002e
LE
1969 }
1970 if (!ovr) {
1971 crypto_free_hash(tconn->verify_tfm);
0fd0ea06
PR
1972 tconn->verify_tfm = crypto.verify_tfm;
1973 crypto.verify_tfm = NULL;
f399002e
LE
1974 }
1975
0fd0ea06
PR
1976 kfree(tconn->int_dig_in);
1977 tconn->int_dig_in = crypto.int_dig_in;
1978 kfree(tconn->int_dig_vv);
1979 tconn->int_dig_vv = crypto.int_dig_vv;
8d412fc6
AG
1980 crypto_free_hash(tconn->integrity_tfm);
1981 tconn->integrity_tfm = crypto.integrity_tfm;
d659f2aa 1982 if (tconn->cstate >= C_WF_REPORT_PARAMS && tconn->agreed_pro_version >= 100)
88104ca4 1983 /* Do this without trying to take tconn->data.mutex again. */
d659f2aa 1984 __drbd_send_protocol(tconn, P_PROTOCOL_UPDATE);
0fd0ea06 1985
0fd0ea06
PR
1986 crypto_free_hash(tconn->cram_hmac_tfm);
1987 tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
1988
a0095508 1989 mutex_unlock(&tconn->conf_update);
88104ca4 1990 mutex_unlock(&tconn->data.mutex);
91fd4dad
PR
1991 synchronize_rcu();
1992 kfree(old_conf);
1993
f399002e
LE
1994 if (tconn->cstate >= C_WF_REPORT_PARAMS)
1995 drbd_send_sync_param(minor_to_mdev(conn_lowest_minor(tconn)));
1996
91fd4dad
PR
1997 goto done;
1998
f399002e 1999 fail:
a0095508 2000 mutex_unlock(&tconn->conf_update);
88104ca4 2001 mutex_unlock(&tconn->data.mutex);
0fd0ea06 2002 free_crypto(&crypto);
f399002e 2003 kfree(new_conf);
91fd4dad 2004 done:
f399002e
LE
2005 conn_reconfig_done(tconn);
2006 out:
2007 drbd_adm_finish(info, retcode);
2008 return 0;
2009}
2010
3b98c0c2 2011int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
b411b363 2012{
3b98c0c2 2013 struct drbd_conf *mdev;
44ed167d 2014 struct net_conf *old_conf, *new_conf = NULL;
0fd0ea06 2015 struct crypto crypto = { };
3b98c0c2 2016 struct drbd_tconn *tconn;
3b98c0c2
LE
2017 enum drbd_ret_code retcode;
2018 int i;
2019 int err;
b411b363 2020
44e52cfa 2021 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
089c075d 2022
3b98c0c2
LE
2023 if (!adm_ctx.reply_skb)
2024 return retcode;
2025 if (retcode != NO_ERROR)
2026 goto out;
089c075d
AG
2027 if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
2028 drbd_msg_put_info("connection endpoint(s) missing");
2029 retcode = ERR_INVALID_REQUEST;
2030 goto out;
2031 }
2032
2033 /* No need for _rcu here. All reconfiguration is
2034 * strictly serialized on genl_lock(). We are protected against
2035 * concurrent reconfiguration/addition/deletion */
2036 list_for_each_entry(tconn, &drbd_tconns, all_tconn) {
2037 if (nla_len(adm_ctx.my_addr) == tconn->my_addr_len &&
2038 !memcmp(nla_data(adm_ctx.my_addr), &tconn->my_addr, tconn->my_addr_len)) {
2039 retcode = ERR_LOCAL_ADDR;
2040 goto out;
2041 }
2042
2043 if (nla_len(adm_ctx.peer_addr) == tconn->peer_addr_len &&
2044 !memcmp(nla_data(adm_ctx.peer_addr), &tconn->peer_addr, tconn->peer_addr_len)) {
2045 retcode = ERR_PEER_ADDR;
2046 goto out;
2047 }
2048 }
3b98c0c2
LE
2049
2050 tconn = adm_ctx.tconn;
80883197 2051 conn_reconfig_start(tconn);
b411b363 2052
80883197 2053 if (tconn->cstate > C_STANDALONE) {
b411b363
PR
2054 retcode = ERR_NET_CONFIGURED;
2055 goto fail;
2056 }
2057
2058 /* allocation not in the IO path, cqueue thread context */
5979e361 2059 new_conf = kzalloc(sizeof(*new_conf), GFP_KERNEL);
b411b363
PR
2060 if (!new_conf) {
2061 retcode = ERR_NOMEM;
2062 goto fail;
2063 }
2064
b966b5dd 2065 set_net_conf_defaults(new_conf);
f399002e
LE
2066
2067 err = net_conf_from_attrs(new_conf, info);
3b98c0c2 2068 if (err) {
b411b363 2069 retcode = ERR_MANDATORY_TAG;
3b98c0c2 2070 drbd_msg_put_info(from_attrs_err_to_txt(err));
b411b363
PR
2071 goto fail;
2072 }
2073
cd64397c
PR
2074 retcode = check_net_options(tconn, new_conf);
2075 if (retcode != NO_ERROR)
422028b1 2076 goto fail;
422028b1 2077
0fd0ea06
PR
2078 retcode = alloc_crypto(&crypto, new_conf);
2079 if (retcode != NO_ERROR)
2080 goto fail;
b411b363 2081
b411b363
PR
2082 ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
2083
80883197 2084 conn_flush_workqueue(tconn);
91fd4dad 2085
a0095508 2086 mutex_lock(&tconn->conf_update);
91fd4dad
PR
2087 old_conf = tconn->net_conf;
2088 if (old_conf) {
b411b363 2089 retcode = ERR_NET_CONFIGURED;
a0095508 2090 mutex_unlock(&tconn->conf_update);
b411b363
PR
2091 goto fail;
2092 }
44ed167d 2093 rcu_assign_pointer(tconn->net_conf, new_conf);
b411b363 2094
91fd4dad 2095 conn_free_crypto(tconn);
0fd0ea06
PR
2096 tconn->int_dig_in = crypto.int_dig_in;
2097 tconn->int_dig_vv = crypto.int_dig_vv;
2098 tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
8d412fc6 2099 tconn->integrity_tfm = crypto.integrity_tfm;
0fd0ea06
PR
2100 tconn->csums_tfm = crypto.csums_tfm;
2101 tconn->verify_tfm = crypto.verify_tfm;
b411b363 2102
089c075d
AG
2103 tconn->my_addr_len = nla_len(adm_ctx.my_addr);
2104 memcpy(&tconn->my_addr, nla_data(adm_ctx.my_addr), tconn->my_addr_len);
2105 tconn->peer_addr_len = nla_len(adm_ctx.peer_addr);
2106 memcpy(&tconn->peer_addr, nla_data(adm_ctx.peer_addr), tconn->peer_addr_len);
2107
a0095508 2108 mutex_unlock(&tconn->conf_update);
91fd4dad 2109
695d08fa 2110 rcu_read_lock();
80883197
PR
2111 idr_for_each_entry(&tconn->volumes, mdev, i) {
2112 mdev->send_cnt = 0;
2113 mdev->recv_cnt = 0;
80883197 2114 }
695d08fa 2115 rcu_read_unlock();
5ee743e9
LE
2116
2117 retcode = conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
2118
80883197 2119 conn_reconfig_done(tconn);
3b98c0c2 2120 drbd_adm_finish(info, retcode);
b411b363
PR
2121 return 0;
2122
2123fail:
0fd0ea06 2124 free_crypto(&crypto);
b411b363
PR
2125 kfree(new_conf);
2126
80883197 2127 conn_reconfig_done(tconn);
3b98c0c2
LE
2128out:
2129 drbd_adm_finish(info, retcode);
b411b363
PR
2130 return 0;
2131}
2132
85f75dd7
LE
2133static enum drbd_state_rv conn_try_disconnect(struct drbd_tconn *tconn, bool force)
2134{
2135 enum drbd_state_rv rv;
85f75dd7 2136
f3dfa40a
LE
2137 rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
2138 force ? CS_HARD : 0);
85f75dd7
LE
2139
2140 switch (rv) {
2141 case SS_NOTHING_TO_DO:
f3dfa40a 2142 break;
85f75dd7
LE
2143 case SS_ALREADY_STANDALONE:
2144 return SS_SUCCESS;
2145 case SS_PRIMARY_NOP:
2146 /* Our state checking code wants to see the peer outdated. */
2147 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
f3dfa40a 2148 pdsk, D_OUTDATED), CS_VERBOSE);
85f75dd7
LE
2149 break;
2150 case SS_CW_FAILED_BY_PEER:
2151 /* The peer probably wants to see us outdated. */
2152 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
2153 disk, D_OUTDATED), 0);
2154 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
f3dfa40a
LE
2155 rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
2156 CS_HARD);
85f75dd7
LE
2157 }
2158 break;
2159 default:;
2160 /* no special handling necessary */
2161 }
2162
f3dfa40a
LE
2163 if (rv >= SS_SUCCESS) {
2164 enum drbd_state_rv rv2;
2165 /* No one else can reconfigure the network while I am here.
2166 * The state handling only uses drbd_thread_stop_nowait(),
2167 * we want to really wait here until the receiver is no more.
2168 */
2169 drbd_thread_stop(&adm_ctx.tconn->receiver);
2170
2171 /* Race breaker. This additional state change request may be
2172 * necessary, if this was a forced disconnect during a receiver
2173 * restart. We may have "killed" the receiver thread just
2174 * after drbdd_init() returned. Typically, we should be
2175 * C_STANDALONE already, now, and this becomes a no-op.
2176 */
2177 rv2 = conn_request_state(tconn, NS(conn, C_STANDALONE),
2178 CS_VERBOSE | CS_HARD);
2179 if (rv2 < SS_SUCCESS)
2180 conn_err(tconn,
2181 "unexpected rv2=%d in conn_try_disconnect()\n",
2182 rv2);
2183 }
85f75dd7
LE
2184 return rv;
2185}
2186
3b98c0c2 2187int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
b411b363 2188{
3b98c0c2
LE
2189 struct disconnect_parms parms;
2190 struct drbd_tconn *tconn;
85f75dd7 2191 enum drbd_state_rv rv;
3b98c0c2
LE
2192 enum drbd_ret_code retcode;
2193 int err;
2561b9c1 2194
089c075d 2195 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
3b98c0c2
LE
2196 if (!adm_ctx.reply_skb)
2197 return retcode;
2198 if (retcode != NO_ERROR)
2561b9c1 2199 goto fail;
3b98c0c2
LE
2200
2201 tconn = adm_ctx.tconn;
2202 memset(&parms, 0, sizeof(parms));
2203 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
f399002e 2204 err = disconnect_parms_from_attrs(&parms, info);
3b98c0c2
LE
2205 if (err) {
2206 retcode = ERR_MANDATORY_TAG;
2207 drbd_msg_put_info(from_attrs_err_to_txt(err));
2208 goto fail;
2209 }
2561b9c1
PR
2210 }
2211
85f75dd7
LE
2212 rv = conn_try_disconnect(tconn, parms.force_disconnect);
2213 if (rv < SS_SUCCESS)
f3dfa40a
LE
2214 retcode = rv; /* FIXME: Type mismatch. */
2215 else
2216 retcode = NO_ERROR;
b411b363 2217 fail:
3b98c0c2 2218 drbd_adm_finish(info, retcode);
b411b363
PR
2219 return 0;
2220}
2221
2222void resync_after_online_grow(struct drbd_conf *mdev)
2223{
2224 int iass; /* I am sync source */
2225
2226 dev_info(DEV, "Resync of new storage after online grow\n");
2227 if (mdev->state.role != mdev->state.peer)
2228 iass = (mdev->state.role == R_PRIMARY);
2229 else
25703f83 2230 iass = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags);
b411b363
PR
2231
2232 if (iass)
2233 drbd_start_resync(mdev, C_SYNC_SOURCE);
2234 else
2235 _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
2236}
2237
3b98c0c2 2238int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
b411b363 2239{
daeda1cc 2240 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
3b98c0c2
LE
2241 struct resize_parms rs;
2242 struct drbd_conf *mdev;
2243 enum drbd_ret_code retcode;
b411b363 2244 enum determine_dev_size dd;
6495d2c6 2245 enum dds_flags ddsf;
daeda1cc 2246 sector_t u_size;
3b98c0c2 2247 int err;
b411b363 2248
3b98c0c2
LE
2249 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2250 if (!adm_ctx.reply_skb)
2251 return retcode;
2252 if (retcode != NO_ERROR)
b411b363 2253 goto fail;
3b98c0c2
LE
2254
2255 memset(&rs, 0, sizeof(struct resize_parms));
2256 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
f399002e 2257 err = resize_parms_from_attrs(&rs, info);
3b98c0c2
LE
2258 if (err) {
2259 retcode = ERR_MANDATORY_TAG;
2260 drbd_msg_put_info(from_attrs_err_to_txt(err));
2261 goto fail;
2262 }
b411b363
PR
2263 }
2264
3b98c0c2 2265 mdev = adm_ctx.mdev;
b411b363
PR
2266 if (mdev->state.conn > C_CONNECTED) {
2267 retcode = ERR_RESIZE_RESYNC;
2268 goto fail;
2269 }
2270
2271 if (mdev->state.role == R_SECONDARY &&
2272 mdev->state.peer == R_SECONDARY) {
2273 retcode = ERR_NO_PRIMARY;
2274 goto fail;
2275 }
2276
2277 if (!get_ldev(mdev)) {
2278 retcode = ERR_NO_DISK;
2279 goto fail;
2280 }
2281
31890f4a 2282 if (rs.no_resync && mdev->tconn->agreed_pro_version < 93) {
6495d2c6
PR
2283 retcode = ERR_NEED_APV_93;
2284 goto fail;
2285 }
2286
daeda1cc
PR
2287 rcu_read_lock();
2288 u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
2289 rcu_read_unlock();
2290 if (u_size != (sector_t)rs.resize_size) {
2291 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
2292 if (!new_disk_conf) {
2293 retcode = ERR_NOMEM;
2294 goto fail;
2295 }
2296 }
2297
087c2492 2298 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev))
b411b363 2299 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
b411b363 2300
daeda1cc
PR
2301 if (new_disk_conf) {
2302 mutex_lock(&mdev->tconn->conf_update);
2303 old_disk_conf = mdev->ldev->disk_conf;
2304 *new_disk_conf = *old_disk_conf;
2305 new_disk_conf->disk_size = (sector_t)rs.resize_size;
2306 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
2307 mutex_unlock(&mdev->tconn->conf_update);
2308 synchronize_rcu();
2309 kfree(old_disk_conf);
2310 }
2311
6495d2c6 2312 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
24c4830c 2313 dd = drbd_determine_dev_size(mdev, ddsf);
b411b363
PR
2314 drbd_md_sync(mdev);
2315 put_ldev(mdev);
2316 if (dd == dev_size_error) {
2317 retcode = ERR_NOMEM_BITMAP;
2318 goto fail;
2319 }
2320
087c2492 2321 if (mdev->state.conn == C_CONNECTED) {
b411b363
PR
2322 if (dd == grew)
2323 set_bit(RESIZE_PENDING, &mdev->flags);
2324
2325 drbd_send_uuids(mdev);
6495d2c6 2326 drbd_send_sizes(mdev, 1, ddsf);
b411b363
PR
2327 }
2328
2329 fail:
3b98c0c2 2330 drbd_adm_finish(info, retcode);
b411b363
PR
2331 return 0;
2332}
2333
f399002e 2334int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
b411b363 2335{
3b98c0c2 2336 enum drbd_ret_code retcode;
f399002e 2337 struct drbd_tconn *tconn;
b57a1e27 2338 struct res_opts res_opts;
f399002e 2339 int err;
b411b363 2340
44e52cfa 2341 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
3b98c0c2
LE
2342 if (!adm_ctx.reply_skb)
2343 return retcode;
2344 if (retcode != NO_ERROR)
2345 goto fail;
f399002e 2346 tconn = adm_ctx.tconn;
3b98c0c2 2347
b57a1e27 2348 res_opts = tconn->res_opts;
5979e361 2349 if (should_set_defaults(info))
b966b5dd 2350 set_res_opts_defaults(&res_opts);
b411b363 2351
b57a1e27 2352 err = res_opts_from_attrs(&res_opts, info);
c75b9b10 2353 if (err && err != -ENOMSG) {
b411b363 2354 retcode = ERR_MANDATORY_TAG;
3b98c0c2 2355 drbd_msg_put_info(from_attrs_err_to_txt(err));
b411b363
PR
2356 goto fail;
2357 }
2358
afbbfa88
AG
2359 err = set_resource_options(tconn, &res_opts);
2360 if (err) {
2361 retcode = ERR_INVALID_REQUEST;
2362 if (err == -ENOMEM)
2363 retcode = ERR_NOMEM;
b411b363
PR
2364 }
2365
b411b363 2366fail:
3b98c0c2 2367 drbd_adm_finish(info, retcode);
b411b363
PR
2368 return 0;
2369}
2370
3b98c0c2 2371int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
b411b363 2372{
3b98c0c2
LE
2373 struct drbd_conf *mdev;
2374 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2375
2376 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2377 if (!adm_ctx.reply_skb)
2378 return retcode;
2379 if (retcode != NO_ERROR)
2380 goto out;
2381
2382 mdev = adm_ctx.mdev;
b411b363 2383
194bfb32
LE
2384 /* If there is still bitmap IO pending, probably because of a previous
2385 * resync just being finished, wait for it before requesting a new resync. */
2386 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
2387
b411b363
PR
2388 retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T), CS_ORDERED);
2389
2390 if (retcode < SS_SUCCESS && retcode != SS_NEED_CONNECTION)
2391 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
2392
2393 while (retcode == SS_NEED_CONNECTION) {
87eeee41 2394 spin_lock_irq(&mdev->tconn->req_lock);
b411b363
PR
2395 if (mdev->state.conn < C_CONNECTED)
2396 retcode = _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_VERBOSE, NULL);
87eeee41 2397 spin_unlock_irq(&mdev->tconn->req_lock);
b411b363
PR
2398
2399 if (retcode != SS_NEED_CONNECTION)
2400 break;
2401
2402 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
2403 }
2404
3b98c0c2
LE
2405out:
2406 drbd_adm_finish(info, retcode);
b411b363
PR
2407 return 0;
2408}
2409
0778286a
PR
2410static int drbd_bmio_set_susp_al(struct drbd_conf *mdev)
2411{
2412 int rv;
2413
2414 rv = drbd_bmio_set_n_write(mdev);
2415 drbd_suspend_al(mdev);
2416 return rv;
2417}
2418
3b98c0c2
LE
2419static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2420 union drbd_state mask, union drbd_state val)
b411b363 2421{
3b98c0c2 2422 enum drbd_ret_code retcode;
194bfb32 2423
3b98c0c2
LE
2424 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2425 if (!adm_ctx.reply_skb)
2426 return retcode;
2427 if (retcode != NO_ERROR)
2428 goto out;
b411b363 2429
3b98c0c2
LE
2430 retcode = drbd_request_state(adm_ctx.mdev, mask, val);
2431out:
2432 drbd_adm_finish(info, retcode);
b411b363
PR
2433 return 0;
2434}
2435
3b98c0c2 2436int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
b411b363 2437{
3b98c0c2
LE
2438 return drbd_adm_simple_request_state(skb, info, NS(conn, C_STARTING_SYNC_S));
2439}
b411b363 2440
3b98c0c2
LE
2441int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
2442{
2443 enum drbd_ret_code retcode;
2444
2445 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2446 if (!adm_ctx.reply_skb)
2447 return retcode;
2448 if (retcode != NO_ERROR)
2449 goto out;
b411b363 2450
3b98c0c2
LE
2451 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
2452 retcode = ERR_PAUSE_IS_SET;
2453out:
2454 drbd_adm_finish(info, retcode);
b411b363
PR
2455 return 0;
2456}
2457
3b98c0c2 2458int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
b411b363 2459{
da9fbc27 2460 union drbd_dev_state s;
3b98c0c2
LE
2461 enum drbd_ret_code retcode;
2462
2463 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2464 if (!adm_ctx.reply_skb)
2465 return retcode;
2466 if (retcode != NO_ERROR)
2467 goto out;
b411b363 2468
3b98c0c2
LE
2469 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2470 s = adm_ctx.mdev->state;
cd88d030
PR
2471 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2472 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2473 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2474 } else {
2475 retcode = ERR_PAUSE_IS_CLEAR;
2476 }
2477 }
b411b363 2478
3b98c0c2
LE
2479out:
2480 drbd_adm_finish(info, retcode);
b411b363
PR
2481 return 0;
2482}
2483
3b98c0c2 2484int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
b411b363 2485{
3b98c0c2 2486 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
b411b363
PR
2487}
2488
3b98c0c2 2489int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
b411b363 2490{
3b98c0c2
LE
2491 struct drbd_conf *mdev;
2492 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2493
2494 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2495 if (!adm_ctx.reply_skb)
2496 return retcode;
2497 if (retcode != NO_ERROR)
2498 goto out;
2499
2500 mdev = adm_ctx.mdev;
43a5182c
PR
2501 if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
2502 drbd_uuid_new_current(mdev);
2503 clear_bit(NEW_CUR_UUID, &mdev->flags);
43a5182c 2504 }
265be2d0 2505 drbd_suspend_io(mdev);
3b98c0c2
LE
2506 retcode = drbd_request_state(mdev, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
2507 if (retcode == SS_SUCCESS) {
265be2d0 2508 if (mdev->state.conn < C_CONNECTED)
2f5cdd0b 2509 tl_clear(mdev->tconn);
265be2d0 2510 if (mdev->state.disk == D_DISKLESS || mdev->state.disk == D_FAILED)
2f5cdd0b 2511 tl_restart(mdev->tconn, FAIL_FROZEN_DISK_IO);
265be2d0
PR
2512 }
2513 drbd_resume_io(mdev);
2514
3b98c0c2
LE
2515out:
2516 drbd_adm_finish(info, retcode);
b411b363
PR
2517 return 0;
2518}
2519
3b98c0c2 2520int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
b411b363 2521{
3b98c0c2 2522 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
b411b363
PR
2523}
2524
089c075d 2525int nla_put_drbd_cfg_context(struct sk_buff *skb, struct drbd_tconn *tconn, unsigned vnr)
543cc10b
LE
2526{
2527 struct nlattr *nla;
2528 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
2529 if (!nla)
2530 goto nla_put_failure;
2531 if (vnr != VOLUME_UNSPECIFIED)
2532 NLA_PUT_U32(skb, T_ctx_volume, vnr);
089c075d
AG
2533 NLA_PUT_STRING(skb, T_ctx_resource_name, tconn->name);
2534 if (tconn->my_addr_len)
2535 NLA_PUT(skb, T_ctx_my_addr, tconn->my_addr_len, &tconn->my_addr);
2536 if (tconn->peer_addr_len)
2537 NLA_PUT(skb, T_ctx_peer_addr, tconn->peer_addr_len, &tconn->peer_addr);
543cc10b
LE
2538 nla_nest_end(skb, nla);
2539 return 0;
2540
2541nla_put_failure:
2542 if (nla)
2543 nla_nest_cancel(skb, nla);
2544 return -EMSGSIZE;
2545}
2546
3b98c0c2
LE
2547int nla_put_status_info(struct sk_buff *skb, struct drbd_conf *mdev,
2548 const struct sib_info *sib)
b411b363 2549{
3b98c0c2 2550 struct state_info *si = NULL; /* for sizeof(si->member); */
44ed167d 2551 struct net_conf *nc;
3b98c0c2
LE
2552 struct nlattr *nla;
2553 int got_ldev;
3b98c0c2
LE
2554 int err = 0;
2555 int exclude_sensitive;
2556
2557 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2558 * to. So we better exclude_sensitive information.
2559 *
2560 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2561 * in the context of the requesting user process. Exclude sensitive
2562 * information, unless current has superuser.
2563 *
2564 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2565 * relies on the current implementation of netlink_dump(), which
2566 * executes the dump callback successively from netlink_recvmsg(),
2567 * always in the context of the receiving process */
2568 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
2569
2570 got_ldev = get_ldev(mdev);
3b98c0c2
LE
2571
2572 /* We need to add connection name and volume number information still.
2573 * Minor number is in drbd_genlmsghdr. */
089c075d 2574 if (nla_put_drbd_cfg_context(skb, mdev->tconn, mdev->vnr))
3b98c0c2 2575 goto nla_put_failure;
3b98c0c2 2576
f399002e
LE
2577 if (res_opts_to_skb(skb, &mdev->tconn->res_opts, exclude_sensitive))
2578 goto nla_put_failure;
2579
daeda1cc 2580 rcu_read_lock();
3b98c0c2 2581 if (got_ldev)
daeda1cc 2582 if (disk_conf_to_skb(skb, rcu_dereference(mdev->ldev->disk_conf), exclude_sensitive))
3b98c0c2 2583 goto nla_put_failure;
44ed167d 2584
44ed167d
PR
2585 nc = rcu_dereference(mdev->tconn->net_conf);
2586 if (nc)
2587 err = net_conf_to_skb(skb, nc, exclude_sensitive);
2588 rcu_read_unlock();
2589 if (err)
2590 goto nla_put_failure;
3b98c0c2 2591
3b98c0c2
LE
2592 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
2593 if (!nla)
2594 goto nla_put_failure;
2595 NLA_PUT_U32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY);
2596 NLA_PUT_U32(skb, T_current_state, mdev->state.i);
2597 NLA_PUT_U64(skb, T_ed_uuid, mdev->ed_uuid);
2598 NLA_PUT_U64(skb, T_capacity, drbd_get_capacity(mdev->this_bdev));
2599
2600 if (got_ldev) {
2601 NLA_PUT_U32(skb, T_disk_flags, mdev->ldev->md.flags);
2602 NLA_PUT(skb, T_uuids, sizeof(si->uuids), mdev->ldev->md.uuid);
2603 NLA_PUT_U64(skb, T_bits_total, drbd_bm_bits(mdev));
2604 NLA_PUT_U64(skb, T_bits_oos, drbd_bm_total_weight(mdev));
2605 if (C_SYNC_SOURCE <= mdev->state.conn &&
2606 C_PAUSED_SYNC_T >= mdev->state.conn) {
2607 NLA_PUT_U64(skb, T_bits_rs_total, mdev->rs_total);
2608 NLA_PUT_U64(skb, T_bits_rs_failed, mdev->rs_failed);
2609 }
b411b363
PR
2610 }
2611
3b98c0c2
LE
2612 if (sib) {
2613 switch(sib->sib_reason) {
2614 case SIB_SYNC_PROGRESS:
2615 case SIB_GET_STATUS_REPLY:
2616 break;
2617 case SIB_STATE_CHANGE:
2618 NLA_PUT_U32(skb, T_prev_state, sib->os.i);
2619 NLA_PUT_U32(skb, T_new_state, sib->ns.i);
2620 break;
2621 case SIB_HELPER_POST:
2622 NLA_PUT_U32(skb,
2623 T_helper_exit_code, sib->helper_exit_code);
2624 /* fall through */
2625 case SIB_HELPER_PRE:
2626 NLA_PUT_STRING(skb, T_helper, sib->helper_name);
2627 break;
2628 }
b411b363 2629 }
3b98c0c2 2630 nla_nest_end(skb, nla);
b411b363 2631
3b98c0c2
LE
2632 if (0)
2633nla_put_failure:
2634 err = -EMSGSIZE;
2635 if (got_ldev)
2636 put_ldev(mdev);
3b98c0c2 2637 return err;
b411b363
PR
2638}
2639
3b98c0c2 2640int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
b411b363 2641{
3b98c0c2
LE
2642 enum drbd_ret_code retcode;
2643 int err;
b411b363 2644
3b98c0c2
LE
2645 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2646 if (!adm_ctx.reply_skb)
2647 return retcode;
2648 if (retcode != NO_ERROR)
2649 goto out;
b411b363 2650
3b98c0c2
LE
2651 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.mdev, NULL);
2652 if (err) {
2653 nlmsg_free(adm_ctx.reply_skb);
2654 return err;
b411b363 2655 }
3b98c0c2
LE
2656out:
2657 drbd_adm_finish(info, retcode);
2658 return 0;
b411b363
PR
2659}
2660
71932efc 2661int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
b411b363 2662{
3b98c0c2
LE
2663 struct drbd_conf *mdev;
2664 struct drbd_genlmsghdr *dh;
543cc10b
LE
2665 struct drbd_tconn *pos = (struct drbd_tconn*)cb->args[0];
2666 struct drbd_tconn *tconn = NULL;
2667 struct drbd_tconn *tmp;
2668 unsigned volume = cb->args[1];
2669
2670 /* Open coded, deferred, iteration:
2671 * list_for_each_entry_safe(tconn, tmp, &drbd_tconns, all_tconn) {
2672 * idr_for_each_entry(&tconn->volumes, mdev, i) {
2673 * ...
2674 * }
2675 * }
2676 * where tconn is cb->args[0];
2677 * and i is cb->args[1];
2678 *
71932efc
LE
2679 * cb->args[2] indicates if we shall loop over all resources,
2680 * or just dump all volumes of a single resource.
2681 *
3b98c0c2
LE
2682 * This may miss entries inserted after this dump started,
2683 * or entries deleted before they are reached.
543cc10b
LE
2684 *
2685 * We need to make sure the mdev won't disappear while
2686 * we are looking at it, and revalidate our iterators
2687 * on each iteration.
2688 */
3b98c0c2 2689
9dc9fbb3 2690 /* synchronize with conn_create()/conn_destroy() */
c141ebda 2691 rcu_read_lock();
543cc10b 2692 /* revalidate iterator position */
ec0bddbc 2693 list_for_each_entry_rcu(tmp, &drbd_tconns, all_tconn) {
543cc10b
LE
2694 if (pos == NULL) {
2695 /* first iteration */
2696 pos = tmp;
2697 tconn = pos;
2698 break;
2699 }
2700 if (tmp == pos) {
2701 tconn = pos;
2702 break;
2703 }
2704 }
2705 if (tconn) {
71932efc 2706next_tconn:
543cc10b
LE
2707 mdev = idr_get_next(&tconn->volumes, &volume);
2708 if (!mdev) {
2709 /* No more volumes to dump on this tconn.
2710 * Advance tconn iterator. */
ec0bddbc
PR
2711 pos = list_entry_rcu(tconn->all_tconn.next,
2712 struct drbd_tconn, all_tconn);
71932efc 2713 /* Did we dump any volume on this tconn yet? */
543cc10b 2714 if (volume != 0) {
71932efc
LE
2715 /* If we reached the end of the list,
2716 * or only a single resource dump was requested,
2717 * we are done. */
2718 if (&pos->all_tconn == &drbd_tconns || cb->args[2])
2719 goto out;
543cc10b 2720 volume = 0;
71932efc 2721 tconn = pos;
543cc10b
LE
2722 goto next_tconn;
2723 }
2724 }
2725
3b98c0c2
LE
2726 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).pid,
2727 cb->nlh->nlmsg_seq, &drbd_genl_family,
2728 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
2729 if (!dh)
543cc10b
LE
2730 goto out;
2731
2732 if (!mdev) {
2733 /* this is a tconn without a single volume */
2734 dh->minor = -1U;
2735 dh->ret_code = NO_ERROR;
089c075d 2736 if (nla_put_drbd_cfg_context(skb, tconn, VOLUME_UNSPECIFIED))
543cc10b
LE
2737 genlmsg_cancel(skb, dh);
2738 else
2739 genlmsg_end(skb, dh);
2740 goto out;
2741 }
3b98c0c2 2742
543cc10b
LE
2743 D_ASSERT(mdev->vnr == volume);
2744 D_ASSERT(mdev->tconn == tconn);
3b98c0c2 2745
543cc10b 2746 dh->minor = mdev_to_minor(mdev);
3b98c0c2
LE
2747 dh->ret_code = NO_ERROR;
2748
2749 if (nla_put_status_info(skb, mdev, NULL)) {
2750 genlmsg_cancel(skb, dh);
543cc10b 2751 goto out;
3b98c0c2
LE
2752 }
2753 genlmsg_end(skb, dh);
2754 }
b411b363 2755
543cc10b 2756out:
c141ebda 2757 rcu_read_unlock();
543cc10b
LE
2758 /* where to start the next iteration */
2759 cb->args[0] = (long)pos;
2760 cb->args[1] = (pos == tconn) ? volume + 1 : 0;
b411b363 2761
543cc10b
LE
2762 /* No more tconns/volumes/minors found results in an empty skb.
2763 * Which will terminate the dump. */
3b98c0c2 2764 return skb->len;
b411b363
PR
2765}
2766
71932efc
LE
2767/*
2768 * Request status of all resources, or of all volumes within a single resource.
2769 *
2770 * This is a dump, as the answer may not fit in a single reply skb otherwise.
2771 * Which means we cannot use the family->attrbuf or other such members, because
2772 * dump is NOT protected by the genl_lock(). During dump, we only have access
2773 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
2774 *
2775 * Once things are setup properly, we call into get_one_status().
2776 */
2777int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
2778{
2779 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
2780 struct nlattr *nla;
7c3063cc 2781 const char *resource_name;
71932efc 2782 struct drbd_tconn *tconn;
7c3063cc 2783 int maxtype;
71932efc
LE
2784
2785 /* Is this a followup call? */
2786 if (cb->args[0]) {
2787 /* ... of a single resource dump,
2788 * and the resource iterator has been advanced already? */
2789 if (cb->args[2] && cb->args[2] != cb->args[0])
2790 return 0; /* DONE. */
2791 goto dump;
2792 }
2793
2794 /* First call (from netlink_dump_start). We need to figure out
2795 * which resource(s) the user wants us to dump. */
2796 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
2797 nlmsg_attrlen(cb->nlh, hdrlen),
2798 DRBD_NLA_CFG_CONTEXT);
2799
2800 /* No explicit context given. Dump all. */
2801 if (!nla)
2802 goto dump;
7c3063cc
AG
2803 maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
2804 nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
2805 if (IS_ERR(nla))
2806 return PTR_ERR(nla);
71932efc
LE
2807 /* context given, but no name present? */
2808 if (!nla)
2809 return -EINVAL;
7c3063cc
AG
2810 resource_name = nla_data(nla);
2811 tconn = conn_get_by_name(resource_name);
0ace9dfa 2812
71932efc
LE
2813 if (!tconn)
2814 return -ENODEV;
2815
0ace9dfa
PR
2816 kref_put(&tconn->kref, &conn_destroy); /* get_one_status() (re)validates tconn by itself */
2817
71932efc
LE
2818 /* prime iterators, and set "filter" mode mark:
2819 * only dump this tconn. */
2820 cb->args[0] = (long)tconn;
2821 /* cb->args[1] = 0; passed in this way. */
2822 cb->args[2] = (long)tconn;
2823
2824dump:
2825 return get_one_status(skb, cb);
2826}
2827
3b98c0c2 2828int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
b411b363 2829{
3b98c0c2
LE
2830 enum drbd_ret_code retcode;
2831 struct timeout_parms tp;
2832 int err;
b411b363 2833
3b98c0c2
LE
2834 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2835 if (!adm_ctx.reply_skb)
2836 return retcode;
2837 if (retcode != NO_ERROR)
2838 goto out;
b411b363 2839
3b98c0c2
LE
2840 tp.timeout_type =
2841 adm_ctx.mdev->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
2842 test_bit(USE_DEGR_WFC_T, &adm_ctx.mdev->flags) ? UT_DEGRADED :
2843 UT_DEFAULT;
b411b363 2844
3b98c0c2
LE
2845 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
2846 if (err) {
2847 nlmsg_free(adm_ctx.reply_skb);
2848 return err;
2849 }
2850out:
2851 drbd_adm_finish(info, retcode);
2852 return 0;
b411b363
PR
2853}
2854
3b98c0c2 2855int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
b411b363 2856{
3b98c0c2
LE
2857 struct drbd_conf *mdev;
2858 enum drbd_ret_code retcode;
b411b363 2859
3b98c0c2
LE
2860 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2861 if (!adm_ctx.reply_skb)
2862 return retcode;
2863 if (retcode != NO_ERROR)
2864 goto out;
873b0d5f 2865
3b98c0c2
LE
2866 mdev = adm_ctx.mdev;
2867 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
2868 /* resume from last known position, if possible */
2869 struct start_ov_parms parms =
2870 { .ov_start_sector = mdev->ov_start_sector };
f399002e 2871 int err = start_ov_parms_from_attrs(&parms, info);
3b98c0c2
LE
2872 if (err) {
2873 retcode = ERR_MANDATORY_TAG;
2874 drbd_msg_put_info(from_attrs_err_to_txt(err));
2875 goto out;
2876 }
2877 /* w_make_ov_request expects position to be aligned */
2878 mdev->ov_start_sector = parms.ov_start_sector & ~BM_SECT_PER_BIT;
2879 }
873b0d5f
LE
2880 /* If there is still bitmap IO pending, e.g. previous resync or verify
2881 * just being finished, wait for it before requesting a new resync. */
2882 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
3b98c0c2
LE
2883 retcode = drbd_request_state(mdev,NS(conn,C_VERIFY_S));
2884out:
2885 drbd_adm_finish(info, retcode);
b411b363
PR
2886 return 0;
2887}
2888
2889
3b98c0c2 2890int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
b411b363 2891{
3b98c0c2
LE
2892 struct drbd_conf *mdev;
2893 enum drbd_ret_code retcode;
b411b363
PR
2894 int skip_initial_sync = 0;
2895 int err;
3b98c0c2 2896 struct new_c_uuid_parms args;
b411b363 2897
3b98c0c2
LE
2898 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2899 if (!adm_ctx.reply_skb)
2900 return retcode;
2901 if (retcode != NO_ERROR)
2902 goto out_nolock;
b411b363 2903
3b98c0c2
LE
2904 mdev = adm_ctx.mdev;
2905 memset(&args, 0, sizeof(args));
2906 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
f399002e 2907 err = new_c_uuid_parms_from_attrs(&args, info);
3b98c0c2
LE
2908 if (err) {
2909 retcode = ERR_MANDATORY_TAG;
2910 drbd_msg_put_info(from_attrs_err_to_txt(err));
2911 goto out_nolock;
2912 }
b411b363
PR
2913 }
2914
8410da8f 2915 mutex_lock(mdev->state_mutex); /* Protects us against serialized state changes. */
b411b363
PR
2916
2917 if (!get_ldev(mdev)) {
2918 retcode = ERR_NO_DISK;
2919 goto out;
2920 }
2921
2922 /* this is "skip initial sync", assume to be clean */
31890f4a 2923 if (mdev->state.conn == C_CONNECTED && mdev->tconn->agreed_pro_version >= 90 &&
b411b363
PR
2924 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
2925 dev_info(DEV, "Preparing to skip initial sync\n");
2926 skip_initial_sync = 1;
2927 } else if (mdev->state.conn != C_STANDALONE) {
2928 retcode = ERR_CONNECTED;
2929 goto out_dec;
2930 }
2931
2932 drbd_uuid_set(mdev, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
2933 drbd_uuid_new_current(mdev); /* New current, previous to UI_BITMAP */
2934
2935 if (args.clear_bm) {
20ceb2b2
LE
2936 err = drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
2937 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
b411b363
PR
2938 if (err) {
2939 dev_err(DEV, "Writing bitmap failed with %d\n",err);
2940 retcode = ERR_IO_MD_DISK;
2941 }
2942 if (skip_initial_sync) {
2943 drbd_send_uuids_skip_initial_sync(mdev);
2944 _drbd_uuid_set(mdev, UI_BITMAP, 0);
62b0da3a 2945 drbd_print_uuids(mdev, "cleared bitmap UUID");
87eeee41 2946 spin_lock_irq(&mdev->tconn->req_lock);
b411b363
PR
2947 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
2948 CS_VERBOSE, NULL);
87eeee41 2949 spin_unlock_irq(&mdev->tconn->req_lock);
b411b363
PR
2950 }
2951 }
2952
2953 drbd_md_sync(mdev);
2954out_dec:
2955 put_ldev(mdev);
2956out:
8410da8f 2957 mutex_unlock(mdev->state_mutex);
3b98c0c2
LE
2958out_nolock:
2959 drbd_adm_finish(info, retcode);
774b3055
PR
2960 return 0;
2961}
2962
3b98c0c2 2963static enum drbd_ret_code
7c3063cc 2964drbd_check_resource_name(const char *name)
774b3055 2965{
3b98c0c2 2966 if (!name || !name[0]) {
7c3063cc 2967 drbd_msg_put_info("resource name missing");
3b98c0c2 2968 return ERR_MANDATORY_TAG;
774b3055 2969 }
3b98c0c2
LE
2970 /* if we want to use these in sysfs/configfs/debugfs some day,
2971 * we must not allow slashes */
2972 if (strchr(name, '/')) {
7c3063cc 2973 drbd_msg_put_info("invalid resource name");
3b98c0c2 2974 return ERR_INVALID_REQUEST;
774b3055 2975 }
3b98c0c2 2976 return NO_ERROR;
774b3055
PR
2977}
2978
789c1b62 2979int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
b411b363 2980{
3b98c0c2 2981 enum drbd_ret_code retcode;
afbbfa88
AG
2982 struct res_opts res_opts;
2983 int err;
9f5180e5 2984
3b98c0c2
LE
2985 retcode = drbd_adm_prepare(skb, info, 0);
2986 if (!adm_ctx.reply_skb)
2987 return retcode;
2988 if (retcode != NO_ERROR)
2989 goto out;
b411b363 2990
afbbfa88
AG
2991 set_res_opts_defaults(&res_opts);
2992 err = res_opts_from_attrs(&res_opts, info);
2993 if (err && err != -ENOMSG) {
2994 retcode = ERR_MANDATORY_TAG;
2995 drbd_msg_put_info(from_attrs_err_to_txt(err));
2996 goto out;
2997 }
2998
7c3063cc 2999 retcode = drbd_check_resource_name(adm_ctx.resource_name);
3b98c0c2
LE
3000 if (retcode != NO_ERROR)
3001 goto out;
b411b363 3002
3b98c0c2 3003 if (adm_ctx.tconn) {
38f19616
LE
3004 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
3005 retcode = ERR_INVALID_REQUEST;
789c1b62 3006 drbd_msg_put_info("resource exists");
38f19616
LE
3007 }
3008 /* else: still NO_ERROR */
3b98c0c2 3009 goto out;
b411b363
PR
3010 }
3011
afbbfa88 3012 if (!conn_create(adm_ctx.resource_name, &res_opts))
b411b363 3013 retcode = ERR_NOMEM;
3b98c0c2
LE
3014out:
3015 drbd_adm_finish(info, retcode);
3016 return 0;
b411b363
PR
3017}
3018
3b98c0c2 3019int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info)
b411b363 3020{
3b98c0c2
LE
3021 struct drbd_genlmsghdr *dh = info->userhdr;
3022 enum drbd_ret_code retcode;
b411b363 3023
44e52cfa 3024 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
3b98c0c2
LE
3025 if (!adm_ctx.reply_skb)
3026 return retcode;
3027 if (retcode != NO_ERROR)
3028 goto out;
b411b363 3029
3b98c0c2
LE
3030 /* FIXME drop minor_count parameter, limit to MINORMASK */
3031 if (dh->minor >= minor_count) {
3032 drbd_msg_put_info("requested minor out of range");
3033 retcode = ERR_INVALID_REQUEST;
3034 goto out;
b411b363 3035 }
0c8e36d9 3036 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
3b98c0c2
LE
3037 drbd_msg_put_info("requested volume id out of range");
3038 retcode = ERR_INVALID_REQUEST;
3039 goto out;
b411b363 3040 }
b411b363 3041
38f19616
LE
3042 /* drbd_adm_prepare made sure already
3043 * that mdev->tconn and mdev->vnr match the request. */
3044 if (adm_ctx.mdev) {
3045 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
3046 retcode = ERR_MINOR_EXISTS;
3047 /* else: still NO_ERROR */
3048 goto out;
3049 }
3050
3b98c0c2
LE
3051 retcode = conn_new_minor(adm_ctx.tconn, dh->minor, adm_ctx.volume);
3052out:
3053 drbd_adm_finish(info, retcode);
3054 return 0;
b411b363
PR
3055}
3056
85f75dd7
LE
3057static enum drbd_ret_code adm_delete_minor(struct drbd_conf *mdev)
3058{
3059 if (mdev->state.disk == D_DISKLESS &&
3060 /* no need to be mdev->state.conn == C_STANDALONE &&
3061 * we may want to delete a minor from a live replication group.
3062 */
3063 mdev->state.role == R_SECONDARY) {
81fa2e67
PR
3064 idr_remove(&mdev->tconn->volumes, mdev->vnr);
3065 idr_remove(&minors, mdev_to_minor(mdev));
3066 del_gendisk(mdev->vdisk);
3067 synchronize_rcu();
3068 kref_put(&mdev->kref, &drbd_minor_destroy);
85f75dd7
LE
3069 return NO_ERROR;
3070 } else
3071 return ERR_MINOR_CONFIGURED;
3072}
3073
3b98c0c2 3074int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info)
b411b363 3075{
3b98c0c2 3076 enum drbd_ret_code retcode;
b411b363 3077
3b98c0c2
LE
3078 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3079 if (!adm_ctx.reply_skb)
3080 return retcode;
3081 if (retcode != NO_ERROR)
3082 goto out;
b411b363 3083
85f75dd7 3084 retcode = adm_delete_minor(adm_ctx.mdev);
85f75dd7
LE
3085out:
3086 drbd_adm_finish(info, retcode);
3087 return 0;
3088}
3089
3090int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
3091{
f3dfa40a 3092 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
85f75dd7
LE
3093 struct drbd_conf *mdev;
3094 unsigned i;
3095
3096 retcode = drbd_adm_prepare(skb, info, 0);
3097 if (!adm_ctx.reply_skb)
3098 return retcode;
3099 if (retcode != NO_ERROR)
3100 goto out;
3101
3102 if (!adm_ctx.tconn) {
789c1b62 3103 retcode = ERR_RES_NOT_KNOWN;
85f75dd7
LE
3104 goto out;
3105 }
3106
85f75dd7
LE
3107 /* demote */
3108 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3109 retcode = drbd_set_role(mdev, R_SECONDARY, 0);
3110 if (retcode < SS_SUCCESS) {
3111 drbd_msg_put_info("failed to demote");
c141ebda 3112 goto out;
85f75dd7
LE
3113 }
3114 }
3115
f3dfa40a
LE
3116 retcode = conn_try_disconnect(adm_ctx.tconn, 0);
3117 if (retcode < SS_SUCCESS) {
85f75dd7 3118 drbd_msg_put_info("failed to disconnect");
f3dfa40a 3119 goto out;
85f75dd7
LE
3120 }
3121
3122 /* detach */
3123 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
cdfda633 3124 retcode = adm_detach(mdev, 0);
f3dfa40a 3125 if (retcode < SS_SUCCESS) {
85f75dd7 3126 drbd_msg_put_info("failed to detach");
c141ebda 3127 goto out;
85f75dd7
LE
3128 }
3129 }
3130
f3dfa40a
LE
3131 /* If we reach this, all volumes (of this tconn) are Secondary,
3132 * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
c141ebda 3133 * actually stopped, state handling only does drbd_thread_stop_nowait(). */
f3dfa40a
LE
3134 drbd_thread_stop(&adm_ctx.tconn->worker);
3135
3136 /* Now, nothing can fail anymore */
3137
85f75dd7
LE
3138 /* delete volumes */
3139 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3140 retcode = adm_delete_minor(mdev);
3141 if (retcode != NO_ERROR) {
3142 /* "can not happen" */
3143 drbd_msg_put_info("failed to delete volume");
ef356262 3144 goto out;
85f75dd7
LE
3145 }
3146 }
3147
85f75dd7
LE
3148 /* delete connection */
3149 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
ec0bddbc
PR
3150 list_del_rcu(&adm_ctx.tconn->all_tconn);
3151 synchronize_rcu();
9dc9fbb3
PR
3152 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
3153
85f75dd7
LE
3154 retcode = NO_ERROR;
3155 } else {
3156 /* "can not happen" */
789c1b62 3157 retcode = ERR_RES_IN_USE;
85f75dd7 3158 drbd_msg_put_info("failed to delete connection");
85f75dd7 3159 }
ef356262 3160 goto out;
3b98c0c2
LE
3161out:
3162 drbd_adm_finish(info, retcode);
3163 return 0;
b411b363
PR
3164}
3165
789c1b62 3166int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
b411b363 3167{
3b98c0c2 3168 enum drbd_ret_code retcode;
b411b363 3169
44e52cfa 3170 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
3b98c0c2
LE
3171 if (!adm_ctx.reply_skb)
3172 return retcode;
3173 if (retcode != NO_ERROR)
3174 goto out;
3175
3176 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
ec0bddbc
PR
3177 list_del_rcu(&adm_ctx.tconn->all_tconn);
3178 synchronize_rcu();
9dc9fbb3
PR
3179 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
3180
3b98c0c2
LE
3181 retcode = NO_ERROR;
3182 } else {
789c1b62 3183 retcode = ERR_RES_IN_USE;
b411b363
PR
3184 }
3185
992d6e91
LE
3186 if (retcode == NO_ERROR)
3187 drbd_thread_stop(&adm_ctx.tconn->worker);
3b98c0c2
LE
3188out:
3189 drbd_adm_finish(info, retcode);
b411b363
PR
3190 return 0;
3191}
3192
3b98c0c2 3193void drbd_bcast_event(struct drbd_conf *mdev, const struct sib_info *sib)
b411b363 3194{
3b98c0c2
LE
3195 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
3196 struct sk_buff *msg;
3197 struct drbd_genlmsghdr *d_out;
3198 unsigned seq;
3199 int err = -ENOMEM;
3200
3201 seq = atomic_inc_return(&drbd_genl_seq);
3202 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
3203 if (!msg)
3204 goto failed;
3205
3206 err = -EMSGSIZE;
3207 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
3208 if (!d_out) /* cannot happen, but anyways. */
3209 goto nla_put_failure;
3210 d_out->minor = mdev_to_minor(mdev);
6f9b5f84 3211 d_out->ret_code = NO_ERROR;
3b98c0c2
LE
3212
3213 if (nla_put_status_info(msg, mdev, sib))
3214 goto nla_put_failure;
3215 genlmsg_end(msg, d_out);
3216 err = drbd_genl_multicast_events(msg, 0);
3217 /* msg has been consumed or freed in netlink_broadcast() */
3218 if (err && err != -ESRCH)
3219 goto failed;
b411b363 3220
3b98c0c2 3221 return;
b411b363 3222
3b98c0c2
LE
3223nla_put_failure:
3224 nlmsg_free(msg);
3225failed:
3226 dev_err(DEV, "Error %d while broadcasting event. "
3227 "Event seq:%u sib_reason:%u\n",
3228 err, seq, sib->sib_reason);
b411b363 3229}
This page took 0.300797 seconds and 5 git commands to generate.