blob: 39e9a91a8f310d4655f1c6f2e467a10e1140ac26 [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" :( */
Philipp Reisner98683652012-11-09 14:18:43 +0100158 if (cmd != DRBD_ADM_GET_STATUS && !capable(CAP_NET_ADMIN))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100159 return -EPERM;
160
161 adm_ctx.reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200162 if (!adm_ctx.reply_skb) {
163 err = -ENOMEM;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100164 goto fail;
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200165 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100166
167 adm_ctx.reply_dh = genlmsg_put_reply(adm_ctx.reply_skb,
168 info, &drbd_genl_family, 0, cmd);
169 /* put of a few bytes into a fresh skb of >= 4k will always succeed.
170 * but anyways */
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200171 if (!adm_ctx.reply_dh) {
172 err = -ENOMEM;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100173 goto fail;
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200174 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100175
176 adm_ctx.reply_dh->minor = d_in->minor;
177 adm_ctx.reply_dh->ret_code = NO_ERROR;
178
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200179 adm_ctx.volume = VOLUME_UNSPECIFIED;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100180 if (info->attrs[DRBD_NLA_CFG_CONTEXT]) {
181 struct nlattr *nla;
182 /* parse and validate only */
Lars Ellenbergf3990022011-03-23 14:31:09 +0100183 err = drbd_cfg_context_from_attrs(NULL, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100184 if (err)
185 goto fail;
186
187 /* It was present, and valid,
188 * copy it over to the reply skb. */
189 err = nla_put_nohdr(adm_ctx.reply_skb,
190 info->attrs[DRBD_NLA_CFG_CONTEXT]->nla_len,
191 info->attrs[DRBD_NLA_CFG_CONTEXT]);
192 if (err)
193 goto fail;
194
195 /* and assign stuff to the global adm_ctx */
196 nla = nested_attr_tb[__nla_type(T_ctx_volume)];
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200197 if (nla)
198 adm_ctx.volume = nla_get_u32(nla);
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +0200199 nla = nested_attr_tb[__nla_type(T_ctx_resource_name)];
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100200 if (nla)
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +0200201 adm_ctx.resource_name = nla_data(nla);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200202 adm_ctx.my_addr = nested_attr_tb[__nla_type(T_ctx_my_addr)];
203 adm_ctx.peer_addr = nested_attr_tb[__nla_type(T_ctx_peer_addr)];
204 if ((adm_ctx.my_addr &&
205 nla_len(adm_ctx.my_addr) > sizeof(adm_ctx.tconn->my_addr)) ||
206 (adm_ctx.peer_addr &&
207 nla_len(adm_ctx.peer_addr) > sizeof(adm_ctx.tconn->peer_addr))) {
208 err = -EINVAL;
209 goto fail;
210 }
211 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100212
213 adm_ctx.minor = d_in->minor;
214 adm_ctx.mdev = minor_to_mdev(d_in->minor);
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +0200215 adm_ctx.tconn = conn_get_by_name(adm_ctx.resource_name);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100216
217 if (!adm_ctx.mdev && (flags & DRBD_ADM_NEED_MINOR)) {
218 drbd_msg_put_info("unknown minor");
219 return ERR_MINOR_INVALID;
220 }
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +0200221 if (!adm_ctx.tconn && (flags & DRBD_ADM_NEED_RESOURCE)) {
222 drbd_msg_put_info("unknown resource");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100223 return ERR_INVALID_REQUEST;
224 }
225
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200226 if (flags & DRBD_ADM_NEED_CONNECTION) {
227 if (adm_ctx.tconn && !(flags & DRBD_ADM_NEED_RESOURCE)) {
228 drbd_msg_put_info("no resource name expected");
229 return ERR_INVALID_REQUEST;
230 }
231 if (adm_ctx.mdev) {
232 drbd_msg_put_info("no minor number expected");
233 return ERR_INVALID_REQUEST;
234 }
235 if (adm_ctx.my_addr && adm_ctx.peer_addr)
236 adm_ctx.tconn = conn_get_by_addrs(nla_data(adm_ctx.my_addr),
237 nla_len(adm_ctx.my_addr),
238 nla_data(adm_ctx.peer_addr),
239 nla_len(adm_ctx.peer_addr));
240 if (!adm_ctx.tconn) {
241 drbd_msg_put_info("unknown connection");
242 return ERR_INVALID_REQUEST;
243 }
244 }
245
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100246 /* some more paranoia, if the request was over-determined */
Lars Ellenberg527f4b22011-03-14 13:58:03 +0100247 if (adm_ctx.mdev && adm_ctx.tconn &&
248 adm_ctx.mdev->tconn != adm_ctx.tconn) {
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +0200249 pr_warning("request: minor=%u, resource=%s; but that minor belongs to connection %s\n",
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +0200250 adm_ctx.minor, adm_ctx.resource_name,
251 adm_ctx.mdev->tconn->name);
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +0200252 drbd_msg_put_info("minor exists in different resource");
Lars Ellenberg527f4b22011-03-14 13:58:03 +0100253 return ERR_INVALID_REQUEST;
254 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100255 if (adm_ctx.mdev &&
256 adm_ctx.volume != VOLUME_UNSPECIFIED &&
257 adm_ctx.volume != adm_ctx.mdev->vnr) {
258 pr_warning("request: minor=%u, volume=%u; but that minor is volume %u in %s\n",
259 adm_ctx.minor, adm_ctx.volume,
260 adm_ctx.mdev->vnr, adm_ctx.mdev->tconn->name);
Lars Ellenberg527f4b22011-03-14 13:58:03 +0100261 drbd_msg_put_info("minor exists as different volume");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100262 return ERR_INVALID_REQUEST;
263 }
Philipp Reisner0ace9df2011-04-24 10:53:19 +0200264
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100265 return NO_ERROR;
266
267fail:
268 nlmsg_free(adm_ctx.reply_skb);
269 adm_ctx.reply_skb = NULL;
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200270 return err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100271}
272
273static int drbd_adm_finish(struct genl_info *info, int retcode)
274{
Philipp Reisner0ace9df2011-04-24 10:53:19 +0200275 if (adm_ctx.tconn) {
276 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
277 adm_ctx.tconn = NULL;
278 }
279
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100280 if (!adm_ctx.reply_skb)
281 return -ENOMEM;
282
283 adm_ctx.reply_dh->ret_code = retcode;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100284 drbd_adm_send_reply(adm_ctx.reply_skb, info);
285 return 0;
286}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700287
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100288static void setup_khelper_env(struct drbd_tconn *tconn, char **envp)
289{
290 char *afs;
291
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200292 /* FIXME: A future version will not allow this case. */
293 if (tconn->my_addr_len == 0 || tconn->peer_addr_len == 0)
294 return;
295
296 switch (((struct sockaddr *)&tconn->peer_addr)->sa_family) {
297 case AF_INET6:
298 afs = "ipv6";
299 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
300 &((struct sockaddr_in6 *)&tconn->peer_addr)->sin6_addr);
301 break;
302 case AF_INET:
303 afs = "ipv4";
304 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
305 &((struct sockaddr_in *)&tconn->peer_addr)->sin_addr);
306 break;
307 default:
308 afs = "ssocks";
309 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
310 &((struct sockaddr_in *)&tconn->peer_addr)->sin_addr);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100311 }
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200312 snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100313}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700314
315int drbd_khelper(struct drbd_conf *mdev, char *cmd)
316{
317 char *envp[] = { "HOME=/",
318 "TERM=linux",
319 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100320 (char[20]) { }, /* address family */
321 (char[60]) { }, /* address */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700322 NULL };
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100323 char mb[12];
Philipp Reisnerb411b362009-09-25 16:07:19 -0700324 char *argv[] = {usermode_helper, cmd, mb, NULL };
Lars Ellenberg6f3465e2012-07-30 09:08:25 +0200325 struct drbd_tconn *tconn = mdev->tconn;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100326 struct sib_info sib;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700327 int ret;
328
Lars Ellenberg6f3465e2012-07-30 09:08:25 +0200329 if (current == tconn->worker.task)
330 set_bit(CALLBACK_PENDING, &tconn->flags);
Lars Ellenbergc2ba6862012-06-14 15:14:06 +0200331
Philipp Reisnerb411b362009-09-25 16:07:19 -0700332 snprintf(mb, 12, "minor-%d", mdev_to_minor(mdev));
Lars Ellenberg6f3465e2012-07-30 09:08:25 +0200333 setup_khelper_env(tconn, envp);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700334
Lars Ellenberg1090c052010-07-19 17:41:04 +0200335 /* The helper may take some time.
336 * write out any unsynced meta data changes now */
337 drbd_md_sync(mdev);
338
Philipp Reisnerb411b362009-09-25 16:07:19 -0700339 dev_info(DEV, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100340 sib.sib_reason = SIB_HELPER_PRE;
341 sib.helper_name = cmd;
342 drbd_bcast_event(mdev, &sib);
Oleg Nesterov70834d32012-03-23 15:02:46 -0700343 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700344 if (ret)
345 dev_warn(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
346 usermode_helper, cmd, mb,
347 (ret >> 8) & 0xff, ret);
348 else
349 dev_info(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
350 usermode_helper, cmd, mb,
351 (ret >> 8) & 0xff, ret);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100352 sib.sib_reason = SIB_HELPER_POST;
353 sib.helper_exit_code = ret;
354 drbd_bcast_event(mdev, &sib);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700355
Lars Ellenberg6f3465e2012-07-30 09:08:25 +0200356 if (current == tconn->worker.task)
357 clear_bit(CALLBACK_PENDING, &tconn->flags);
Lars Ellenbergc2ba6862012-06-14 15:14:06 +0200358
Philipp Reisnerb411b362009-09-25 16:07:19 -0700359 if (ret < 0) /* Ignore any ERRNOs we got. */
360 ret = 0;
361
362 return ret;
363}
364
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100365int conn_khelper(struct drbd_tconn *tconn, char *cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700366{
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100367 char *envp[] = { "HOME=/",
368 "TERM=linux",
369 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
370 (char[20]) { }, /* address family */
371 (char[60]) { }, /* address */
372 NULL };
373 char *argv[] = {usermode_helper, cmd, tconn->name, NULL };
374 int ret;
375
376 setup_khelper_env(tconn, envp);
377 conn_md_sync(tconn);
378
379 conn_info(tconn, "helper command: %s %s %s\n", usermode_helper, cmd, tconn->name);
380 /* TODO: conn_bcast_event() ?? */
381
Philipp Reisner98683652012-11-09 14:18:43 +0100382 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100383 if (ret)
384 conn_warn(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
385 usermode_helper, cmd, tconn->name,
386 (ret >> 8) & 0xff, ret);
387 else
388 conn_info(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
389 usermode_helper, cmd, tconn->name,
390 (ret >> 8) & 0xff, ret);
391 /* TODO: conn_bcast_event() ?? */
392
393 if (ret < 0) /* Ignore any ERRNOs we got. */
394 ret = 0;
395
396 return ret;
397}
398
Philipp Reisnercb703452011-03-24 11:03:07 +0100399static enum drbd_fencing_p highest_fencing_policy(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700400{
Philipp Reisnercb703452011-03-24 11:03:07 +0100401 enum drbd_fencing_p fp = FP_NOT_AVAIL;
402 struct drbd_conf *mdev;
403 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700404
Philipp Reisner695d08f2011-04-11 22:53:32 -0700405 rcu_read_lock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100406 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
407 if (get_ldev_if_state(mdev, D_CONSISTENT)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200408 fp = max_t(enum drbd_fencing_p, fp,
409 rcu_dereference(mdev->ldev->disk_conf)->fencing);
Philipp Reisnercb703452011-03-24 11:03:07 +0100410 put_ldev(mdev);
411 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700412 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700413 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700414
Philipp Reisnercb703452011-03-24 11:03:07 +0100415 return fp;
416}
417
418bool conn_try_outdate_peer(struct drbd_tconn *tconn)
419{
420 union drbd_state mask = { };
421 union drbd_state val = { };
422 enum drbd_fencing_p fp;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700423 char *ex_to_string;
424 int r;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700425
Philipp Reisnercb703452011-03-24 11:03:07 +0100426 if (tconn->cstate >= C_WF_REPORT_PARAMS) {
427 conn_err(tconn, "Expected cstate < C_WF_REPORT_PARAMS\n");
428 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700429 }
430
Philipp Reisnercb703452011-03-24 11:03:07 +0100431 fp = highest_fencing_policy(tconn);
432 switch (fp) {
433 case FP_NOT_AVAIL:
434 conn_warn(tconn, "Not fencing peer, I'm not even Consistent myself.\n");
435 goto out;
436 case FP_DONT_CARE:
437 return true;
438 default: ;
439 }
440
441 r = conn_khelper(tconn, "fence-peer");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700442
443 switch ((r>>8) & 0xff) {
444 case 3: /* peer is inconsistent */
445 ex_to_string = "peer is inconsistent or worse";
Philipp Reisnercb703452011-03-24 11:03:07 +0100446 mask.pdsk = D_MASK;
447 val.pdsk = D_INCONSISTENT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700448 break;
449 case 4: /* peer got outdated, or was already outdated */
450 ex_to_string = "peer was fenced";
Philipp Reisnercb703452011-03-24 11:03:07 +0100451 mask.pdsk = D_MASK;
452 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700453 break;
454 case 5: /* peer was down */
Philipp Reisnercb703452011-03-24 11:03:07 +0100455 if (conn_highest_disk(tconn) == D_UP_TO_DATE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700456 /* we will(have) create(d) a new UUID anyways... */
457 ex_to_string = "peer is unreachable, assumed to be dead";
Philipp Reisnercb703452011-03-24 11:03:07 +0100458 mask.pdsk = D_MASK;
459 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700460 } else {
461 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
Philipp Reisnerb411b362009-09-25 16:07:19 -0700462 }
463 break;
464 case 6: /* Peer is primary, voluntarily outdate myself.
465 * This is useful when an unconnected R_SECONDARY is asked to
466 * become R_PRIMARY, but finds the other peer being active. */
467 ex_to_string = "peer is active";
Philipp Reisnercb703452011-03-24 11:03:07 +0100468 conn_warn(tconn, "Peer is primary, outdating myself.\n");
469 mask.disk = D_MASK;
470 val.disk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700471 break;
472 case 7:
473 if (fp != FP_STONITH)
Philipp Reisnercb703452011-03-24 11:03:07 +0100474 conn_err(tconn, "fence-peer() = 7 && fencing != Stonith !!!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700475 ex_to_string = "peer was stonithed";
Philipp Reisnercb703452011-03-24 11:03:07 +0100476 mask.pdsk = D_MASK;
477 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700478 break;
479 default:
480 /* The script is broken ... */
Philipp Reisnercb703452011-03-24 11:03:07 +0100481 conn_err(tconn, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
482 return false; /* Eventually leave IO frozen */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700483 }
484
Philipp Reisnercb703452011-03-24 11:03:07 +0100485 conn_info(tconn, "fence-peer helper returned %d (%s)\n",
486 (r>>8) & 0xff, ex_to_string);
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200487
Philipp Reisnercb703452011-03-24 11:03:07 +0100488 out:
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200489
Philipp Reisnercb703452011-03-24 11:03:07 +0100490 /* Not using
491 conn_request_state(tconn, mask, val, CS_VERBOSE);
492 here, because we might were able to re-establish the connection in the
493 meantime. */
494 spin_lock_irq(&tconn->req_lock);
Philipp Reisnera1096a62012-04-06 12:07:34 +0200495 if (tconn->cstate < C_WF_REPORT_PARAMS && !test_bit(STATE_SENT, &tconn->flags))
Philipp Reisnercb703452011-03-24 11:03:07 +0100496 _conn_request_state(tconn, mask, val, CS_VERBOSE);
497 spin_unlock_irq(&tconn->req_lock);
498
499 return conn_highest_pdsk(tconn) <= D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700500}
501
Philipp Reisner87f7be42010-06-11 13:56:33 +0200502static int _try_outdate_peer_async(void *data)
503{
Philipp Reisnercb703452011-03-24 11:03:07 +0100504 struct drbd_tconn *tconn = (struct drbd_tconn *)data;
Philipp Reisner87f7be42010-06-11 13:56:33 +0200505
Philipp Reisnercb703452011-03-24 11:03:07 +0100506 conn_try_outdate_peer(tconn);
Philipp Reisner21423fa2011-05-17 14:19:41 +0200507
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200508 kref_put(&tconn->kref, &conn_destroy);
Philipp Reisner87f7be42010-06-11 13:56:33 +0200509 return 0;
510}
511
Philipp Reisnercb703452011-03-24 11:03:07 +0100512void conn_try_outdate_peer_async(struct drbd_tconn *tconn)
Philipp Reisner87f7be42010-06-11 13:56:33 +0200513{
514 struct task_struct *opa;
515
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200516 kref_get(&tconn->kref);
Philipp Reisnercb703452011-03-24 11:03:07 +0100517 opa = kthread_run(_try_outdate_peer_async, tconn, "drbd_async_h");
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200518 if (IS_ERR(opa)) {
Philipp Reisnercb703452011-03-24 11:03:07 +0100519 conn_err(tconn, "out of mem, failed to invoke fence-peer helper\n");
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200520 kref_put(&tconn->kref, &conn_destroy);
521 }
Philipp Reisner87f7be42010-06-11 13:56:33 +0200522}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700523
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100524enum drbd_state_rv
525drbd_set_role(struct drbd_conf *mdev, enum drbd_role new_role, int force)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700526{
527 const int max_tries = 4;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100528 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200529 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700530 int try = 0;
531 int forced = 0;
532 union drbd_state mask, val;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700533
534 if (new_role == R_PRIMARY)
Philipp Reisner0625ac12011-02-07 14:49:19 +0100535 request_ping(mdev->tconn); /* Detect a dead peer ASAP */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700536
Philipp Reisner8410da82011-02-11 20:11:10 +0100537 mutex_lock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700538
539 mask.i = 0; mask.role = R_MASK;
540 val.i = 0; val.role = new_role;
541
542 while (try++ < max_tries) {
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100543 rv = _drbd_request_state(mdev, mask, val, CS_WAIT_COMPLETE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700544
545 /* in case we first succeeded to outdate,
546 * but now suddenly could establish a connection */
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100547 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700548 val.pdsk = 0;
549 mask.pdsk = 0;
550 continue;
551 }
552
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100553 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
Philipp Reisnerd10a33c2010-03-04 15:11:39 +0100554 (mdev->state.disk < D_UP_TO_DATE &&
555 mdev->state.disk >= D_INCONSISTENT)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700556 mask.disk = D_MASK;
557 val.disk = D_UP_TO_DATE;
558 forced = 1;
559 continue;
560 }
561
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100562 if (rv == SS_NO_UP_TO_DATE_DISK &&
Philipp Reisnerb411b362009-09-25 16:07:19 -0700563 mdev->state.disk == D_CONSISTENT && mask.pdsk == 0) {
564 D_ASSERT(mdev->state.pdsk == D_UNKNOWN);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700565
Philipp Reisnercb703452011-03-24 11:03:07 +0100566 if (conn_try_outdate_peer(mdev->tconn)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700567 val.disk = D_UP_TO_DATE;
568 mask.disk = D_MASK;
569 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700570 continue;
571 }
572
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100573 if (rv == SS_NOTHING_TO_DO)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100574 goto out;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100575 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
Philipp Reisnercb703452011-03-24 11:03:07 +0100576 if (!conn_try_outdate_peer(mdev->tconn) && force) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700577 dev_warn(DEV, "Forced into split brain situation!\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100578 mask.pdsk = D_MASK;
579 val.pdsk = D_OUTDATED;
580
Philipp Reisnerb411b362009-09-25 16:07:19 -0700581 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700582 continue;
583 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100584 if (rv == SS_TWO_PRIMARIES) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700585 /* Maybe the peer is detected as dead very soon...
586 retry at most once more in this case. */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200587 int timeo;
588 rcu_read_lock();
589 nc = rcu_dereference(mdev->tconn->net_conf);
590 timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
591 rcu_read_unlock();
592 schedule_timeout_interruptible(timeo);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700593 if (try < max_tries)
594 try = max_tries - 1;
595 continue;
596 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100597 if (rv < SS_SUCCESS) {
598 rv = _drbd_request_state(mdev, mask, val,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700599 CS_VERBOSE + CS_WAIT_COMPLETE);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100600 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100601 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700602 }
603 break;
604 }
605
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100606 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100607 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700608
609 if (forced)
610 dev_warn(DEV, "Forced to consider local data as UpToDate!\n");
611
612 /* Wait until nothing is on the fly :) */
613 wait_event(mdev->misc_wait, atomic_read(&mdev->ap_pending_cnt) == 0);
614
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100615 /* FIXME also wait for all pending P_BARRIER_ACK? */
616
Philipp Reisnerb411b362009-09-25 16:07:19 -0700617 if (new_role == R_SECONDARY) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100618 set_disk_ro(mdev->vdisk, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700619 if (get_ldev(mdev)) {
620 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
621 put_ldev(mdev);
622 }
623 } else {
Philipp Reisnera0095502011-05-03 13:14:15 +0200624 mutex_lock(&mdev->tconn->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +0200625 nc = mdev->tconn->net_conf;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200626 if (nc)
Andreas Gruenbacher6139f602011-05-06 20:00:02 +0200627 nc->discard_my_data = 0; /* without copy; single bit op is atomic */
Philipp Reisnera0095502011-05-03 13:14:15 +0200628 mutex_unlock(&mdev->tconn->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +0200629
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100630 set_disk_ro(mdev->vdisk, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700631 if (get_ldev(mdev)) {
632 if (((mdev->state.conn < C_CONNECTED ||
633 mdev->state.pdsk <= D_FAILED)
634 && mdev->ldev->md.uuid[UI_BITMAP] == 0) || forced)
635 drbd_uuid_new_current(mdev);
636
637 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
638 put_ldev(mdev);
639 }
640 }
641
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100642 /* writeout of activity log covered areas of the bitmap
643 * to stable storage done in after state change already */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700644
645 if (mdev->state.conn >= C_WF_REPORT_PARAMS) {
646 /* if this was forced, we should consider sync */
647 if (forced)
648 drbd_send_uuids(mdev);
Lars Ellenbergf479ea02011-10-27 16:52:30 +0200649 drbd_send_current_state(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700650 }
651
652 drbd_md_sync(mdev);
653
654 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100655out:
Philipp Reisner8410da82011-02-11 20:11:10 +0100656 mutex_unlock(mdev->state_mutex);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100657 return rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700658}
659
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100660static const char *from_attrs_err_to_txt(int err)
Lars Ellenbergef50a3e2010-09-01 14:39:30 +0200661{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100662 return err == -ENOMSG ? "required attribute missing" :
663 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
Lars Ellenbergf3990022011-03-23 14:31:09 +0100664 err == -EEXIST ? "can not change invariant setting" :
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100665 "invalid attribute value";
Lars Ellenbergef50a3e2010-09-01 14:39:30 +0200666}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700667
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100668int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700669{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100670 struct set_role_parms parms;
671 int err;
672 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700673
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100674 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
675 if (!adm_ctx.reply_skb)
676 return retcode;
677 if (retcode != NO_ERROR)
678 goto out;
679
680 memset(&parms, 0, sizeof(parms));
681 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +0100682 err = set_role_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100683 if (err) {
684 retcode = ERR_MANDATORY_TAG;
685 drbd_msg_put_info(from_attrs_err_to_txt(err));
686 goto out;
687 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700688 }
689
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100690 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
691 retcode = drbd_set_role(adm_ctx.mdev, R_PRIMARY, parms.assume_uptodate);
692 else
693 retcode = drbd_set_role(adm_ctx.mdev, R_SECONDARY, 0);
694out:
695 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700696 return 0;
697}
698
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100699/* Initializes the md.*_offset members, so we are able to find
700 * the on disk meta data.
701 *
702 * We currently have two possible layouts:
703 * external:
704 * |----------- md_size_sect ------------------|
705 * [ 4k superblock ][ activity log ][ Bitmap ]
706 * | al_offset == 8 |
707 * | bm_offset = al_offset + X |
708 * ==> bitmap sectors = md_size_sect - bm_offset
709 *
710 * internal:
711 * |----------- md_size_sect ------------------|
712 * [data.....][ Bitmap ][ activity log ][ 4k superblock ]
713 * | al_offset < 0 |
714 * | bm_offset = al_offset - Y |
715 * ==> bitmap sectors = Y = al_offset - bm_offset
716 *
717 * Activity log size used to be fixed 32kB,
718 * but is about to become configurable.
719 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700720static void drbd_md_set_sector_offsets(struct drbd_conf *mdev,
721 struct drbd_backing_dev *bdev)
722{
723 sector_t md_size_sect = 0;
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +0100724 unsigned int al_size_sect = bdev->md.al_size_4k * 8;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200725
Lars Ellenberg3a4d4eb2013-03-19 18:16:44 +0100726 bdev->md.md_offset = drbd_md_ss(bdev);
727
Lars Ellenberg68e41a42013-03-19 18:16:45 +0100728 switch (bdev->md.meta_dev_idx) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700729 default:
730 /* v07 style fixed size indexed meta data */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100731 bdev->md.md_size_sect = MD_128MB_SECT;
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100732 bdev->md.al_offset = MD_4kB_SECT;
733 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700734 break;
735 case DRBD_MD_INDEX_FLEX_EXT:
736 /* just occupy the full device; unit: sectors */
737 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100738 bdev->md.al_offset = MD_4kB_SECT;
739 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700740 break;
741 case DRBD_MD_INDEX_INTERNAL:
742 case DRBD_MD_INDEX_FLEX_INT:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700743 /* al size is still fixed */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100744 bdev->md.al_offset = -al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700745 /* we need (slightly less than) ~ this much bitmap sectors: */
746 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
747 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
748 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
749 md_size_sect = ALIGN(md_size_sect, 8);
750
751 /* plus the "drbd meta data super block",
752 * and the activity log; */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100753 md_size_sect += MD_4kB_SECT + al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700754
755 bdev->md.md_size_sect = md_size_sect;
756 /* bitmap offset is adjusted by 'super' block size */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100757 bdev->md.bm_offset = -md_size_sect + MD_4kB_SECT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700758 break;
759 }
760}
761
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100762/* input size is expected to be in KB */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700763char *ppsize(char *buf, unsigned long long size)
764{
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100765 /* Needs 9 bytes at max including trailing NUL:
766 * -1ULL ==> "16384 EB" */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700767 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
768 int base = 0;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100769 while (size >= 10000 && base < sizeof(units)-1) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700770 /* shift + round */
771 size = (size >> 10) + !!(size & (1<<9));
772 base++;
773 }
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100774 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700775
776 return buf;
777}
778
779/* there is still a theoretical deadlock when called from receiver
780 * on an D_INCONSISTENT R_PRIMARY:
781 * remote READ does inc_ap_bio, receiver would need to receive answer
782 * packet from remote to dec_ap_bio again.
783 * receiver receive_sizes(), comes here,
784 * waits for ap_bio_cnt == 0. -> deadlock.
785 * but this cannot happen, actually, because:
786 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
787 * (not connected, or bad/no disk on peer):
788 * see drbd_fail_request_early, ap_bio_cnt is zero.
789 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
790 * peer may not initiate a resize.
791 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100792/* Note these are not to be confused with
793 * drbd_adm_suspend_io/drbd_adm_resume_io,
794 * which are (sub) state changes triggered by admin (drbdsetup),
795 * and can be long lived.
796 * This changes an mdev->flag, is triggered by drbd internals,
797 * and should be short-lived. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700798void drbd_suspend_io(struct drbd_conf *mdev)
799{
800 set_bit(SUSPEND_IO, &mdev->flags);
Philipp Reisner2aebfab2011-03-28 16:48:11 +0200801 if (drbd_suspended(mdev))
Philipp Reisner265be2d2010-05-31 10:14:17 +0200802 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700803 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
804}
805
806void drbd_resume_io(struct drbd_conf *mdev)
807{
808 clear_bit(SUSPEND_IO, &mdev->flags);
809 wake_up(&mdev->misc_wait);
810}
811
812/**
813 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
814 * @mdev: DRBD device.
815 *
816 * Returns 0 on success, negative return values indicate errors.
817 * You should call drbd_md_sync() after calling this function.
818 */
Bart Van Assche24c48302011-05-21 18:32:29 +0200819enum 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 -0700820{
821 sector_t prev_first_sect, prev_size; /* previous meta location */
Lars Ellenbergcccac982013-03-19 18:16:46 +0100822 sector_t la_size_sect, u_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700823 sector_t size;
824 char ppb[10];
825
826 int md_moved, la_size_changed;
827 enum determine_dev_size rv = unchanged;
828
829 /* race:
830 * application request passes inc_ap_bio,
831 * but then cannot get an AL-reference.
832 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
833 *
834 * to avoid that:
835 * Suspend IO right here.
836 * still lock the act_log to not trigger ASSERTs there.
837 */
838 drbd_suspend_io(mdev);
839
840 /* no wait necessary anymore, actually we could assert that */
841 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
842
843 prev_first_sect = drbd_md_first_sector(mdev->ldev);
844 prev_size = mdev->ldev->md.md_size_sect;
Lars Ellenbergcccac982013-03-19 18:16:46 +0100845 la_size_sect = mdev->ldev->md.la_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700846
847 /* TODO: should only be some assert here, not (re)init... */
848 drbd_md_set_sector_offsets(mdev, mdev->ldev);
849
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200850 rcu_read_lock();
851 u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
852 rcu_read_unlock();
Philipp Reisneref5e44a2011-05-03 13:27:43 +0200853 size = drbd_new_dev_size(mdev, mdev->ldev, u_size, flags & DDSF_FORCED);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700854
855 if (drbd_get_capacity(mdev->this_bdev) != size ||
856 drbd_bm_capacity(mdev) != size) {
857 int err;
Philipp Reisner02d9a942010-03-24 16:23:03 +0100858 err = drbd_bm_resize(mdev, size, !(flags & DDSF_NO_RESYNC));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700859 if (unlikely(err)) {
860 /* currently there is only one error: ENOMEM! */
861 size = drbd_bm_capacity(mdev)>>1;
862 if (size == 0) {
863 dev_err(DEV, "OUT OF MEMORY! "
864 "Could not allocate bitmap!\n");
865 } else {
866 dev_err(DEV, "BM resizing failed. "
867 "Leaving size unchanged at size = %lu KB\n",
868 (unsigned long)size);
869 }
870 rv = dev_size_error;
871 }
872 /* racy, see comments above. */
873 drbd_set_my_capacity(mdev, size);
874 mdev->ldev->md.la_size_sect = size;
875 dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
876 (unsigned long long)size>>1);
877 }
878 if (rv == dev_size_error)
879 goto out;
880
Lars Ellenbergcccac982013-03-19 18:16:46 +0100881 la_size_changed = (la_size_sect != mdev->ldev->md.la_size_sect);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700882
883 md_moved = prev_first_sect != drbd_md_first_sector(mdev->ldev)
884 || prev_size != mdev->ldev->md.md_size_sect;
885
886 if (la_size_changed || md_moved) {
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100887 int err;
888
Philipp Reisnerb411b362009-09-25 16:07:19 -0700889 drbd_al_shrink(mdev); /* All extents inactive. */
890 dev_info(DEV, "Writing the whole bitmap, %s\n",
891 la_size_changed && md_moved ? "size changed and md moved" :
892 la_size_changed ? "size changed" : "md moved");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100893 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
Philipp Reisnerd1aa4d02012-08-08 21:19:09 +0200894 err = drbd_bitmap_io(mdev, md_moved ? &drbd_bm_write_all : &drbd_bm_write,
895 "size changed", BM_LOCKED_MASK);
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100896 if (err) {
897 rv = dev_size_error;
898 goto out;
899 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700900 drbd_md_mark_dirty(mdev);
901 }
902
Lars Ellenbergcccac982013-03-19 18:16:46 +0100903 if (size > la_size_sect)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700904 rv = grew;
Lars Ellenbergcccac982013-03-19 18:16:46 +0100905 if (size < la_size_sect)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700906 rv = shrunk;
907out:
908 lc_unlock(mdev->act_log);
909 wake_up(&mdev->al_wait);
910 drbd_resume_io(mdev);
911
912 return rv;
913}
914
915sector_t
Philipp Reisneref5e44a2011-05-03 13:27:43 +0200916drbd_new_dev_size(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
917 sector_t u_size, int assume_peer_has_space)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700918{
919 sector_t p_size = mdev->p_size; /* partner's disk size. */
Lars Ellenbergcccac982013-03-19 18:16:46 +0100920 sector_t la_size_sect = bdev->md.la_size_sect; /* last agreed size. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700921 sector_t m_size; /* my size */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700922 sector_t size = 0;
923
924 m_size = drbd_get_max_capacity(bdev);
925
Philipp Reisnera393db62009-12-22 13:35:52 +0100926 if (mdev->state.conn < C_CONNECTED && assume_peer_has_space) {
927 dev_warn(DEV, "Resize while not connected was forced by the user!\n");
928 p_size = m_size;
929 }
930
Philipp Reisnerb411b362009-09-25 16:07:19 -0700931 if (p_size && m_size) {
932 size = min_t(sector_t, p_size, m_size);
933 } else {
Lars Ellenbergcccac982013-03-19 18:16:46 +0100934 if (la_size_sect) {
935 size = la_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700936 if (m_size && m_size < size)
937 size = m_size;
938 if (p_size && p_size < size)
939 size = p_size;
940 } else {
941 if (m_size)
942 size = m_size;
943 if (p_size)
944 size = p_size;
945 }
946 }
947
948 if (size == 0)
949 dev_err(DEV, "Both nodes diskless!\n");
950
951 if (u_size) {
952 if (u_size > size)
953 dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n",
954 (unsigned long)u_size>>1, (unsigned long)size>>1);
955 else
956 size = u_size;
957 }
958
959 return size;
960}
961
962/**
963 * drbd_check_al_size() - Ensures that the AL is of the right size
964 * @mdev: DRBD device.
965 *
966 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
967 * failed, and 0 on success. You should call drbd_md_sync() after you called
968 * this function.
969 */
Lars Ellenbergf3990022011-03-23 14:31:09 +0100970static int drbd_check_al_size(struct drbd_conf *mdev, struct disk_conf *dc)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700971{
972 struct lru_cache *n, *t;
973 struct lc_element *e;
974 unsigned int in_use;
975 int i;
976
Philipp Reisnerb411b362009-09-25 16:07:19 -0700977 if (mdev->act_log &&
Lars Ellenbergf3990022011-03-23 14:31:09 +0100978 mdev->act_log->nr_elements == dc->al_extents)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700979 return 0;
980
981 in_use = 0;
982 t = mdev->act_log;
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100983 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
Lars Ellenbergf3990022011-03-23 14:31:09 +0100984 dc->al_extents, sizeof(struct lc_element), 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700985
986 if (n == NULL) {
987 dev_err(DEV, "Cannot allocate act_log lru!\n");
988 return -ENOMEM;
989 }
990 spin_lock_irq(&mdev->al_lock);
991 if (t) {
992 for (i = 0; i < t->nr_elements; i++) {
993 e = lc_element_by_index(t, i);
994 if (e->refcnt)
995 dev_err(DEV, "refcnt(%d)==%d\n",
996 e->lc_number, e->refcnt);
997 in_use += e->refcnt;
998 }
999 }
1000 if (!in_use)
1001 mdev->act_log = n;
1002 spin_unlock_irq(&mdev->al_lock);
1003 if (in_use) {
1004 dev_err(DEV, "Activity log still in use!\n");
1005 lc_destroy(n);
1006 return -EBUSY;
1007 } else {
1008 if (t)
1009 lc_destroy(t);
1010 }
1011 drbd_md_mark_dirty(mdev); /* we changed mdev->act_log->nr_elemens */
1012 return 0;
1013}
1014
Philipp Reisner99432fc2011-05-20 16:39:13 +02001015static void drbd_setup_queue_param(struct drbd_conf *mdev, unsigned int max_bio_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001016{
1017 struct request_queue * const q = mdev->rq_queue;
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001018 unsigned int max_hw_sectors = max_bio_size >> 9;
1019 unsigned int max_segments = 0;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001020
1021 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1022 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
1023
1024 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001025 rcu_read_lock();
1026 max_segments = rcu_dereference(mdev->ldev->disk_conf)->max_bio_bvecs;
1027 rcu_read_unlock();
Philipp Reisner99432fc2011-05-20 16:39:13 +02001028 put_ldev(mdev);
1029 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001030
Philipp Reisnerb411b362009-09-25 16:07:19 -07001031 blk_queue_logical_block_size(q, 512);
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001032 blk_queue_max_hw_sectors(q, max_hw_sectors);
1033 /* This is the workaround for "bio would need to, but cannot, be split" */
1034 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
1035 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001036
Philipp Reisner99432fc2011-05-20 16:39:13 +02001037 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1038 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001039
Philipp Reisner99432fc2011-05-20 16:39:13 +02001040 blk_queue_stack_limits(q, b);
1041
1042 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
1043 dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
1044 q->backing_dev_info.ra_pages,
1045 b->backing_dev_info.ra_pages);
1046 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1047 }
1048 put_ldev(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001049 }
1050}
1051
Philipp Reisner99432fc2011-05-20 16:39:13 +02001052void drbd_reconsider_max_bio_size(struct drbd_conf *mdev)
1053{
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001054 unsigned int now, new, local, peer;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001055
1056 now = queue_max_hw_sectors(mdev->rq_queue) << 9;
1057 local = mdev->local_max_bio_size; /* Eventually last known value, from volatile memory */
1058 peer = mdev->peer_max_bio_size; /* Eventually last known value, from meta data */
1059
1060 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1061 local = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
1062 mdev->local_max_bio_size = local;
1063 put_ldev(mdev);
1064 }
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001065 local = min(local, DRBD_MAX_BIO_SIZE);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001066
1067 /* We may ignore peer limits if the peer is modern enough.
1068 Because new from 8.3.8 onwards the peer can use multiple
1069 BIOs for a single peer_request */
1070 if (mdev->state.conn >= C_CONNECTED) {
Philipp Reisner31890f42011-01-19 14:12:51 +01001071 if (mdev->tconn->agreed_pro_version < 94)
Philipp Reisner98683652012-11-09 14:18:43 +01001072 peer = min( mdev->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
Philipp Reisner68093842011-06-30 15:43:06 +02001073 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
Philipp Reisner31890f42011-01-19 14:12:51 +01001074 else if (mdev->tconn->agreed_pro_version == 94)
Philipp Reisner99432fc2011-05-20 16:39:13 +02001075 peer = DRBD_MAX_SIZE_H80_PACKET;
Philipp Reisner2ffca4f2011-06-30 15:43:06 +02001076 else if (mdev->tconn->agreed_pro_version < 100)
1077 peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
1078 else
Philipp Reisner99432fc2011-05-20 16:39:13 +02001079 peer = DRBD_MAX_BIO_SIZE;
1080 }
1081
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001082 new = min(local, peer);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001083
1084 if (mdev->state.role == R_PRIMARY && new < now)
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001085 dev_err(DEV, "ASSERT FAILED new < now; (%u < %u)\n", new, now);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001086
1087 if (new != now)
1088 dev_info(DEV, "max BIO size = %u\n", new);
1089
1090 drbd_setup_queue_param(mdev, new);
1091}
1092
Philipp Reisnera18e9d12011-04-24 11:09:55 +02001093/* Starts the worker thread */
Philipp Reisner0e29d162011-02-18 14:23:11 +01001094static void conn_reconfig_start(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001095{
Philipp Reisner0e29d162011-02-18 14:23:11 +01001096 drbd_thread_start(&tconn->worker);
1097 conn_flush_workqueue(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001098}
1099
Philipp Reisnera18e9d12011-04-24 11:09:55 +02001100/* if still unconfigured, stops worker again. */
Philipp Reisner0e29d162011-02-18 14:23:11 +01001101static void conn_reconfig_done(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001102{
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001103 bool stop_threads;
Philipp Reisner0e29d162011-02-18 14:23:11 +01001104 spin_lock_irq(&tconn->req_lock);
Philipp Reisnere0e16652011-07-11 17:04:23 +02001105 stop_threads = conn_all_vols_unconf(tconn) &&
1106 tconn->cstate == C_STANDALONE;
Philipp Reisner0e29d162011-02-18 14:23:11 +01001107 spin_unlock_irq(&tconn->req_lock);
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001108 if (stop_threads) {
1109 /* asender is implicitly stopped by receiver
Philipp Reisner81fa2e62011-05-04 15:10:30 +02001110 * in conn_disconnect() */
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001111 drbd_thread_stop(&tconn->receiver);
1112 drbd_thread_stop(&tconn->worker);
1113 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001114}
1115
Philipp Reisner07782862010-08-31 12:00:50 +02001116/* Make sure IO is suspended before calling this function(). */
1117static void drbd_suspend_al(struct drbd_conf *mdev)
1118{
1119 int s = 0;
1120
Lars Ellenberg61610422011-02-21 13:20:54 +01001121 if (!lc_try_lock(mdev->act_log)) {
Philipp Reisner07782862010-08-31 12:00:50 +02001122 dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n");
1123 return;
1124 }
1125
Lars Ellenberg61610422011-02-21 13:20:54 +01001126 drbd_al_shrink(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001127 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner07782862010-08-31 12:00:50 +02001128 if (mdev->state.conn < C_CONNECTED)
1129 s = !test_and_set_bit(AL_SUSPENDED, &mdev->flags);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001130 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg61610422011-02-21 13:20:54 +01001131 lc_unlock(mdev->act_log);
Philipp Reisner07782862010-08-31 12:00:50 +02001132
1133 if (s)
1134 dev_info(DEV, "Suspended AL updates\n");
1135}
1136
Lars Ellenberg5979e362011-04-27 21:09:55 +02001137
1138static bool should_set_defaults(struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001139{
Lars Ellenberg5979e362011-04-27 21:09:55 +02001140 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1141 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1142}
1143
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001144static unsigned int drbd_al_extents_max(struct drbd_backing_dev *bdev)
Philipp Reisnerd589a212011-05-04 10:06:52 +02001145{
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001146 /* This is limited by 16 bit "slot" numbers,
1147 * and by available on-disk context storage.
1148 *
1149 * Also (u16)~0 is special (denotes a "free" extent).
1150 *
1151 * One transaction occupies one 4kB on-disk block,
1152 * we have n such blocks in the on disk ring buffer,
1153 * the "current" transaction may fail (n-1),
1154 * and there is 919 slot numbers context information per transaction.
1155 *
1156 * 72 transaction blocks amounts to more than 2**16 context slots,
1157 * so cap there first.
1158 */
1159 const unsigned int max_al_nr = DRBD_AL_EXTENTS_MAX;
1160 const unsigned int sufficient_on_disk =
1161 (max_al_nr + AL_CONTEXT_PER_TRANSACTION -1)
1162 /AL_CONTEXT_PER_TRANSACTION;
Philipp Reisnerd589a212011-05-04 10:06:52 +02001163
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001164 unsigned int al_size_4k = bdev->md.al_size_4k;
1165
1166 if (al_size_4k > sufficient_on_disk)
1167 return max_al_nr;
1168
1169 return (al_size_4k - 1) * AL_CONTEXT_PER_TRANSACTION;
Philipp Reisnerd589a212011-05-04 10:06:52 +02001170}
1171
Lars Ellenbergf3990022011-03-23 14:31:09 +01001172int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1173{
1174 enum drbd_ret_code retcode;
1175 struct drbd_conf *mdev;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001176 struct disk_conf *new_disk_conf, *old_disk_conf;
Philipp Reisner813472c2011-05-03 16:47:02 +02001177 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001178 int err, fifo_size;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001179
1180 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1181 if (!adm_ctx.reply_skb)
1182 return retcode;
1183 if (retcode != NO_ERROR)
1184 goto out;
1185
1186 mdev = adm_ctx.mdev;
1187
1188 /* we also need a disk
1189 * to change the options on */
1190 if (!get_ldev(mdev)) {
1191 retcode = ERR_NO_DISK;
1192 goto out;
1193 }
1194
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001195 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001196 if (!new_disk_conf) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001197 retcode = ERR_NOMEM;
1198 goto fail;
1199 }
1200
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001201 mutex_lock(&mdev->tconn->conf_update);
1202 old_disk_conf = mdev->ldev->disk_conf;
1203 *new_disk_conf = *old_disk_conf;
Lars Ellenberg5979e362011-04-27 21:09:55 +02001204 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02001205 set_disk_conf_defaults(new_disk_conf);
Lars Ellenberg5979e362011-04-27 21:09:55 +02001206
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001207 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02001208 if (err && err != -ENOMSG) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001209 retcode = ERR_MANDATORY_TAG;
1210 drbd_msg_put_info(from_attrs_err_to_txt(err));
1211 }
1212
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001213 if (!expect(new_disk_conf->resync_rate >= 1))
1214 new_disk_conf->resync_rate = 1;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001215
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001216 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1217 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
1218 if (new_disk_conf->al_extents > drbd_al_extents_max(mdev->ldev))
1219 new_disk_conf->al_extents = drbd_al_extents_max(mdev->ldev);
1220
1221 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1222 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001223
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001224 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Philipp Reisner9958c852011-05-03 16:19:31 +02001225 if (fifo_size != mdev->rs_plan_s->size) {
Philipp Reisner813472c2011-05-03 16:47:02 +02001226 new_plan = fifo_alloc(fifo_size);
1227 if (!new_plan) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001228 dev_err(DEV, "kmalloc of fifo_buffer failed");
1229 retcode = ERR_NOMEM;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001230 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001231 }
1232 }
1233
Lars Ellenberg0ee98e22012-08-20 14:54:48 +02001234 drbd_suspend_io(mdev);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001235 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
1236 drbd_al_shrink(mdev);
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001237 err = drbd_check_al_size(mdev, new_disk_conf);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001238 lc_unlock(mdev->act_log);
1239 wake_up(&mdev->al_wait);
Lars Ellenberg0ee98e22012-08-20 14:54:48 +02001240 drbd_resume_io(mdev);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001241
1242 if (err) {
1243 retcode = ERR_NOMEM;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001244 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001245 }
1246
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001247 write_lock_irq(&global_state_lock);
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001248 retcode = drbd_resync_after_valid(mdev, new_disk_conf->resync_after);
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001249 if (retcode == NO_ERROR) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001250 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001251 drbd_resync_after_changed(mdev);
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001252 }
1253 write_unlock_irq(&global_state_lock);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001254
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001255 if (retcode != NO_ERROR)
1256 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001257
Philipp Reisner813472c2011-05-03 16:47:02 +02001258 if (new_plan) {
1259 old_plan = mdev->rs_plan_s;
1260 rcu_assign_pointer(mdev->rs_plan_s, new_plan);
Philipp Reisner9958c852011-05-03 16:19:31 +02001261 }
Philipp Reisner9958c852011-05-03 16:19:31 +02001262
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001263 mutex_unlock(&mdev->tconn->conf_update);
Philipp Reisner27eb13e2012-03-30 14:12:15 +02001264
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001265 if (new_disk_conf->al_updates)
Philipp Reisner4035e4c2012-10-01 18:04:12 +02001266 mdev->ldev->md.flags &= ~MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001267 else
1268 mdev->ldev->md.flags |= MDF_AL_DISABLED;
1269
Lars Ellenberg691631c2012-10-26 00:41:50 +02001270 if (new_disk_conf->md_flushes)
1271 clear_bit(MD_NO_FUA, &mdev->flags);
1272 else
1273 set_bit(MD_NO_FUA, &mdev->flags);
1274
Philipp Reisner27eb13e2012-03-30 14:12:15 +02001275 drbd_bump_write_ordering(mdev->tconn, WO_bdev_flush);
1276
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001277 drbd_md_sync(mdev);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001278
1279 if (mdev->state.conn >= C_CONNECTED)
1280 drbd_send_sync_param(mdev);
1281
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001282 synchronize_rcu();
1283 kfree(old_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02001284 kfree(old_plan);
Philipp Reisnercdfda632011-07-05 15:38:59 +02001285 mod_timer(&mdev->request_timer, jiffies + HZ);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001286 goto success;
1287
1288fail_unlock:
1289 mutex_unlock(&mdev->tconn->conf_update);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001290 fail:
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001291 kfree(new_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02001292 kfree(new_plan);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001293success:
1294 put_ldev(mdev);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001295 out:
1296 drbd_adm_finish(info, retcode);
1297 return 0;
1298}
1299
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001300int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001301{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001302 struct drbd_conf *mdev;
1303 int err;
Andreas Gruenbacher116676c2010-12-08 13:33:11 +01001304 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001305 enum determine_dev_size dd;
1306 sector_t max_possible_sectors;
1307 sector_t min_md_device_sectors;
1308 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001309 struct disk_conf *new_disk_conf = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001310 struct block_device *bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001311 struct lru_cache *resync_lru = NULL;
Philipp Reisner9958c852011-05-03 16:19:31 +02001312 struct fifo_buffer *new_plan = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001313 union drbd_state ns, os;
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001314 enum drbd_state_rv rv;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001315 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001316
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001317 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1318 if (!adm_ctx.reply_skb)
1319 return retcode;
1320 if (retcode != NO_ERROR)
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001321 goto finish;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001322
1323 mdev = adm_ctx.mdev;
Philipp Reisner0e29d162011-02-18 14:23:11 +01001324 conn_reconfig_start(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001325
1326 /* if you want to reconfigure, please tear down first */
1327 if (mdev->state.disk > D_DISKLESS) {
1328 retcode = ERR_DISK_CONFIGURED;
1329 goto fail;
1330 }
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001331 /* It may just now have detached because of IO error. Make sure
1332 * drbd_ldev_destroy is done already, we may end up here very fast,
1333 * e.g. if someone calls attach from the on-io-error handler,
1334 * to realize a "hot spare" feature (not that I'd recommend that) */
1335 wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001336
Lars Ellenberg383606e2012-06-14 14:21:32 +02001337 /* make sure there is no leftover from previous force-detach attempts */
Lars Ellenberg0c849662012-07-30 09:07:28 +02001338 clear_bit(FORCE_DETACH, &mdev->flags);
Lars Ellenbergedc9f5e2012-09-27 15:18:21 +02001339 clear_bit(WAS_IO_ERROR, &mdev->flags);
1340 clear_bit(WAS_READ_ERROR, &mdev->flags);
Lars Ellenberg383606e2012-06-14 14:21:32 +02001341
Lars Ellenberg0029d622012-06-14 18:02:52 +02001342 /* and no leftover from previously aborted resync or verify, either */
1343 mdev->rs_total = 0;
1344 mdev->rs_failed = 0;
1345 atomic_set(&mdev->rs_pending_cnt, 0);
1346
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001347 /* allocation not in the IO path, drbdsetup context */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001348 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1349 if (!nbc) {
1350 retcode = ERR_NOMEM;
1351 goto fail;
1352 }
Philipp Reisner9f2247b2012-08-16 14:25:58 +02001353 spin_lock_init(&nbc->md.uuid_lock);
1354
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001355 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
1356 if (!new_disk_conf) {
1357 retcode = ERR_NOMEM;
1358 goto fail;
1359 }
1360 nbc->disk_conf = new_disk_conf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001361
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001362 set_disk_conf_defaults(new_disk_conf);
1363 err = disk_conf_from_attrs(new_disk_conf, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001364 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001365 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001366 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001367 goto fail;
1368 }
1369
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001370 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1371 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
Philipp Reisnerd589a212011-05-04 10:06:52 +02001372
Philipp Reisner9958c852011-05-03 16:19:31 +02001373 new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
1374 if (!new_plan) {
1375 retcode = ERR_NOMEM;
1376 goto fail;
1377 }
1378
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001379 if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001380 retcode = ERR_MD_IDX_INVALID;
1381 goto fail;
1382 }
1383
Philipp Reisner44ed1672011-04-19 17:10:19 +02001384 rcu_read_lock();
1385 nc = rcu_dereference(mdev->tconn->net_conf);
1386 if (nc) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001387 if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02001388 rcu_read_unlock();
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001389 retcode = ERR_STONITH_AND_PROT_A;
1390 goto fail;
1391 }
1392 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02001393 rcu_read_unlock();
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001394
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001395 bdev = blkdev_get_by_path(new_disk_conf->backing_dev,
Tejun Heod4d77622010-11-13 11:55:18 +01001396 FMODE_READ | FMODE_WRITE | FMODE_EXCL, mdev);
Tejun Heoe525fd82010-11-13 11:55:17 +01001397 if (IS_ERR(bdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001398 dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->backing_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001399 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001400 retcode = ERR_OPEN_DISK;
1401 goto fail;
1402 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001403 nbc->backing_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001404
Tejun Heoe525fd82010-11-13 11:55:17 +01001405 /*
1406 * meta_dev_idx >= 0: external fixed size, possibly multiple
1407 * drbd sharing one meta device. TODO in that case, paranoia
1408 * check that [md_bdev, meta_dev_idx] is not yet used by some
1409 * other drbd minor! (if you use drbd.conf + drbdadm, that
1410 * should check it for you already; but if you don't, or
1411 * someone fooled it, we need to double check here)
1412 */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001413 bdev = blkdev_get_by_path(new_disk_conf->meta_dev,
Tejun Heod4d77622010-11-13 11:55:18 +01001414 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001415 (new_disk_conf->meta_dev_idx < 0) ?
Tejun Heod4d77622010-11-13 11:55:18 +01001416 (void *)mdev : (void *)drbd_m_holder);
Tejun Heoe525fd82010-11-13 11:55:17 +01001417 if (IS_ERR(bdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001418 dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->meta_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001419 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001420 retcode = ERR_OPEN_MD_DISK;
1421 goto fail;
1422 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001423 nbc->md_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001424
Tejun Heoe525fd82010-11-13 11:55:17 +01001425 if ((nbc->backing_bdev == nbc->md_bdev) !=
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001426 (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1427 new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001428 retcode = ERR_MD_IDX_INVALID;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001429 goto fail;
1430 }
1431
1432 resync_lru = lc_create("resync", drbd_bm_ext_cache,
Lars Ellenberg46a15bc2011-02-21 13:21:01 +01001433 1, 61, sizeof(struct bm_extent),
Philipp Reisnerb411b362009-09-25 16:07:19 -07001434 offsetof(struct bm_extent, lce));
1435 if (!resync_lru) {
1436 retcode = ERR_NOMEM;
Tejun Heoe525fd82010-11-13 11:55:17 +01001437 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001438 }
1439
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +01001440 /* Read our meta data super block early.
1441 * This also sets other on-disk offsets. */
1442 retcode = drbd_md_read(mdev, nbc);
1443 if (retcode != NO_ERROR)
1444 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001445
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001446 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1447 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
1448 if (new_disk_conf->al_extents > drbd_al_extents_max(nbc))
1449 new_disk_conf->al_extents = drbd_al_extents_max(nbc);
1450
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001451 if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001452 dev_err(DEV, "max capacity %llu smaller than disk size %llu\n",
1453 (unsigned long long) drbd_get_max_capacity(nbc),
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001454 (unsigned long long) new_disk_conf->disk_size);
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001455 retcode = ERR_DISK_TOO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001456 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001457 }
1458
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001459 if (new_disk_conf->meta_dev_idx < 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001460 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1461 /* at least one MB, otherwise it does not make sense */
1462 min_md_device_sectors = (2<<10);
1463 } else {
1464 max_possible_sectors = DRBD_MAX_SECTORS;
Lars Ellenbergae8bf312013-03-19 18:16:43 +01001465 min_md_device_sectors = MD_128MB_SECT * (new_disk_conf->meta_dev_idx + 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001466 }
1467
Philipp Reisnerb411b362009-09-25 16:07:19 -07001468 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001469 retcode = ERR_MD_DISK_TOO_SMALL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001470 dev_warn(DEV, "refusing attach: md-device too small, "
1471 "at least %llu sectors needed for this meta-disk type\n",
1472 (unsigned long long) min_md_device_sectors);
Tejun Heoe525fd82010-11-13 11:55:17 +01001473 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001474 }
1475
1476 /* Make sure the new disk is big enough
1477 * (we may currently be R_PRIMARY with no local disk...) */
1478 if (drbd_get_max_capacity(nbc) <
1479 drbd_get_capacity(mdev->this_bdev)) {
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001480 retcode = ERR_DISK_TOO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001481 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001482 }
1483
1484 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1485
Lars Ellenberg13529942009-10-12 19:07:49 +02001486 if (nbc->known_size > max_possible_sectors) {
1487 dev_warn(DEV, "==> truncating very big lower level device "
1488 "to currently maximum possible %llu sectors <==\n",
1489 (unsigned long long) max_possible_sectors);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001490 if (new_disk_conf->meta_dev_idx >= 0)
Lars Ellenberg13529942009-10-12 19:07:49 +02001491 dev_warn(DEV, "==>> using internal or flexible "
1492 "meta data may help <<==\n");
1493 }
1494
Philipp Reisnerb411b362009-09-25 16:07:19 -07001495 drbd_suspend_io(mdev);
1496 /* also wait for the last barrier ack. */
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001497 /* FIXME see also https://daiquiri.linbit/cgi-bin/bugzilla/show_bug.cgi?id=171
1498 * We need a way to either ignore barrier acks for barriers sent before a device
1499 * was attached, or a way to wait for all pending barrier acks to come in.
1500 * As barriers are counted per resource,
1501 * we'd need to suspend io on all devices of a resource.
1502 */
Philipp Reisner2aebfab2011-03-28 16:48:11 +02001503 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_pending_cnt) || drbd_suspended(mdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001504 /* and for any other previously queued work */
1505 drbd_flush_workqueue(mdev);
1506
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001507 rv = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE);
1508 retcode = rv; /* FIXME: Type mismatch. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001509 drbd_resume_io(mdev);
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001510 if (rv < SS_SUCCESS)
Tejun Heoe525fd82010-11-13 11:55:17 +01001511 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001512
1513 if (!get_ldev_if_state(mdev, D_ATTACHING))
1514 goto force_diskless;
1515
Philipp Reisnerb411b362009-09-25 16:07:19 -07001516 if (!mdev->bitmap) {
1517 if (drbd_bm_init(mdev)) {
1518 retcode = ERR_NOMEM;
1519 goto force_diskless_dec;
1520 }
1521 }
1522
Philipp Reisnerb411b362009-09-25 16:07:19 -07001523 if (mdev->state.conn < C_CONNECTED &&
1524 mdev->state.role == R_PRIMARY &&
1525 (mdev->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
1526 dev_err(DEV, "Can only attach to data with current UUID=%016llX\n",
1527 (unsigned long long)mdev->ed_uuid);
1528 retcode = ERR_DATA_NOT_CURRENT;
1529 goto force_diskless_dec;
1530 }
1531
1532 /* Since we are diskless, fix the activity log first... */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001533 if (drbd_check_al_size(mdev, new_disk_conf)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001534 retcode = ERR_NOMEM;
1535 goto force_diskless_dec;
1536 }
1537
1538 /* Prevent shrinking of consistent devices ! */
1539 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001540 drbd_new_dev_size(mdev, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001541 dev_warn(DEV, "refusing to truncate a consistent device\n");
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001542 retcode = ERR_DISK_TOO_SMALL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001543 goto force_diskless_dec;
1544 }
1545
Philipp Reisnerb411b362009-09-25 16:07:19 -07001546 /* Reset the "barriers don't work" bits here, then force meta data to
1547 * be written, to ensure we determine if barriers are supported. */
Andreas Gruenbachere5440462011-05-04 15:25:35 +02001548 if (new_disk_conf->md_flushes)
Philipp Reisnera8a4e512010-08-25 10:21:04 +02001549 clear_bit(MD_NO_FUA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001550 else
Andreas Gruenbachere5440462011-05-04 15:25:35 +02001551 set_bit(MD_NO_FUA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001552
1553 /* Point of no return reached.
1554 * Devices and memory are no longer released by error cleanup below.
1555 * now mdev takes over responsibility, and the state engine should
1556 * clean it up somewhere. */
1557 D_ASSERT(mdev->ldev == NULL);
1558 mdev->ldev = nbc;
1559 mdev->resync = resync_lru;
Philipp Reisner9958c852011-05-03 16:19:31 +02001560 mdev->rs_plan_s = new_plan;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001561 nbc = NULL;
1562 resync_lru = NULL;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001563 new_disk_conf = NULL;
Philipp Reisner9958c852011-05-03 16:19:31 +02001564 new_plan = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001565
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001566 drbd_bump_write_ordering(mdev->tconn, WO_bdev_flush);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001567
1568 if (drbd_md_test_flag(mdev->ldev, MDF_CRASHED_PRIMARY))
1569 set_bit(CRASHED_PRIMARY, &mdev->flags);
1570 else
1571 clear_bit(CRASHED_PRIMARY, &mdev->flags);
1572
Philipp Reisner894c6a92010-06-18 16:03:20 +02001573 if (drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
Lars Ellenbergd5d7ebd2011-07-05 20:59:26 +02001574 !(mdev->state.role == R_PRIMARY && mdev->tconn->susp_nod))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001575 set_bit(CRASHED_PRIMARY, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001576
1577 mdev->send_cnt = 0;
1578 mdev->recv_cnt = 0;
1579 mdev->read_cnt = 0;
1580 mdev->writ_cnt = 0;
1581
Philipp Reisner99432fc2011-05-20 16:39:13 +02001582 drbd_reconsider_max_bio_size(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001583
1584 /* If I am currently not R_PRIMARY,
1585 * but meta data primary indicator is set,
1586 * I just now recover from a hard crash,
1587 * and have been R_PRIMARY before that crash.
1588 *
1589 * Now, if I had no connection before that crash
1590 * (have been degraded R_PRIMARY), chances are that
1591 * I won't find my peer now either.
1592 *
1593 * In that case, and _only_ in that case,
1594 * we use the degr-wfc-timeout instead of the default,
1595 * so we can automatically recover from a crash of a
1596 * degraded but active "cluster" after a certain timeout.
1597 */
1598 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
1599 if (mdev->state.role != R_PRIMARY &&
1600 drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
1601 !drbd_md_test_flag(mdev->ldev, MDF_CONNECTED_IND))
1602 set_bit(USE_DEGR_WFC_T, &mdev->flags);
1603
Bart Van Assche24c48302011-05-21 18:32:29 +02001604 dd = drbd_determine_dev_size(mdev, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001605 if (dd == dev_size_error) {
1606 retcode = ERR_NOMEM_BITMAP;
1607 goto force_diskless_dec;
1608 } else if (dd == grew)
1609 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
1610
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001611 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC) ||
1612 (test_bit(CRASHED_PRIMARY, &mdev->flags) &&
1613 drbd_md_test_flag(mdev->ldev, MDF_AL_DISABLED))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001614 dev_info(DEV, "Assuming that all blocks are out of sync "
1615 "(aka FullSync)\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001616 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write,
1617 "set_n_write from attaching", BM_LOCKED_MASK)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001618 retcode = ERR_IO_MD_DISK;
1619 goto force_diskless_dec;
1620 }
1621 } else {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001622 if (drbd_bitmap_io(mdev, &drbd_bm_read,
Andreas Gruenbacher22ab6a32010-12-13 01:44:11 +01001623 "read from attaching", BM_LOCKED_MASK)) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001624 retcode = ERR_IO_MD_DISK;
1625 goto force_diskless_dec;
1626 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001627 }
1628
Philipp Reisner07782862010-08-31 12:00:50 +02001629 if (_drbd_bm_total_weight(mdev) == drbd_bm_bits(mdev))
1630 drbd_suspend_al(mdev); /* IO is still suspended here... */
1631
Philipp Reisner87eeee42011-01-19 14:16:30 +01001632 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner78bae592011-03-28 15:40:12 +02001633 os = drbd_read_state(mdev);
1634 ns = os;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001635 /* If MDF_CONSISTENT is not set go into inconsistent state,
1636 otherwise investigate MDF_WasUpToDate...
1637 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1638 otherwise into D_CONSISTENT state.
1639 */
1640 if (drbd_md_test_flag(mdev->ldev, MDF_CONSISTENT)) {
1641 if (drbd_md_test_flag(mdev->ldev, MDF_WAS_UP_TO_DATE))
1642 ns.disk = D_CONSISTENT;
1643 else
1644 ns.disk = D_OUTDATED;
1645 } else {
1646 ns.disk = D_INCONSISTENT;
1647 }
1648
1649 if (drbd_md_test_flag(mdev->ldev, MDF_PEER_OUT_DATED))
1650 ns.pdsk = D_OUTDATED;
1651
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001652 rcu_read_lock();
1653 if (ns.disk == D_CONSISTENT &&
1654 (ns.pdsk == D_OUTDATED || rcu_dereference(mdev->ldev->disk_conf)->fencing == FP_DONT_CARE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001655 ns.disk = D_UP_TO_DATE;
1656
1657 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1658 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1659 this point, because drbd_request_state() modifies these
1660 flags. */
1661
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001662 if (rcu_dereference(mdev->ldev->disk_conf)->al_updates)
Philipp Reisner4035e4c2012-10-01 18:04:12 +02001663 mdev->ldev->md.flags &= ~MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001664 else
1665 mdev->ldev->md.flags |= MDF_AL_DISABLED;
1666
1667 rcu_read_unlock();
1668
Philipp Reisnerb411b362009-09-25 16:07:19 -07001669 /* In case we are C_CONNECTED postpone any decision on the new disk
1670 state after the negotiation phase. */
1671 if (mdev->state.conn == C_CONNECTED) {
1672 mdev->new_state_tmp.i = ns.i;
1673 ns.i = os.i;
1674 ns.disk = D_NEGOTIATING;
Philipp Reisnerdc66c742010-06-02 14:31:29 +02001675
1676 /* We expect to receive up-to-date UUIDs soon.
1677 To avoid a race in receive_state, free p_uuid while
1678 holding req_lock. I.e. atomic with the state change */
1679 kfree(mdev->p_uuid);
1680 mdev->p_uuid = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001681 }
1682
1683 rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001684 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001685
1686 if (rv < SS_SUCCESS)
1687 goto force_diskless_dec;
1688
Philipp Reisnercdfda632011-07-05 15:38:59 +02001689 mod_timer(&mdev->request_timer, jiffies + HZ);
1690
Philipp Reisnerb411b362009-09-25 16:07:19 -07001691 if (mdev->state.role == R_PRIMARY)
1692 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
1693 else
1694 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
1695
1696 drbd_md_mark_dirty(mdev);
1697 drbd_md_sync(mdev);
1698
1699 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1700 put_ldev(mdev);
Philipp Reisner0e29d162011-02-18 14:23:11 +01001701 conn_reconfig_done(mdev->tconn);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001702 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001703 return 0;
1704
1705 force_diskless_dec:
1706 put_ldev(mdev);
1707 force_diskless:
Philipp Reisner9510b242011-07-01 17:00:57 +02001708 drbd_force_state(mdev, NS(disk, D_DISKLESS));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001709 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001710 fail:
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001711 conn_reconfig_done(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001712 if (nbc) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001713 if (nbc->backing_bdev)
1714 blkdev_put(nbc->backing_bdev,
1715 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1716 if (nbc->md_bdev)
1717 blkdev_put(nbc->md_bdev,
1718 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001719 kfree(nbc);
1720 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001721 kfree(new_disk_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001722 lc_destroy(resync_lru);
Philipp Reisner9958c852011-05-03 16:19:31 +02001723 kfree(new_plan);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001724
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001725 finish:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001726 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001727 return 0;
1728}
1729
Philipp Reisnercdfda632011-07-05 15:38:59 +02001730static int adm_detach(struct drbd_conf *mdev, int force)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001731{
Philipp Reisner19f83c72011-03-29 14:21:03 +02001732 enum drbd_state_rv retcode;
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001733 int ret;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001734
Philipp Reisnercdfda632011-07-05 15:38:59 +02001735 if (force) {
Lars Ellenberg0c849662012-07-30 09:07:28 +02001736 set_bit(FORCE_DETACH, &mdev->flags);
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001737 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnercdfda632011-07-05 15:38:59 +02001738 retcode = SS_SUCCESS;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001739 goto out;
1740 }
1741
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001742 drbd_suspend_io(mdev); /* so no-one is stuck in drbd_al_begin_io */
Lars Ellenberga2e91382011-10-06 17:30:26 +02001743 drbd_md_get_buffer(mdev); /* make sure there is no in-flight meta-data IO */
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001744 retcode = drbd_request_state(mdev, NS(disk, D_FAILED));
Lars Ellenberga2e91382011-10-06 17:30:26 +02001745 drbd_md_put_buffer(mdev);
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001746 /* D_FAILED will transition to DISKLESS. */
1747 ret = wait_event_interruptible(mdev->misc_wait,
1748 mdev->state.disk != D_FAILED);
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001749 drbd_resume_io(mdev);
Philipp Reisner9b2f61a2011-05-24 10:27:38 +02001750 if ((int)retcode == (int)SS_IS_DISKLESS)
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001751 retcode = SS_NOTHING_TO_DO;
1752 if (ret)
1753 retcode = ERR_INTR;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001754out:
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001755 return retcode;
1756}
1757
Philipp Reisnerb411b362009-09-25 16:07:19 -07001758/* Detaching the disk is a process in multiple stages. First we need to lock
1759 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1760 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1761 * internal references as well.
1762 * Only then we have finally detached. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001763int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001764{
1765 enum drbd_ret_code retcode;
Philipp Reisnercdfda632011-07-05 15:38:59 +02001766 struct detach_parms parms = { };
1767 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001768
1769 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1770 if (!adm_ctx.reply_skb)
1771 return retcode;
1772 if (retcode != NO_ERROR)
1773 goto out;
1774
Philipp Reisnercdfda632011-07-05 15:38:59 +02001775 if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
1776 err = detach_parms_from_attrs(&parms, info);
1777 if (err) {
1778 retcode = ERR_MANDATORY_TAG;
1779 drbd_msg_put_info(from_attrs_err_to_txt(err));
1780 goto out;
1781 }
1782 }
1783
1784 retcode = adm_detach(adm_ctx.mdev, parms.force_detach);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001785out:
1786 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001787 return 0;
1788}
1789
Lars Ellenbergf3990022011-03-23 14:31:09 +01001790static bool conn_resync_running(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001791{
Lars Ellenbergf3990022011-03-23 14:31:09 +01001792 struct drbd_conf *mdev;
Philipp Reisner695d08f2011-04-11 22:53:32 -07001793 bool rv = false;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001794 int vnr;
1795
Philipp Reisner695d08f2011-04-11 22:53:32 -07001796 rcu_read_lock();
Lars Ellenbergf3990022011-03-23 14:31:09 +01001797 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1798 if (mdev->state.conn == C_SYNC_SOURCE ||
1799 mdev->state.conn == C_SYNC_TARGET ||
1800 mdev->state.conn == C_PAUSED_SYNC_S ||
Philipp Reisner695d08f2011-04-11 22:53:32 -07001801 mdev->state.conn == C_PAUSED_SYNC_T) {
1802 rv = true;
1803 break;
1804 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001805 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001806 rcu_read_unlock();
1807
1808 return rv;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001809}
1810
1811static bool conn_ov_running(struct drbd_tconn *tconn)
1812{
1813 struct drbd_conf *mdev;
Philipp Reisner695d08f2011-04-11 22:53:32 -07001814 bool rv = false;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001815 int vnr;
1816
Philipp Reisner695d08f2011-04-11 22:53:32 -07001817 rcu_read_lock();
Lars Ellenbergf3990022011-03-23 14:31:09 +01001818 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1819 if (mdev->state.conn == C_VERIFY_S ||
Philipp Reisner695d08f2011-04-11 22:53:32 -07001820 mdev->state.conn == C_VERIFY_T) {
1821 rv = true;
1822 break;
1823 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001824 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001825 rcu_read_unlock();
1826
1827 return rv;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001828}
1829
Philipp Reisnercd643972011-04-13 18:00:59 -07001830static enum drbd_ret_code
Philipp Reisner44ed1672011-04-19 17:10:19 +02001831_check_net_options(struct drbd_tconn *tconn, struct net_conf *old_conf, struct net_conf *new_conf)
Philipp Reisnercd643972011-04-13 18:00:59 -07001832{
1833 struct drbd_conf *mdev;
1834 int i;
1835
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02001836 if (old_conf && tconn->cstate == C_WF_REPORT_PARAMS && tconn->agreed_pro_version < 100) {
1837 if (new_conf->wire_protocol != old_conf->wire_protocol)
1838 return ERR_NEED_APV_100;
1839
1840 if (new_conf->two_primaries != old_conf->two_primaries)
1841 return ERR_NEED_APV_100;
1842
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02001843 if (strcmp(new_conf->integrity_alg, old_conf->integrity_alg))
1844 return ERR_NEED_APV_100;
1845 }
1846
1847 if (!new_conf->two_primaries &&
1848 conn_highest_role(tconn) == R_PRIMARY &&
1849 conn_highest_peer(tconn) == R_PRIMARY)
1850 return ERR_NEED_ALLOW_TWO_PRI;
Philipp Reisnerb032b6f2011-04-13 18:16:10 -07001851
Philipp Reisnercd643972011-04-13 18:00:59 -07001852 if (new_conf->two_primaries &&
1853 (new_conf->wire_protocol != DRBD_PROT_C))
1854 return ERR_NOT_PROTO_C;
1855
Philipp Reisnercd643972011-04-13 18:00:59 -07001856 idr_for_each_entry(&tconn->volumes, mdev, i) {
1857 if (get_ldev(mdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001858 enum drbd_fencing_p fp = rcu_dereference(mdev->ldev->disk_conf)->fencing;
Philipp Reisnercd643972011-04-13 18:00:59 -07001859 put_ldev(mdev);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001860 if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
Philipp Reisnercd643972011-04-13 18:00:59 -07001861 return ERR_STONITH_AND_PROT_A;
Philipp Reisnercd643972011-04-13 18:00:59 -07001862 }
Andreas Gruenbacher6139f602011-05-06 20:00:02 +02001863 if (mdev->state.role == R_PRIMARY && new_conf->discard_my_data)
Lars Ellenbergeb120102012-08-01 12:46:20 +02001864 return ERR_DISCARD_IMPOSSIBLE;
Philipp Reisnercd643972011-04-13 18:00:59 -07001865 }
Philipp Reisnercd643972011-04-13 18:00:59 -07001866
1867 if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A)
1868 return ERR_CONG_NOT_PROTO_A;
1869
1870 return NO_ERROR;
1871}
1872
Philipp Reisner44ed1672011-04-19 17:10:19 +02001873static enum drbd_ret_code
1874check_net_options(struct drbd_tconn *tconn, struct net_conf *new_conf)
1875{
1876 static enum drbd_ret_code rv;
1877 struct drbd_conf *mdev;
1878 int i;
1879
1880 rcu_read_lock();
1881 rv = _check_net_options(tconn, rcu_dereference(tconn->net_conf), new_conf);
1882 rcu_read_unlock();
1883
1884 /* tconn->volumes protected by genl_lock() here */
1885 idr_for_each_entry(&tconn->volumes, mdev, i) {
1886 if (!mdev->bitmap) {
1887 if(drbd_bm_init(mdev))
1888 return ERR_NOMEM;
1889 }
1890 }
1891
1892 return rv;
1893}
1894
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001895struct crypto {
1896 struct crypto_hash *verify_tfm;
1897 struct crypto_hash *csums_tfm;
1898 struct crypto_hash *cram_hmac_tfm;
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02001899 struct crypto_hash *integrity_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001900};
1901
1902static int
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02001903alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001904{
1905 if (!tfm_name[0])
1906 return NO_ERROR;
1907
1908 *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
1909 if (IS_ERR(*tfm)) {
1910 *tfm = NULL;
1911 return err_alg;
1912 }
1913
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001914 return NO_ERROR;
1915}
1916
1917static enum drbd_ret_code
1918alloc_crypto(struct crypto *crypto, struct net_conf *new_conf)
1919{
Philipp Reisnerb411b362009-09-25 16:07:19 -07001920 char hmac_name[CRYPTO_MAX_ALG_NAME];
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001921 enum drbd_ret_code rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001922
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02001923 rv = alloc_hash(&crypto->csums_tfm, new_conf->csums_alg,
1924 ERR_CSUMS_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001925 if (rv != NO_ERROR)
1926 return rv;
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02001927 rv = alloc_hash(&crypto->verify_tfm, new_conf->verify_alg,
1928 ERR_VERIFY_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001929 if (rv != NO_ERROR)
1930 return rv;
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02001931 rv = alloc_hash(&crypto->integrity_tfm, new_conf->integrity_alg,
1932 ERR_INTEGRITY_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001933 if (rv != NO_ERROR)
1934 return rv;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001935 if (new_conf->cram_hmac_alg[0] != 0) {
1936 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
1937 new_conf->cram_hmac_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001938
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02001939 rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
1940 ERR_AUTH_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001941 }
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001942
1943 return rv;
1944}
1945
1946static void free_crypto(struct crypto *crypto)
1947{
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001948 crypto_free_hash(crypto->cram_hmac_tfm);
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02001949 crypto_free_hash(crypto->integrity_tfm);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001950 crypto_free_hash(crypto->csums_tfm);
1951 crypto_free_hash(crypto->verify_tfm);
1952}
1953
Lars Ellenbergf3990022011-03-23 14:31:09 +01001954int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
1955{
1956 enum drbd_ret_code retcode;
1957 struct drbd_tconn *tconn;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001958 struct net_conf *old_conf, *new_conf = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001959 int err;
1960 int ovr; /* online verify running */
1961 int rsr; /* re-sync running */
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001962 struct crypto crypto = { };
Lars Ellenbergf3990022011-03-23 14:31:09 +01001963
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02001964 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001965 if (!adm_ctx.reply_skb)
1966 return retcode;
1967 if (retcode != NO_ERROR)
1968 goto out;
1969
1970 tconn = adm_ctx.tconn;
1971
1972 new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
1973 if (!new_conf) {
1974 retcode = ERR_NOMEM;
1975 goto out;
1976 }
1977
Lars Ellenbergf3990022011-03-23 14:31:09 +01001978 conn_reconfig_start(tconn);
1979
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001980 mutex_lock(&tconn->data.mutex);
Philipp Reisnera0095502011-05-03 13:14:15 +02001981 mutex_lock(&tconn->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001982 old_conf = tconn->net_conf;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001983
1984 if (!old_conf) {
1985 drbd_msg_put_info("net conf missing, try connect");
1986 retcode = ERR_INVALID_REQUEST;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001987 goto fail;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001988 }
1989
1990 *new_conf = *old_conf;
Lars Ellenberg5979e362011-04-27 21:09:55 +02001991 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02001992 set_net_conf_defaults(new_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001993
Lars Ellenbergf3990022011-03-23 14:31:09 +01001994 err = net_conf_from_attrs_for_change(new_conf, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02001995 if (err && err != -ENOMSG) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001996 retcode = ERR_MANDATORY_TAG;
1997 drbd_msg_put_info(from_attrs_err_to_txt(err));
1998 goto fail;
1999 }
2000
Philipp Reisnercd643972011-04-13 18:00:59 -07002001 retcode = check_net_options(tconn, new_conf);
2002 if (retcode != NO_ERROR)
2003 goto fail;
2004
Lars Ellenbergf3990022011-03-23 14:31:09 +01002005 /* re-sync running */
2006 rsr = conn_resync_running(tconn);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002007 if (rsr && strcmp(new_conf->csums_alg, old_conf->csums_alg)) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002008 retcode = ERR_CSUMS_RESYNC_RUNNING;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002009 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002010 }
2011
Lars Ellenbergf3990022011-03-23 14:31:09 +01002012 /* online verify running */
2013 ovr = conn_ov_running(tconn);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002014 if (ovr && strcmp(new_conf->verify_alg, old_conf->verify_alg)) {
2015 retcode = ERR_VERIFY_RUNNING;
2016 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002017 }
2018
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002019 retcode = alloc_crypto(&crypto, new_conf);
2020 if (retcode != NO_ERROR)
2021 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002022
Philipp Reisner44ed1672011-04-19 17:10:19 +02002023 rcu_assign_pointer(tconn->net_conf, new_conf);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002024
2025 if (!rsr) {
2026 crypto_free_hash(tconn->csums_tfm);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002027 tconn->csums_tfm = crypto.csums_tfm;
2028 crypto.csums_tfm = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002029 }
2030 if (!ovr) {
2031 crypto_free_hash(tconn->verify_tfm);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002032 tconn->verify_tfm = crypto.verify_tfm;
2033 crypto.verify_tfm = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002034 }
2035
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02002036 crypto_free_hash(tconn->integrity_tfm);
2037 tconn->integrity_tfm = crypto.integrity_tfm;
Philipp Reisnerd659f2a2011-05-16 17:38:45 +02002038 if (tconn->cstate >= C_WF_REPORT_PARAMS && tconn->agreed_pro_version >= 100)
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02002039 /* Do this without trying to take tconn->data.mutex again. */
Philipp Reisnerd659f2a2011-05-16 17:38:45 +02002040 __drbd_send_protocol(tconn, P_PROTOCOL_UPDATE);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002041
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002042 crypto_free_hash(tconn->cram_hmac_tfm);
2043 tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
2044
Philipp Reisnera0095502011-05-03 13:14:15 +02002045 mutex_unlock(&tconn->conf_update);
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02002046 mutex_unlock(&tconn->data.mutex);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002047 synchronize_rcu();
2048 kfree(old_conf);
2049
Lars Ellenbergf3990022011-03-23 14:31:09 +01002050 if (tconn->cstate >= C_WF_REPORT_PARAMS)
2051 drbd_send_sync_param(minor_to_mdev(conn_lowest_minor(tconn)));
2052
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002053 goto done;
2054
Lars Ellenbergf3990022011-03-23 14:31:09 +01002055 fail:
Philipp Reisnera0095502011-05-03 13:14:15 +02002056 mutex_unlock(&tconn->conf_update);
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02002057 mutex_unlock(&tconn->data.mutex);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002058 free_crypto(&crypto);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002059 kfree(new_conf);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002060 done:
Lars Ellenbergf3990022011-03-23 14:31:09 +01002061 conn_reconfig_done(tconn);
2062 out:
2063 drbd_adm_finish(info, retcode);
2064 return 0;
2065}
2066
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002067int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002068{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002069 struct drbd_conf *mdev;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002070 struct net_conf *old_conf, *new_conf = NULL;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002071 struct crypto crypto = { };
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002072 struct drbd_tconn *tconn;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002073 enum drbd_ret_code retcode;
2074 int i;
2075 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002076
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02002077 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002078
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002079 if (!adm_ctx.reply_skb)
2080 return retcode;
2081 if (retcode != NO_ERROR)
2082 goto out;
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002083 if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
2084 drbd_msg_put_info("connection endpoint(s) missing");
2085 retcode = ERR_INVALID_REQUEST;
2086 goto out;
2087 }
2088
2089 /* No need for _rcu here. All reconfiguration is
2090 * strictly serialized on genl_lock(). We are protected against
2091 * concurrent reconfiguration/addition/deletion */
2092 list_for_each_entry(tconn, &drbd_tconns, all_tconn) {
2093 if (nla_len(adm_ctx.my_addr) == tconn->my_addr_len &&
2094 !memcmp(nla_data(adm_ctx.my_addr), &tconn->my_addr, tconn->my_addr_len)) {
2095 retcode = ERR_LOCAL_ADDR;
2096 goto out;
2097 }
2098
2099 if (nla_len(adm_ctx.peer_addr) == tconn->peer_addr_len &&
2100 !memcmp(nla_data(adm_ctx.peer_addr), &tconn->peer_addr, tconn->peer_addr_len)) {
2101 retcode = ERR_PEER_ADDR;
2102 goto out;
2103 }
2104 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002105
2106 tconn = adm_ctx.tconn;
Philipp Reisner80883192011-02-18 14:56:45 +01002107 conn_reconfig_start(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002108
Philipp Reisner80883192011-02-18 14:56:45 +01002109 if (tconn->cstate > C_STANDALONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002110 retcode = ERR_NET_CONFIGURED;
2111 goto fail;
2112 }
2113
Andreas Gruenbachera209b4a2011-08-17 12:43:25 +02002114 /* allocation not in the IO path, drbdsetup / netlink process context */
Lars Ellenberg5979e362011-04-27 21:09:55 +02002115 new_conf = kzalloc(sizeof(*new_conf), GFP_KERNEL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002116 if (!new_conf) {
2117 retcode = ERR_NOMEM;
2118 goto fail;
2119 }
2120
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02002121 set_net_conf_defaults(new_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002122
Lars Ellenbergf3990022011-03-23 14:31:09 +01002123 err = net_conf_from_attrs(new_conf, info);
Lars Ellenberg25e40932011-08-19 10:39:00 +02002124 if (err && err != -ENOMSG) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002125 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002126 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002127 goto fail;
2128 }
2129
Philipp Reisnercd643972011-04-13 18:00:59 -07002130 retcode = check_net_options(tconn, new_conf);
2131 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002132 goto fail;
Philipp Reisner47ff2d02010-06-18 13:56:57 +02002133
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002134 retcode = alloc_crypto(&crypto, new_conf);
2135 if (retcode != NO_ERROR)
Philipp Reisner422028b2010-10-27 11:12:07 +02002136 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002137
2138 ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
2139
Philipp Reisner80883192011-02-18 14:56:45 +01002140 conn_flush_workqueue(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002141
Philipp Reisnera0095502011-05-03 13:14:15 +02002142 mutex_lock(&tconn->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002143 old_conf = tconn->net_conf;
2144 if (old_conf) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002145 retcode = ERR_NET_CONFIGURED;
Philipp Reisnera0095502011-05-03 13:14:15 +02002146 mutex_unlock(&tconn->conf_update);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002147 goto fail;
2148 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002149 rcu_assign_pointer(tconn->net_conf, new_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002150
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002151 conn_free_crypto(tconn);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002152 tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02002153 tconn->integrity_tfm = crypto.integrity_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002154 tconn->csums_tfm = crypto.csums_tfm;
2155 tconn->verify_tfm = crypto.verify_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002156
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002157 tconn->my_addr_len = nla_len(adm_ctx.my_addr);
2158 memcpy(&tconn->my_addr, nla_data(adm_ctx.my_addr), tconn->my_addr_len);
2159 tconn->peer_addr_len = nla_len(adm_ctx.peer_addr);
2160 memcpy(&tconn->peer_addr, nla_data(adm_ctx.peer_addr), tconn->peer_addr_len);
2161
Philipp Reisnera0095502011-05-03 13:14:15 +02002162 mutex_unlock(&tconn->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002163
Philipp Reisner695d08f2011-04-11 22:53:32 -07002164 rcu_read_lock();
Philipp Reisner80883192011-02-18 14:56:45 +01002165 idr_for_each_entry(&tconn->volumes, mdev, i) {
2166 mdev->send_cnt = 0;
2167 mdev->recv_cnt = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002168 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07002169 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002170
Lars Ellenberg5ee743e2011-04-26 16:22:25 +02002171 retcode = conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002172
Philipp Reisner80883192011-02-18 14:56:45 +01002173 conn_reconfig_done(tconn);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002174 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002175 return 0;
2176
2177fail:
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002178 free_crypto(&crypto);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002179 kfree(new_conf);
2180
Philipp Reisner80883192011-02-18 14:56:45 +01002181 conn_reconfig_done(tconn);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002182out:
2183 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002184 return 0;
2185}
2186
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002187static enum drbd_state_rv conn_try_disconnect(struct drbd_tconn *tconn, bool force)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002188{
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002189 enum drbd_state_rv rv;
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002190
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002191 rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
2192 force ? CS_HARD : 0);
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002193
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002194 switch (rv) {
2195 case SS_NOTHING_TO_DO:
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002196 break;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002197 case SS_ALREADY_STANDALONE:
2198 return SS_SUCCESS;
2199 case SS_PRIMARY_NOP:
2200 /* Our state checking code wants to see the peer outdated. */
Philipp Reisner2bd5ed52013-03-27 14:08:40 +01002201 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING, pdsk, D_OUTDATED), 0);
2202
2203 if (rv == SS_OUTDATE_WO_CONN) /* lost connection before graceful disconnect succeeded */
2204 rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_VERBOSE);
2205
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002206 break;
2207 case SS_CW_FAILED_BY_PEER:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002208 /* The peer probably wants to see us outdated. */
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002209 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
2210 disk, D_OUTDATED), 0);
2211 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002212 rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
2213 CS_HARD);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002214 }
2215 break;
2216 default:;
2217 /* no special handling necessary */
2218 }
2219
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002220 if (rv >= SS_SUCCESS) {
2221 enum drbd_state_rv rv2;
2222 /* No one else can reconfigure the network while I am here.
2223 * The state handling only uses drbd_thread_stop_nowait(),
2224 * we want to really wait here until the receiver is no more.
2225 */
2226 drbd_thread_stop(&adm_ctx.tconn->receiver);
2227
2228 /* Race breaker. This additional state change request may be
2229 * necessary, if this was a forced disconnect during a receiver
2230 * restart. We may have "killed" the receiver thread just
2231 * after drbdd_init() returned. Typically, we should be
2232 * C_STANDALONE already, now, and this becomes a no-op.
2233 */
2234 rv2 = conn_request_state(tconn, NS(conn, C_STANDALONE),
2235 CS_VERBOSE | CS_HARD);
2236 if (rv2 < SS_SUCCESS)
2237 conn_err(tconn,
2238 "unexpected rv2=%d in conn_try_disconnect()\n",
2239 rv2);
2240 }
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002241 return rv;
2242}
2243
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002244int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002245{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002246 struct disconnect_parms parms;
2247 struct drbd_tconn *tconn;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002248 enum drbd_state_rv rv;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002249 enum drbd_ret_code retcode;
2250 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002251
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002252 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002253 if (!adm_ctx.reply_skb)
2254 return retcode;
2255 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002256 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002257
2258 tconn = adm_ctx.tconn;
2259 memset(&parms, 0, sizeof(parms));
2260 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002261 err = disconnect_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002262 if (err) {
2263 retcode = ERR_MANDATORY_TAG;
2264 drbd_msg_put_info(from_attrs_err_to_txt(err));
2265 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002266 }
2267 }
2268
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002269 rv = conn_try_disconnect(tconn, parms.force_disconnect);
2270 if (rv < SS_SUCCESS)
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002271 retcode = rv; /* FIXME: Type mismatch. */
2272 else
2273 retcode = NO_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002274 fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002275 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002276 return 0;
2277}
2278
2279void resync_after_online_grow(struct drbd_conf *mdev)
2280{
2281 int iass; /* I am sync source */
2282
2283 dev_info(DEV, "Resync of new storage after online grow\n");
2284 if (mdev->state.role != mdev->state.peer)
2285 iass = (mdev->state.role == R_PRIMARY);
2286 else
Lars Ellenberg427c0432012-08-01 12:43:01 +02002287 iass = test_bit(RESOLVE_CONFLICTS, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002288
2289 if (iass)
2290 drbd_start_resync(mdev, C_SYNC_SOURCE);
2291 else
2292 _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
2293}
2294
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002295int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002296{
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002297 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002298 struct resize_parms rs;
2299 struct drbd_conf *mdev;
2300 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002301 enum determine_dev_size dd;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002302 enum dds_flags ddsf;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002303 sector_t u_size;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002304 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002305
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002306 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2307 if (!adm_ctx.reply_skb)
2308 return retcode;
2309 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002310 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002311
2312 memset(&rs, 0, sizeof(struct resize_parms));
2313 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002314 err = resize_parms_from_attrs(&rs, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002315 if (err) {
2316 retcode = ERR_MANDATORY_TAG;
2317 drbd_msg_put_info(from_attrs_err_to_txt(err));
2318 goto fail;
2319 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002320 }
2321
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002322 mdev = adm_ctx.mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002323 if (mdev->state.conn > C_CONNECTED) {
2324 retcode = ERR_RESIZE_RESYNC;
2325 goto fail;
2326 }
2327
2328 if (mdev->state.role == R_SECONDARY &&
2329 mdev->state.peer == R_SECONDARY) {
2330 retcode = ERR_NO_PRIMARY;
2331 goto fail;
2332 }
2333
2334 if (!get_ldev(mdev)) {
2335 retcode = ERR_NO_DISK;
2336 goto fail;
2337 }
2338
Philipp Reisner31890f42011-01-19 14:12:51 +01002339 if (rs.no_resync && mdev->tconn->agreed_pro_version < 93) {
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002340 retcode = ERR_NEED_APV_93;
Andreas Gruenbacher7b4e4d32011-09-28 22:15:04 +02002341 goto fail_ldev;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002342 }
2343
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002344 rcu_read_lock();
2345 u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
2346 rcu_read_unlock();
2347 if (u_size != (sector_t)rs.resize_size) {
2348 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
2349 if (!new_disk_conf) {
2350 retcode = ERR_NOMEM;
Philipp Reisner9bcd2522011-09-29 13:00:14 +02002351 goto fail_ldev;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002352 }
2353 }
2354
Philipp Reisner087c2492010-03-26 13:49:56 +01002355 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002356 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002357
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002358 if (new_disk_conf) {
2359 mutex_lock(&mdev->tconn->conf_update);
2360 old_disk_conf = mdev->ldev->disk_conf;
2361 *new_disk_conf = *old_disk_conf;
2362 new_disk_conf->disk_size = (sector_t)rs.resize_size;
2363 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
2364 mutex_unlock(&mdev->tconn->conf_update);
2365 synchronize_rcu();
2366 kfree(old_disk_conf);
2367 }
2368
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002369 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
Bart Van Assche24c48302011-05-21 18:32:29 +02002370 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002371 drbd_md_sync(mdev);
2372 put_ldev(mdev);
2373 if (dd == dev_size_error) {
2374 retcode = ERR_NOMEM_BITMAP;
2375 goto fail;
2376 }
2377
Philipp Reisner087c2492010-03-26 13:49:56 +01002378 if (mdev->state.conn == C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002379 if (dd == grew)
2380 set_bit(RESIZE_PENDING, &mdev->flags);
2381
2382 drbd_send_uuids(mdev);
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002383 drbd_send_sizes(mdev, 1, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002384 }
2385
2386 fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002387 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002388 return 0;
Andreas Gruenbacher7b4e4d32011-09-28 22:15:04 +02002389
2390 fail_ldev:
2391 put_ldev(mdev);
2392 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002393}
2394
Lars Ellenbergf3990022011-03-23 14:31:09 +01002395int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002396{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002397 enum drbd_ret_code retcode;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002398 struct drbd_tconn *tconn;
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002399 struct res_opts res_opts;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002400 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002401
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02002402 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002403 if (!adm_ctx.reply_skb)
2404 return retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002405 if (retcode != NO_ERROR)
2406 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002407 tconn = adm_ctx.tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002408
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002409 res_opts = tconn->res_opts;
Lars Ellenberg5979e362011-04-27 21:09:55 +02002410 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02002411 set_res_opts_defaults(&res_opts);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002412
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002413 err = res_opts_from_attrs(&res_opts, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02002414 if (err && err != -ENOMSG) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002415 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002416 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002417 goto fail;
2418 }
2419
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02002420 err = set_resource_options(tconn, &res_opts);
2421 if (err) {
2422 retcode = ERR_INVALID_REQUEST;
2423 if (err == -ENOMEM)
Philipp Reisner778f2712010-07-06 11:14:00 +02002424 retcode = ERR_NOMEM;
Philipp Reisner778f2712010-07-06 11:14:00 +02002425 }
2426
Philipp Reisnerb411b362009-09-25 16:07:19 -07002427fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002428 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002429 return 0;
2430}
2431
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002432int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002433{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002434 struct drbd_conf *mdev;
2435 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2436
2437 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2438 if (!adm_ctx.reply_skb)
2439 return retcode;
2440 if (retcode != NO_ERROR)
2441 goto out;
2442
2443 mdev = adm_ctx.mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002444
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002445 /* If there is still bitmap IO pending, probably because of a previous
Lars Ellenberg7ee1fb92012-06-19 10:27:58 +02002446 * resync just being finished, wait for it before requesting a new resync.
2447 * Also wait for it's after_state_ch(). */
Lars Ellenberga574daf2012-04-25 16:27:35 +02002448 drbd_suspend_io(mdev);
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002449 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
Lars Ellenberg7ee1fb92012-06-19 10:27:58 +02002450 drbd_flush_workqueue(mdev);
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002451
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002452 /* If we happen to be C_STANDALONE R_SECONDARY, just change to
2453 * D_INCONSISTENT, and set all bits in the bitmap. Otherwise,
2454 * try to start a resync handshake as sync target for full sync.
Philipp Reisner9376d9f2013-03-27 14:08:36 +01002455 */
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002456 if (mdev->state.conn == C_STANDALONE && mdev->state.role == R_SECONDARY) {
2457 retcode = drbd_request_state(mdev, NS(disk, D_INCONSISTENT));
2458 if (retcode >= SS_SUCCESS) {
2459 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write,
2460 "set_n_write from invalidate", BM_LOCKED_MASK))
2461 retcode = ERR_IO_MD_DISK;
2462 }
2463 } else
2464 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
Lars Ellenberga574daf2012-04-25 16:27:35 +02002465 drbd_resume_io(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002466
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002467out:
2468 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002469 return 0;
2470}
2471
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002472static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2473 union drbd_state mask, union drbd_state val)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002474{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002475 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002476
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002477 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2478 if (!adm_ctx.reply_skb)
2479 return retcode;
2480 if (retcode != NO_ERROR)
2481 goto out;
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002482
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002483 retcode = drbd_request_state(adm_ctx.mdev, mask, val);
2484out:
2485 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002486 return 0;
2487}
2488
2489static int drbd_bmio_set_susp_al(struct drbd_conf *mdev)
2490{
2491 int rv;
2492
2493 rv = drbd_bmio_set_n_write(mdev);
2494 drbd_suspend_al(mdev);
2495 return rv;
2496}
2497
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002498int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002499{
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002500 int retcode; /* drbd_ret_code, drbd_state_rv */
2501 struct drbd_conf *mdev;
2502
2503 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2504 if (!adm_ctx.reply_skb)
2505 return retcode;
2506 if (retcode != NO_ERROR)
2507 goto out;
2508
2509 mdev = adm_ctx.mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002510
2511 /* If there is still bitmap IO pending, probably because of a previous
Lars Ellenberg7ee1fb92012-06-19 10:27:58 +02002512 * resync just being finished, wait for it before requesting a new resync.
2513 * Also wait for it's after_state_ch(). */
Lars Ellenberga574daf2012-04-25 16:27:35 +02002514 drbd_suspend_io(mdev);
Lars Ellenberg5016b822012-05-07 12:00:56 +02002515 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
Lars Ellenberg7ee1fb92012-06-19 10:27:58 +02002516 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002517
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002518 /* If we happen to be C_STANDALONE R_PRIMARY, just set all bits
2519 * in the bitmap. Otherwise, try to start a resync handshake
2520 * as sync source for full sync.
2521 */
2522 if (mdev->state.conn == C_STANDALONE && mdev->state.role == R_PRIMARY) {
2523 /* The peer will get a resync upon connect anyways. Just make that
2524 into a full resync. */
2525 retcode = drbd_request_state(mdev, NS(pdsk, D_INCONSISTENT));
2526 if (retcode >= SS_SUCCESS) {
2527 if (drbd_bitmap_io(mdev, &drbd_bmio_set_susp_al,
2528 "set_n_write from invalidate_peer",
2529 BM_LOCKED_SET_ALLOWED))
2530 retcode = ERR_IO_MD_DISK;
2531 }
2532 } else
2533 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S));
Lars Ellenberga574daf2012-04-25 16:27:35 +02002534 drbd_resume_io(mdev);
Philipp Reisner07782862010-08-31 12:00:50 +02002535
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002536out:
2537 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002538 return 0;
2539}
2540
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002541int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002542{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002543 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002544
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002545 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2546 if (!adm_ctx.reply_skb)
2547 return retcode;
2548 if (retcode != NO_ERROR)
2549 goto out;
2550
2551 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002552 retcode = ERR_PAUSE_IS_SET;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002553out:
2554 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002555 return 0;
2556}
2557
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002558int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002559{
Philipp Reisnerda9fbc22011-03-29 10:52:01 +02002560 union drbd_dev_state s;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002561 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002562
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002563 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2564 if (!adm_ctx.reply_skb)
2565 return retcode;
2566 if (retcode != NO_ERROR)
2567 goto out;
2568
2569 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2570 s = adm_ctx.mdev->state;
Philipp Reisnercd88d032011-01-20 11:46:41 +01002571 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2572 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2573 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2574 } else {
2575 retcode = ERR_PAUSE_IS_CLEAR;
2576 }
2577 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002578
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002579out:
2580 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002581 return 0;
2582}
2583
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002584int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002585{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002586 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002587}
2588
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002589int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002590{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002591 struct drbd_conf *mdev;
2592 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2593
2594 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2595 if (!adm_ctx.reply_skb)
2596 return retcode;
2597 if (retcode != NO_ERROR)
2598 goto out;
2599
2600 mdev = adm_ctx.mdev;
Philipp Reisner43a51822010-06-11 11:26:34 +02002601 if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
2602 drbd_uuid_new_current(mdev);
2603 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner43a51822010-06-11 11:26:34 +02002604 }
Philipp Reisner265be2d2010-05-31 10:14:17 +02002605 drbd_suspend_io(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002606 retcode = drbd_request_state(mdev, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
2607 if (retcode == SS_SUCCESS) {
Philipp Reisner265be2d2010-05-31 10:14:17 +02002608 if (mdev->state.conn < C_CONNECTED)
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01002609 tl_clear(mdev->tconn);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002610 if (mdev->state.disk == D_DISKLESS || mdev->state.disk == D_FAILED)
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01002611 tl_restart(mdev->tconn, FAIL_FROZEN_DISK_IO);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002612 }
2613 drbd_resume_io(mdev);
2614
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002615out:
2616 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002617 return 0;
2618}
2619
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002620int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002621{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002622 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002623}
2624
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002625int nla_put_drbd_cfg_context(struct sk_buff *skb, struct drbd_tconn *tconn, unsigned vnr)
Lars Ellenberg543cc102011-03-10 22:18:18 +01002626{
2627 struct nlattr *nla;
2628 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
2629 if (!nla)
2630 goto nla_put_failure;
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002631 if (vnr != VOLUME_UNSPECIFIED &&
2632 nla_put_u32(skb, T_ctx_volume, vnr))
2633 goto nla_put_failure;
2634 if (nla_put_string(skb, T_ctx_resource_name, tconn->name))
2635 goto nla_put_failure;
2636 if (tconn->my_addr_len &&
2637 nla_put(skb, T_ctx_my_addr, tconn->my_addr_len, &tconn->my_addr))
2638 goto nla_put_failure;
2639 if (tconn->peer_addr_len &&
2640 nla_put(skb, T_ctx_peer_addr, tconn->peer_addr_len, &tconn->peer_addr))
2641 goto nla_put_failure;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002642 nla_nest_end(skb, nla);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002643 return 0;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002644
2645nla_put_failure:
2646 if (nla)
2647 nla_nest_cancel(skb, nla);
2648 return -EMSGSIZE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002649}
2650
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002651int nla_put_status_info(struct sk_buff *skb, struct drbd_conf *mdev,
2652 const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002653{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002654 struct state_info *si = NULL; /* for sizeof(si->member); */
Philipp Reisner44ed1672011-04-19 17:10:19 +02002655 struct net_conf *nc;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002656 struct nlattr *nla;
2657 int got_ldev;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002658 int err = 0;
2659 int exclude_sensitive;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002660
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002661 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2662 * to. So we better exclude_sensitive information.
2663 *
2664 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2665 * in the context of the requesting user process. Exclude sensitive
2666 * information, unless current has superuser.
2667 *
2668 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2669 * relies on the current implementation of netlink_dump(), which
2670 * executes the dump callback successively from netlink_recvmsg(),
2671 * always in the context of the receiving process */
2672 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002673
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002674 got_ldev = get_ldev(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002675
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002676 /* We need to add connection name and volume number information still.
2677 * Minor number is in drbd_genlmsghdr. */
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002678 if (nla_put_drbd_cfg_context(skb, mdev->tconn, mdev->vnr))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002679 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002680
Lars Ellenbergf3990022011-03-23 14:31:09 +01002681 if (res_opts_to_skb(skb, &mdev->tconn->res_opts, exclude_sensitive))
2682 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002683
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002684 rcu_read_lock();
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002685 if (got_ldev)
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002686 if (disk_conf_to_skb(skb, rcu_dereference(mdev->ldev->disk_conf), exclude_sensitive))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002687 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002688
Philipp Reisner44ed1672011-04-19 17:10:19 +02002689 nc = rcu_dereference(mdev->tconn->net_conf);
2690 if (nc)
2691 err = net_conf_to_skb(skb, nc, exclude_sensitive);
2692 rcu_read_unlock();
2693 if (err)
2694 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002695
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002696 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
2697 if (!nla)
2698 goto nla_put_failure;
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002699 if (nla_put_u32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY) ||
2700 nla_put_u32(skb, T_current_state, mdev->state.i) ||
2701 nla_put_u64(skb, T_ed_uuid, mdev->ed_uuid) ||
Philipp Marek3174f8c2012-03-03 21:04:30 +01002702 nla_put_u64(skb, T_capacity, drbd_get_capacity(mdev->this_bdev)) ||
2703 nla_put_u64(skb, T_send_cnt, mdev->send_cnt) ||
2704 nla_put_u64(skb, T_recv_cnt, mdev->recv_cnt) ||
2705 nla_put_u64(skb, T_read_cnt, mdev->read_cnt) ||
2706 nla_put_u64(skb, T_writ_cnt, mdev->writ_cnt) ||
2707 nla_put_u64(skb, T_al_writ_cnt, mdev->al_writ_cnt) ||
2708 nla_put_u64(skb, T_bm_writ_cnt, mdev->bm_writ_cnt) ||
2709 nla_put_u32(skb, T_ap_bio_cnt, atomic_read(&mdev->ap_bio_cnt)) ||
2710 nla_put_u32(skb, T_ap_pending_cnt, atomic_read(&mdev->ap_pending_cnt)) ||
2711 nla_put_u32(skb, T_rs_pending_cnt, atomic_read(&mdev->rs_pending_cnt)))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002712 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002713
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002714 if (got_ldev) {
Philipp Reisner39a1aa7f2012-08-08 21:19:09 +02002715 int err;
2716
2717 spin_lock_irq(&mdev->ldev->md.uuid_lock);
2718 err = nla_put(skb, T_uuids, sizeof(si->uuids), mdev->ldev->md.uuid);
2719 spin_unlock_irq(&mdev->ldev->md.uuid_lock);
2720
2721 if (err)
2722 goto nla_put_failure;
2723
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002724 if (nla_put_u32(skb, T_disk_flags, mdev->ldev->md.flags) ||
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002725 nla_put_u64(skb, T_bits_total, drbd_bm_bits(mdev)) ||
2726 nla_put_u64(skb, T_bits_oos, drbd_bm_total_weight(mdev)))
2727 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002728 if (C_SYNC_SOURCE <= mdev->state.conn &&
2729 C_PAUSED_SYNC_T >= mdev->state.conn) {
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002730 if (nla_put_u64(skb, T_bits_rs_total, mdev->rs_total) ||
2731 nla_put_u64(skb, T_bits_rs_failed, mdev->rs_failed))
2732 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002733 }
2734 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002735
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002736 if (sib) {
2737 switch(sib->sib_reason) {
2738 case SIB_SYNC_PROGRESS:
2739 case SIB_GET_STATUS_REPLY:
2740 break;
2741 case SIB_STATE_CHANGE:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002742 if (nla_put_u32(skb, T_prev_state, sib->os.i) ||
2743 nla_put_u32(skb, T_new_state, sib->ns.i))
2744 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002745 break;
2746 case SIB_HELPER_POST:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002747 if (nla_put_u32(skb, T_helper_exit_code,
2748 sib->helper_exit_code))
2749 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002750 /* fall through */
2751 case SIB_HELPER_PRE:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002752 if (nla_put_string(skb, T_helper, sib->helper_name))
2753 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002754 break;
2755 }
2756 }
2757 nla_nest_end(skb, nla);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002758
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002759 if (0)
2760nla_put_failure:
2761 err = -EMSGSIZE;
2762 if (got_ldev)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002763 put_ldev(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002764 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002765}
2766
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002767int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002768{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002769 enum drbd_ret_code retcode;
2770 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002771
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002772 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2773 if (!adm_ctx.reply_skb)
2774 return retcode;
2775 if (retcode != NO_ERROR)
2776 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002777
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002778 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.mdev, NULL);
2779 if (err) {
2780 nlmsg_free(adm_ctx.reply_skb);
2781 return err;
2782 }
2783out:
2784 drbd_adm_finish(info, retcode);
2785 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002786}
2787
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002788int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002789{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002790 struct drbd_conf *mdev;
2791 struct drbd_genlmsghdr *dh;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002792 struct drbd_tconn *pos = (struct drbd_tconn*)cb->args[0];
2793 struct drbd_tconn *tconn = NULL;
2794 struct drbd_tconn *tmp;
2795 unsigned volume = cb->args[1];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002796
Lars Ellenberg543cc102011-03-10 22:18:18 +01002797 /* Open coded, deferred, iteration:
2798 * list_for_each_entry_safe(tconn, tmp, &drbd_tconns, all_tconn) {
2799 * idr_for_each_entry(&tconn->volumes, mdev, i) {
2800 * ...
2801 * }
2802 * }
2803 * where tconn is cb->args[0];
2804 * and i is cb->args[1];
2805 *
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002806 * cb->args[2] indicates if we shall loop over all resources,
2807 * or just dump all volumes of a single resource.
2808 *
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002809 * This may miss entries inserted after this dump started,
2810 * or entries deleted before they are reached.
Lars Ellenberg543cc102011-03-10 22:18:18 +01002811 *
2812 * We need to make sure the mdev won't disappear while
2813 * we are looking at it, and revalidate our iterators
2814 * on each iteration.
2815 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002816
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02002817 /* synchronize with conn_create()/conn_destroy() */
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02002818 rcu_read_lock();
Lars Ellenberg543cc102011-03-10 22:18:18 +01002819 /* revalidate iterator position */
Philipp Reisnerec0bddb2011-05-04 15:47:01 +02002820 list_for_each_entry_rcu(tmp, &drbd_tconns, all_tconn) {
Lars Ellenberg543cc102011-03-10 22:18:18 +01002821 if (pos == NULL) {
2822 /* first iteration */
2823 pos = tmp;
2824 tconn = pos;
2825 break;
2826 }
2827 if (tmp == pos) {
2828 tconn = pos;
2829 break;
2830 }
2831 }
2832 if (tconn) {
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002833next_tconn:
Lars Ellenberg543cc102011-03-10 22:18:18 +01002834 mdev = idr_get_next(&tconn->volumes, &volume);
2835 if (!mdev) {
2836 /* No more volumes to dump on this tconn.
2837 * Advance tconn iterator. */
Philipp Reisnerec0bddb2011-05-04 15:47:01 +02002838 pos = list_entry_rcu(tconn->all_tconn.next,
2839 struct drbd_tconn, all_tconn);
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002840 /* Did we dump any volume on this tconn yet? */
Lars Ellenberg543cc102011-03-10 22:18:18 +01002841 if (volume != 0) {
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002842 /* If we reached the end of the list,
2843 * or only a single resource dump was requested,
2844 * we are done. */
2845 if (&pos->all_tconn == &drbd_tconns || cb->args[2])
2846 goto out;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002847 volume = 0;
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002848 tconn = pos;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002849 goto next_tconn;
2850 }
2851 }
2852
Philipp Reisner98683652012-11-09 14:18:43 +01002853 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002854 cb->nlh->nlmsg_seq, &drbd_genl_family,
2855 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
2856 if (!dh)
Lars Ellenberg543cc102011-03-10 22:18:18 +01002857 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002858
Lars Ellenberg543cc102011-03-10 22:18:18 +01002859 if (!mdev) {
Lars Ellenberg367d675d2011-07-11 23:49:55 +02002860 /* This is a tconn without a single volume.
2861 * Suprisingly enough, it may have a network
2862 * configuration. */
2863 struct net_conf *nc;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002864 dh->minor = -1U;
2865 dh->ret_code = NO_ERROR;
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002866 if (nla_put_drbd_cfg_context(skb, tconn, VOLUME_UNSPECIFIED))
Lars Ellenberg367d675d2011-07-11 23:49:55 +02002867 goto cancel;
2868 nc = rcu_dereference(tconn->net_conf);
2869 if (nc && net_conf_to_skb(skb, nc, 1) != 0)
2870 goto cancel;
2871 goto done;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002872 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002873
Lars Ellenberg543cc102011-03-10 22:18:18 +01002874 D_ASSERT(mdev->vnr == volume);
2875 D_ASSERT(mdev->tconn == tconn);
2876
2877 dh->minor = mdev_to_minor(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002878 dh->ret_code = NO_ERROR;
2879
2880 if (nla_put_status_info(skb, mdev, NULL)) {
Lars Ellenberg367d675d2011-07-11 23:49:55 +02002881cancel:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002882 genlmsg_cancel(skb, dh);
Lars Ellenberg543cc102011-03-10 22:18:18 +01002883 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002884 }
Lars Ellenberg367d675d2011-07-11 23:49:55 +02002885done:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002886 genlmsg_end(skb, dh);
2887 }
2888
Lars Ellenberg543cc102011-03-10 22:18:18 +01002889out:
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02002890 rcu_read_unlock();
Lars Ellenberg543cc102011-03-10 22:18:18 +01002891 /* where to start the next iteration */
2892 cb->args[0] = (long)pos;
2893 cb->args[1] = (pos == tconn) ? volume + 1 : 0;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002894
Lars Ellenberg543cc102011-03-10 22:18:18 +01002895 /* No more tconns/volumes/minors found results in an empty skb.
2896 * Which will terminate the dump. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002897 return skb->len;
2898}
2899
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002900/*
2901 * Request status of all resources, or of all volumes within a single resource.
2902 *
2903 * This is a dump, as the answer may not fit in a single reply skb otherwise.
2904 * Which means we cannot use the family->attrbuf or other such members, because
2905 * dump is NOT protected by the genl_lock(). During dump, we only have access
2906 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
2907 *
2908 * Once things are setup properly, we call into get_one_status().
Philipp Reisnerb411b362009-09-25 16:07:19 -07002909 */
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002910int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002911{
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002912 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
2913 struct nlattr *nla;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02002914 const char *resource_name;
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002915 struct drbd_tconn *tconn;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02002916 int maxtype;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002917
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002918 /* Is this a followup call? */
2919 if (cb->args[0]) {
2920 /* ... of a single resource dump,
2921 * and the resource iterator has been advanced already? */
2922 if (cb->args[2] && cb->args[2] != cb->args[0])
2923 return 0; /* DONE. */
2924 goto dump;
2925 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002926
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002927 /* First call (from netlink_dump_start). We need to figure out
2928 * which resource(s) the user wants us to dump. */
2929 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
2930 nlmsg_attrlen(cb->nlh, hdrlen),
2931 DRBD_NLA_CFG_CONTEXT);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002932
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002933 /* No explicit context given. Dump all. */
2934 if (!nla)
2935 goto dump;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02002936 maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
2937 nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
2938 if (IS_ERR(nla))
2939 return PTR_ERR(nla);
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002940 /* context given, but no name present? */
2941 if (!nla)
2942 return -EINVAL;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02002943 resource_name = nla_data(nla);
2944 tconn = conn_get_by_name(resource_name);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002945
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002946 if (!tconn)
2947 return -ENODEV;
2948
Philipp Reisner0ace9df2011-04-24 10:53:19 +02002949 kref_put(&tconn->kref, &conn_destroy); /* get_one_status() (re)validates tconn by itself */
2950
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002951 /* prime iterators, and set "filter" mode mark:
2952 * only dump this tconn. */
2953 cb->args[0] = (long)tconn;
2954 /* cb->args[1] = 0; passed in this way. */
2955 cb->args[2] = (long)tconn;
2956
2957dump:
2958 return get_one_status(skb, cb);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002959}
2960
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002961int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002962{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002963 enum drbd_ret_code retcode;
2964 struct timeout_parms tp;
2965 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002966
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002967 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2968 if (!adm_ctx.reply_skb)
2969 return retcode;
2970 if (retcode != NO_ERROR)
2971 goto out;
2972
2973 tp.timeout_type =
2974 adm_ctx.mdev->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
2975 test_bit(USE_DEGR_WFC_T, &adm_ctx.mdev->flags) ? UT_DEGRADED :
2976 UT_DEFAULT;
2977
2978 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
2979 if (err) {
2980 nlmsg_free(adm_ctx.reply_skb);
2981 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002982 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002983out:
2984 drbd_adm_finish(info, retcode);
2985 return 0;
2986}
Lars Ellenberg873b0d52011-01-21 22:53:48 +01002987
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002988int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
2989{
2990 struct drbd_conf *mdev;
2991 enum drbd_ret_code retcode;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02002992 struct start_ov_parms parms;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002993
2994 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2995 if (!adm_ctx.reply_skb)
2996 return retcode;
2997 if (retcode != NO_ERROR)
2998 goto out;
2999
3000 mdev = adm_ctx.mdev;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003001
3002 /* resume from last known position, if possible */
3003 parms.ov_start_sector = mdev->ov_start_sector;
3004 parms.ov_stop_sector = ULLONG_MAX;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003005 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01003006 int err = start_ov_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003007 if (err) {
3008 retcode = ERR_MANDATORY_TAG;
3009 drbd_msg_put_info(from_attrs_err_to_txt(err));
3010 goto out;
3011 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003012 }
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003013 /* w_make_ov_request expects position to be aligned */
3014 mdev->ov_start_sector = parms.ov_start_sector & ~(BM_SECT_PER_BIT-1);
3015 mdev->ov_stop_sector = parms.ov_stop_sector;
Lars Ellenberg873b0d52011-01-21 22:53:48 +01003016
3017 /* If there is still bitmap IO pending, e.g. previous resync or verify
3018 * just being finished, wait for it before requesting a new resync. */
Lars Ellenberga574daf2012-04-25 16:27:35 +02003019 drbd_suspend_io(mdev);
Lars Ellenberg873b0d52011-01-21 22:53:48 +01003020 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003021 retcode = drbd_request_state(mdev,NS(conn,C_VERIFY_S));
Lars Ellenberga574daf2012-04-25 16:27:35 +02003022 drbd_resume_io(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003023out:
3024 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003025 return 0;
3026}
3027
3028
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003029int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003030{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003031 struct drbd_conf *mdev;
3032 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003033 int skip_initial_sync = 0;
3034 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003035 struct new_c_uuid_parms args;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003036
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003037 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3038 if (!adm_ctx.reply_skb)
3039 return retcode;
3040 if (retcode != NO_ERROR)
3041 goto out_nolock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003042
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003043 mdev = adm_ctx.mdev;
3044 memset(&args, 0, sizeof(args));
3045 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01003046 err = new_c_uuid_parms_from_attrs(&args, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003047 if (err) {
3048 retcode = ERR_MANDATORY_TAG;
3049 drbd_msg_put_info(from_attrs_err_to_txt(err));
3050 goto out_nolock;
3051 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003052 }
3053
Philipp Reisner8410da82011-02-11 20:11:10 +01003054 mutex_lock(mdev->state_mutex); /* Protects us against serialized state changes. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003055
3056 if (!get_ldev(mdev)) {
3057 retcode = ERR_NO_DISK;
3058 goto out;
3059 }
3060
3061 /* this is "skip initial sync", assume to be clean */
Philipp Reisner31890f42011-01-19 14:12:51 +01003062 if (mdev->state.conn == C_CONNECTED && mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003063 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
3064 dev_info(DEV, "Preparing to skip initial sync\n");
3065 skip_initial_sync = 1;
3066 } else if (mdev->state.conn != C_STANDALONE) {
3067 retcode = ERR_CONNECTED;
3068 goto out_dec;
3069 }
3070
3071 drbd_uuid_set(mdev, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
3072 drbd_uuid_new_current(mdev); /* New current, previous to UI_BITMAP */
3073
3074 if (args.clear_bm) {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003075 err = drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
3076 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003077 if (err) {
3078 dev_err(DEV, "Writing bitmap failed with %d\n",err);
3079 retcode = ERR_IO_MD_DISK;
3080 }
3081 if (skip_initial_sync) {
3082 drbd_send_uuids_skip_initial_sync(mdev);
3083 _drbd_uuid_set(mdev, UI_BITMAP, 0);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003084 drbd_print_uuids(mdev, "cleared bitmap UUID");
Philipp Reisner87eeee42011-01-19 14:16:30 +01003085 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003086 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3087 CS_VERBOSE, NULL);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003088 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003089 }
3090 }
3091
3092 drbd_md_sync(mdev);
3093out_dec:
3094 put_ldev(mdev);
3095out:
Philipp Reisner8410da82011-02-11 20:11:10 +01003096 mutex_unlock(mdev->state_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003097out_nolock:
3098 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003099 return 0;
3100}
3101
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003102static enum drbd_ret_code
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003103drbd_check_resource_name(const char *name)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003104{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003105 if (!name || !name[0]) {
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003106 drbd_msg_put_info("resource name missing");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003107 return ERR_MANDATORY_TAG;
3108 }
3109 /* if we want to use these in sysfs/configfs/debugfs some day,
3110 * we must not allow slashes */
3111 if (strchr(name, '/')) {
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003112 drbd_msg_put_info("invalid resource name");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003113 return ERR_INVALID_REQUEST;
3114 }
3115 return NO_ERROR;
3116}
Philipp Reisnerb411b362009-09-25 16:07:19 -07003117
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003118int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003119{
3120 enum drbd_ret_code retcode;
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003121 struct res_opts res_opts;
3122 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003123
3124 retcode = drbd_adm_prepare(skb, info, 0);
3125 if (!adm_ctx.reply_skb)
3126 return retcode;
3127 if (retcode != NO_ERROR)
3128 goto out;
3129
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003130 set_res_opts_defaults(&res_opts);
3131 err = res_opts_from_attrs(&res_opts, info);
3132 if (err && err != -ENOMSG) {
3133 retcode = ERR_MANDATORY_TAG;
3134 drbd_msg_put_info(from_attrs_err_to_txt(err));
3135 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003136 }
3137
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003138 retcode = drbd_check_resource_name(adm_ctx.resource_name);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003139 if (retcode != NO_ERROR)
3140 goto out;
3141
3142 if (adm_ctx.tconn) {
Lars Ellenberg38f19612011-03-14 13:22:35 +01003143 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
3144 retcode = ERR_INVALID_REQUEST;
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003145 drbd_msg_put_info("resource exists");
Lars Ellenberg38f19612011-03-14 13:22:35 +01003146 }
3147 /* else: still NO_ERROR */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003148 goto out;
Philipp Reisner9f5180e2009-10-06 09:30:14 +02003149 }
3150
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003151 if (!conn_create(adm_ctx.resource_name, &res_opts))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003152 retcode = ERR_NOMEM;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003153out:
3154 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003155 return 0;
3156}
3157
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003158int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003159{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003160 struct drbd_genlmsghdr *dh = info->userhdr;
3161 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003162
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02003163 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003164 if (!adm_ctx.reply_skb)
3165 return retcode;
3166 if (retcode != NO_ERROR)
3167 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003168
Andreas Gruenbacherf2257a52011-07-14 16:00:40 +02003169 if (dh->minor > MINORMASK) {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003170 drbd_msg_put_info("requested minor out of range");
3171 retcode = ERR_INVALID_REQUEST;
3172 goto out;
3173 }
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +02003174 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003175 drbd_msg_put_info("requested volume id out of range");
3176 retcode = ERR_INVALID_REQUEST;
3177 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05003178 }
3179
Lars Ellenberg38f19612011-03-14 13:22:35 +01003180 /* drbd_adm_prepare made sure already
3181 * that mdev->tconn and mdev->vnr match the request. */
3182 if (adm_ctx.mdev) {
3183 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
3184 retcode = ERR_MINOR_EXISTS;
3185 /* else: still NO_ERROR */
3186 goto out;
3187 }
3188
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003189 retcode = conn_new_minor(adm_ctx.tconn, dh->minor, adm_ctx.volume);
3190out:
3191 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003192 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003193}
3194
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003195static enum drbd_ret_code adm_delete_minor(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003196{
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003197 if (mdev->state.disk == D_DISKLESS &&
3198 /* no need to be mdev->state.conn == C_STANDALONE &&
3199 * we may want to delete a minor from a live replication group.
3200 */
3201 mdev->state.role == R_SECONDARY) {
Philipp Reisner369bea62011-07-06 23:04:44 +02003202 _drbd_request_state(mdev, NS(conn, C_WF_REPORT_PARAMS),
3203 CS_VERBOSE + CS_WAIT_COMPLETE);
Philipp Reisner81fa2e62011-05-04 15:10:30 +02003204 idr_remove(&mdev->tconn->volumes, mdev->vnr);
3205 idr_remove(&minors, mdev_to_minor(mdev));
Lars Ellenberg113fef92013-03-22 18:14:40 -06003206 destroy_workqueue(mdev->submit.wq);
Philipp Reisner81fa2e62011-05-04 15:10:30 +02003207 del_gendisk(mdev->vdisk);
3208 synchronize_rcu();
3209 kref_put(&mdev->kref, &drbd_minor_destroy);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003210 return NO_ERROR;
3211 } else
3212 return ERR_MINOR_CONFIGURED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003213}
3214
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003215int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05003216{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003217 enum drbd_ret_code retcode;
3218
3219 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3220 if (!adm_ctx.reply_skb)
3221 return retcode;
3222 if (retcode != NO_ERROR)
3223 goto out;
3224
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003225 retcode = adm_delete_minor(adm_ctx.mdev);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003226out:
3227 drbd_adm_finish(info, retcode);
3228 return 0;
3229}
3230
3231int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
3232{
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003233 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003234 struct drbd_conf *mdev;
3235 unsigned i;
3236
3237 retcode = drbd_adm_prepare(skb, info, 0);
3238 if (!adm_ctx.reply_skb)
3239 return retcode;
3240 if (retcode != NO_ERROR)
3241 goto out;
3242
3243 if (!adm_ctx.tconn) {
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003244 retcode = ERR_RES_NOT_KNOWN;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003245 goto out;
3246 }
3247
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003248 /* demote */
3249 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3250 retcode = drbd_set_role(mdev, R_SECONDARY, 0);
3251 if (retcode < SS_SUCCESS) {
3252 drbd_msg_put_info("failed to demote");
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003253 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003254 }
3255 }
3256
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003257 retcode = conn_try_disconnect(adm_ctx.tconn, 0);
3258 if (retcode < SS_SUCCESS) {
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003259 drbd_msg_put_info("failed to disconnect");
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003260 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003261 }
3262
3263 /* detach */
3264 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
Philipp Reisnercdfda632011-07-05 15:38:59 +02003265 retcode = adm_detach(mdev, 0);
Lars Ellenberg27012382012-07-24 10:13:55 +02003266 if (retcode < SS_SUCCESS || retcode > NO_ERROR) {
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003267 drbd_msg_put_info("failed to detach");
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003268 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003269 }
3270 }
3271
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003272 /* If we reach this, all volumes (of this tconn) are Secondary,
3273 * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003274 * actually stopped, state handling only does drbd_thread_stop_nowait(). */
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003275 drbd_thread_stop(&adm_ctx.tconn->worker);
3276
3277 /* Now, nothing can fail anymore */
3278
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003279 /* delete volumes */
3280 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3281 retcode = adm_delete_minor(mdev);
3282 if (retcode != NO_ERROR) {
3283 /* "can not happen" */
3284 drbd_msg_put_info("failed to delete volume");
Philipp Reisneref356262011-04-13 14:21:29 -07003285 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003286 }
3287 }
3288
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003289 /* delete connection */
3290 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
Philipp Reisnerec0bddb2011-05-04 15:47:01 +02003291 list_del_rcu(&adm_ctx.tconn->all_tconn);
3292 synchronize_rcu();
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02003293 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
3294
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003295 retcode = NO_ERROR;
3296 } else {
3297 /* "can not happen" */
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003298 retcode = ERR_RES_IN_USE;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003299 drbd_msg_put_info("failed to delete connection");
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003300 }
Philipp Reisneref356262011-04-13 14:21:29 -07003301 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003302out:
3303 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003304 return 0;
3305}
3306
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003307int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05003308{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003309 enum drbd_ret_code retcode;
3310
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02003311 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003312 if (!adm_ctx.reply_skb)
3313 return retcode;
3314 if (retcode != NO_ERROR)
3315 goto out;
3316
3317 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
Philipp Reisnerec0bddb2011-05-04 15:47:01 +02003318 list_del_rcu(&adm_ctx.tconn->all_tconn);
3319 synchronize_rcu();
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02003320 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
3321
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003322 retcode = NO_ERROR;
Philipp Reisner774b3052011-02-22 02:07:03 -05003323 } else {
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003324 retcode = ERR_RES_IN_USE;
Philipp Reisner774b3052011-02-22 02:07:03 -05003325 }
3326
Lars Ellenberg992d6e92011-05-02 11:47:18 +02003327 if (retcode == NO_ERROR)
3328 drbd_thread_stop(&adm_ctx.tconn->worker);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003329out:
3330 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003331 return 0;
3332}
3333
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003334void drbd_bcast_event(struct drbd_conf *mdev, const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003335{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003336 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
3337 struct sk_buff *msg;
3338 struct drbd_genlmsghdr *d_out;
3339 unsigned seq;
3340 int err = -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003341
Philipp Reisneref86b772012-12-06 10:34:34 +01003342 if (sib->sib_reason == SIB_SYNC_PROGRESS) {
3343 if (time_after(jiffies, mdev->rs_last_bcast + HZ))
3344 mdev->rs_last_bcast = jiffies;
3345 else
3346 return;
3347 }
Philipp Reisner328e0f122012-10-19 14:37:47 +02003348
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003349 seq = atomic_inc_return(&drbd_genl_seq);
3350 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
3351 if (!msg)
3352 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003353
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003354 err = -EMSGSIZE;
3355 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
3356 if (!d_out) /* cannot happen, but anyways. */
3357 goto nla_put_failure;
3358 d_out->minor = mdev_to_minor(mdev);
Andreas Gruenbacher6f9b5f82011-05-06 01:03:32 +02003359 d_out->ret_code = NO_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003360
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003361 if (nla_put_status_info(msg, mdev, sib))
3362 goto nla_put_failure;
3363 genlmsg_end(msg, d_out);
3364 err = drbd_genl_multicast_events(msg, 0);
3365 /* msg has been consumed or freed in netlink_broadcast() */
3366 if (err && err != -ESRCH)
3367 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003368
Philipp Reisnerb411b362009-09-25 16:07:19 -07003369 return;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003370
3371nla_put_failure:
3372 nlmsg_free(msg);
3373failed:
3374 dev_err(DEV, "Error %d while broadcasting event. "
3375 "Event seq:%u sib_reason:%u\n",
3376 err, seq, sib->sib_reason);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003377}