blob: dc5bd6bbb280e7df3b63fdbc9c0ca48952227e7d [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
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +020050int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info);
51int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info);
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>
Andreas Gruenbacher01b39b52011-06-10 12:57:26 +020078#include "drbd_nla.h"
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010079#include <linux/genl_magic_func.h>
80
81/* used blkdev_get_by_path, to claim our meta data device(s) */
Philipp Reisnerb411b362009-09-25 16:07:19 -070082static char *drbd_m_holder = "Hands off! this is DRBD's meta data device.";
83
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010084/* Configuration is strictly serialized, because generic netlink message
85 * processing is strictly serialized by the genl_lock().
86 * Which means we can use one static global drbd_config_context struct.
87 */
88static struct drbd_config_context {
89 /* assigned from drbd_genlmsghdr */
90 unsigned int minor;
91 /* assigned from request attributes, if present */
92 unsigned int volume;
93#define VOLUME_UNSPECIFIED (-1U)
94 /* pointer into the request skb,
95 * limited lifetime! */
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +020096 char *resource_name;
Andreas Gruenbacher089c0752011-06-14 18:28:09 +020097 struct nlattr *my_addr;
98 struct nlattr *peer_addr;
Philipp Reisnerb411b362009-09-25 16:07:19 -070099
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100100 /* reply buffer */
101 struct sk_buff *reply_skb;
102 /* pointer into reply buffer */
103 struct drbd_genlmsghdr *reply_dh;
104 /* resolved from attributes, if possible */
105 struct drbd_conf *mdev;
106 struct drbd_tconn *tconn;
107} adm_ctx;
108
109static void drbd_adm_send_reply(struct sk_buff *skb, struct genl_info *info)
110{
111 genlmsg_end(skb, genlmsg_data(nlmsg_data(nlmsg_hdr(skb))));
112 if (genlmsg_reply(skb, info))
113 printk(KERN_ERR "drbd: error sending genl reply\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700114}
115
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100116/* Used on a fresh "drbd_adm_prepare"d reply_skb, this cannot fail: The only
117 * reason it could fail was no space in skb, and there are 4k available. */
Lars Ellenberg8432b312011-03-08 16:11:16 +0100118int drbd_msg_put_info(const char *info)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100119{
120 struct sk_buff *skb = adm_ctx.reply_skb;
121 struct nlattr *nla;
122 int err = -EMSGSIZE;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700123
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100124 if (!info || !info[0])
125 return 0;
126
127 nla = nla_nest_start(skb, DRBD_NLA_CFG_REPLY);
128 if (!nla)
129 return err;
130
131 err = nla_put_string(skb, T_info_text, info);
132 if (err) {
133 nla_nest_cancel(skb, nla);
134 return err;
135 } else
136 nla_nest_end(skb, nla);
137 return 0;
138}
139
140/* This would be a good candidate for a "pre_doit" hook,
141 * and per-family private info->pointers.
142 * But we need to stay compatible with older kernels.
143 * If it returns successfully, adm_ctx members are valid.
144 */
145#define DRBD_ADM_NEED_MINOR 1
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +0200146#define DRBD_ADM_NEED_RESOURCE 2
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200147#define DRBD_ADM_NEED_CONNECTION 4
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100148static int drbd_adm_prepare(struct sk_buff *skb, struct genl_info *info,
149 unsigned flags)
150{
151 struct drbd_genlmsghdr *d_in = info->userhdr;
152 const u8 cmd = info->genlhdr->cmd;
153 int err;
154
155 memset(&adm_ctx, 0, sizeof(adm_ctx));
156
157 /* genl_rcv_msg only checks for CAP_NET_ADMIN on "GENL_ADMIN_PERM" :( */
158 if (cmd != DRBD_ADM_GET_STATUS
159 && security_netlink_recv(skb, CAP_SYS_ADMIN))
160 return -EPERM;
161
162 adm_ctx.reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200163 if (!adm_ctx.reply_skb) {
164 err = -ENOMEM;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100165 goto fail;
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200166 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100167
168 adm_ctx.reply_dh = genlmsg_put_reply(adm_ctx.reply_skb,
169 info, &drbd_genl_family, 0, cmd);
170 /* put of a few bytes into a fresh skb of >= 4k will always succeed.
171 * but anyways */
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200172 if (!adm_ctx.reply_dh) {
173 err = -ENOMEM;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100174 goto fail;
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200175 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100176
177 adm_ctx.reply_dh->minor = d_in->minor;
178 adm_ctx.reply_dh->ret_code = NO_ERROR;
179
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200180 adm_ctx.volume = VOLUME_UNSPECIFIED;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100181 if (info->attrs[DRBD_NLA_CFG_CONTEXT]) {
182 struct nlattr *nla;
183 /* parse and validate only */
Lars Ellenbergf3990022011-03-23 14:31:09 +0100184 err = drbd_cfg_context_from_attrs(NULL, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100185 if (err)
186 goto fail;
187
188 /* It was present, and valid,
189 * copy it over to the reply skb. */
190 err = nla_put_nohdr(adm_ctx.reply_skb,
191 info->attrs[DRBD_NLA_CFG_CONTEXT]->nla_len,
192 info->attrs[DRBD_NLA_CFG_CONTEXT]);
193 if (err)
194 goto fail;
195
196 /* and assign stuff to the global adm_ctx */
197 nla = nested_attr_tb[__nla_type(T_ctx_volume)];
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200198 if (nla)
199 adm_ctx.volume = nla_get_u32(nla);
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +0200200 nla = nested_attr_tb[__nla_type(T_ctx_resource_name)];
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100201 if (nla)
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +0200202 adm_ctx.resource_name = nla_data(nla);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200203 adm_ctx.my_addr = nested_attr_tb[__nla_type(T_ctx_my_addr)];
204 adm_ctx.peer_addr = nested_attr_tb[__nla_type(T_ctx_peer_addr)];
205 if ((adm_ctx.my_addr &&
206 nla_len(adm_ctx.my_addr) > sizeof(adm_ctx.tconn->my_addr)) ||
207 (adm_ctx.peer_addr &&
208 nla_len(adm_ctx.peer_addr) > sizeof(adm_ctx.tconn->peer_addr))) {
209 err = -EINVAL;
210 goto fail;
211 }
212 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100213
214 adm_ctx.minor = d_in->minor;
215 adm_ctx.mdev = minor_to_mdev(d_in->minor);
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +0200216 adm_ctx.tconn = conn_get_by_name(adm_ctx.resource_name);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100217
218 if (!adm_ctx.mdev && (flags & DRBD_ADM_NEED_MINOR)) {
219 drbd_msg_put_info("unknown minor");
220 return ERR_MINOR_INVALID;
221 }
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +0200222 if (!adm_ctx.tconn && (flags & DRBD_ADM_NEED_RESOURCE)) {
223 drbd_msg_put_info("unknown resource");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100224 return ERR_INVALID_REQUEST;
225 }
226
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200227 if (flags & DRBD_ADM_NEED_CONNECTION) {
228 if (adm_ctx.tconn && !(flags & DRBD_ADM_NEED_RESOURCE)) {
229 drbd_msg_put_info("no resource name expected");
230 return ERR_INVALID_REQUEST;
231 }
232 if (adm_ctx.mdev) {
233 drbd_msg_put_info("no minor number expected");
234 return ERR_INVALID_REQUEST;
235 }
236 if (adm_ctx.my_addr && adm_ctx.peer_addr)
237 adm_ctx.tconn = conn_get_by_addrs(nla_data(adm_ctx.my_addr),
238 nla_len(adm_ctx.my_addr),
239 nla_data(adm_ctx.peer_addr),
240 nla_len(adm_ctx.peer_addr));
241 if (!adm_ctx.tconn) {
242 drbd_msg_put_info("unknown connection");
243 return ERR_INVALID_REQUEST;
244 }
245 }
246
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100247 /* some more paranoia, if the request was over-determined */
Lars Ellenberg527f4b22011-03-14 13:58:03 +0100248 if (adm_ctx.mdev && adm_ctx.tconn &&
249 adm_ctx.mdev->tconn != adm_ctx.tconn) {
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +0200250 pr_warning("request: minor=%u, resource=%s; but that minor belongs to connection %s\n",
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +0200251 adm_ctx.minor, adm_ctx.resource_name,
252 adm_ctx.mdev->tconn->name);
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +0200253 drbd_msg_put_info("minor exists in different resource");
Lars Ellenberg527f4b22011-03-14 13:58:03 +0100254 return ERR_INVALID_REQUEST;
255 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100256 if (adm_ctx.mdev &&
257 adm_ctx.volume != VOLUME_UNSPECIFIED &&
258 adm_ctx.volume != adm_ctx.mdev->vnr) {
259 pr_warning("request: minor=%u, volume=%u; but that minor is volume %u in %s\n",
260 adm_ctx.minor, adm_ctx.volume,
261 adm_ctx.mdev->vnr, adm_ctx.mdev->tconn->name);
Lars Ellenberg527f4b22011-03-14 13:58:03 +0100262 drbd_msg_put_info("minor exists as different volume");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100263 return ERR_INVALID_REQUEST;
264 }
Philipp Reisner0ace9df2011-04-24 10:53:19 +0200265
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100266 return NO_ERROR;
267
268fail:
269 nlmsg_free(adm_ctx.reply_skb);
270 adm_ctx.reply_skb = NULL;
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200271 return err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100272}
273
274static int drbd_adm_finish(struct genl_info *info, int retcode)
275{
Philipp Reisner0ace9df2011-04-24 10:53:19 +0200276 if (adm_ctx.tconn) {
277 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
278 adm_ctx.tconn = NULL;
279 }
280
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100281 if (!adm_ctx.reply_skb)
282 return -ENOMEM;
283
284 adm_ctx.reply_dh->ret_code = retcode;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100285 drbd_adm_send_reply(adm_ctx.reply_skb, info);
286 return 0;
287}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700288
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100289static void setup_khelper_env(struct drbd_tconn *tconn, char **envp)
290{
291 char *afs;
292
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200293 /* FIXME: A future version will not allow this case. */
294 if (tconn->my_addr_len == 0 || tconn->peer_addr_len == 0)
295 return;
296
297 switch (((struct sockaddr *)&tconn->peer_addr)->sa_family) {
298 case AF_INET6:
299 afs = "ipv6";
300 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
301 &((struct sockaddr_in6 *)&tconn->peer_addr)->sin6_addr);
302 break;
303 case AF_INET:
304 afs = "ipv4";
305 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
306 &((struct sockaddr_in *)&tconn->peer_addr)->sin_addr);
307 break;
308 default:
309 afs = "ssocks";
310 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
311 &((struct sockaddr_in *)&tconn->peer_addr)->sin_addr);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100312 }
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200313 snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100314}
315
Philipp Reisnerb411b362009-09-25 16:07:19 -0700316int drbd_khelper(struct drbd_conf *mdev, char *cmd)
317{
318 char *envp[] = { "HOME=/",
319 "TERM=linux",
320 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100321 (char[20]) { }, /* address family */
322 (char[60]) { }, /* address */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700323 NULL };
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100324 char mb[12];
Philipp Reisnerb411b362009-09-25 16:07:19 -0700325 char *argv[] = {usermode_helper, cmd, mb, NULL };
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100326 struct sib_info sib;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700327 int ret;
328
329 snprintf(mb, 12, "minor-%d", mdev_to_minor(mdev));
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100330 setup_khelper_env(mdev->tconn, envp);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700331
Lars Ellenberg1090c052010-07-19 17:41:04 +0200332 /* The helper may take some time.
333 * write out any unsynced meta data changes now */
334 drbd_md_sync(mdev);
335
Philipp Reisnerb411b362009-09-25 16:07:19 -0700336 dev_info(DEV, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100337 sib.sib_reason = SIB_HELPER_PRE;
338 sib.helper_name = cmd;
339 drbd_bcast_event(mdev, &sib);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700340 ret = call_usermodehelper(usermode_helper, argv, envp, 1);
341 if (ret)
342 dev_warn(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
343 usermode_helper, cmd, mb,
344 (ret >> 8) & 0xff, ret);
345 else
346 dev_info(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
347 usermode_helper, cmd, mb,
348 (ret >> 8) & 0xff, ret);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100349 sib.sib_reason = SIB_HELPER_POST;
350 sib.helper_exit_code = ret;
351 drbd_bcast_event(mdev, &sib);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700352
353 if (ret < 0) /* Ignore any ERRNOs we got. */
354 ret = 0;
355
356 return ret;
357}
358
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100359static void conn_md_sync(struct drbd_tconn *tconn)
360{
361 struct drbd_conf *mdev;
Philipp Reisnere90285e2011-03-22 12:51:21 +0100362 int vnr;
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100363
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200364 rcu_read_lock();
365 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
366 kref_get(&mdev->kref);
367 rcu_read_unlock();
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100368 drbd_md_sync(mdev);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200369 kref_put(&mdev->kref, &drbd_minor_destroy);
370 rcu_read_lock();
371 }
372 rcu_read_unlock();
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100373}
374
375int conn_khelper(struct drbd_tconn *tconn, char *cmd)
376{
377 char *envp[] = { "HOME=/",
378 "TERM=linux",
379 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
380 (char[20]) { }, /* address family */
381 (char[60]) { }, /* address */
382 NULL };
383 char *argv[] = {usermode_helper, cmd, tconn->name, NULL };
384 int ret;
385
386 setup_khelper_env(tconn, envp);
387 conn_md_sync(tconn);
388
389 conn_info(tconn, "helper command: %s %s %s\n", usermode_helper, cmd, tconn->name);
390 /* TODO: conn_bcast_event() ?? */
391
392 ret = call_usermodehelper(usermode_helper, argv, envp, 1);
393 if (ret)
394 conn_warn(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
395 usermode_helper, cmd, tconn->name,
396 (ret >> 8) & 0xff, ret);
397 else
398 conn_info(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
399 usermode_helper, cmd, tconn->name,
400 (ret >> 8) & 0xff, ret);
401 /* TODO: conn_bcast_event() ?? */
402
403 if (ret < 0) /* Ignore any ERRNOs we got. */
404 ret = 0;
405
406 return ret;
407}
408
Philipp Reisnercb703452011-03-24 11:03:07 +0100409static enum drbd_fencing_p highest_fencing_policy(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700410{
Philipp Reisnercb703452011-03-24 11:03:07 +0100411 enum drbd_fencing_p fp = FP_NOT_AVAIL;
412 struct drbd_conf *mdev;
413 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700414
Philipp Reisner695d08f2011-04-11 22:53:32 -0700415 rcu_read_lock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100416 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
417 if (get_ldev_if_state(mdev, D_CONSISTENT)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200418 fp = max_t(enum drbd_fencing_p, fp,
419 rcu_dereference(mdev->ldev->disk_conf)->fencing);
Philipp Reisnercb703452011-03-24 11:03:07 +0100420 put_ldev(mdev);
421 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700422 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700423 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700424
Philipp Reisnercb703452011-03-24 11:03:07 +0100425 return fp;
426}
427
428bool conn_try_outdate_peer(struct drbd_tconn *tconn)
429{
430 union drbd_state mask = { };
431 union drbd_state val = { };
432 enum drbd_fencing_p fp;
433 char *ex_to_string;
434 int r;
435
436 if (tconn->cstate >= C_WF_REPORT_PARAMS) {
437 conn_err(tconn, "Expected cstate < C_WF_REPORT_PARAMS\n");
438 return false;
439 }
440
441 fp = highest_fencing_policy(tconn);
442 switch (fp) {
443 case FP_NOT_AVAIL:
444 conn_warn(tconn, "Not fencing peer, I'm not even Consistent myself.\n");
445 goto out;
446 case FP_DONT_CARE:
447 return true;
448 default: ;
449 }
450
451 r = conn_khelper(tconn, "fence-peer");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700452
453 switch ((r>>8) & 0xff) {
454 case 3: /* peer is inconsistent */
455 ex_to_string = "peer is inconsistent or worse";
Philipp Reisnercb703452011-03-24 11:03:07 +0100456 mask.pdsk = D_MASK;
457 val.pdsk = D_INCONSISTENT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700458 break;
459 case 4: /* peer got outdated, or was already outdated */
460 ex_to_string = "peer was fenced";
Philipp Reisnercb703452011-03-24 11:03:07 +0100461 mask.pdsk = D_MASK;
462 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700463 break;
464 case 5: /* peer was down */
Philipp Reisnercb703452011-03-24 11:03:07 +0100465 if (conn_highest_disk(tconn) == D_UP_TO_DATE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700466 /* we will(have) create(d) a new UUID anyways... */
467 ex_to_string = "peer is unreachable, assumed to be dead";
Philipp Reisnercb703452011-03-24 11:03:07 +0100468 mask.pdsk = D_MASK;
469 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700470 } else {
471 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
Philipp Reisnerb411b362009-09-25 16:07:19 -0700472 }
473 break;
474 case 6: /* Peer is primary, voluntarily outdate myself.
475 * This is useful when an unconnected R_SECONDARY is asked to
476 * become R_PRIMARY, but finds the other peer being active. */
477 ex_to_string = "peer is active";
Philipp Reisnercb703452011-03-24 11:03:07 +0100478 conn_warn(tconn, "Peer is primary, outdating myself.\n");
479 mask.disk = D_MASK;
480 val.disk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700481 break;
482 case 7:
483 if (fp != FP_STONITH)
Philipp Reisnercb703452011-03-24 11:03:07 +0100484 conn_err(tconn, "fence-peer() = 7 && fencing != Stonith !!!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700485 ex_to_string = "peer was stonithed";
Philipp Reisnercb703452011-03-24 11:03:07 +0100486 mask.pdsk = D_MASK;
487 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700488 break;
489 default:
490 /* The script is broken ... */
Philipp Reisnercb703452011-03-24 11:03:07 +0100491 conn_err(tconn, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
492 return false; /* Eventually leave IO frozen */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700493 }
494
Philipp Reisnercb703452011-03-24 11:03:07 +0100495 conn_info(tconn, "fence-peer helper returned %d (%s)\n",
496 (r>>8) & 0xff, ex_to_string);
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200497
Philipp Reisnercb703452011-03-24 11:03:07 +0100498 out:
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200499
Philipp Reisnercb703452011-03-24 11:03:07 +0100500 /* Not using
501 conn_request_state(tconn, mask, val, CS_VERBOSE);
502 here, because we might were able to re-establish the connection in the
503 meantime. */
504 spin_lock_irq(&tconn->req_lock);
Philipp Reisnera1096a62012-04-06 12:07:34 +0200505 if (tconn->cstate < C_WF_REPORT_PARAMS && !test_bit(STATE_SENT, &tconn->flags))
Philipp Reisnercb703452011-03-24 11:03:07 +0100506 _conn_request_state(tconn, mask, val, CS_VERBOSE);
507 spin_unlock_irq(&tconn->req_lock);
508
509 return conn_highest_pdsk(tconn) <= D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700510}
511
Philipp Reisner87f7be42010-06-11 13:56:33 +0200512static int _try_outdate_peer_async(void *data)
513{
Philipp Reisnercb703452011-03-24 11:03:07 +0100514 struct drbd_tconn *tconn = (struct drbd_tconn *)data;
Philipp Reisner87f7be42010-06-11 13:56:33 +0200515
Philipp Reisnercb703452011-03-24 11:03:07 +0100516 conn_try_outdate_peer(tconn);
Philipp Reisner87f7be42010-06-11 13:56:33 +0200517
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200518 kref_put(&tconn->kref, &conn_destroy);
Philipp Reisner87f7be42010-06-11 13:56:33 +0200519 return 0;
520}
521
Philipp Reisnercb703452011-03-24 11:03:07 +0100522void conn_try_outdate_peer_async(struct drbd_tconn *tconn)
Philipp Reisner87f7be42010-06-11 13:56:33 +0200523{
524 struct task_struct *opa;
525
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200526 kref_get(&tconn->kref);
Philipp Reisnercb703452011-03-24 11:03:07 +0100527 opa = kthread_run(_try_outdate_peer_async, tconn, "drbd_async_h");
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200528 if (IS_ERR(opa)) {
Philipp Reisnercb703452011-03-24 11:03:07 +0100529 conn_err(tconn, "out of mem, failed to invoke fence-peer helper\n");
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200530 kref_put(&tconn->kref, &conn_destroy);
531 }
Philipp Reisner87f7be42010-06-11 13:56:33 +0200532}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700533
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100534enum drbd_state_rv
535drbd_set_role(struct drbd_conf *mdev, enum drbd_role new_role, int force)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700536{
537 const int max_tries = 4;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100538 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200539 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700540 int try = 0;
541 int forced = 0;
542 union drbd_state mask, val;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700543
544 if (new_role == R_PRIMARY)
Philipp Reisner0625ac12011-02-07 14:49:19 +0100545 request_ping(mdev->tconn); /* Detect a dead peer ASAP */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700546
Philipp Reisner8410da82011-02-11 20:11:10 +0100547 mutex_lock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700548
549 mask.i = 0; mask.role = R_MASK;
550 val.i = 0; val.role = new_role;
551
552 while (try++ < max_tries) {
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100553 rv = _drbd_request_state(mdev, mask, val, CS_WAIT_COMPLETE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700554
555 /* in case we first succeeded to outdate,
556 * but now suddenly could establish a connection */
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100557 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700558 val.pdsk = 0;
559 mask.pdsk = 0;
560 continue;
561 }
562
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100563 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
Philipp Reisnerd10a33c2010-03-04 15:11:39 +0100564 (mdev->state.disk < D_UP_TO_DATE &&
565 mdev->state.disk >= D_INCONSISTENT)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700566 mask.disk = D_MASK;
567 val.disk = D_UP_TO_DATE;
568 forced = 1;
569 continue;
570 }
571
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100572 if (rv == SS_NO_UP_TO_DATE_DISK &&
Philipp Reisnerb411b362009-09-25 16:07:19 -0700573 mdev->state.disk == D_CONSISTENT && mask.pdsk == 0) {
574 D_ASSERT(mdev->state.pdsk == D_UNKNOWN);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700575
Philipp Reisnercb703452011-03-24 11:03:07 +0100576 if (conn_try_outdate_peer(mdev->tconn)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700577 val.disk = D_UP_TO_DATE;
578 mask.disk = D_MASK;
579 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700580 continue;
581 }
582
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100583 if (rv == SS_NOTHING_TO_DO)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100584 goto out;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100585 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
Philipp Reisnercb703452011-03-24 11:03:07 +0100586 if (!conn_try_outdate_peer(mdev->tconn) && force) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700587 dev_warn(DEV, "Forced into split brain situation!\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100588 mask.pdsk = D_MASK;
589 val.pdsk = D_OUTDATED;
590
Philipp Reisnerb411b362009-09-25 16:07:19 -0700591 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700592 continue;
593 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100594 if (rv == SS_TWO_PRIMARIES) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700595 /* Maybe the peer is detected as dead very soon...
596 retry at most once more in this case. */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200597 int timeo;
598 rcu_read_lock();
599 nc = rcu_dereference(mdev->tconn->net_conf);
600 timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
601 rcu_read_unlock();
602 schedule_timeout_interruptible(timeo);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700603 if (try < max_tries)
604 try = max_tries - 1;
605 continue;
606 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100607 if (rv < SS_SUCCESS) {
608 rv = _drbd_request_state(mdev, mask, val,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700609 CS_VERBOSE + CS_WAIT_COMPLETE);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100610 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100611 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700612 }
613 break;
614 }
615
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100616 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100617 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700618
619 if (forced)
620 dev_warn(DEV, "Forced to consider local data as UpToDate!\n");
621
622 /* Wait until nothing is on the fly :) */
623 wait_event(mdev->misc_wait, atomic_read(&mdev->ap_pending_cnt) == 0);
624
625 if (new_role == R_SECONDARY) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100626 set_disk_ro(mdev->vdisk, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700627 if (get_ldev(mdev)) {
628 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
629 put_ldev(mdev);
630 }
631 } else {
Philipp Reisnera0095502011-05-03 13:14:15 +0200632 mutex_lock(&mdev->tconn->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +0200633 nc = mdev->tconn->net_conf;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200634 if (nc)
Andreas Gruenbacher6139f602011-05-06 20:00:02 +0200635 nc->discard_my_data = 0; /* without copy; single bit op is atomic */
Philipp Reisnera0095502011-05-03 13:14:15 +0200636 mutex_unlock(&mdev->tconn->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +0200637
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100638 set_disk_ro(mdev->vdisk, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700639 if (get_ldev(mdev)) {
640 if (((mdev->state.conn < C_CONNECTED ||
641 mdev->state.pdsk <= D_FAILED)
642 && mdev->ldev->md.uuid[UI_BITMAP] == 0) || forced)
643 drbd_uuid_new_current(mdev);
644
645 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
646 put_ldev(mdev);
647 }
648 }
649
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100650 /* writeout of activity log covered areas of the bitmap
651 * to stable storage done in after state change already */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700652
653 if (mdev->state.conn >= C_WF_REPORT_PARAMS) {
654 /* if this was forced, we should consider sync */
655 if (forced)
656 drbd_send_uuids(mdev);
Philipp Reisner43de7c82011-11-10 13:16:13 +0100657 drbd_send_current_state(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700658 }
659
660 drbd_md_sync(mdev);
661
662 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100663out:
Philipp Reisner8410da82011-02-11 20:11:10 +0100664 mutex_unlock(mdev->state_mutex);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100665 return rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700666}
667
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100668static const char *from_attrs_err_to_txt(int err)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700669{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100670 return err == -ENOMSG ? "required attribute missing" :
671 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
Lars Ellenbergf3990022011-03-23 14:31:09 +0100672 err == -EEXIST ? "can not change invariant setting" :
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100673 "invalid attribute value";
Philipp Reisnerb411b362009-09-25 16:07:19 -0700674}
675
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100676int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700677{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100678 struct set_role_parms parms;
679 int err;
680 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700681
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100682 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
683 if (!adm_ctx.reply_skb)
684 return retcode;
685 if (retcode != NO_ERROR)
686 goto out;
687
688 memset(&parms, 0, sizeof(parms));
689 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +0100690 err = set_role_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100691 if (err) {
692 retcode = ERR_MANDATORY_TAG;
693 drbd_msg_put_info(from_attrs_err_to_txt(err));
694 goto out;
695 }
696 }
697
698 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
699 retcode = drbd_set_role(adm_ctx.mdev, R_PRIMARY, parms.assume_uptodate);
700 else
701 retcode = drbd_set_role(adm_ctx.mdev, R_SECONDARY, 0);
702out:
703 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700704 return 0;
705}
706
707/* initializes the md.*_offset members, so we are able to find
708 * the on disk meta data */
709static void drbd_md_set_sector_offsets(struct drbd_conf *mdev,
710 struct drbd_backing_dev *bdev)
711{
712 sector_t md_size_sect = 0;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200713 int meta_dev_idx;
714
715 rcu_read_lock();
716 meta_dev_idx = rcu_dereference(bdev->disk_conf)->meta_dev_idx;
717
718 switch (meta_dev_idx) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700719 default:
720 /* v07 style fixed size indexed meta data */
721 bdev->md.md_size_sect = MD_RESERVED_SECT;
722 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
723 bdev->md.al_offset = MD_AL_OFFSET;
724 bdev->md.bm_offset = MD_BM_OFFSET;
725 break;
726 case DRBD_MD_INDEX_FLEX_EXT:
727 /* just occupy the full device; unit: sectors */
728 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
729 bdev->md.md_offset = 0;
730 bdev->md.al_offset = MD_AL_OFFSET;
731 bdev->md.bm_offset = MD_BM_OFFSET;
732 break;
733 case DRBD_MD_INDEX_INTERNAL:
734 case DRBD_MD_INDEX_FLEX_INT:
735 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
736 /* al size is still fixed */
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100737 bdev->md.al_offset = -MD_AL_SECTORS;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700738 /* we need (slightly less than) ~ this much bitmap sectors: */
739 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
740 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
741 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
742 md_size_sect = ALIGN(md_size_sect, 8);
743
744 /* plus the "drbd meta data super block",
745 * and the activity log; */
746 md_size_sect += MD_BM_OFFSET;
747
748 bdev->md.md_size_sect = md_size_sect;
749 /* bitmap offset is adjusted by 'super' block size */
750 bdev->md.bm_offset = -md_size_sect + MD_AL_OFFSET;
751 break;
752 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200753 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700754}
755
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100756/* input size is expected to be in KB */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700757char *ppsize(char *buf, unsigned long long size)
758{
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100759 /* Needs 9 bytes at max including trailing NUL:
760 * -1ULL ==> "16384 EB" */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700761 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
762 int base = 0;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100763 while (size >= 10000 && base < sizeof(units)-1) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700764 /* shift + round */
765 size = (size >> 10) + !!(size & (1<<9));
766 base++;
767 }
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100768 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700769
770 return buf;
771}
772
773/* there is still a theoretical deadlock when called from receiver
774 * on an D_INCONSISTENT R_PRIMARY:
775 * remote READ does inc_ap_bio, receiver would need to receive answer
776 * packet from remote to dec_ap_bio again.
777 * receiver receive_sizes(), comes here,
778 * waits for ap_bio_cnt == 0. -> deadlock.
779 * but this cannot happen, actually, because:
780 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
781 * (not connected, or bad/no disk on peer):
782 * see drbd_fail_request_early, ap_bio_cnt is zero.
783 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
784 * peer may not initiate a resize.
785 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100786/* Note these are not to be confused with
787 * drbd_adm_suspend_io/drbd_adm_resume_io,
788 * which are (sub) state changes triggered by admin (drbdsetup),
789 * and can be long lived.
790 * This changes an mdev->flag, is triggered by drbd internals,
791 * and should be short-lived. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700792void drbd_suspend_io(struct drbd_conf *mdev)
793{
794 set_bit(SUSPEND_IO, &mdev->flags);
Philipp Reisner2aebfab2011-03-28 16:48:11 +0200795 if (drbd_suspended(mdev))
Philipp Reisner265be2d2010-05-31 10:14:17 +0200796 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700797 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
798}
799
800void drbd_resume_io(struct drbd_conf *mdev)
801{
802 clear_bit(SUSPEND_IO, &mdev->flags);
803 wake_up(&mdev->misc_wait);
804}
805
806/**
807 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
808 * @mdev: DRBD device.
809 *
810 * Returns 0 on success, negative return values indicate errors.
811 * You should call drbd_md_sync() after calling this function.
812 */
Bart Van Assche24c48302011-05-21 18:32:29 +0200813enum 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 -0700814{
815 sector_t prev_first_sect, prev_size; /* previous meta location */
Philipp Reisneref5e44a2011-05-03 13:27:43 +0200816 sector_t la_size, u_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700817 sector_t size;
818 char ppb[10];
819
820 int md_moved, la_size_changed;
821 enum determine_dev_size rv = unchanged;
822
823 /* race:
824 * application request passes inc_ap_bio,
825 * but then cannot get an AL-reference.
826 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
827 *
828 * to avoid that:
829 * Suspend IO right here.
830 * still lock the act_log to not trigger ASSERTs there.
831 */
832 drbd_suspend_io(mdev);
833
834 /* no wait necessary anymore, actually we could assert that */
835 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
836
837 prev_first_sect = drbd_md_first_sector(mdev->ldev);
838 prev_size = mdev->ldev->md.md_size_sect;
839 la_size = mdev->ldev->md.la_size_sect;
840
841 /* TODO: should only be some assert here, not (re)init... */
842 drbd_md_set_sector_offsets(mdev, mdev->ldev);
843
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200844 rcu_read_lock();
845 u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
846 rcu_read_unlock();
Philipp Reisneref5e44a2011-05-03 13:27:43 +0200847 size = drbd_new_dev_size(mdev, mdev->ldev, u_size, flags & DDSF_FORCED);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700848
849 if (drbd_get_capacity(mdev->this_bdev) != size ||
850 drbd_bm_capacity(mdev) != size) {
851 int err;
Philipp Reisner02d9a942010-03-24 16:23:03 +0100852 err = drbd_bm_resize(mdev, size, !(flags & DDSF_NO_RESYNC));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700853 if (unlikely(err)) {
854 /* currently there is only one error: ENOMEM! */
855 size = drbd_bm_capacity(mdev)>>1;
856 if (size == 0) {
857 dev_err(DEV, "OUT OF MEMORY! "
858 "Could not allocate bitmap!\n");
859 } else {
860 dev_err(DEV, "BM resizing failed. "
861 "Leaving size unchanged at size = %lu KB\n",
862 (unsigned long)size);
863 }
864 rv = dev_size_error;
865 }
866 /* racy, see comments above. */
867 drbd_set_my_capacity(mdev, size);
868 mdev->ldev->md.la_size_sect = size;
869 dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
870 (unsigned long long)size>>1);
871 }
872 if (rv == dev_size_error)
873 goto out;
874
875 la_size_changed = (la_size != mdev->ldev->md.la_size_sect);
876
877 md_moved = prev_first_sect != drbd_md_first_sector(mdev->ldev)
878 || prev_size != mdev->ldev->md.md_size_sect;
879
880 if (la_size_changed || md_moved) {
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100881 int err;
882
Philipp Reisnerb411b362009-09-25 16:07:19 -0700883 drbd_al_shrink(mdev); /* All extents inactive. */
884 dev_info(DEV, "Writing the whole bitmap, %s\n",
885 la_size_changed && md_moved ? "size changed and md moved" :
886 la_size_changed ? "size changed" : "md moved");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100887 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
888 err = drbd_bitmap_io(mdev, &drbd_bm_write,
889 "size changed", BM_LOCKED_MASK);
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100890 if (err) {
891 rv = dev_size_error;
892 goto out;
893 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700894 drbd_md_mark_dirty(mdev);
895 }
896
897 if (size > la_size)
898 rv = grew;
899 if (size < la_size)
900 rv = shrunk;
901out:
902 lc_unlock(mdev->act_log);
903 wake_up(&mdev->al_wait);
904 drbd_resume_io(mdev);
905
906 return rv;
907}
908
909sector_t
Philipp Reisneref5e44a2011-05-03 13:27:43 +0200910drbd_new_dev_size(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
911 sector_t u_size, int assume_peer_has_space)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700912{
913 sector_t p_size = mdev->p_size; /* partner's disk size. */
914 sector_t la_size = bdev->md.la_size_sect; /* last agreed size. */
915 sector_t m_size; /* my size */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700916 sector_t size = 0;
917
918 m_size = drbd_get_max_capacity(bdev);
919
Philipp Reisnera393db62009-12-22 13:35:52 +0100920 if (mdev->state.conn < C_CONNECTED && assume_peer_has_space) {
921 dev_warn(DEV, "Resize while not connected was forced by the user!\n");
922 p_size = m_size;
923 }
924
Philipp Reisnerb411b362009-09-25 16:07:19 -0700925 if (p_size && m_size) {
926 size = min_t(sector_t, p_size, m_size);
927 } else {
928 if (la_size) {
929 size = la_size;
930 if (m_size && m_size < size)
931 size = m_size;
932 if (p_size && p_size < size)
933 size = p_size;
934 } else {
935 if (m_size)
936 size = m_size;
937 if (p_size)
938 size = p_size;
939 }
940 }
941
942 if (size == 0)
943 dev_err(DEV, "Both nodes diskless!\n");
944
945 if (u_size) {
946 if (u_size > size)
947 dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n",
948 (unsigned long)u_size>>1, (unsigned long)size>>1);
949 else
950 size = u_size;
951 }
952
953 return size;
954}
955
956/**
957 * drbd_check_al_size() - Ensures that the AL is of the right size
958 * @mdev: DRBD device.
959 *
960 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
961 * failed, and 0 on success. You should call drbd_md_sync() after you called
962 * this function.
963 */
Lars Ellenbergf3990022011-03-23 14:31:09 +0100964static int drbd_check_al_size(struct drbd_conf *mdev, struct disk_conf *dc)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700965{
966 struct lru_cache *n, *t;
967 struct lc_element *e;
968 unsigned int in_use;
969 int i;
970
Philipp Reisnerb411b362009-09-25 16:07:19 -0700971 if (mdev->act_log &&
Lars Ellenbergf3990022011-03-23 14:31:09 +0100972 mdev->act_log->nr_elements == dc->al_extents)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700973 return 0;
974
975 in_use = 0;
976 t = mdev->act_log;
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100977 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
Lars Ellenbergf3990022011-03-23 14:31:09 +0100978 dc->al_extents, sizeof(struct lc_element), 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700979
980 if (n == NULL) {
981 dev_err(DEV, "Cannot allocate act_log lru!\n");
982 return -ENOMEM;
983 }
984 spin_lock_irq(&mdev->al_lock);
985 if (t) {
986 for (i = 0; i < t->nr_elements; i++) {
987 e = lc_element_by_index(t, i);
988 if (e->refcnt)
989 dev_err(DEV, "refcnt(%d)==%d\n",
990 e->lc_number, e->refcnt);
991 in_use += e->refcnt;
992 }
993 }
994 if (!in_use)
995 mdev->act_log = n;
996 spin_unlock_irq(&mdev->al_lock);
997 if (in_use) {
998 dev_err(DEV, "Activity log still in use!\n");
999 lc_destroy(n);
1000 return -EBUSY;
1001 } else {
1002 if (t)
1003 lc_destroy(t);
1004 }
1005 drbd_md_mark_dirty(mdev); /* we changed mdev->act_log->nr_elemens */
1006 return 0;
1007}
1008
Philipp Reisner99432fc2011-05-20 16:39:13 +02001009static void drbd_setup_queue_param(struct drbd_conf *mdev, unsigned int max_bio_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001010{
1011 struct request_queue * const q = mdev->rq_queue;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001012 int max_hw_sectors = max_bio_size >> 9;
1013 int max_segments = 0;
1014
1015 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1016 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
1017
1018 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001019 rcu_read_lock();
1020 max_segments = rcu_dereference(mdev->ldev->disk_conf)->max_bio_bvecs;
1021 rcu_read_unlock();
Philipp Reisner99432fc2011-05-20 16:39:13 +02001022 put_ldev(mdev);
1023 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001024
Philipp Reisnerb411b362009-09-25 16:07:19 -07001025 blk_queue_logical_block_size(q, 512);
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001026 blk_queue_max_hw_sectors(q, max_hw_sectors);
1027 /* This is the workaround for "bio would need to, but cannot, be split" */
1028 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
1029 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001030
Philipp Reisner99432fc2011-05-20 16:39:13 +02001031 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1032 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001033
Philipp Reisner99432fc2011-05-20 16:39:13 +02001034 blk_queue_stack_limits(q, b);
1035
1036 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
1037 dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
1038 q->backing_dev_info.ra_pages,
1039 b->backing_dev_info.ra_pages);
1040 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1041 }
1042 put_ldev(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001043 }
1044}
1045
Philipp Reisner99432fc2011-05-20 16:39:13 +02001046void drbd_reconsider_max_bio_size(struct drbd_conf *mdev)
1047{
1048 int now, new, local, peer;
1049
1050 now = queue_max_hw_sectors(mdev->rq_queue) << 9;
1051 local = mdev->local_max_bio_size; /* Eventually last known value, from volatile memory */
1052 peer = mdev->peer_max_bio_size; /* Eventually last known value, from meta data */
1053
1054 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1055 local = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
1056 mdev->local_max_bio_size = local;
1057 put_ldev(mdev);
1058 }
1059
1060 /* We may ignore peer limits if the peer is modern enough.
1061 Because new from 8.3.8 onwards the peer can use multiple
1062 BIOs for a single peer_request */
1063 if (mdev->state.conn >= C_CONNECTED) {
Philipp Reisner31890f42011-01-19 14:12:51 +01001064 if (mdev->tconn->agreed_pro_version < 94)
Philipp Reisner2ffca4f2011-06-30 15:43:06 +02001065 peer = min_t(int, mdev->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
1066 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
Philipp Reisner31890f42011-01-19 14:12:51 +01001067 else if (mdev->tconn->agreed_pro_version == 94)
Philipp Reisner99432fc2011-05-20 16:39:13 +02001068 peer = DRBD_MAX_SIZE_H80_PACKET;
Philipp Reisner2ffca4f2011-06-30 15:43:06 +02001069 else if (mdev->tconn->agreed_pro_version < 100)
1070 peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
1071 else
Philipp Reisner99432fc2011-05-20 16:39:13 +02001072 peer = DRBD_MAX_BIO_SIZE;
1073 }
1074
1075 new = min_t(int, local, peer);
1076
1077 if (mdev->state.role == R_PRIMARY && new < now)
1078 dev_err(DEV, "ASSERT FAILED new < now; (%d < %d)\n", new, now);
1079
1080 if (new != now)
1081 dev_info(DEV, "max BIO size = %u\n", new);
1082
1083 drbd_setup_queue_param(mdev, new);
1084}
1085
Philipp Reisnera18e9d12011-04-24 11:09:55 +02001086/* Starts the worker thread */
Philipp Reisner0e29d162011-02-18 14:23:11 +01001087static void conn_reconfig_start(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001088{
Philipp Reisner0e29d162011-02-18 14:23:11 +01001089 drbd_thread_start(&tconn->worker);
1090 conn_flush_workqueue(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001091}
1092
Philipp Reisnera18e9d12011-04-24 11:09:55 +02001093/* if still unconfigured, stops worker again. */
Philipp Reisner0e29d162011-02-18 14:23:11 +01001094static void conn_reconfig_done(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001095{
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001096 bool stop_threads;
Philipp Reisner0e29d162011-02-18 14:23:11 +01001097 spin_lock_irq(&tconn->req_lock);
Philipp Reisnere0e16652011-07-11 17:04:23 +02001098 stop_threads = conn_all_vols_unconf(tconn) &&
1099 tconn->cstate == C_STANDALONE;
Philipp Reisner0e29d162011-02-18 14:23:11 +01001100 spin_unlock_irq(&tconn->req_lock);
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001101 if (stop_threads) {
1102 /* asender is implicitly stopped by receiver
Philipp Reisner81fa2e62011-05-04 15:10:30 +02001103 * in conn_disconnect() */
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001104 drbd_thread_stop(&tconn->receiver);
1105 drbd_thread_stop(&tconn->worker);
1106 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001107}
1108
Philipp Reisner07782862010-08-31 12:00:50 +02001109/* Make sure IO is suspended before calling this function(). */
1110static void drbd_suspend_al(struct drbd_conf *mdev)
1111{
1112 int s = 0;
1113
Lars Ellenberg61610422011-02-21 13:20:54 +01001114 if (!lc_try_lock(mdev->act_log)) {
Philipp Reisner07782862010-08-31 12:00:50 +02001115 dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n");
1116 return;
1117 }
1118
Lars Ellenberg61610422011-02-21 13:20:54 +01001119 drbd_al_shrink(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001120 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner07782862010-08-31 12:00:50 +02001121 if (mdev->state.conn < C_CONNECTED)
1122 s = !test_and_set_bit(AL_SUSPENDED, &mdev->flags);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001123 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg61610422011-02-21 13:20:54 +01001124 lc_unlock(mdev->act_log);
Philipp Reisner07782862010-08-31 12:00:50 +02001125
1126 if (s)
1127 dev_info(DEV, "Suspended AL updates\n");
1128}
1129
Lars Ellenberg5979e362011-04-27 21:09:55 +02001130
1131static bool should_set_defaults(struct genl_info *info)
1132{
1133 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1134 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1135}
1136
Philipp Reisnerd589a212011-05-04 10:06:52 +02001137static void enforce_disk_conf_limits(struct disk_conf *dc)
1138{
1139 if (dc->al_extents < DRBD_AL_EXTENTS_MIN)
1140 dc->al_extents = DRBD_AL_EXTENTS_MIN;
1141 if (dc->al_extents > DRBD_AL_EXTENTS_MAX)
1142 dc->al_extents = DRBD_AL_EXTENTS_MAX;
1143
1144 if (dc->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1145 dc->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
1146}
1147
Lars Ellenbergf3990022011-03-23 14:31:09 +01001148int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1149{
1150 enum drbd_ret_code retcode;
1151 struct drbd_conf *mdev;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001152 struct disk_conf *new_disk_conf, *old_disk_conf;
Philipp Reisner813472c2011-05-03 16:47:02 +02001153 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001154 int err, fifo_size;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001155
1156 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1157 if (!adm_ctx.reply_skb)
1158 return retcode;
1159 if (retcode != NO_ERROR)
1160 goto out;
1161
1162 mdev = adm_ctx.mdev;
1163
1164 /* we also need a disk
1165 * to change the options on */
1166 if (!get_ldev(mdev)) {
1167 retcode = ERR_NO_DISK;
1168 goto out;
1169 }
1170
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001171 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001172 if (!new_disk_conf) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001173 retcode = ERR_NOMEM;
1174 goto fail;
1175 }
1176
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001177 mutex_lock(&mdev->tconn->conf_update);
1178 old_disk_conf = mdev->ldev->disk_conf;
1179 *new_disk_conf = *old_disk_conf;
Lars Ellenberg5979e362011-04-27 21:09:55 +02001180 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02001181 set_disk_conf_defaults(new_disk_conf);
Lars Ellenberg5979e362011-04-27 21:09:55 +02001182
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001183 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02001184 if (err && err != -ENOMSG) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001185 retcode = ERR_MANDATORY_TAG;
1186 drbd_msg_put_info(from_attrs_err_to_txt(err));
1187 }
1188
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001189 if (!expect(new_disk_conf->resync_rate >= 1))
1190 new_disk_conf->resync_rate = 1;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001191
Philipp Reisnerd589a212011-05-04 10:06:52 +02001192 enforce_disk_conf_limits(new_disk_conf);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001193
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001194 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Philipp Reisner9958c852011-05-03 16:19:31 +02001195 if (fifo_size != mdev->rs_plan_s->size) {
Philipp Reisner813472c2011-05-03 16:47:02 +02001196 new_plan = fifo_alloc(fifo_size);
1197 if (!new_plan) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001198 dev_err(DEV, "kmalloc of fifo_buffer failed");
1199 retcode = ERR_NOMEM;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001200 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001201 }
1202 }
1203
Lars Ellenbergf3990022011-03-23 14:31:09 +01001204 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
1205 drbd_al_shrink(mdev);
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001206 err = drbd_check_al_size(mdev, new_disk_conf);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001207 lc_unlock(mdev->act_log);
1208 wake_up(&mdev->al_wait);
1209
1210 if (err) {
1211 retcode = ERR_NOMEM;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001212 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001213 }
1214
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001215 write_lock_irq(&global_state_lock);
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001216 retcode = drbd_resync_after_valid(mdev, new_disk_conf->resync_after);
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001217 if (retcode == NO_ERROR) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001218 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001219 drbd_resync_after_changed(mdev);
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001220 }
1221 write_unlock_irq(&global_state_lock);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001222
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001223 if (retcode != NO_ERROR)
1224 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001225
Philipp Reisner813472c2011-05-03 16:47:02 +02001226 if (new_plan) {
1227 old_plan = mdev->rs_plan_s;
1228 rcu_assign_pointer(mdev->rs_plan_s, new_plan);
Philipp Reisner9958c852011-05-03 16:19:31 +02001229 }
Philipp Reisner9958c852011-05-03 16:19:31 +02001230
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001231 mutex_unlock(&mdev->tconn->conf_update);
Philipp Reisner27eb13e2012-03-30 14:12:15 +02001232
1233 drbd_bump_write_ordering(mdev->tconn, WO_bdev_flush);
1234
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001235 drbd_md_sync(mdev);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001236
1237 if (mdev->state.conn >= C_CONNECTED)
1238 drbd_send_sync_param(mdev);
1239
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001240 synchronize_rcu();
1241 kfree(old_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02001242 kfree(old_plan);
Philipp Reisnercdfda632011-07-05 15:38:59 +02001243 mod_timer(&mdev->request_timer, jiffies + HZ);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001244 goto success;
1245
1246fail_unlock:
1247 mutex_unlock(&mdev->tconn->conf_update);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001248 fail:
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001249 kfree(new_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02001250 kfree(new_plan);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001251success:
1252 put_ldev(mdev);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001253 out:
1254 drbd_adm_finish(info, retcode);
1255 return 0;
1256}
1257
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001258int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001259{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001260 struct drbd_conf *mdev;
1261 int err;
Andreas Gruenbacher116676c2010-12-08 13:33:11 +01001262 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001263 enum determine_dev_size dd;
1264 sector_t max_possible_sectors;
1265 sector_t min_md_device_sectors;
1266 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001267 struct disk_conf *new_disk_conf = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001268 struct block_device *bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001269 struct lru_cache *resync_lru = NULL;
Philipp Reisner9958c852011-05-03 16:19:31 +02001270 struct fifo_buffer *new_plan = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001271 union drbd_state ns, os;
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001272 enum drbd_state_rv rv;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001273 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001274
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001275 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1276 if (!adm_ctx.reply_skb)
1277 return retcode;
1278 if (retcode != NO_ERROR)
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001279 goto finish;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001280
1281 mdev = adm_ctx.mdev;
Philipp Reisner0e29d162011-02-18 14:23:11 +01001282 conn_reconfig_start(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001283
1284 /* if you want to reconfigure, please tear down first */
1285 if (mdev->state.disk > D_DISKLESS) {
1286 retcode = ERR_DISK_CONFIGURED;
1287 goto fail;
1288 }
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001289 /* It may just now have detached because of IO error. Make sure
1290 * drbd_ldev_destroy is done already, we may end up here very fast,
1291 * e.g. if someone calls attach from the on-io-error handler,
1292 * to realize a "hot spare" feature (not that I'd recommend that) */
1293 wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001294
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001295 /* allocation not in the IO path, drbdsetup context */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001296 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1297 if (!nbc) {
1298 retcode = ERR_NOMEM;
1299 goto fail;
1300 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001301 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
1302 if (!new_disk_conf) {
1303 retcode = ERR_NOMEM;
1304 goto fail;
1305 }
1306 nbc->disk_conf = new_disk_conf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001307
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001308 set_disk_conf_defaults(new_disk_conf);
1309 err = disk_conf_from_attrs(new_disk_conf, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001310 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001311 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001312 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001313 goto fail;
1314 }
1315
Philipp Reisnerd589a212011-05-04 10:06:52 +02001316 enforce_disk_conf_limits(new_disk_conf);
1317
Philipp Reisner9958c852011-05-03 16:19:31 +02001318 new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
1319 if (!new_plan) {
1320 retcode = ERR_NOMEM;
1321 goto fail;
1322 }
1323
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001324 if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001325 retcode = ERR_MD_IDX_INVALID;
1326 goto fail;
1327 }
1328
Philipp Reisner44ed1672011-04-19 17:10:19 +02001329 rcu_read_lock();
1330 nc = rcu_dereference(mdev->tconn->net_conf);
1331 if (nc) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001332 if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02001333 rcu_read_unlock();
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001334 retcode = ERR_STONITH_AND_PROT_A;
1335 goto fail;
1336 }
1337 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02001338 rcu_read_unlock();
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001339
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001340 bdev = blkdev_get_by_path(new_disk_conf->backing_dev,
Tejun Heod4d77622010-11-13 11:55:18 +01001341 FMODE_READ | FMODE_WRITE | FMODE_EXCL, mdev);
Tejun Heoe525fd82010-11-13 11:55:17 +01001342 if (IS_ERR(bdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001343 dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->backing_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001344 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001345 retcode = ERR_OPEN_DISK;
1346 goto fail;
1347 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001348 nbc->backing_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001349
Tejun Heoe525fd82010-11-13 11:55:17 +01001350 /*
1351 * meta_dev_idx >= 0: external fixed size, possibly multiple
1352 * drbd sharing one meta device. TODO in that case, paranoia
1353 * check that [md_bdev, meta_dev_idx] is not yet used by some
1354 * other drbd minor! (if you use drbd.conf + drbdadm, that
1355 * should check it for you already; but if you don't, or
1356 * someone fooled it, we need to double check here)
1357 */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001358 bdev = blkdev_get_by_path(new_disk_conf->meta_dev,
Tejun Heod4d77622010-11-13 11:55:18 +01001359 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001360 (new_disk_conf->meta_dev_idx < 0) ?
Tejun Heod4d77622010-11-13 11:55:18 +01001361 (void *)mdev : (void *)drbd_m_holder);
Tejun Heoe525fd82010-11-13 11:55:17 +01001362 if (IS_ERR(bdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001363 dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->meta_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001364 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001365 retcode = ERR_OPEN_MD_DISK;
1366 goto fail;
1367 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001368 nbc->md_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001369
Tejun Heoe525fd82010-11-13 11:55:17 +01001370 if ((nbc->backing_bdev == nbc->md_bdev) !=
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001371 (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1372 new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001373 retcode = ERR_MD_IDX_INVALID;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001374 goto fail;
1375 }
1376
1377 resync_lru = lc_create("resync", drbd_bm_ext_cache,
Lars Ellenberg46a15bc2011-02-21 13:21:01 +01001378 1, 61, sizeof(struct bm_extent),
Philipp Reisnerb411b362009-09-25 16:07:19 -07001379 offsetof(struct bm_extent, lce));
1380 if (!resync_lru) {
1381 retcode = ERR_NOMEM;
Tejun Heoe525fd82010-11-13 11:55:17 +01001382 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001383 }
1384
1385 /* RT - for drbd_get_max_capacity() DRBD_MD_INDEX_FLEX_INT */
1386 drbd_md_set_sector_offsets(mdev, nbc);
1387
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001388 if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001389 dev_err(DEV, "max capacity %llu smaller than disk size %llu\n",
1390 (unsigned long long) drbd_get_max_capacity(nbc),
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001391 (unsigned long long) new_disk_conf->disk_size);
Lars Ellenberg67b58bf2011-06-06 15:36:04 +02001392 retcode = ERR_DISK_TOO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001393 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001394 }
1395
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001396 if (new_disk_conf->meta_dev_idx < 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001397 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1398 /* at least one MB, otherwise it does not make sense */
1399 min_md_device_sectors = (2<<10);
1400 } else {
1401 max_possible_sectors = DRBD_MAX_SECTORS;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001402 min_md_device_sectors = MD_RESERVED_SECT * (new_disk_conf->meta_dev_idx + 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001403 }
1404
Philipp Reisnerb411b362009-09-25 16:07:19 -07001405 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
Lars Ellenberg67b58bf2011-06-06 15:36:04 +02001406 retcode = ERR_MD_DISK_TOO_SMALL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001407 dev_warn(DEV, "refusing attach: md-device too small, "
1408 "at least %llu sectors needed for this meta-disk type\n",
1409 (unsigned long long) min_md_device_sectors);
Tejun Heoe525fd82010-11-13 11:55:17 +01001410 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001411 }
1412
1413 /* Make sure the new disk is big enough
1414 * (we may currently be R_PRIMARY with no local disk...) */
1415 if (drbd_get_max_capacity(nbc) <
1416 drbd_get_capacity(mdev->this_bdev)) {
Lars Ellenberg67b58bf2011-06-06 15:36:04 +02001417 retcode = ERR_DISK_TOO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001418 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001419 }
1420
1421 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1422
Lars Ellenberg13529942009-10-12 19:07:49 +02001423 if (nbc->known_size > max_possible_sectors) {
1424 dev_warn(DEV, "==> truncating very big lower level device "
1425 "to currently maximum possible %llu sectors <==\n",
1426 (unsigned long long) max_possible_sectors);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001427 if (new_disk_conf->meta_dev_idx >= 0)
Lars Ellenberg13529942009-10-12 19:07:49 +02001428 dev_warn(DEV, "==>> using internal or flexible "
1429 "meta data may help <<==\n");
1430 }
1431
Philipp Reisnerb411b362009-09-25 16:07:19 -07001432 drbd_suspend_io(mdev);
1433 /* also wait for the last barrier ack. */
Philipp Reisner2aebfab2011-03-28 16:48:11 +02001434 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_pending_cnt) || drbd_suspended(mdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001435 /* and for any other previously queued work */
Philipp Reisnera21e9292011-02-08 15:08:49 +01001436 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001437
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001438 rv = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE);
1439 retcode = rv; /* FIXME: Type mismatch. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001440 drbd_resume_io(mdev);
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001441 if (rv < SS_SUCCESS)
Tejun Heoe525fd82010-11-13 11:55:17 +01001442 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001443
1444 if (!get_ldev_if_state(mdev, D_ATTACHING))
1445 goto force_diskless;
1446
1447 drbd_md_set_sector_offsets(mdev, nbc);
1448
1449 if (!mdev->bitmap) {
1450 if (drbd_bm_init(mdev)) {
1451 retcode = ERR_NOMEM;
1452 goto force_diskless_dec;
1453 }
1454 }
1455
1456 retcode = drbd_md_read(mdev, nbc);
1457 if (retcode != NO_ERROR)
1458 goto force_diskless_dec;
1459
1460 if (mdev->state.conn < C_CONNECTED &&
1461 mdev->state.role == R_PRIMARY &&
1462 (mdev->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
1463 dev_err(DEV, "Can only attach to data with current UUID=%016llX\n",
1464 (unsigned long long)mdev->ed_uuid);
1465 retcode = ERR_DATA_NOT_CURRENT;
1466 goto force_diskless_dec;
1467 }
1468
1469 /* Since we are diskless, fix the activity log first... */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001470 if (drbd_check_al_size(mdev, new_disk_conf)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001471 retcode = ERR_NOMEM;
1472 goto force_diskless_dec;
1473 }
1474
1475 /* Prevent shrinking of consistent devices ! */
1476 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001477 drbd_new_dev_size(mdev, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001478 dev_warn(DEV, "refusing to truncate a consistent device\n");
Lars Ellenberg67b58bf2011-06-06 15:36:04 +02001479 retcode = ERR_DISK_TOO_SMALL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001480 goto force_diskless_dec;
1481 }
1482
Philipp Reisnerb411b362009-09-25 16:07:19 -07001483 /* Reset the "barriers don't work" bits here, then force meta data to
1484 * be written, to ensure we determine if barriers are supported. */
Andreas Gruenbachere5440462011-05-04 15:25:35 +02001485 if (new_disk_conf->md_flushes)
Philipp Reisnera8a4e512010-08-25 10:21:04 +02001486 clear_bit(MD_NO_FUA, &mdev->flags);
Andreas Gruenbachere5440462011-05-04 15:25:35 +02001487 else
1488 set_bit(MD_NO_FUA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001489
1490 /* Point of no return reached.
1491 * Devices and memory are no longer released by error cleanup below.
1492 * now mdev takes over responsibility, and the state engine should
1493 * clean it up somewhere. */
1494 D_ASSERT(mdev->ldev == NULL);
1495 mdev->ldev = nbc;
1496 mdev->resync = resync_lru;
Philipp Reisner9958c852011-05-03 16:19:31 +02001497 mdev->rs_plan_s = new_plan;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001498 nbc = NULL;
1499 resync_lru = NULL;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001500 new_disk_conf = NULL;
Philipp Reisner9958c852011-05-03 16:19:31 +02001501 new_plan = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001502
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001503 drbd_bump_write_ordering(mdev->tconn, WO_bdev_flush);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001504
1505 if (drbd_md_test_flag(mdev->ldev, MDF_CRASHED_PRIMARY))
1506 set_bit(CRASHED_PRIMARY, &mdev->flags);
1507 else
1508 clear_bit(CRASHED_PRIMARY, &mdev->flags);
1509
Philipp Reisner894c6a92010-06-18 16:03:20 +02001510 if (drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
Lars Ellenbergd5d7ebd2011-07-05 20:59:26 +02001511 !(mdev->state.role == R_PRIMARY && mdev->tconn->susp_nod))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001512 set_bit(CRASHED_PRIMARY, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001513
1514 mdev->send_cnt = 0;
1515 mdev->recv_cnt = 0;
1516 mdev->read_cnt = 0;
1517 mdev->writ_cnt = 0;
1518
Philipp Reisner99432fc2011-05-20 16:39:13 +02001519 drbd_reconsider_max_bio_size(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001520
1521 /* If I am currently not R_PRIMARY,
1522 * but meta data primary indicator is set,
1523 * I just now recover from a hard crash,
1524 * and have been R_PRIMARY before that crash.
1525 *
1526 * Now, if I had no connection before that crash
1527 * (have been degraded R_PRIMARY), chances are that
1528 * I won't find my peer now either.
1529 *
1530 * In that case, and _only_ in that case,
1531 * we use the degr-wfc-timeout instead of the default,
1532 * so we can automatically recover from a crash of a
1533 * degraded but active "cluster" after a certain timeout.
1534 */
1535 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
1536 if (mdev->state.role != R_PRIMARY &&
1537 drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
1538 !drbd_md_test_flag(mdev->ldev, MDF_CONNECTED_IND))
1539 set_bit(USE_DEGR_WFC_T, &mdev->flags);
1540
Bart Van Assche24c48302011-05-21 18:32:29 +02001541 dd = drbd_determine_dev_size(mdev, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001542 if (dd == dev_size_error) {
1543 retcode = ERR_NOMEM_BITMAP;
1544 goto force_diskless_dec;
1545 } else if (dd == grew)
1546 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
1547
1548 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
1549 dev_info(DEV, "Assuming that all blocks are out of sync "
1550 "(aka FullSync)\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001551 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write,
1552 "set_n_write from attaching", BM_LOCKED_MASK)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001553 retcode = ERR_IO_MD_DISK;
1554 goto force_diskless_dec;
1555 }
1556 } else {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001557 if (drbd_bitmap_io(mdev, &drbd_bm_read,
Andreas Gruenbacher22ab6a32010-12-13 01:44:11 +01001558 "read from attaching", BM_LOCKED_MASK)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001559 retcode = ERR_IO_MD_DISK;
1560 goto force_diskless_dec;
1561 }
1562 }
1563
Philipp Reisner07782862010-08-31 12:00:50 +02001564 if (_drbd_bm_total_weight(mdev) == drbd_bm_bits(mdev))
1565 drbd_suspend_al(mdev); /* IO is still suspended here... */
1566
Philipp Reisner87eeee42011-01-19 14:16:30 +01001567 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner78bae592011-03-28 15:40:12 +02001568 os = drbd_read_state(mdev);
1569 ns = os;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001570 /* If MDF_CONSISTENT is not set go into inconsistent state,
1571 otherwise investigate MDF_WasUpToDate...
1572 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1573 otherwise into D_CONSISTENT state.
1574 */
1575 if (drbd_md_test_flag(mdev->ldev, MDF_CONSISTENT)) {
1576 if (drbd_md_test_flag(mdev->ldev, MDF_WAS_UP_TO_DATE))
1577 ns.disk = D_CONSISTENT;
1578 else
1579 ns.disk = D_OUTDATED;
1580 } else {
1581 ns.disk = D_INCONSISTENT;
1582 }
1583
1584 if (drbd_md_test_flag(mdev->ldev, MDF_PEER_OUT_DATED))
1585 ns.pdsk = D_OUTDATED;
1586
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001587 rcu_read_lock();
1588 if (ns.disk == D_CONSISTENT &&
1589 (ns.pdsk == D_OUTDATED || rcu_dereference(mdev->ldev->disk_conf)->fencing == FP_DONT_CARE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001590 ns.disk = D_UP_TO_DATE;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001591 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001592
1593 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1594 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1595 this point, because drbd_request_state() modifies these
1596 flags. */
1597
1598 /* In case we are C_CONNECTED postpone any decision on the new disk
1599 state after the negotiation phase. */
1600 if (mdev->state.conn == C_CONNECTED) {
1601 mdev->new_state_tmp.i = ns.i;
1602 ns.i = os.i;
1603 ns.disk = D_NEGOTIATING;
Philipp Reisnerdc66c742010-06-02 14:31:29 +02001604
1605 /* We expect to receive up-to-date UUIDs soon.
1606 To avoid a race in receive_state, free p_uuid while
1607 holding req_lock. I.e. atomic with the state change */
1608 kfree(mdev->p_uuid);
1609 mdev->p_uuid = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001610 }
1611
1612 rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001613 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001614
1615 if (rv < SS_SUCCESS)
1616 goto force_diskless_dec;
1617
Philipp Reisnercdfda632011-07-05 15:38:59 +02001618 mod_timer(&mdev->request_timer, jiffies + HZ);
1619
Philipp Reisnerb411b362009-09-25 16:07:19 -07001620 if (mdev->state.role == R_PRIMARY)
1621 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
1622 else
1623 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
1624
1625 drbd_md_mark_dirty(mdev);
1626 drbd_md_sync(mdev);
1627
1628 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1629 put_ldev(mdev);
Philipp Reisner0e29d162011-02-18 14:23:11 +01001630 conn_reconfig_done(mdev->tconn);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001631 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001632 return 0;
1633
1634 force_diskless_dec:
1635 put_ldev(mdev);
1636 force_diskless:
Philipp Reisner9510b242011-07-01 17:00:57 +02001637 drbd_force_state(mdev, NS(disk, D_DISKLESS));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001638 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001639 fail:
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001640 conn_reconfig_done(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001641 if (nbc) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001642 if (nbc->backing_bdev)
1643 blkdev_put(nbc->backing_bdev,
1644 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1645 if (nbc->md_bdev)
1646 blkdev_put(nbc->md_bdev,
1647 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001648 kfree(nbc);
1649 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001650 kfree(new_disk_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001651 lc_destroy(resync_lru);
Philipp Reisner9958c852011-05-03 16:19:31 +02001652 kfree(new_plan);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001653
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001654 finish:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001655 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001656 return 0;
1657}
1658
Philipp Reisnercdfda632011-07-05 15:38:59 +02001659static int adm_detach(struct drbd_conf *mdev, int force)
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001660{
Philipp Reisner19f83c72011-03-29 14:21:03 +02001661 enum drbd_state_rv retcode;
Lars Ellenberg009ba892011-05-02 11:51:31 +02001662 int ret;
Philipp Reisnercdfda632011-07-05 15:38:59 +02001663
1664 if (force) {
1665 drbd_force_state(mdev, NS(disk, D_FAILED));
1666 retcode = SS_SUCCESS;
1667 goto out;
1668 }
1669
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001670 drbd_suspend_io(mdev); /* so no-one is stuck in drbd_al_begin_io */
Philipp Reisner0cfac5d2011-11-10 12:12:52 +01001671 drbd_md_get_buffer(mdev); /* make sure there is no in-flight meta-data IO */
Lars Ellenberg009ba892011-05-02 11:51:31 +02001672 retcode = drbd_request_state(mdev, NS(disk, D_FAILED));
Philipp Reisner0cfac5d2011-11-10 12:12:52 +01001673 drbd_md_put_buffer(mdev);
Lars Ellenberg009ba892011-05-02 11:51:31 +02001674 /* D_FAILED will transition to DISKLESS. */
1675 ret = wait_event_interruptible(mdev->misc_wait,
1676 mdev->state.disk != D_FAILED);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001677 drbd_resume_io(mdev);
Lars Ellenberg009ba892011-05-02 11:51:31 +02001678 if ((int)retcode == (int)SS_IS_DISKLESS)
1679 retcode = SS_NOTHING_TO_DO;
1680 if (ret)
1681 retcode = ERR_INTR;
Philipp Reisnercdfda632011-07-05 15:38:59 +02001682out:
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001683 return retcode;
1684}
1685
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001686/* Detaching the disk is a process in multiple stages. First we need to lock
1687 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1688 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1689 * internal references as well.
1690 * Only then we have finally detached. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001691int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001692{
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001693 enum drbd_ret_code retcode;
Philipp Reisnercdfda632011-07-05 15:38:59 +02001694 struct detach_parms parms = { };
1695 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001696
1697 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1698 if (!adm_ctx.reply_skb)
1699 return retcode;
1700 if (retcode != NO_ERROR)
1701 goto out;
1702
Philipp Reisnercdfda632011-07-05 15:38:59 +02001703 if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
1704 err = detach_parms_from_attrs(&parms, info);
1705 if (err) {
1706 retcode = ERR_MANDATORY_TAG;
1707 drbd_msg_put_info(from_attrs_err_to_txt(err));
1708 goto out;
1709 }
1710 }
1711
1712 retcode = adm_detach(adm_ctx.mdev, parms.force_detach);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001713out:
1714 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001715 return 0;
1716}
1717
Lars Ellenbergf3990022011-03-23 14:31:09 +01001718static bool conn_resync_running(struct drbd_tconn *tconn)
1719{
1720 struct drbd_conf *mdev;
Philipp Reisner695d08f2011-04-11 22:53:32 -07001721 bool rv = false;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001722 int vnr;
1723
Philipp Reisner695d08f2011-04-11 22:53:32 -07001724 rcu_read_lock();
Lars Ellenbergf3990022011-03-23 14:31:09 +01001725 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1726 if (mdev->state.conn == C_SYNC_SOURCE ||
1727 mdev->state.conn == C_SYNC_TARGET ||
1728 mdev->state.conn == C_PAUSED_SYNC_S ||
Philipp Reisner695d08f2011-04-11 22:53:32 -07001729 mdev->state.conn == C_PAUSED_SYNC_T) {
1730 rv = true;
1731 break;
1732 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001733 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001734 rcu_read_unlock();
1735
1736 return rv;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001737}
1738
1739static bool conn_ov_running(struct drbd_tconn *tconn)
1740{
1741 struct drbd_conf *mdev;
Philipp Reisner695d08f2011-04-11 22:53:32 -07001742 bool rv = false;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001743 int vnr;
1744
Philipp Reisner695d08f2011-04-11 22:53:32 -07001745 rcu_read_lock();
Lars Ellenbergf3990022011-03-23 14:31:09 +01001746 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1747 if (mdev->state.conn == C_VERIFY_S ||
Philipp Reisner695d08f2011-04-11 22:53:32 -07001748 mdev->state.conn == C_VERIFY_T) {
1749 rv = true;
1750 break;
1751 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001752 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001753 rcu_read_unlock();
1754
1755 return rv;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001756}
1757
Philipp Reisnercd643972011-04-13 18:00:59 -07001758static enum drbd_ret_code
Philipp Reisner44ed1672011-04-19 17:10:19 +02001759_check_net_options(struct drbd_tconn *tconn, struct net_conf *old_conf, struct net_conf *new_conf)
Philipp Reisnercd643972011-04-13 18:00:59 -07001760{
1761 struct drbd_conf *mdev;
1762 int i;
1763
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02001764 if (old_conf && tconn->cstate == C_WF_REPORT_PARAMS && tconn->agreed_pro_version < 100) {
1765 if (new_conf->wire_protocol != old_conf->wire_protocol)
1766 return ERR_NEED_APV_100;
1767
1768 if (new_conf->two_primaries != old_conf->two_primaries)
1769 return ERR_NEED_APV_100;
1770
1771 if (!new_conf->integrity_alg != !old_conf->integrity_alg)
1772 return ERR_NEED_APV_100;
1773
1774 if (strcmp(new_conf->integrity_alg, old_conf->integrity_alg))
1775 return ERR_NEED_APV_100;
1776 }
1777
1778 if (!new_conf->two_primaries &&
1779 conn_highest_role(tconn) == R_PRIMARY &&
1780 conn_highest_peer(tconn) == R_PRIMARY)
1781 return ERR_NEED_ALLOW_TWO_PRI;
Philipp Reisnerb032b6f2011-04-13 18:16:10 -07001782
Philipp Reisnercd643972011-04-13 18:00:59 -07001783 if (new_conf->two_primaries &&
1784 (new_conf->wire_protocol != DRBD_PROT_C))
1785 return ERR_NOT_PROTO_C;
1786
Philipp Reisnercd643972011-04-13 18:00:59 -07001787 idr_for_each_entry(&tconn->volumes, mdev, i) {
1788 if (get_ldev(mdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001789 enum drbd_fencing_p fp = rcu_dereference(mdev->ldev->disk_conf)->fencing;
Philipp Reisnercd643972011-04-13 18:00:59 -07001790 put_ldev(mdev);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001791 if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
Philipp Reisnercd643972011-04-13 18:00:59 -07001792 return ERR_STONITH_AND_PROT_A;
Philipp Reisnercd643972011-04-13 18:00:59 -07001793 }
Andreas Gruenbacher6139f602011-05-06 20:00:02 +02001794 if (mdev->state.role == R_PRIMARY && new_conf->discard_my_data)
Philipp Reisnercd643972011-04-13 18:00:59 -07001795 return ERR_DISCARD;
Philipp Reisnercd643972011-04-13 18:00:59 -07001796 }
Philipp Reisnercd643972011-04-13 18:00:59 -07001797
1798 if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A)
1799 return ERR_CONG_NOT_PROTO_A;
1800
1801 return NO_ERROR;
1802}
1803
Philipp Reisner44ed1672011-04-19 17:10:19 +02001804static enum drbd_ret_code
1805check_net_options(struct drbd_tconn *tconn, struct net_conf *new_conf)
1806{
1807 static enum drbd_ret_code rv;
1808 struct drbd_conf *mdev;
1809 int i;
1810
1811 rcu_read_lock();
1812 rv = _check_net_options(tconn, rcu_dereference(tconn->net_conf), new_conf);
1813 rcu_read_unlock();
1814
1815 /* tconn->volumes protected by genl_lock() here */
1816 idr_for_each_entry(&tconn->volumes, mdev, i) {
1817 if (!mdev->bitmap) {
1818 if(drbd_bm_init(mdev))
1819 return ERR_NOMEM;
1820 }
1821 }
1822
1823 return rv;
1824}
1825
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001826struct crypto {
1827 struct crypto_hash *verify_tfm;
1828 struct crypto_hash *csums_tfm;
1829 struct crypto_hash *cram_hmac_tfm;
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02001830 struct crypto_hash *integrity_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001831};
1832
1833static int
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02001834alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001835{
1836 if (!tfm_name[0])
1837 return NO_ERROR;
1838
1839 *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
1840 if (IS_ERR(*tfm)) {
1841 *tfm = NULL;
1842 return err_alg;
1843 }
1844
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001845 return NO_ERROR;
1846}
1847
1848static enum drbd_ret_code
1849alloc_crypto(struct crypto *crypto, struct net_conf *new_conf)
1850{
1851 char hmac_name[CRYPTO_MAX_ALG_NAME];
1852 enum drbd_ret_code rv;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001853
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02001854 rv = alloc_hash(&crypto->csums_tfm, new_conf->csums_alg,
1855 ERR_CSUMS_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001856 if (rv != NO_ERROR)
1857 return rv;
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02001858 rv = alloc_hash(&crypto->verify_tfm, new_conf->verify_alg,
1859 ERR_VERIFY_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001860 if (rv != NO_ERROR)
1861 return rv;
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02001862 rv = alloc_hash(&crypto->integrity_tfm, new_conf->integrity_alg,
1863 ERR_INTEGRITY_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001864 if (rv != NO_ERROR)
1865 return rv;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001866 if (new_conf->cram_hmac_alg[0] != 0) {
1867 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
1868 new_conf->cram_hmac_alg);
1869
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02001870 rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
1871 ERR_AUTH_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001872 }
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001873
1874 return rv;
1875}
1876
1877static void free_crypto(struct crypto *crypto)
1878{
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001879 crypto_free_hash(crypto->cram_hmac_tfm);
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02001880 crypto_free_hash(crypto->integrity_tfm);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001881 crypto_free_hash(crypto->csums_tfm);
1882 crypto_free_hash(crypto->verify_tfm);
1883}
1884
Lars Ellenbergf3990022011-03-23 14:31:09 +01001885int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
1886{
1887 enum drbd_ret_code retcode;
1888 struct drbd_tconn *tconn;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001889 struct net_conf *old_conf, *new_conf = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001890 int err;
1891 int ovr; /* online verify running */
1892 int rsr; /* re-sync running */
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001893 struct crypto crypto = { };
Lars Ellenbergf3990022011-03-23 14:31:09 +01001894
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02001895 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001896 if (!adm_ctx.reply_skb)
1897 return retcode;
1898 if (retcode != NO_ERROR)
1899 goto out;
1900
1901 tconn = adm_ctx.tconn;
1902
1903 new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
1904 if (!new_conf) {
1905 retcode = ERR_NOMEM;
1906 goto out;
1907 }
1908
Lars Ellenbergf3990022011-03-23 14:31:09 +01001909 conn_reconfig_start(tconn);
1910
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001911 mutex_lock(&tconn->data.mutex);
Philipp Reisnera0095502011-05-03 13:14:15 +02001912 mutex_lock(&tconn->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001913 old_conf = tconn->net_conf;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001914
1915 if (!old_conf) {
1916 drbd_msg_put_info("net conf missing, try connect");
1917 retcode = ERR_INVALID_REQUEST;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001918 goto fail;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001919 }
1920
1921 *new_conf = *old_conf;
Lars Ellenberg5979e362011-04-27 21:09:55 +02001922 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02001923 set_net_conf_defaults(new_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001924
Lars Ellenbergf3990022011-03-23 14:31:09 +01001925 err = net_conf_from_attrs_for_change(new_conf, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02001926 if (err && err != -ENOMSG) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001927 retcode = ERR_MANDATORY_TAG;
1928 drbd_msg_put_info(from_attrs_err_to_txt(err));
1929 goto fail;
1930 }
1931
Philipp Reisnercd643972011-04-13 18:00:59 -07001932 retcode = check_net_options(tconn, new_conf);
1933 if (retcode != NO_ERROR)
1934 goto fail;
1935
Lars Ellenbergf3990022011-03-23 14:31:09 +01001936 /* re-sync running */
1937 rsr = conn_resync_running(tconn);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001938 if (rsr && strcmp(new_conf->csums_alg, old_conf->csums_alg)) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001939 retcode = ERR_CSUMS_RESYNC_RUNNING;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001940 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001941 }
1942
Lars Ellenbergf3990022011-03-23 14:31:09 +01001943 /* online verify running */
1944 ovr = conn_ov_running(tconn);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001945 if (ovr && strcmp(new_conf->verify_alg, old_conf->verify_alg)) {
1946 retcode = ERR_VERIFY_RUNNING;
1947 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001948 }
1949
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001950 retcode = alloc_crypto(&crypto, new_conf);
1951 if (retcode != NO_ERROR)
1952 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001953
Philipp Reisner44ed1672011-04-19 17:10:19 +02001954 rcu_assign_pointer(tconn->net_conf, new_conf);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001955
1956 if (!rsr) {
1957 crypto_free_hash(tconn->csums_tfm);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001958 tconn->csums_tfm = crypto.csums_tfm;
1959 crypto.csums_tfm = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001960 }
1961 if (!ovr) {
1962 crypto_free_hash(tconn->verify_tfm);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001963 tconn->verify_tfm = crypto.verify_tfm;
1964 crypto.verify_tfm = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001965 }
1966
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02001967 crypto_free_hash(tconn->integrity_tfm);
1968 tconn->integrity_tfm = crypto.integrity_tfm;
Philipp Reisnerd659f2a2011-05-16 17:38:45 +02001969 if (tconn->cstate >= C_WF_REPORT_PARAMS && tconn->agreed_pro_version >= 100)
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001970 /* Do this without trying to take tconn->data.mutex again. */
Philipp Reisnerd659f2a2011-05-16 17:38:45 +02001971 __drbd_send_protocol(tconn, P_PROTOCOL_UPDATE);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001972
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001973 crypto_free_hash(tconn->cram_hmac_tfm);
1974 tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
1975
Philipp Reisnera0095502011-05-03 13:14:15 +02001976 mutex_unlock(&tconn->conf_update);
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001977 mutex_unlock(&tconn->data.mutex);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001978 synchronize_rcu();
1979 kfree(old_conf);
1980
Lars Ellenbergf3990022011-03-23 14:31:09 +01001981 if (tconn->cstate >= C_WF_REPORT_PARAMS)
1982 drbd_send_sync_param(minor_to_mdev(conn_lowest_minor(tconn)));
1983
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001984 goto done;
1985
Lars Ellenbergf3990022011-03-23 14:31:09 +01001986 fail:
Philipp Reisnera0095502011-05-03 13:14:15 +02001987 mutex_unlock(&tconn->conf_update);
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001988 mutex_unlock(&tconn->data.mutex);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001989 free_crypto(&crypto);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001990 kfree(new_conf);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001991 done:
Lars Ellenbergf3990022011-03-23 14:31:09 +01001992 conn_reconfig_done(tconn);
1993 out:
1994 drbd_adm_finish(info, retcode);
1995 return 0;
1996}
1997
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001998int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001999{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002000 struct drbd_conf *mdev;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002001 struct net_conf *old_conf, *new_conf = NULL;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002002 struct crypto crypto = { };
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002003 struct drbd_tconn *tconn;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002004 enum drbd_ret_code retcode;
2005 int i;
2006 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002007
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02002008 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002009
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002010 if (!adm_ctx.reply_skb)
2011 return retcode;
2012 if (retcode != NO_ERROR)
2013 goto out;
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002014 if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
2015 drbd_msg_put_info("connection endpoint(s) missing");
2016 retcode = ERR_INVALID_REQUEST;
2017 goto out;
2018 }
2019
2020 /* No need for _rcu here. All reconfiguration is
2021 * strictly serialized on genl_lock(). We are protected against
2022 * concurrent reconfiguration/addition/deletion */
2023 list_for_each_entry(tconn, &drbd_tconns, all_tconn) {
2024 if (nla_len(adm_ctx.my_addr) == tconn->my_addr_len &&
2025 !memcmp(nla_data(adm_ctx.my_addr), &tconn->my_addr, tconn->my_addr_len)) {
2026 retcode = ERR_LOCAL_ADDR;
2027 goto out;
2028 }
2029
2030 if (nla_len(adm_ctx.peer_addr) == tconn->peer_addr_len &&
2031 !memcmp(nla_data(adm_ctx.peer_addr), &tconn->peer_addr, tconn->peer_addr_len)) {
2032 retcode = ERR_PEER_ADDR;
2033 goto out;
2034 }
2035 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002036
2037 tconn = adm_ctx.tconn;
Philipp Reisner80883192011-02-18 14:56:45 +01002038 conn_reconfig_start(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002039
Philipp Reisner80883192011-02-18 14:56:45 +01002040 if (tconn->cstate > C_STANDALONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002041 retcode = ERR_NET_CONFIGURED;
2042 goto fail;
2043 }
2044
Andreas Gruenbachera209b4a2011-08-17 12:43:25 +02002045 /* allocation not in the IO path, drbdsetup / netlink process context */
Lars Ellenberg5979e362011-04-27 21:09:55 +02002046 new_conf = kzalloc(sizeof(*new_conf), GFP_KERNEL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002047 if (!new_conf) {
2048 retcode = ERR_NOMEM;
2049 goto fail;
2050 }
2051
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02002052 set_net_conf_defaults(new_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002053
Lars Ellenbergf3990022011-03-23 14:31:09 +01002054 err = net_conf_from_attrs(new_conf, info);
Lars Ellenberg25e40932011-08-19 10:39:00 +02002055 if (err && err != -ENOMSG) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002056 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002057 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002058 goto fail;
2059 }
2060
Philipp Reisnercd643972011-04-13 18:00:59 -07002061 retcode = check_net_options(tconn, new_conf);
2062 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002063 goto fail;
Philipp Reisner422028b2010-10-27 11:12:07 +02002064
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002065 retcode = alloc_crypto(&crypto, new_conf);
2066 if (retcode != NO_ERROR)
2067 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002068
Philipp Reisnerb411b362009-09-25 16:07:19 -07002069 ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
2070
Philipp Reisner80883192011-02-18 14:56:45 +01002071 conn_flush_workqueue(tconn);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002072
Philipp Reisnera0095502011-05-03 13:14:15 +02002073 mutex_lock(&tconn->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002074 old_conf = tconn->net_conf;
2075 if (old_conf) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002076 retcode = ERR_NET_CONFIGURED;
Philipp Reisnera0095502011-05-03 13:14:15 +02002077 mutex_unlock(&tconn->conf_update);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002078 goto fail;
2079 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002080 rcu_assign_pointer(tconn->net_conf, new_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002081
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002082 conn_free_crypto(tconn);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002083 tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02002084 tconn->integrity_tfm = crypto.integrity_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002085 tconn->csums_tfm = crypto.csums_tfm;
2086 tconn->verify_tfm = crypto.verify_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002087
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002088 tconn->my_addr_len = nla_len(adm_ctx.my_addr);
2089 memcpy(&tconn->my_addr, nla_data(adm_ctx.my_addr), tconn->my_addr_len);
2090 tconn->peer_addr_len = nla_len(adm_ctx.peer_addr);
2091 memcpy(&tconn->peer_addr, nla_data(adm_ctx.peer_addr), tconn->peer_addr_len);
2092
Philipp Reisnera0095502011-05-03 13:14:15 +02002093 mutex_unlock(&tconn->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002094
Philipp Reisner695d08f2011-04-11 22:53:32 -07002095 rcu_read_lock();
Philipp Reisner80883192011-02-18 14:56:45 +01002096 idr_for_each_entry(&tconn->volumes, mdev, i) {
2097 mdev->send_cnt = 0;
2098 mdev->recv_cnt = 0;
Philipp Reisner80883192011-02-18 14:56:45 +01002099 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07002100 rcu_read_unlock();
Lars Ellenberg5ee743e2011-04-26 16:22:25 +02002101
2102 retcode = conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
2103
Philipp Reisner80883192011-02-18 14:56:45 +01002104 conn_reconfig_done(tconn);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002105 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002106 return 0;
2107
2108fail:
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002109 free_crypto(&crypto);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002110 kfree(new_conf);
2111
Philipp Reisner80883192011-02-18 14:56:45 +01002112 conn_reconfig_done(tconn);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002113out:
2114 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002115 return 0;
2116}
2117
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002118static enum drbd_state_rv conn_try_disconnect(struct drbd_tconn *tconn, bool force)
2119{
2120 enum drbd_state_rv rv;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002121
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002122 rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
2123 force ? CS_HARD : 0);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002124
2125 switch (rv) {
2126 case SS_NOTHING_TO_DO:
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002127 break;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002128 case SS_ALREADY_STANDALONE:
2129 return SS_SUCCESS;
2130 case SS_PRIMARY_NOP:
2131 /* Our state checking code wants to see the peer outdated. */
2132 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002133 pdsk, D_OUTDATED), CS_VERBOSE);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002134 break;
2135 case SS_CW_FAILED_BY_PEER:
2136 /* The peer probably wants to see us outdated. */
2137 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
2138 disk, D_OUTDATED), 0);
2139 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002140 rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
2141 CS_HARD);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002142 }
2143 break;
2144 default:;
2145 /* no special handling necessary */
2146 }
2147
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002148 if (rv >= SS_SUCCESS) {
2149 enum drbd_state_rv rv2;
2150 /* No one else can reconfigure the network while I am here.
2151 * The state handling only uses drbd_thread_stop_nowait(),
2152 * we want to really wait here until the receiver is no more.
2153 */
2154 drbd_thread_stop(&adm_ctx.tconn->receiver);
2155
2156 /* Race breaker. This additional state change request may be
2157 * necessary, if this was a forced disconnect during a receiver
2158 * restart. We may have "killed" the receiver thread just
2159 * after drbdd_init() returned. Typically, we should be
2160 * C_STANDALONE already, now, and this becomes a no-op.
2161 */
2162 rv2 = conn_request_state(tconn, NS(conn, C_STANDALONE),
2163 CS_VERBOSE | CS_HARD);
2164 if (rv2 < SS_SUCCESS)
2165 conn_err(tconn,
2166 "unexpected rv2=%d in conn_try_disconnect()\n",
2167 rv2);
2168 }
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002169 return rv;
2170}
2171
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002172int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002173{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002174 struct disconnect_parms parms;
2175 struct drbd_tconn *tconn;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002176 enum drbd_state_rv rv;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002177 enum drbd_ret_code retcode;
2178 int err;
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002179
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002180 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002181 if (!adm_ctx.reply_skb)
2182 return retcode;
2183 if (retcode != NO_ERROR)
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002184 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002185
2186 tconn = adm_ctx.tconn;
2187 memset(&parms, 0, sizeof(parms));
2188 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002189 err = disconnect_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002190 if (err) {
2191 retcode = ERR_MANDATORY_TAG;
2192 drbd_msg_put_info(from_attrs_err_to_txt(err));
2193 goto fail;
2194 }
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002195 }
2196
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002197 rv = conn_try_disconnect(tconn, parms.force_disconnect);
2198 if (rv < SS_SUCCESS)
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002199 retcode = rv; /* FIXME: Type mismatch. */
2200 else
2201 retcode = NO_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002202 fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002203 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002204 return 0;
2205}
2206
2207void resync_after_online_grow(struct drbd_conf *mdev)
2208{
2209 int iass; /* I am sync source */
2210
2211 dev_info(DEV, "Resync of new storage after online grow\n");
2212 if (mdev->state.role != mdev->state.peer)
2213 iass = (mdev->state.role == R_PRIMARY);
2214 else
Philipp Reisner25703f82011-02-07 14:35:25 +01002215 iass = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002216
2217 if (iass)
2218 drbd_start_resync(mdev, C_SYNC_SOURCE);
2219 else
2220 _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
2221}
2222
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002223int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002224{
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002225 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002226 struct resize_parms rs;
2227 struct drbd_conf *mdev;
2228 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002229 enum determine_dev_size dd;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002230 enum dds_flags ddsf;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002231 sector_t u_size;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002232 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002233
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002234 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2235 if (!adm_ctx.reply_skb)
2236 return retcode;
2237 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002238 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002239
2240 memset(&rs, 0, sizeof(struct resize_parms));
2241 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002242 err = resize_parms_from_attrs(&rs, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002243 if (err) {
2244 retcode = ERR_MANDATORY_TAG;
2245 drbd_msg_put_info(from_attrs_err_to_txt(err));
2246 goto fail;
2247 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002248 }
2249
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002250 mdev = adm_ctx.mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002251 if (mdev->state.conn > C_CONNECTED) {
2252 retcode = ERR_RESIZE_RESYNC;
2253 goto fail;
2254 }
2255
2256 if (mdev->state.role == R_SECONDARY &&
2257 mdev->state.peer == R_SECONDARY) {
2258 retcode = ERR_NO_PRIMARY;
2259 goto fail;
2260 }
2261
2262 if (!get_ldev(mdev)) {
2263 retcode = ERR_NO_DISK;
2264 goto fail;
2265 }
2266
Philipp Reisner31890f42011-01-19 14:12:51 +01002267 if (rs.no_resync && mdev->tconn->agreed_pro_version < 93) {
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002268 retcode = ERR_NEED_APV_93;
Philipp Reisner9bcd2522011-09-29 13:00:14 +02002269 goto fail_ldev;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002270 }
2271
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002272 rcu_read_lock();
2273 u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
2274 rcu_read_unlock();
2275 if (u_size != (sector_t)rs.resize_size) {
2276 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
2277 if (!new_disk_conf) {
2278 retcode = ERR_NOMEM;
Philipp Reisner9bcd2522011-09-29 13:00:14 +02002279 goto fail_ldev;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002280 }
2281 }
2282
Philipp Reisner087c2492010-03-26 13:49:56 +01002283 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002284 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002285
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002286 if (new_disk_conf) {
2287 mutex_lock(&mdev->tconn->conf_update);
2288 old_disk_conf = mdev->ldev->disk_conf;
2289 *new_disk_conf = *old_disk_conf;
2290 new_disk_conf->disk_size = (sector_t)rs.resize_size;
2291 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
2292 mutex_unlock(&mdev->tconn->conf_update);
2293 synchronize_rcu();
2294 kfree(old_disk_conf);
2295 }
2296
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002297 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
Bart Van Assche24c48302011-05-21 18:32:29 +02002298 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002299 drbd_md_sync(mdev);
2300 put_ldev(mdev);
2301 if (dd == dev_size_error) {
2302 retcode = ERR_NOMEM_BITMAP;
2303 goto fail;
2304 }
2305
Philipp Reisner087c2492010-03-26 13:49:56 +01002306 if (mdev->state.conn == C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002307 if (dd == grew)
2308 set_bit(RESIZE_PENDING, &mdev->flags);
2309
2310 drbd_send_uuids(mdev);
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002311 drbd_send_sizes(mdev, 1, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002312 }
2313
2314 fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002315 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002316 return 0;
Philipp Reisner9bcd2522011-09-29 13:00:14 +02002317
2318 fail_ldev:
2319 put_ldev(mdev);
2320 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002321}
2322
Lars Ellenbergf3990022011-03-23 14:31:09 +01002323int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002324{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002325 enum drbd_ret_code retcode;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002326 struct drbd_tconn *tconn;
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002327 struct res_opts res_opts;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002328 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002329
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02002330 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002331 if (!adm_ctx.reply_skb)
2332 return retcode;
2333 if (retcode != NO_ERROR)
2334 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002335 tconn = adm_ctx.tconn;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002336
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002337 res_opts = tconn->res_opts;
Lars Ellenberg5979e362011-04-27 21:09:55 +02002338 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02002339 set_res_opts_defaults(&res_opts);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002340
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002341 err = res_opts_from_attrs(&res_opts, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02002342 if (err && err != -ENOMSG) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002343 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002344 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002345 goto fail;
2346 }
2347
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02002348 err = set_resource_options(tconn, &res_opts);
2349 if (err) {
2350 retcode = ERR_INVALID_REQUEST;
2351 if (err == -ENOMEM)
2352 retcode = ERR_NOMEM;
Philipp Reisner778f2712010-07-06 11:14:00 +02002353 }
2354
Philipp Reisnerb411b362009-09-25 16:07:19 -07002355fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002356 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002357 return 0;
2358}
2359
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002360int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002361{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002362 struct drbd_conf *mdev;
2363 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2364
2365 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2366 if (!adm_ctx.reply_skb)
2367 return retcode;
2368 if (retcode != NO_ERROR)
2369 goto out;
2370
2371 mdev = adm_ctx.mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002372
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002373 /* If there is still bitmap IO pending, probably because of a previous
2374 * resync just being finished, wait for it before requesting a new resync. */
Lars Ellenberg5016b822012-05-07 12:00:56 +02002375 drbd_suspend_io(mdev);
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002376 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
2377
Philipp Reisnerb411b362009-09-25 16:07:19 -07002378 retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T), CS_ORDERED);
2379
2380 if (retcode < SS_SUCCESS && retcode != SS_NEED_CONNECTION)
2381 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
2382
2383 while (retcode == SS_NEED_CONNECTION) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01002384 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002385 if (mdev->state.conn < C_CONNECTED)
2386 retcode = _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_VERBOSE, NULL);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002387 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002388
2389 if (retcode != SS_NEED_CONNECTION)
2390 break;
2391
2392 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
2393 }
Lars Ellenberg5016b822012-05-07 12:00:56 +02002394 drbd_resume_io(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002395
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002396out:
2397 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002398 return 0;
2399}
2400
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002401static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2402 union drbd_state mask, union drbd_state val)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002403{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002404 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002405
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002406 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2407 if (!adm_ctx.reply_skb)
2408 return retcode;
2409 if (retcode != NO_ERROR)
2410 goto out;
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002411
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002412 retcode = drbd_request_state(adm_ctx.mdev, mask, val);
2413out:
2414 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002415 return 0;
2416}
2417
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002418static int drbd_bmio_set_susp_al(struct drbd_conf *mdev)
2419{
2420 int rv;
2421
2422 rv = drbd_bmio_set_n_write(mdev);
2423 drbd_suspend_al(mdev);
2424 return rv;
2425}
2426
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002427int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002428{
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002429 int retcode; /* drbd_ret_code, drbd_state_rv */
2430 struct drbd_conf *mdev;
2431
2432 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2433 if (!adm_ctx.reply_skb)
2434 return retcode;
2435 if (retcode != NO_ERROR)
2436 goto out;
2437
2438 mdev = adm_ctx.mdev;
2439
Lars Ellenberg5016b822012-05-07 12:00:56 +02002440 /* If there is still bitmap IO pending, probably because of a previous
2441 * resync just being finished, wait for it before requesting a new resync. */
2442 drbd_suspend_io(mdev);
2443 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
2444
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002445 retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S), CS_ORDERED);
2446 if (retcode < SS_SUCCESS) {
2447 if (retcode == SS_NEED_CONNECTION && mdev->state.role == R_PRIMARY) {
2448 /* The peer will get a resync upon connect anyways.
2449 * Just make that into a full resync. */
2450 retcode = drbd_request_state(mdev, NS(pdsk, D_INCONSISTENT));
2451 if (retcode >= SS_SUCCESS) {
2452 if (drbd_bitmap_io(mdev, &drbd_bmio_set_susp_al,
2453 "set_n_write from invalidate_peer",
2454 BM_LOCKED_SET_ALLOWED))
2455 retcode = ERR_IO_MD_DISK;
2456 }
2457 } else
2458 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S));
2459 }
Lars Ellenberg5016b822012-05-07 12:00:56 +02002460 drbd_resume_io(mdev);
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002461
2462out:
2463 drbd_adm_finish(info, retcode);
2464 return 0;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002465}
Philipp Reisnerb411b362009-09-25 16:07:19 -07002466
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002467int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
2468{
2469 enum drbd_ret_code retcode;
2470
2471 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2472 if (!adm_ctx.reply_skb)
2473 return retcode;
2474 if (retcode != NO_ERROR)
2475 goto out;
2476
2477 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002478 retcode = ERR_PAUSE_IS_SET;
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_resume_sync(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002485{
Philipp Reisnerda9fbc22011-03-29 10:52:01 +02002486 union drbd_dev_state s;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002487 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002488
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002489 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2490 if (!adm_ctx.reply_skb)
2491 return retcode;
2492 if (retcode != NO_ERROR)
2493 goto out;
2494
2495 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2496 s = adm_ctx.mdev->state;
Philipp Reisnercd88d032011-01-20 11:46:41 +01002497 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2498 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2499 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2500 } else {
2501 retcode = ERR_PAUSE_IS_CLEAR;
2502 }
2503 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002504
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002505out:
2506 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002507 return 0;
2508}
2509
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002510int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002511{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002512 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002513}
2514
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002515int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002516{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002517 struct drbd_conf *mdev;
2518 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2519
2520 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2521 if (!adm_ctx.reply_skb)
2522 return retcode;
2523 if (retcode != NO_ERROR)
2524 goto out;
2525
2526 mdev = adm_ctx.mdev;
Philipp Reisner43a51822010-06-11 11:26:34 +02002527 if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
2528 drbd_uuid_new_current(mdev);
2529 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner43a51822010-06-11 11:26:34 +02002530 }
Philipp Reisner265be2d2010-05-31 10:14:17 +02002531 drbd_suspend_io(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002532 retcode = drbd_request_state(mdev, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
2533 if (retcode == SS_SUCCESS) {
Philipp Reisner265be2d2010-05-31 10:14:17 +02002534 if (mdev->state.conn < C_CONNECTED)
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01002535 tl_clear(mdev->tconn);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002536 if (mdev->state.disk == D_DISKLESS || mdev->state.disk == D_FAILED)
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01002537 tl_restart(mdev->tconn, FAIL_FROZEN_DISK_IO);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002538 }
2539 drbd_resume_io(mdev);
2540
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002541out:
2542 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002543 return 0;
2544}
2545
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002546int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002547{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002548 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002549}
2550
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002551int nla_put_drbd_cfg_context(struct sk_buff *skb, struct drbd_tconn *tconn, unsigned vnr)
Lars Ellenberg543cc102011-03-10 22:18:18 +01002552{
2553 struct nlattr *nla;
2554 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
2555 if (!nla)
2556 goto nla_put_failure;
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002557 if (vnr != VOLUME_UNSPECIFIED &&
2558 nla_put_u32(skb, T_ctx_volume, vnr))
2559 goto nla_put_failure;
2560 if (nla_put_string(skb, T_ctx_resource_name, tconn->name))
2561 goto nla_put_failure;
2562 if (tconn->my_addr_len &&
2563 nla_put(skb, T_ctx_my_addr, tconn->my_addr_len, &tconn->my_addr))
2564 goto nla_put_failure;
2565 if (tconn->peer_addr_len &&
2566 nla_put(skb, T_ctx_peer_addr, tconn->peer_addr_len, &tconn->peer_addr))
2567 goto nla_put_failure;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002568 nla_nest_end(skb, nla);
2569 return 0;
2570
2571nla_put_failure:
2572 if (nla)
2573 nla_nest_cancel(skb, nla);
2574 return -EMSGSIZE;
2575}
2576
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002577int nla_put_status_info(struct sk_buff *skb, struct drbd_conf *mdev,
2578 const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002579{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002580 struct state_info *si = NULL; /* for sizeof(si->member); */
Philipp Reisner44ed1672011-04-19 17:10:19 +02002581 struct net_conf *nc;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002582 struct nlattr *nla;
2583 int got_ldev;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002584 int err = 0;
2585 int exclude_sensitive;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002586
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002587 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2588 * to. So we better exclude_sensitive information.
2589 *
2590 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2591 * in the context of the requesting user process. Exclude sensitive
2592 * information, unless current has superuser.
2593 *
2594 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2595 * relies on the current implementation of netlink_dump(), which
2596 * executes the dump callback successively from netlink_recvmsg(),
2597 * always in the context of the receiving process */
2598 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002599
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002600 got_ldev = get_ldev(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002601
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002602 /* We need to add connection name and volume number information still.
2603 * Minor number is in drbd_genlmsghdr. */
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002604 if (nla_put_drbd_cfg_context(skb, mdev->tconn, mdev->vnr))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002605 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002606
Lars Ellenbergf3990022011-03-23 14:31:09 +01002607 if (res_opts_to_skb(skb, &mdev->tconn->res_opts, exclude_sensitive))
2608 goto nla_put_failure;
2609
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002610 rcu_read_lock();
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002611 if (got_ldev)
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002612 if (disk_conf_to_skb(skb, rcu_dereference(mdev->ldev->disk_conf), exclude_sensitive))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002613 goto nla_put_failure;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002614
Philipp Reisner44ed1672011-04-19 17:10:19 +02002615 nc = rcu_dereference(mdev->tconn->net_conf);
2616 if (nc)
2617 err = net_conf_to_skb(skb, nc, exclude_sensitive);
2618 rcu_read_unlock();
2619 if (err)
2620 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002621
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002622 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
2623 if (!nla)
2624 goto nla_put_failure;
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002625 if (nla_put_u32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY) ||
2626 nla_put_u32(skb, T_current_state, mdev->state.i) ||
2627 nla_put_u64(skb, T_ed_uuid, mdev->ed_uuid) ||
2628 nla_put_u64(skb, T_capacity, drbd_get_capacity(mdev->this_bdev)))
2629 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002630
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002631 if (got_ldev) {
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002632 if (nla_put_u32(skb, T_disk_flags, mdev->ldev->md.flags) ||
2633 nla_put(skb, T_uuids, sizeof(si->uuids), mdev->ldev->md.uuid) ||
2634 nla_put_u64(skb, T_bits_total, drbd_bm_bits(mdev)) ||
2635 nla_put_u64(skb, T_bits_oos, drbd_bm_total_weight(mdev)))
2636 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002637 if (C_SYNC_SOURCE <= mdev->state.conn &&
2638 C_PAUSED_SYNC_T >= mdev->state.conn) {
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002639 if (nla_put_u64(skb, T_bits_rs_total, mdev->rs_total) ||
2640 nla_put_u64(skb, T_bits_rs_failed, mdev->rs_failed))
2641 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002642 }
2643 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002644
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002645 if (sib) {
2646 switch(sib->sib_reason) {
2647 case SIB_SYNC_PROGRESS:
2648 case SIB_GET_STATUS_REPLY:
2649 break;
2650 case SIB_STATE_CHANGE:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002651 if (nla_put_u32(skb, T_prev_state, sib->os.i) ||
2652 nla_put_u32(skb, T_new_state, sib->ns.i))
2653 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002654 break;
2655 case SIB_HELPER_POST:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002656 if (nla_put_u32(skb, T_helper_exit_code,
2657 sib->helper_exit_code))
2658 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002659 /* fall through */
2660 case SIB_HELPER_PRE:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002661 if (nla_put_string(skb, T_helper, sib->helper_name))
2662 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002663 break;
2664 }
2665 }
2666 nla_nest_end(skb, nla);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002667
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002668 if (0)
2669nla_put_failure:
2670 err = -EMSGSIZE;
2671 if (got_ldev)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002672 put_ldev(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002673 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002674}
2675
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002676int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002677{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002678 enum drbd_ret_code retcode;
2679 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002680
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002681 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2682 if (!adm_ctx.reply_skb)
2683 return retcode;
2684 if (retcode != NO_ERROR)
2685 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002686
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002687 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.mdev, NULL);
2688 if (err) {
2689 nlmsg_free(adm_ctx.reply_skb);
2690 return err;
2691 }
2692out:
2693 drbd_adm_finish(info, retcode);
2694 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002695}
2696
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002697int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002698{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002699 struct drbd_conf *mdev;
2700 struct drbd_genlmsghdr *dh;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002701 struct drbd_tconn *pos = (struct drbd_tconn*)cb->args[0];
2702 struct drbd_tconn *tconn = NULL;
2703 struct drbd_tconn *tmp;
2704 unsigned volume = cb->args[1];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002705
Lars Ellenberg543cc102011-03-10 22:18:18 +01002706 /* Open coded, deferred, iteration:
2707 * list_for_each_entry_safe(tconn, tmp, &drbd_tconns, all_tconn) {
2708 * idr_for_each_entry(&tconn->volumes, mdev, i) {
2709 * ...
2710 * }
2711 * }
2712 * where tconn is cb->args[0];
2713 * and i is cb->args[1];
2714 *
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002715 * cb->args[2] indicates if we shall loop over all resources,
2716 * or just dump all volumes of a single resource.
2717 *
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002718 * This may miss entries inserted after this dump started,
2719 * or entries deleted before they are reached.
Lars Ellenberg543cc102011-03-10 22:18:18 +01002720 *
2721 * We need to make sure the mdev won't disappear while
2722 * we are looking at it, and revalidate our iterators
2723 * on each iteration.
2724 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002725
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02002726 /* synchronize with conn_create()/conn_destroy() */
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02002727 rcu_read_lock();
Lars Ellenberg543cc102011-03-10 22:18:18 +01002728 /* revalidate iterator position */
Philipp Reisnerec0bddb2011-05-04 15:47:01 +02002729 list_for_each_entry_rcu(tmp, &drbd_tconns, all_tconn) {
Lars Ellenberg543cc102011-03-10 22:18:18 +01002730 if (pos == NULL) {
2731 /* first iteration */
2732 pos = tmp;
2733 tconn = pos;
2734 break;
2735 }
2736 if (tmp == pos) {
2737 tconn = pos;
2738 break;
2739 }
2740 }
2741 if (tconn) {
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002742next_tconn:
Lars Ellenberg543cc102011-03-10 22:18:18 +01002743 mdev = idr_get_next(&tconn->volumes, &volume);
2744 if (!mdev) {
2745 /* No more volumes to dump on this tconn.
2746 * Advance tconn iterator. */
Philipp Reisnerec0bddb2011-05-04 15:47:01 +02002747 pos = list_entry_rcu(tconn->all_tconn.next,
2748 struct drbd_tconn, all_tconn);
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002749 /* Did we dump any volume on this tconn yet? */
Lars Ellenberg543cc102011-03-10 22:18:18 +01002750 if (volume != 0) {
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002751 /* If we reached the end of the list,
2752 * or only a single resource dump was requested,
2753 * we are done. */
2754 if (&pos->all_tconn == &drbd_tconns || cb->args[2])
2755 goto out;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002756 volume = 0;
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002757 tconn = pos;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002758 goto next_tconn;
2759 }
2760 }
2761
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002762 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).pid,
2763 cb->nlh->nlmsg_seq, &drbd_genl_family,
2764 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
2765 if (!dh)
Lars Ellenberg543cc102011-03-10 22:18:18 +01002766 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002767
Lars Ellenberg543cc102011-03-10 22:18:18 +01002768 if (!mdev) {
Lars Ellenberg367d675d2011-07-11 23:49:55 +02002769 /* This is a tconn without a single volume.
2770 * Suprisingly enough, it may have a network
2771 * configuration. */
2772 struct net_conf *nc;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002773 dh->minor = -1U;
2774 dh->ret_code = NO_ERROR;
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002775 if (nla_put_drbd_cfg_context(skb, tconn, VOLUME_UNSPECIFIED))
Lars Ellenberg367d675d2011-07-11 23:49:55 +02002776 goto cancel;
2777 nc = rcu_dereference(tconn->net_conf);
2778 if (nc && net_conf_to_skb(skb, nc, 1) != 0)
2779 goto cancel;
2780 goto done;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002781 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002782
Lars Ellenberg543cc102011-03-10 22:18:18 +01002783 D_ASSERT(mdev->vnr == volume);
2784 D_ASSERT(mdev->tconn == tconn);
2785
2786 dh->minor = mdev_to_minor(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002787 dh->ret_code = NO_ERROR;
2788
2789 if (nla_put_status_info(skb, mdev, NULL)) {
Lars Ellenberg367d675d2011-07-11 23:49:55 +02002790cancel:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002791 genlmsg_cancel(skb, dh);
Lars Ellenberg543cc102011-03-10 22:18:18 +01002792 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002793 }
Lars Ellenberg367d675d2011-07-11 23:49:55 +02002794done:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002795 genlmsg_end(skb, dh);
2796 }
2797
Lars Ellenberg543cc102011-03-10 22:18:18 +01002798out:
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02002799 rcu_read_unlock();
Lars Ellenberg543cc102011-03-10 22:18:18 +01002800 /* where to start the next iteration */
2801 cb->args[0] = (long)pos;
2802 cb->args[1] = (pos == tconn) ? volume + 1 : 0;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002803
Lars Ellenberg543cc102011-03-10 22:18:18 +01002804 /* No more tconns/volumes/minors found results in an empty skb.
2805 * Which will terminate the dump. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002806 return skb->len;
2807}
2808
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002809/*
2810 * Request status of all resources, or of all volumes within a single resource.
2811 *
2812 * This is a dump, as the answer may not fit in a single reply skb otherwise.
2813 * Which means we cannot use the family->attrbuf or other such members, because
2814 * dump is NOT protected by the genl_lock(). During dump, we only have access
2815 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
2816 *
2817 * Once things are setup properly, we call into get_one_status().
2818 */
2819int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
2820{
2821 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
2822 struct nlattr *nla;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02002823 const char *resource_name;
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002824 struct drbd_tconn *tconn;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02002825 int maxtype;
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002826
2827 /* Is this a followup call? */
2828 if (cb->args[0]) {
2829 /* ... of a single resource dump,
2830 * and the resource iterator has been advanced already? */
2831 if (cb->args[2] && cb->args[2] != cb->args[0])
2832 return 0; /* DONE. */
2833 goto dump;
2834 }
2835
2836 /* First call (from netlink_dump_start). We need to figure out
2837 * which resource(s) the user wants us to dump. */
2838 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
2839 nlmsg_attrlen(cb->nlh, hdrlen),
2840 DRBD_NLA_CFG_CONTEXT);
2841
2842 /* No explicit context given. Dump all. */
2843 if (!nla)
2844 goto dump;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02002845 maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
2846 nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
2847 if (IS_ERR(nla))
2848 return PTR_ERR(nla);
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002849 /* context given, but no name present? */
2850 if (!nla)
2851 return -EINVAL;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02002852 resource_name = nla_data(nla);
2853 tconn = conn_get_by_name(resource_name);
Philipp Reisner0ace9df2011-04-24 10:53:19 +02002854
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002855 if (!tconn)
2856 return -ENODEV;
2857
Philipp Reisner0ace9df2011-04-24 10:53:19 +02002858 kref_put(&tconn->kref, &conn_destroy); /* get_one_status() (re)validates tconn by itself */
2859
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002860 /* prime iterators, and set "filter" mode mark:
2861 * only dump this tconn. */
2862 cb->args[0] = (long)tconn;
2863 /* cb->args[1] = 0; passed in this way. */
2864 cb->args[2] = (long)tconn;
2865
2866dump:
2867 return get_one_status(skb, cb);
2868}
2869
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002870int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
2871{
2872 enum drbd_ret_code retcode;
2873 struct timeout_parms tp;
2874 int err;
2875
2876 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2877 if (!adm_ctx.reply_skb)
2878 return retcode;
2879 if (retcode != NO_ERROR)
2880 goto out;
2881
2882 tp.timeout_type =
2883 adm_ctx.mdev->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
2884 test_bit(USE_DEGR_WFC_T, &adm_ctx.mdev->flags) ? UT_DEGRADED :
2885 UT_DEFAULT;
2886
2887 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
2888 if (err) {
2889 nlmsg_free(adm_ctx.reply_skb);
2890 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002891 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002892out:
2893 drbd_adm_finish(info, retcode);
2894 return 0;
2895}
Lars Ellenberg873b0d52011-01-21 22:53:48 +01002896
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002897int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
2898{
2899 struct drbd_conf *mdev;
2900 enum drbd_ret_code retcode;
2901
2902 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2903 if (!adm_ctx.reply_skb)
2904 return retcode;
2905 if (retcode != NO_ERROR)
2906 goto out;
2907
2908 mdev = adm_ctx.mdev;
2909 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
2910 /* resume from last known position, if possible */
2911 struct start_ov_parms parms =
2912 { .ov_start_sector = mdev->ov_start_sector };
Lars Ellenbergf3990022011-03-23 14:31:09 +01002913 int err = start_ov_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002914 if (err) {
2915 retcode = ERR_MANDATORY_TAG;
2916 drbd_msg_put_info(from_attrs_err_to_txt(err));
2917 goto out;
2918 }
2919 /* w_make_ov_request expects position to be aligned */
2920 mdev->ov_start_sector = parms.ov_start_sector & ~BM_SECT_PER_BIT;
2921 }
Lars Ellenberg873b0d52011-01-21 22:53:48 +01002922 /* If there is still bitmap IO pending, e.g. previous resync or verify
2923 * just being finished, wait for it before requesting a new resync. */
Lars Ellenberg5016b822012-05-07 12:00:56 +02002924 drbd_suspend_io(mdev);
Lars Ellenberg873b0d52011-01-21 22:53:48 +01002925 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002926 retcode = drbd_request_state(mdev,NS(conn,C_VERIFY_S));
Lars Ellenberg5016b822012-05-07 12:00:56 +02002927 drbd_resume_io(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002928out:
2929 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002930 return 0;
2931}
2932
2933
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002934int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002935{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002936 struct drbd_conf *mdev;
2937 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002938 int skip_initial_sync = 0;
2939 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002940 struct new_c_uuid_parms args;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002941
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002942 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2943 if (!adm_ctx.reply_skb)
2944 return retcode;
2945 if (retcode != NO_ERROR)
2946 goto out_nolock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002947
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002948 mdev = adm_ctx.mdev;
2949 memset(&args, 0, sizeof(args));
2950 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002951 err = new_c_uuid_parms_from_attrs(&args, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002952 if (err) {
2953 retcode = ERR_MANDATORY_TAG;
2954 drbd_msg_put_info(from_attrs_err_to_txt(err));
2955 goto out_nolock;
2956 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002957 }
2958
Philipp Reisner8410da82011-02-11 20:11:10 +01002959 mutex_lock(mdev->state_mutex); /* Protects us against serialized state changes. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002960
2961 if (!get_ldev(mdev)) {
2962 retcode = ERR_NO_DISK;
2963 goto out;
2964 }
2965
2966 /* this is "skip initial sync", assume to be clean */
Philipp Reisner31890f42011-01-19 14:12:51 +01002967 if (mdev->state.conn == C_CONNECTED && mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07002968 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
2969 dev_info(DEV, "Preparing to skip initial sync\n");
2970 skip_initial_sync = 1;
2971 } else if (mdev->state.conn != C_STANDALONE) {
2972 retcode = ERR_CONNECTED;
2973 goto out_dec;
2974 }
2975
2976 drbd_uuid_set(mdev, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
2977 drbd_uuid_new_current(mdev); /* New current, previous to UI_BITMAP */
2978
2979 if (args.clear_bm) {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002980 err = drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
2981 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002982 if (err) {
2983 dev_err(DEV, "Writing bitmap failed with %d\n",err);
2984 retcode = ERR_IO_MD_DISK;
2985 }
2986 if (skip_initial_sync) {
2987 drbd_send_uuids_skip_initial_sync(mdev);
2988 _drbd_uuid_set(mdev, UI_BITMAP, 0);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002989 drbd_print_uuids(mdev, "cleared bitmap UUID");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002990 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002991 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
2992 CS_VERBOSE, NULL);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002993 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002994 }
2995 }
2996
2997 drbd_md_sync(mdev);
2998out_dec:
2999 put_ldev(mdev);
3000out:
Philipp Reisner8410da82011-02-11 20:11:10 +01003001 mutex_unlock(mdev->state_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003002out_nolock:
3003 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003004 return 0;
3005}
3006
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003007static enum drbd_ret_code
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003008drbd_check_resource_name(const char *name)
Philipp Reisner774b3052011-02-22 02:07:03 -05003009{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003010 if (!name || !name[0]) {
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003011 drbd_msg_put_info("resource name missing");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003012 return ERR_MANDATORY_TAG;
3013 }
3014 /* if we want to use these in sysfs/configfs/debugfs some day,
3015 * we must not allow slashes */
3016 if (strchr(name, '/')) {
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003017 drbd_msg_put_info("invalid resource name");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003018 return ERR_INVALID_REQUEST;
3019 }
3020 return NO_ERROR;
3021}
Philipp Reisner774b3052011-02-22 02:07:03 -05003022
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003023int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003024{
3025 enum drbd_ret_code retcode;
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003026 struct res_opts res_opts;
3027 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003028
3029 retcode = drbd_adm_prepare(skb, info, 0);
3030 if (!adm_ctx.reply_skb)
3031 return retcode;
3032 if (retcode != NO_ERROR)
3033 goto out;
3034
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003035 set_res_opts_defaults(&res_opts);
3036 err = res_opts_from_attrs(&res_opts, info);
3037 if (err && err != -ENOMSG) {
3038 retcode = ERR_MANDATORY_TAG;
3039 drbd_msg_put_info(from_attrs_err_to_txt(err));
3040 goto out;
3041 }
3042
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003043 retcode = drbd_check_resource_name(adm_ctx.resource_name);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003044 if (retcode != NO_ERROR)
3045 goto out;
3046
3047 if (adm_ctx.tconn) {
Lars Ellenberg38f19612011-03-14 13:22:35 +01003048 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
3049 retcode = ERR_INVALID_REQUEST;
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003050 drbd_msg_put_info("resource exists");
Lars Ellenberg38f19612011-03-14 13:22:35 +01003051 }
3052 /* else: still NO_ERROR */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003053 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05003054 }
3055
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003056 if (!conn_create(adm_ctx.resource_name, &res_opts))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003057 retcode = ERR_NOMEM;
3058out:
3059 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003060 return 0;
3061}
3062
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003063int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05003064{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003065 struct drbd_genlmsghdr *dh = info->userhdr;
3066 enum drbd_ret_code retcode;
Philipp Reisner774b3052011-02-22 02:07:03 -05003067
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02003068 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003069 if (!adm_ctx.reply_skb)
3070 return retcode;
3071 if (retcode != NO_ERROR)
3072 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05003073
Andreas Gruenbacherf2257a52011-07-14 16:00:40 +02003074 if (dh->minor > MINORMASK) {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003075 drbd_msg_put_info("requested minor out of range");
3076 retcode = ERR_INVALID_REQUEST;
3077 goto out;
3078 }
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +02003079 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003080 drbd_msg_put_info("requested volume id out of range");
3081 retcode = ERR_INVALID_REQUEST;
3082 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05003083 }
3084
Lars Ellenberg38f19612011-03-14 13:22:35 +01003085 /* drbd_adm_prepare made sure already
3086 * that mdev->tconn and mdev->vnr match the request. */
3087 if (adm_ctx.mdev) {
3088 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
3089 retcode = ERR_MINOR_EXISTS;
3090 /* else: still NO_ERROR */
3091 goto out;
3092 }
3093
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003094 retcode = conn_new_minor(adm_ctx.tconn, dh->minor, adm_ctx.volume);
3095out:
3096 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003097 return 0;
3098}
3099
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003100static enum drbd_ret_code adm_delete_minor(struct drbd_conf *mdev)
3101{
3102 if (mdev->state.disk == D_DISKLESS &&
3103 /* no need to be mdev->state.conn == C_STANDALONE &&
3104 * we may want to delete a minor from a live replication group.
3105 */
3106 mdev->state.role == R_SECONDARY) {
Philipp Reisner369bea62011-07-06 23:04:44 +02003107 _drbd_request_state(mdev, NS(conn, C_WF_REPORT_PARAMS),
3108 CS_VERBOSE + CS_WAIT_COMPLETE);
Philipp Reisner81fa2e62011-05-04 15:10:30 +02003109 idr_remove(&mdev->tconn->volumes, mdev->vnr);
3110 idr_remove(&minors, mdev_to_minor(mdev));
3111 del_gendisk(mdev->vdisk);
3112 synchronize_rcu();
3113 kref_put(&mdev->kref, &drbd_minor_destroy);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003114 return NO_ERROR;
3115 } else
3116 return ERR_MINOR_CONFIGURED;
3117}
3118
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003119int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05003120{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003121 enum drbd_ret_code retcode;
3122
3123 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3124 if (!adm_ctx.reply_skb)
3125 return retcode;
3126 if (retcode != NO_ERROR)
3127 goto out;
3128
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003129 retcode = adm_delete_minor(adm_ctx.mdev);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003130out:
3131 drbd_adm_finish(info, retcode);
3132 return 0;
3133}
3134
3135int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
3136{
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003137 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003138 struct drbd_conf *mdev;
3139 unsigned i;
3140
3141 retcode = drbd_adm_prepare(skb, info, 0);
3142 if (!adm_ctx.reply_skb)
3143 return retcode;
3144 if (retcode != NO_ERROR)
3145 goto out;
3146
3147 if (!adm_ctx.tconn) {
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003148 retcode = ERR_RES_NOT_KNOWN;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003149 goto out;
3150 }
3151
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003152 /* demote */
3153 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3154 retcode = drbd_set_role(mdev, R_SECONDARY, 0);
3155 if (retcode < SS_SUCCESS) {
3156 drbd_msg_put_info("failed to demote");
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003157 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003158 }
3159 }
3160
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003161 retcode = conn_try_disconnect(adm_ctx.tconn, 0);
3162 if (retcode < SS_SUCCESS) {
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003163 drbd_msg_put_info("failed to disconnect");
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003164 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003165 }
3166
3167 /* detach */
3168 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
Philipp Reisnercdfda632011-07-05 15:38:59 +02003169 retcode = adm_detach(mdev, 0);
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003170 if (retcode < SS_SUCCESS) {
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003171 drbd_msg_put_info("failed to detach");
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003172 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003173 }
3174 }
3175
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003176 /* If we reach this, all volumes (of this tconn) are Secondary,
3177 * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003178 * actually stopped, state handling only does drbd_thread_stop_nowait(). */
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003179 drbd_thread_stop(&adm_ctx.tconn->worker);
3180
3181 /* Now, nothing can fail anymore */
3182
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003183 /* delete volumes */
3184 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3185 retcode = adm_delete_minor(mdev);
3186 if (retcode != NO_ERROR) {
3187 /* "can not happen" */
3188 drbd_msg_put_info("failed to delete volume");
Philipp Reisneref356262011-04-13 14:21:29 -07003189 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003190 }
3191 }
3192
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003193 /* delete connection */
3194 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
Philipp Reisnerec0bddb2011-05-04 15:47:01 +02003195 list_del_rcu(&adm_ctx.tconn->all_tconn);
3196 synchronize_rcu();
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02003197 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
3198
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003199 retcode = NO_ERROR;
3200 } else {
3201 /* "can not happen" */
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003202 retcode = ERR_RES_IN_USE;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003203 drbd_msg_put_info("failed to delete connection");
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003204 }
Philipp Reisneref356262011-04-13 14:21:29 -07003205 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003206out:
3207 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003208 return 0;
3209}
3210
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003211int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05003212{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003213 enum drbd_ret_code retcode;
3214
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02003215 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003216 if (!adm_ctx.reply_skb)
3217 return retcode;
3218 if (retcode != NO_ERROR)
3219 goto out;
3220
3221 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
Philipp Reisnerec0bddb2011-05-04 15:47:01 +02003222 list_del_rcu(&adm_ctx.tconn->all_tconn);
3223 synchronize_rcu();
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02003224 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
3225
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003226 retcode = NO_ERROR;
Philipp Reisner774b3052011-02-22 02:07:03 -05003227 } else {
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003228 retcode = ERR_RES_IN_USE;
Philipp Reisner774b3052011-02-22 02:07:03 -05003229 }
3230
Lars Ellenberg992d6e92011-05-02 11:47:18 +02003231 if (retcode == NO_ERROR)
3232 drbd_thread_stop(&adm_ctx.tconn->worker);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003233out:
3234 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003235 return 0;
3236}
3237
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003238void drbd_bcast_event(struct drbd_conf *mdev, const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003239{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003240 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
3241 struct sk_buff *msg;
3242 struct drbd_genlmsghdr *d_out;
3243 unsigned seq;
3244 int err = -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003245
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003246 seq = atomic_inc_return(&drbd_genl_seq);
3247 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
3248 if (!msg)
3249 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003250
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003251 err = -EMSGSIZE;
3252 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
3253 if (!d_out) /* cannot happen, but anyways. */
3254 goto nla_put_failure;
3255 d_out->minor = mdev_to_minor(mdev);
Andreas Gruenbacher6f9b5f82011-05-06 01:03:32 +02003256 d_out->ret_code = NO_ERROR;
Philipp Reisner9f5180e2009-10-06 09:30:14 +02003257
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003258 if (nla_put_status_info(msg, mdev, sib))
3259 goto nla_put_failure;
3260 genlmsg_end(msg, d_out);
3261 err = drbd_genl_multicast_events(msg, 0);
3262 /* msg has been consumed or freed in netlink_broadcast() */
3263 if (err && err != -ESRCH)
3264 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003265
Philipp Reisnerb411b362009-09-25 16:07:19 -07003266 return;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003267
3268nla_put_failure:
3269 nlmsg_free(msg);
3270failed:
3271 dev_err(DEV, "Error %d while broadcasting event. "
3272 "Event seq:%u sib_reason:%u\n",
3273 err, seq, sib->sib_reason);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003274}