blob: b81f924c47bc615117f43c0c0a67ac042a3147e8 [file] [log] [blame]
Philipp Reisnerb411b362009-09-25 16:07:19 -07001/*
2 drbd_nl.c
3
4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5
6 Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
7 Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8 Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
9
10 drbd is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 drbd is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with drbd; see the file COPYING. If not, write to
22 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 */
25
Philipp Reisnerb411b362009-09-25 16:07:19 -070026#include <linux/module.h>
27#include <linux/drbd.h>
28#include <linux/in.h>
29#include <linux/fs.h>
30#include <linux/file.h>
31#include <linux/slab.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070032#include <linux/blkpg.h>
33#include <linux/cpumask.h>
34#include "drbd_int.h"
Philipp Reisner265be2d2010-05-31 10:14:17 +020035#include "drbd_req.h"
Philipp Reisnerb411b362009-09-25 16:07:19 -070036#include "drbd_wrappers.h"
37#include <asm/unaligned.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070038#include <linux/drbd_limits.h>
Philipp Reisner87f7be42010-06-11 13:56:33 +020039#include <linux/kthread.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070040
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010041#include <net/genetlink.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070042
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010043/* .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
50int drbd_adm_create_connection(struct sk_buff *skb, struct genl_info *info);
51int drbd_adm_delete_connection(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +010052int drbd_adm_down(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010053
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);
Lars Ellenbergf3990022011-03-23 14:31:09 +010056int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010057int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info);
58int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info);
Lars Ellenbergf3990022011-03-23 14:31:09 +010059int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010060int 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);
Lars Ellenbergf3990022011-03-23 14:31:09 +010071int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010072int 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>
78#include <linux/genl_magic_func.h>
79
80/* used blkdev_get_by_path, to claim our meta data device(s) */
Philipp Reisnerb411b362009-09-25 16:07:19 -070081static char *drbd_m_holder = "Hands off! this is DRBD's meta data device.";
82
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010083/* Configuration is strictly serialized, because generic netlink message
84 * processing is strictly serialized by the genl_lock().
85 * Which means we can use one static global drbd_config_context struct.
86 */
87static struct drbd_config_context {
88 /* assigned from drbd_genlmsghdr */
89 unsigned int minor;
90 /* assigned from request attributes, if present */
91 unsigned int volume;
92#define VOLUME_UNSPECIFIED (-1U)
93 /* pointer into the request skb,
94 * limited lifetime! */
95 char *conn_name;
Philipp Reisnerb411b362009-09-25 16:07:19 -070096
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010097 /* reply buffer */
98 struct sk_buff *reply_skb;
99 /* pointer into reply buffer */
100 struct drbd_genlmsghdr *reply_dh;
101 /* resolved from attributes, if possible */
102 struct drbd_conf *mdev;
103 struct drbd_tconn *tconn;
104} adm_ctx;
105
106static void drbd_adm_send_reply(struct sk_buff *skb, struct genl_info *info)
107{
108 genlmsg_end(skb, genlmsg_data(nlmsg_data(nlmsg_hdr(skb))));
109 if (genlmsg_reply(skb, info))
110 printk(KERN_ERR "drbd: error sending genl reply\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700111}
112
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100113/* Used on a fresh "drbd_adm_prepare"d reply_skb, this cannot fail: The only
114 * reason it could fail was no space in skb, and there are 4k available. */
Lars Ellenberg8432b312011-03-08 16:11:16 +0100115int drbd_msg_put_info(const char *info)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100116{
117 struct sk_buff *skb = adm_ctx.reply_skb;
118 struct nlattr *nla;
119 int err = -EMSGSIZE;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700120
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100121 if (!info || !info[0])
122 return 0;
123
124 nla = nla_nest_start(skb, DRBD_NLA_CFG_REPLY);
125 if (!nla)
126 return err;
127
128 err = nla_put_string(skb, T_info_text, info);
129 if (err) {
130 nla_nest_cancel(skb, nla);
131 return err;
132 } else
133 nla_nest_end(skb, nla);
134 return 0;
135}
136
137/* This would be a good candidate for a "pre_doit" hook,
138 * and per-family private info->pointers.
139 * But we need to stay compatible with older kernels.
140 * If it returns successfully, adm_ctx members are valid.
141 */
142#define DRBD_ADM_NEED_MINOR 1
143#define DRBD_ADM_NEED_CONN 2
144static int drbd_adm_prepare(struct sk_buff *skb, struct genl_info *info,
145 unsigned flags)
146{
147 struct drbd_genlmsghdr *d_in = info->userhdr;
148 const u8 cmd = info->genlhdr->cmd;
149 int err;
150
151 memset(&adm_ctx, 0, sizeof(adm_ctx));
152
153 /* genl_rcv_msg only checks for CAP_NET_ADMIN on "GENL_ADMIN_PERM" :( */
154 if (cmd != DRBD_ADM_GET_STATUS
155 && security_netlink_recv(skb, CAP_SYS_ADMIN))
156 return -EPERM;
157
158 adm_ctx.reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
159 if (!adm_ctx.reply_skb)
160 goto fail;
161
162 adm_ctx.reply_dh = genlmsg_put_reply(adm_ctx.reply_skb,
163 info, &drbd_genl_family, 0, cmd);
164 /* put of a few bytes into a fresh skb of >= 4k will always succeed.
165 * but anyways */
166 if (!adm_ctx.reply_dh)
167 goto fail;
168
169 adm_ctx.reply_dh->minor = d_in->minor;
170 adm_ctx.reply_dh->ret_code = NO_ERROR;
171
172 if (info->attrs[DRBD_NLA_CFG_CONTEXT]) {
173 struct nlattr *nla;
174 /* parse and validate only */
Lars Ellenbergf3990022011-03-23 14:31:09 +0100175 err = drbd_cfg_context_from_attrs(NULL, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100176 if (err)
177 goto fail;
178
179 /* It was present, and valid,
180 * copy it over to the reply skb. */
181 err = nla_put_nohdr(adm_ctx.reply_skb,
182 info->attrs[DRBD_NLA_CFG_CONTEXT]->nla_len,
183 info->attrs[DRBD_NLA_CFG_CONTEXT]);
184 if (err)
185 goto fail;
186
187 /* and assign stuff to the global adm_ctx */
188 nla = nested_attr_tb[__nla_type(T_ctx_volume)];
189 adm_ctx.volume = nla ? nla_get_u32(nla) : VOLUME_UNSPECIFIED;
190 nla = nested_attr_tb[__nla_type(T_ctx_conn_name)];
191 if (nla)
192 adm_ctx.conn_name = nla_data(nla);
193 } else
194 adm_ctx.volume = VOLUME_UNSPECIFIED;
195
196 adm_ctx.minor = d_in->minor;
197 adm_ctx.mdev = minor_to_mdev(d_in->minor);
Philipp Reisner0ace9df2011-04-24 10:53:19 +0200198 adm_ctx.tconn = conn_get_by_name(adm_ctx.conn_name);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100199
200 if (!adm_ctx.mdev && (flags & DRBD_ADM_NEED_MINOR)) {
201 drbd_msg_put_info("unknown minor");
202 return ERR_MINOR_INVALID;
203 }
204 if (!adm_ctx.tconn && (flags & DRBD_ADM_NEED_CONN)) {
205 drbd_msg_put_info("unknown connection");
206 return ERR_INVALID_REQUEST;
207 }
208
209 /* some more paranoia, if the request was over-determined */
Lars Ellenberg527f4b22011-03-14 13:58:03 +0100210 if (adm_ctx.mdev && adm_ctx.tconn &&
211 adm_ctx.mdev->tconn != adm_ctx.tconn) {
212 pr_warning("request: minor=%u, conn=%s; but that minor belongs to connection %s\n",
213 adm_ctx.minor, adm_ctx.conn_name, adm_ctx.mdev->tconn->name);
214 drbd_msg_put_info("minor exists in different connection");
215 return ERR_INVALID_REQUEST;
216 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100217 if (adm_ctx.mdev &&
218 adm_ctx.volume != VOLUME_UNSPECIFIED &&
219 adm_ctx.volume != adm_ctx.mdev->vnr) {
220 pr_warning("request: minor=%u, volume=%u; but that minor is volume %u in %s\n",
221 adm_ctx.minor, adm_ctx.volume,
222 adm_ctx.mdev->vnr, adm_ctx.mdev->tconn->name);
Lars Ellenberg527f4b22011-03-14 13:58:03 +0100223 drbd_msg_put_info("minor exists as different volume");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100224 return ERR_INVALID_REQUEST;
225 }
Philipp Reisner0ace9df2011-04-24 10:53:19 +0200226
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100227 return NO_ERROR;
228
229fail:
230 nlmsg_free(adm_ctx.reply_skb);
231 adm_ctx.reply_skb = NULL;
232 return -ENOMEM;
233}
234
235static int drbd_adm_finish(struct genl_info *info, int retcode)
236{
237 struct nlattr *nla;
238 const char *conn_name = NULL;
239
Philipp Reisner0ace9df2011-04-24 10:53:19 +0200240 if (adm_ctx.tconn) {
241 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
242 adm_ctx.tconn = NULL;
243 }
244
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100245 if (!adm_ctx.reply_skb)
246 return -ENOMEM;
247
248 adm_ctx.reply_dh->ret_code = retcode;
249
250 nla = info->attrs[DRBD_NLA_CFG_CONTEXT];
251 if (nla) {
252 nla = nla_find_nested(nla, __nla_type(T_ctx_conn_name));
253 if (nla)
254 conn_name = nla_data(nla);
255 }
256
257 drbd_adm_send_reply(adm_ctx.reply_skb, info);
258 return 0;
259}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700260
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100261static void setup_khelper_env(struct drbd_tconn *tconn, char **envp)
262{
263 char *afs;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200264 struct net_conf *nc;
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100265
Philipp Reisner44ed1672011-04-19 17:10:19 +0200266 rcu_read_lock();
267 nc = rcu_dereference(tconn->net_conf);
268 if (nc) {
269 switch (((struct sockaddr *)nc->peer_addr)->sa_family) {
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100270 case AF_INET6:
271 afs = "ipv6";
272 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
Philipp Reisner44ed1672011-04-19 17:10:19 +0200273 &((struct sockaddr_in6 *)nc->peer_addr)->sin6_addr);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100274 break;
275 case AF_INET:
276 afs = "ipv4";
277 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
Philipp Reisner44ed1672011-04-19 17:10:19 +0200278 &((struct sockaddr_in *)nc->peer_addr)->sin_addr);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100279 break;
280 default:
281 afs = "ssocks";
282 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
Philipp Reisner44ed1672011-04-19 17:10:19 +0200283 &((struct sockaddr_in *)nc->peer_addr)->sin_addr);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100284 }
285 snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100286 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200287 rcu_read_unlock();
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100288}
289
Philipp Reisnerb411b362009-09-25 16:07:19 -0700290int drbd_khelper(struct drbd_conf *mdev, char *cmd)
291{
292 char *envp[] = { "HOME=/",
293 "TERM=linux",
294 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100295 (char[20]) { }, /* address family */
296 (char[60]) { }, /* address */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700297 NULL };
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100298 char mb[12];
Philipp Reisnerb411b362009-09-25 16:07:19 -0700299 char *argv[] = {usermode_helper, cmd, mb, NULL };
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100300 struct sib_info sib;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700301 int ret;
302
303 snprintf(mb, 12, "minor-%d", mdev_to_minor(mdev));
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100304 setup_khelper_env(mdev->tconn, envp);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700305
Lars Ellenberg1090c052010-07-19 17:41:04 +0200306 /* The helper may take some time.
307 * write out any unsynced meta data changes now */
308 drbd_md_sync(mdev);
309
Philipp Reisnerb411b362009-09-25 16:07:19 -0700310 dev_info(DEV, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100311 sib.sib_reason = SIB_HELPER_PRE;
312 sib.helper_name = cmd;
313 drbd_bcast_event(mdev, &sib);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700314 ret = call_usermodehelper(usermode_helper, argv, envp, 1);
315 if (ret)
316 dev_warn(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
317 usermode_helper, cmd, mb,
318 (ret >> 8) & 0xff, ret);
319 else
320 dev_info(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
321 usermode_helper, cmd, mb,
322 (ret >> 8) & 0xff, ret);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100323 sib.sib_reason = SIB_HELPER_POST;
324 sib.helper_exit_code = ret;
325 drbd_bcast_event(mdev, &sib);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700326
327 if (ret < 0) /* Ignore any ERRNOs we got. */
328 ret = 0;
329
330 return ret;
331}
332
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100333static void conn_md_sync(struct drbd_tconn *tconn)
334{
335 struct drbd_conf *mdev;
Philipp Reisnere90285e2011-03-22 12:51:21 +0100336 int vnr;
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100337
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200338 rcu_read_lock();
339 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
340 kref_get(&mdev->kref);
341 rcu_read_unlock();
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100342 drbd_md_sync(mdev);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200343 kref_put(&mdev->kref, &drbd_minor_destroy);
344 rcu_read_lock();
345 }
346 rcu_read_unlock();
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100347}
348
349int conn_khelper(struct drbd_tconn *tconn, char *cmd)
350{
351 char *envp[] = { "HOME=/",
352 "TERM=linux",
353 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
354 (char[20]) { }, /* address family */
355 (char[60]) { }, /* address */
356 NULL };
357 char *argv[] = {usermode_helper, cmd, tconn->name, NULL };
358 int ret;
359
360 setup_khelper_env(tconn, envp);
361 conn_md_sync(tconn);
362
363 conn_info(tconn, "helper command: %s %s %s\n", usermode_helper, cmd, tconn->name);
364 /* TODO: conn_bcast_event() ?? */
365
366 ret = call_usermodehelper(usermode_helper, argv, envp, 1);
367 if (ret)
368 conn_warn(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
369 usermode_helper, cmd, tconn->name,
370 (ret >> 8) & 0xff, ret);
371 else
372 conn_info(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
373 usermode_helper, cmd, tconn->name,
374 (ret >> 8) & 0xff, ret);
375 /* TODO: conn_bcast_event() ?? */
376
377 if (ret < 0) /* Ignore any ERRNOs we got. */
378 ret = 0;
379
380 return ret;
381}
382
Philipp Reisnercb703452011-03-24 11:03:07 +0100383static enum drbd_fencing_p highest_fencing_policy(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700384{
Philipp Reisnercb703452011-03-24 11:03:07 +0100385 enum drbd_fencing_p fp = FP_NOT_AVAIL;
386 struct drbd_conf *mdev;
387 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700388
Philipp Reisner695d08f2011-04-11 22:53:32 -0700389 rcu_read_lock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100390 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
391 if (get_ldev_if_state(mdev, D_CONSISTENT)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200392 fp = max_t(enum drbd_fencing_p, fp,
393 rcu_dereference(mdev->ldev->disk_conf)->fencing);
Philipp Reisnercb703452011-03-24 11:03:07 +0100394 put_ldev(mdev);
395 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700396 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700397 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700398
Philipp Reisnercb703452011-03-24 11:03:07 +0100399 return fp;
400}
401
402bool conn_try_outdate_peer(struct drbd_tconn *tconn)
403{
404 union drbd_state mask = { };
405 union drbd_state val = { };
406 enum drbd_fencing_p fp;
407 char *ex_to_string;
408 int r;
409
410 if (tconn->cstate >= C_WF_REPORT_PARAMS) {
411 conn_err(tconn, "Expected cstate < C_WF_REPORT_PARAMS\n");
412 return false;
413 }
414
415 fp = highest_fencing_policy(tconn);
416 switch (fp) {
417 case FP_NOT_AVAIL:
418 conn_warn(tconn, "Not fencing peer, I'm not even Consistent myself.\n");
419 goto out;
420 case FP_DONT_CARE:
421 return true;
422 default: ;
423 }
424
425 r = conn_khelper(tconn, "fence-peer");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700426
427 switch ((r>>8) & 0xff) {
428 case 3: /* peer is inconsistent */
429 ex_to_string = "peer is inconsistent or worse";
Philipp Reisnercb703452011-03-24 11:03:07 +0100430 mask.pdsk = D_MASK;
431 val.pdsk = D_INCONSISTENT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700432 break;
433 case 4: /* peer got outdated, or was already outdated */
434 ex_to_string = "peer was fenced";
Philipp Reisnercb703452011-03-24 11:03:07 +0100435 mask.pdsk = D_MASK;
436 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700437 break;
438 case 5: /* peer was down */
Philipp Reisnercb703452011-03-24 11:03:07 +0100439 if (conn_highest_disk(tconn) == D_UP_TO_DATE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700440 /* we will(have) create(d) a new UUID anyways... */
441 ex_to_string = "peer is unreachable, assumed to be dead";
Philipp Reisnercb703452011-03-24 11:03:07 +0100442 mask.pdsk = D_MASK;
443 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700444 } else {
445 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
Philipp Reisnerb411b362009-09-25 16:07:19 -0700446 }
447 break;
448 case 6: /* Peer is primary, voluntarily outdate myself.
449 * This is useful when an unconnected R_SECONDARY is asked to
450 * become R_PRIMARY, but finds the other peer being active. */
451 ex_to_string = "peer is active";
Philipp Reisnercb703452011-03-24 11:03:07 +0100452 conn_warn(tconn, "Peer is primary, outdating myself.\n");
453 mask.disk = D_MASK;
454 val.disk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700455 break;
456 case 7:
457 if (fp != FP_STONITH)
Philipp Reisnercb703452011-03-24 11:03:07 +0100458 conn_err(tconn, "fence-peer() = 7 && fencing != Stonith !!!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700459 ex_to_string = "peer was stonithed";
Philipp Reisnercb703452011-03-24 11:03:07 +0100460 mask.pdsk = D_MASK;
461 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700462 break;
463 default:
464 /* The script is broken ... */
Philipp Reisnercb703452011-03-24 11:03:07 +0100465 conn_err(tconn, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
466 return false; /* Eventually leave IO frozen */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700467 }
468
Philipp Reisnercb703452011-03-24 11:03:07 +0100469 conn_info(tconn, "fence-peer helper returned %d (%s)\n",
470 (r>>8) & 0xff, ex_to_string);
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200471
Philipp Reisnercb703452011-03-24 11:03:07 +0100472 out:
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200473
Philipp Reisnercb703452011-03-24 11:03:07 +0100474 /* Not using
475 conn_request_state(tconn, mask, val, CS_VERBOSE);
476 here, because we might were able to re-establish the connection in the
477 meantime. */
478 spin_lock_irq(&tconn->req_lock);
479 if (tconn->cstate < C_WF_REPORT_PARAMS)
480 _conn_request_state(tconn, mask, val, CS_VERBOSE);
481 spin_unlock_irq(&tconn->req_lock);
482
483 return conn_highest_pdsk(tconn) <= D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700484}
485
Philipp Reisner87f7be42010-06-11 13:56:33 +0200486static int _try_outdate_peer_async(void *data)
487{
Philipp Reisnercb703452011-03-24 11:03:07 +0100488 struct drbd_tconn *tconn = (struct drbd_tconn *)data;
Philipp Reisner87f7be42010-06-11 13:56:33 +0200489
Philipp Reisnercb703452011-03-24 11:03:07 +0100490 conn_try_outdate_peer(tconn);
Philipp Reisner87f7be42010-06-11 13:56:33 +0200491
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200492 kref_put(&tconn->kref, &conn_destroy);
Philipp Reisner87f7be42010-06-11 13:56:33 +0200493 return 0;
494}
495
Philipp Reisnercb703452011-03-24 11:03:07 +0100496void conn_try_outdate_peer_async(struct drbd_tconn *tconn)
Philipp Reisner87f7be42010-06-11 13:56:33 +0200497{
498 struct task_struct *opa;
499
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200500 kref_get(&tconn->kref);
Philipp Reisnercb703452011-03-24 11:03:07 +0100501 opa = kthread_run(_try_outdate_peer_async, tconn, "drbd_async_h");
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200502 if (IS_ERR(opa)) {
Philipp Reisnercb703452011-03-24 11:03:07 +0100503 conn_err(tconn, "out of mem, failed to invoke fence-peer helper\n");
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200504 kref_put(&tconn->kref, &conn_destroy);
505 }
Philipp Reisner87f7be42010-06-11 13:56:33 +0200506}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700507
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100508enum drbd_state_rv
509drbd_set_role(struct drbd_conf *mdev, enum drbd_role new_role, int force)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700510{
511 const int max_tries = 4;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100512 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200513 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700514 int try = 0;
515 int forced = 0;
516 union drbd_state mask, val;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700517
518 if (new_role == R_PRIMARY)
Philipp Reisner0625ac12011-02-07 14:49:19 +0100519 request_ping(mdev->tconn); /* Detect a dead peer ASAP */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700520
Philipp Reisner8410da82011-02-11 20:11:10 +0100521 mutex_lock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700522
523 mask.i = 0; mask.role = R_MASK;
524 val.i = 0; val.role = new_role;
525
526 while (try++ < max_tries) {
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100527 rv = _drbd_request_state(mdev, mask, val, CS_WAIT_COMPLETE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700528
529 /* in case we first succeeded to outdate,
530 * but now suddenly could establish a connection */
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100531 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700532 val.pdsk = 0;
533 mask.pdsk = 0;
534 continue;
535 }
536
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100537 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
Philipp Reisnerd10a33c2010-03-04 15:11:39 +0100538 (mdev->state.disk < D_UP_TO_DATE &&
539 mdev->state.disk >= D_INCONSISTENT)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700540 mask.disk = D_MASK;
541 val.disk = D_UP_TO_DATE;
542 forced = 1;
543 continue;
544 }
545
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100546 if (rv == SS_NO_UP_TO_DATE_DISK &&
Philipp Reisnerb411b362009-09-25 16:07:19 -0700547 mdev->state.disk == D_CONSISTENT && mask.pdsk == 0) {
548 D_ASSERT(mdev->state.pdsk == D_UNKNOWN);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700549
Philipp Reisnercb703452011-03-24 11:03:07 +0100550 if (conn_try_outdate_peer(mdev->tconn)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700551 val.disk = D_UP_TO_DATE;
552 mask.disk = D_MASK;
553 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700554 continue;
555 }
556
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100557 if (rv == SS_NOTHING_TO_DO)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100558 goto out;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100559 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
Philipp Reisnercb703452011-03-24 11:03:07 +0100560 if (!conn_try_outdate_peer(mdev->tconn) && force) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700561 dev_warn(DEV, "Forced into split brain situation!\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100562 mask.pdsk = D_MASK;
563 val.pdsk = D_OUTDATED;
564
Philipp Reisnerb411b362009-09-25 16:07:19 -0700565 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700566 continue;
567 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100568 if (rv == SS_TWO_PRIMARIES) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700569 /* Maybe the peer is detected as dead very soon...
570 retry at most once more in this case. */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200571 int timeo;
572 rcu_read_lock();
573 nc = rcu_dereference(mdev->tconn->net_conf);
574 timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
575 rcu_read_unlock();
576 schedule_timeout_interruptible(timeo);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700577 if (try < max_tries)
578 try = max_tries - 1;
579 continue;
580 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100581 if (rv < SS_SUCCESS) {
582 rv = _drbd_request_state(mdev, mask, val,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700583 CS_VERBOSE + CS_WAIT_COMPLETE);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100584 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100585 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700586 }
587 break;
588 }
589
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100590 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100591 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700592
593 if (forced)
594 dev_warn(DEV, "Forced to consider local data as UpToDate!\n");
595
596 /* Wait until nothing is on the fly :) */
597 wait_event(mdev->misc_wait, atomic_read(&mdev->ap_pending_cnt) == 0);
598
599 if (new_role == R_SECONDARY) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100600 set_disk_ro(mdev->vdisk, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700601 if (get_ldev(mdev)) {
602 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
603 put_ldev(mdev);
604 }
605 } else {
Philipp Reisnera0095502011-05-03 13:14:15 +0200606 mutex_lock(&mdev->tconn->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +0200607 nc = mdev->tconn->net_conf;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200608 if (nc)
Andreas Gruenbacher6139f602011-05-06 20:00:02 +0200609 nc->discard_my_data = 0; /* without copy; single bit op is atomic */
Philipp Reisnera0095502011-05-03 13:14:15 +0200610 mutex_unlock(&mdev->tconn->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +0200611
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100612 set_disk_ro(mdev->vdisk, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700613 if (get_ldev(mdev)) {
614 if (((mdev->state.conn < C_CONNECTED ||
615 mdev->state.pdsk <= D_FAILED)
616 && mdev->ldev->md.uuid[UI_BITMAP] == 0) || forced)
617 drbd_uuid_new_current(mdev);
618
619 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
620 put_ldev(mdev);
621 }
622 }
623
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100624 /* writeout of activity log covered areas of the bitmap
625 * to stable storage done in after state change already */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700626
627 if (mdev->state.conn >= C_WF_REPORT_PARAMS) {
628 /* if this was forced, we should consider sync */
629 if (forced)
630 drbd_send_uuids(mdev);
631 drbd_send_state(mdev);
632 }
633
634 drbd_md_sync(mdev);
635
636 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100637out:
Philipp Reisner8410da82011-02-11 20:11:10 +0100638 mutex_unlock(mdev->state_mutex);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100639 return rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700640}
641
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100642static const char *from_attrs_err_to_txt(int err)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700643{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100644 return err == -ENOMSG ? "required attribute missing" :
645 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
Lars Ellenbergf3990022011-03-23 14:31:09 +0100646 err == -EEXIST ? "can not change invariant setting" :
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100647 "invalid attribute value";
Philipp Reisnerb411b362009-09-25 16:07:19 -0700648}
649
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100650int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700651{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100652 struct set_role_parms parms;
653 int err;
654 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700655
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100656 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
657 if (!adm_ctx.reply_skb)
658 return retcode;
659 if (retcode != NO_ERROR)
660 goto out;
661
662 memset(&parms, 0, sizeof(parms));
663 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +0100664 err = set_role_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100665 if (err) {
666 retcode = ERR_MANDATORY_TAG;
667 drbd_msg_put_info(from_attrs_err_to_txt(err));
668 goto out;
669 }
670 }
671
672 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
673 retcode = drbd_set_role(adm_ctx.mdev, R_PRIMARY, parms.assume_uptodate);
674 else
675 retcode = drbd_set_role(adm_ctx.mdev, R_SECONDARY, 0);
676out:
677 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700678 return 0;
679}
680
681/* initializes the md.*_offset members, so we are able to find
682 * the on disk meta data */
683static void drbd_md_set_sector_offsets(struct drbd_conf *mdev,
684 struct drbd_backing_dev *bdev)
685{
686 sector_t md_size_sect = 0;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200687 int meta_dev_idx;
688
689 rcu_read_lock();
690 meta_dev_idx = rcu_dereference(bdev->disk_conf)->meta_dev_idx;
691
692 switch (meta_dev_idx) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700693 default:
694 /* v07 style fixed size indexed meta data */
695 bdev->md.md_size_sect = MD_RESERVED_SECT;
696 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
697 bdev->md.al_offset = MD_AL_OFFSET;
698 bdev->md.bm_offset = MD_BM_OFFSET;
699 break;
700 case DRBD_MD_INDEX_FLEX_EXT:
701 /* just occupy the full device; unit: sectors */
702 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
703 bdev->md.md_offset = 0;
704 bdev->md.al_offset = MD_AL_OFFSET;
705 bdev->md.bm_offset = MD_BM_OFFSET;
706 break;
707 case DRBD_MD_INDEX_INTERNAL:
708 case DRBD_MD_INDEX_FLEX_INT:
709 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
710 /* al size is still fixed */
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100711 bdev->md.al_offset = -MD_AL_SECTORS;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700712 /* we need (slightly less than) ~ this much bitmap sectors: */
713 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
714 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
715 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
716 md_size_sect = ALIGN(md_size_sect, 8);
717
718 /* plus the "drbd meta data super block",
719 * and the activity log; */
720 md_size_sect += MD_BM_OFFSET;
721
722 bdev->md.md_size_sect = md_size_sect;
723 /* bitmap offset is adjusted by 'super' block size */
724 bdev->md.bm_offset = -md_size_sect + MD_AL_OFFSET;
725 break;
726 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200727 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700728}
729
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100730/* input size is expected to be in KB */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700731char *ppsize(char *buf, unsigned long long size)
732{
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100733 /* Needs 9 bytes at max including trailing NUL:
734 * -1ULL ==> "16384 EB" */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700735 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
736 int base = 0;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100737 while (size >= 10000 && base < sizeof(units)-1) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700738 /* shift + round */
739 size = (size >> 10) + !!(size & (1<<9));
740 base++;
741 }
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100742 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700743
744 return buf;
745}
746
747/* there is still a theoretical deadlock when called from receiver
748 * on an D_INCONSISTENT R_PRIMARY:
749 * remote READ does inc_ap_bio, receiver would need to receive answer
750 * packet from remote to dec_ap_bio again.
751 * receiver receive_sizes(), comes here,
752 * waits for ap_bio_cnt == 0. -> deadlock.
753 * but this cannot happen, actually, because:
754 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
755 * (not connected, or bad/no disk on peer):
756 * see drbd_fail_request_early, ap_bio_cnt is zero.
757 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
758 * peer may not initiate a resize.
759 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100760/* Note these are not to be confused with
761 * drbd_adm_suspend_io/drbd_adm_resume_io,
762 * which are (sub) state changes triggered by admin (drbdsetup),
763 * and can be long lived.
764 * This changes an mdev->flag, is triggered by drbd internals,
765 * and should be short-lived. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700766void drbd_suspend_io(struct drbd_conf *mdev)
767{
768 set_bit(SUSPEND_IO, &mdev->flags);
Philipp Reisner2aebfab2011-03-28 16:48:11 +0200769 if (drbd_suspended(mdev))
Philipp Reisner265be2d2010-05-31 10:14:17 +0200770 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700771 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
772}
773
774void drbd_resume_io(struct drbd_conf *mdev)
775{
776 clear_bit(SUSPEND_IO, &mdev->flags);
777 wake_up(&mdev->misc_wait);
778}
779
780/**
781 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
782 * @mdev: DRBD device.
783 *
784 * Returns 0 on success, negative return values indicate errors.
785 * You should call drbd_md_sync() after calling this function.
786 */
Bart Van Assche24c48302011-05-21 18:32:29 +0200787enum determine_dev_size drbd_determine_dev_size(struct drbd_conf *mdev, enum dds_flags flags) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700788{
789 sector_t prev_first_sect, prev_size; /* previous meta location */
Philipp Reisneref5e44a2011-05-03 13:27:43 +0200790 sector_t la_size, u_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700791 sector_t size;
792 char ppb[10];
793
794 int md_moved, la_size_changed;
795 enum determine_dev_size rv = unchanged;
796
797 /* race:
798 * application request passes inc_ap_bio,
799 * but then cannot get an AL-reference.
800 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
801 *
802 * to avoid that:
803 * Suspend IO right here.
804 * still lock the act_log to not trigger ASSERTs there.
805 */
806 drbd_suspend_io(mdev);
807
808 /* no wait necessary anymore, actually we could assert that */
809 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
810
811 prev_first_sect = drbd_md_first_sector(mdev->ldev);
812 prev_size = mdev->ldev->md.md_size_sect;
813 la_size = mdev->ldev->md.la_size_sect;
814
815 /* TODO: should only be some assert here, not (re)init... */
816 drbd_md_set_sector_offsets(mdev, mdev->ldev);
817
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200818 rcu_read_lock();
819 u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
820 rcu_read_unlock();
Philipp Reisneref5e44a2011-05-03 13:27:43 +0200821 size = drbd_new_dev_size(mdev, mdev->ldev, u_size, flags & DDSF_FORCED);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700822
823 if (drbd_get_capacity(mdev->this_bdev) != size ||
824 drbd_bm_capacity(mdev) != size) {
825 int err;
Philipp Reisner02d9a942010-03-24 16:23:03 +0100826 err = drbd_bm_resize(mdev, size, !(flags & DDSF_NO_RESYNC));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700827 if (unlikely(err)) {
828 /* currently there is only one error: ENOMEM! */
829 size = drbd_bm_capacity(mdev)>>1;
830 if (size == 0) {
831 dev_err(DEV, "OUT OF MEMORY! "
832 "Could not allocate bitmap!\n");
833 } else {
834 dev_err(DEV, "BM resizing failed. "
835 "Leaving size unchanged at size = %lu KB\n",
836 (unsigned long)size);
837 }
838 rv = dev_size_error;
839 }
840 /* racy, see comments above. */
841 drbd_set_my_capacity(mdev, size);
842 mdev->ldev->md.la_size_sect = size;
843 dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
844 (unsigned long long)size>>1);
845 }
846 if (rv == dev_size_error)
847 goto out;
848
849 la_size_changed = (la_size != mdev->ldev->md.la_size_sect);
850
851 md_moved = prev_first_sect != drbd_md_first_sector(mdev->ldev)
852 || prev_size != mdev->ldev->md.md_size_sect;
853
854 if (la_size_changed || md_moved) {
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100855 int err;
856
Philipp Reisnerb411b362009-09-25 16:07:19 -0700857 drbd_al_shrink(mdev); /* All extents inactive. */
858 dev_info(DEV, "Writing the whole bitmap, %s\n",
859 la_size_changed && md_moved ? "size changed and md moved" :
860 la_size_changed ? "size changed" : "md moved");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100861 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
862 err = drbd_bitmap_io(mdev, &drbd_bm_write,
863 "size changed", BM_LOCKED_MASK);
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100864 if (err) {
865 rv = dev_size_error;
866 goto out;
867 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700868 drbd_md_mark_dirty(mdev);
869 }
870
871 if (size > la_size)
872 rv = grew;
873 if (size < la_size)
874 rv = shrunk;
875out:
876 lc_unlock(mdev->act_log);
877 wake_up(&mdev->al_wait);
878 drbd_resume_io(mdev);
879
880 return rv;
881}
882
883sector_t
Philipp Reisneref5e44a2011-05-03 13:27:43 +0200884drbd_new_dev_size(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
885 sector_t u_size, int assume_peer_has_space)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700886{
887 sector_t p_size = mdev->p_size; /* partner's disk size. */
888 sector_t la_size = bdev->md.la_size_sect; /* last agreed size. */
889 sector_t m_size; /* my size */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700890 sector_t size = 0;
891
892 m_size = drbd_get_max_capacity(bdev);
893
Philipp Reisnera393db62009-12-22 13:35:52 +0100894 if (mdev->state.conn < C_CONNECTED && assume_peer_has_space) {
895 dev_warn(DEV, "Resize while not connected was forced by the user!\n");
896 p_size = m_size;
897 }
898
Philipp Reisnerb411b362009-09-25 16:07:19 -0700899 if (p_size && m_size) {
900 size = min_t(sector_t, p_size, m_size);
901 } else {
902 if (la_size) {
903 size = la_size;
904 if (m_size && m_size < size)
905 size = m_size;
906 if (p_size && p_size < size)
907 size = p_size;
908 } else {
909 if (m_size)
910 size = m_size;
911 if (p_size)
912 size = p_size;
913 }
914 }
915
916 if (size == 0)
917 dev_err(DEV, "Both nodes diskless!\n");
918
919 if (u_size) {
920 if (u_size > size)
921 dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n",
922 (unsigned long)u_size>>1, (unsigned long)size>>1);
923 else
924 size = u_size;
925 }
926
927 return size;
928}
929
930/**
931 * drbd_check_al_size() - Ensures that the AL is of the right size
932 * @mdev: DRBD device.
933 *
934 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
935 * failed, and 0 on success. You should call drbd_md_sync() after you called
936 * this function.
937 */
Lars Ellenbergf3990022011-03-23 14:31:09 +0100938static int drbd_check_al_size(struct drbd_conf *mdev, struct disk_conf *dc)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700939{
940 struct lru_cache *n, *t;
941 struct lc_element *e;
942 unsigned int in_use;
943 int i;
944
Philipp Reisnerb411b362009-09-25 16:07:19 -0700945 if (mdev->act_log &&
Lars Ellenbergf3990022011-03-23 14:31:09 +0100946 mdev->act_log->nr_elements == dc->al_extents)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700947 return 0;
948
949 in_use = 0;
950 t = mdev->act_log;
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100951 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
Lars Ellenbergf3990022011-03-23 14:31:09 +0100952 dc->al_extents, sizeof(struct lc_element), 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700953
954 if (n == NULL) {
955 dev_err(DEV, "Cannot allocate act_log lru!\n");
956 return -ENOMEM;
957 }
958 spin_lock_irq(&mdev->al_lock);
959 if (t) {
960 for (i = 0; i < t->nr_elements; i++) {
961 e = lc_element_by_index(t, i);
962 if (e->refcnt)
963 dev_err(DEV, "refcnt(%d)==%d\n",
964 e->lc_number, e->refcnt);
965 in_use += e->refcnt;
966 }
967 }
968 if (!in_use)
969 mdev->act_log = n;
970 spin_unlock_irq(&mdev->al_lock);
971 if (in_use) {
972 dev_err(DEV, "Activity log still in use!\n");
973 lc_destroy(n);
974 return -EBUSY;
975 } else {
976 if (t)
977 lc_destroy(t);
978 }
979 drbd_md_mark_dirty(mdev); /* we changed mdev->act_log->nr_elemens */
980 return 0;
981}
982
Philipp Reisner99432fc2011-05-20 16:39:13 +0200983static void drbd_setup_queue_param(struct drbd_conf *mdev, unsigned int max_bio_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700984{
985 struct request_queue * const q = mdev->rq_queue;
Philipp Reisner99432fc2011-05-20 16:39:13 +0200986 int max_hw_sectors = max_bio_size >> 9;
987 int max_segments = 0;
988
989 if (get_ldev_if_state(mdev, D_ATTACHING)) {
990 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
991
992 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200993 rcu_read_lock();
994 max_segments = rcu_dereference(mdev->ldev->disk_conf)->max_bio_bvecs;
995 rcu_read_unlock();
Philipp Reisner99432fc2011-05-20 16:39:13 +0200996 put_ldev(mdev);
997 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700998
Philipp Reisnerb411b362009-09-25 16:07:19 -0700999 blk_queue_logical_block_size(q, 512);
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001000 blk_queue_max_hw_sectors(q, max_hw_sectors);
1001 /* This is the workaround for "bio would need to, but cannot, be split" */
1002 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
1003 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001004
Philipp Reisner99432fc2011-05-20 16:39:13 +02001005 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1006 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001007
Philipp Reisner99432fc2011-05-20 16:39:13 +02001008 blk_queue_stack_limits(q, b);
1009
1010 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
1011 dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
1012 q->backing_dev_info.ra_pages,
1013 b->backing_dev_info.ra_pages);
1014 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1015 }
1016 put_ldev(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001017 }
1018}
1019
Philipp Reisner99432fc2011-05-20 16:39:13 +02001020void drbd_reconsider_max_bio_size(struct drbd_conf *mdev)
1021{
1022 int now, new, local, peer;
1023
1024 now = queue_max_hw_sectors(mdev->rq_queue) << 9;
1025 local = mdev->local_max_bio_size; /* Eventually last known value, from volatile memory */
1026 peer = mdev->peer_max_bio_size; /* Eventually last known value, from meta data */
1027
1028 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1029 local = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
1030 mdev->local_max_bio_size = local;
1031 put_ldev(mdev);
1032 }
1033
1034 /* We may ignore peer limits if the peer is modern enough.
1035 Because new from 8.3.8 onwards the peer can use multiple
1036 BIOs for a single peer_request */
1037 if (mdev->state.conn >= C_CONNECTED) {
Philipp Reisner31890f42011-01-19 14:12:51 +01001038 if (mdev->tconn->agreed_pro_version < 94)
Philipp Reisner99432fc2011-05-20 16:39:13 +02001039 peer = mdev->peer_max_bio_size;
Philipp Reisner31890f42011-01-19 14:12:51 +01001040 else if (mdev->tconn->agreed_pro_version == 94)
Philipp Reisner99432fc2011-05-20 16:39:13 +02001041 peer = DRBD_MAX_SIZE_H80_PACKET;
1042 else /* drbd 8.3.8 onwards */
1043 peer = DRBD_MAX_BIO_SIZE;
1044 }
1045
1046 new = min_t(int, local, peer);
1047
1048 if (mdev->state.role == R_PRIMARY && new < now)
1049 dev_err(DEV, "ASSERT FAILED new < now; (%d < %d)\n", new, now);
1050
1051 if (new != now)
1052 dev_info(DEV, "max BIO size = %u\n", new);
1053
1054 drbd_setup_queue_param(mdev, new);
1055}
1056
Philipp Reisnera18e9d12011-04-24 11:09:55 +02001057/* Starts the worker thread */
Philipp Reisner0e29d162011-02-18 14:23:11 +01001058static void conn_reconfig_start(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001059{
Philipp Reisner0e29d162011-02-18 14:23:11 +01001060 drbd_thread_start(&tconn->worker);
1061 conn_flush_workqueue(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001062}
1063
Philipp Reisnera18e9d12011-04-24 11:09:55 +02001064/* if still unconfigured, stops worker again. */
Philipp Reisner0e29d162011-02-18 14:23:11 +01001065static void conn_reconfig_done(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001066{
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001067 bool stop_threads;
Philipp Reisner0e29d162011-02-18 14:23:11 +01001068 spin_lock_irq(&tconn->req_lock);
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001069 stop_threads = conn_all_vols_unconf(tconn);
Philipp Reisner0e29d162011-02-18 14:23:11 +01001070 spin_unlock_irq(&tconn->req_lock);
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001071 if (stop_threads) {
1072 /* asender is implicitly stopped by receiver
Philipp Reisner81fa2e62011-05-04 15:10:30 +02001073 * in conn_disconnect() */
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001074 drbd_thread_stop(&tconn->receiver);
1075 drbd_thread_stop(&tconn->worker);
1076 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001077}
1078
Philipp Reisner07782862010-08-31 12:00:50 +02001079/* Make sure IO is suspended before calling this function(). */
1080static void drbd_suspend_al(struct drbd_conf *mdev)
1081{
1082 int s = 0;
1083
Lars Ellenberg61610422011-02-21 13:20:54 +01001084 if (!lc_try_lock(mdev->act_log)) {
Philipp Reisner07782862010-08-31 12:00:50 +02001085 dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n");
1086 return;
1087 }
1088
Lars Ellenberg61610422011-02-21 13:20:54 +01001089 drbd_al_shrink(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001090 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner07782862010-08-31 12:00:50 +02001091 if (mdev->state.conn < C_CONNECTED)
1092 s = !test_and_set_bit(AL_SUSPENDED, &mdev->flags);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001093 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg61610422011-02-21 13:20:54 +01001094 lc_unlock(mdev->act_log);
Philipp Reisner07782862010-08-31 12:00:50 +02001095
1096 if (s)
1097 dev_info(DEV, "Suspended AL updates\n");
1098}
1099
Lars Ellenberg5979e362011-04-27 21:09:55 +02001100
1101static bool should_set_defaults(struct genl_info *info)
1102{
1103 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1104 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1105}
1106
Philipp Reisnerd589a212011-05-04 10:06:52 +02001107static void enforce_disk_conf_limits(struct disk_conf *dc)
1108{
1109 if (dc->al_extents < DRBD_AL_EXTENTS_MIN)
1110 dc->al_extents = DRBD_AL_EXTENTS_MIN;
1111 if (dc->al_extents > DRBD_AL_EXTENTS_MAX)
1112 dc->al_extents = DRBD_AL_EXTENTS_MAX;
1113
1114 if (dc->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1115 dc->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
1116}
1117
Lars Ellenbergf3990022011-03-23 14:31:09 +01001118int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1119{
1120 enum drbd_ret_code retcode;
1121 struct drbd_conf *mdev;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001122 struct disk_conf *new_disk_conf, *old_disk_conf;
Philipp Reisner813472c2011-05-03 16:47:02 +02001123 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001124 int err, fifo_size;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001125
1126 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1127 if (!adm_ctx.reply_skb)
1128 return retcode;
1129 if (retcode != NO_ERROR)
1130 goto out;
1131
1132 mdev = adm_ctx.mdev;
1133
1134 /* we also need a disk
1135 * to change the options on */
1136 if (!get_ldev(mdev)) {
1137 retcode = ERR_NO_DISK;
1138 goto out;
1139 }
1140
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001141 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001142 if (!new_disk_conf) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001143 retcode = ERR_NOMEM;
1144 goto fail;
1145 }
1146
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001147 mutex_lock(&mdev->tconn->conf_update);
1148 old_disk_conf = mdev->ldev->disk_conf;
1149 *new_disk_conf = *old_disk_conf;
Lars Ellenberg5979e362011-04-27 21:09:55 +02001150 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02001151 set_disk_conf_defaults(new_disk_conf);
Lars Ellenberg5979e362011-04-27 21:09:55 +02001152
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001153 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001154 if (err) {
1155 retcode = ERR_MANDATORY_TAG;
1156 drbd_msg_put_info(from_attrs_err_to_txt(err));
1157 }
1158
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001159 if (!expect(new_disk_conf->resync_rate >= 1))
1160 new_disk_conf->resync_rate = 1;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001161
Philipp Reisnerd589a212011-05-04 10:06:52 +02001162 enforce_disk_conf_limits(new_disk_conf);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001163
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001164 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Philipp Reisner9958c852011-05-03 16:19:31 +02001165 if (fifo_size != mdev->rs_plan_s->size) {
Philipp Reisner813472c2011-05-03 16:47:02 +02001166 new_plan = fifo_alloc(fifo_size);
1167 if (!new_plan) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001168 dev_err(DEV, "kmalloc of fifo_buffer failed");
1169 retcode = ERR_NOMEM;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001170 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001171 }
1172 }
1173
Lars Ellenbergf3990022011-03-23 14:31:09 +01001174 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
1175 drbd_al_shrink(mdev);
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001176 err = drbd_check_al_size(mdev, new_disk_conf);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001177 lc_unlock(mdev->act_log);
1178 wake_up(&mdev->al_wait);
1179
1180 if (err) {
1181 retcode = ERR_NOMEM;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001182 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001183 }
1184
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001185 write_lock_irq(&global_state_lock);
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001186 retcode = drbd_resync_after_valid(mdev, new_disk_conf->resync_after);
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001187 if (retcode == NO_ERROR) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001188 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001189 drbd_resync_after_changed(mdev);
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001190 }
1191 write_unlock_irq(&global_state_lock);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001192
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001193 if (retcode != NO_ERROR)
1194 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001195
Philipp Reisner813472c2011-05-03 16:47:02 +02001196 if (new_plan) {
1197 old_plan = mdev->rs_plan_s;
1198 rcu_assign_pointer(mdev->rs_plan_s, new_plan);
Philipp Reisner9958c852011-05-03 16:19:31 +02001199 }
Philipp Reisner9958c852011-05-03 16:19:31 +02001200
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001201 mutex_unlock(&mdev->tconn->conf_update);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001202 drbd_md_sync(mdev);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001203
1204 if (mdev->state.conn >= C_CONNECTED)
1205 drbd_send_sync_param(mdev);
1206
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001207 synchronize_rcu();
1208 kfree(old_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02001209 kfree(old_plan);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001210 goto success;
1211
1212fail_unlock:
1213 mutex_unlock(&mdev->tconn->conf_update);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001214 fail:
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001215 kfree(new_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02001216 kfree(new_plan);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001217success:
1218 put_ldev(mdev);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001219 out:
1220 drbd_adm_finish(info, retcode);
1221 return 0;
1222}
1223
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001224int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001225{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001226 struct drbd_conf *mdev;
1227 int err;
Andreas Gruenbacher116676c2010-12-08 13:33:11 +01001228 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001229 enum determine_dev_size dd;
1230 sector_t max_possible_sectors;
1231 sector_t min_md_device_sectors;
1232 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001233 struct disk_conf *new_disk_conf = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001234 struct block_device *bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001235 struct lru_cache *resync_lru = NULL;
Philipp Reisner9958c852011-05-03 16:19:31 +02001236 struct fifo_buffer *new_plan = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001237 union drbd_state ns, os;
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001238 enum drbd_state_rv rv;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001239 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001240 int cp_discovered = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001241
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001242 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1243 if (!adm_ctx.reply_skb)
1244 return retcode;
1245 if (retcode != NO_ERROR)
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001246 goto finish;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001247
1248 mdev = adm_ctx.mdev;
Philipp Reisner0e29d162011-02-18 14:23:11 +01001249 conn_reconfig_start(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001250
1251 /* if you want to reconfigure, please tear down first */
1252 if (mdev->state.disk > D_DISKLESS) {
1253 retcode = ERR_DISK_CONFIGURED;
1254 goto fail;
1255 }
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001256 /* It may just now have detached because of IO error. Make sure
1257 * drbd_ldev_destroy is done already, we may end up here very fast,
1258 * e.g. if someone calls attach from the on-io-error handler,
1259 * to realize a "hot spare" feature (not that I'd recommend that) */
1260 wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001261
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001262 /* allocation not in the IO path, drbdsetup context */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001263 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1264 if (!nbc) {
1265 retcode = ERR_NOMEM;
1266 goto fail;
1267 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001268 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
1269 if (!new_disk_conf) {
1270 retcode = ERR_NOMEM;
1271 goto fail;
1272 }
1273 nbc->disk_conf = new_disk_conf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001274
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001275 set_disk_conf_defaults(new_disk_conf);
1276 err = disk_conf_from_attrs(new_disk_conf, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001277 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001278 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001279 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001280 goto fail;
1281 }
1282
Philipp Reisnerd589a212011-05-04 10:06:52 +02001283 enforce_disk_conf_limits(new_disk_conf);
1284
Philipp Reisner9958c852011-05-03 16:19:31 +02001285 new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
1286 if (!new_plan) {
1287 retcode = ERR_NOMEM;
1288 goto fail;
1289 }
1290
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001291 if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001292 retcode = ERR_MD_IDX_INVALID;
1293 goto fail;
1294 }
1295
Philipp Reisner44ed1672011-04-19 17:10:19 +02001296 rcu_read_lock();
1297 nc = rcu_dereference(mdev->tconn->net_conf);
1298 if (nc) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001299 if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02001300 rcu_read_unlock();
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001301 retcode = ERR_STONITH_AND_PROT_A;
1302 goto fail;
1303 }
1304 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02001305 rcu_read_unlock();
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001306
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001307 bdev = blkdev_get_by_path(new_disk_conf->backing_dev,
Tejun Heod4d77622010-11-13 11:55:18 +01001308 FMODE_READ | FMODE_WRITE | FMODE_EXCL, mdev);
Tejun Heoe525fd82010-11-13 11:55:17 +01001309 if (IS_ERR(bdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001310 dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->backing_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001311 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001312 retcode = ERR_OPEN_DISK;
1313 goto fail;
1314 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001315 nbc->backing_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001316
Tejun Heoe525fd82010-11-13 11:55:17 +01001317 /*
1318 * meta_dev_idx >= 0: external fixed size, possibly multiple
1319 * drbd sharing one meta device. TODO in that case, paranoia
1320 * check that [md_bdev, meta_dev_idx] is not yet used by some
1321 * other drbd minor! (if you use drbd.conf + drbdadm, that
1322 * should check it for you already; but if you don't, or
1323 * someone fooled it, we need to double check here)
1324 */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001325 bdev = blkdev_get_by_path(new_disk_conf->meta_dev,
Tejun Heod4d77622010-11-13 11:55:18 +01001326 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001327 (new_disk_conf->meta_dev_idx < 0) ?
Tejun Heod4d77622010-11-13 11:55:18 +01001328 (void *)mdev : (void *)drbd_m_holder);
Tejun Heoe525fd82010-11-13 11:55:17 +01001329 if (IS_ERR(bdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001330 dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->meta_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001331 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001332 retcode = ERR_OPEN_MD_DISK;
1333 goto fail;
1334 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001335 nbc->md_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001336
Tejun Heoe525fd82010-11-13 11:55:17 +01001337 if ((nbc->backing_bdev == nbc->md_bdev) !=
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001338 (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1339 new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001340 retcode = ERR_MD_IDX_INVALID;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001341 goto fail;
1342 }
1343
1344 resync_lru = lc_create("resync", drbd_bm_ext_cache,
Lars Ellenberg46a15bc2011-02-21 13:21:01 +01001345 1, 61, sizeof(struct bm_extent),
Philipp Reisnerb411b362009-09-25 16:07:19 -07001346 offsetof(struct bm_extent, lce));
1347 if (!resync_lru) {
1348 retcode = ERR_NOMEM;
Tejun Heoe525fd82010-11-13 11:55:17 +01001349 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001350 }
1351
1352 /* RT - for drbd_get_max_capacity() DRBD_MD_INDEX_FLEX_INT */
1353 drbd_md_set_sector_offsets(mdev, nbc);
1354
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001355 if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001356 dev_err(DEV, "max capacity %llu smaller than disk size %llu\n",
1357 (unsigned long long) drbd_get_max_capacity(nbc),
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001358 (unsigned long long) new_disk_conf->disk_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001359 retcode = ERR_DISK_TO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001360 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001361 }
1362
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001363 if (new_disk_conf->meta_dev_idx < 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001364 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1365 /* at least one MB, otherwise it does not make sense */
1366 min_md_device_sectors = (2<<10);
1367 } else {
1368 max_possible_sectors = DRBD_MAX_SECTORS;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001369 min_md_device_sectors = MD_RESERVED_SECT * (new_disk_conf->meta_dev_idx + 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001370 }
1371
Philipp Reisnerb411b362009-09-25 16:07:19 -07001372 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
1373 retcode = ERR_MD_DISK_TO_SMALL;
1374 dev_warn(DEV, "refusing attach: md-device too small, "
1375 "at least %llu sectors needed for this meta-disk type\n",
1376 (unsigned long long) min_md_device_sectors);
Tejun Heoe525fd82010-11-13 11:55:17 +01001377 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001378 }
1379
1380 /* Make sure the new disk is big enough
1381 * (we may currently be R_PRIMARY with no local disk...) */
1382 if (drbd_get_max_capacity(nbc) <
1383 drbd_get_capacity(mdev->this_bdev)) {
1384 retcode = ERR_DISK_TO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001385 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001386 }
1387
1388 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1389
Lars Ellenberg13529942009-10-12 19:07:49 +02001390 if (nbc->known_size > max_possible_sectors) {
1391 dev_warn(DEV, "==> truncating very big lower level device "
1392 "to currently maximum possible %llu sectors <==\n",
1393 (unsigned long long) max_possible_sectors);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001394 if (new_disk_conf->meta_dev_idx >= 0)
Lars Ellenberg13529942009-10-12 19:07:49 +02001395 dev_warn(DEV, "==>> using internal or flexible "
1396 "meta data may help <<==\n");
1397 }
1398
Philipp Reisnerb411b362009-09-25 16:07:19 -07001399 drbd_suspend_io(mdev);
1400 /* also wait for the last barrier ack. */
Philipp Reisner2aebfab2011-03-28 16:48:11 +02001401 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_pending_cnt) || drbd_suspended(mdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001402 /* and for any other previously queued work */
Philipp Reisnera21e9292011-02-08 15:08:49 +01001403 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001404
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001405 rv = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE);
1406 retcode = rv; /* FIXME: Type mismatch. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001407 drbd_resume_io(mdev);
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001408 if (rv < SS_SUCCESS)
Tejun Heoe525fd82010-11-13 11:55:17 +01001409 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001410
1411 if (!get_ldev_if_state(mdev, D_ATTACHING))
1412 goto force_diskless;
1413
1414 drbd_md_set_sector_offsets(mdev, nbc);
1415
1416 if (!mdev->bitmap) {
1417 if (drbd_bm_init(mdev)) {
1418 retcode = ERR_NOMEM;
1419 goto force_diskless_dec;
1420 }
1421 }
1422
1423 retcode = drbd_md_read(mdev, nbc);
1424 if (retcode != NO_ERROR)
1425 goto force_diskless_dec;
1426
1427 if (mdev->state.conn < C_CONNECTED &&
1428 mdev->state.role == R_PRIMARY &&
1429 (mdev->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
1430 dev_err(DEV, "Can only attach to data with current UUID=%016llX\n",
1431 (unsigned long long)mdev->ed_uuid);
1432 retcode = ERR_DATA_NOT_CURRENT;
1433 goto force_diskless_dec;
1434 }
1435
1436 /* Since we are diskless, fix the activity log first... */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001437 if (drbd_check_al_size(mdev, new_disk_conf)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001438 retcode = ERR_NOMEM;
1439 goto force_diskless_dec;
1440 }
1441
1442 /* Prevent shrinking of consistent devices ! */
1443 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001444 drbd_new_dev_size(mdev, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001445 dev_warn(DEV, "refusing to truncate a consistent device\n");
1446 retcode = ERR_DISK_TO_SMALL;
1447 goto force_diskless_dec;
1448 }
1449
1450 if (!drbd_al_read_log(mdev, nbc)) {
1451 retcode = ERR_IO_MD_DISK;
1452 goto force_diskless_dec;
1453 }
1454
Philipp Reisnerb411b362009-09-25 16:07:19 -07001455 /* Reset the "barriers don't work" bits here, then force meta data to
1456 * be written, to ensure we determine if barriers are supported. */
Andreas Gruenbachere5440462011-05-04 15:25:35 +02001457 if (new_disk_conf->md_flushes)
Philipp Reisnera8a4e512010-08-25 10:21:04 +02001458 clear_bit(MD_NO_FUA, &mdev->flags);
Andreas Gruenbachere5440462011-05-04 15:25:35 +02001459 else
1460 set_bit(MD_NO_FUA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001461
1462 /* Point of no return reached.
1463 * Devices and memory are no longer released by error cleanup below.
1464 * now mdev takes over responsibility, and the state engine should
1465 * clean it up somewhere. */
1466 D_ASSERT(mdev->ldev == NULL);
1467 mdev->ldev = nbc;
1468 mdev->resync = resync_lru;
Philipp Reisner9958c852011-05-03 16:19:31 +02001469 mdev->rs_plan_s = new_plan;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001470 nbc = NULL;
1471 resync_lru = NULL;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001472 new_disk_conf = NULL;
Philipp Reisner9958c852011-05-03 16:19:31 +02001473 new_plan = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001474
Philipp Reisner2451fc32010-08-24 13:43:11 +02001475 mdev->write_ordering = WO_bdev_flush;
1476 drbd_bump_write_ordering(mdev, WO_bdev_flush);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001477
1478 if (drbd_md_test_flag(mdev->ldev, MDF_CRASHED_PRIMARY))
1479 set_bit(CRASHED_PRIMARY, &mdev->flags);
1480 else
1481 clear_bit(CRASHED_PRIMARY, &mdev->flags);
1482
Philipp Reisner894c6a92010-06-18 16:03:20 +02001483 if (drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
Philipp Reisnerda9fbc22011-03-29 10:52:01 +02001484 !(mdev->state.role == R_PRIMARY && mdev->tconn->susp_nod)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001485 set_bit(CRASHED_PRIMARY, &mdev->flags);
1486 cp_discovered = 1;
1487 }
1488
1489 mdev->send_cnt = 0;
1490 mdev->recv_cnt = 0;
1491 mdev->read_cnt = 0;
1492 mdev->writ_cnt = 0;
1493
Philipp Reisner99432fc2011-05-20 16:39:13 +02001494 drbd_reconsider_max_bio_size(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001495
1496 /* If I am currently not R_PRIMARY,
1497 * but meta data primary indicator is set,
1498 * I just now recover from a hard crash,
1499 * and have been R_PRIMARY before that crash.
1500 *
1501 * Now, if I had no connection before that crash
1502 * (have been degraded R_PRIMARY), chances are that
1503 * I won't find my peer now either.
1504 *
1505 * In that case, and _only_ in that case,
1506 * we use the degr-wfc-timeout instead of the default,
1507 * so we can automatically recover from a crash of a
1508 * degraded but active "cluster" after a certain timeout.
1509 */
1510 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
1511 if (mdev->state.role != R_PRIMARY &&
1512 drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
1513 !drbd_md_test_flag(mdev->ldev, MDF_CONNECTED_IND))
1514 set_bit(USE_DEGR_WFC_T, &mdev->flags);
1515
Bart Van Assche24c48302011-05-21 18:32:29 +02001516 dd = drbd_determine_dev_size(mdev, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001517 if (dd == dev_size_error) {
1518 retcode = ERR_NOMEM_BITMAP;
1519 goto force_diskless_dec;
1520 } else if (dd == grew)
1521 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
1522
1523 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
1524 dev_info(DEV, "Assuming that all blocks are out of sync "
1525 "(aka FullSync)\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001526 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write,
1527 "set_n_write from attaching", BM_LOCKED_MASK)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001528 retcode = ERR_IO_MD_DISK;
1529 goto force_diskless_dec;
1530 }
1531 } else {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001532 if (drbd_bitmap_io(mdev, &drbd_bm_read,
Andreas Gruenbacher22ab6a32010-12-13 01:44:11 +01001533 "read from attaching", BM_LOCKED_MASK)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001534 retcode = ERR_IO_MD_DISK;
1535 goto force_diskless_dec;
1536 }
1537 }
1538
1539 if (cp_discovered) {
1540 drbd_al_apply_to_bm(mdev);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001541 if (drbd_bitmap_io(mdev, &drbd_bm_write,
1542 "crashed primary apply AL", BM_LOCKED_MASK)) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001543 retcode = ERR_IO_MD_DISK;
1544 goto force_diskless_dec;
1545 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001546 }
1547
Philipp Reisner07782862010-08-31 12:00:50 +02001548 if (_drbd_bm_total_weight(mdev) == drbd_bm_bits(mdev))
1549 drbd_suspend_al(mdev); /* IO is still suspended here... */
1550
Philipp Reisner87eeee42011-01-19 14:16:30 +01001551 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner78bae592011-03-28 15:40:12 +02001552 os = drbd_read_state(mdev);
1553 ns = os;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001554 /* If MDF_CONSISTENT is not set go into inconsistent state,
1555 otherwise investigate MDF_WasUpToDate...
1556 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1557 otherwise into D_CONSISTENT state.
1558 */
1559 if (drbd_md_test_flag(mdev->ldev, MDF_CONSISTENT)) {
1560 if (drbd_md_test_flag(mdev->ldev, MDF_WAS_UP_TO_DATE))
1561 ns.disk = D_CONSISTENT;
1562 else
1563 ns.disk = D_OUTDATED;
1564 } else {
1565 ns.disk = D_INCONSISTENT;
1566 }
1567
1568 if (drbd_md_test_flag(mdev->ldev, MDF_PEER_OUT_DATED))
1569 ns.pdsk = D_OUTDATED;
1570
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001571 rcu_read_lock();
1572 if (ns.disk == D_CONSISTENT &&
1573 (ns.pdsk == D_OUTDATED || rcu_dereference(mdev->ldev->disk_conf)->fencing == FP_DONT_CARE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001574 ns.disk = D_UP_TO_DATE;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001575 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001576
1577 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1578 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1579 this point, because drbd_request_state() modifies these
1580 flags. */
1581
1582 /* In case we are C_CONNECTED postpone any decision on the new disk
1583 state after the negotiation phase. */
1584 if (mdev->state.conn == C_CONNECTED) {
1585 mdev->new_state_tmp.i = ns.i;
1586 ns.i = os.i;
1587 ns.disk = D_NEGOTIATING;
Philipp Reisnerdc66c742010-06-02 14:31:29 +02001588
1589 /* We expect to receive up-to-date UUIDs soon.
1590 To avoid a race in receive_state, free p_uuid while
1591 holding req_lock. I.e. atomic with the state change */
1592 kfree(mdev->p_uuid);
1593 mdev->p_uuid = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001594 }
1595
1596 rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001597 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001598
1599 if (rv < SS_SUCCESS)
1600 goto force_diskless_dec;
1601
1602 if (mdev->state.role == R_PRIMARY)
1603 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
1604 else
1605 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
1606
1607 drbd_md_mark_dirty(mdev);
1608 drbd_md_sync(mdev);
1609
1610 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1611 put_ldev(mdev);
Philipp Reisner0e29d162011-02-18 14:23:11 +01001612 conn_reconfig_done(mdev->tconn);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001613 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001614 return 0;
1615
1616 force_diskless_dec:
1617 put_ldev(mdev);
1618 force_diskless:
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001619 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001620 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001621 fail:
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001622 conn_reconfig_done(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001623 if (nbc) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001624 if (nbc->backing_bdev)
1625 blkdev_put(nbc->backing_bdev,
1626 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1627 if (nbc->md_bdev)
1628 blkdev_put(nbc->md_bdev,
1629 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001630 kfree(nbc);
1631 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001632 kfree(new_disk_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001633 lc_destroy(resync_lru);
Philipp Reisner9958c852011-05-03 16:19:31 +02001634 kfree(new_plan);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001635
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001636 finish:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001637 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001638 return 0;
1639}
1640
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001641static int adm_detach(struct drbd_conf *mdev)
1642{
Philipp Reisner19f83c72011-03-29 14:21:03 +02001643 enum drbd_state_rv retcode;
Lars Ellenberg009ba892011-05-02 11:51:31 +02001644 int ret;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001645 drbd_suspend_io(mdev); /* so no-one is stuck in drbd_al_begin_io */
Lars Ellenberg009ba892011-05-02 11:51:31 +02001646 retcode = drbd_request_state(mdev, NS(disk, D_FAILED));
1647 /* D_FAILED will transition to DISKLESS. */
1648 ret = wait_event_interruptible(mdev->misc_wait,
1649 mdev->state.disk != D_FAILED);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001650 drbd_resume_io(mdev);
Lars Ellenberg009ba892011-05-02 11:51:31 +02001651 if ((int)retcode == (int)SS_IS_DISKLESS)
1652 retcode = SS_NOTHING_TO_DO;
1653 if (ret)
1654 retcode = ERR_INTR;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001655 return retcode;
1656}
1657
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001658/* Detaching the disk is a process in multiple stages. First we need to lock
1659 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1660 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1661 * internal references as well.
1662 * Only then we have finally detached. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001663int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001664{
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001665 enum drbd_ret_code retcode;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001666
1667 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1668 if (!adm_ctx.reply_skb)
1669 return retcode;
1670 if (retcode != NO_ERROR)
1671 goto out;
1672
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001673 retcode = adm_detach(adm_ctx.mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001674out:
1675 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001676 return 0;
1677}
1678
Lars Ellenbergf3990022011-03-23 14:31:09 +01001679static bool conn_resync_running(struct drbd_tconn *tconn)
1680{
1681 struct drbd_conf *mdev;
Philipp Reisner695d08f2011-04-11 22:53:32 -07001682 bool rv = false;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001683 int vnr;
1684
Philipp Reisner695d08f2011-04-11 22:53:32 -07001685 rcu_read_lock();
Lars Ellenbergf3990022011-03-23 14:31:09 +01001686 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1687 if (mdev->state.conn == C_SYNC_SOURCE ||
1688 mdev->state.conn == C_SYNC_TARGET ||
1689 mdev->state.conn == C_PAUSED_SYNC_S ||
Philipp Reisner695d08f2011-04-11 22:53:32 -07001690 mdev->state.conn == C_PAUSED_SYNC_T) {
1691 rv = true;
1692 break;
1693 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001694 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001695 rcu_read_unlock();
1696
1697 return rv;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001698}
1699
1700static bool conn_ov_running(struct drbd_tconn *tconn)
1701{
1702 struct drbd_conf *mdev;
Philipp Reisner695d08f2011-04-11 22:53:32 -07001703 bool rv = false;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001704 int vnr;
1705
Philipp Reisner695d08f2011-04-11 22:53:32 -07001706 rcu_read_lock();
Lars Ellenbergf3990022011-03-23 14:31:09 +01001707 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1708 if (mdev->state.conn == C_VERIFY_S ||
Philipp Reisner695d08f2011-04-11 22:53:32 -07001709 mdev->state.conn == C_VERIFY_T) {
1710 rv = true;
1711 break;
1712 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001713 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001714 rcu_read_unlock();
1715
1716 return rv;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001717}
1718
Philipp Reisnercd643972011-04-13 18:00:59 -07001719static enum drbd_ret_code
Philipp Reisner44ed1672011-04-19 17:10:19 +02001720_check_net_options(struct drbd_tconn *tconn, struct net_conf *old_conf, struct net_conf *new_conf)
Philipp Reisnercd643972011-04-13 18:00:59 -07001721{
1722 struct drbd_conf *mdev;
1723 int i;
1724
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02001725 if (old_conf && tconn->cstate == C_WF_REPORT_PARAMS && tconn->agreed_pro_version < 100) {
1726 if (new_conf->wire_protocol != old_conf->wire_protocol)
1727 return ERR_NEED_APV_100;
1728
1729 if (new_conf->two_primaries != old_conf->two_primaries)
1730 return ERR_NEED_APV_100;
1731
1732 if (!new_conf->integrity_alg != !old_conf->integrity_alg)
1733 return ERR_NEED_APV_100;
1734
1735 if (strcmp(new_conf->integrity_alg, old_conf->integrity_alg))
1736 return ERR_NEED_APV_100;
1737 }
1738
1739 if (!new_conf->two_primaries &&
1740 conn_highest_role(tconn) == R_PRIMARY &&
1741 conn_highest_peer(tconn) == R_PRIMARY)
1742 return ERR_NEED_ALLOW_TWO_PRI;
Philipp Reisnerb032b6f2011-04-13 18:16:10 -07001743
Philipp Reisnercd643972011-04-13 18:00:59 -07001744 if (new_conf->two_primaries &&
1745 (new_conf->wire_protocol != DRBD_PROT_C))
1746 return ERR_NOT_PROTO_C;
1747
Philipp Reisnercd643972011-04-13 18:00:59 -07001748 idr_for_each_entry(&tconn->volumes, mdev, i) {
1749 if (get_ldev(mdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001750 enum drbd_fencing_p fp = rcu_dereference(mdev->ldev->disk_conf)->fencing;
Philipp Reisnercd643972011-04-13 18:00:59 -07001751 put_ldev(mdev);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001752 if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
Philipp Reisnercd643972011-04-13 18:00:59 -07001753 return ERR_STONITH_AND_PROT_A;
Philipp Reisnercd643972011-04-13 18:00:59 -07001754 }
Andreas Gruenbacher6139f602011-05-06 20:00:02 +02001755 if (mdev->state.role == R_PRIMARY && new_conf->discard_my_data)
Philipp Reisnercd643972011-04-13 18:00:59 -07001756 return ERR_DISCARD;
Philipp Reisnercd643972011-04-13 18:00:59 -07001757 }
Philipp Reisnercd643972011-04-13 18:00:59 -07001758
1759 if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A)
1760 return ERR_CONG_NOT_PROTO_A;
1761
1762 return NO_ERROR;
1763}
1764
Philipp Reisner44ed1672011-04-19 17:10:19 +02001765static enum drbd_ret_code
1766check_net_options(struct drbd_tconn *tconn, struct net_conf *new_conf)
1767{
1768 static enum drbd_ret_code rv;
1769 struct drbd_conf *mdev;
1770 int i;
1771
1772 rcu_read_lock();
1773 rv = _check_net_options(tconn, rcu_dereference(tconn->net_conf), new_conf);
1774 rcu_read_unlock();
1775
1776 /* tconn->volumes protected by genl_lock() here */
1777 idr_for_each_entry(&tconn->volumes, mdev, i) {
1778 if (!mdev->bitmap) {
1779 if(drbd_bm_init(mdev))
1780 return ERR_NOMEM;
1781 }
1782 }
1783
1784 return rv;
1785}
1786
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001787struct crypto {
1788 struct crypto_hash *verify_tfm;
1789 struct crypto_hash *csums_tfm;
1790 struct crypto_hash *cram_hmac_tfm;
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02001791 struct crypto_hash *integrity_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001792 void *int_dig_in;
1793 void *int_dig_vv;
1794};
1795
1796static int
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02001797alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001798{
1799 if (!tfm_name[0])
1800 return NO_ERROR;
1801
1802 *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
1803 if (IS_ERR(*tfm)) {
1804 *tfm = NULL;
1805 return err_alg;
1806 }
1807
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001808 return NO_ERROR;
1809}
1810
1811static enum drbd_ret_code
1812alloc_crypto(struct crypto *crypto, struct net_conf *new_conf)
1813{
1814 char hmac_name[CRYPTO_MAX_ALG_NAME];
1815 enum drbd_ret_code rv;
1816 int hash_size;
1817
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02001818 rv = alloc_hash(&crypto->csums_tfm, new_conf->csums_alg,
1819 ERR_CSUMS_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001820 if (rv != NO_ERROR)
1821 return rv;
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02001822 rv = alloc_hash(&crypto->verify_tfm, new_conf->verify_alg,
1823 ERR_VERIFY_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001824 if (rv != NO_ERROR)
1825 return rv;
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02001826 rv = alloc_hash(&crypto->integrity_tfm, new_conf->integrity_alg,
1827 ERR_INTEGRITY_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001828 if (rv != NO_ERROR)
1829 return rv;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001830 if (new_conf->cram_hmac_alg[0] != 0) {
1831 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
1832 new_conf->cram_hmac_alg);
1833
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02001834 rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
1835 ERR_AUTH_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001836 }
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02001837 if (crypto->integrity_tfm) {
1838 hash_size = crypto_hash_digestsize(crypto->integrity_tfm);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001839 crypto->int_dig_in = kmalloc(hash_size, GFP_KERNEL);
1840 if (!crypto->int_dig_in)
1841 return ERR_NOMEM;
1842 crypto->int_dig_vv = kmalloc(hash_size, GFP_KERNEL);
1843 if (!crypto->int_dig_vv)
1844 return ERR_NOMEM;
1845 }
1846
1847 return rv;
1848}
1849
1850static void free_crypto(struct crypto *crypto)
1851{
1852 kfree(crypto->int_dig_in);
1853 kfree(crypto->int_dig_vv);
1854 crypto_free_hash(crypto->cram_hmac_tfm);
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02001855 crypto_free_hash(crypto->integrity_tfm);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001856 crypto_free_hash(crypto->csums_tfm);
1857 crypto_free_hash(crypto->verify_tfm);
1858}
1859
Lars Ellenbergf3990022011-03-23 14:31:09 +01001860int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
1861{
1862 enum drbd_ret_code retcode;
1863 struct drbd_tconn *tconn;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001864 struct net_conf *old_conf, *new_conf = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001865 int err;
1866 int ovr; /* online verify running */
1867 int rsr; /* re-sync running */
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001868 struct crypto crypto = { };
Lars Ellenbergf3990022011-03-23 14:31:09 +01001869
1870 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
1871 if (!adm_ctx.reply_skb)
1872 return retcode;
1873 if (retcode != NO_ERROR)
1874 goto out;
1875
1876 tconn = adm_ctx.tconn;
1877
1878 new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
1879 if (!new_conf) {
1880 retcode = ERR_NOMEM;
1881 goto out;
1882 }
1883
Lars Ellenbergf3990022011-03-23 14:31:09 +01001884 conn_reconfig_start(tconn);
1885
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001886 mutex_lock(&tconn->data.mutex);
Philipp Reisnera0095502011-05-03 13:14:15 +02001887 mutex_lock(&tconn->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001888 old_conf = tconn->net_conf;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001889
1890 if (!old_conf) {
1891 drbd_msg_put_info("net conf missing, try connect");
1892 retcode = ERR_INVALID_REQUEST;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001893 goto fail;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001894 }
1895
1896 *new_conf = *old_conf;
Lars Ellenberg5979e362011-04-27 21:09:55 +02001897 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02001898 set_net_conf_defaults(new_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001899
Lars Ellenbergf3990022011-03-23 14:31:09 +01001900 err = net_conf_from_attrs_for_change(new_conf, info);
1901 if (err) {
1902 retcode = ERR_MANDATORY_TAG;
1903 drbd_msg_put_info(from_attrs_err_to_txt(err));
1904 goto fail;
1905 }
1906
Philipp Reisnercd643972011-04-13 18:00:59 -07001907 retcode = check_net_options(tconn, new_conf);
1908 if (retcode != NO_ERROR)
1909 goto fail;
1910
Lars Ellenbergf3990022011-03-23 14:31:09 +01001911 /* re-sync running */
1912 rsr = conn_resync_running(tconn);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001913 if (rsr && strcmp(new_conf->csums_alg, old_conf->csums_alg)) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001914 retcode = ERR_CSUMS_RESYNC_RUNNING;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001915 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001916 }
1917
Lars Ellenbergf3990022011-03-23 14:31:09 +01001918 /* online verify running */
1919 ovr = conn_ov_running(tconn);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001920 if (ovr && strcmp(new_conf->verify_alg, old_conf->verify_alg)) {
1921 retcode = ERR_VERIFY_RUNNING;
1922 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001923 }
1924
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001925 retcode = alloc_crypto(&crypto, new_conf);
1926 if (retcode != NO_ERROR)
1927 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001928
Philipp Reisner44ed1672011-04-19 17:10:19 +02001929 rcu_assign_pointer(tconn->net_conf, new_conf);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001930
1931 if (!rsr) {
1932 crypto_free_hash(tconn->csums_tfm);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001933 tconn->csums_tfm = crypto.csums_tfm;
1934 crypto.csums_tfm = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001935 }
1936 if (!ovr) {
1937 crypto_free_hash(tconn->verify_tfm);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001938 tconn->verify_tfm = crypto.verify_tfm;
1939 crypto.verify_tfm = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001940 }
1941
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001942 kfree(tconn->int_dig_in);
1943 tconn->int_dig_in = crypto.int_dig_in;
1944 kfree(tconn->int_dig_vv);
1945 tconn->int_dig_vv = crypto.int_dig_vv;
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02001946 crypto_free_hash(tconn->integrity_tfm);
1947 tconn->integrity_tfm = crypto.integrity_tfm;
Philipp Reisnerd659f2a2011-05-16 17:38:45 +02001948 if (tconn->cstate >= C_WF_REPORT_PARAMS && tconn->agreed_pro_version >= 100)
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001949 /* Do this without trying to take tconn->data.mutex again. */
Philipp Reisnerd659f2a2011-05-16 17:38:45 +02001950 __drbd_send_protocol(tconn, P_PROTOCOL_UPDATE);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001951
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001952 crypto_free_hash(tconn->cram_hmac_tfm);
1953 tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
1954
Philipp Reisnera0095502011-05-03 13:14:15 +02001955 mutex_unlock(&tconn->conf_update);
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001956 mutex_unlock(&tconn->data.mutex);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001957 synchronize_rcu();
1958 kfree(old_conf);
1959
Lars Ellenbergf3990022011-03-23 14:31:09 +01001960 if (tconn->cstate >= C_WF_REPORT_PARAMS)
1961 drbd_send_sync_param(minor_to_mdev(conn_lowest_minor(tconn)));
1962
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001963 goto done;
1964
Lars Ellenbergf3990022011-03-23 14:31:09 +01001965 fail:
Philipp Reisnera0095502011-05-03 13:14:15 +02001966 mutex_unlock(&tconn->conf_update);
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001967 mutex_unlock(&tconn->data.mutex);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001968 free_crypto(&crypto);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001969 kfree(new_conf);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001970 done:
Lars Ellenbergf3990022011-03-23 14:31:09 +01001971 conn_reconfig_done(tconn);
1972 out:
1973 drbd_adm_finish(info, retcode);
1974 return 0;
1975}
1976
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001977int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001978{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001979 struct drbd_conf *mdev;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001980 struct net_conf *old_conf, *new_conf = NULL;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001981 struct crypto crypto = { };
Philipp Reisner80883192011-02-18 14:56:45 +01001982 struct drbd_tconn *oconn;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001983 struct drbd_tconn *tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001984 struct sockaddr *new_my_addr, *new_peer_addr, *taken_addr;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001985 enum drbd_ret_code retcode;
1986 int i;
1987 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001988
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001989 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
1990 if (!adm_ctx.reply_skb)
1991 return retcode;
1992 if (retcode != NO_ERROR)
1993 goto out;
1994
1995 tconn = adm_ctx.tconn;
Philipp Reisner80883192011-02-18 14:56:45 +01001996 conn_reconfig_start(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001997
Philipp Reisner80883192011-02-18 14:56:45 +01001998 if (tconn->cstate > C_STANDALONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001999 retcode = ERR_NET_CONFIGURED;
2000 goto fail;
2001 }
2002
2003 /* allocation not in the IO path, cqueue thread context */
Lars Ellenberg5979e362011-04-27 21:09:55 +02002004 new_conf = kzalloc(sizeof(*new_conf), GFP_KERNEL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002005 if (!new_conf) {
2006 retcode = ERR_NOMEM;
2007 goto fail;
2008 }
2009
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02002010 set_net_conf_defaults(new_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002011
Lars Ellenbergf3990022011-03-23 14:31:09 +01002012 err = net_conf_from_attrs(new_conf, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002013 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002014 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002015 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002016 goto fail;
2017 }
2018
Philipp Reisnercd643972011-04-13 18:00:59 -07002019 retcode = check_net_options(tconn, new_conf);
2020 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002021 goto fail;
Philipp Reisner422028b2010-10-27 11:12:07 +02002022
Philipp Reisnerb411b362009-09-25 16:07:19 -07002023 retcode = NO_ERROR;
2024
2025 new_my_addr = (struct sockaddr *)&new_conf->my_addr;
2026 new_peer_addr = (struct sockaddr *)&new_conf->peer_addr;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002027
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02002028 /* No need for _rcu here. All reconfiguration is
Lars Ellenberg543cc102011-03-10 22:18:18 +01002029 * strictly serialized on genl_lock(). We are protected against
2030 * concurrent reconfiguration/addition/deletion */
Philipp Reisner80883192011-02-18 14:56:45 +01002031 list_for_each_entry(oconn, &drbd_tconns, all_tconn) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02002032 struct net_conf *nc;
Philipp Reisner80883192011-02-18 14:56:45 +01002033 if (oconn == tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002034 continue;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002035
2036 rcu_read_lock();
2037 nc = rcu_dereference(oconn->net_conf);
2038 if (nc) {
2039 taken_addr = (struct sockaddr *)&nc->my_addr;
2040 if (new_conf->my_addr_len == nc->my_addr_len &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07002041 !memcmp(new_my_addr, taken_addr, new_conf->my_addr_len))
2042 retcode = ERR_LOCAL_ADDR;
2043
Philipp Reisner44ed1672011-04-19 17:10:19 +02002044 taken_addr = (struct sockaddr *)&nc->peer_addr;
2045 if (new_conf->peer_addr_len == nc->peer_addr_len &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07002046 !memcmp(new_peer_addr, taken_addr, new_conf->peer_addr_len))
2047 retcode = ERR_PEER_ADDR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002048 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002049 rcu_read_unlock();
2050 if (retcode != NO_ERROR)
2051 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002052 }
2053
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002054 retcode = alloc_crypto(&crypto, new_conf);
2055 if (retcode != NO_ERROR)
2056 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002057
Philipp Reisnerb411b362009-09-25 16:07:19 -07002058 ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
2059
Philipp Reisner80883192011-02-18 14:56:45 +01002060 conn_flush_workqueue(tconn);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002061
Philipp Reisnera0095502011-05-03 13:14:15 +02002062 mutex_lock(&tconn->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002063 old_conf = tconn->net_conf;
2064 if (old_conf) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002065 retcode = ERR_NET_CONFIGURED;
Philipp Reisnera0095502011-05-03 13:14:15 +02002066 mutex_unlock(&tconn->conf_update);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002067 goto fail;
2068 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002069 rcu_assign_pointer(tconn->net_conf, new_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002070
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002071 conn_free_crypto(tconn);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002072 tconn->int_dig_in = crypto.int_dig_in;
2073 tconn->int_dig_vv = crypto.int_dig_vv;
2074 tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02002075 tconn->integrity_tfm = crypto.integrity_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002076 tconn->csums_tfm = crypto.csums_tfm;
2077 tconn->verify_tfm = crypto.verify_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002078
Philipp Reisnera0095502011-05-03 13:14:15 +02002079 mutex_unlock(&tconn->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002080
Philipp Reisner695d08f2011-04-11 22:53:32 -07002081 rcu_read_lock();
Philipp Reisner80883192011-02-18 14:56:45 +01002082 idr_for_each_entry(&tconn->volumes, mdev, i) {
2083 mdev->send_cnt = 0;
2084 mdev->recv_cnt = 0;
Philipp Reisner80883192011-02-18 14:56:45 +01002085 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07002086 rcu_read_unlock();
Lars Ellenberg5ee743e2011-04-26 16:22:25 +02002087
2088 retcode = conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
2089
Philipp Reisner80883192011-02-18 14:56:45 +01002090 conn_reconfig_done(tconn);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002091 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002092 return 0;
2093
2094fail:
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002095 free_crypto(&crypto);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002096 kfree(new_conf);
2097
Philipp Reisner80883192011-02-18 14:56:45 +01002098 conn_reconfig_done(tconn);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002099out:
2100 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002101 return 0;
2102}
2103
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002104static enum drbd_state_rv conn_try_disconnect(struct drbd_tconn *tconn, bool force)
2105{
2106 enum drbd_state_rv rv;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002107
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002108 rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
2109 force ? CS_HARD : 0);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002110
2111 switch (rv) {
2112 case SS_NOTHING_TO_DO:
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002113 break;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002114 case SS_ALREADY_STANDALONE:
2115 return SS_SUCCESS;
2116 case SS_PRIMARY_NOP:
2117 /* Our state checking code wants to see the peer outdated. */
2118 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002119 pdsk, D_OUTDATED), CS_VERBOSE);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002120 break;
2121 case SS_CW_FAILED_BY_PEER:
2122 /* The peer probably wants to see us outdated. */
2123 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
2124 disk, D_OUTDATED), 0);
2125 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002126 rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
2127 CS_HARD);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002128 }
2129 break;
2130 default:;
2131 /* no special handling necessary */
2132 }
2133
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002134 if (rv >= SS_SUCCESS) {
2135 enum drbd_state_rv rv2;
2136 /* No one else can reconfigure the network while I am here.
2137 * The state handling only uses drbd_thread_stop_nowait(),
2138 * we want to really wait here until the receiver is no more.
2139 */
2140 drbd_thread_stop(&adm_ctx.tconn->receiver);
2141
2142 /* Race breaker. This additional state change request may be
2143 * necessary, if this was a forced disconnect during a receiver
2144 * restart. We may have "killed" the receiver thread just
2145 * after drbdd_init() returned. Typically, we should be
2146 * C_STANDALONE already, now, and this becomes a no-op.
2147 */
2148 rv2 = conn_request_state(tconn, NS(conn, C_STANDALONE),
2149 CS_VERBOSE | CS_HARD);
2150 if (rv2 < SS_SUCCESS)
2151 conn_err(tconn,
2152 "unexpected rv2=%d in conn_try_disconnect()\n",
2153 rv2);
2154 }
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002155 return rv;
2156}
2157
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002158int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002159{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002160 struct disconnect_parms parms;
2161 struct drbd_tconn *tconn;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002162 enum drbd_state_rv rv;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002163 enum drbd_ret_code retcode;
2164 int err;
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002165
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002166 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
2167 if (!adm_ctx.reply_skb)
2168 return retcode;
2169 if (retcode != NO_ERROR)
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002170 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002171
2172 tconn = adm_ctx.tconn;
2173 memset(&parms, 0, sizeof(parms));
2174 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002175 err = disconnect_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002176 if (err) {
2177 retcode = ERR_MANDATORY_TAG;
2178 drbd_msg_put_info(from_attrs_err_to_txt(err));
2179 goto fail;
2180 }
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002181 }
2182
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002183 rv = conn_try_disconnect(tconn, parms.force_disconnect);
2184 if (rv < SS_SUCCESS)
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002185 retcode = rv; /* FIXME: Type mismatch. */
2186 else
2187 retcode = NO_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002188 fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002189 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002190 return 0;
2191}
2192
2193void resync_after_online_grow(struct drbd_conf *mdev)
2194{
2195 int iass; /* I am sync source */
2196
2197 dev_info(DEV, "Resync of new storage after online grow\n");
2198 if (mdev->state.role != mdev->state.peer)
2199 iass = (mdev->state.role == R_PRIMARY);
2200 else
Philipp Reisner25703f82011-02-07 14:35:25 +01002201 iass = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002202
2203 if (iass)
2204 drbd_start_resync(mdev, C_SYNC_SOURCE);
2205 else
2206 _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
2207}
2208
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002209int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002210{
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002211 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002212 struct resize_parms rs;
2213 struct drbd_conf *mdev;
2214 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002215 enum determine_dev_size dd;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002216 enum dds_flags ddsf;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002217 sector_t u_size;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002218 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002219
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002220 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2221 if (!adm_ctx.reply_skb)
2222 return retcode;
2223 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002224 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002225
2226 memset(&rs, 0, sizeof(struct resize_parms));
2227 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002228 err = resize_parms_from_attrs(&rs, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002229 if (err) {
2230 retcode = ERR_MANDATORY_TAG;
2231 drbd_msg_put_info(from_attrs_err_to_txt(err));
2232 goto fail;
2233 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002234 }
2235
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002236 mdev = adm_ctx.mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002237 if (mdev->state.conn > C_CONNECTED) {
2238 retcode = ERR_RESIZE_RESYNC;
2239 goto fail;
2240 }
2241
2242 if (mdev->state.role == R_SECONDARY &&
2243 mdev->state.peer == R_SECONDARY) {
2244 retcode = ERR_NO_PRIMARY;
2245 goto fail;
2246 }
2247
2248 if (!get_ldev(mdev)) {
2249 retcode = ERR_NO_DISK;
2250 goto fail;
2251 }
2252
Philipp Reisner31890f42011-01-19 14:12:51 +01002253 if (rs.no_resync && mdev->tconn->agreed_pro_version < 93) {
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002254 retcode = ERR_NEED_APV_93;
2255 goto fail;
2256 }
2257
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002258 rcu_read_lock();
2259 u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
2260 rcu_read_unlock();
2261 if (u_size != (sector_t)rs.resize_size) {
2262 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
2263 if (!new_disk_conf) {
2264 retcode = ERR_NOMEM;
2265 goto fail;
2266 }
2267 }
2268
Philipp Reisner087c2492010-03-26 13:49:56 +01002269 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002270 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002271
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002272 if (new_disk_conf) {
2273 mutex_lock(&mdev->tconn->conf_update);
2274 old_disk_conf = mdev->ldev->disk_conf;
2275 *new_disk_conf = *old_disk_conf;
2276 new_disk_conf->disk_size = (sector_t)rs.resize_size;
2277 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
2278 mutex_unlock(&mdev->tconn->conf_update);
2279 synchronize_rcu();
2280 kfree(old_disk_conf);
2281 }
2282
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002283 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
Bart Van Assche24c48302011-05-21 18:32:29 +02002284 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002285 drbd_md_sync(mdev);
2286 put_ldev(mdev);
2287 if (dd == dev_size_error) {
2288 retcode = ERR_NOMEM_BITMAP;
2289 goto fail;
2290 }
2291
Philipp Reisner087c2492010-03-26 13:49:56 +01002292 if (mdev->state.conn == C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002293 if (dd == grew)
2294 set_bit(RESIZE_PENDING, &mdev->flags);
2295
2296 drbd_send_uuids(mdev);
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002297 drbd_send_sizes(mdev, 1, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002298 }
2299
2300 fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002301 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002302 return 0;
2303}
2304
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02002305void drbd_set_res_opts_defaults(struct res_opts *r)
2306{
2307 return set_res_opts_defaults(r);
2308}
2309
Lars Ellenbergf3990022011-03-23 14:31:09 +01002310int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002311{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002312 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002313 cpumask_var_t new_cpu_mask;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002314 struct drbd_tconn *tconn;
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002315 struct res_opts res_opts;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002316 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002317
Lars Ellenbergf3990022011-03-23 14:31:09 +01002318 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002319 if (!adm_ctx.reply_skb)
2320 return retcode;
2321 if (retcode != NO_ERROR)
2322 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002323 tconn = adm_ctx.tconn;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002324
Philipp Reisnerb411b362009-09-25 16:07:19 -07002325 if (!zalloc_cpumask_var(&new_cpu_mask, GFP_KERNEL)) {
2326 retcode = ERR_NOMEM;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002327 drbd_msg_put_info("unable to allocate cpumask");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002328 goto fail;
2329 }
2330
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002331 res_opts = tconn->res_opts;
Lars Ellenberg5979e362011-04-27 21:09:55 +02002332 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02002333 set_res_opts_defaults(&res_opts);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002334
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002335 err = res_opts_from_attrs(&res_opts, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002336 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002337 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002338 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002339 goto fail;
2340 }
2341
Philipp Reisnerb411b362009-09-25 16:07:19 -07002342 /* silently ignore cpu mask on UP kernel */
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002343 if (nr_cpu_ids > 1 && res_opts.cpu_mask[0] != 0) {
2344 err = __bitmap_parse(res_opts.cpu_mask, 32, 0,
Philipp Reisnerb411b362009-09-25 16:07:19 -07002345 cpumask_bits(new_cpu_mask), nr_cpu_ids);
2346 if (err) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002347 conn_warn(tconn, "__bitmap_parse() failed with %d\n", err);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002348 retcode = ERR_CPU_MASK_PARSE;
2349 goto fail;
2350 }
2351 }
2352
Lars Ellenberg7ad651b2011-02-21 13:21:03 +01002353
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002354 tconn->res_opts = res_opts;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002355
Lars Ellenbergf3990022011-03-23 14:31:09 +01002356 if (!cpumask_equal(tconn->cpu_mask, new_cpu_mask)) {
2357 cpumask_copy(tconn->cpu_mask, new_cpu_mask);
2358 drbd_calc_cpu_mask(tconn);
2359 tconn->receiver.reset_cpu_mask = 1;
2360 tconn->asender.reset_cpu_mask = 1;
2361 tconn->worker.reset_cpu_mask = 1;
Philipp Reisner778f2712010-07-06 11:14:00 +02002362 }
2363
Philipp Reisnerb411b362009-09-25 16:07:19 -07002364fail:
2365 free_cpumask_var(new_cpu_mask);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002366
2367 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002368 return 0;
2369}
2370
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002371int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002372{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002373 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;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002383
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002384 /* 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
Philipp Reisnerb411b362009-09-25 16:07:19 -07002388 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) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01002394 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002395 if (mdev->state.conn < C_CONNECTED)
2396 retcode = _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_VERBOSE, NULL);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002397 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002398
2399 if (retcode != SS_NEED_CONNECTION)
2400 break;
2401
2402 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
2403 }
2404
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002405out:
2406 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002407 return 0;
2408}
2409
Philipp Reisner07782862010-08-31 12:00:50 +02002410static 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
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002419static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2420 union drbd_state mask, union drbd_state val)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002421{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002422 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002423
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002424 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;
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002429
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002430 retcode = drbd_request_state(adm_ctx.mdev, mask, val);
2431out:
2432 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002433 return 0;
2434}
2435
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002436int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002437{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002438 return drbd_adm_simple_request_state(skb, info, NS(conn, C_STARTING_SYNC_S));
2439}
Philipp Reisnerb411b362009-09-25 16:07:19 -07002440
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002441int 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;
2450
2451 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002452 retcode = ERR_PAUSE_IS_SET;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002453out:
2454 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002455 return 0;
2456}
2457
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002458int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002459{
Philipp Reisnerda9fbc22011-03-29 10:52:01 +02002460 union drbd_dev_state s;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002461 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002462
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002463 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;
2468
2469 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2470 s = adm_ctx.mdev->state;
Philipp Reisnercd88d032011-01-20 11:46:41 +01002471 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 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002478
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002479out:
2480 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002481 return 0;
2482}
2483
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002484int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002485{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002486 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002487}
2488
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002489int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002490{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002491 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;
Philipp Reisner43a51822010-06-11 11:26:34 +02002501 if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
2502 drbd_uuid_new_current(mdev);
2503 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner43a51822010-06-11 11:26:34 +02002504 }
Philipp Reisner265be2d2010-05-31 10:14:17 +02002505 drbd_suspend_io(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002506 retcode = drbd_request_state(mdev, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
2507 if (retcode == SS_SUCCESS) {
Philipp Reisner265be2d2010-05-31 10:14:17 +02002508 if (mdev->state.conn < C_CONNECTED)
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01002509 tl_clear(mdev->tconn);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002510 if (mdev->state.disk == D_DISKLESS || mdev->state.disk == D_FAILED)
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01002511 tl_restart(mdev->tconn, FAIL_FROZEN_DISK_IO);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002512 }
2513 drbd_resume_io(mdev);
2514
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002515out:
2516 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002517 return 0;
2518}
2519
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002520int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002521{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002522 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002523}
2524
Lars Ellenberg543cc102011-03-10 22:18:18 +01002525int nla_put_drbd_cfg_context(struct sk_buff *skb, const char *conn_name, unsigned vnr)
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);
2533 NLA_PUT_STRING(skb, T_ctx_conn_name, conn_name);
2534 nla_nest_end(skb, nla);
2535 return 0;
2536
2537nla_put_failure:
2538 if (nla)
2539 nla_nest_cancel(skb, nla);
2540 return -EMSGSIZE;
2541}
2542
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002543int nla_put_status_info(struct sk_buff *skb, struct drbd_conf *mdev,
2544 const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002545{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002546 struct state_info *si = NULL; /* for sizeof(si->member); */
Philipp Reisner44ed1672011-04-19 17:10:19 +02002547 struct net_conf *nc;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002548 struct nlattr *nla;
2549 int got_ldev;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002550 int err = 0;
2551 int exclude_sensitive;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002552
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002553 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2554 * to. So we better exclude_sensitive information.
2555 *
2556 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2557 * in the context of the requesting user process. Exclude sensitive
2558 * information, unless current has superuser.
2559 *
2560 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2561 * relies on the current implementation of netlink_dump(), which
2562 * executes the dump callback successively from netlink_recvmsg(),
2563 * always in the context of the receiving process */
2564 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002565
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002566 got_ldev = get_ldev(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002567
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002568 /* We need to add connection name and volume number information still.
2569 * Minor number is in drbd_genlmsghdr. */
Lars Ellenberg543cc102011-03-10 22:18:18 +01002570 if (nla_put_drbd_cfg_context(skb, mdev->tconn->name, mdev->vnr))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002571 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002572
Lars Ellenbergf3990022011-03-23 14:31:09 +01002573 if (res_opts_to_skb(skb, &mdev->tconn->res_opts, exclude_sensitive))
2574 goto nla_put_failure;
2575
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002576 rcu_read_lock();
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002577 if (got_ldev)
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002578 if (disk_conf_to_skb(skb, rcu_dereference(mdev->ldev->disk_conf), exclude_sensitive))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002579 goto nla_put_failure;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002580
Philipp Reisner44ed1672011-04-19 17:10:19 +02002581 nc = rcu_dereference(mdev->tconn->net_conf);
2582 if (nc)
2583 err = net_conf_to_skb(skb, nc, exclude_sensitive);
2584 rcu_read_unlock();
2585 if (err)
2586 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002587
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002588 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
2589 if (!nla)
2590 goto nla_put_failure;
2591 NLA_PUT_U32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY);
2592 NLA_PUT_U32(skb, T_current_state, mdev->state.i);
2593 NLA_PUT_U64(skb, T_ed_uuid, mdev->ed_uuid);
2594 NLA_PUT_U64(skb, T_capacity, drbd_get_capacity(mdev->this_bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002595
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002596 if (got_ldev) {
2597 NLA_PUT_U32(skb, T_disk_flags, mdev->ldev->md.flags);
2598 NLA_PUT(skb, T_uuids, sizeof(si->uuids), mdev->ldev->md.uuid);
2599 NLA_PUT_U64(skb, T_bits_total, drbd_bm_bits(mdev));
2600 NLA_PUT_U64(skb, T_bits_oos, drbd_bm_total_weight(mdev));
2601 if (C_SYNC_SOURCE <= mdev->state.conn &&
2602 C_PAUSED_SYNC_T >= mdev->state.conn) {
2603 NLA_PUT_U64(skb, T_bits_rs_total, mdev->rs_total);
2604 NLA_PUT_U64(skb, T_bits_rs_failed, mdev->rs_failed);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002605 }
2606 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002607
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002608 if (sib) {
2609 switch(sib->sib_reason) {
2610 case SIB_SYNC_PROGRESS:
2611 case SIB_GET_STATUS_REPLY:
2612 break;
2613 case SIB_STATE_CHANGE:
2614 NLA_PUT_U32(skb, T_prev_state, sib->os.i);
2615 NLA_PUT_U32(skb, T_new_state, sib->ns.i);
2616 break;
2617 case SIB_HELPER_POST:
2618 NLA_PUT_U32(skb,
2619 T_helper_exit_code, sib->helper_exit_code);
2620 /* fall through */
2621 case SIB_HELPER_PRE:
2622 NLA_PUT_STRING(skb, T_helper, sib->helper_name);
2623 break;
2624 }
2625 }
2626 nla_nest_end(skb, nla);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002627
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002628 if (0)
2629nla_put_failure:
2630 err = -EMSGSIZE;
2631 if (got_ldev)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002632 put_ldev(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002633 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002634}
2635
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002636int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002637{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002638 enum drbd_ret_code retcode;
2639 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002640
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002641 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2642 if (!adm_ctx.reply_skb)
2643 return retcode;
2644 if (retcode != NO_ERROR)
2645 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002646
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002647 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.mdev, NULL);
2648 if (err) {
2649 nlmsg_free(adm_ctx.reply_skb);
2650 return err;
2651 }
2652out:
2653 drbd_adm_finish(info, retcode);
2654 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002655}
2656
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002657int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002658{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002659 struct drbd_conf *mdev;
2660 struct drbd_genlmsghdr *dh;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002661 struct drbd_tconn *pos = (struct drbd_tconn*)cb->args[0];
2662 struct drbd_tconn *tconn = NULL;
2663 struct drbd_tconn *tmp;
2664 unsigned volume = cb->args[1];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002665
Lars Ellenberg543cc102011-03-10 22:18:18 +01002666 /* Open coded, deferred, iteration:
2667 * list_for_each_entry_safe(tconn, tmp, &drbd_tconns, all_tconn) {
2668 * idr_for_each_entry(&tconn->volumes, mdev, i) {
2669 * ...
2670 * }
2671 * }
2672 * where tconn is cb->args[0];
2673 * and i is cb->args[1];
2674 *
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002675 * cb->args[2] indicates if we shall loop over all resources,
2676 * or just dump all volumes of a single resource.
2677 *
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002678 * This may miss entries inserted after this dump started,
2679 * or entries deleted before they are reached.
Lars Ellenberg543cc102011-03-10 22:18:18 +01002680 *
2681 * We need to make sure the mdev won't disappear while
2682 * we are looking at it, and revalidate our iterators
2683 * on each iteration.
2684 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002685
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02002686 /* synchronize with conn_create()/conn_destroy() */
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02002687 rcu_read_lock();
Lars Ellenberg543cc102011-03-10 22:18:18 +01002688 /* revalidate iterator position */
Philipp Reisnerec0bddb2011-05-04 15:47:01 +02002689 list_for_each_entry_rcu(tmp, &drbd_tconns, all_tconn) {
Lars Ellenberg543cc102011-03-10 22:18:18 +01002690 if (pos == NULL) {
2691 /* first iteration */
2692 pos = tmp;
2693 tconn = pos;
2694 break;
2695 }
2696 if (tmp == pos) {
2697 tconn = pos;
2698 break;
2699 }
2700 }
2701 if (tconn) {
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002702next_tconn:
Lars Ellenberg543cc102011-03-10 22:18:18 +01002703 mdev = idr_get_next(&tconn->volumes, &volume);
2704 if (!mdev) {
2705 /* No more volumes to dump on this tconn.
2706 * Advance tconn iterator. */
Philipp Reisnerec0bddb2011-05-04 15:47:01 +02002707 pos = list_entry_rcu(tconn->all_tconn.next,
2708 struct drbd_tconn, all_tconn);
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002709 /* Did we dump any volume on this tconn yet? */
Lars Ellenberg543cc102011-03-10 22:18:18 +01002710 if (volume != 0) {
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002711 /* If we reached the end of the list,
2712 * or only a single resource dump was requested,
2713 * we are done. */
2714 if (&pos->all_tconn == &drbd_tconns || cb->args[2])
2715 goto out;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002716 volume = 0;
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002717 tconn = pos;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002718 goto next_tconn;
2719 }
2720 }
2721
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002722 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).pid,
2723 cb->nlh->nlmsg_seq, &drbd_genl_family,
2724 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
2725 if (!dh)
Lars Ellenberg543cc102011-03-10 22:18:18 +01002726 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002727
Lars Ellenberg543cc102011-03-10 22:18:18 +01002728 if (!mdev) {
2729 /* this is a tconn without a single volume */
2730 dh->minor = -1U;
2731 dh->ret_code = NO_ERROR;
2732 if (nla_put_drbd_cfg_context(skb, tconn->name, VOLUME_UNSPECIFIED))
2733 genlmsg_cancel(skb, dh);
2734 else
2735 genlmsg_end(skb, dh);
2736 goto out;
2737 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002738
Lars Ellenberg543cc102011-03-10 22:18:18 +01002739 D_ASSERT(mdev->vnr == volume);
2740 D_ASSERT(mdev->tconn == tconn);
2741
2742 dh->minor = mdev_to_minor(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002743 dh->ret_code = NO_ERROR;
2744
2745 if (nla_put_status_info(skb, mdev, NULL)) {
2746 genlmsg_cancel(skb, dh);
Lars Ellenberg543cc102011-03-10 22:18:18 +01002747 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002748 }
2749 genlmsg_end(skb, dh);
2750 }
2751
Lars Ellenberg543cc102011-03-10 22:18:18 +01002752out:
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02002753 rcu_read_unlock();
Lars Ellenberg543cc102011-03-10 22:18:18 +01002754 /* where to start the next iteration */
2755 cb->args[0] = (long)pos;
2756 cb->args[1] = (pos == tconn) ? volume + 1 : 0;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002757
Lars Ellenberg543cc102011-03-10 22:18:18 +01002758 /* No more tconns/volumes/minors found results in an empty skb.
2759 * Which will terminate the dump. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002760 return skb->len;
2761}
2762
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002763/*
2764 * Request status of all resources, or of all volumes within a single resource.
2765 *
2766 * This is a dump, as the answer may not fit in a single reply skb otherwise.
2767 * Which means we cannot use the family->attrbuf or other such members, because
2768 * dump is NOT protected by the genl_lock(). During dump, we only have access
2769 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
2770 *
2771 * Once things are setup properly, we call into get_one_status().
2772 */
2773int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
2774{
2775 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
2776 struct nlattr *nla;
2777 const char *conn_name;
2778 struct drbd_tconn *tconn;
2779
2780 /* Is this a followup call? */
2781 if (cb->args[0]) {
2782 /* ... of a single resource dump,
2783 * and the resource iterator has been advanced already? */
2784 if (cb->args[2] && cb->args[2] != cb->args[0])
2785 return 0; /* DONE. */
2786 goto dump;
2787 }
2788
2789 /* First call (from netlink_dump_start). We need to figure out
2790 * which resource(s) the user wants us to dump. */
2791 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
2792 nlmsg_attrlen(cb->nlh, hdrlen),
2793 DRBD_NLA_CFG_CONTEXT);
2794
2795 /* No explicit context given. Dump all. */
2796 if (!nla)
2797 goto dump;
2798 nla = nla_find_nested(nla, __nla_type(T_ctx_conn_name));
2799 /* context given, but no name present? */
2800 if (!nla)
2801 return -EINVAL;
2802 conn_name = nla_data(nla);
Philipp Reisner0ace9df2011-04-24 10:53:19 +02002803 tconn = conn_get_by_name(conn_name);
2804
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002805 if (!tconn)
2806 return -ENODEV;
2807
Philipp Reisner0ace9df2011-04-24 10:53:19 +02002808 kref_put(&tconn->kref, &conn_destroy); /* get_one_status() (re)validates tconn by itself */
2809
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002810 /* prime iterators, and set "filter" mode mark:
2811 * only dump this tconn. */
2812 cb->args[0] = (long)tconn;
2813 /* cb->args[1] = 0; passed in this way. */
2814 cb->args[2] = (long)tconn;
2815
2816dump:
2817 return get_one_status(skb, cb);
2818}
2819
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002820int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
2821{
2822 enum drbd_ret_code retcode;
2823 struct timeout_parms tp;
2824 int err;
2825
2826 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2827 if (!adm_ctx.reply_skb)
2828 return retcode;
2829 if (retcode != NO_ERROR)
2830 goto out;
2831
2832 tp.timeout_type =
2833 adm_ctx.mdev->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
2834 test_bit(USE_DEGR_WFC_T, &adm_ctx.mdev->flags) ? UT_DEGRADED :
2835 UT_DEFAULT;
2836
2837 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
2838 if (err) {
2839 nlmsg_free(adm_ctx.reply_skb);
2840 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002841 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002842out:
2843 drbd_adm_finish(info, retcode);
2844 return 0;
2845}
Lars Ellenberg873b0d52011-01-21 22:53:48 +01002846
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002847int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
2848{
2849 struct drbd_conf *mdev;
2850 enum drbd_ret_code retcode;
2851
2852 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2853 if (!adm_ctx.reply_skb)
2854 return retcode;
2855 if (retcode != NO_ERROR)
2856 goto out;
2857
2858 mdev = adm_ctx.mdev;
2859 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
2860 /* resume from last known position, if possible */
2861 struct start_ov_parms parms =
2862 { .ov_start_sector = mdev->ov_start_sector };
Lars Ellenbergf3990022011-03-23 14:31:09 +01002863 int err = start_ov_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002864 if (err) {
2865 retcode = ERR_MANDATORY_TAG;
2866 drbd_msg_put_info(from_attrs_err_to_txt(err));
2867 goto out;
2868 }
2869 /* w_make_ov_request expects position to be aligned */
2870 mdev->ov_start_sector = parms.ov_start_sector & ~BM_SECT_PER_BIT;
2871 }
Lars Ellenberg873b0d52011-01-21 22:53:48 +01002872 /* If there is still bitmap IO pending, e.g. previous resync or verify
2873 * just being finished, wait for it before requesting a new resync. */
2874 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002875 retcode = drbd_request_state(mdev,NS(conn,C_VERIFY_S));
2876out:
2877 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002878 return 0;
2879}
2880
2881
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002882int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002883{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002884 struct drbd_conf *mdev;
2885 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002886 int skip_initial_sync = 0;
2887 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002888 struct new_c_uuid_parms args;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002889
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002890 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2891 if (!adm_ctx.reply_skb)
2892 return retcode;
2893 if (retcode != NO_ERROR)
2894 goto out_nolock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002895
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002896 mdev = adm_ctx.mdev;
2897 memset(&args, 0, sizeof(args));
2898 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002899 err = new_c_uuid_parms_from_attrs(&args, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002900 if (err) {
2901 retcode = ERR_MANDATORY_TAG;
2902 drbd_msg_put_info(from_attrs_err_to_txt(err));
2903 goto out_nolock;
2904 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002905 }
2906
Philipp Reisner8410da82011-02-11 20:11:10 +01002907 mutex_lock(mdev->state_mutex); /* Protects us against serialized state changes. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002908
2909 if (!get_ldev(mdev)) {
2910 retcode = ERR_NO_DISK;
2911 goto out;
2912 }
2913
2914 /* this is "skip initial sync", assume to be clean */
Philipp Reisner31890f42011-01-19 14:12:51 +01002915 if (mdev->state.conn == C_CONNECTED && mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07002916 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
2917 dev_info(DEV, "Preparing to skip initial sync\n");
2918 skip_initial_sync = 1;
2919 } else if (mdev->state.conn != C_STANDALONE) {
2920 retcode = ERR_CONNECTED;
2921 goto out_dec;
2922 }
2923
2924 drbd_uuid_set(mdev, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
2925 drbd_uuid_new_current(mdev); /* New current, previous to UI_BITMAP */
2926
2927 if (args.clear_bm) {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002928 err = drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
2929 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002930 if (err) {
2931 dev_err(DEV, "Writing bitmap failed with %d\n",err);
2932 retcode = ERR_IO_MD_DISK;
2933 }
2934 if (skip_initial_sync) {
2935 drbd_send_uuids_skip_initial_sync(mdev);
2936 _drbd_uuid_set(mdev, UI_BITMAP, 0);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002937 drbd_print_uuids(mdev, "cleared bitmap UUID");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002938 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002939 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
2940 CS_VERBOSE, NULL);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002941 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002942 }
2943 }
2944
2945 drbd_md_sync(mdev);
2946out_dec:
2947 put_ldev(mdev);
2948out:
Philipp Reisner8410da82011-02-11 20:11:10 +01002949 mutex_unlock(mdev->state_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002950out_nolock:
2951 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002952 return 0;
2953}
2954
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002955static enum drbd_ret_code
2956drbd_check_conn_name(const char *name)
Philipp Reisner774b3052011-02-22 02:07:03 -05002957{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002958 if (!name || !name[0]) {
2959 drbd_msg_put_info("connection name missing");
2960 return ERR_MANDATORY_TAG;
2961 }
2962 /* if we want to use these in sysfs/configfs/debugfs some day,
2963 * we must not allow slashes */
2964 if (strchr(name, '/')) {
2965 drbd_msg_put_info("invalid connection name");
2966 return ERR_INVALID_REQUEST;
2967 }
2968 return NO_ERROR;
2969}
Philipp Reisner774b3052011-02-22 02:07:03 -05002970
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002971int drbd_adm_create_connection(struct sk_buff *skb, struct genl_info *info)
2972{
2973 enum drbd_ret_code retcode;
2974
2975 retcode = drbd_adm_prepare(skb, info, 0);
2976 if (!adm_ctx.reply_skb)
2977 return retcode;
2978 if (retcode != NO_ERROR)
2979 goto out;
2980
2981 retcode = drbd_check_conn_name(adm_ctx.conn_name);
2982 if (retcode != NO_ERROR)
2983 goto out;
2984
2985 if (adm_ctx.tconn) {
Lars Ellenberg38f19612011-03-14 13:22:35 +01002986 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
2987 retcode = ERR_INVALID_REQUEST;
2988 drbd_msg_put_info("connection exists");
2989 }
2990 /* else: still NO_ERROR */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002991 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05002992 }
2993
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02002994 if (!conn_create(adm_ctx.conn_name))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002995 retcode = ERR_NOMEM;
2996out:
2997 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05002998 return 0;
2999}
3000
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003001int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05003002{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003003 struct drbd_genlmsghdr *dh = info->userhdr;
3004 enum drbd_ret_code retcode;
Philipp Reisner774b3052011-02-22 02:07:03 -05003005
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003006 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
3007 if (!adm_ctx.reply_skb)
3008 return retcode;
3009 if (retcode != NO_ERROR)
3010 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05003011
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003012 /* FIXME drop minor_count parameter, limit to MINORMASK */
3013 if (dh->minor >= minor_count) {
3014 drbd_msg_put_info("requested minor out of range");
3015 retcode = ERR_INVALID_REQUEST;
3016 goto out;
3017 }
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +02003018 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003019 drbd_msg_put_info("requested volume id out of range");
3020 retcode = ERR_INVALID_REQUEST;
3021 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05003022 }
3023
Lars Ellenberg38f19612011-03-14 13:22:35 +01003024 /* drbd_adm_prepare made sure already
3025 * that mdev->tconn and mdev->vnr match the request. */
3026 if (adm_ctx.mdev) {
3027 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
3028 retcode = ERR_MINOR_EXISTS;
3029 /* else: still NO_ERROR */
3030 goto out;
3031 }
3032
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003033 retcode = conn_new_minor(adm_ctx.tconn, dh->minor, adm_ctx.volume);
3034out:
3035 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003036 return 0;
3037}
3038
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003039static enum drbd_ret_code adm_delete_minor(struct drbd_conf *mdev)
3040{
3041 if (mdev->state.disk == D_DISKLESS &&
3042 /* no need to be mdev->state.conn == C_STANDALONE &&
3043 * we may want to delete a minor from a live replication group.
3044 */
3045 mdev->state.role == R_SECONDARY) {
Philipp Reisner81fa2e62011-05-04 15:10:30 +02003046 idr_remove(&mdev->tconn->volumes, mdev->vnr);
3047 idr_remove(&minors, mdev_to_minor(mdev));
3048 del_gendisk(mdev->vdisk);
3049 synchronize_rcu();
3050 kref_put(&mdev->kref, &drbd_minor_destroy);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003051 return NO_ERROR;
3052 } else
3053 return ERR_MINOR_CONFIGURED;
3054}
3055
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003056int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05003057{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003058 enum drbd_ret_code retcode;
3059
3060 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3061 if (!adm_ctx.reply_skb)
3062 return retcode;
3063 if (retcode != NO_ERROR)
3064 goto out;
3065
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003066 retcode = adm_delete_minor(adm_ctx.mdev);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003067out:
3068 drbd_adm_finish(info, retcode);
3069 return 0;
3070}
3071
3072int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
3073{
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003074 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003075 struct drbd_conf *mdev;
3076 unsigned i;
3077
3078 retcode = drbd_adm_prepare(skb, info, 0);
3079 if (!adm_ctx.reply_skb)
3080 return retcode;
3081 if (retcode != NO_ERROR)
3082 goto out;
3083
3084 if (!adm_ctx.tconn) {
3085 retcode = ERR_CONN_NOT_KNOWN;
3086 goto out;
3087 }
3088
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003089 /* demote */
3090 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3091 retcode = drbd_set_role(mdev, R_SECONDARY, 0);
3092 if (retcode < SS_SUCCESS) {
3093 drbd_msg_put_info("failed to demote");
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003094 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003095 }
3096 }
3097
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003098 retcode = conn_try_disconnect(adm_ctx.tconn, 0);
3099 if (retcode < SS_SUCCESS) {
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003100 drbd_msg_put_info("failed to disconnect");
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003101 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003102 }
3103
3104 /* detach */
3105 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003106 retcode = adm_detach(mdev);
3107 if (retcode < SS_SUCCESS) {
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003108 drbd_msg_put_info("failed to detach");
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003109 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003110 }
3111 }
3112
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003113 /* If we reach this, all volumes (of this tconn) are Secondary,
3114 * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003115 * actually stopped, state handling only does drbd_thread_stop_nowait(). */
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003116 drbd_thread_stop(&adm_ctx.tconn->worker);
3117
3118 /* Now, nothing can fail anymore */
3119
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003120 /* delete volumes */
3121 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3122 retcode = adm_delete_minor(mdev);
3123 if (retcode != NO_ERROR) {
3124 /* "can not happen" */
3125 drbd_msg_put_info("failed to delete volume");
Philipp Reisneref356262011-04-13 14:21:29 -07003126 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003127 }
3128 }
3129
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003130 /* delete connection */
3131 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
Philipp Reisnerec0bddb2011-05-04 15:47:01 +02003132 list_del_rcu(&adm_ctx.tconn->all_tconn);
3133 synchronize_rcu();
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02003134 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
3135
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003136 retcode = NO_ERROR;
3137 } else {
3138 /* "can not happen" */
3139 retcode = ERR_CONN_IN_USE;
3140 drbd_msg_put_info("failed to delete connection");
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003141 }
Philipp Reisneref356262011-04-13 14:21:29 -07003142 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003143out:
3144 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003145 return 0;
3146}
3147
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003148int drbd_adm_delete_connection(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05003149{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003150 enum drbd_ret_code retcode;
3151
3152 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
3153 if (!adm_ctx.reply_skb)
3154 return retcode;
3155 if (retcode != NO_ERROR)
3156 goto out;
3157
3158 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
Philipp Reisnerec0bddb2011-05-04 15:47:01 +02003159 list_del_rcu(&adm_ctx.tconn->all_tconn);
3160 synchronize_rcu();
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02003161 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
3162
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003163 retcode = NO_ERROR;
Philipp Reisner774b3052011-02-22 02:07:03 -05003164 } else {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003165 retcode = ERR_CONN_IN_USE;
Philipp Reisner774b3052011-02-22 02:07:03 -05003166 }
3167
Lars Ellenberg992d6e92011-05-02 11:47:18 +02003168 if (retcode == NO_ERROR)
3169 drbd_thread_stop(&adm_ctx.tconn->worker);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003170out:
3171 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003172 return 0;
3173}
3174
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003175void drbd_bcast_event(struct drbd_conf *mdev, const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003176{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003177 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
3178 struct sk_buff *msg;
3179 struct drbd_genlmsghdr *d_out;
3180 unsigned seq;
3181 int err = -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003182
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003183 seq = atomic_inc_return(&drbd_genl_seq);
3184 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
3185 if (!msg)
3186 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003187
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003188 err = -EMSGSIZE;
3189 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
3190 if (!d_out) /* cannot happen, but anyways. */
3191 goto nla_put_failure;
3192 d_out->minor = mdev_to_minor(mdev);
Andreas Gruenbacher6f9b5f82011-05-06 01:03:32 +02003193 d_out->ret_code = NO_ERROR;
Philipp Reisner9f5180e2009-10-06 09:30:14 +02003194
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003195 if (nla_put_status_info(msg, mdev, sib))
3196 goto nla_put_failure;
3197 genlmsg_end(msg, d_out);
3198 err = drbd_genl_multicast_events(msg, 0);
3199 /* msg has been consumed or freed in netlink_broadcast() */
3200 if (err && err != -ESRCH)
3201 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003202
Philipp Reisnerb411b362009-09-25 16:07:19 -07003203 return;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003204
3205nla_put_failure:
3206 nlmsg_free(msg);
3207failed:
3208 dev_err(DEV, "Error %d while broadcasting event. "
3209 "Event seq:%u sib_reason:%u\n",
3210 err, seq, sib->sib_reason);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003211}