blob: 2af26fc9528083d23cbde63fbe42128a26757599 [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
699/* initializes the md.*_offset members, so we are able to find
700 * the on disk meta data */
701static void drbd_md_set_sector_offsets(struct drbd_conf *mdev,
702 struct drbd_backing_dev *bdev)
703{
704 sector_t md_size_sect = 0;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200705 int meta_dev_idx;
706
707 rcu_read_lock();
708 meta_dev_idx = rcu_dereference(bdev->disk_conf)->meta_dev_idx;
709
710 switch (meta_dev_idx) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700711 default:
712 /* v07 style fixed size indexed meta data */
713 bdev->md.md_size_sect = MD_RESERVED_SECT;
714 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
715 bdev->md.al_offset = MD_AL_OFFSET;
716 bdev->md.bm_offset = MD_BM_OFFSET;
717 break;
718 case DRBD_MD_INDEX_FLEX_EXT:
719 /* just occupy the full device; unit: sectors */
720 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
721 bdev->md.md_offset = 0;
722 bdev->md.al_offset = MD_AL_OFFSET;
723 bdev->md.bm_offset = MD_BM_OFFSET;
724 break;
725 case DRBD_MD_INDEX_INTERNAL:
726 case DRBD_MD_INDEX_FLEX_INT:
727 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
728 /* al size is still fixed */
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100729 bdev->md.al_offset = -MD_AL_SECTORS;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700730 /* we need (slightly less than) ~ this much bitmap sectors: */
731 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
732 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
733 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
734 md_size_sect = ALIGN(md_size_sect, 8);
735
736 /* plus the "drbd meta data super block",
737 * and the activity log; */
738 md_size_sect += MD_BM_OFFSET;
739
740 bdev->md.md_size_sect = md_size_sect;
741 /* bitmap offset is adjusted by 'super' block size */
742 bdev->md.bm_offset = -md_size_sect + MD_AL_OFFSET;
743 break;
744 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200745 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700746}
747
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100748/* input size is expected to be in KB */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700749char *ppsize(char *buf, unsigned long long size)
750{
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100751 /* Needs 9 bytes at max including trailing NUL:
752 * -1ULL ==> "16384 EB" */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700753 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
754 int base = 0;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100755 while (size >= 10000 && base < sizeof(units)-1) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700756 /* shift + round */
757 size = (size >> 10) + !!(size & (1<<9));
758 base++;
759 }
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100760 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700761
762 return buf;
763}
764
765/* there is still a theoretical deadlock when called from receiver
766 * on an D_INCONSISTENT R_PRIMARY:
767 * remote READ does inc_ap_bio, receiver would need to receive answer
768 * packet from remote to dec_ap_bio again.
769 * receiver receive_sizes(), comes here,
770 * waits for ap_bio_cnt == 0. -> deadlock.
771 * but this cannot happen, actually, because:
772 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
773 * (not connected, or bad/no disk on peer):
774 * see drbd_fail_request_early, ap_bio_cnt is zero.
775 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
776 * peer may not initiate a resize.
777 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100778/* Note these are not to be confused with
779 * drbd_adm_suspend_io/drbd_adm_resume_io,
780 * which are (sub) state changes triggered by admin (drbdsetup),
781 * and can be long lived.
782 * This changes an mdev->flag, is triggered by drbd internals,
783 * and should be short-lived. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700784void drbd_suspend_io(struct drbd_conf *mdev)
785{
786 set_bit(SUSPEND_IO, &mdev->flags);
Philipp Reisner2aebfab2011-03-28 16:48:11 +0200787 if (drbd_suspended(mdev))
Philipp Reisner265be2d2010-05-31 10:14:17 +0200788 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700789 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
790}
791
792void drbd_resume_io(struct drbd_conf *mdev)
793{
794 clear_bit(SUSPEND_IO, &mdev->flags);
795 wake_up(&mdev->misc_wait);
796}
797
798/**
799 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
800 * @mdev: DRBD device.
801 *
802 * Returns 0 on success, negative return values indicate errors.
803 * You should call drbd_md_sync() after calling this function.
804 */
Bart Van Assche24c48302011-05-21 18:32:29 +0200805enum 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 -0700806{
807 sector_t prev_first_sect, prev_size; /* previous meta location */
Philipp Reisneref5e44a2011-05-03 13:27:43 +0200808 sector_t la_size, u_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700809 sector_t size;
810 char ppb[10];
811
812 int md_moved, la_size_changed;
813 enum determine_dev_size rv = unchanged;
814
815 /* race:
816 * application request passes inc_ap_bio,
817 * but then cannot get an AL-reference.
818 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
819 *
820 * to avoid that:
821 * Suspend IO right here.
822 * still lock the act_log to not trigger ASSERTs there.
823 */
824 drbd_suspend_io(mdev);
825
826 /* no wait necessary anymore, actually we could assert that */
827 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
828
829 prev_first_sect = drbd_md_first_sector(mdev->ldev);
830 prev_size = mdev->ldev->md.md_size_sect;
831 la_size = mdev->ldev->md.la_size_sect;
832
833 /* TODO: should only be some assert here, not (re)init... */
834 drbd_md_set_sector_offsets(mdev, mdev->ldev);
835
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200836 rcu_read_lock();
837 u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
838 rcu_read_unlock();
Philipp Reisneref5e44a2011-05-03 13:27:43 +0200839 size = drbd_new_dev_size(mdev, mdev->ldev, u_size, flags & DDSF_FORCED);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700840
841 if (drbd_get_capacity(mdev->this_bdev) != size ||
842 drbd_bm_capacity(mdev) != size) {
843 int err;
Philipp Reisner02d9a942010-03-24 16:23:03 +0100844 err = drbd_bm_resize(mdev, size, !(flags & DDSF_NO_RESYNC));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700845 if (unlikely(err)) {
846 /* currently there is only one error: ENOMEM! */
847 size = drbd_bm_capacity(mdev)>>1;
848 if (size == 0) {
849 dev_err(DEV, "OUT OF MEMORY! "
850 "Could not allocate bitmap!\n");
851 } else {
852 dev_err(DEV, "BM resizing failed. "
853 "Leaving size unchanged at size = %lu KB\n",
854 (unsigned long)size);
855 }
856 rv = dev_size_error;
857 }
858 /* racy, see comments above. */
859 drbd_set_my_capacity(mdev, size);
860 mdev->ldev->md.la_size_sect = size;
861 dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
862 (unsigned long long)size>>1);
863 }
864 if (rv == dev_size_error)
865 goto out;
866
867 la_size_changed = (la_size != mdev->ldev->md.la_size_sect);
868
869 md_moved = prev_first_sect != drbd_md_first_sector(mdev->ldev)
870 || prev_size != mdev->ldev->md.md_size_sect;
871
872 if (la_size_changed || md_moved) {
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100873 int err;
874
Philipp Reisnerb411b362009-09-25 16:07:19 -0700875 drbd_al_shrink(mdev); /* All extents inactive. */
876 dev_info(DEV, "Writing the whole bitmap, %s\n",
877 la_size_changed && md_moved ? "size changed and md moved" :
878 la_size_changed ? "size changed" : "md moved");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100879 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
Philipp Reisnerd1aa4d02012-08-08 21:19:09 +0200880 err = drbd_bitmap_io(mdev, md_moved ? &drbd_bm_write_all : &drbd_bm_write,
881 "size changed", BM_LOCKED_MASK);
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100882 if (err) {
883 rv = dev_size_error;
884 goto out;
885 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700886 drbd_md_mark_dirty(mdev);
887 }
888
889 if (size > la_size)
890 rv = grew;
891 if (size < la_size)
892 rv = shrunk;
893out:
894 lc_unlock(mdev->act_log);
895 wake_up(&mdev->al_wait);
896 drbd_resume_io(mdev);
897
898 return rv;
899}
900
901sector_t
Philipp Reisneref5e44a2011-05-03 13:27:43 +0200902drbd_new_dev_size(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
903 sector_t u_size, int assume_peer_has_space)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700904{
905 sector_t p_size = mdev->p_size; /* partner's disk size. */
906 sector_t la_size = bdev->md.la_size_sect; /* last agreed size. */
907 sector_t m_size; /* my size */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700908 sector_t size = 0;
909
910 m_size = drbd_get_max_capacity(bdev);
911
Philipp Reisnera393db62009-12-22 13:35:52 +0100912 if (mdev->state.conn < C_CONNECTED && assume_peer_has_space) {
913 dev_warn(DEV, "Resize while not connected was forced by the user!\n");
914 p_size = m_size;
915 }
916
Philipp Reisnerb411b362009-09-25 16:07:19 -0700917 if (p_size && m_size) {
918 size = min_t(sector_t, p_size, m_size);
919 } else {
920 if (la_size) {
921 size = la_size;
922 if (m_size && m_size < size)
923 size = m_size;
924 if (p_size && p_size < size)
925 size = p_size;
926 } else {
927 if (m_size)
928 size = m_size;
929 if (p_size)
930 size = p_size;
931 }
932 }
933
934 if (size == 0)
935 dev_err(DEV, "Both nodes diskless!\n");
936
937 if (u_size) {
938 if (u_size > size)
939 dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n",
940 (unsigned long)u_size>>1, (unsigned long)size>>1);
941 else
942 size = u_size;
943 }
944
945 return size;
946}
947
948/**
949 * drbd_check_al_size() - Ensures that the AL is of the right size
950 * @mdev: DRBD device.
951 *
952 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
953 * failed, and 0 on success. You should call drbd_md_sync() after you called
954 * this function.
955 */
Lars Ellenbergf3990022011-03-23 14:31:09 +0100956static int drbd_check_al_size(struct drbd_conf *mdev, struct disk_conf *dc)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700957{
958 struct lru_cache *n, *t;
959 struct lc_element *e;
960 unsigned int in_use;
961 int i;
962
Philipp Reisnerb411b362009-09-25 16:07:19 -0700963 if (mdev->act_log &&
Lars Ellenbergf3990022011-03-23 14:31:09 +0100964 mdev->act_log->nr_elements == dc->al_extents)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700965 return 0;
966
967 in_use = 0;
968 t = mdev->act_log;
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100969 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
Lars Ellenbergf3990022011-03-23 14:31:09 +0100970 dc->al_extents, sizeof(struct lc_element), 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700971
972 if (n == NULL) {
973 dev_err(DEV, "Cannot allocate act_log lru!\n");
974 return -ENOMEM;
975 }
976 spin_lock_irq(&mdev->al_lock);
977 if (t) {
978 for (i = 0; i < t->nr_elements; i++) {
979 e = lc_element_by_index(t, i);
980 if (e->refcnt)
981 dev_err(DEV, "refcnt(%d)==%d\n",
982 e->lc_number, e->refcnt);
983 in_use += e->refcnt;
984 }
985 }
986 if (!in_use)
987 mdev->act_log = n;
988 spin_unlock_irq(&mdev->al_lock);
989 if (in_use) {
990 dev_err(DEV, "Activity log still in use!\n");
991 lc_destroy(n);
992 return -EBUSY;
993 } else {
994 if (t)
995 lc_destroy(t);
996 }
997 drbd_md_mark_dirty(mdev); /* we changed mdev->act_log->nr_elemens */
998 return 0;
999}
1000
Philipp Reisner99432fc2011-05-20 16:39:13 +02001001static void drbd_setup_queue_param(struct drbd_conf *mdev, unsigned int max_bio_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001002{
1003 struct request_queue * const q = mdev->rq_queue;
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001004 unsigned int max_hw_sectors = max_bio_size >> 9;
1005 unsigned int max_segments = 0;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001006
1007 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1008 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
1009
1010 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001011 rcu_read_lock();
1012 max_segments = rcu_dereference(mdev->ldev->disk_conf)->max_bio_bvecs;
1013 rcu_read_unlock();
Philipp Reisner99432fc2011-05-20 16:39:13 +02001014 put_ldev(mdev);
1015 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001016
Philipp Reisnerb411b362009-09-25 16:07:19 -07001017 blk_queue_logical_block_size(q, 512);
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001018 blk_queue_max_hw_sectors(q, max_hw_sectors);
1019 /* This is the workaround for "bio would need to, but cannot, be split" */
1020 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
1021 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001022
Philipp Reisner99432fc2011-05-20 16:39:13 +02001023 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1024 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001025
Philipp Reisner99432fc2011-05-20 16:39:13 +02001026 blk_queue_stack_limits(q, b);
1027
1028 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
1029 dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
1030 q->backing_dev_info.ra_pages,
1031 b->backing_dev_info.ra_pages);
1032 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1033 }
1034 put_ldev(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001035 }
1036}
1037
Philipp Reisner99432fc2011-05-20 16:39:13 +02001038void drbd_reconsider_max_bio_size(struct drbd_conf *mdev)
1039{
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001040 unsigned int now, new, local, peer;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001041
1042 now = queue_max_hw_sectors(mdev->rq_queue) << 9;
1043 local = mdev->local_max_bio_size; /* Eventually last known value, from volatile memory */
1044 peer = mdev->peer_max_bio_size; /* Eventually last known value, from meta data */
1045
1046 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1047 local = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
1048 mdev->local_max_bio_size = local;
1049 put_ldev(mdev);
1050 }
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001051 local = min(local, DRBD_MAX_BIO_SIZE);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001052
1053 /* We may ignore peer limits if the peer is modern enough.
1054 Because new from 8.3.8 onwards the peer can use multiple
1055 BIOs for a single peer_request */
1056 if (mdev->state.conn >= C_CONNECTED) {
Philipp Reisner31890f42011-01-19 14:12:51 +01001057 if (mdev->tconn->agreed_pro_version < 94)
Philipp Reisner98683652012-11-09 14:18:43 +01001058 peer = min( mdev->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
Philipp Reisner68093842011-06-30 15:43:06 +02001059 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
Philipp Reisner31890f42011-01-19 14:12:51 +01001060 else if (mdev->tconn->agreed_pro_version == 94)
Philipp Reisner99432fc2011-05-20 16:39:13 +02001061 peer = DRBD_MAX_SIZE_H80_PACKET;
Philipp Reisner2ffca4f2011-06-30 15:43:06 +02001062 else if (mdev->tconn->agreed_pro_version < 100)
1063 peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
1064 else
Philipp Reisner99432fc2011-05-20 16:39:13 +02001065 peer = DRBD_MAX_BIO_SIZE;
1066 }
1067
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001068 new = min(local, peer);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001069
1070 if (mdev->state.role == R_PRIMARY && new < now)
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001071 dev_err(DEV, "ASSERT FAILED new < now; (%u < %u)\n", new, now);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001072
1073 if (new != now)
1074 dev_info(DEV, "max BIO size = %u\n", new);
1075
1076 drbd_setup_queue_param(mdev, new);
1077}
1078
Philipp Reisnera18e9d12011-04-24 11:09:55 +02001079/* Starts the worker thread */
Philipp Reisner0e29d162011-02-18 14:23:11 +01001080static void conn_reconfig_start(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001081{
Philipp Reisner0e29d162011-02-18 14:23:11 +01001082 drbd_thread_start(&tconn->worker);
1083 conn_flush_workqueue(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001084}
1085
Philipp Reisnera18e9d12011-04-24 11:09:55 +02001086/* if still unconfigured, stops worker again. */
Philipp Reisner0e29d162011-02-18 14:23:11 +01001087static void conn_reconfig_done(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001088{
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001089 bool stop_threads;
Philipp Reisner0e29d162011-02-18 14:23:11 +01001090 spin_lock_irq(&tconn->req_lock);
Philipp Reisnere0e16652011-07-11 17:04:23 +02001091 stop_threads = conn_all_vols_unconf(tconn) &&
1092 tconn->cstate == C_STANDALONE;
Philipp Reisner0e29d162011-02-18 14:23:11 +01001093 spin_unlock_irq(&tconn->req_lock);
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001094 if (stop_threads) {
1095 /* asender is implicitly stopped by receiver
Philipp Reisner81fa2e62011-05-04 15:10:30 +02001096 * in conn_disconnect() */
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001097 drbd_thread_stop(&tconn->receiver);
1098 drbd_thread_stop(&tconn->worker);
1099 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001100}
1101
Philipp Reisner07782862010-08-31 12:00:50 +02001102/* Make sure IO is suspended before calling this function(). */
1103static void drbd_suspend_al(struct drbd_conf *mdev)
1104{
1105 int s = 0;
1106
Lars Ellenberg61610422011-02-21 13:20:54 +01001107 if (!lc_try_lock(mdev->act_log)) {
Philipp Reisner07782862010-08-31 12:00:50 +02001108 dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n");
1109 return;
1110 }
1111
Lars Ellenberg61610422011-02-21 13:20:54 +01001112 drbd_al_shrink(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001113 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner07782862010-08-31 12:00:50 +02001114 if (mdev->state.conn < C_CONNECTED)
1115 s = !test_and_set_bit(AL_SUSPENDED, &mdev->flags);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001116 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg61610422011-02-21 13:20:54 +01001117 lc_unlock(mdev->act_log);
Philipp Reisner07782862010-08-31 12:00:50 +02001118
1119 if (s)
1120 dev_info(DEV, "Suspended AL updates\n");
1121}
1122
Lars Ellenberg5979e362011-04-27 21:09:55 +02001123
1124static bool should_set_defaults(struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001125{
Lars Ellenberg5979e362011-04-27 21:09:55 +02001126 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1127 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1128}
1129
Philipp Reisnerd589a212011-05-04 10:06:52 +02001130static void enforce_disk_conf_limits(struct disk_conf *dc)
1131{
1132 if (dc->al_extents < DRBD_AL_EXTENTS_MIN)
1133 dc->al_extents = DRBD_AL_EXTENTS_MIN;
1134 if (dc->al_extents > DRBD_AL_EXTENTS_MAX)
1135 dc->al_extents = DRBD_AL_EXTENTS_MAX;
1136
1137 if (dc->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1138 dc->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
1139}
1140
Lars Ellenbergf3990022011-03-23 14:31:09 +01001141int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1142{
1143 enum drbd_ret_code retcode;
1144 struct drbd_conf *mdev;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001145 struct disk_conf *new_disk_conf, *old_disk_conf;
Philipp Reisner813472c2011-05-03 16:47:02 +02001146 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001147 int err, fifo_size;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001148
1149 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1150 if (!adm_ctx.reply_skb)
1151 return retcode;
1152 if (retcode != NO_ERROR)
1153 goto out;
1154
1155 mdev = adm_ctx.mdev;
1156
1157 /* we also need a disk
1158 * to change the options on */
1159 if (!get_ldev(mdev)) {
1160 retcode = ERR_NO_DISK;
1161 goto out;
1162 }
1163
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001164 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001165 if (!new_disk_conf) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001166 retcode = ERR_NOMEM;
1167 goto fail;
1168 }
1169
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001170 mutex_lock(&mdev->tconn->conf_update);
1171 old_disk_conf = mdev->ldev->disk_conf;
1172 *new_disk_conf = *old_disk_conf;
Lars Ellenberg5979e362011-04-27 21:09:55 +02001173 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02001174 set_disk_conf_defaults(new_disk_conf);
Lars Ellenberg5979e362011-04-27 21:09:55 +02001175
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001176 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02001177 if (err && err != -ENOMSG) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001178 retcode = ERR_MANDATORY_TAG;
1179 drbd_msg_put_info(from_attrs_err_to_txt(err));
1180 }
1181
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001182 if (!expect(new_disk_conf->resync_rate >= 1))
1183 new_disk_conf->resync_rate = 1;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001184
Philipp Reisnerd589a212011-05-04 10:06:52 +02001185 enforce_disk_conf_limits(new_disk_conf);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001186
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001187 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Philipp Reisner9958c852011-05-03 16:19:31 +02001188 if (fifo_size != mdev->rs_plan_s->size) {
Philipp Reisner813472c2011-05-03 16:47:02 +02001189 new_plan = fifo_alloc(fifo_size);
1190 if (!new_plan) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001191 dev_err(DEV, "kmalloc of fifo_buffer failed");
1192 retcode = ERR_NOMEM;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001193 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001194 }
1195 }
1196
Lars Ellenberg0ee98e22012-08-20 14:54:48 +02001197 drbd_suspend_io(mdev);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001198 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
1199 drbd_al_shrink(mdev);
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001200 err = drbd_check_al_size(mdev, new_disk_conf);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001201 lc_unlock(mdev->act_log);
1202 wake_up(&mdev->al_wait);
Lars Ellenberg0ee98e22012-08-20 14:54:48 +02001203 drbd_resume_io(mdev);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001204
1205 if (err) {
1206 retcode = ERR_NOMEM;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001207 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001208 }
1209
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001210 write_lock_irq(&global_state_lock);
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001211 retcode = drbd_resync_after_valid(mdev, new_disk_conf->resync_after);
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001212 if (retcode == NO_ERROR) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001213 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001214 drbd_resync_after_changed(mdev);
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001215 }
1216 write_unlock_irq(&global_state_lock);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001217
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001218 if (retcode != NO_ERROR)
1219 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001220
Philipp Reisner813472c2011-05-03 16:47:02 +02001221 if (new_plan) {
1222 old_plan = mdev->rs_plan_s;
1223 rcu_assign_pointer(mdev->rs_plan_s, new_plan);
Philipp Reisner9958c852011-05-03 16:19:31 +02001224 }
Philipp Reisner9958c852011-05-03 16:19:31 +02001225
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001226 mutex_unlock(&mdev->tconn->conf_update);
Philipp Reisner27eb13e2012-03-30 14:12:15 +02001227
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001228 if (new_disk_conf->al_updates)
Philipp Reisner4035e4c2012-10-01 18:04:12 +02001229 mdev->ldev->md.flags &= ~MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001230 else
1231 mdev->ldev->md.flags |= MDF_AL_DISABLED;
1232
Lars Ellenberg691631c2012-10-26 00:41:50 +02001233 if (new_disk_conf->md_flushes)
1234 clear_bit(MD_NO_FUA, &mdev->flags);
1235 else
1236 set_bit(MD_NO_FUA, &mdev->flags);
1237
Philipp Reisner27eb13e2012-03-30 14:12:15 +02001238 drbd_bump_write_ordering(mdev->tconn, WO_bdev_flush);
1239
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001240 drbd_md_sync(mdev);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001241
1242 if (mdev->state.conn >= C_CONNECTED)
1243 drbd_send_sync_param(mdev);
1244
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001245 synchronize_rcu();
1246 kfree(old_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02001247 kfree(old_plan);
Philipp Reisnercdfda632011-07-05 15:38:59 +02001248 mod_timer(&mdev->request_timer, jiffies + HZ);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001249 goto success;
1250
1251fail_unlock:
1252 mutex_unlock(&mdev->tconn->conf_update);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001253 fail:
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001254 kfree(new_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02001255 kfree(new_plan);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001256success:
1257 put_ldev(mdev);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001258 out:
1259 drbd_adm_finish(info, retcode);
1260 return 0;
1261}
1262
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001263int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001264{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001265 struct drbd_conf *mdev;
1266 int err;
Andreas Gruenbacher116676c2010-12-08 13:33:11 +01001267 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001268 enum determine_dev_size dd;
1269 sector_t max_possible_sectors;
1270 sector_t min_md_device_sectors;
1271 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001272 struct disk_conf *new_disk_conf = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001273 struct block_device *bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001274 struct lru_cache *resync_lru = NULL;
Philipp Reisner9958c852011-05-03 16:19:31 +02001275 struct fifo_buffer *new_plan = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001276 union drbd_state ns, os;
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001277 enum drbd_state_rv rv;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001278 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001279
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001280 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1281 if (!adm_ctx.reply_skb)
1282 return retcode;
1283 if (retcode != NO_ERROR)
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001284 goto finish;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001285
1286 mdev = adm_ctx.mdev;
Philipp Reisner0e29d162011-02-18 14:23:11 +01001287 conn_reconfig_start(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001288
1289 /* if you want to reconfigure, please tear down first */
1290 if (mdev->state.disk > D_DISKLESS) {
1291 retcode = ERR_DISK_CONFIGURED;
1292 goto fail;
1293 }
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001294 /* It may just now have detached because of IO error. Make sure
1295 * drbd_ldev_destroy is done already, we may end up here very fast,
1296 * e.g. if someone calls attach from the on-io-error handler,
1297 * to realize a "hot spare" feature (not that I'd recommend that) */
1298 wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001299
Lars Ellenberg383606e2012-06-14 14:21:32 +02001300 /* make sure there is no leftover from previous force-detach attempts */
Lars Ellenberg0c849662012-07-30 09:07:28 +02001301 clear_bit(FORCE_DETACH, &mdev->flags);
Lars Ellenbergedc9f5e2012-09-27 15:18:21 +02001302 clear_bit(WAS_IO_ERROR, &mdev->flags);
1303 clear_bit(WAS_READ_ERROR, &mdev->flags);
Lars Ellenberg383606e2012-06-14 14:21:32 +02001304
Lars Ellenberg0029d622012-06-14 18:02:52 +02001305 /* and no leftover from previously aborted resync or verify, either */
1306 mdev->rs_total = 0;
1307 mdev->rs_failed = 0;
1308 atomic_set(&mdev->rs_pending_cnt, 0);
1309
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001310 /* allocation not in the IO path, drbdsetup context */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001311 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1312 if (!nbc) {
1313 retcode = ERR_NOMEM;
1314 goto fail;
1315 }
Philipp Reisner9f2247b2012-08-16 14:25:58 +02001316 spin_lock_init(&nbc->md.uuid_lock);
1317
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001318 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
1319 if (!new_disk_conf) {
1320 retcode = ERR_NOMEM;
1321 goto fail;
1322 }
1323 nbc->disk_conf = new_disk_conf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001324
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001325 set_disk_conf_defaults(new_disk_conf);
1326 err = disk_conf_from_attrs(new_disk_conf, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001327 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001328 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001329 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001330 goto fail;
1331 }
1332
Philipp Reisnerd589a212011-05-04 10:06:52 +02001333 enforce_disk_conf_limits(new_disk_conf);
1334
Philipp Reisner9958c852011-05-03 16:19:31 +02001335 new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
1336 if (!new_plan) {
1337 retcode = ERR_NOMEM;
1338 goto fail;
1339 }
1340
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001341 if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001342 retcode = ERR_MD_IDX_INVALID;
1343 goto fail;
1344 }
1345
Philipp Reisner44ed1672011-04-19 17:10:19 +02001346 rcu_read_lock();
1347 nc = rcu_dereference(mdev->tconn->net_conf);
1348 if (nc) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001349 if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02001350 rcu_read_unlock();
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001351 retcode = ERR_STONITH_AND_PROT_A;
1352 goto fail;
1353 }
1354 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02001355 rcu_read_unlock();
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001356
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001357 bdev = blkdev_get_by_path(new_disk_conf->backing_dev,
Tejun Heod4d77622010-11-13 11:55:18 +01001358 FMODE_READ | FMODE_WRITE | FMODE_EXCL, mdev);
Tejun Heoe525fd82010-11-13 11:55:17 +01001359 if (IS_ERR(bdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001360 dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->backing_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001361 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001362 retcode = ERR_OPEN_DISK;
1363 goto fail;
1364 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001365 nbc->backing_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001366
Tejun Heoe525fd82010-11-13 11:55:17 +01001367 /*
1368 * meta_dev_idx >= 0: external fixed size, possibly multiple
1369 * drbd sharing one meta device. TODO in that case, paranoia
1370 * check that [md_bdev, meta_dev_idx] is not yet used by some
1371 * other drbd minor! (if you use drbd.conf + drbdadm, that
1372 * should check it for you already; but if you don't, or
1373 * someone fooled it, we need to double check here)
1374 */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001375 bdev = blkdev_get_by_path(new_disk_conf->meta_dev,
Tejun Heod4d77622010-11-13 11:55:18 +01001376 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001377 (new_disk_conf->meta_dev_idx < 0) ?
Tejun Heod4d77622010-11-13 11:55:18 +01001378 (void *)mdev : (void *)drbd_m_holder);
Tejun Heoe525fd82010-11-13 11:55:17 +01001379 if (IS_ERR(bdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001380 dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->meta_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001381 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001382 retcode = ERR_OPEN_MD_DISK;
1383 goto fail;
1384 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001385 nbc->md_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001386
Tejun Heoe525fd82010-11-13 11:55:17 +01001387 if ((nbc->backing_bdev == nbc->md_bdev) !=
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001388 (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1389 new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001390 retcode = ERR_MD_IDX_INVALID;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001391 goto fail;
1392 }
1393
1394 resync_lru = lc_create("resync", drbd_bm_ext_cache,
Lars Ellenberg46a15bc2011-02-21 13:21:01 +01001395 1, 61, sizeof(struct bm_extent),
Philipp Reisnerb411b362009-09-25 16:07:19 -07001396 offsetof(struct bm_extent, lce));
1397 if (!resync_lru) {
1398 retcode = ERR_NOMEM;
Tejun Heoe525fd82010-11-13 11:55:17 +01001399 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001400 }
1401
1402 /* RT - for drbd_get_max_capacity() DRBD_MD_INDEX_FLEX_INT */
1403 drbd_md_set_sector_offsets(mdev, nbc);
1404
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001405 if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001406 dev_err(DEV, "max capacity %llu smaller than disk size %llu\n",
1407 (unsigned long long) drbd_get_max_capacity(nbc),
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001408 (unsigned long long) new_disk_conf->disk_size);
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001409 retcode = ERR_DISK_TOO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001410 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001411 }
1412
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001413 if (new_disk_conf->meta_dev_idx < 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001414 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1415 /* at least one MB, otherwise it does not make sense */
1416 min_md_device_sectors = (2<<10);
1417 } else {
1418 max_possible_sectors = DRBD_MAX_SECTORS;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001419 min_md_device_sectors = MD_RESERVED_SECT * (new_disk_conf->meta_dev_idx + 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001420 }
1421
Philipp Reisnerb411b362009-09-25 16:07:19 -07001422 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001423 retcode = ERR_MD_DISK_TOO_SMALL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001424 dev_warn(DEV, "refusing attach: md-device too small, "
1425 "at least %llu sectors needed for this meta-disk type\n",
1426 (unsigned long long) min_md_device_sectors);
Tejun Heoe525fd82010-11-13 11:55:17 +01001427 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001428 }
1429
1430 /* Make sure the new disk is big enough
1431 * (we may currently be R_PRIMARY with no local disk...) */
1432 if (drbd_get_max_capacity(nbc) <
1433 drbd_get_capacity(mdev->this_bdev)) {
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001434 retcode = ERR_DISK_TOO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001435 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001436 }
1437
1438 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1439
Lars Ellenberg13529942009-10-12 19:07:49 +02001440 if (nbc->known_size > max_possible_sectors) {
1441 dev_warn(DEV, "==> truncating very big lower level device "
1442 "to currently maximum possible %llu sectors <==\n",
1443 (unsigned long long) max_possible_sectors);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001444 if (new_disk_conf->meta_dev_idx >= 0)
Lars Ellenberg13529942009-10-12 19:07:49 +02001445 dev_warn(DEV, "==>> using internal or flexible "
1446 "meta data may help <<==\n");
1447 }
1448
Philipp Reisnerb411b362009-09-25 16:07:19 -07001449 drbd_suspend_io(mdev);
1450 /* also wait for the last barrier ack. */
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001451 /* FIXME see also https://daiquiri.linbit/cgi-bin/bugzilla/show_bug.cgi?id=171
1452 * We need a way to either ignore barrier acks for barriers sent before a device
1453 * was attached, or a way to wait for all pending barrier acks to come in.
1454 * As barriers are counted per resource,
1455 * we'd need to suspend io on all devices of a resource.
1456 */
Philipp Reisner2aebfab2011-03-28 16:48:11 +02001457 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_pending_cnt) || drbd_suspended(mdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001458 /* and for any other previously queued work */
1459 drbd_flush_workqueue(mdev);
1460
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001461 rv = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE);
1462 retcode = rv; /* FIXME: Type mismatch. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001463 drbd_resume_io(mdev);
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001464 if (rv < SS_SUCCESS)
Tejun Heoe525fd82010-11-13 11:55:17 +01001465 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001466
1467 if (!get_ldev_if_state(mdev, D_ATTACHING))
1468 goto force_diskless;
1469
1470 drbd_md_set_sector_offsets(mdev, nbc);
1471
1472 if (!mdev->bitmap) {
1473 if (drbd_bm_init(mdev)) {
1474 retcode = ERR_NOMEM;
1475 goto force_diskless_dec;
1476 }
1477 }
1478
1479 retcode = drbd_md_read(mdev, nbc);
1480 if (retcode != NO_ERROR)
1481 goto force_diskless_dec;
1482
1483 if (mdev->state.conn < C_CONNECTED &&
1484 mdev->state.role == R_PRIMARY &&
1485 (mdev->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
1486 dev_err(DEV, "Can only attach to data with current UUID=%016llX\n",
1487 (unsigned long long)mdev->ed_uuid);
1488 retcode = ERR_DATA_NOT_CURRENT;
1489 goto force_diskless_dec;
1490 }
1491
1492 /* Since we are diskless, fix the activity log first... */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001493 if (drbd_check_al_size(mdev, new_disk_conf)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001494 retcode = ERR_NOMEM;
1495 goto force_diskless_dec;
1496 }
1497
1498 /* Prevent shrinking of consistent devices ! */
1499 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001500 drbd_new_dev_size(mdev, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001501 dev_warn(DEV, "refusing to truncate a consistent device\n");
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001502 retcode = ERR_DISK_TOO_SMALL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001503 goto force_diskless_dec;
1504 }
1505
Philipp Reisnerb411b362009-09-25 16:07:19 -07001506 /* Reset the "barriers don't work" bits here, then force meta data to
1507 * be written, to ensure we determine if barriers are supported. */
Andreas Gruenbachere5440462011-05-04 15:25:35 +02001508 if (new_disk_conf->md_flushes)
Philipp Reisnera8a4e512010-08-25 10:21:04 +02001509 clear_bit(MD_NO_FUA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001510 else
Andreas Gruenbachere5440462011-05-04 15:25:35 +02001511 set_bit(MD_NO_FUA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001512
1513 /* Point of no return reached.
1514 * Devices and memory are no longer released by error cleanup below.
1515 * now mdev takes over responsibility, and the state engine should
1516 * clean it up somewhere. */
1517 D_ASSERT(mdev->ldev == NULL);
1518 mdev->ldev = nbc;
1519 mdev->resync = resync_lru;
Philipp Reisner9958c852011-05-03 16:19:31 +02001520 mdev->rs_plan_s = new_plan;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001521 nbc = NULL;
1522 resync_lru = NULL;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001523 new_disk_conf = NULL;
Philipp Reisner9958c852011-05-03 16:19:31 +02001524 new_plan = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001525
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001526 drbd_bump_write_ordering(mdev->tconn, WO_bdev_flush);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001527
1528 if (drbd_md_test_flag(mdev->ldev, MDF_CRASHED_PRIMARY))
1529 set_bit(CRASHED_PRIMARY, &mdev->flags);
1530 else
1531 clear_bit(CRASHED_PRIMARY, &mdev->flags);
1532
Philipp Reisner894c6a92010-06-18 16:03:20 +02001533 if (drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
Lars Ellenbergd5d7ebd2011-07-05 20:59:26 +02001534 !(mdev->state.role == R_PRIMARY && mdev->tconn->susp_nod))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001535 set_bit(CRASHED_PRIMARY, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001536
1537 mdev->send_cnt = 0;
1538 mdev->recv_cnt = 0;
1539 mdev->read_cnt = 0;
1540 mdev->writ_cnt = 0;
1541
Philipp Reisner99432fc2011-05-20 16:39:13 +02001542 drbd_reconsider_max_bio_size(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001543
1544 /* If I am currently not R_PRIMARY,
1545 * but meta data primary indicator is set,
1546 * I just now recover from a hard crash,
1547 * and have been R_PRIMARY before that crash.
1548 *
1549 * Now, if I had no connection before that crash
1550 * (have been degraded R_PRIMARY), chances are that
1551 * I won't find my peer now either.
1552 *
1553 * In that case, and _only_ in that case,
1554 * we use the degr-wfc-timeout instead of the default,
1555 * so we can automatically recover from a crash of a
1556 * degraded but active "cluster" after a certain timeout.
1557 */
1558 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
1559 if (mdev->state.role != R_PRIMARY &&
1560 drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
1561 !drbd_md_test_flag(mdev->ldev, MDF_CONNECTED_IND))
1562 set_bit(USE_DEGR_WFC_T, &mdev->flags);
1563
Bart Van Assche24c48302011-05-21 18:32:29 +02001564 dd = drbd_determine_dev_size(mdev, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001565 if (dd == dev_size_error) {
1566 retcode = ERR_NOMEM_BITMAP;
1567 goto force_diskless_dec;
1568 } else if (dd == grew)
1569 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
1570
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001571 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC) ||
1572 (test_bit(CRASHED_PRIMARY, &mdev->flags) &&
1573 drbd_md_test_flag(mdev->ldev, MDF_AL_DISABLED))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001574 dev_info(DEV, "Assuming that all blocks are out of sync "
1575 "(aka FullSync)\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001576 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write,
1577 "set_n_write from attaching", BM_LOCKED_MASK)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001578 retcode = ERR_IO_MD_DISK;
1579 goto force_diskless_dec;
1580 }
1581 } else {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001582 if (drbd_bitmap_io(mdev, &drbd_bm_read,
Andreas Gruenbacher22ab6a32010-12-13 01:44:11 +01001583 "read from attaching", BM_LOCKED_MASK)) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001584 retcode = ERR_IO_MD_DISK;
1585 goto force_diskless_dec;
1586 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001587 }
1588
Philipp Reisner07782862010-08-31 12:00:50 +02001589 if (_drbd_bm_total_weight(mdev) == drbd_bm_bits(mdev))
1590 drbd_suspend_al(mdev); /* IO is still suspended here... */
1591
Philipp Reisner87eeee42011-01-19 14:16:30 +01001592 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner78bae592011-03-28 15:40:12 +02001593 os = drbd_read_state(mdev);
1594 ns = os;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001595 /* If MDF_CONSISTENT is not set go into inconsistent state,
1596 otherwise investigate MDF_WasUpToDate...
1597 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1598 otherwise into D_CONSISTENT state.
1599 */
1600 if (drbd_md_test_flag(mdev->ldev, MDF_CONSISTENT)) {
1601 if (drbd_md_test_flag(mdev->ldev, MDF_WAS_UP_TO_DATE))
1602 ns.disk = D_CONSISTENT;
1603 else
1604 ns.disk = D_OUTDATED;
1605 } else {
1606 ns.disk = D_INCONSISTENT;
1607 }
1608
1609 if (drbd_md_test_flag(mdev->ldev, MDF_PEER_OUT_DATED))
1610 ns.pdsk = D_OUTDATED;
1611
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001612 rcu_read_lock();
1613 if (ns.disk == D_CONSISTENT &&
1614 (ns.pdsk == D_OUTDATED || rcu_dereference(mdev->ldev->disk_conf)->fencing == FP_DONT_CARE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001615 ns.disk = D_UP_TO_DATE;
1616
1617 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1618 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1619 this point, because drbd_request_state() modifies these
1620 flags. */
1621
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001622 if (rcu_dereference(mdev->ldev->disk_conf)->al_updates)
Philipp Reisner4035e4c2012-10-01 18:04:12 +02001623 mdev->ldev->md.flags &= ~MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001624 else
1625 mdev->ldev->md.flags |= MDF_AL_DISABLED;
1626
1627 rcu_read_unlock();
1628
Philipp Reisnerb411b362009-09-25 16:07:19 -07001629 /* In case we are C_CONNECTED postpone any decision on the new disk
1630 state after the negotiation phase. */
1631 if (mdev->state.conn == C_CONNECTED) {
1632 mdev->new_state_tmp.i = ns.i;
1633 ns.i = os.i;
1634 ns.disk = D_NEGOTIATING;
Philipp Reisnerdc66c742010-06-02 14:31:29 +02001635
1636 /* We expect to receive up-to-date UUIDs soon.
1637 To avoid a race in receive_state, free p_uuid while
1638 holding req_lock. I.e. atomic with the state change */
1639 kfree(mdev->p_uuid);
1640 mdev->p_uuid = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001641 }
1642
1643 rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001644 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001645
1646 if (rv < SS_SUCCESS)
1647 goto force_diskless_dec;
1648
Philipp Reisnercdfda632011-07-05 15:38:59 +02001649 mod_timer(&mdev->request_timer, jiffies + HZ);
1650
Philipp Reisnerb411b362009-09-25 16:07:19 -07001651 if (mdev->state.role == R_PRIMARY)
1652 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
1653 else
1654 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
1655
1656 drbd_md_mark_dirty(mdev);
1657 drbd_md_sync(mdev);
1658
1659 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1660 put_ldev(mdev);
Philipp Reisner0e29d162011-02-18 14:23:11 +01001661 conn_reconfig_done(mdev->tconn);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001662 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001663 return 0;
1664
1665 force_diskless_dec:
1666 put_ldev(mdev);
1667 force_diskless:
Philipp Reisner9510b242011-07-01 17:00:57 +02001668 drbd_force_state(mdev, NS(disk, D_DISKLESS));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001669 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001670 fail:
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001671 conn_reconfig_done(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001672 if (nbc) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001673 if (nbc->backing_bdev)
1674 blkdev_put(nbc->backing_bdev,
1675 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1676 if (nbc->md_bdev)
1677 blkdev_put(nbc->md_bdev,
1678 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001679 kfree(nbc);
1680 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001681 kfree(new_disk_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001682 lc_destroy(resync_lru);
Philipp Reisner9958c852011-05-03 16:19:31 +02001683 kfree(new_plan);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001684
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001685 finish:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001686 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001687 return 0;
1688}
1689
Philipp Reisnercdfda632011-07-05 15:38:59 +02001690static int adm_detach(struct drbd_conf *mdev, int force)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001691{
Philipp Reisner19f83c72011-03-29 14:21:03 +02001692 enum drbd_state_rv retcode;
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001693 int ret;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001694
Philipp Reisnercdfda632011-07-05 15:38:59 +02001695 if (force) {
Lars Ellenberg0c849662012-07-30 09:07:28 +02001696 set_bit(FORCE_DETACH, &mdev->flags);
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001697 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnercdfda632011-07-05 15:38:59 +02001698 retcode = SS_SUCCESS;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001699 goto out;
1700 }
1701
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001702 drbd_suspend_io(mdev); /* so no-one is stuck in drbd_al_begin_io */
Lars Ellenberga2e91382011-10-06 17:30:26 +02001703 drbd_md_get_buffer(mdev); /* make sure there is no in-flight meta-data IO */
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001704 retcode = drbd_request_state(mdev, NS(disk, D_FAILED));
Lars Ellenberga2e91382011-10-06 17:30:26 +02001705 drbd_md_put_buffer(mdev);
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001706 /* D_FAILED will transition to DISKLESS. */
1707 ret = wait_event_interruptible(mdev->misc_wait,
1708 mdev->state.disk != D_FAILED);
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001709 drbd_resume_io(mdev);
Philipp Reisner9b2f61a2011-05-24 10:27:38 +02001710 if ((int)retcode == (int)SS_IS_DISKLESS)
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001711 retcode = SS_NOTHING_TO_DO;
1712 if (ret)
1713 retcode = ERR_INTR;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001714out:
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001715 return retcode;
1716}
1717
Philipp Reisnerb411b362009-09-25 16:07:19 -07001718/* Detaching the disk is a process in multiple stages. First we need to lock
1719 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1720 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1721 * internal references as well.
1722 * Only then we have finally detached. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001723int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001724{
1725 enum drbd_ret_code retcode;
Philipp Reisnercdfda632011-07-05 15:38:59 +02001726 struct detach_parms parms = { };
1727 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001728
1729 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1730 if (!adm_ctx.reply_skb)
1731 return retcode;
1732 if (retcode != NO_ERROR)
1733 goto out;
1734
Philipp Reisnercdfda632011-07-05 15:38:59 +02001735 if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
1736 err = detach_parms_from_attrs(&parms, info);
1737 if (err) {
1738 retcode = ERR_MANDATORY_TAG;
1739 drbd_msg_put_info(from_attrs_err_to_txt(err));
1740 goto out;
1741 }
1742 }
1743
1744 retcode = adm_detach(adm_ctx.mdev, parms.force_detach);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001745out:
1746 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001747 return 0;
1748}
1749
Lars Ellenbergf3990022011-03-23 14:31:09 +01001750static bool conn_resync_running(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001751{
Lars Ellenbergf3990022011-03-23 14:31:09 +01001752 struct drbd_conf *mdev;
Philipp Reisner695d08f2011-04-11 22:53:32 -07001753 bool rv = false;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001754 int vnr;
1755
Philipp Reisner695d08f2011-04-11 22:53:32 -07001756 rcu_read_lock();
Lars Ellenbergf3990022011-03-23 14:31:09 +01001757 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1758 if (mdev->state.conn == C_SYNC_SOURCE ||
1759 mdev->state.conn == C_SYNC_TARGET ||
1760 mdev->state.conn == C_PAUSED_SYNC_S ||
Philipp Reisner695d08f2011-04-11 22:53:32 -07001761 mdev->state.conn == C_PAUSED_SYNC_T) {
1762 rv = true;
1763 break;
1764 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001765 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001766 rcu_read_unlock();
1767
1768 return rv;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001769}
1770
1771static bool conn_ov_running(struct drbd_tconn *tconn)
1772{
1773 struct drbd_conf *mdev;
Philipp Reisner695d08f2011-04-11 22:53:32 -07001774 bool rv = false;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001775 int vnr;
1776
Philipp Reisner695d08f2011-04-11 22:53:32 -07001777 rcu_read_lock();
Lars Ellenbergf3990022011-03-23 14:31:09 +01001778 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1779 if (mdev->state.conn == C_VERIFY_S ||
Philipp Reisner695d08f2011-04-11 22:53:32 -07001780 mdev->state.conn == C_VERIFY_T) {
1781 rv = true;
1782 break;
1783 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001784 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001785 rcu_read_unlock();
1786
1787 return rv;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001788}
1789
Philipp Reisnercd643972011-04-13 18:00:59 -07001790static enum drbd_ret_code
Philipp Reisner44ed1672011-04-19 17:10:19 +02001791_check_net_options(struct drbd_tconn *tconn, struct net_conf *old_conf, struct net_conf *new_conf)
Philipp Reisnercd643972011-04-13 18:00:59 -07001792{
1793 struct drbd_conf *mdev;
1794 int i;
1795
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02001796 if (old_conf && tconn->cstate == C_WF_REPORT_PARAMS && tconn->agreed_pro_version < 100) {
1797 if (new_conf->wire_protocol != old_conf->wire_protocol)
1798 return ERR_NEED_APV_100;
1799
1800 if (new_conf->two_primaries != old_conf->two_primaries)
1801 return ERR_NEED_APV_100;
1802
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02001803 if (strcmp(new_conf->integrity_alg, old_conf->integrity_alg))
1804 return ERR_NEED_APV_100;
1805 }
1806
1807 if (!new_conf->two_primaries &&
1808 conn_highest_role(tconn) == R_PRIMARY &&
1809 conn_highest_peer(tconn) == R_PRIMARY)
1810 return ERR_NEED_ALLOW_TWO_PRI;
Philipp Reisnerb032b6f2011-04-13 18:16:10 -07001811
Philipp Reisnercd643972011-04-13 18:00:59 -07001812 if (new_conf->two_primaries &&
1813 (new_conf->wire_protocol != DRBD_PROT_C))
1814 return ERR_NOT_PROTO_C;
1815
Philipp Reisnercd643972011-04-13 18:00:59 -07001816 idr_for_each_entry(&tconn->volumes, mdev, i) {
1817 if (get_ldev(mdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001818 enum drbd_fencing_p fp = rcu_dereference(mdev->ldev->disk_conf)->fencing;
Philipp Reisnercd643972011-04-13 18:00:59 -07001819 put_ldev(mdev);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001820 if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
Philipp Reisnercd643972011-04-13 18:00:59 -07001821 return ERR_STONITH_AND_PROT_A;
Philipp Reisnercd643972011-04-13 18:00:59 -07001822 }
Andreas Gruenbacher6139f602011-05-06 20:00:02 +02001823 if (mdev->state.role == R_PRIMARY && new_conf->discard_my_data)
Lars Ellenbergeb120102012-08-01 12:46:20 +02001824 return ERR_DISCARD_IMPOSSIBLE;
Philipp Reisnercd643972011-04-13 18:00:59 -07001825 }
Philipp Reisnercd643972011-04-13 18:00:59 -07001826
1827 if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A)
1828 return ERR_CONG_NOT_PROTO_A;
1829
1830 return NO_ERROR;
1831}
1832
Philipp Reisner44ed1672011-04-19 17:10:19 +02001833static enum drbd_ret_code
1834check_net_options(struct drbd_tconn *tconn, struct net_conf *new_conf)
1835{
1836 static enum drbd_ret_code rv;
1837 struct drbd_conf *mdev;
1838 int i;
1839
1840 rcu_read_lock();
1841 rv = _check_net_options(tconn, rcu_dereference(tconn->net_conf), new_conf);
1842 rcu_read_unlock();
1843
1844 /* tconn->volumes protected by genl_lock() here */
1845 idr_for_each_entry(&tconn->volumes, mdev, i) {
1846 if (!mdev->bitmap) {
1847 if(drbd_bm_init(mdev))
1848 return ERR_NOMEM;
1849 }
1850 }
1851
1852 return rv;
1853}
1854
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001855struct crypto {
1856 struct crypto_hash *verify_tfm;
1857 struct crypto_hash *csums_tfm;
1858 struct crypto_hash *cram_hmac_tfm;
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02001859 struct crypto_hash *integrity_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001860};
1861
1862static int
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02001863alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001864{
1865 if (!tfm_name[0])
1866 return NO_ERROR;
1867
1868 *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
1869 if (IS_ERR(*tfm)) {
1870 *tfm = NULL;
1871 return err_alg;
1872 }
1873
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001874 return NO_ERROR;
1875}
1876
1877static enum drbd_ret_code
1878alloc_crypto(struct crypto *crypto, struct net_conf *new_conf)
1879{
Philipp Reisnerb411b362009-09-25 16:07:19 -07001880 char hmac_name[CRYPTO_MAX_ALG_NAME];
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001881 enum drbd_ret_code rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001882
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02001883 rv = alloc_hash(&crypto->csums_tfm, new_conf->csums_alg,
1884 ERR_CSUMS_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001885 if (rv != NO_ERROR)
1886 return rv;
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02001887 rv = alloc_hash(&crypto->verify_tfm, new_conf->verify_alg,
1888 ERR_VERIFY_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001889 if (rv != NO_ERROR)
1890 return rv;
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02001891 rv = alloc_hash(&crypto->integrity_tfm, new_conf->integrity_alg,
1892 ERR_INTEGRITY_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001893 if (rv != NO_ERROR)
1894 return rv;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001895 if (new_conf->cram_hmac_alg[0] != 0) {
1896 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
1897 new_conf->cram_hmac_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001898
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02001899 rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
1900 ERR_AUTH_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001901 }
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001902
1903 return rv;
1904}
1905
1906static void free_crypto(struct crypto *crypto)
1907{
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001908 crypto_free_hash(crypto->cram_hmac_tfm);
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02001909 crypto_free_hash(crypto->integrity_tfm);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001910 crypto_free_hash(crypto->csums_tfm);
1911 crypto_free_hash(crypto->verify_tfm);
1912}
1913
Lars Ellenbergf3990022011-03-23 14:31:09 +01001914int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
1915{
1916 enum drbd_ret_code retcode;
1917 struct drbd_tconn *tconn;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001918 struct net_conf *old_conf, *new_conf = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001919 int err;
1920 int ovr; /* online verify running */
1921 int rsr; /* re-sync running */
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001922 struct crypto crypto = { };
Lars Ellenbergf3990022011-03-23 14:31:09 +01001923
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02001924 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001925 if (!adm_ctx.reply_skb)
1926 return retcode;
1927 if (retcode != NO_ERROR)
1928 goto out;
1929
1930 tconn = adm_ctx.tconn;
1931
1932 new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
1933 if (!new_conf) {
1934 retcode = ERR_NOMEM;
1935 goto out;
1936 }
1937
Lars Ellenbergf3990022011-03-23 14:31:09 +01001938 conn_reconfig_start(tconn);
1939
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001940 mutex_lock(&tconn->data.mutex);
Philipp Reisnera0095502011-05-03 13:14:15 +02001941 mutex_lock(&tconn->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001942 old_conf = tconn->net_conf;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001943
1944 if (!old_conf) {
1945 drbd_msg_put_info("net conf missing, try connect");
1946 retcode = ERR_INVALID_REQUEST;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001947 goto fail;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001948 }
1949
1950 *new_conf = *old_conf;
Lars Ellenberg5979e362011-04-27 21:09:55 +02001951 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02001952 set_net_conf_defaults(new_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001953
Lars Ellenbergf3990022011-03-23 14:31:09 +01001954 err = net_conf_from_attrs_for_change(new_conf, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02001955 if (err && err != -ENOMSG) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001956 retcode = ERR_MANDATORY_TAG;
1957 drbd_msg_put_info(from_attrs_err_to_txt(err));
1958 goto fail;
1959 }
1960
Philipp Reisnercd643972011-04-13 18:00:59 -07001961 retcode = check_net_options(tconn, new_conf);
1962 if (retcode != NO_ERROR)
1963 goto fail;
1964
Lars Ellenbergf3990022011-03-23 14:31:09 +01001965 /* re-sync running */
1966 rsr = conn_resync_running(tconn);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001967 if (rsr && strcmp(new_conf->csums_alg, old_conf->csums_alg)) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001968 retcode = ERR_CSUMS_RESYNC_RUNNING;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001969 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001970 }
1971
Lars Ellenbergf3990022011-03-23 14:31:09 +01001972 /* online verify running */
1973 ovr = conn_ov_running(tconn);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001974 if (ovr && strcmp(new_conf->verify_alg, old_conf->verify_alg)) {
1975 retcode = ERR_VERIFY_RUNNING;
1976 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001977 }
1978
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001979 retcode = alloc_crypto(&crypto, new_conf);
1980 if (retcode != NO_ERROR)
1981 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001982
Philipp Reisner44ed1672011-04-19 17:10:19 +02001983 rcu_assign_pointer(tconn->net_conf, new_conf);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001984
1985 if (!rsr) {
1986 crypto_free_hash(tconn->csums_tfm);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001987 tconn->csums_tfm = crypto.csums_tfm;
1988 crypto.csums_tfm = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001989 }
1990 if (!ovr) {
1991 crypto_free_hash(tconn->verify_tfm);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001992 tconn->verify_tfm = crypto.verify_tfm;
1993 crypto.verify_tfm = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001994 }
1995
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02001996 crypto_free_hash(tconn->integrity_tfm);
1997 tconn->integrity_tfm = crypto.integrity_tfm;
Philipp Reisnerd659f2a2011-05-16 17:38:45 +02001998 if (tconn->cstate >= C_WF_REPORT_PARAMS && tconn->agreed_pro_version >= 100)
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001999 /* Do this without trying to take tconn->data.mutex again. */
Philipp Reisnerd659f2a2011-05-16 17:38:45 +02002000 __drbd_send_protocol(tconn, P_PROTOCOL_UPDATE);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002001
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002002 crypto_free_hash(tconn->cram_hmac_tfm);
2003 tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
2004
Philipp Reisnera0095502011-05-03 13:14:15 +02002005 mutex_unlock(&tconn->conf_update);
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02002006 mutex_unlock(&tconn->data.mutex);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002007 synchronize_rcu();
2008 kfree(old_conf);
2009
Lars Ellenbergf3990022011-03-23 14:31:09 +01002010 if (tconn->cstate >= C_WF_REPORT_PARAMS)
2011 drbd_send_sync_param(minor_to_mdev(conn_lowest_minor(tconn)));
2012
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002013 goto done;
2014
Lars Ellenbergf3990022011-03-23 14:31:09 +01002015 fail:
Philipp Reisnera0095502011-05-03 13:14:15 +02002016 mutex_unlock(&tconn->conf_update);
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02002017 mutex_unlock(&tconn->data.mutex);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002018 free_crypto(&crypto);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002019 kfree(new_conf);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002020 done:
Lars Ellenbergf3990022011-03-23 14:31:09 +01002021 conn_reconfig_done(tconn);
2022 out:
2023 drbd_adm_finish(info, retcode);
2024 return 0;
2025}
2026
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002027int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002028{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002029 struct drbd_conf *mdev;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002030 struct net_conf *old_conf, *new_conf = NULL;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002031 struct crypto crypto = { };
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002032 struct drbd_tconn *tconn;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002033 enum drbd_ret_code retcode;
2034 int i;
2035 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002036
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02002037 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002038
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002039 if (!adm_ctx.reply_skb)
2040 return retcode;
2041 if (retcode != NO_ERROR)
2042 goto out;
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002043 if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
2044 drbd_msg_put_info("connection endpoint(s) missing");
2045 retcode = ERR_INVALID_REQUEST;
2046 goto out;
2047 }
2048
2049 /* No need for _rcu here. All reconfiguration is
2050 * strictly serialized on genl_lock(). We are protected against
2051 * concurrent reconfiguration/addition/deletion */
2052 list_for_each_entry(tconn, &drbd_tconns, all_tconn) {
2053 if (nla_len(adm_ctx.my_addr) == tconn->my_addr_len &&
2054 !memcmp(nla_data(adm_ctx.my_addr), &tconn->my_addr, tconn->my_addr_len)) {
2055 retcode = ERR_LOCAL_ADDR;
2056 goto out;
2057 }
2058
2059 if (nla_len(adm_ctx.peer_addr) == tconn->peer_addr_len &&
2060 !memcmp(nla_data(adm_ctx.peer_addr), &tconn->peer_addr, tconn->peer_addr_len)) {
2061 retcode = ERR_PEER_ADDR;
2062 goto out;
2063 }
2064 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002065
2066 tconn = adm_ctx.tconn;
Philipp Reisner80883192011-02-18 14:56:45 +01002067 conn_reconfig_start(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002068
Philipp Reisner80883192011-02-18 14:56:45 +01002069 if (tconn->cstate > C_STANDALONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002070 retcode = ERR_NET_CONFIGURED;
2071 goto fail;
2072 }
2073
Andreas Gruenbachera209b4a2011-08-17 12:43:25 +02002074 /* allocation not in the IO path, drbdsetup / netlink process context */
Lars Ellenberg5979e362011-04-27 21:09:55 +02002075 new_conf = kzalloc(sizeof(*new_conf), GFP_KERNEL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002076 if (!new_conf) {
2077 retcode = ERR_NOMEM;
2078 goto fail;
2079 }
2080
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02002081 set_net_conf_defaults(new_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002082
Lars Ellenbergf3990022011-03-23 14:31:09 +01002083 err = net_conf_from_attrs(new_conf, info);
Lars Ellenberg25e40932011-08-19 10:39:00 +02002084 if (err && err != -ENOMSG) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002085 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002086 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002087 goto fail;
2088 }
2089
Philipp Reisnercd643972011-04-13 18:00:59 -07002090 retcode = check_net_options(tconn, new_conf);
2091 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002092 goto fail;
Philipp Reisner47ff2d02010-06-18 13:56:57 +02002093
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002094 retcode = alloc_crypto(&crypto, new_conf);
2095 if (retcode != NO_ERROR)
Philipp Reisner422028b2010-10-27 11:12:07 +02002096 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002097
2098 ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
2099
Philipp Reisner80883192011-02-18 14:56:45 +01002100 conn_flush_workqueue(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002101
Philipp Reisnera0095502011-05-03 13:14:15 +02002102 mutex_lock(&tconn->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002103 old_conf = tconn->net_conf;
2104 if (old_conf) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002105 retcode = ERR_NET_CONFIGURED;
Philipp Reisnera0095502011-05-03 13:14:15 +02002106 mutex_unlock(&tconn->conf_update);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002107 goto fail;
2108 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002109 rcu_assign_pointer(tconn->net_conf, new_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002110
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002111 conn_free_crypto(tconn);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002112 tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02002113 tconn->integrity_tfm = crypto.integrity_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002114 tconn->csums_tfm = crypto.csums_tfm;
2115 tconn->verify_tfm = crypto.verify_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002116
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002117 tconn->my_addr_len = nla_len(adm_ctx.my_addr);
2118 memcpy(&tconn->my_addr, nla_data(adm_ctx.my_addr), tconn->my_addr_len);
2119 tconn->peer_addr_len = nla_len(adm_ctx.peer_addr);
2120 memcpy(&tconn->peer_addr, nla_data(adm_ctx.peer_addr), tconn->peer_addr_len);
2121
Philipp Reisnera0095502011-05-03 13:14:15 +02002122 mutex_unlock(&tconn->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002123
Philipp Reisner695d08f2011-04-11 22:53:32 -07002124 rcu_read_lock();
Philipp Reisner80883192011-02-18 14:56:45 +01002125 idr_for_each_entry(&tconn->volumes, mdev, i) {
2126 mdev->send_cnt = 0;
2127 mdev->recv_cnt = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002128 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07002129 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002130
Lars Ellenberg5ee743e2011-04-26 16:22:25 +02002131 retcode = conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002132
Philipp Reisner80883192011-02-18 14:56:45 +01002133 conn_reconfig_done(tconn);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002134 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002135 return 0;
2136
2137fail:
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002138 free_crypto(&crypto);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002139 kfree(new_conf);
2140
Philipp Reisner80883192011-02-18 14:56:45 +01002141 conn_reconfig_done(tconn);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002142out:
2143 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002144 return 0;
2145}
2146
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002147static enum drbd_state_rv conn_try_disconnect(struct drbd_tconn *tconn, bool force)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002148{
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002149 enum drbd_state_rv rv;
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002150
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002151 rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
2152 force ? CS_HARD : 0);
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002153
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002154 switch (rv) {
2155 case SS_NOTHING_TO_DO:
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002156 break;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002157 case SS_ALREADY_STANDALONE:
2158 return SS_SUCCESS;
2159 case SS_PRIMARY_NOP:
2160 /* Our state checking code wants to see the peer outdated. */
2161 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002162 pdsk, D_OUTDATED), CS_VERBOSE);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002163 break;
2164 case SS_CW_FAILED_BY_PEER:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002165 /* The peer probably wants to see us outdated. */
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002166 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
2167 disk, D_OUTDATED), 0);
2168 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002169 rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
2170 CS_HARD);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002171 }
2172 break;
2173 default:;
2174 /* no special handling necessary */
2175 }
2176
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002177 if (rv >= SS_SUCCESS) {
2178 enum drbd_state_rv rv2;
2179 /* No one else can reconfigure the network while I am here.
2180 * The state handling only uses drbd_thread_stop_nowait(),
2181 * we want to really wait here until the receiver is no more.
2182 */
2183 drbd_thread_stop(&adm_ctx.tconn->receiver);
2184
2185 /* Race breaker. This additional state change request may be
2186 * necessary, if this was a forced disconnect during a receiver
2187 * restart. We may have "killed" the receiver thread just
2188 * after drbdd_init() returned. Typically, we should be
2189 * C_STANDALONE already, now, and this becomes a no-op.
2190 */
2191 rv2 = conn_request_state(tconn, NS(conn, C_STANDALONE),
2192 CS_VERBOSE | CS_HARD);
2193 if (rv2 < SS_SUCCESS)
2194 conn_err(tconn,
2195 "unexpected rv2=%d in conn_try_disconnect()\n",
2196 rv2);
2197 }
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002198 return rv;
2199}
2200
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002201int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002202{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002203 struct disconnect_parms parms;
2204 struct drbd_tconn *tconn;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002205 enum drbd_state_rv rv;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002206 enum drbd_ret_code retcode;
2207 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002208
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002209 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002210 if (!adm_ctx.reply_skb)
2211 return retcode;
2212 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002213 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002214
2215 tconn = adm_ctx.tconn;
2216 memset(&parms, 0, sizeof(parms));
2217 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002218 err = disconnect_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002219 if (err) {
2220 retcode = ERR_MANDATORY_TAG;
2221 drbd_msg_put_info(from_attrs_err_to_txt(err));
2222 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002223 }
2224 }
2225
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002226 rv = conn_try_disconnect(tconn, parms.force_disconnect);
2227 if (rv < SS_SUCCESS)
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002228 retcode = rv; /* FIXME: Type mismatch. */
2229 else
2230 retcode = NO_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002231 fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002232 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002233 return 0;
2234}
2235
2236void resync_after_online_grow(struct drbd_conf *mdev)
2237{
2238 int iass; /* I am sync source */
2239
2240 dev_info(DEV, "Resync of new storage after online grow\n");
2241 if (mdev->state.role != mdev->state.peer)
2242 iass = (mdev->state.role == R_PRIMARY);
2243 else
Lars Ellenberg427c0432012-08-01 12:43:01 +02002244 iass = test_bit(RESOLVE_CONFLICTS, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002245
2246 if (iass)
2247 drbd_start_resync(mdev, C_SYNC_SOURCE);
2248 else
2249 _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
2250}
2251
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002252int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002253{
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002254 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002255 struct resize_parms rs;
2256 struct drbd_conf *mdev;
2257 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002258 enum determine_dev_size dd;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002259 enum dds_flags ddsf;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002260 sector_t u_size;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002261 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002262
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002263 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2264 if (!adm_ctx.reply_skb)
2265 return retcode;
2266 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002267 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002268
2269 memset(&rs, 0, sizeof(struct resize_parms));
2270 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002271 err = resize_parms_from_attrs(&rs, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002272 if (err) {
2273 retcode = ERR_MANDATORY_TAG;
2274 drbd_msg_put_info(from_attrs_err_to_txt(err));
2275 goto fail;
2276 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002277 }
2278
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002279 mdev = adm_ctx.mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002280 if (mdev->state.conn > C_CONNECTED) {
2281 retcode = ERR_RESIZE_RESYNC;
2282 goto fail;
2283 }
2284
2285 if (mdev->state.role == R_SECONDARY &&
2286 mdev->state.peer == R_SECONDARY) {
2287 retcode = ERR_NO_PRIMARY;
2288 goto fail;
2289 }
2290
2291 if (!get_ldev(mdev)) {
2292 retcode = ERR_NO_DISK;
2293 goto fail;
2294 }
2295
Philipp Reisner31890f42011-01-19 14:12:51 +01002296 if (rs.no_resync && mdev->tconn->agreed_pro_version < 93) {
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002297 retcode = ERR_NEED_APV_93;
Andreas Gruenbacher7b4e4d32011-09-28 22:15:04 +02002298 goto fail_ldev;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002299 }
2300
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002301 rcu_read_lock();
2302 u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
2303 rcu_read_unlock();
2304 if (u_size != (sector_t)rs.resize_size) {
2305 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
2306 if (!new_disk_conf) {
2307 retcode = ERR_NOMEM;
Philipp Reisner9bcd2522011-09-29 13:00:14 +02002308 goto fail_ldev;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002309 }
2310 }
2311
Philipp Reisner087c2492010-03-26 13:49:56 +01002312 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002313 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002314
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002315 if (new_disk_conf) {
2316 mutex_lock(&mdev->tconn->conf_update);
2317 old_disk_conf = mdev->ldev->disk_conf;
2318 *new_disk_conf = *old_disk_conf;
2319 new_disk_conf->disk_size = (sector_t)rs.resize_size;
2320 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
2321 mutex_unlock(&mdev->tconn->conf_update);
2322 synchronize_rcu();
2323 kfree(old_disk_conf);
2324 }
2325
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002326 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
Bart Van Assche24c48302011-05-21 18:32:29 +02002327 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002328 drbd_md_sync(mdev);
2329 put_ldev(mdev);
2330 if (dd == dev_size_error) {
2331 retcode = ERR_NOMEM_BITMAP;
2332 goto fail;
2333 }
2334
Philipp Reisner087c2492010-03-26 13:49:56 +01002335 if (mdev->state.conn == C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002336 if (dd == grew)
2337 set_bit(RESIZE_PENDING, &mdev->flags);
2338
2339 drbd_send_uuids(mdev);
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002340 drbd_send_sizes(mdev, 1, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002341 }
2342
2343 fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002344 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002345 return 0;
Andreas Gruenbacher7b4e4d32011-09-28 22:15:04 +02002346
2347 fail_ldev:
2348 put_ldev(mdev);
2349 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002350}
2351
Lars Ellenbergf3990022011-03-23 14:31:09 +01002352int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002353{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002354 enum drbd_ret_code retcode;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002355 struct drbd_tconn *tconn;
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002356 struct res_opts res_opts;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002357 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002358
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02002359 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002360 if (!adm_ctx.reply_skb)
2361 return retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002362 if (retcode != NO_ERROR)
2363 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002364 tconn = adm_ctx.tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002365
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002366 res_opts = tconn->res_opts;
Lars Ellenberg5979e362011-04-27 21:09:55 +02002367 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02002368 set_res_opts_defaults(&res_opts);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002369
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002370 err = res_opts_from_attrs(&res_opts, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02002371 if (err && err != -ENOMSG) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002372 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002373 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002374 goto fail;
2375 }
2376
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02002377 err = set_resource_options(tconn, &res_opts);
2378 if (err) {
2379 retcode = ERR_INVALID_REQUEST;
2380 if (err == -ENOMEM)
Philipp Reisner778f2712010-07-06 11:14:00 +02002381 retcode = ERR_NOMEM;
Philipp Reisner778f2712010-07-06 11:14:00 +02002382 }
2383
Philipp Reisnerb411b362009-09-25 16:07:19 -07002384fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002385 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002386 return 0;
2387}
2388
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002389int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002390{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002391 struct drbd_conf *mdev;
2392 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2393
2394 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2395 if (!adm_ctx.reply_skb)
2396 return retcode;
2397 if (retcode != NO_ERROR)
2398 goto out;
2399
2400 mdev = adm_ctx.mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002401
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002402 /* If there is still bitmap IO pending, probably because of a previous
Lars Ellenberg7ee1fb92012-06-19 10:27:58 +02002403 * resync just being finished, wait for it before requesting a new resync.
2404 * Also wait for it's after_state_ch(). */
Lars Ellenberga574daf2012-04-25 16:27:35 +02002405 drbd_suspend_io(mdev);
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002406 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
Lars Ellenberg7ee1fb92012-06-19 10:27:58 +02002407 drbd_flush_workqueue(mdev);
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002408
Philipp Reisnerb411b362009-09-25 16:07:19 -07002409 retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T), CS_ORDERED);
2410
2411 if (retcode < SS_SUCCESS && retcode != SS_NEED_CONNECTION)
2412 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
2413
2414 while (retcode == SS_NEED_CONNECTION) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01002415 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002416 if (mdev->state.conn < C_CONNECTED)
2417 retcode = _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_VERBOSE, NULL);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002418 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002419
2420 if (retcode != SS_NEED_CONNECTION)
2421 break;
2422
2423 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
2424 }
Lars Ellenberga574daf2012-04-25 16:27:35 +02002425 drbd_resume_io(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002426
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002427out:
2428 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002429 return 0;
2430}
2431
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002432static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2433 union drbd_state mask, union drbd_state val)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002434{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002435 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002436
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002437 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;
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002442
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002443 retcode = drbd_request_state(adm_ctx.mdev, mask, val);
2444out:
2445 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002446 return 0;
2447}
2448
2449static int drbd_bmio_set_susp_al(struct drbd_conf *mdev)
2450{
2451 int rv;
2452
2453 rv = drbd_bmio_set_n_write(mdev);
2454 drbd_suspend_al(mdev);
2455 return rv;
2456}
2457
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002458int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002459{
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002460 int retcode; /* drbd_ret_code, drbd_state_rv */
2461 struct drbd_conf *mdev;
2462
2463 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2464 if (!adm_ctx.reply_skb)
2465 return retcode;
2466 if (retcode != NO_ERROR)
2467 goto out;
2468
2469 mdev = adm_ctx.mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002470
2471 /* If there is still bitmap IO pending, probably because of a previous
Lars Ellenberg7ee1fb92012-06-19 10:27:58 +02002472 * resync just being finished, wait for it before requesting a new resync.
2473 * Also wait for it's after_state_ch(). */
Lars Ellenberga574daf2012-04-25 16:27:35 +02002474 drbd_suspend_io(mdev);
Lars Ellenberg5016b822012-05-07 12:00:56 +02002475 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
Lars Ellenberg7ee1fb92012-06-19 10:27:58 +02002476 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002477
Philipp Reisner07782862010-08-31 12:00:50 +02002478 retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S), CS_ORDERED);
Philipp Reisner07782862010-08-31 12:00:50 +02002479 if (retcode < SS_SUCCESS) {
2480 if (retcode == SS_NEED_CONNECTION && mdev->state.role == R_PRIMARY) {
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002481 /* The peer will get a resync upon connect anyways.
2482 * Just make that into a full resync. */
Philipp Reisner07782862010-08-31 12:00:50 +02002483 retcode = drbd_request_state(mdev, NS(pdsk, D_INCONSISTENT));
2484 if (retcode >= SS_SUCCESS) {
Philipp Reisner07782862010-08-31 12:00:50 +02002485 if (drbd_bitmap_io(mdev, &drbd_bmio_set_susp_al,
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002486 "set_n_write from invalidate_peer",
2487 BM_LOCKED_SET_ALLOWED))
Philipp Reisner07782862010-08-31 12:00:50 +02002488 retcode = ERR_IO_MD_DISK;
2489 }
2490 } else
2491 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S));
2492 }
Lars Ellenberga574daf2012-04-25 16:27:35 +02002493 drbd_resume_io(mdev);
Philipp Reisner07782862010-08-31 12:00:50 +02002494
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002495out:
2496 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002497 return 0;
2498}
2499
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002500int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002501{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002502 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002503
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002504 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2505 if (!adm_ctx.reply_skb)
2506 return retcode;
2507 if (retcode != NO_ERROR)
2508 goto out;
2509
2510 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002511 retcode = ERR_PAUSE_IS_SET;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002512out:
2513 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002514 return 0;
2515}
2516
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002517int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002518{
Philipp Reisnerda9fbc22011-03-29 10:52:01 +02002519 union drbd_dev_state s;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002520 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002521
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002522 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2523 if (!adm_ctx.reply_skb)
2524 return retcode;
2525 if (retcode != NO_ERROR)
2526 goto out;
2527
2528 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2529 s = adm_ctx.mdev->state;
Philipp Reisnercd88d032011-01-20 11:46:41 +01002530 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2531 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2532 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2533 } else {
2534 retcode = ERR_PAUSE_IS_CLEAR;
2535 }
2536 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002537
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002538out:
2539 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002540 return 0;
2541}
2542
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002543int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002544{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002545 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002546}
2547
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002548int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002549{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002550 struct drbd_conf *mdev;
2551 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2552
2553 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2554 if (!adm_ctx.reply_skb)
2555 return retcode;
2556 if (retcode != NO_ERROR)
2557 goto out;
2558
2559 mdev = adm_ctx.mdev;
Philipp Reisner43a51822010-06-11 11:26:34 +02002560 if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
2561 drbd_uuid_new_current(mdev);
2562 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner43a51822010-06-11 11:26:34 +02002563 }
Philipp Reisner265be2d2010-05-31 10:14:17 +02002564 drbd_suspend_io(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002565 retcode = drbd_request_state(mdev, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
2566 if (retcode == SS_SUCCESS) {
Philipp Reisner265be2d2010-05-31 10:14:17 +02002567 if (mdev->state.conn < C_CONNECTED)
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01002568 tl_clear(mdev->tconn);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002569 if (mdev->state.disk == D_DISKLESS || mdev->state.disk == D_FAILED)
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01002570 tl_restart(mdev->tconn, FAIL_FROZEN_DISK_IO);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002571 }
2572 drbd_resume_io(mdev);
2573
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002574out:
2575 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002576 return 0;
2577}
2578
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002579int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002580{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002581 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002582}
2583
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002584int nla_put_drbd_cfg_context(struct sk_buff *skb, struct drbd_tconn *tconn, unsigned vnr)
Lars Ellenberg543cc102011-03-10 22:18:18 +01002585{
2586 struct nlattr *nla;
2587 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
2588 if (!nla)
2589 goto nla_put_failure;
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002590 if (vnr != VOLUME_UNSPECIFIED &&
2591 nla_put_u32(skb, T_ctx_volume, vnr))
2592 goto nla_put_failure;
2593 if (nla_put_string(skb, T_ctx_resource_name, tconn->name))
2594 goto nla_put_failure;
2595 if (tconn->my_addr_len &&
2596 nla_put(skb, T_ctx_my_addr, tconn->my_addr_len, &tconn->my_addr))
2597 goto nla_put_failure;
2598 if (tconn->peer_addr_len &&
2599 nla_put(skb, T_ctx_peer_addr, tconn->peer_addr_len, &tconn->peer_addr))
2600 goto nla_put_failure;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002601 nla_nest_end(skb, nla);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002602 return 0;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002603
2604nla_put_failure:
2605 if (nla)
2606 nla_nest_cancel(skb, nla);
2607 return -EMSGSIZE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002608}
2609
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002610int nla_put_status_info(struct sk_buff *skb, struct drbd_conf *mdev,
2611 const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002612{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002613 struct state_info *si = NULL; /* for sizeof(si->member); */
Philipp Reisner44ed1672011-04-19 17:10:19 +02002614 struct net_conf *nc;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002615 struct nlattr *nla;
2616 int got_ldev;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002617 int err = 0;
2618 int exclude_sensitive;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002619
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002620 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2621 * to. So we better exclude_sensitive information.
2622 *
2623 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2624 * in the context of the requesting user process. Exclude sensitive
2625 * information, unless current has superuser.
2626 *
2627 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2628 * relies on the current implementation of netlink_dump(), which
2629 * executes the dump callback successively from netlink_recvmsg(),
2630 * always in the context of the receiving process */
2631 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002632
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002633 got_ldev = get_ldev(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002634
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002635 /* We need to add connection name and volume number information still.
2636 * Minor number is in drbd_genlmsghdr. */
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002637 if (nla_put_drbd_cfg_context(skb, mdev->tconn, mdev->vnr))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002638 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002639
Lars Ellenbergf3990022011-03-23 14:31:09 +01002640 if (res_opts_to_skb(skb, &mdev->tconn->res_opts, exclude_sensitive))
2641 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002642
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002643 rcu_read_lock();
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002644 if (got_ldev)
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002645 if (disk_conf_to_skb(skb, rcu_dereference(mdev->ldev->disk_conf), exclude_sensitive))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002646 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002647
Philipp Reisner44ed1672011-04-19 17:10:19 +02002648 nc = rcu_dereference(mdev->tconn->net_conf);
2649 if (nc)
2650 err = net_conf_to_skb(skb, nc, exclude_sensitive);
2651 rcu_read_unlock();
2652 if (err)
2653 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002654
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002655 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
2656 if (!nla)
2657 goto nla_put_failure;
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002658 if (nla_put_u32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY) ||
2659 nla_put_u32(skb, T_current_state, mdev->state.i) ||
2660 nla_put_u64(skb, T_ed_uuid, mdev->ed_uuid) ||
Philipp Marek3174f8c2012-03-03 21:04:30 +01002661 nla_put_u64(skb, T_capacity, drbd_get_capacity(mdev->this_bdev)) ||
2662 nla_put_u64(skb, T_send_cnt, mdev->send_cnt) ||
2663 nla_put_u64(skb, T_recv_cnt, mdev->recv_cnt) ||
2664 nla_put_u64(skb, T_read_cnt, mdev->read_cnt) ||
2665 nla_put_u64(skb, T_writ_cnt, mdev->writ_cnt) ||
2666 nla_put_u64(skb, T_al_writ_cnt, mdev->al_writ_cnt) ||
2667 nla_put_u64(skb, T_bm_writ_cnt, mdev->bm_writ_cnt) ||
2668 nla_put_u32(skb, T_ap_bio_cnt, atomic_read(&mdev->ap_bio_cnt)) ||
2669 nla_put_u32(skb, T_ap_pending_cnt, atomic_read(&mdev->ap_pending_cnt)) ||
2670 nla_put_u32(skb, T_rs_pending_cnt, atomic_read(&mdev->rs_pending_cnt)))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002671 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002672
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002673 if (got_ldev) {
Philipp Reisner39a1aa7f2012-08-08 21:19:09 +02002674 int err;
2675
2676 spin_lock_irq(&mdev->ldev->md.uuid_lock);
2677 err = nla_put(skb, T_uuids, sizeof(si->uuids), mdev->ldev->md.uuid);
2678 spin_unlock_irq(&mdev->ldev->md.uuid_lock);
2679
2680 if (err)
2681 goto nla_put_failure;
2682
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002683 if (nla_put_u32(skb, T_disk_flags, mdev->ldev->md.flags) ||
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002684 nla_put_u64(skb, T_bits_total, drbd_bm_bits(mdev)) ||
2685 nla_put_u64(skb, T_bits_oos, drbd_bm_total_weight(mdev)))
2686 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002687 if (C_SYNC_SOURCE <= mdev->state.conn &&
2688 C_PAUSED_SYNC_T >= mdev->state.conn) {
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002689 if (nla_put_u64(skb, T_bits_rs_total, mdev->rs_total) ||
2690 nla_put_u64(skb, T_bits_rs_failed, mdev->rs_failed))
2691 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002692 }
2693 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002694
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002695 if (sib) {
2696 switch(sib->sib_reason) {
2697 case SIB_SYNC_PROGRESS:
2698 case SIB_GET_STATUS_REPLY:
2699 break;
2700 case SIB_STATE_CHANGE:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002701 if (nla_put_u32(skb, T_prev_state, sib->os.i) ||
2702 nla_put_u32(skb, T_new_state, sib->ns.i))
2703 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002704 break;
2705 case SIB_HELPER_POST:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002706 if (nla_put_u32(skb, T_helper_exit_code,
2707 sib->helper_exit_code))
2708 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002709 /* fall through */
2710 case SIB_HELPER_PRE:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002711 if (nla_put_string(skb, T_helper, sib->helper_name))
2712 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002713 break;
2714 }
2715 }
2716 nla_nest_end(skb, nla);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002717
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002718 if (0)
2719nla_put_failure:
2720 err = -EMSGSIZE;
2721 if (got_ldev)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002722 put_ldev(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002723 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002724}
2725
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002726int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002727{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002728 enum drbd_ret_code retcode;
2729 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002730
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002731 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2732 if (!adm_ctx.reply_skb)
2733 return retcode;
2734 if (retcode != NO_ERROR)
2735 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002736
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002737 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.mdev, NULL);
2738 if (err) {
2739 nlmsg_free(adm_ctx.reply_skb);
2740 return err;
2741 }
2742out:
2743 drbd_adm_finish(info, retcode);
2744 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002745}
2746
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002747int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002748{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002749 struct drbd_conf *mdev;
2750 struct drbd_genlmsghdr *dh;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002751 struct drbd_tconn *pos = (struct drbd_tconn*)cb->args[0];
2752 struct drbd_tconn *tconn = NULL;
2753 struct drbd_tconn *tmp;
2754 unsigned volume = cb->args[1];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002755
Lars Ellenberg543cc102011-03-10 22:18:18 +01002756 /* Open coded, deferred, iteration:
2757 * list_for_each_entry_safe(tconn, tmp, &drbd_tconns, all_tconn) {
2758 * idr_for_each_entry(&tconn->volumes, mdev, i) {
2759 * ...
2760 * }
2761 * }
2762 * where tconn is cb->args[0];
2763 * and i is cb->args[1];
2764 *
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002765 * cb->args[2] indicates if we shall loop over all resources,
2766 * or just dump all volumes of a single resource.
2767 *
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002768 * This may miss entries inserted after this dump started,
2769 * or entries deleted before they are reached.
Lars Ellenberg543cc102011-03-10 22:18:18 +01002770 *
2771 * We need to make sure the mdev won't disappear while
2772 * we are looking at it, and revalidate our iterators
2773 * on each iteration.
2774 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002775
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02002776 /* synchronize with conn_create()/conn_destroy() */
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02002777 rcu_read_lock();
Lars Ellenberg543cc102011-03-10 22:18:18 +01002778 /* revalidate iterator position */
Philipp Reisnerec0bddb2011-05-04 15:47:01 +02002779 list_for_each_entry_rcu(tmp, &drbd_tconns, all_tconn) {
Lars Ellenberg543cc102011-03-10 22:18:18 +01002780 if (pos == NULL) {
2781 /* first iteration */
2782 pos = tmp;
2783 tconn = pos;
2784 break;
2785 }
2786 if (tmp == pos) {
2787 tconn = pos;
2788 break;
2789 }
2790 }
2791 if (tconn) {
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002792next_tconn:
Lars Ellenberg543cc102011-03-10 22:18:18 +01002793 mdev = idr_get_next(&tconn->volumes, &volume);
2794 if (!mdev) {
2795 /* No more volumes to dump on this tconn.
2796 * Advance tconn iterator. */
Philipp Reisnerec0bddb2011-05-04 15:47:01 +02002797 pos = list_entry_rcu(tconn->all_tconn.next,
2798 struct drbd_tconn, all_tconn);
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002799 /* Did we dump any volume on this tconn yet? */
Lars Ellenberg543cc102011-03-10 22:18:18 +01002800 if (volume != 0) {
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002801 /* If we reached the end of the list,
2802 * or only a single resource dump was requested,
2803 * we are done. */
2804 if (&pos->all_tconn == &drbd_tconns || cb->args[2])
2805 goto out;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002806 volume = 0;
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002807 tconn = pos;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002808 goto next_tconn;
2809 }
2810 }
2811
Philipp Reisner98683652012-11-09 14:18:43 +01002812 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002813 cb->nlh->nlmsg_seq, &drbd_genl_family,
2814 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
2815 if (!dh)
Lars Ellenberg543cc102011-03-10 22:18:18 +01002816 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002817
Lars Ellenberg543cc102011-03-10 22:18:18 +01002818 if (!mdev) {
Lars Ellenberg367d675d2011-07-11 23:49:55 +02002819 /* This is a tconn without a single volume.
2820 * Suprisingly enough, it may have a network
2821 * configuration. */
2822 struct net_conf *nc;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002823 dh->minor = -1U;
2824 dh->ret_code = NO_ERROR;
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002825 if (nla_put_drbd_cfg_context(skb, tconn, VOLUME_UNSPECIFIED))
Lars Ellenberg367d675d2011-07-11 23:49:55 +02002826 goto cancel;
2827 nc = rcu_dereference(tconn->net_conf);
2828 if (nc && net_conf_to_skb(skb, nc, 1) != 0)
2829 goto cancel;
2830 goto done;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002831 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002832
Lars Ellenberg543cc102011-03-10 22:18:18 +01002833 D_ASSERT(mdev->vnr == volume);
2834 D_ASSERT(mdev->tconn == tconn);
2835
2836 dh->minor = mdev_to_minor(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002837 dh->ret_code = NO_ERROR;
2838
2839 if (nla_put_status_info(skb, mdev, NULL)) {
Lars Ellenberg367d675d2011-07-11 23:49:55 +02002840cancel:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002841 genlmsg_cancel(skb, dh);
Lars Ellenberg543cc102011-03-10 22:18:18 +01002842 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002843 }
Lars Ellenberg367d675d2011-07-11 23:49:55 +02002844done:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002845 genlmsg_end(skb, dh);
2846 }
2847
Lars Ellenberg543cc102011-03-10 22:18:18 +01002848out:
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02002849 rcu_read_unlock();
Lars Ellenberg543cc102011-03-10 22:18:18 +01002850 /* where to start the next iteration */
2851 cb->args[0] = (long)pos;
2852 cb->args[1] = (pos == tconn) ? volume + 1 : 0;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002853
Lars Ellenberg543cc102011-03-10 22:18:18 +01002854 /* No more tconns/volumes/minors found results in an empty skb.
2855 * Which will terminate the dump. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002856 return skb->len;
2857}
2858
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002859/*
2860 * Request status of all resources, or of all volumes within a single resource.
2861 *
2862 * This is a dump, as the answer may not fit in a single reply skb otherwise.
2863 * Which means we cannot use the family->attrbuf or other such members, because
2864 * dump is NOT protected by the genl_lock(). During dump, we only have access
2865 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
2866 *
2867 * Once things are setup properly, we call into get_one_status().
Philipp Reisnerb411b362009-09-25 16:07:19 -07002868 */
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002869int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002870{
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002871 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
2872 struct nlattr *nla;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02002873 const char *resource_name;
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002874 struct drbd_tconn *tconn;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02002875 int maxtype;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002876
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002877 /* Is this a followup call? */
2878 if (cb->args[0]) {
2879 /* ... of a single resource dump,
2880 * and the resource iterator has been advanced already? */
2881 if (cb->args[2] && cb->args[2] != cb->args[0])
2882 return 0; /* DONE. */
2883 goto dump;
2884 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002885
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002886 /* First call (from netlink_dump_start). We need to figure out
2887 * which resource(s) the user wants us to dump. */
2888 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
2889 nlmsg_attrlen(cb->nlh, hdrlen),
2890 DRBD_NLA_CFG_CONTEXT);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002891
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002892 /* No explicit context given. Dump all. */
2893 if (!nla)
2894 goto dump;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02002895 maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
2896 nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
2897 if (IS_ERR(nla))
2898 return PTR_ERR(nla);
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002899 /* context given, but no name present? */
2900 if (!nla)
2901 return -EINVAL;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02002902 resource_name = nla_data(nla);
2903 tconn = conn_get_by_name(resource_name);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002904
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002905 if (!tconn)
2906 return -ENODEV;
2907
Philipp Reisner0ace9df2011-04-24 10:53:19 +02002908 kref_put(&tconn->kref, &conn_destroy); /* get_one_status() (re)validates tconn by itself */
2909
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002910 /* prime iterators, and set "filter" mode mark:
2911 * only dump this tconn. */
2912 cb->args[0] = (long)tconn;
2913 /* cb->args[1] = 0; passed in this way. */
2914 cb->args[2] = (long)tconn;
2915
2916dump:
2917 return get_one_status(skb, cb);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002918}
2919
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002920int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002921{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002922 enum drbd_ret_code retcode;
2923 struct timeout_parms tp;
2924 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002925
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002926 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2927 if (!adm_ctx.reply_skb)
2928 return retcode;
2929 if (retcode != NO_ERROR)
2930 goto out;
2931
2932 tp.timeout_type =
2933 adm_ctx.mdev->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
2934 test_bit(USE_DEGR_WFC_T, &adm_ctx.mdev->flags) ? UT_DEGRADED :
2935 UT_DEFAULT;
2936
2937 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
2938 if (err) {
2939 nlmsg_free(adm_ctx.reply_skb);
2940 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002941 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002942out:
2943 drbd_adm_finish(info, retcode);
2944 return 0;
2945}
Lars Ellenberg873b0d52011-01-21 22:53:48 +01002946
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002947int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
2948{
2949 struct drbd_conf *mdev;
2950 enum drbd_ret_code retcode;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02002951 struct start_ov_parms parms;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002952
2953 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2954 if (!adm_ctx.reply_skb)
2955 return retcode;
2956 if (retcode != NO_ERROR)
2957 goto out;
2958
2959 mdev = adm_ctx.mdev;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02002960
2961 /* resume from last known position, if possible */
2962 parms.ov_start_sector = mdev->ov_start_sector;
2963 parms.ov_stop_sector = ULLONG_MAX;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002964 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002965 int err = start_ov_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002966 if (err) {
2967 retcode = ERR_MANDATORY_TAG;
2968 drbd_msg_put_info(from_attrs_err_to_txt(err));
2969 goto out;
2970 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002971 }
Lars Ellenberg58ffa582012-07-26 14:09:49 +02002972 /* w_make_ov_request expects position to be aligned */
2973 mdev->ov_start_sector = parms.ov_start_sector & ~(BM_SECT_PER_BIT-1);
2974 mdev->ov_stop_sector = parms.ov_stop_sector;
Lars Ellenberg873b0d52011-01-21 22:53:48 +01002975
2976 /* If there is still bitmap IO pending, e.g. previous resync or verify
2977 * just being finished, wait for it before requesting a new resync. */
Lars Ellenberga574daf2012-04-25 16:27:35 +02002978 drbd_suspend_io(mdev);
Lars Ellenberg873b0d52011-01-21 22:53:48 +01002979 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002980 retcode = drbd_request_state(mdev,NS(conn,C_VERIFY_S));
Lars Ellenberga574daf2012-04-25 16:27:35 +02002981 drbd_resume_io(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002982out:
2983 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002984 return 0;
2985}
2986
2987
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002988int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002989{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002990 struct drbd_conf *mdev;
2991 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002992 int skip_initial_sync = 0;
2993 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002994 struct new_c_uuid_parms args;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002995
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002996 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2997 if (!adm_ctx.reply_skb)
2998 return retcode;
2999 if (retcode != NO_ERROR)
3000 goto out_nolock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003001
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003002 mdev = adm_ctx.mdev;
3003 memset(&args, 0, sizeof(args));
3004 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01003005 err = new_c_uuid_parms_from_attrs(&args, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003006 if (err) {
3007 retcode = ERR_MANDATORY_TAG;
3008 drbd_msg_put_info(from_attrs_err_to_txt(err));
3009 goto out_nolock;
3010 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003011 }
3012
Philipp Reisner8410da82011-02-11 20:11:10 +01003013 mutex_lock(mdev->state_mutex); /* Protects us against serialized state changes. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003014
3015 if (!get_ldev(mdev)) {
3016 retcode = ERR_NO_DISK;
3017 goto out;
3018 }
3019
3020 /* this is "skip initial sync", assume to be clean */
Philipp Reisner31890f42011-01-19 14:12:51 +01003021 if (mdev->state.conn == C_CONNECTED && mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003022 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
3023 dev_info(DEV, "Preparing to skip initial sync\n");
3024 skip_initial_sync = 1;
3025 } else if (mdev->state.conn != C_STANDALONE) {
3026 retcode = ERR_CONNECTED;
3027 goto out_dec;
3028 }
3029
3030 drbd_uuid_set(mdev, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
3031 drbd_uuid_new_current(mdev); /* New current, previous to UI_BITMAP */
3032
3033 if (args.clear_bm) {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003034 err = drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
3035 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003036 if (err) {
3037 dev_err(DEV, "Writing bitmap failed with %d\n",err);
3038 retcode = ERR_IO_MD_DISK;
3039 }
3040 if (skip_initial_sync) {
3041 drbd_send_uuids_skip_initial_sync(mdev);
3042 _drbd_uuid_set(mdev, UI_BITMAP, 0);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003043 drbd_print_uuids(mdev, "cleared bitmap UUID");
Philipp Reisner87eeee42011-01-19 14:16:30 +01003044 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003045 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3046 CS_VERBOSE, NULL);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003047 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003048 }
3049 }
3050
3051 drbd_md_sync(mdev);
3052out_dec:
3053 put_ldev(mdev);
3054out:
Philipp Reisner8410da82011-02-11 20:11:10 +01003055 mutex_unlock(mdev->state_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003056out_nolock:
3057 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003058 return 0;
3059}
3060
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003061static enum drbd_ret_code
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003062drbd_check_resource_name(const char *name)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003063{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003064 if (!name || !name[0]) {
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003065 drbd_msg_put_info("resource name missing");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003066 return ERR_MANDATORY_TAG;
3067 }
3068 /* if we want to use these in sysfs/configfs/debugfs some day,
3069 * we must not allow slashes */
3070 if (strchr(name, '/')) {
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003071 drbd_msg_put_info("invalid resource name");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003072 return ERR_INVALID_REQUEST;
3073 }
3074 return NO_ERROR;
3075}
Philipp Reisnerb411b362009-09-25 16:07:19 -07003076
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003077int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003078{
3079 enum drbd_ret_code retcode;
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003080 struct res_opts res_opts;
3081 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003082
3083 retcode = drbd_adm_prepare(skb, info, 0);
3084 if (!adm_ctx.reply_skb)
3085 return retcode;
3086 if (retcode != NO_ERROR)
3087 goto out;
3088
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003089 set_res_opts_defaults(&res_opts);
3090 err = res_opts_from_attrs(&res_opts, info);
3091 if (err && err != -ENOMSG) {
3092 retcode = ERR_MANDATORY_TAG;
3093 drbd_msg_put_info(from_attrs_err_to_txt(err));
3094 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003095 }
3096
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003097 retcode = drbd_check_resource_name(adm_ctx.resource_name);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003098 if (retcode != NO_ERROR)
3099 goto out;
3100
3101 if (adm_ctx.tconn) {
Lars Ellenberg38f19612011-03-14 13:22:35 +01003102 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
3103 retcode = ERR_INVALID_REQUEST;
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003104 drbd_msg_put_info("resource exists");
Lars Ellenberg38f19612011-03-14 13:22:35 +01003105 }
3106 /* else: still NO_ERROR */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003107 goto out;
Philipp Reisner9f5180e2009-10-06 09:30:14 +02003108 }
3109
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003110 if (!conn_create(adm_ctx.resource_name, &res_opts))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003111 retcode = ERR_NOMEM;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003112out:
3113 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003114 return 0;
3115}
3116
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003117int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003118{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003119 struct drbd_genlmsghdr *dh = info->userhdr;
3120 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003121
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02003122 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003123 if (!adm_ctx.reply_skb)
3124 return retcode;
3125 if (retcode != NO_ERROR)
3126 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003127
Andreas Gruenbacherf2257a52011-07-14 16:00:40 +02003128 if (dh->minor > MINORMASK) {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003129 drbd_msg_put_info("requested minor out of range");
3130 retcode = ERR_INVALID_REQUEST;
3131 goto out;
3132 }
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +02003133 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003134 drbd_msg_put_info("requested volume id out of range");
3135 retcode = ERR_INVALID_REQUEST;
3136 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05003137 }
3138
Lars Ellenberg38f19612011-03-14 13:22:35 +01003139 /* drbd_adm_prepare made sure already
3140 * that mdev->tconn and mdev->vnr match the request. */
3141 if (adm_ctx.mdev) {
3142 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
3143 retcode = ERR_MINOR_EXISTS;
3144 /* else: still NO_ERROR */
3145 goto out;
3146 }
3147
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003148 retcode = conn_new_minor(adm_ctx.tconn, dh->minor, adm_ctx.volume);
3149out:
3150 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003151 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003152}
3153
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003154static enum drbd_ret_code adm_delete_minor(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003155{
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003156 if (mdev->state.disk == D_DISKLESS &&
3157 /* no need to be mdev->state.conn == C_STANDALONE &&
3158 * we may want to delete a minor from a live replication group.
3159 */
3160 mdev->state.role == R_SECONDARY) {
Philipp Reisner369bea62011-07-06 23:04:44 +02003161 _drbd_request_state(mdev, NS(conn, C_WF_REPORT_PARAMS),
3162 CS_VERBOSE + CS_WAIT_COMPLETE);
Philipp Reisner81fa2e62011-05-04 15:10:30 +02003163 idr_remove(&mdev->tconn->volumes, mdev->vnr);
3164 idr_remove(&minors, mdev_to_minor(mdev));
3165 del_gendisk(mdev->vdisk);
3166 synchronize_rcu();
3167 kref_put(&mdev->kref, &drbd_minor_destroy);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003168 return NO_ERROR;
3169 } else
3170 return ERR_MINOR_CONFIGURED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003171}
3172
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003173int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05003174{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003175 enum drbd_ret_code retcode;
3176
3177 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3178 if (!adm_ctx.reply_skb)
3179 return retcode;
3180 if (retcode != NO_ERROR)
3181 goto out;
3182
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003183 retcode = adm_delete_minor(adm_ctx.mdev);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003184out:
3185 drbd_adm_finish(info, retcode);
3186 return 0;
3187}
3188
3189int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
3190{
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003191 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003192 struct drbd_conf *mdev;
3193 unsigned i;
3194
3195 retcode = drbd_adm_prepare(skb, info, 0);
3196 if (!adm_ctx.reply_skb)
3197 return retcode;
3198 if (retcode != NO_ERROR)
3199 goto out;
3200
3201 if (!adm_ctx.tconn) {
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003202 retcode = ERR_RES_NOT_KNOWN;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003203 goto out;
3204 }
3205
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003206 /* demote */
3207 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3208 retcode = drbd_set_role(mdev, R_SECONDARY, 0);
3209 if (retcode < SS_SUCCESS) {
3210 drbd_msg_put_info("failed to demote");
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003211 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003212 }
3213 }
3214
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003215 retcode = conn_try_disconnect(adm_ctx.tconn, 0);
3216 if (retcode < SS_SUCCESS) {
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003217 drbd_msg_put_info("failed to disconnect");
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003218 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003219 }
3220
3221 /* detach */
3222 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
Philipp Reisnercdfda632011-07-05 15:38:59 +02003223 retcode = adm_detach(mdev, 0);
Lars Ellenberg27012382012-07-24 10:13:55 +02003224 if (retcode < SS_SUCCESS || retcode > NO_ERROR) {
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003225 drbd_msg_put_info("failed to detach");
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003226 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003227 }
3228 }
3229
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003230 /* If we reach this, all volumes (of this tconn) are Secondary,
3231 * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003232 * actually stopped, state handling only does drbd_thread_stop_nowait(). */
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003233 drbd_thread_stop(&adm_ctx.tconn->worker);
3234
3235 /* Now, nothing can fail anymore */
3236
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003237 /* delete volumes */
3238 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3239 retcode = adm_delete_minor(mdev);
3240 if (retcode != NO_ERROR) {
3241 /* "can not happen" */
3242 drbd_msg_put_info("failed to delete volume");
Philipp Reisneref356262011-04-13 14:21:29 -07003243 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003244 }
3245 }
3246
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003247 /* delete connection */
3248 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
Philipp Reisnerec0bddb2011-05-04 15:47:01 +02003249 list_del_rcu(&adm_ctx.tconn->all_tconn);
3250 synchronize_rcu();
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02003251 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
3252
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003253 retcode = NO_ERROR;
3254 } else {
3255 /* "can not happen" */
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003256 retcode = ERR_RES_IN_USE;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003257 drbd_msg_put_info("failed to delete connection");
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003258 }
Philipp Reisneref356262011-04-13 14:21:29 -07003259 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003260out:
3261 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003262 return 0;
3263}
3264
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003265int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05003266{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003267 enum drbd_ret_code retcode;
3268
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02003269 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003270 if (!adm_ctx.reply_skb)
3271 return retcode;
3272 if (retcode != NO_ERROR)
3273 goto out;
3274
3275 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
Philipp Reisnerec0bddb2011-05-04 15:47:01 +02003276 list_del_rcu(&adm_ctx.tconn->all_tconn);
3277 synchronize_rcu();
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02003278 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
3279
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003280 retcode = NO_ERROR;
Philipp Reisner774b3052011-02-22 02:07:03 -05003281 } else {
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003282 retcode = ERR_RES_IN_USE;
Philipp Reisner774b3052011-02-22 02:07:03 -05003283 }
3284
Lars Ellenberg992d6e92011-05-02 11:47:18 +02003285 if (retcode == NO_ERROR)
3286 drbd_thread_stop(&adm_ctx.tconn->worker);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003287out:
3288 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003289 return 0;
3290}
3291
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003292void drbd_bcast_event(struct drbd_conf *mdev, const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003293{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003294 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
3295 struct sk_buff *msg;
3296 struct drbd_genlmsghdr *d_out;
3297 unsigned seq;
3298 int err = -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003299
Philipp Reisneref86b772012-12-06 10:34:34 +01003300 if (sib->sib_reason == SIB_SYNC_PROGRESS) {
3301 if (time_after(jiffies, mdev->rs_last_bcast + HZ))
3302 mdev->rs_last_bcast = jiffies;
3303 else
3304 return;
3305 }
Philipp Reisner328e0f122012-10-19 14:37:47 +02003306
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003307 seq = atomic_inc_return(&drbd_genl_seq);
3308 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
3309 if (!msg)
3310 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003311
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003312 err = -EMSGSIZE;
3313 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
3314 if (!d_out) /* cannot happen, but anyways. */
3315 goto nla_put_failure;
3316 d_out->minor = mdev_to_minor(mdev);
Andreas Gruenbacher6f9b5f82011-05-06 01:03:32 +02003317 d_out->ret_code = NO_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003318
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003319 if (nla_put_status_info(msg, mdev, sib))
3320 goto nla_put_failure;
3321 genlmsg_end(msg, d_out);
3322 err = drbd_genl_multicast_events(msg, 0);
3323 /* msg has been consumed or freed in netlink_broadcast() */
3324 if (err && err != -ESRCH)
3325 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003326
Philipp Reisnerb411b362009-09-25 16:07:19 -07003327 return;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003328
3329nla_put_failure:
3330 nlmsg_free(msg);
3331failed:
3332 dev_err(DEV, "Error %d while broadcasting event. "
3333 "Event seq:%u sib_reason:%u\n",
3334 err, seq, sib->sib_reason);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003335}