blob: 526414bc2cab18db5fb290189cccfa8951af4050 [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"
Andreas Gruenbachera3603a62011-05-30 11:47:37 +020035#include "drbd_protocol.h"
Philipp Reisner265be2d2010-05-31 10:14:17 +020036#include "drbd_req.h"
Philipp Reisnerb411b362009-09-25 16:07:19 -070037#include "drbd_wrappers.h"
38#include <asm/unaligned.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070039#include <linux/drbd_limits.h>
Philipp Reisner87f7be42010-06-11 13:56:33 +020040#include <linux/kthread.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070041
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010042#include <net/genetlink.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070043
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010044/* .doit */
45// int drbd_adm_create_resource(struct sk_buff *skb, struct genl_info *info);
46// int drbd_adm_delete_resource(struct sk_buff *skb, struct genl_info *info);
47
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +020048int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info);
49int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010050
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +020051int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info);
52int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +010053int drbd_adm_down(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010054
55int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info);
56int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info);
Lars Ellenbergf3990022011-03-23 14:31:09 +010057int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010058int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info);
59int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info);
Lars Ellenbergf3990022011-03-23 14:31:09 +010060int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010061int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info);
62int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info);
63int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info);
64int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info);
65int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info);
66int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info);
67int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info);
68int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info);
69int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info);
70int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info);
71int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info);
Lars Ellenbergf3990022011-03-23 14:31:09 +010072int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010073int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info);
74int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info);
75/* .dumpit */
76int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb);
77
78#include <linux/drbd_genl_api.h>
Andreas Gruenbacher01b39b52011-06-10 12:57:26 +020079#include "drbd_nla.h"
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010080#include <linux/genl_magic_func.h>
81
82/* used blkdev_get_by_path, to claim our meta data device(s) */
Philipp Reisnerb411b362009-09-25 16:07:19 -070083static char *drbd_m_holder = "Hands off! this is DRBD's meta data device.";
84
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010085/* Configuration is strictly serialized, because generic netlink message
86 * processing is strictly serialized by the genl_lock().
87 * Which means we can use one static global drbd_config_context struct.
88 */
89static struct drbd_config_context {
90 /* assigned from drbd_genlmsghdr */
91 unsigned int minor;
92 /* assigned from request attributes, if present */
93 unsigned int volume;
94#define VOLUME_UNSPECIFIED (-1U)
95 /* pointer into the request skb,
96 * limited lifetime! */
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +020097 char *resource_name;
Andreas Gruenbacher089c0752011-06-14 18:28:09 +020098 struct nlattr *my_addr;
99 struct nlattr *peer_addr;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700100
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100101 /* reply buffer */
102 struct sk_buff *reply_skb;
103 /* pointer into reply buffer */
104 struct drbd_genlmsghdr *reply_dh;
105 /* resolved from attributes, if possible */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200106 struct drbd_device *device;
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +0200107 struct drbd_resource *resource;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200108 struct drbd_connection *connection;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100109} adm_ctx;
110
111static void drbd_adm_send_reply(struct sk_buff *skb, struct genl_info *info)
112{
113 genlmsg_end(skb, genlmsg_data(nlmsg_data(nlmsg_hdr(skb))));
114 if (genlmsg_reply(skb, info))
115 printk(KERN_ERR "drbd: error sending genl reply\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700116}
117
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100118/* Used on a fresh "drbd_adm_prepare"d reply_skb, this cannot fail: The only
119 * reason it could fail was no space in skb, and there are 4k available. */
Lars Ellenberg8432b312011-03-08 16:11:16 +0100120int drbd_msg_put_info(const char *info)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100121{
122 struct sk_buff *skb = adm_ctx.reply_skb;
123 struct nlattr *nla;
124 int err = -EMSGSIZE;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700125
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100126 if (!info || !info[0])
127 return 0;
128
129 nla = nla_nest_start(skb, DRBD_NLA_CFG_REPLY);
130 if (!nla)
131 return err;
132
133 err = nla_put_string(skb, T_info_text, info);
134 if (err) {
135 nla_nest_cancel(skb, nla);
136 return err;
137 } else
138 nla_nest_end(skb, nla);
139 return 0;
140}
141
142/* This would be a good candidate for a "pre_doit" hook,
143 * and per-family private info->pointers.
144 * But we need to stay compatible with older kernels.
145 * If it returns successfully, adm_ctx members are valid.
146 */
147#define DRBD_ADM_NEED_MINOR 1
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +0200148#define DRBD_ADM_NEED_RESOURCE 2
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200149#define DRBD_ADM_NEED_CONNECTION 4
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100150static int drbd_adm_prepare(struct sk_buff *skb, struct genl_info *info,
151 unsigned flags)
152{
153 struct drbd_genlmsghdr *d_in = info->userhdr;
154 const u8 cmd = info->genlhdr->cmd;
155 int err;
156
157 memset(&adm_ctx, 0, sizeof(adm_ctx));
158
159 /* genl_rcv_msg only checks for CAP_NET_ADMIN on "GENL_ADMIN_PERM" :( */
Philipp Reisner98683652012-11-09 14:18:43 +0100160 if (cmd != DRBD_ADM_GET_STATUS && !capable(CAP_NET_ADMIN))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100161 return -EPERM;
162
163 adm_ctx.reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200164 if (!adm_ctx.reply_skb) {
165 err = -ENOMEM;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100166 goto fail;
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200167 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100168
169 adm_ctx.reply_dh = genlmsg_put_reply(adm_ctx.reply_skb,
170 info, &drbd_genl_family, 0, cmd);
171 /* put of a few bytes into a fresh skb of >= 4k will always succeed.
172 * but anyways */
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200173 if (!adm_ctx.reply_dh) {
174 err = -ENOMEM;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100175 goto fail;
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200176 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100177
178 adm_ctx.reply_dh->minor = d_in->minor;
179 adm_ctx.reply_dh->ret_code = NO_ERROR;
180
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200181 adm_ctx.volume = VOLUME_UNSPECIFIED;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100182 if (info->attrs[DRBD_NLA_CFG_CONTEXT]) {
183 struct nlattr *nla;
184 /* parse and validate only */
Lars Ellenbergf3990022011-03-23 14:31:09 +0100185 err = drbd_cfg_context_from_attrs(NULL, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100186 if (err)
187 goto fail;
188
189 /* It was present, and valid,
190 * copy it over to the reply skb. */
191 err = nla_put_nohdr(adm_ctx.reply_skb,
192 info->attrs[DRBD_NLA_CFG_CONTEXT]->nla_len,
193 info->attrs[DRBD_NLA_CFG_CONTEXT]);
194 if (err)
195 goto fail;
196
197 /* and assign stuff to the global adm_ctx */
198 nla = nested_attr_tb[__nla_type(T_ctx_volume)];
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200199 if (nla)
200 adm_ctx.volume = nla_get_u32(nla);
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +0200201 nla = nested_attr_tb[__nla_type(T_ctx_resource_name)];
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100202 if (nla)
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +0200203 adm_ctx.resource_name = nla_data(nla);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200204 adm_ctx.my_addr = nested_attr_tb[__nla_type(T_ctx_my_addr)];
205 adm_ctx.peer_addr = nested_attr_tb[__nla_type(T_ctx_peer_addr)];
206 if ((adm_ctx.my_addr &&
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200207 nla_len(adm_ctx.my_addr) > sizeof(adm_ctx.connection->my_addr)) ||
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200208 (adm_ctx.peer_addr &&
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200209 nla_len(adm_ctx.peer_addr) > sizeof(adm_ctx.connection->peer_addr))) {
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200210 err = -EINVAL;
211 goto fail;
212 }
213 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100214
215 adm_ctx.minor = d_in->minor;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200216 adm_ctx.device = minor_to_device(d_in->minor);
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +0200217 if (adm_ctx.resource_name) {
218 adm_ctx.resource = drbd_find_resource(adm_ctx.resource_name);
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +0200219 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100220
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200221 if (!adm_ctx.device && (flags & DRBD_ADM_NEED_MINOR)) {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100222 drbd_msg_put_info("unknown minor");
223 return ERR_MINOR_INVALID;
224 }
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +0200225 if (!adm_ctx.resource && (flags & DRBD_ADM_NEED_RESOURCE)) {
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +0200226 drbd_msg_put_info("unknown resource");
Andreas Gruenbachera10f6b82011-06-09 01:26:16 +0200227 if (adm_ctx.resource_name)
228 return ERR_RES_NOT_KNOWN;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100229 return ERR_INVALID_REQUEST;
230 }
231
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200232 if (flags & DRBD_ADM_NEED_CONNECTION) {
Andreas Gruenbacher3ab706f2011-07-06 15:05:58 +0200233 if (adm_ctx.resource) {
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200234 drbd_msg_put_info("no resource name expected");
235 return ERR_INVALID_REQUEST;
236 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200237 if (adm_ctx.device) {
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200238 drbd_msg_put_info("no minor number expected");
239 return ERR_INVALID_REQUEST;
240 }
241 if (adm_ctx.my_addr && adm_ctx.peer_addr)
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200242 adm_ctx.connection = conn_get_by_addrs(nla_data(adm_ctx.my_addr),
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200243 nla_len(adm_ctx.my_addr),
244 nla_data(adm_ctx.peer_addr),
245 nla_len(adm_ctx.peer_addr));
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200246 if (!adm_ctx.connection) {
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200247 drbd_msg_put_info("unknown connection");
248 return ERR_INVALID_REQUEST;
249 }
250 }
251
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100252 /* some more paranoia, if the request was over-determined */
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +0200253 if (adm_ctx.device && adm_ctx.resource &&
254 adm_ctx.device->resource != adm_ctx.resource) {
255 pr_warning("request: minor=%u, resource=%s; but that minor belongs to resource %s\n",
256 adm_ctx.minor, adm_ctx.resource->name,
Andreas Gruenbacherd8628a82011-06-09 01:38:00 +0200257 adm_ctx.device->resource->name);
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +0200258 drbd_msg_put_info("minor exists in different resource");
Lars Ellenberg527f4b22011-03-14 13:58:03 +0100259 return ERR_INVALID_REQUEST;
260 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200261 if (adm_ctx.device &&
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100262 adm_ctx.volume != VOLUME_UNSPECIFIED &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200263 adm_ctx.volume != adm_ctx.device->vnr) {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100264 pr_warning("request: minor=%u, volume=%u; but that minor is volume %u in %s\n",
265 adm_ctx.minor, adm_ctx.volume,
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +0200266 adm_ctx.device->vnr,
Andreas Gruenbacherd8628a82011-06-09 01:38:00 +0200267 adm_ctx.device->resource->name);
Lars Ellenberg527f4b22011-03-14 13:58:03 +0100268 drbd_msg_put_info("minor exists as different volume");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100269 return ERR_INVALID_REQUEST;
270 }
Philipp Reisner0ace9df2011-04-24 10:53:19 +0200271
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100272 return NO_ERROR;
273
274fail:
275 nlmsg_free(adm_ctx.reply_skb);
276 adm_ctx.reply_skb = NULL;
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200277 return err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100278}
279
280static int drbd_adm_finish(struct genl_info *info, int retcode)
281{
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200282 if (adm_ctx.connection) {
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +0200283 kref_put(&adm_ctx.connection->kref, drbd_destroy_connection);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200284 adm_ctx.connection = NULL;
Philipp Reisner0ace9df2011-04-24 10:53:19 +0200285 }
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +0200286 if (adm_ctx.resource) {
287 kref_put(&adm_ctx.resource->kref, drbd_destroy_resource);
288 adm_ctx.resource = NULL;
289 }
Philipp Reisner0ace9df2011-04-24 10:53:19 +0200290
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100291 if (!adm_ctx.reply_skb)
292 return -ENOMEM;
293
294 adm_ctx.reply_dh->ret_code = retcode;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100295 drbd_adm_send_reply(adm_ctx.reply_skb, info);
296 return 0;
297}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700298
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200299static void setup_khelper_env(struct drbd_connection *connection, char **envp)
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100300{
301 char *afs;
302
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200303 /* FIXME: A future version will not allow this case. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200304 if (connection->my_addr_len == 0 || connection->peer_addr_len == 0)
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200305 return;
306
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200307 switch (((struct sockaddr *)&connection->peer_addr)->sa_family) {
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200308 case AF_INET6:
309 afs = "ipv6";
310 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200311 &((struct sockaddr_in6 *)&connection->peer_addr)->sin6_addr);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200312 break;
313 case AF_INET:
314 afs = "ipv4";
315 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200316 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200317 break;
318 default:
319 afs = "ssocks";
320 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200321 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100322 }
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200323 snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100324}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700325
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200326int drbd_khelper(struct drbd_device *device, char *cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700327{
328 char *envp[] = { "HOME=/",
329 "TERM=linux",
330 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100331 (char[20]) { }, /* address family */
332 (char[60]) { }, /* address */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700333 NULL };
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100334 char mb[12];
Philipp Reisnerb411b362009-09-25 16:07:19 -0700335 char *argv[] = {usermode_helper, cmd, mb, NULL };
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200336 struct drbd_connection *connection = first_peer_device(device)->connection;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100337 struct sib_info sib;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700338 int ret;
339
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200340 if (current == connection->worker.task)
341 set_bit(CALLBACK_PENDING, &connection->flags);
Lars Ellenbergc2ba6862012-06-14 15:14:06 +0200342
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200343 snprintf(mb, 12, "minor-%d", device_to_minor(device));
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200344 setup_khelper_env(connection, envp);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700345
Lars Ellenberg1090c052010-07-19 17:41:04 +0200346 /* The helper may take some time.
347 * write out any unsynced meta data changes now */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200348 drbd_md_sync(device);
Lars Ellenberg1090c052010-07-19 17:41:04 +0200349
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200350 drbd_info(device, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100351 sib.sib_reason = SIB_HELPER_PRE;
352 sib.helper_name = cmd;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200353 drbd_bcast_event(device, &sib);
Oleg Nesterov70834d32012-03-23 15:02:46 -0700354 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700355 if (ret)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200356 drbd_warn(device, "helper command: %s %s %s exit code %u (0x%x)\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -0700357 usermode_helper, cmd, mb,
358 (ret >> 8) & 0xff, ret);
359 else
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200360 drbd_info(device, "helper command: %s %s %s exit code %u (0x%x)\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -0700361 usermode_helper, cmd, mb,
362 (ret >> 8) & 0xff, ret);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100363 sib.sib_reason = SIB_HELPER_POST;
364 sib.helper_exit_code = ret;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200365 drbd_bcast_event(device, &sib);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700366
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200367 if (current == connection->worker.task)
368 clear_bit(CALLBACK_PENDING, &connection->flags);
Lars Ellenbergc2ba6862012-06-14 15:14:06 +0200369
Philipp Reisnerb411b362009-09-25 16:07:19 -0700370 if (ret < 0) /* Ignore any ERRNOs we got. */
371 ret = 0;
372
373 return ret;
374}
375
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200376static int conn_khelper(struct drbd_connection *connection, char *cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700377{
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100378 char *envp[] = { "HOME=/",
379 "TERM=linux",
380 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
381 (char[20]) { }, /* address family */
382 (char[60]) { }, /* address */
383 NULL };
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +0200384 char *resource_name = connection->resource->name;
385 char *argv[] = {usermode_helper, cmd, resource_name, NULL };
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100386 int ret;
387
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200388 setup_khelper_env(connection, envp);
389 conn_md_sync(connection);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100390
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200391 drbd_info(connection, "helper command: %s %s %s\n", usermode_helper, cmd, resource_name);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100392 /* TODO: conn_bcast_event() ?? */
393
Philipp Reisner98683652012-11-09 14:18:43 +0100394 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100395 if (ret)
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200396 drbd_warn(connection, "helper command: %s %s %s exit code %u (0x%x)\n",
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +0200397 usermode_helper, cmd, resource_name,
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100398 (ret >> 8) & 0xff, ret);
399 else
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200400 drbd_info(connection, "helper command: %s %s %s exit code %u (0x%x)\n",
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +0200401 usermode_helper, cmd, resource_name,
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100402 (ret >> 8) & 0xff, ret);
403 /* TODO: conn_bcast_event() ?? */
404
405 if (ret < 0) /* Ignore any ERRNOs we got. */
406 ret = 0;
407
408 return ret;
409}
410
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200411static enum drbd_fencing_p highest_fencing_policy(struct drbd_connection *connection)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700412{
Philipp Reisnercb703452011-03-24 11:03:07 +0100413 enum drbd_fencing_p fp = FP_NOT_AVAIL;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200414 struct drbd_peer_device *peer_device;
Philipp Reisnercb703452011-03-24 11:03:07 +0100415 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700416
Philipp Reisner695d08f2011-04-11 22:53:32 -0700417 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200418 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
419 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200420 if (get_ldev_if_state(device, D_CONSISTENT)) {
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200421 struct disk_conf *disk_conf =
422 rcu_dereference(peer_device->device->ldev->disk_conf);
423 fp = max_t(enum drbd_fencing_p, fp, disk_conf->fencing);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200424 put_ldev(device);
Philipp Reisnercb703452011-03-24 11:03:07 +0100425 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700426 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700427 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700428
Philipp Reisnercb703452011-03-24 11:03:07 +0100429 return fp;
430}
431
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200432bool conn_try_outdate_peer(struct drbd_connection *connection)
Philipp Reisnercb703452011-03-24 11:03:07 +0100433{
Philipp Reisner28e448b2013-06-25 16:50:06 +0200434 unsigned int connect_cnt;
Philipp Reisnercb703452011-03-24 11:03:07 +0100435 union drbd_state mask = { };
436 union drbd_state val = { };
437 enum drbd_fencing_p fp;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700438 char *ex_to_string;
439 int r;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700440
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200441 if (connection->cstate >= C_WF_REPORT_PARAMS) {
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200442 drbd_err(connection, "Expected cstate < C_WF_REPORT_PARAMS\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100443 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700444 }
445
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200446 spin_lock_irq(&connection->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200447 connect_cnt = connection->connect_cnt;
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200448 spin_unlock_irq(&connection->resource->req_lock);
Philipp Reisner28e448b2013-06-25 16:50:06 +0200449
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200450 fp = highest_fencing_policy(connection);
Philipp Reisnercb703452011-03-24 11:03:07 +0100451 switch (fp) {
452 case FP_NOT_AVAIL:
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200453 drbd_warn(connection, "Not fencing peer, I'm not even Consistent myself.\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100454 goto out;
455 case FP_DONT_CARE:
456 return true;
457 default: ;
458 }
459
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200460 r = conn_khelper(connection, "fence-peer");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700461
462 switch ((r>>8) & 0xff) {
463 case 3: /* peer is inconsistent */
464 ex_to_string = "peer is inconsistent or worse";
Philipp Reisnercb703452011-03-24 11:03:07 +0100465 mask.pdsk = D_MASK;
466 val.pdsk = D_INCONSISTENT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700467 break;
468 case 4: /* peer got outdated, or was already outdated */
469 ex_to_string = "peer was fenced";
Philipp Reisnercb703452011-03-24 11:03:07 +0100470 mask.pdsk = D_MASK;
471 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700472 break;
473 case 5: /* peer was down */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200474 if (conn_highest_disk(connection) == D_UP_TO_DATE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700475 /* we will(have) create(d) a new UUID anyways... */
476 ex_to_string = "peer is unreachable, assumed to be dead";
Philipp Reisnercb703452011-03-24 11:03:07 +0100477 mask.pdsk = D_MASK;
478 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700479 } else {
480 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
Philipp Reisnerb411b362009-09-25 16:07:19 -0700481 }
482 break;
483 case 6: /* Peer is primary, voluntarily outdate myself.
484 * This is useful when an unconnected R_SECONDARY is asked to
485 * become R_PRIMARY, but finds the other peer being active. */
486 ex_to_string = "peer is active";
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200487 drbd_warn(connection, "Peer is primary, outdating myself.\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100488 mask.disk = D_MASK;
489 val.disk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700490 break;
491 case 7:
492 if (fp != FP_STONITH)
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200493 drbd_err(connection, "fence-peer() = 7 && fencing != Stonith !!!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700494 ex_to_string = "peer was stonithed";
Philipp Reisnercb703452011-03-24 11:03:07 +0100495 mask.pdsk = D_MASK;
496 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700497 break;
498 default:
499 /* The script is broken ... */
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200500 drbd_err(connection, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
Philipp Reisnercb703452011-03-24 11:03:07 +0100501 return false; /* Eventually leave IO frozen */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700502 }
503
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200504 drbd_info(connection, "fence-peer helper returned %d (%s)\n",
Philipp Reisnercb703452011-03-24 11:03:07 +0100505 (r>>8) & 0xff, ex_to_string);
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200506
Philipp Reisnercb703452011-03-24 11:03:07 +0100507 out:
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200508
Philipp Reisnercb703452011-03-24 11:03:07 +0100509 /* Not using
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200510 conn_request_state(connection, mask, val, CS_VERBOSE);
Philipp Reisnercb703452011-03-24 11:03:07 +0100511 here, because we might were able to re-establish the connection in the
512 meantime. */
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200513 spin_lock_irq(&connection->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200514 if (connection->cstate < C_WF_REPORT_PARAMS && !test_bit(STATE_SENT, &connection->flags)) {
515 if (connection->connect_cnt != connect_cnt)
Philipp Reisner28e448b2013-06-25 16:50:06 +0200516 /* In case the connection was established and droped
517 while the fence-peer handler was running, ignore it */
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200518 drbd_info(connection, "Ignoring fence-peer exit code\n");
Philipp Reisner28e448b2013-06-25 16:50:06 +0200519 else
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200520 _conn_request_state(connection, mask, val, CS_VERBOSE);
Philipp Reisner28e448b2013-06-25 16:50:06 +0200521 }
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200522 spin_unlock_irq(&connection->resource->req_lock);
Philipp Reisnercb703452011-03-24 11:03:07 +0100523
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200524 return conn_highest_pdsk(connection) <= D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700525}
526
Philipp Reisner87f7be42010-06-11 13:56:33 +0200527static int _try_outdate_peer_async(void *data)
528{
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200529 struct drbd_connection *connection = (struct drbd_connection *)data;
Philipp Reisner87f7be42010-06-11 13:56:33 +0200530
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200531 conn_try_outdate_peer(connection);
Philipp Reisner21423fa2011-05-17 14:19:41 +0200532
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +0200533 kref_put(&connection->kref, drbd_destroy_connection);
Philipp Reisner87f7be42010-06-11 13:56:33 +0200534 return 0;
535}
536
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200537void conn_try_outdate_peer_async(struct drbd_connection *connection)
Philipp Reisner87f7be42010-06-11 13:56:33 +0200538{
539 struct task_struct *opa;
540
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200541 kref_get(&connection->kref);
542 opa = kthread_run(_try_outdate_peer_async, connection, "drbd_async_h");
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200543 if (IS_ERR(opa)) {
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200544 drbd_err(connection, "out of mem, failed to invoke fence-peer helper\n");
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +0200545 kref_put(&connection->kref, drbd_destroy_connection);
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200546 }
Philipp Reisner87f7be42010-06-11 13:56:33 +0200547}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700548
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100549enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200550drbd_set_role(struct drbd_device *device, enum drbd_role new_role, int force)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700551{
552 const int max_tries = 4;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100553 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200554 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700555 int try = 0;
556 int forced = 0;
557 union drbd_state mask, val;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700558
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +0200559 if (new_role == R_PRIMARY) {
560 struct drbd_connection *connection;
561
562 /* Detect dead peers as soon as possible. */
563
564 rcu_read_lock();
565 for_each_connection(connection, device->resource)
566 request_ping(connection);
567 rcu_read_unlock();
568 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700569
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200570 mutex_lock(device->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700571
572 mask.i = 0; mask.role = R_MASK;
573 val.i = 0; val.role = new_role;
574
575 while (try++ < max_tries) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200576 rv = _drbd_request_state(device, mask, val, CS_WAIT_COMPLETE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700577
578 /* in case we first succeeded to outdate,
579 * but now suddenly could establish a connection */
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100580 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700581 val.pdsk = 0;
582 mask.pdsk = 0;
583 continue;
584 }
585
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100586 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200587 (device->state.disk < D_UP_TO_DATE &&
588 device->state.disk >= D_INCONSISTENT)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700589 mask.disk = D_MASK;
590 val.disk = D_UP_TO_DATE;
591 forced = 1;
592 continue;
593 }
594
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100595 if (rv == SS_NO_UP_TO_DATE_DISK &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200596 device->state.disk == D_CONSISTENT && mask.pdsk == 0) {
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +0200597 D_ASSERT(device, device->state.pdsk == D_UNKNOWN);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700598
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200599 if (conn_try_outdate_peer(first_peer_device(device)->connection)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700600 val.disk = D_UP_TO_DATE;
601 mask.disk = D_MASK;
602 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700603 continue;
604 }
605
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100606 if (rv == SS_NOTHING_TO_DO)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100607 goto out;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100608 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200609 if (!conn_try_outdate_peer(first_peer_device(device)->connection) && force) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200610 drbd_warn(device, "Forced into split brain situation!\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100611 mask.pdsk = D_MASK;
612 val.pdsk = D_OUTDATED;
613
Philipp Reisnerb411b362009-09-25 16:07:19 -0700614 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700615 continue;
616 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100617 if (rv == SS_TWO_PRIMARIES) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700618 /* Maybe the peer is detected as dead very soon...
619 retry at most once more in this case. */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200620 int timeo;
621 rcu_read_lock();
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200622 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +0200623 timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
624 rcu_read_unlock();
625 schedule_timeout_interruptible(timeo);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700626 if (try < max_tries)
627 try = max_tries - 1;
628 continue;
629 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100630 if (rv < SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200631 rv = _drbd_request_state(device, mask, val,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700632 CS_VERBOSE + CS_WAIT_COMPLETE);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100633 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100634 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700635 }
636 break;
637 }
638
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100639 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100640 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700641
642 if (forced)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200643 drbd_warn(device, "Forced to consider local data as UpToDate!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700644
645 /* Wait until nothing is on the fly :) */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200646 wait_event(device->misc_wait, atomic_read(&device->ap_pending_cnt) == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700647
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100648 /* FIXME also wait for all pending P_BARRIER_ACK? */
649
Philipp Reisnerb411b362009-09-25 16:07:19 -0700650 if (new_role == R_SECONDARY) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200651 set_disk_ro(device->vdisk, true);
652 if (get_ldev(device)) {
653 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
654 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700655 }
656 } else {
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200657 mutex_lock(&device->resource->conf_update);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200658 nc = first_peer_device(device)->connection->net_conf;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200659 if (nc)
Andreas Gruenbacher6139f602011-05-06 20:00:02 +0200660 nc->discard_my_data = 0; /* without copy; single bit op is atomic */
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200661 mutex_unlock(&device->resource->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +0200662
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200663 set_disk_ro(device->vdisk, false);
664 if (get_ldev(device)) {
665 if (((device->state.conn < C_CONNECTED ||
666 device->state.pdsk <= D_FAILED)
667 && device->ldev->md.uuid[UI_BITMAP] == 0) || forced)
668 drbd_uuid_new_current(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700669
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200670 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
671 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700672 }
673 }
674
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100675 /* writeout of activity log covered areas of the bitmap
676 * to stable storage done in after state change already */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700677
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200678 if (device->state.conn >= C_WF_REPORT_PARAMS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700679 /* if this was forced, we should consider sync */
680 if (forced)
Andreas Gruenbacher69a22772011-08-09 00:47:13 +0200681 drbd_send_uuids(first_peer_device(device));
682 drbd_send_current_state(first_peer_device(device));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700683 }
684
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200685 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700686
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200687 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100688out:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200689 mutex_unlock(device->state_mutex);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100690 return rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700691}
692
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100693static const char *from_attrs_err_to_txt(int err)
Lars Ellenbergef50a3e2010-09-01 14:39:30 +0200694{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100695 return err == -ENOMSG ? "required attribute missing" :
696 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
Lars Ellenbergf3990022011-03-23 14:31:09 +0100697 err == -EEXIST ? "can not change invariant setting" :
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100698 "invalid attribute value";
Lars Ellenbergef50a3e2010-09-01 14:39:30 +0200699}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700700
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100701int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700702{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100703 struct set_role_parms parms;
704 int err;
705 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700706
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100707 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
708 if (!adm_ctx.reply_skb)
709 return retcode;
710 if (retcode != NO_ERROR)
711 goto out;
712
713 memset(&parms, 0, sizeof(parms));
714 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +0100715 err = set_role_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100716 if (err) {
717 retcode = ERR_MANDATORY_TAG;
718 drbd_msg_put_info(from_attrs_err_to_txt(err));
719 goto out;
720 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700721 }
722
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100723 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200724 retcode = drbd_set_role(adm_ctx.device, R_PRIMARY, parms.assume_uptodate);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100725 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200726 retcode = drbd_set_role(adm_ctx.device, R_SECONDARY, 0);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100727out:
728 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700729 return 0;
730}
731
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100732/* Initializes the md.*_offset members, so we are able to find
733 * the on disk meta data.
734 *
735 * We currently have two possible layouts:
736 * external:
737 * |----------- md_size_sect ------------------|
738 * [ 4k superblock ][ activity log ][ Bitmap ]
739 * | al_offset == 8 |
740 * | bm_offset = al_offset + X |
741 * ==> bitmap sectors = md_size_sect - bm_offset
742 *
743 * internal:
744 * |----------- md_size_sect ------------------|
745 * [data.....][ Bitmap ][ activity log ][ 4k superblock ]
746 * | al_offset < 0 |
747 * | bm_offset = al_offset - Y |
748 * ==> bitmap sectors = Y = al_offset - bm_offset
749 *
750 * Activity log size used to be fixed 32kB,
751 * but is about to become configurable.
752 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200753static void drbd_md_set_sector_offsets(struct drbd_device *device,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700754 struct drbd_backing_dev *bdev)
755{
756 sector_t md_size_sect = 0;
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +0100757 unsigned int al_size_sect = bdev->md.al_size_4k * 8;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200758
Lars Ellenberg3a4d4eb2013-03-19 18:16:44 +0100759 bdev->md.md_offset = drbd_md_ss(bdev);
760
Lars Ellenberg68e41a42013-03-19 18:16:45 +0100761 switch (bdev->md.meta_dev_idx) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700762 default:
763 /* v07 style fixed size indexed meta data */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100764 bdev->md.md_size_sect = MD_128MB_SECT;
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100765 bdev->md.al_offset = MD_4kB_SECT;
766 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700767 break;
768 case DRBD_MD_INDEX_FLEX_EXT:
769 /* just occupy the full device; unit: sectors */
770 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100771 bdev->md.al_offset = MD_4kB_SECT;
772 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700773 break;
774 case DRBD_MD_INDEX_INTERNAL:
775 case DRBD_MD_INDEX_FLEX_INT:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700776 /* al size is still fixed */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100777 bdev->md.al_offset = -al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700778 /* we need (slightly less than) ~ this much bitmap sectors: */
779 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
780 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
781 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
782 md_size_sect = ALIGN(md_size_sect, 8);
783
784 /* plus the "drbd meta data super block",
785 * and the activity log; */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100786 md_size_sect += MD_4kB_SECT + al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700787
788 bdev->md.md_size_sect = md_size_sect;
789 /* bitmap offset is adjusted by 'super' block size */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100790 bdev->md.bm_offset = -md_size_sect + MD_4kB_SECT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700791 break;
792 }
793}
794
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100795/* input size is expected to be in KB */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700796char *ppsize(char *buf, unsigned long long size)
797{
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100798 /* Needs 9 bytes at max including trailing NUL:
799 * -1ULL ==> "16384 EB" */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700800 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
801 int base = 0;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100802 while (size >= 10000 && base < sizeof(units)-1) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700803 /* shift + round */
804 size = (size >> 10) + !!(size & (1<<9));
805 base++;
806 }
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100807 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700808
809 return buf;
810}
811
812/* there is still a theoretical deadlock when called from receiver
813 * on an D_INCONSISTENT R_PRIMARY:
814 * remote READ does inc_ap_bio, receiver would need to receive answer
815 * packet from remote to dec_ap_bio again.
816 * receiver receive_sizes(), comes here,
817 * waits for ap_bio_cnt == 0. -> deadlock.
818 * but this cannot happen, actually, because:
819 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
820 * (not connected, or bad/no disk on peer):
821 * see drbd_fail_request_early, ap_bio_cnt is zero.
822 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
823 * peer may not initiate a resize.
824 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100825/* Note these are not to be confused with
826 * drbd_adm_suspend_io/drbd_adm_resume_io,
827 * which are (sub) state changes triggered by admin (drbdsetup),
828 * and can be long lived.
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200829 * This changes an device->flag, is triggered by drbd internals,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100830 * and should be short-lived. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200831void drbd_suspend_io(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700832{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200833 set_bit(SUSPEND_IO, &device->flags);
834 if (drbd_suspended(device))
Philipp Reisner265be2d2010-05-31 10:14:17 +0200835 return;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200836 wait_event(device->misc_wait, !atomic_read(&device->ap_bio_cnt));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700837}
838
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200839void drbd_resume_io(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700840{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200841 clear_bit(SUSPEND_IO, &device->flags);
842 wake_up(&device->misc_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700843}
844
845/**
846 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200847 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700848 *
849 * Returns 0 on success, negative return values indicate errors.
850 * You should call drbd_md_sync() after calling this function.
851 */
Philipp Reisnerd752b262013-06-25 16:50:08 +0200852enum determine_dev_size
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200853drbd_determine_dev_size(struct drbd_device *device, enum dds_flags flags, struct resize_parms *rs) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700854{
855 sector_t prev_first_sect, prev_size; /* previous meta location */
Lars Ellenbergcccac982013-03-19 18:16:46 +0100856 sector_t la_size_sect, u_size;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200857 struct drbd_md *md = &device->ldev->md;
Philipp Reisnerd752b262013-06-25 16:50:08 +0200858 u32 prev_al_stripe_size_4k;
859 u32 prev_al_stripes;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700860 sector_t size;
861 char ppb[10];
Philipp Reisnerd752b262013-06-25 16:50:08 +0200862 void *buffer;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700863
864 int md_moved, la_size_changed;
Philipp Reisnere96c9632013-06-25 16:50:07 +0200865 enum determine_dev_size rv = DS_UNCHANGED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700866
867 /* race:
868 * application request passes inc_ap_bio,
869 * but then cannot get an AL-reference.
870 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
871 *
872 * to avoid that:
873 * Suspend IO right here.
874 * still lock the act_log to not trigger ASSERTs there.
875 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200876 drbd_suspend_io(device);
877 buffer = drbd_md_get_buffer(device); /* Lock meta-data IO */
Philipp Reisnerd752b262013-06-25 16:50:08 +0200878 if (!buffer) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200879 drbd_resume_io(device);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200880 return DS_ERROR;
881 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700882
883 /* no wait necessary anymore, actually we could assert that */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200884 wait_event(device->al_wait, lc_try_lock(device->act_log));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700885
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200886 prev_first_sect = drbd_md_first_sector(device->ldev);
887 prev_size = device->ldev->md.md_size_sect;
888 la_size_sect = device->ldev->md.la_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700889
Philipp Reisnerd752b262013-06-25 16:50:08 +0200890 if (rs) {
891 /* rs is non NULL if we should change the AL layout only */
892
893 prev_al_stripes = md->al_stripes;
894 prev_al_stripe_size_4k = md->al_stripe_size_4k;
895
896 md->al_stripes = rs->al_stripes;
897 md->al_stripe_size_4k = rs->al_stripe_size / 4;
898 md->al_size_4k = (u64)rs->al_stripes * rs->al_stripe_size / 4;
899 }
900
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200901 drbd_md_set_sector_offsets(device, device->ldev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700902
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200903 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200904 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200905 rcu_read_unlock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200906 size = drbd_new_dev_size(device, device->ldev, u_size, flags & DDSF_FORCED);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700907
Philipp Reisnerd752b262013-06-25 16:50:08 +0200908 if (size < la_size_sect) {
909 if (rs && u_size == 0) {
910 /* Remove "rs &&" later. This check should always be active, but
911 right now the receiver expects the permissive behavior */
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200912 drbd_warn(device, "Implicit shrink not allowed. "
Philipp Reisnerd752b262013-06-25 16:50:08 +0200913 "Use --size=%llus for explicit shrink.\n",
914 (unsigned long long)size);
915 rv = DS_ERROR_SHRINK;
916 }
917 if (u_size > size)
918 rv = DS_ERROR_SPACE_MD;
919 if (rv != DS_UNCHANGED)
920 goto err_out;
921 }
922
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200923 if (drbd_get_capacity(device->this_bdev) != size ||
924 drbd_bm_capacity(device) != size) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700925 int err;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200926 err = drbd_bm_resize(device, size, !(flags & DDSF_NO_RESYNC));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700927 if (unlikely(err)) {
928 /* currently there is only one error: ENOMEM! */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200929 size = drbd_bm_capacity(device)>>1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700930 if (size == 0) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200931 drbd_err(device, "OUT OF MEMORY! "
Philipp Reisnerb411b362009-09-25 16:07:19 -0700932 "Could not allocate bitmap!\n");
933 } else {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200934 drbd_err(device, "BM resizing failed. "
Philipp Reisnerb411b362009-09-25 16:07:19 -0700935 "Leaving size unchanged at size = %lu KB\n",
936 (unsigned long)size);
937 }
Philipp Reisnere96c9632013-06-25 16:50:07 +0200938 rv = DS_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700939 }
940 /* racy, see comments above. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200941 drbd_set_my_capacity(device, size);
942 device->ldev->md.la_size_sect = size;
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200943 drbd_info(device, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
Philipp Reisnerb411b362009-09-25 16:07:19 -0700944 (unsigned long long)size>>1);
945 }
Philipp Reisnerd752b262013-06-25 16:50:08 +0200946 if (rv <= DS_ERROR)
947 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700948
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200949 la_size_changed = (la_size_sect != device->ldev->md.la_size_sect);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700950
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200951 md_moved = prev_first_sect != drbd_md_first_sector(device->ldev)
952 || prev_size != device->ldev->md.md_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700953
Philipp Reisnerd752b262013-06-25 16:50:08 +0200954 if (la_size_changed || md_moved || rs) {
955 u32 prev_flags;
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100956
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200957 drbd_al_shrink(device); /* All extents inactive. */
Philipp Reisnerd752b262013-06-25 16:50:08 +0200958
959 prev_flags = md->flags;
960 md->flags &= ~MDF_PRIMARY_IND;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200961 drbd_md_write(device, buffer);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200962
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200963 drbd_info(device, "Writing the whole bitmap, %s\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -0700964 la_size_changed && md_moved ? "size changed and md moved" :
965 la_size_changed ? "size changed" : "md moved");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100966 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200967 drbd_bitmap_io(device, md_moved ? &drbd_bm_write_all : &drbd_bm_write,
Philipp Reisnerd752b262013-06-25 16:50:08 +0200968 "size changed", BM_LOCKED_MASK);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200969 drbd_initialize_al(device, buffer);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200970
971 md->flags = prev_flags;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200972 drbd_md_write(device, buffer);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200973
974 if (rs)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200975 drbd_info(device, "Changed AL layout to al-stripes = %d, al-stripe-size-kB = %d\n",
976 md->al_stripes, md->al_stripe_size_4k * 4);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700977 }
978
Lars Ellenbergcccac982013-03-19 18:16:46 +0100979 if (size > la_size_sect)
Philipp Reisner57737ad2013-10-23 10:59:17 +0200980 rv = la_size_sect ? DS_GREW : DS_GREW_FROM_ZERO;
Lars Ellenbergcccac982013-03-19 18:16:46 +0100981 if (size < la_size_sect)
Philipp Reisnere96c9632013-06-25 16:50:07 +0200982 rv = DS_SHRUNK;
Philipp Reisnerd752b262013-06-25 16:50:08 +0200983
984 if (0) {
985 err_out:
986 if (rs) {
987 md->al_stripes = prev_al_stripes;
988 md->al_stripe_size_4k = prev_al_stripe_size_4k;
989 md->al_size_4k = (u64)prev_al_stripes * prev_al_stripe_size_4k;
990
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200991 drbd_md_set_sector_offsets(device, device->ldev);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200992 }
993 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200994 lc_unlock(device->act_log);
995 wake_up(&device->al_wait);
996 drbd_md_put_buffer(device);
997 drbd_resume_io(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700998
999 return rv;
1000}
1001
1002sector_t
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001003drbd_new_dev_size(struct drbd_device *device, struct drbd_backing_dev *bdev,
Philipp Reisneref5e44a2011-05-03 13:27:43 +02001004 sector_t u_size, int assume_peer_has_space)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001005{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001006 sector_t p_size = device->p_size; /* partner's disk size. */
Lars Ellenbergcccac982013-03-19 18:16:46 +01001007 sector_t la_size_sect = bdev->md.la_size_sect; /* last agreed size. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001008 sector_t m_size; /* my size */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001009 sector_t size = 0;
1010
1011 m_size = drbd_get_max_capacity(bdev);
1012
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001013 if (device->state.conn < C_CONNECTED && assume_peer_has_space) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001014 drbd_warn(device, "Resize while not connected was forced by the user!\n");
Philipp Reisnera393db62009-12-22 13:35:52 +01001015 p_size = m_size;
1016 }
1017
Philipp Reisnerb411b362009-09-25 16:07:19 -07001018 if (p_size && m_size) {
1019 size = min_t(sector_t, p_size, m_size);
1020 } else {
Lars Ellenbergcccac982013-03-19 18:16:46 +01001021 if (la_size_sect) {
1022 size = la_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001023 if (m_size && m_size < size)
1024 size = m_size;
1025 if (p_size && p_size < size)
1026 size = p_size;
1027 } else {
1028 if (m_size)
1029 size = m_size;
1030 if (p_size)
1031 size = p_size;
1032 }
1033 }
1034
1035 if (size == 0)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001036 drbd_err(device, "Both nodes diskless!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001037
1038 if (u_size) {
1039 if (u_size > size)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001040 drbd_err(device, "Requested disk size is too big (%lu > %lu)\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001041 (unsigned long)u_size>>1, (unsigned long)size>>1);
1042 else
1043 size = u_size;
1044 }
1045
1046 return size;
1047}
1048
1049/**
1050 * drbd_check_al_size() - Ensures that the AL is of the right size
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001051 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001052 *
1053 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
1054 * failed, and 0 on success. You should call drbd_md_sync() after you called
1055 * this function.
1056 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001057static int drbd_check_al_size(struct drbd_device *device, struct disk_conf *dc)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001058{
1059 struct lru_cache *n, *t;
1060 struct lc_element *e;
1061 unsigned int in_use;
1062 int i;
1063
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001064 if (device->act_log &&
1065 device->act_log->nr_elements == dc->al_extents)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001066 return 0;
1067
1068 in_use = 0;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001069 t = device->act_log;
Lars Ellenberg7ad651b2011-02-21 13:21:03 +01001070 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
Lars Ellenbergf3990022011-03-23 14:31:09 +01001071 dc->al_extents, sizeof(struct lc_element), 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001072
1073 if (n == NULL) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001074 drbd_err(device, "Cannot allocate act_log lru!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001075 return -ENOMEM;
1076 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001077 spin_lock_irq(&device->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001078 if (t) {
1079 for (i = 0; i < t->nr_elements; i++) {
1080 e = lc_element_by_index(t, i);
1081 if (e->refcnt)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001082 drbd_err(device, "refcnt(%d)==%d\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001083 e->lc_number, e->refcnt);
1084 in_use += e->refcnt;
1085 }
1086 }
1087 if (!in_use)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001088 device->act_log = n;
1089 spin_unlock_irq(&device->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001090 if (in_use) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001091 drbd_err(device, "Activity log still in use!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001092 lc_destroy(n);
1093 return -EBUSY;
1094 } else {
1095 if (t)
1096 lc_destroy(t);
1097 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001098 drbd_md_mark_dirty(device); /* we changed device->act_log->nr_elemens */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001099 return 0;
1100}
1101
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001102static void drbd_setup_queue_param(struct drbd_device *device, unsigned int max_bio_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001103{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001104 struct request_queue * const q = device->rq_queue;
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001105 unsigned int max_hw_sectors = max_bio_size >> 9;
1106 unsigned int max_segments = 0;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001107
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001108 if (get_ldev_if_state(device, D_ATTACHING)) {
1109 struct request_queue * const b = device->ldev->backing_bdev->bd_disk->queue;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001110
1111 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001112 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001113 max_segments = rcu_dereference(device->ldev->disk_conf)->max_bio_bvecs;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001114 rcu_read_unlock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001115 put_ldev(device);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001116 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001117
Philipp Reisnerb411b362009-09-25 16:07:19 -07001118 blk_queue_logical_block_size(q, 512);
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001119 blk_queue_max_hw_sectors(q, max_hw_sectors);
1120 /* This is the workaround for "bio would need to, but cannot, be split" */
1121 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
1122 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001123
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001124 if (get_ldev_if_state(device, D_ATTACHING)) {
1125 struct request_queue * const b = device->ldev->backing_bdev->bd_disk->queue;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001126
Philipp Reisner99432fc2011-05-20 16:39:13 +02001127 blk_queue_stack_limits(q, b);
1128
1129 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001130 drbd_info(device, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
Philipp Reisner99432fc2011-05-20 16:39:13 +02001131 q->backing_dev_info.ra_pages,
1132 b->backing_dev_info.ra_pages);
1133 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1134 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001135 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001136 }
1137}
1138
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001139void drbd_reconsider_max_bio_size(struct drbd_device *device)
Philipp Reisner99432fc2011-05-20 16:39:13 +02001140{
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001141 unsigned int now, new, local, peer;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001142
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001143 now = queue_max_hw_sectors(device->rq_queue) << 9;
1144 local = device->local_max_bio_size; /* Eventually last known value, from volatile memory */
1145 peer = device->peer_max_bio_size; /* Eventually last known value, from meta data */
Philipp Reisner99432fc2011-05-20 16:39:13 +02001146
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001147 if (get_ldev_if_state(device, D_ATTACHING)) {
1148 local = queue_max_hw_sectors(device->ldev->backing_bdev->bd_disk->queue) << 9;
1149 device->local_max_bio_size = local;
1150 put_ldev(device);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001151 }
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001152 local = min(local, DRBD_MAX_BIO_SIZE);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001153
1154 /* We may ignore peer limits if the peer is modern enough.
1155 Because new from 8.3.8 onwards the peer can use multiple
1156 BIOs for a single peer_request */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001157 if (device->state.conn >= C_WF_REPORT_PARAMS) {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001158 if (first_peer_device(device)->connection->agreed_pro_version < 94)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001159 peer = min(device->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
Philipp Reisner68093842011-06-30 15:43:06 +02001160 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001161 else if (first_peer_device(device)->connection->agreed_pro_version == 94)
Philipp Reisner99432fc2011-05-20 16:39:13 +02001162 peer = DRBD_MAX_SIZE_H80_PACKET;
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001163 else if (first_peer_device(device)->connection->agreed_pro_version < 100)
Philipp Reisner2ffca4f2011-06-30 15:43:06 +02001164 peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
1165 else
Philipp Reisner99432fc2011-05-20 16:39:13 +02001166 peer = DRBD_MAX_BIO_SIZE;
1167 }
1168
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001169 new = min(local, peer);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001170
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001171 if (device->state.role == R_PRIMARY && new < now)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001172 drbd_err(device, "ASSERT FAILED new < now; (%u < %u)\n", new, now);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001173
1174 if (new != now)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001175 drbd_info(device, "max BIO size = %u\n", new);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001176
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001177 drbd_setup_queue_param(device, new);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001178}
1179
Philipp Reisnera18e9d12011-04-24 11:09:55 +02001180/* Starts the worker thread */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001181static void conn_reconfig_start(struct drbd_connection *connection)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001182{
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001183 drbd_thread_start(&connection->worker);
Andreas Gruenbacherb5043c52011-07-28 15:56:02 +02001184 drbd_flush_workqueue(&connection->sender_work);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001185}
1186
Philipp Reisnera18e9d12011-04-24 11:09:55 +02001187/* if still unconfigured, stops worker again. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001188static void conn_reconfig_done(struct drbd_connection *connection)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001189{
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001190 bool stop_threads;
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001191 spin_lock_irq(&connection->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001192 stop_threads = conn_all_vols_unconf(connection) &&
1193 connection->cstate == C_STANDALONE;
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001194 spin_unlock_irq(&connection->resource->req_lock);
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001195 if (stop_threads) {
1196 /* asender is implicitly stopped by receiver
Philipp Reisner81fa2e62011-05-04 15:10:30 +02001197 * in conn_disconnect() */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001198 drbd_thread_stop(&connection->receiver);
1199 drbd_thread_stop(&connection->worker);
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001200 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001201}
1202
Philipp Reisner07782862010-08-31 12:00:50 +02001203/* Make sure IO is suspended before calling this function(). */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001204static void drbd_suspend_al(struct drbd_device *device)
Philipp Reisner07782862010-08-31 12:00:50 +02001205{
1206 int s = 0;
1207
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001208 if (!lc_try_lock(device->act_log)) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001209 drbd_warn(device, "Failed to lock al in drbd_suspend_al()\n");
Philipp Reisner07782862010-08-31 12:00:50 +02001210 return;
1211 }
1212
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001213 drbd_al_shrink(device);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001214 spin_lock_irq(&device->resource->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001215 if (device->state.conn < C_CONNECTED)
1216 s = !test_and_set_bit(AL_SUSPENDED, &device->flags);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001217 spin_unlock_irq(&device->resource->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001218 lc_unlock(device->act_log);
Philipp Reisner07782862010-08-31 12:00:50 +02001219
1220 if (s)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001221 drbd_info(device, "Suspended AL updates\n");
Philipp Reisner07782862010-08-31 12:00:50 +02001222}
1223
Lars Ellenberg5979e362011-04-27 21:09:55 +02001224
1225static bool should_set_defaults(struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001226{
Lars Ellenberg5979e362011-04-27 21:09:55 +02001227 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1228 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1229}
1230
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001231static unsigned int drbd_al_extents_max(struct drbd_backing_dev *bdev)
Philipp Reisnerd589a212011-05-04 10:06:52 +02001232{
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001233 /* This is limited by 16 bit "slot" numbers,
1234 * and by available on-disk context storage.
1235 *
1236 * Also (u16)~0 is special (denotes a "free" extent).
1237 *
1238 * One transaction occupies one 4kB on-disk block,
1239 * we have n such blocks in the on disk ring buffer,
1240 * the "current" transaction may fail (n-1),
1241 * and there is 919 slot numbers context information per transaction.
1242 *
1243 * 72 transaction blocks amounts to more than 2**16 context slots,
1244 * so cap there first.
1245 */
1246 const unsigned int max_al_nr = DRBD_AL_EXTENTS_MAX;
1247 const unsigned int sufficient_on_disk =
1248 (max_al_nr + AL_CONTEXT_PER_TRANSACTION -1)
1249 /AL_CONTEXT_PER_TRANSACTION;
Philipp Reisnerd589a212011-05-04 10:06:52 +02001250
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001251 unsigned int al_size_4k = bdev->md.al_size_4k;
1252
1253 if (al_size_4k > sufficient_on_disk)
1254 return max_al_nr;
1255
1256 return (al_size_4k - 1) * AL_CONTEXT_PER_TRANSACTION;
Philipp Reisnerd589a212011-05-04 10:06:52 +02001257}
1258
Lars Ellenbergf3990022011-03-23 14:31:09 +01001259int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1260{
1261 enum drbd_ret_code retcode;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001262 struct drbd_device *device;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001263 struct disk_conf *new_disk_conf, *old_disk_conf;
Philipp Reisner813472c2011-05-03 16:47:02 +02001264 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001265 int err, fifo_size;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001266
1267 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1268 if (!adm_ctx.reply_skb)
1269 return retcode;
1270 if (retcode != NO_ERROR)
1271 goto out;
1272
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001273 device = adm_ctx.device;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001274
1275 /* we also need a disk
1276 * to change the options on */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001277 if (!get_ldev(device)) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001278 retcode = ERR_NO_DISK;
1279 goto out;
1280 }
1281
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001282 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001283 if (!new_disk_conf) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001284 retcode = ERR_NOMEM;
1285 goto fail;
1286 }
1287
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001288 mutex_lock(&device->resource->conf_update);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001289 old_disk_conf = device->ldev->disk_conf;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001290 *new_disk_conf = *old_disk_conf;
Lars Ellenberg5979e362011-04-27 21:09:55 +02001291 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02001292 set_disk_conf_defaults(new_disk_conf);
Lars Ellenberg5979e362011-04-27 21:09:55 +02001293
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001294 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02001295 if (err && err != -ENOMSG) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001296 retcode = ERR_MANDATORY_TAG;
1297 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisner8e229432013-08-01 10:21:47 +02001298 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001299 }
1300
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001301 if (!expect(new_disk_conf->resync_rate >= 1))
1302 new_disk_conf->resync_rate = 1;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001303
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001304 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1305 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001306 if (new_disk_conf->al_extents > drbd_al_extents_max(device->ldev))
1307 new_disk_conf->al_extents = drbd_al_extents_max(device->ldev);
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001308
1309 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1310 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001311
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001312 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001313 if (fifo_size != device->rs_plan_s->size) {
Philipp Reisner813472c2011-05-03 16:47:02 +02001314 new_plan = fifo_alloc(fifo_size);
1315 if (!new_plan) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001316 drbd_err(device, "kmalloc of fifo_buffer failed");
Lars Ellenbergf3990022011-03-23 14:31:09 +01001317 retcode = ERR_NOMEM;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001318 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001319 }
1320 }
1321
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001322 drbd_suspend_io(device);
1323 wait_event(device->al_wait, lc_try_lock(device->act_log));
1324 drbd_al_shrink(device);
1325 err = drbd_check_al_size(device, new_disk_conf);
1326 lc_unlock(device->act_log);
1327 wake_up(&device->al_wait);
1328 drbd_resume_io(device);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001329
1330 if (err) {
1331 retcode = ERR_NOMEM;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001332 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001333 }
1334
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001335 write_lock_irq(&global_state_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001336 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001337 if (retcode == NO_ERROR) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001338 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
1339 drbd_resync_after_changed(device);
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001340 }
1341 write_unlock_irq(&global_state_lock);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001342
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001343 if (retcode != NO_ERROR)
1344 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001345
Philipp Reisner813472c2011-05-03 16:47:02 +02001346 if (new_plan) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001347 old_plan = device->rs_plan_s;
1348 rcu_assign_pointer(device->rs_plan_s, new_plan);
Philipp Reisner9958c852011-05-03 16:19:31 +02001349 }
Philipp Reisner9958c852011-05-03 16:19:31 +02001350
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001351 mutex_unlock(&device->resource->conf_update);
Philipp Reisner27eb13e2012-03-30 14:12:15 +02001352
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001353 if (new_disk_conf->al_updates)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001354 device->ldev->md.flags &= ~MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001355 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001356 device->ldev->md.flags |= MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001357
Lars Ellenberg691631c2012-10-26 00:41:50 +02001358 if (new_disk_conf->md_flushes)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001359 clear_bit(MD_NO_FUA, &device->flags);
Lars Ellenberg691631c2012-10-26 00:41:50 +02001360 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001361 set_bit(MD_NO_FUA, &device->flags);
Lars Ellenberg691631c2012-10-26 00:41:50 +02001362
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001363 drbd_bump_write_ordering(first_peer_device(device)->connection, WO_bdev_flush);
Philipp Reisner27eb13e2012-03-30 14:12:15 +02001364
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001365 drbd_md_sync(device);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001366
Andreas Gruenbacher69a22772011-08-09 00:47:13 +02001367 if (device->state.conn >= C_CONNECTED) {
1368 struct drbd_peer_device *peer_device;
1369
1370 for_each_peer_device(peer_device, device)
1371 drbd_send_sync_param(peer_device);
1372 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001373
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001374 synchronize_rcu();
1375 kfree(old_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02001376 kfree(old_plan);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001377 mod_timer(&device->request_timer, jiffies + HZ);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001378 goto success;
1379
1380fail_unlock:
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001381 mutex_unlock(&device->resource->conf_update);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001382 fail:
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001383 kfree(new_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02001384 kfree(new_plan);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001385success:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001386 put_ldev(device);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001387 out:
1388 drbd_adm_finish(info, retcode);
1389 return 0;
1390}
1391
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001392int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001393{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001394 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001395 int err;
Andreas Gruenbacher116676c2010-12-08 13:33:11 +01001396 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001397 enum determine_dev_size dd;
1398 sector_t max_possible_sectors;
1399 sector_t min_md_device_sectors;
1400 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001401 struct disk_conf *new_disk_conf = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001402 struct block_device *bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001403 struct lru_cache *resync_lru = NULL;
Philipp Reisner9958c852011-05-03 16:19:31 +02001404 struct fifo_buffer *new_plan = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001405 union drbd_state ns, os;
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001406 enum drbd_state_rv rv;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001407 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001408
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001409 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1410 if (!adm_ctx.reply_skb)
1411 return retcode;
1412 if (retcode != NO_ERROR)
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001413 goto finish;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001414
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001415 device = adm_ctx.device;
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001416 conn_reconfig_start(first_peer_device(device)->connection);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001417
1418 /* if you want to reconfigure, please tear down first */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001419 if (device->state.disk > D_DISKLESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001420 retcode = ERR_DISK_CONFIGURED;
1421 goto fail;
1422 }
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001423 /* It may just now have detached because of IO error. Make sure
1424 * drbd_ldev_destroy is done already, we may end up here very fast,
1425 * e.g. if someone calls attach from the on-io-error handler,
1426 * to realize a "hot spare" feature (not that I'd recommend that) */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001427 wait_event(device->misc_wait, !atomic_read(&device->local_cnt));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001428
Lars Ellenberg383606e2012-06-14 14:21:32 +02001429 /* make sure there is no leftover from previous force-detach attempts */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001430 clear_bit(FORCE_DETACH, &device->flags);
1431 clear_bit(WAS_IO_ERROR, &device->flags);
1432 clear_bit(WAS_READ_ERROR, &device->flags);
Lars Ellenberg383606e2012-06-14 14:21:32 +02001433
Lars Ellenberg0029d622012-06-14 18:02:52 +02001434 /* and no leftover from previously aborted resync or verify, either */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001435 device->rs_total = 0;
1436 device->rs_failed = 0;
1437 atomic_set(&device->rs_pending_cnt, 0);
Lars Ellenberg0029d622012-06-14 18:02:52 +02001438
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001439 /* allocation not in the IO path, drbdsetup context */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001440 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1441 if (!nbc) {
1442 retcode = ERR_NOMEM;
1443 goto fail;
1444 }
Philipp Reisner9f2247b2012-08-16 14:25:58 +02001445 spin_lock_init(&nbc->md.uuid_lock);
1446
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001447 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
1448 if (!new_disk_conf) {
1449 retcode = ERR_NOMEM;
1450 goto fail;
1451 }
1452 nbc->disk_conf = new_disk_conf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001453
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001454 set_disk_conf_defaults(new_disk_conf);
1455 err = disk_conf_from_attrs(new_disk_conf, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001456 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001457 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001458 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001459 goto fail;
1460 }
1461
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001462 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1463 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
Philipp Reisnerd589a212011-05-04 10:06:52 +02001464
Philipp Reisner9958c852011-05-03 16:19:31 +02001465 new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
1466 if (!new_plan) {
1467 retcode = ERR_NOMEM;
1468 goto fail;
1469 }
1470
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001471 if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001472 retcode = ERR_MD_IDX_INVALID;
1473 goto fail;
1474 }
1475
Lars Ellenberga3f8f7d2013-03-27 14:08:43 +01001476 write_lock_irq(&global_state_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001477 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
Lars Ellenberga3f8f7d2013-03-27 14:08:43 +01001478 write_unlock_irq(&global_state_lock);
1479 if (retcode != NO_ERROR)
1480 goto fail;
1481
Philipp Reisner44ed1672011-04-19 17:10:19 +02001482 rcu_read_lock();
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001483 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001484 if (nc) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001485 if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02001486 rcu_read_unlock();
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001487 retcode = ERR_STONITH_AND_PROT_A;
1488 goto fail;
1489 }
1490 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02001491 rcu_read_unlock();
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001492
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001493 bdev = blkdev_get_by_path(new_disk_conf->backing_dev,
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001494 FMODE_READ | FMODE_WRITE | FMODE_EXCL, device);
Tejun Heoe525fd82010-11-13 11:55:17 +01001495 if (IS_ERR(bdev)) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001496 drbd_err(device, "open(\"%s\") failed with %ld\n", new_disk_conf->backing_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001497 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001498 retcode = ERR_OPEN_DISK;
1499 goto fail;
1500 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001501 nbc->backing_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001502
Tejun Heoe525fd82010-11-13 11:55:17 +01001503 /*
1504 * meta_dev_idx >= 0: external fixed size, possibly multiple
1505 * drbd sharing one meta device. TODO in that case, paranoia
1506 * check that [md_bdev, meta_dev_idx] is not yet used by some
1507 * other drbd minor! (if you use drbd.conf + drbdadm, that
1508 * should check it for you already; but if you don't, or
1509 * someone fooled it, we need to double check here)
1510 */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001511 bdev = blkdev_get_by_path(new_disk_conf->meta_dev,
Tejun Heod4d77622010-11-13 11:55:18 +01001512 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001513 (new_disk_conf->meta_dev_idx < 0) ?
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001514 (void *)device : (void *)drbd_m_holder);
Tejun Heoe525fd82010-11-13 11:55:17 +01001515 if (IS_ERR(bdev)) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001516 drbd_err(device, "open(\"%s\") failed with %ld\n", new_disk_conf->meta_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001517 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001518 retcode = ERR_OPEN_MD_DISK;
1519 goto fail;
1520 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001521 nbc->md_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001522
Tejun Heoe525fd82010-11-13 11:55:17 +01001523 if ((nbc->backing_bdev == nbc->md_bdev) !=
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001524 (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1525 new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001526 retcode = ERR_MD_IDX_INVALID;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001527 goto fail;
1528 }
1529
1530 resync_lru = lc_create("resync", drbd_bm_ext_cache,
Lars Ellenberg46a15bc2011-02-21 13:21:01 +01001531 1, 61, sizeof(struct bm_extent),
Philipp Reisnerb411b362009-09-25 16:07:19 -07001532 offsetof(struct bm_extent, lce));
1533 if (!resync_lru) {
1534 retcode = ERR_NOMEM;
Tejun Heoe525fd82010-11-13 11:55:17 +01001535 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001536 }
1537
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +01001538 /* Read our meta data super block early.
1539 * This also sets other on-disk offsets. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001540 retcode = drbd_md_read(device, nbc);
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +01001541 if (retcode != NO_ERROR)
1542 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001543
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001544 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1545 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
1546 if (new_disk_conf->al_extents > drbd_al_extents_max(nbc))
1547 new_disk_conf->al_extents = drbd_al_extents_max(nbc);
1548
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001549 if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001550 drbd_err(device, "max capacity %llu smaller than disk size %llu\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001551 (unsigned long long) drbd_get_max_capacity(nbc),
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001552 (unsigned long long) new_disk_conf->disk_size);
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001553 retcode = ERR_DISK_TOO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001554 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001555 }
1556
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001557 if (new_disk_conf->meta_dev_idx < 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001558 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1559 /* at least one MB, otherwise it does not make sense */
1560 min_md_device_sectors = (2<<10);
1561 } else {
1562 max_possible_sectors = DRBD_MAX_SECTORS;
Lars Ellenbergae8bf312013-03-19 18:16:43 +01001563 min_md_device_sectors = MD_128MB_SECT * (new_disk_conf->meta_dev_idx + 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001564 }
1565
Philipp Reisnerb411b362009-09-25 16:07:19 -07001566 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001567 retcode = ERR_MD_DISK_TOO_SMALL;
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001568 drbd_warn(device, "refusing attach: md-device too small, "
Philipp Reisnerb411b362009-09-25 16:07:19 -07001569 "at least %llu sectors needed for this meta-disk type\n",
1570 (unsigned long long) min_md_device_sectors);
Tejun Heoe525fd82010-11-13 11:55:17 +01001571 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001572 }
1573
1574 /* Make sure the new disk is big enough
1575 * (we may currently be R_PRIMARY with no local disk...) */
1576 if (drbd_get_max_capacity(nbc) <
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001577 drbd_get_capacity(device->this_bdev)) {
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001578 retcode = ERR_DISK_TOO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001579 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001580 }
1581
1582 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1583
Lars Ellenberg13529942009-10-12 19:07:49 +02001584 if (nbc->known_size > max_possible_sectors) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001585 drbd_warn(device, "==> truncating very big lower level device "
Lars Ellenberg13529942009-10-12 19:07:49 +02001586 "to currently maximum possible %llu sectors <==\n",
1587 (unsigned long long) max_possible_sectors);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001588 if (new_disk_conf->meta_dev_idx >= 0)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001589 drbd_warn(device, "==>> using internal or flexible "
Lars Ellenberg13529942009-10-12 19:07:49 +02001590 "meta data may help <<==\n");
1591 }
1592
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001593 drbd_suspend_io(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001594 /* also wait for the last barrier ack. */
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001595 /* FIXME see also https://daiquiri.linbit/cgi-bin/bugzilla/show_bug.cgi?id=171
1596 * We need a way to either ignore barrier acks for barriers sent before a device
1597 * was attached, or a way to wait for all pending barrier acks to come in.
1598 * As barriers are counted per resource,
1599 * we'd need to suspend io on all devices of a resource.
1600 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001601 wait_event(device->misc_wait, !atomic_read(&device->ap_pending_cnt) || drbd_suspended(device));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001602 /* and for any other previously queued work */
Andreas Gruenbacherb5043c52011-07-28 15:56:02 +02001603 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001604
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001605 rv = _drbd_request_state(device, NS(disk, D_ATTACHING), CS_VERBOSE);
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001606 retcode = rv; /* FIXME: Type mismatch. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001607 drbd_resume_io(device);
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001608 if (rv < SS_SUCCESS)
Tejun Heoe525fd82010-11-13 11:55:17 +01001609 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001610
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001611 if (!get_ldev_if_state(device, D_ATTACHING))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001612 goto force_diskless;
1613
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001614 if (!device->bitmap) {
1615 if (drbd_bm_init(device)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001616 retcode = ERR_NOMEM;
1617 goto force_diskless_dec;
1618 }
1619 }
1620
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001621 if (device->state.conn < C_CONNECTED &&
1622 device->state.role == R_PRIMARY &&
1623 (device->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001624 drbd_err(device, "Can only attach to data with current UUID=%016llX\n",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001625 (unsigned long long)device->ed_uuid);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001626 retcode = ERR_DATA_NOT_CURRENT;
1627 goto force_diskless_dec;
1628 }
1629
1630 /* Since we are diskless, fix the activity log first... */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001631 if (drbd_check_al_size(device, new_disk_conf)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001632 retcode = ERR_NOMEM;
1633 goto force_diskless_dec;
1634 }
1635
1636 /* Prevent shrinking of consistent devices ! */
1637 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001638 drbd_new_dev_size(device, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001639 drbd_warn(device, "refusing to truncate a consistent device\n");
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001640 retcode = ERR_DISK_TOO_SMALL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001641 goto force_diskless_dec;
1642 }
1643
Philipp Reisnerb411b362009-09-25 16:07:19 -07001644 /* Reset the "barriers don't work" bits here, then force meta data to
1645 * be written, to ensure we determine if barriers are supported. */
Andreas Gruenbachere5440462011-05-04 15:25:35 +02001646 if (new_disk_conf->md_flushes)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001647 clear_bit(MD_NO_FUA, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001648 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001649 set_bit(MD_NO_FUA, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001650
1651 /* Point of no return reached.
1652 * Devices and memory are no longer released by error cleanup below.
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001653 * now device takes over responsibility, and the state engine should
Philipp Reisnerb411b362009-09-25 16:07:19 -07001654 * clean it up somewhere. */
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +02001655 D_ASSERT(device, device->ldev == NULL);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001656 device->ldev = nbc;
1657 device->resync = resync_lru;
1658 device->rs_plan_s = new_plan;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001659 nbc = NULL;
1660 resync_lru = NULL;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001661 new_disk_conf = NULL;
Philipp Reisner9958c852011-05-03 16:19:31 +02001662 new_plan = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001663
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001664 drbd_bump_write_ordering(first_peer_device(device)->connection, WO_bdev_flush);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001665
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001666 if (drbd_md_test_flag(device->ldev, MDF_CRASHED_PRIMARY))
1667 set_bit(CRASHED_PRIMARY, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001668 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001669 clear_bit(CRASHED_PRIMARY, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001670
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001671 if (drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
Andreas Gruenbacher6bbf53c2011-07-08 01:19:44 +02001672 !(device->state.role == R_PRIMARY && device->resource->susp_nod))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001673 set_bit(CRASHED_PRIMARY, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001674
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001675 device->send_cnt = 0;
1676 device->recv_cnt = 0;
1677 device->read_cnt = 0;
1678 device->writ_cnt = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001679
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001680 drbd_reconsider_max_bio_size(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001681
1682 /* If I am currently not R_PRIMARY,
1683 * but meta data primary indicator is set,
1684 * I just now recover from a hard crash,
1685 * and have been R_PRIMARY before that crash.
1686 *
1687 * Now, if I had no connection before that crash
1688 * (have been degraded R_PRIMARY), chances are that
1689 * I won't find my peer now either.
1690 *
1691 * In that case, and _only_ in that case,
1692 * we use the degr-wfc-timeout instead of the default,
1693 * so we can automatically recover from a crash of a
1694 * degraded but active "cluster" after a certain timeout.
1695 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001696 clear_bit(USE_DEGR_WFC_T, &device->flags);
1697 if (device->state.role != R_PRIMARY &&
1698 drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
1699 !drbd_md_test_flag(device->ldev, MDF_CONNECTED_IND))
1700 set_bit(USE_DEGR_WFC_T, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001701
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001702 dd = drbd_determine_dev_size(device, 0, NULL);
Philipp Reisnerd752b262013-06-25 16:50:08 +02001703 if (dd <= DS_ERROR) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001704 retcode = ERR_NOMEM_BITMAP;
1705 goto force_diskless_dec;
Philipp Reisnere96c9632013-06-25 16:50:07 +02001706 } else if (dd == DS_GREW)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001707 set_bit(RESYNC_AFTER_NEG, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001708
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001709 if (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC) ||
1710 (test_bit(CRASHED_PRIMARY, &device->flags) &&
1711 drbd_md_test_flag(device->ldev, MDF_AL_DISABLED))) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001712 drbd_info(device, "Assuming that all blocks are out of sync "
Philipp Reisnerb411b362009-09-25 16:07:19 -07001713 "(aka FullSync)\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001714 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001715 "set_n_write from attaching", BM_LOCKED_MASK)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001716 retcode = ERR_IO_MD_DISK;
1717 goto force_diskless_dec;
1718 }
1719 } else {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001720 if (drbd_bitmap_io(device, &drbd_bm_read,
Andreas Gruenbacher22ab6a32010-12-13 01:44:11 +01001721 "read from attaching", BM_LOCKED_MASK)) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001722 retcode = ERR_IO_MD_DISK;
1723 goto force_diskless_dec;
1724 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001725 }
1726
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001727 if (_drbd_bm_total_weight(device) == drbd_bm_bits(device))
1728 drbd_suspend_al(device); /* IO is still suspended here... */
Philipp Reisner07782862010-08-31 12:00:50 +02001729
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001730 spin_lock_irq(&device->resource->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001731 os = drbd_read_state(device);
Philipp Reisner78bae592011-03-28 15:40:12 +02001732 ns = os;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001733 /* If MDF_CONSISTENT is not set go into inconsistent state,
1734 otherwise investigate MDF_WasUpToDate...
1735 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1736 otherwise into D_CONSISTENT state.
1737 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001738 if (drbd_md_test_flag(device->ldev, MDF_CONSISTENT)) {
1739 if (drbd_md_test_flag(device->ldev, MDF_WAS_UP_TO_DATE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001740 ns.disk = D_CONSISTENT;
1741 else
1742 ns.disk = D_OUTDATED;
1743 } else {
1744 ns.disk = D_INCONSISTENT;
1745 }
1746
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001747 if (drbd_md_test_flag(device->ldev, MDF_PEER_OUT_DATED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001748 ns.pdsk = D_OUTDATED;
1749
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001750 rcu_read_lock();
1751 if (ns.disk == D_CONSISTENT &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001752 (ns.pdsk == D_OUTDATED || rcu_dereference(device->ldev->disk_conf)->fencing == FP_DONT_CARE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001753 ns.disk = D_UP_TO_DATE;
1754
1755 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1756 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1757 this point, because drbd_request_state() modifies these
1758 flags. */
1759
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001760 if (rcu_dereference(device->ldev->disk_conf)->al_updates)
1761 device->ldev->md.flags &= ~MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001762 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001763 device->ldev->md.flags |= MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001764
1765 rcu_read_unlock();
1766
Philipp Reisnerb411b362009-09-25 16:07:19 -07001767 /* In case we are C_CONNECTED postpone any decision on the new disk
1768 state after the negotiation phase. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001769 if (device->state.conn == C_CONNECTED) {
1770 device->new_state_tmp.i = ns.i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001771 ns.i = os.i;
1772 ns.disk = D_NEGOTIATING;
Philipp Reisnerdc66c742010-06-02 14:31:29 +02001773
1774 /* We expect to receive up-to-date UUIDs soon.
1775 To avoid a race in receive_state, free p_uuid while
1776 holding req_lock. I.e. atomic with the state change */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001777 kfree(device->p_uuid);
1778 device->p_uuid = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001779 }
1780
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001781 rv = _drbd_set_state(device, ns, CS_VERBOSE, NULL);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001782 spin_unlock_irq(&device->resource->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001783
1784 if (rv < SS_SUCCESS)
1785 goto force_diskless_dec;
1786
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001787 mod_timer(&device->request_timer, jiffies + HZ);
Philipp Reisnercdfda632011-07-05 15:38:59 +02001788
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001789 if (device->state.role == R_PRIMARY)
1790 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001791 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001792 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001793
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001794 drbd_md_mark_dirty(device);
1795 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001796
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001797 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
1798 put_ldev(device);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001799 conn_reconfig_done(first_peer_device(device)->connection);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001800 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001801 return 0;
1802
1803 force_diskless_dec:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001804 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001805 force_diskless:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001806 drbd_force_state(device, NS(disk, D_DISKLESS));
1807 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001808 fail:
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001809 conn_reconfig_done(first_peer_device(device)->connection);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001810 if (nbc) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001811 if (nbc->backing_bdev)
1812 blkdev_put(nbc->backing_bdev,
1813 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1814 if (nbc->md_bdev)
1815 blkdev_put(nbc->md_bdev,
1816 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001817 kfree(nbc);
1818 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001819 kfree(new_disk_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001820 lc_destroy(resync_lru);
Philipp Reisner9958c852011-05-03 16:19:31 +02001821 kfree(new_plan);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001822
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001823 finish:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001824 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001825 return 0;
1826}
1827
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001828static int adm_detach(struct drbd_device *device, int force)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001829{
Philipp Reisner19f83c72011-03-29 14:21:03 +02001830 enum drbd_state_rv retcode;
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001831 int ret;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001832
Philipp Reisnercdfda632011-07-05 15:38:59 +02001833 if (force) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001834 set_bit(FORCE_DETACH, &device->flags);
1835 drbd_force_state(device, NS(disk, D_FAILED));
Philipp Reisnercdfda632011-07-05 15:38:59 +02001836 retcode = SS_SUCCESS;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001837 goto out;
1838 }
1839
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001840 drbd_suspend_io(device); /* so no-one is stuck in drbd_al_begin_io */
1841 drbd_md_get_buffer(device); /* make sure there is no in-flight meta-data IO */
1842 retcode = drbd_request_state(device, NS(disk, D_FAILED));
1843 drbd_md_put_buffer(device);
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001844 /* D_FAILED will transition to DISKLESS. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001845 ret = wait_event_interruptible(device->misc_wait,
1846 device->state.disk != D_FAILED);
1847 drbd_resume_io(device);
Philipp Reisner9b2f61a2011-05-24 10:27:38 +02001848 if ((int)retcode == (int)SS_IS_DISKLESS)
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001849 retcode = SS_NOTHING_TO_DO;
1850 if (ret)
1851 retcode = ERR_INTR;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001852out:
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001853 return retcode;
1854}
1855
Philipp Reisnerb411b362009-09-25 16:07:19 -07001856/* Detaching the disk is a process in multiple stages. First we need to lock
1857 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1858 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1859 * internal references as well.
1860 * Only then we have finally detached. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001861int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001862{
1863 enum drbd_ret_code retcode;
Philipp Reisnercdfda632011-07-05 15:38:59 +02001864 struct detach_parms parms = { };
1865 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001866
1867 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1868 if (!adm_ctx.reply_skb)
1869 return retcode;
1870 if (retcode != NO_ERROR)
1871 goto out;
1872
Philipp Reisnercdfda632011-07-05 15:38:59 +02001873 if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
1874 err = detach_parms_from_attrs(&parms, info);
1875 if (err) {
1876 retcode = ERR_MANDATORY_TAG;
1877 drbd_msg_put_info(from_attrs_err_to_txt(err));
1878 goto out;
1879 }
1880 }
1881
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001882 retcode = adm_detach(adm_ctx.device, parms.force_detach);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001883out:
1884 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001885 return 0;
1886}
1887
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001888static bool conn_resync_running(struct drbd_connection *connection)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001889{
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001890 struct drbd_peer_device *peer_device;
Philipp Reisner695d08f2011-04-11 22:53:32 -07001891 bool rv = false;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001892 int vnr;
1893
Philipp Reisner695d08f2011-04-11 22:53:32 -07001894 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001895 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1896 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001897 if (device->state.conn == C_SYNC_SOURCE ||
1898 device->state.conn == C_SYNC_TARGET ||
1899 device->state.conn == C_PAUSED_SYNC_S ||
1900 device->state.conn == C_PAUSED_SYNC_T) {
Philipp Reisner695d08f2011-04-11 22:53:32 -07001901 rv = true;
1902 break;
1903 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001904 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001905 rcu_read_unlock();
1906
1907 return rv;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001908}
1909
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001910static bool conn_ov_running(struct drbd_connection *connection)
Lars Ellenbergf3990022011-03-23 14:31:09 +01001911{
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001912 struct drbd_peer_device *peer_device;
Philipp Reisner695d08f2011-04-11 22:53:32 -07001913 bool rv = false;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001914 int vnr;
1915
Philipp Reisner695d08f2011-04-11 22:53:32 -07001916 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001917 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1918 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001919 if (device->state.conn == C_VERIFY_S ||
1920 device->state.conn == C_VERIFY_T) {
Philipp Reisner695d08f2011-04-11 22:53:32 -07001921 rv = true;
1922 break;
1923 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001924 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001925 rcu_read_unlock();
1926
1927 return rv;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001928}
1929
Philipp Reisnercd643972011-04-13 18:00:59 -07001930static enum drbd_ret_code
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001931_check_net_options(struct drbd_connection *connection, struct net_conf *old_net_conf, struct net_conf *new_net_conf)
Philipp Reisnercd643972011-04-13 18:00:59 -07001932{
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001933 struct drbd_peer_device *peer_device;
Philipp Reisnercd643972011-04-13 18:00:59 -07001934 int i;
1935
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001936 if (old_net_conf && connection->cstate == C_WF_REPORT_PARAMS && connection->agreed_pro_version < 100) {
1937 if (new_net_conf->wire_protocol != old_net_conf->wire_protocol)
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02001938 return ERR_NEED_APV_100;
1939
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001940 if (new_net_conf->two_primaries != old_net_conf->two_primaries)
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02001941 return ERR_NEED_APV_100;
1942
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001943 if (strcmp(new_net_conf->integrity_alg, old_net_conf->integrity_alg))
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02001944 return ERR_NEED_APV_100;
1945 }
1946
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001947 if (!new_net_conf->two_primaries &&
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001948 conn_highest_role(connection) == R_PRIMARY &&
1949 conn_highest_peer(connection) == R_PRIMARY)
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02001950 return ERR_NEED_ALLOW_TWO_PRI;
Philipp Reisnerb032b6f2011-04-13 18:16:10 -07001951
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001952 if (new_net_conf->two_primaries &&
1953 (new_net_conf->wire_protocol != DRBD_PROT_C))
Philipp Reisnercd643972011-04-13 18:00:59 -07001954 return ERR_NOT_PROTO_C;
1955
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001956 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
1957 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001958 if (get_ldev(device)) {
1959 enum drbd_fencing_p fp = rcu_dereference(device->ldev->disk_conf)->fencing;
1960 put_ldev(device);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001961 if (new_net_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
Philipp Reisnercd643972011-04-13 18:00:59 -07001962 return ERR_STONITH_AND_PROT_A;
Philipp Reisnercd643972011-04-13 18:00:59 -07001963 }
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001964 if (device->state.role == R_PRIMARY && new_net_conf->discard_my_data)
Lars Ellenbergeb120102012-08-01 12:46:20 +02001965 return ERR_DISCARD_IMPOSSIBLE;
Philipp Reisnercd643972011-04-13 18:00:59 -07001966 }
Philipp Reisnercd643972011-04-13 18:00:59 -07001967
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001968 if (new_net_conf->on_congestion != OC_BLOCK && new_net_conf->wire_protocol != DRBD_PROT_A)
Philipp Reisnercd643972011-04-13 18:00:59 -07001969 return ERR_CONG_NOT_PROTO_A;
1970
1971 return NO_ERROR;
1972}
1973
Philipp Reisner44ed1672011-04-19 17:10:19 +02001974static enum drbd_ret_code
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001975check_net_options(struct drbd_connection *connection, struct net_conf *new_net_conf)
Philipp Reisner44ed1672011-04-19 17:10:19 +02001976{
1977 static enum drbd_ret_code rv;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001978 struct drbd_peer_device *peer_device;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001979 int i;
1980
1981 rcu_read_lock();
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001982 rv = _check_net_options(connection, rcu_dereference(connection->net_conf), new_net_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001983 rcu_read_unlock();
1984
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001985 /* connection->volumes protected by genl_lock() here */
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001986 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
1987 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001988 if (!device->bitmap) {
1989 if (drbd_bm_init(device))
Philipp Reisner44ed1672011-04-19 17:10:19 +02001990 return ERR_NOMEM;
1991 }
1992 }
1993
1994 return rv;
1995}
1996
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001997struct crypto {
1998 struct crypto_hash *verify_tfm;
1999 struct crypto_hash *csums_tfm;
2000 struct crypto_hash *cram_hmac_tfm;
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02002001 struct crypto_hash *integrity_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002002};
2003
2004static int
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002005alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002006{
2007 if (!tfm_name[0])
2008 return NO_ERROR;
2009
2010 *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
2011 if (IS_ERR(*tfm)) {
2012 *tfm = NULL;
2013 return err_alg;
2014 }
2015
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002016 return NO_ERROR;
2017}
2018
2019static enum drbd_ret_code
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002020alloc_crypto(struct crypto *crypto, struct net_conf *new_net_conf)
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002021{
Philipp Reisnerb411b362009-09-25 16:07:19 -07002022 char hmac_name[CRYPTO_MAX_ALG_NAME];
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002023 enum drbd_ret_code rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002024
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002025 rv = alloc_hash(&crypto->csums_tfm, new_net_conf->csums_alg,
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002026 ERR_CSUMS_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002027 if (rv != NO_ERROR)
2028 return rv;
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002029 rv = alloc_hash(&crypto->verify_tfm, new_net_conf->verify_alg,
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002030 ERR_VERIFY_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002031 if (rv != NO_ERROR)
2032 return rv;
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002033 rv = alloc_hash(&crypto->integrity_tfm, new_net_conf->integrity_alg,
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002034 ERR_INTEGRITY_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002035 if (rv != NO_ERROR)
2036 return rv;
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002037 if (new_net_conf->cram_hmac_alg[0] != 0) {
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002038 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002039 new_net_conf->cram_hmac_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002040
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002041 rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
2042 ERR_AUTH_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002043 }
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002044
2045 return rv;
2046}
2047
2048static void free_crypto(struct crypto *crypto)
2049{
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002050 crypto_free_hash(crypto->cram_hmac_tfm);
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02002051 crypto_free_hash(crypto->integrity_tfm);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002052 crypto_free_hash(crypto->csums_tfm);
2053 crypto_free_hash(crypto->verify_tfm);
2054}
2055
Lars Ellenbergf3990022011-03-23 14:31:09 +01002056int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
2057{
2058 enum drbd_ret_code retcode;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002059 struct drbd_connection *connection;
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002060 struct net_conf *old_net_conf, *new_net_conf = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002061 int err;
2062 int ovr; /* online verify running */
2063 int rsr; /* re-sync running */
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002064 struct crypto crypto = { };
Lars Ellenbergf3990022011-03-23 14:31:09 +01002065
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002066 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002067 if (!adm_ctx.reply_skb)
2068 return retcode;
2069 if (retcode != NO_ERROR)
2070 goto out;
2071
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002072 connection = adm_ctx.connection;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002073
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002074 new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
2075 if (!new_net_conf) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002076 retcode = ERR_NOMEM;
2077 goto out;
2078 }
2079
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002080 conn_reconfig_start(connection);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002081
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002082 mutex_lock(&connection->data.mutex);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002083 mutex_lock(&connection->resource->conf_update);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002084 old_net_conf = connection->net_conf;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002085
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002086 if (!old_net_conf) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02002087 drbd_msg_put_info("net conf missing, try connect");
2088 retcode = ERR_INVALID_REQUEST;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002089 goto fail;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002090 }
2091
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002092 *new_net_conf = *old_net_conf;
Lars Ellenberg5979e362011-04-27 21:09:55 +02002093 if (should_set_defaults(info))
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002094 set_net_conf_defaults(new_net_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +02002095
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002096 err = net_conf_from_attrs_for_change(new_net_conf, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02002097 if (err && err != -ENOMSG) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002098 retcode = ERR_MANDATORY_TAG;
2099 drbd_msg_put_info(from_attrs_err_to_txt(err));
2100 goto fail;
2101 }
2102
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002103 retcode = check_net_options(connection, new_net_conf);
Philipp Reisnercd643972011-04-13 18:00:59 -07002104 if (retcode != NO_ERROR)
2105 goto fail;
2106
Lars Ellenbergf3990022011-03-23 14:31:09 +01002107 /* re-sync running */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002108 rsr = conn_resync_running(connection);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002109 if (rsr && strcmp(new_net_conf->csums_alg, old_net_conf->csums_alg)) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002110 retcode = ERR_CSUMS_RESYNC_RUNNING;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002111 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002112 }
2113
Lars Ellenbergf3990022011-03-23 14:31:09 +01002114 /* online verify running */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002115 ovr = conn_ov_running(connection);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002116 if (ovr && strcmp(new_net_conf->verify_alg, old_net_conf->verify_alg)) {
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002117 retcode = ERR_VERIFY_RUNNING;
2118 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002119 }
2120
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002121 retcode = alloc_crypto(&crypto, new_net_conf);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002122 if (retcode != NO_ERROR)
2123 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002124
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002125 rcu_assign_pointer(connection->net_conf, new_net_conf);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002126
2127 if (!rsr) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002128 crypto_free_hash(connection->csums_tfm);
2129 connection->csums_tfm = crypto.csums_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002130 crypto.csums_tfm = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002131 }
2132 if (!ovr) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002133 crypto_free_hash(connection->verify_tfm);
2134 connection->verify_tfm = crypto.verify_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002135 crypto.verify_tfm = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002136 }
2137
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002138 crypto_free_hash(connection->integrity_tfm);
2139 connection->integrity_tfm = crypto.integrity_tfm;
2140 if (connection->cstate >= C_WF_REPORT_PARAMS && connection->agreed_pro_version >= 100)
2141 /* Do this without trying to take connection->data.mutex again. */
2142 __drbd_send_protocol(connection, P_PROTOCOL_UPDATE);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002143
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002144 crypto_free_hash(connection->cram_hmac_tfm);
2145 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002146
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002147 mutex_unlock(&connection->resource->conf_update);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002148 mutex_unlock(&connection->data.mutex);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002149 synchronize_rcu();
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002150 kfree(old_net_conf);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002151
Andreas Gruenbacher69a22772011-08-09 00:47:13 +02002152 if (connection->cstate >= C_WF_REPORT_PARAMS) {
2153 struct drbd_peer_device *peer_device;
2154 int vnr;
2155
2156 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
2157 drbd_send_sync_param(peer_device);
2158 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01002159
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002160 goto done;
2161
Lars Ellenbergf3990022011-03-23 14:31:09 +01002162 fail:
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002163 mutex_unlock(&connection->resource->conf_update);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002164 mutex_unlock(&connection->data.mutex);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002165 free_crypto(&crypto);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002166 kfree(new_net_conf);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002167 done:
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002168 conn_reconfig_done(connection);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002169 out:
2170 drbd_adm_finish(info, retcode);
2171 return 0;
2172}
2173
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002174int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002175{
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002176 struct drbd_peer_device *peer_device;
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002177 struct net_conf *old_net_conf, *new_net_conf = NULL;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002178 struct crypto crypto = { };
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002179 struct drbd_resource *resource;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002180 struct drbd_connection *connection;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002181 enum drbd_ret_code retcode;
2182 int i;
2183 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002184
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02002185 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002186
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002187 if (!adm_ctx.reply_skb)
2188 return retcode;
2189 if (retcode != NO_ERROR)
2190 goto out;
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002191 if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
2192 drbd_msg_put_info("connection endpoint(s) missing");
2193 retcode = ERR_INVALID_REQUEST;
2194 goto out;
2195 }
2196
2197 /* No need for _rcu here. All reconfiguration is
2198 * strictly serialized on genl_lock(). We are protected against
2199 * concurrent reconfiguration/addition/deletion */
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002200 for_each_resource(resource, &drbd_resources) {
2201 for_each_connection(connection, resource) {
2202 if (nla_len(adm_ctx.my_addr) == connection->my_addr_len &&
2203 !memcmp(nla_data(adm_ctx.my_addr), &connection->my_addr,
2204 connection->my_addr_len)) {
2205 retcode = ERR_LOCAL_ADDR;
2206 goto out;
2207 }
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002208
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002209 if (nla_len(adm_ctx.peer_addr) == connection->peer_addr_len &&
2210 !memcmp(nla_data(adm_ctx.peer_addr), &connection->peer_addr,
2211 connection->peer_addr_len)) {
2212 retcode = ERR_PEER_ADDR;
2213 goto out;
2214 }
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002215 }
2216 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002217
Andreas Gruenbacher3ab706f2011-07-06 15:05:58 +02002218 connection = first_connection(adm_ctx.resource);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002219 conn_reconfig_start(connection);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002220
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002221 if (connection->cstate > C_STANDALONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002222 retcode = ERR_NET_CONFIGURED;
2223 goto fail;
2224 }
2225
Andreas Gruenbachera209b4a2011-08-17 12:43:25 +02002226 /* allocation not in the IO path, drbdsetup / netlink process context */
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002227 new_net_conf = kzalloc(sizeof(*new_net_conf), GFP_KERNEL);
2228 if (!new_net_conf) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002229 retcode = ERR_NOMEM;
2230 goto fail;
2231 }
2232
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002233 set_net_conf_defaults(new_net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002234
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002235 err = net_conf_from_attrs(new_net_conf, info);
Lars Ellenberg25e40932011-08-19 10:39:00 +02002236 if (err && err != -ENOMSG) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002237 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002238 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002239 goto fail;
2240 }
2241
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002242 retcode = check_net_options(connection, new_net_conf);
Philipp Reisnercd643972011-04-13 18:00:59 -07002243 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002244 goto fail;
Philipp Reisner47ff2d02010-06-18 13:56:57 +02002245
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002246 retcode = alloc_crypto(&crypto, new_net_conf);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002247 if (retcode != NO_ERROR)
Philipp Reisner422028b2010-10-27 11:12:07 +02002248 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002249
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002250 ((char *)new_net_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002251
Andreas Gruenbacherb5043c52011-07-28 15:56:02 +02002252 drbd_flush_workqueue(&connection->sender_work);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002253
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002254 mutex_lock(&adm_ctx.resource->conf_update);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002255 old_net_conf = connection->net_conf;
2256 if (old_net_conf) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002257 retcode = ERR_NET_CONFIGURED;
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002258 mutex_unlock(&adm_ctx.resource->conf_update);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002259 goto fail;
2260 }
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002261 rcu_assign_pointer(connection->net_conf, new_net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002262
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002263 conn_free_crypto(connection);
2264 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
2265 connection->integrity_tfm = crypto.integrity_tfm;
2266 connection->csums_tfm = crypto.csums_tfm;
2267 connection->verify_tfm = crypto.verify_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002268
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002269 connection->my_addr_len = nla_len(adm_ctx.my_addr);
2270 memcpy(&connection->my_addr, nla_data(adm_ctx.my_addr), connection->my_addr_len);
2271 connection->peer_addr_len = nla_len(adm_ctx.peer_addr);
2272 memcpy(&connection->peer_addr, nla_data(adm_ctx.peer_addr), connection->peer_addr_len);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002273
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002274 mutex_unlock(&adm_ctx.resource->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002275
Philipp Reisner695d08f2011-04-11 22:53:32 -07002276 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002277 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2278 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002279 device->send_cnt = 0;
2280 device->recv_cnt = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002281 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07002282 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002283
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002284 retcode = conn_request_state(connection, NS(conn, C_UNCONNECTED), CS_VERBOSE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002285
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002286 conn_reconfig_done(connection);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002287 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002288 return 0;
2289
2290fail:
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002291 free_crypto(&crypto);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002292 kfree(new_net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002293
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002294 conn_reconfig_done(connection);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002295out:
2296 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002297 return 0;
2298}
2299
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002300static enum drbd_state_rv conn_try_disconnect(struct drbd_connection *connection, bool force)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002301{
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002302 enum drbd_state_rv rv;
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002303
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002304 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002305 force ? CS_HARD : 0);
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002306
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002307 switch (rv) {
2308 case SS_NOTHING_TO_DO:
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002309 break;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002310 case SS_ALREADY_STANDALONE:
2311 return SS_SUCCESS;
2312 case SS_PRIMARY_NOP:
2313 /* Our state checking code wants to see the peer outdated. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002314 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING, pdsk, D_OUTDATED), 0);
Philipp Reisner2bd5ed52013-03-27 14:08:40 +01002315
2316 if (rv == SS_OUTDATE_WO_CONN) /* lost connection before graceful disconnect succeeded */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002317 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_VERBOSE);
Philipp Reisner2bd5ed52013-03-27 14:08:40 +01002318
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002319 break;
2320 case SS_CW_FAILED_BY_PEER:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002321 /* The peer probably wants to see us outdated. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002322 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING,
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002323 disk, D_OUTDATED), 0);
2324 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002325 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002326 CS_HARD);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002327 }
2328 break;
2329 default:;
2330 /* no special handling necessary */
2331 }
2332
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002333 if (rv >= SS_SUCCESS) {
2334 enum drbd_state_rv rv2;
2335 /* No one else can reconfigure the network while I am here.
2336 * The state handling only uses drbd_thread_stop_nowait(),
2337 * we want to really wait here until the receiver is no more.
2338 */
Andreas Gruenbacher9693da22011-06-21 13:58:18 +02002339 drbd_thread_stop(&connection->receiver);
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002340
2341 /* Race breaker. This additional state change request may be
2342 * necessary, if this was a forced disconnect during a receiver
2343 * restart. We may have "killed" the receiver thread just
Andreas Gruenbacher8fe60552011-07-22 11:04:36 +02002344 * after drbd_receiver() returned. Typically, we should be
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002345 * C_STANDALONE already, now, and this becomes a no-op.
2346 */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002347 rv2 = conn_request_state(connection, NS(conn, C_STANDALONE),
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002348 CS_VERBOSE | CS_HARD);
2349 if (rv2 < SS_SUCCESS)
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +02002350 drbd_err(connection,
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002351 "unexpected rv2=%d in conn_try_disconnect()\n",
2352 rv2);
2353 }
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002354 return rv;
2355}
2356
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002357int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002358{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002359 struct disconnect_parms parms;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002360 struct drbd_connection *connection;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002361 enum drbd_state_rv rv;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002362 enum drbd_ret_code retcode;
2363 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002364
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002365 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002366 if (!adm_ctx.reply_skb)
2367 return retcode;
2368 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002369 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002370
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002371 connection = adm_ctx.connection;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002372 memset(&parms, 0, sizeof(parms));
2373 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002374 err = disconnect_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002375 if (err) {
2376 retcode = ERR_MANDATORY_TAG;
2377 drbd_msg_put_info(from_attrs_err_to_txt(err));
2378 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002379 }
2380 }
2381
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002382 rv = conn_try_disconnect(connection, parms.force_disconnect);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002383 if (rv < SS_SUCCESS)
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002384 retcode = rv; /* FIXME: Type mismatch. */
2385 else
2386 retcode = NO_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002387 fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002388 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002389 return 0;
2390}
2391
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002392void resync_after_online_grow(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002393{
2394 int iass; /* I am sync source */
2395
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02002396 drbd_info(device, "Resync of new storage after online grow\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002397 if (device->state.role != device->state.peer)
2398 iass = (device->state.role == R_PRIMARY);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002399 else
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002400 iass = test_bit(RESOLVE_CONFLICTS, &first_peer_device(device)->connection->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002401
2402 if (iass)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002403 drbd_start_resync(device, C_SYNC_SOURCE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002404 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002405 _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002406}
2407
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002408int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002409{
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002410 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002411 struct resize_parms rs;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002412 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002413 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002414 enum determine_dev_size dd;
Philipp Reisnerd752b262013-06-25 16:50:08 +02002415 bool change_al_layout = false;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002416 enum dds_flags ddsf;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002417 sector_t u_size;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002418 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002419
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002420 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2421 if (!adm_ctx.reply_skb)
2422 return retcode;
2423 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002424 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002425
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002426 device = adm_ctx.device;
2427 if (!get_ldev(device)) {
Philipp Reisnerd752b262013-06-25 16:50:08 +02002428 retcode = ERR_NO_DISK;
2429 goto fail;
2430 }
2431
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002432 memset(&rs, 0, sizeof(struct resize_parms));
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002433 rs.al_stripes = device->ldev->md.al_stripes;
2434 rs.al_stripe_size = device->ldev->md.al_stripe_size_4k * 4;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002435 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002436 err = resize_parms_from_attrs(&rs, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002437 if (err) {
2438 retcode = ERR_MANDATORY_TAG;
2439 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerd752b262013-06-25 16:50:08 +02002440 goto fail_ldev;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002441 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002442 }
2443
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002444 if (device->state.conn > C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002445 retcode = ERR_RESIZE_RESYNC;
Philipp Reisnerd752b262013-06-25 16:50:08 +02002446 goto fail_ldev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002447 }
2448
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002449 if (device->state.role == R_SECONDARY &&
2450 device->state.peer == R_SECONDARY) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002451 retcode = ERR_NO_PRIMARY;
Philipp Reisnerd752b262013-06-25 16:50:08 +02002452 goto fail_ldev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002453 }
2454
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002455 if (rs.no_resync && first_peer_device(device)->connection->agreed_pro_version < 93) {
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002456 retcode = ERR_NEED_APV_93;
Andreas Gruenbacher7b4e4d32011-09-28 22:15:04 +02002457 goto fail_ldev;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002458 }
2459
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002460 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002461 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002462 rcu_read_unlock();
2463 if (u_size != (sector_t)rs.resize_size) {
2464 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
2465 if (!new_disk_conf) {
2466 retcode = ERR_NOMEM;
Philipp Reisner9bcd2522011-09-29 13:00:14 +02002467 goto fail_ldev;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002468 }
2469 }
2470
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002471 if (device->ldev->md.al_stripes != rs.al_stripes ||
2472 device->ldev->md.al_stripe_size_4k != rs.al_stripe_size / 4) {
Philipp Reisnerd752b262013-06-25 16:50:08 +02002473 u32 al_size_k = rs.al_stripes * rs.al_stripe_size;
2474
2475 if (al_size_k > (16 * 1024 * 1024)) {
2476 retcode = ERR_MD_LAYOUT_TOO_BIG;
2477 goto fail_ldev;
2478 }
2479
2480 if (al_size_k < MD_32kB_SECT/2) {
2481 retcode = ERR_MD_LAYOUT_TOO_SMALL;
2482 goto fail_ldev;
2483 }
2484
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002485 if (device->state.conn != C_CONNECTED) {
Philipp Reisnerd752b262013-06-25 16:50:08 +02002486 retcode = ERR_MD_LAYOUT_CONNECTED;
2487 goto fail_ldev;
2488 }
2489
2490 change_al_layout = true;
2491 }
2492
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002493 if (device->ldev->known_size != drbd_get_capacity(device->ldev->backing_bdev))
2494 device->ldev->known_size = drbd_get_capacity(device->ldev->backing_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002495
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002496 if (new_disk_conf) {
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002497 mutex_lock(&device->resource->conf_update);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002498 old_disk_conf = device->ldev->disk_conf;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002499 *new_disk_conf = *old_disk_conf;
2500 new_disk_conf->disk_size = (sector_t)rs.resize_size;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002501 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002502 mutex_unlock(&device->resource->conf_update);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002503 synchronize_rcu();
2504 kfree(old_disk_conf);
2505 }
2506
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002507 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002508 dd = drbd_determine_dev_size(device, ddsf, change_al_layout ? &rs : NULL);
2509 drbd_md_sync(device);
2510 put_ldev(device);
Philipp Reisnere96c9632013-06-25 16:50:07 +02002511 if (dd == DS_ERROR) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002512 retcode = ERR_NOMEM_BITMAP;
2513 goto fail;
Philipp Reisnerd752b262013-06-25 16:50:08 +02002514 } else if (dd == DS_ERROR_SPACE_MD) {
2515 retcode = ERR_MD_LAYOUT_NO_FIT;
2516 goto fail;
2517 } else if (dd == DS_ERROR_SHRINK) {
2518 retcode = ERR_IMPLICIT_SHRINK;
2519 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002520 }
2521
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002522 if (device->state.conn == C_CONNECTED) {
Philipp Reisnere96c9632013-06-25 16:50:07 +02002523 if (dd == DS_GREW)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002524 set_bit(RESIZE_PENDING, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002525
Andreas Gruenbacher69a22772011-08-09 00:47:13 +02002526 drbd_send_uuids(first_peer_device(device));
2527 drbd_send_sizes(first_peer_device(device), 1, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002528 }
2529
2530 fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002531 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002532 return 0;
Andreas Gruenbacher7b4e4d32011-09-28 22:15:04 +02002533
2534 fail_ldev:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002535 put_ldev(device);
Andreas Gruenbacher7b4e4d32011-09-28 22:15:04 +02002536 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002537}
2538
Lars Ellenbergf3990022011-03-23 14:31:09 +01002539int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002540{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002541 enum drbd_ret_code retcode;
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002542 struct res_opts res_opts;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002543 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002544
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02002545 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002546 if (!adm_ctx.reply_skb)
2547 return retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002548 if (retcode != NO_ERROR)
2549 goto fail;
2550
Andreas Gruenbachereb6bea62011-06-21 16:11:28 +02002551 res_opts = adm_ctx.resource->res_opts;
Lars Ellenberg5979e362011-04-27 21:09:55 +02002552 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02002553 set_res_opts_defaults(&res_opts);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002554
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002555 err = res_opts_from_attrs(&res_opts, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02002556 if (err && err != -ENOMSG) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002557 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002558 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002559 goto fail;
2560 }
2561
Andreas Gruenbachereb6bea62011-06-21 16:11:28 +02002562 err = set_resource_options(adm_ctx.resource, &res_opts);
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02002563 if (err) {
2564 retcode = ERR_INVALID_REQUEST;
2565 if (err == -ENOMEM)
Philipp Reisner778f2712010-07-06 11:14:00 +02002566 retcode = ERR_NOMEM;
Philipp Reisner778f2712010-07-06 11:14:00 +02002567 }
2568
Philipp Reisnerb411b362009-09-25 16:07:19 -07002569fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002570 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002571 return 0;
2572}
2573
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002574int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002575{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002576 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002577 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2578
2579 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2580 if (!adm_ctx.reply_skb)
2581 return retcode;
2582 if (retcode != NO_ERROR)
2583 goto out;
2584
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002585 device = adm_ctx.device;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002586
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002587 /* If there is still bitmap IO pending, probably because of a previous
Lars Ellenberg7ee1fb92012-06-19 10:27:58 +02002588 * resync just being finished, wait for it before requesting a new resync.
2589 * Also wait for it's after_state_ch(). */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002590 drbd_suspend_io(device);
2591 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
Andreas Gruenbacherb5043c52011-07-28 15:56:02 +02002592 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002593
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002594 /* If we happen to be C_STANDALONE R_SECONDARY, just change to
2595 * D_INCONSISTENT, and set all bits in the bitmap. Otherwise,
2596 * try to start a resync handshake as sync target for full sync.
Philipp Reisner9376d9f2013-03-27 14:08:36 +01002597 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002598 if (device->state.conn == C_STANDALONE && device->state.role == R_SECONDARY) {
2599 retcode = drbd_request_state(device, NS(disk, D_INCONSISTENT));
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002600 if (retcode >= SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002601 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002602 "set_n_write from invalidate", BM_LOCKED_MASK))
2603 retcode = ERR_IO_MD_DISK;
2604 }
2605 } else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002606 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_T));
2607 drbd_resume_io(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002608
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002609out:
2610 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002611 return 0;
2612}
2613
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002614static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2615 union drbd_state mask, union drbd_state val)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002616{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002617 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002618
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002619 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2620 if (!adm_ctx.reply_skb)
2621 return retcode;
2622 if (retcode != NO_ERROR)
2623 goto out;
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002624
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002625 retcode = drbd_request_state(adm_ctx.device, mask, val);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002626out:
2627 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002628 return 0;
2629}
2630
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002631static int drbd_bmio_set_susp_al(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002632{
2633 int rv;
2634
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002635 rv = drbd_bmio_set_n_write(device);
2636 drbd_suspend_al(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002637 return rv;
2638}
2639
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002640int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002641{
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002642 int retcode; /* drbd_ret_code, drbd_state_rv */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002643 struct drbd_device *device;
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002644
2645 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2646 if (!adm_ctx.reply_skb)
2647 return retcode;
2648 if (retcode != NO_ERROR)
2649 goto out;
2650
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002651 device = adm_ctx.device;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002652
2653 /* If there is still bitmap IO pending, probably because of a previous
Lars Ellenberg7ee1fb92012-06-19 10:27:58 +02002654 * resync just being finished, wait for it before requesting a new resync.
2655 * Also wait for it's after_state_ch(). */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002656 drbd_suspend_io(device);
2657 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
Andreas Gruenbacherb5043c52011-07-28 15:56:02 +02002658 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002659
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002660 /* If we happen to be C_STANDALONE R_PRIMARY, just set all bits
2661 * in the bitmap. Otherwise, try to start a resync handshake
2662 * as sync source for full sync.
2663 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002664 if (device->state.conn == C_STANDALONE && device->state.role == R_PRIMARY) {
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002665 /* The peer will get a resync upon connect anyways. Just make that
2666 into a full resync. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002667 retcode = drbd_request_state(device, NS(pdsk, D_INCONSISTENT));
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002668 if (retcode >= SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002669 if (drbd_bitmap_io(device, &drbd_bmio_set_susp_al,
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002670 "set_n_write from invalidate_peer",
2671 BM_LOCKED_SET_ALLOWED))
2672 retcode = ERR_IO_MD_DISK;
2673 }
2674 } else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002675 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_S));
2676 drbd_resume_io(device);
Philipp Reisner07782862010-08-31 12:00:50 +02002677
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002678out:
2679 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002680 return 0;
2681}
2682
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002683int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002684{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002685 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002686
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002687 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2688 if (!adm_ctx.reply_skb)
2689 return retcode;
2690 if (retcode != NO_ERROR)
2691 goto out;
2692
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002693 if (drbd_request_state(adm_ctx.device, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002694 retcode = ERR_PAUSE_IS_SET;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002695out:
2696 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002697 return 0;
2698}
2699
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002700int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002701{
Philipp Reisnerda9fbc22011-03-29 10:52:01 +02002702 union drbd_dev_state s;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002703 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002704
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002705 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2706 if (!adm_ctx.reply_skb)
2707 return retcode;
2708 if (retcode != NO_ERROR)
2709 goto out;
2710
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002711 if (drbd_request_state(adm_ctx.device, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2712 s = adm_ctx.device->state;
Philipp Reisnercd88d032011-01-20 11:46:41 +01002713 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2714 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2715 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2716 } else {
2717 retcode = ERR_PAUSE_IS_CLEAR;
2718 }
2719 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002720
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002721out:
2722 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002723 return 0;
2724}
2725
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002726int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002727{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002728 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002729}
2730
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002731int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002732{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002733 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002734 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2735
2736 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2737 if (!adm_ctx.reply_skb)
2738 return retcode;
2739 if (retcode != NO_ERROR)
2740 goto out;
2741
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002742 device = adm_ctx.device;
2743 if (test_bit(NEW_CUR_UUID, &device->flags)) {
2744 drbd_uuid_new_current(device);
2745 clear_bit(NEW_CUR_UUID, &device->flags);
Philipp Reisner43a51822010-06-11 11:26:34 +02002746 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002747 drbd_suspend_io(device);
2748 retcode = drbd_request_state(device, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002749 if (retcode == SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002750 if (device->state.conn < C_CONNECTED)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002751 tl_clear(first_peer_device(device)->connection);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002752 if (device->state.disk == D_DISKLESS || device->state.disk == D_FAILED)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002753 tl_restart(first_peer_device(device)->connection, FAIL_FROZEN_DISK_IO);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002754 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002755 drbd_resume_io(device);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002756
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002757out:
2758 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002759 return 0;
2760}
2761
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002762int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002763{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002764 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002765}
2766
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002767static int nla_put_drbd_cfg_context(struct sk_buff *skb,
2768 struct drbd_resource *resource,
2769 struct drbd_connection *connection,
2770 struct drbd_device *device)
Lars Ellenberg543cc102011-03-10 22:18:18 +01002771{
2772 struct nlattr *nla;
2773 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
2774 if (!nla)
2775 goto nla_put_failure;
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002776 if (device &&
2777 nla_put_u32(skb, T_ctx_volume, device->vnr))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002778 goto nla_put_failure;
Andreas Gruenbacherf597f6b2014-02-19 10:49:07 +01002779 if (nla_put_string(skb, T_ctx_resource_name, resource->name))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002780 goto nla_put_failure;
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002781 if (connection) {
2782 if (connection->my_addr_len &&
2783 nla_put(skb, T_ctx_my_addr, connection->my_addr_len, &connection->my_addr))
2784 goto nla_put_failure;
2785 if (connection->peer_addr_len &&
2786 nla_put(skb, T_ctx_peer_addr, connection->peer_addr_len, &connection->peer_addr))
2787 goto nla_put_failure;
2788 }
Lars Ellenberg543cc102011-03-10 22:18:18 +01002789 nla_nest_end(skb, nla);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002790 return 0;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002791
2792nla_put_failure:
2793 if (nla)
2794 nla_nest_cancel(skb, nla);
2795 return -EMSGSIZE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002796}
2797
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002798/*
2799 * Return the connection of @resource if @resource has exactly one connection.
2800 */
2801static struct drbd_connection *the_only_connection(struct drbd_resource *resource)
2802{
2803 struct list_head *connections = &resource->connections;
2804
2805 if (list_empty(connections) || connections->next->next != connections)
2806 return NULL;
2807 return list_first_entry(&resource->connections, struct drbd_connection, connections);
2808}
2809
2810int nla_put_status_info(struct sk_buff *skb, struct drbd_device *device,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002811 const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002812{
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002813 struct drbd_resource *resource = device->resource;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002814 struct state_info *si = NULL; /* for sizeof(si->member); */
2815 struct nlattr *nla;
2816 int got_ldev;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002817 int err = 0;
2818 int exclude_sensitive;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002819
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002820 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2821 * to. So we better exclude_sensitive information.
2822 *
2823 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2824 * in the context of the requesting user process. Exclude sensitive
2825 * information, unless current has superuser.
2826 *
2827 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2828 * relies on the current implementation of netlink_dump(), which
2829 * executes the dump callback successively from netlink_recvmsg(),
2830 * always in the context of the receiving process */
2831 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002832
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002833 got_ldev = get_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002834
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002835 /* We need to add connection name and volume number information still.
2836 * Minor number is in drbd_genlmsghdr. */
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002837 if (nla_put_drbd_cfg_context(skb, resource, the_only_connection(resource), device))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002838 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002839
Andreas Gruenbachereb6bea62011-06-21 16:11:28 +02002840 if (res_opts_to_skb(skb, &device->resource->res_opts, exclude_sensitive))
Lars Ellenbergf3990022011-03-23 14:31:09 +01002841 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002842
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002843 rcu_read_lock();
Andreas Gruenbacherf9eb7bf2013-06-25 16:50:05 +02002844 if (got_ldev) {
2845 struct disk_conf *disk_conf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002846
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002847 disk_conf = rcu_dereference(device->ldev->disk_conf);
Andreas Gruenbacherf9eb7bf2013-06-25 16:50:05 +02002848 err = disk_conf_to_skb(skb, disk_conf, exclude_sensitive);
2849 }
2850 if (!err) {
2851 struct net_conf *nc;
2852
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002853 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
Andreas Gruenbacherf9eb7bf2013-06-25 16:50:05 +02002854 if (nc)
2855 err = net_conf_to_skb(skb, nc, exclude_sensitive);
2856 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002857 rcu_read_unlock();
2858 if (err)
2859 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002860
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002861 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
2862 if (!nla)
2863 goto nla_put_failure;
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002864 if (nla_put_u32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY) ||
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002865 nla_put_u32(skb, T_current_state, device->state.i) ||
2866 nla_put_u64(skb, T_ed_uuid, device->ed_uuid) ||
2867 nla_put_u64(skb, T_capacity, drbd_get_capacity(device->this_bdev)) ||
2868 nla_put_u64(skb, T_send_cnt, device->send_cnt) ||
2869 nla_put_u64(skb, T_recv_cnt, device->recv_cnt) ||
2870 nla_put_u64(skb, T_read_cnt, device->read_cnt) ||
2871 nla_put_u64(skb, T_writ_cnt, device->writ_cnt) ||
2872 nla_put_u64(skb, T_al_writ_cnt, device->al_writ_cnt) ||
2873 nla_put_u64(skb, T_bm_writ_cnt, device->bm_writ_cnt) ||
2874 nla_put_u32(skb, T_ap_bio_cnt, atomic_read(&device->ap_bio_cnt)) ||
2875 nla_put_u32(skb, T_ap_pending_cnt, atomic_read(&device->ap_pending_cnt)) ||
2876 nla_put_u32(skb, T_rs_pending_cnt, atomic_read(&device->rs_pending_cnt)))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002877 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002878
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002879 if (got_ldev) {
Philipp Reisner39a1aa7f2012-08-08 21:19:09 +02002880 int err;
2881
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002882 spin_lock_irq(&device->ldev->md.uuid_lock);
2883 err = nla_put(skb, T_uuids, sizeof(si->uuids), device->ldev->md.uuid);
2884 spin_unlock_irq(&device->ldev->md.uuid_lock);
Philipp Reisner39a1aa7f2012-08-08 21:19:09 +02002885
2886 if (err)
2887 goto nla_put_failure;
2888
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002889 if (nla_put_u32(skb, T_disk_flags, device->ldev->md.flags) ||
2890 nla_put_u64(skb, T_bits_total, drbd_bm_bits(device)) ||
2891 nla_put_u64(skb, T_bits_oos, drbd_bm_total_weight(device)))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002892 goto nla_put_failure;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002893 if (C_SYNC_SOURCE <= device->state.conn &&
2894 C_PAUSED_SYNC_T >= device->state.conn) {
2895 if (nla_put_u64(skb, T_bits_rs_total, device->rs_total) ||
2896 nla_put_u64(skb, T_bits_rs_failed, device->rs_failed))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002897 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002898 }
2899 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002900
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002901 if (sib) {
2902 switch(sib->sib_reason) {
2903 case SIB_SYNC_PROGRESS:
2904 case SIB_GET_STATUS_REPLY:
2905 break;
2906 case SIB_STATE_CHANGE:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002907 if (nla_put_u32(skb, T_prev_state, sib->os.i) ||
2908 nla_put_u32(skb, T_new_state, sib->ns.i))
2909 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002910 break;
2911 case SIB_HELPER_POST:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002912 if (nla_put_u32(skb, T_helper_exit_code,
2913 sib->helper_exit_code))
2914 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002915 /* fall through */
2916 case SIB_HELPER_PRE:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002917 if (nla_put_string(skb, T_helper, sib->helper_name))
2918 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002919 break;
2920 }
2921 }
2922 nla_nest_end(skb, nla);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002923
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002924 if (0)
2925nla_put_failure:
2926 err = -EMSGSIZE;
2927 if (got_ldev)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002928 put_ldev(device);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002929 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002930}
2931
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002932int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002933{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002934 enum drbd_ret_code retcode;
2935 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002936
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002937 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2938 if (!adm_ctx.reply_skb)
2939 return retcode;
2940 if (retcode != NO_ERROR)
2941 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002942
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002943 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.device, NULL);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002944 if (err) {
2945 nlmsg_free(adm_ctx.reply_skb);
2946 return err;
2947 }
2948out:
2949 drbd_adm_finish(info, retcode);
2950 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002951}
2952
Rashika Kheria4b7a5302013-12-19 15:17:33 +05302953static int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002954{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002955 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002956 struct drbd_genlmsghdr *dh;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002957 struct drbd_resource *pos = (struct drbd_resource *)cb->args[0];
2958 struct drbd_resource *resource = NULL;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002959 struct drbd_resource *tmp;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002960 unsigned volume = cb->args[1];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002961
Lars Ellenberg543cc102011-03-10 22:18:18 +01002962 /* Open coded, deferred, iteration:
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002963 * for_each_resource_safe(resource, tmp, &drbd_resources) {
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002964 * connection = "first connection of resource or undefined";
2965 * idr_for_each_entry(&resource->devices, device, i) {
Lars Ellenberg543cc102011-03-10 22:18:18 +01002966 * ...
2967 * }
2968 * }
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002969 * where resource is cb->args[0];
Lars Ellenberg543cc102011-03-10 22:18:18 +01002970 * and i is cb->args[1];
2971 *
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002972 * cb->args[2] indicates if we shall loop over all resources,
2973 * or just dump all volumes of a single resource.
2974 *
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002975 * This may miss entries inserted after this dump started,
2976 * or entries deleted before they are reached.
Lars Ellenberg543cc102011-03-10 22:18:18 +01002977 *
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002978 * We need to make sure the device won't disappear while
Lars Ellenberg543cc102011-03-10 22:18:18 +01002979 * we are looking at it, and revalidate our iterators
2980 * on each iteration.
2981 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002982
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02002983 /* synchronize with conn_create()/drbd_destroy_connection() */
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02002984 rcu_read_lock();
Lars Ellenberg543cc102011-03-10 22:18:18 +01002985 /* revalidate iterator position */
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002986 for_each_resource_rcu(tmp, &drbd_resources) {
Lars Ellenberg543cc102011-03-10 22:18:18 +01002987 if (pos == NULL) {
2988 /* first iteration */
2989 pos = tmp;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002990 resource = pos;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002991 break;
2992 }
2993 if (tmp == pos) {
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002994 resource = pos;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002995 break;
2996 }
2997 }
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002998 if (resource) {
2999next_resource:
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02003000 device = idr_get_next(&resource->devices, &volume);
3001 if (!device) {
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003002 /* No more volumes to dump on this resource.
3003 * Advance resource iterator. */
3004 pos = list_entry_rcu(resource->resources.next,
3005 struct drbd_resource, resources);
3006 /* Did we dump any volume of this resource yet? */
Lars Ellenberg543cc102011-03-10 22:18:18 +01003007 if (volume != 0) {
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003008 /* If we reached the end of the list,
3009 * or only a single resource dump was requested,
3010 * we are done. */
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003011 if (&pos->resources == &drbd_resources || cb->args[2])
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003012 goto out;
Lars Ellenberg543cc102011-03-10 22:18:18 +01003013 volume = 0;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003014 resource = pos;
3015 goto next_resource;
Lars Ellenberg543cc102011-03-10 22:18:18 +01003016 }
3017 }
3018
Philipp Reisner98683652012-11-09 14:18:43 +01003019 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003020 cb->nlh->nlmsg_seq, &drbd_genl_family,
3021 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
3022 if (!dh)
Lars Ellenberg543cc102011-03-10 22:18:18 +01003023 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003024
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02003025 if (!device) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003026 /* This is a connection without a single volume.
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003027 * Suprisingly enough, it may have a network
3028 * configuration. */
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02003029 struct drbd_connection *connection;
3030
Lars Ellenberg543cc102011-03-10 22:18:18 +01003031 dh->minor = -1U;
3032 dh->ret_code = NO_ERROR;
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02003033 connection = the_only_connection(resource);
3034 if (nla_put_drbd_cfg_context(skb, resource, connection, NULL))
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003035 goto cancel;
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02003036 if (connection) {
3037 struct net_conf *nc;
3038
3039 nc = rcu_dereference(connection->net_conf);
3040 if (nc && net_conf_to_skb(skb, nc, 1) != 0)
3041 goto cancel;
3042 }
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003043 goto done;
Lars Ellenberg543cc102011-03-10 22:18:18 +01003044 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003045
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +02003046 D_ASSERT(device, device->vnr == volume);
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02003047 D_ASSERT(device, device->resource == resource);
Lars Ellenberg543cc102011-03-10 22:18:18 +01003048
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003049 dh->minor = device_to_minor(device);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003050 dh->ret_code = NO_ERROR;
3051
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003052 if (nla_put_status_info(skb, device, NULL)) {
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003053cancel:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003054 genlmsg_cancel(skb, dh);
Lars Ellenberg543cc102011-03-10 22:18:18 +01003055 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003056 }
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003057done:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003058 genlmsg_end(skb, dh);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003059 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003060
Lars Ellenberg543cc102011-03-10 22:18:18 +01003061out:
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003062 rcu_read_unlock();
Lars Ellenberg543cc102011-03-10 22:18:18 +01003063 /* where to start the next iteration */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003064 cb->args[0] = (long)pos;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003065 cb->args[1] = (pos == resource) ? volume + 1 : 0;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003066
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003067 /* No more resources/volumes/minors found results in an empty skb.
Lars Ellenberg543cc102011-03-10 22:18:18 +01003068 * Which will terminate the dump. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003069 return skb->len;
3070}
3071
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003072/*
3073 * Request status of all resources, or of all volumes within a single resource.
3074 *
3075 * This is a dump, as the answer may not fit in a single reply skb otherwise.
3076 * Which means we cannot use the family->attrbuf or other such members, because
3077 * dump is NOT protected by the genl_lock(). During dump, we only have access
3078 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
3079 *
3080 * Once things are setup properly, we call into get_one_status().
Philipp Reisnerb411b362009-09-25 16:07:19 -07003081 */
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003082int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003083{
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003084 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
3085 struct nlattr *nla;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003086 const char *resource_name;
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003087 struct drbd_resource *resource;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003088 int maxtype;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003089
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003090 /* Is this a followup call? */
3091 if (cb->args[0]) {
3092 /* ... of a single resource dump,
3093 * and the resource iterator has been advanced already? */
3094 if (cb->args[2] && cb->args[2] != cb->args[0])
3095 return 0; /* DONE. */
3096 goto dump;
3097 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003098
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003099 /* First call (from netlink_dump_start). We need to figure out
3100 * which resource(s) the user wants us to dump. */
3101 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
3102 nlmsg_attrlen(cb->nlh, hdrlen),
3103 DRBD_NLA_CFG_CONTEXT);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003104
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003105 /* No explicit context given. Dump all. */
3106 if (!nla)
3107 goto dump;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003108 maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
3109 nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
3110 if (IS_ERR(nla))
3111 return PTR_ERR(nla);
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003112 /* context given, but no name present? */
3113 if (!nla)
3114 return -EINVAL;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003115 resource_name = nla_data(nla);
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003116 if (!*resource_name)
3117 return -ENODEV;
3118 resource = drbd_find_resource(resource_name);
3119 if (!resource)
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003120 return -ENODEV;
3121
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003122 kref_put(&resource->kref, drbd_destroy_resource); /* get_one_status() revalidates the resource */
Philipp Reisner0ace9df2011-04-24 10:53:19 +02003123
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003124 /* prime iterators, and set "filter" mode mark:
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003125 * only dump this connection. */
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003126 cb->args[0] = (long)resource;
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003127 /* cb->args[1] = 0; passed in this way. */
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003128 cb->args[2] = (long)resource;
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003129
3130dump:
3131 return get_one_status(skb, cb);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003132}
3133
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003134int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003135{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003136 enum drbd_ret_code retcode;
3137 struct timeout_parms tp;
3138 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003139
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003140 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3141 if (!adm_ctx.reply_skb)
3142 return retcode;
3143 if (retcode != NO_ERROR)
3144 goto out;
3145
3146 tp.timeout_type =
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003147 adm_ctx.device->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
3148 test_bit(USE_DEGR_WFC_T, &adm_ctx.device->flags) ? UT_DEGRADED :
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003149 UT_DEFAULT;
3150
3151 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
3152 if (err) {
3153 nlmsg_free(adm_ctx.reply_skb);
3154 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003155 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003156out:
3157 drbd_adm_finish(info, retcode);
3158 return 0;
3159}
Lars Ellenberg873b0d52011-01-21 22:53:48 +01003160
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003161int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
3162{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003163 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003164 enum drbd_ret_code retcode;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003165 struct start_ov_parms parms;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003166
3167 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3168 if (!adm_ctx.reply_skb)
3169 return retcode;
3170 if (retcode != NO_ERROR)
3171 goto out;
3172
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003173 device = adm_ctx.device;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003174
3175 /* resume from last known position, if possible */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003176 parms.ov_start_sector = device->ov_start_sector;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003177 parms.ov_stop_sector = ULLONG_MAX;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003178 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01003179 int err = start_ov_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003180 if (err) {
3181 retcode = ERR_MANDATORY_TAG;
3182 drbd_msg_put_info(from_attrs_err_to_txt(err));
3183 goto out;
3184 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003185 }
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003186 /* w_make_ov_request expects position to be aligned */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003187 device->ov_start_sector = parms.ov_start_sector & ~(BM_SECT_PER_BIT-1);
3188 device->ov_stop_sector = parms.ov_stop_sector;
Lars Ellenberg873b0d52011-01-21 22:53:48 +01003189
3190 /* If there is still bitmap IO pending, e.g. previous resync or verify
3191 * just being finished, wait for it before requesting a new resync. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003192 drbd_suspend_io(device);
3193 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
3194 retcode = drbd_request_state(device, NS(conn, C_VERIFY_S));
3195 drbd_resume_io(device);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003196out:
3197 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003198 return 0;
3199}
3200
3201
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003202int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003203{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003204 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003205 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003206 int skip_initial_sync = 0;
3207 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003208 struct new_c_uuid_parms args;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003209
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003210 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3211 if (!adm_ctx.reply_skb)
3212 return retcode;
3213 if (retcode != NO_ERROR)
3214 goto out_nolock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003215
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003216 device = adm_ctx.device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003217 memset(&args, 0, sizeof(args));
3218 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01003219 err = new_c_uuid_parms_from_attrs(&args, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003220 if (err) {
3221 retcode = ERR_MANDATORY_TAG;
3222 drbd_msg_put_info(from_attrs_err_to_txt(err));
3223 goto out_nolock;
3224 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003225 }
3226
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003227 mutex_lock(device->state_mutex); /* Protects us against serialized state changes. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003228
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003229 if (!get_ldev(device)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003230 retcode = ERR_NO_DISK;
3231 goto out;
3232 }
3233
3234 /* this is "skip initial sync", assume to be clean */
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02003235 if (device->state.conn == C_CONNECTED &&
3236 first_peer_device(device)->connection->agreed_pro_version >= 90 &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003237 device->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02003238 drbd_info(device, "Preparing to skip initial sync\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003239 skip_initial_sync = 1;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003240 } else if (device->state.conn != C_STANDALONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003241 retcode = ERR_CONNECTED;
3242 goto out_dec;
3243 }
3244
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003245 drbd_uuid_set(device, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
3246 drbd_uuid_new_current(device); /* New current, previous to UI_BITMAP */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003247
3248 if (args.clear_bm) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003249 err = drbd_bitmap_io(device, &drbd_bmio_clear_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003250 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003251 if (err) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02003252 drbd_err(device, "Writing bitmap failed with %d\n", err);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003253 retcode = ERR_IO_MD_DISK;
3254 }
3255 if (skip_initial_sync) {
Andreas Gruenbacher69a22772011-08-09 00:47:13 +02003256 drbd_send_uuids_skip_initial_sync(first_peer_device(device));
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003257 _drbd_uuid_set(device, UI_BITMAP, 0);
3258 drbd_print_uuids(device, "cleared bitmap UUID");
Andreas Gruenbacher05008132011-07-07 14:19:42 +02003259 spin_lock_irq(&device->resource->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003260 _drbd_set_state(_NS2(device, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
Philipp Reisnerb411b362009-09-25 16:07:19 -07003261 CS_VERBOSE, NULL);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02003262 spin_unlock_irq(&device->resource->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003263 }
3264 }
3265
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003266 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003267out_dec:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003268 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003269out:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003270 mutex_unlock(device->state_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003271out_nolock:
3272 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003273 return 0;
3274}
3275
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003276static enum drbd_ret_code
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003277drbd_check_resource_name(const char *name)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003278{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003279 if (!name || !name[0]) {
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003280 drbd_msg_put_info("resource name missing");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003281 return ERR_MANDATORY_TAG;
3282 }
3283 /* if we want to use these in sysfs/configfs/debugfs some day,
3284 * we must not allow slashes */
3285 if (strchr(name, '/')) {
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003286 drbd_msg_put_info("invalid resource name");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003287 return ERR_INVALID_REQUEST;
3288 }
3289 return NO_ERROR;
3290}
Philipp Reisnerb411b362009-09-25 16:07:19 -07003291
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003292int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003293{
3294 enum drbd_ret_code retcode;
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003295 struct res_opts res_opts;
3296 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003297
3298 retcode = drbd_adm_prepare(skb, info, 0);
3299 if (!adm_ctx.reply_skb)
3300 return retcode;
3301 if (retcode != NO_ERROR)
3302 goto out;
3303
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003304 set_res_opts_defaults(&res_opts);
3305 err = res_opts_from_attrs(&res_opts, info);
3306 if (err && err != -ENOMSG) {
3307 retcode = ERR_MANDATORY_TAG;
3308 drbd_msg_put_info(from_attrs_err_to_txt(err));
3309 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003310 }
3311
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003312 retcode = drbd_check_resource_name(adm_ctx.resource_name);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003313 if (retcode != NO_ERROR)
3314 goto out;
3315
Andreas Gruenbacher5c661042011-07-05 18:26:00 +02003316 if (adm_ctx.resource) {
Lars Ellenberg38f19612011-03-14 13:22:35 +01003317 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
3318 retcode = ERR_INVALID_REQUEST;
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003319 drbd_msg_put_info("resource exists");
Lars Ellenberg38f19612011-03-14 13:22:35 +01003320 }
3321 /* else: still NO_ERROR */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003322 goto out;
Philipp Reisner9f5180e2009-10-06 09:30:14 +02003323 }
3324
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003325 if (!conn_create(adm_ctx.resource_name, &res_opts))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003326 retcode = ERR_NOMEM;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003327out:
3328 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003329 return 0;
3330}
3331
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003332int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003333{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003334 struct drbd_genlmsghdr *dh = info->userhdr;
3335 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003336
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02003337 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003338 if (!adm_ctx.reply_skb)
3339 return retcode;
3340 if (retcode != NO_ERROR)
3341 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003342
Andreas Gruenbacherf2257a52011-07-14 16:00:40 +02003343 if (dh->minor > MINORMASK) {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003344 drbd_msg_put_info("requested minor out of range");
3345 retcode = ERR_INVALID_REQUEST;
3346 goto out;
3347 }
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +02003348 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003349 drbd_msg_put_info("requested volume id out of range");
3350 retcode = ERR_INVALID_REQUEST;
3351 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05003352 }
3353
Lars Ellenberg38f19612011-03-14 13:22:35 +01003354 /* drbd_adm_prepare made sure already
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02003355 * that first_peer_device(device)->connection and device->vnr match the request. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003356 if (adm_ctx.device) {
Lars Ellenberg38f19612011-03-14 13:22:35 +01003357 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
3358 retcode = ERR_MINOR_EXISTS;
3359 /* else: still NO_ERROR */
3360 goto out;
3361 }
3362
Andreas Gruenbacher59515a22011-07-06 14:20:49 +02003363 retcode = drbd_create_device(adm_ctx.resource, dh->minor, adm_ctx.volume);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003364out:
3365 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003366 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003367}
3368
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003369static enum drbd_ret_code adm_del_minor(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003370{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003371 if (device->state.disk == D_DISKLESS &&
3372 /* no need to be device->state.conn == C_STANDALONE &&
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003373 * we may want to delete a minor from a live replication group.
3374 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003375 device->state.role == R_SECONDARY) {
3376 _drbd_request_state(device, NS(conn, C_WF_REPORT_PARAMS),
Philipp Reisner369bea62011-07-06 23:04:44 +02003377 CS_VERBOSE + CS_WAIT_COMPLETE);
Andreas Gruenbacherf82795d2011-07-03 23:32:26 +02003378 drbd_delete_device(device);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003379 return NO_ERROR;
3380 } else
3381 return ERR_MINOR_CONFIGURED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003382}
3383
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003384int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05003385{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003386 enum drbd_ret_code retcode;
3387
3388 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3389 if (!adm_ctx.reply_skb)
3390 return retcode;
3391 if (retcode != NO_ERROR)
3392 goto out;
3393
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003394 retcode = adm_del_minor(adm_ctx.device);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003395out:
3396 drbd_adm_finish(info, retcode);
3397 return 0;
3398}
3399
3400int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
3401{
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003402 struct drbd_resource *resource;
3403 struct drbd_connection *connection;
3404 struct drbd_device *device;
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003405 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003406 unsigned i;
3407
Andreas Gruenbachera10f6b82011-06-09 01:26:16 +02003408 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003409 if (!adm_ctx.reply_skb)
3410 return retcode;
3411 if (retcode != NO_ERROR)
3412 goto out;
3413
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003414 resource = adm_ctx.resource;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003415 /* demote */
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003416 for_each_connection(connection, resource) {
3417 struct drbd_peer_device *peer_device;
3418
3419 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
3420 retcode = drbd_set_role(peer_device->device, R_SECONDARY, 0);
3421 if (retcode < SS_SUCCESS) {
3422 drbd_msg_put_info("failed to demote");
3423 goto out;
3424 }
3425 }
3426
3427 retcode = conn_try_disconnect(connection, 0);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003428 if (retcode < SS_SUCCESS) {
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003429 drbd_msg_put_info("failed to disconnect");
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003430 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003431 }
3432 }
3433
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003434 /* detach */
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003435 idr_for_each_entry(&resource->devices, device, i) {
3436 retcode = adm_detach(device, 0);
Lars Ellenberg27012382012-07-24 10:13:55 +02003437 if (retcode < SS_SUCCESS || retcode > NO_ERROR) {
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003438 drbd_msg_put_info("failed to detach");
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003439 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003440 }
3441 }
3442
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003443 /* If we reach this, all volumes (of this connection) are Secondary,
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003444 * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003445 * actually stopped, state handling only does drbd_thread_stop_nowait(). */
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003446 for_each_connection(connection, resource)
3447 drbd_thread_stop(&connection->worker);
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003448
3449 /* Now, nothing can fail anymore */
3450
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003451 /* delete volumes */
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003452 idr_for_each_entry(&resource->devices, device, i) {
3453 retcode = adm_del_minor(device);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003454 if (retcode != NO_ERROR) {
3455 /* "can not happen" */
3456 drbd_msg_put_info("failed to delete volume");
Philipp Reisneref356262011-04-13 14:21:29 -07003457 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003458 }
3459 }
3460
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003461 list_del_rcu(&resource->resources);
3462 synchronize_rcu();
3463 drbd_free_resource(resource);
3464 retcode = NO_ERROR;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003465
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003466out:
3467 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003468 return 0;
3469}
3470
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003471int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05003472{
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003473 struct drbd_resource *resource;
3474 struct drbd_connection *connection;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003475 enum drbd_ret_code retcode;
3476
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02003477 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003478 if (!adm_ctx.reply_skb)
3479 return retcode;
3480 if (retcode != NO_ERROR)
3481 goto out;
3482
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003483 resource = adm_ctx.resource;
3484 for_each_connection(connection, resource) {
3485 if (connection->cstate > C_STANDALONE) {
3486 retcode = ERR_NET_CONFIGURED;
3487 goto out;
3488 }
3489 }
3490 if (!idr_is_empty(&resource->devices)) {
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003491 retcode = ERR_RES_IN_USE;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003492 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05003493 }
3494
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003495 list_del_rcu(&resource->resources);
3496 for_each_connection(connection, resource)
3497 drbd_thread_stop(&connection->worker);
3498 synchronize_rcu();
3499 drbd_free_resource(resource);
3500 retcode = NO_ERROR;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003501out:
3502 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003503 return 0;
3504}
3505
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003506void drbd_bcast_event(struct drbd_device *device, const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003507{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003508 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
3509 struct sk_buff *msg;
3510 struct drbd_genlmsghdr *d_out;
3511 unsigned seq;
3512 int err = -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003513
Philipp Reisneref86b772012-12-06 10:34:34 +01003514 if (sib->sib_reason == SIB_SYNC_PROGRESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003515 if (time_after(jiffies, device->rs_last_bcast + HZ))
3516 device->rs_last_bcast = jiffies;
Philipp Reisneref86b772012-12-06 10:34:34 +01003517 else
3518 return;
3519 }
Philipp Reisner328e0f122012-10-19 14:37:47 +02003520
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003521 seq = atomic_inc_return(&drbd_genl_seq);
3522 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
3523 if (!msg)
3524 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003525
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003526 err = -EMSGSIZE;
3527 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
3528 if (!d_out) /* cannot happen, but anyways. */
3529 goto nla_put_failure;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003530 d_out->minor = device_to_minor(device);
Andreas Gruenbacher6f9b5f82011-05-06 01:03:32 +02003531 d_out->ret_code = NO_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003532
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003533 if (nla_put_status_info(msg, device, sib))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003534 goto nla_put_failure;
3535 genlmsg_end(msg, d_out);
3536 err = drbd_genl_multicast_events(msg, 0);
3537 /* msg has been consumed or freed in netlink_broadcast() */
3538 if (err && err != -ESRCH)
3539 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003540
Philipp Reisnerb411b362009-09-25 16:07:19 -07003541 return;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003542
3543nla_put_failure:
3544 nlmsg_free(msg);
3545failed:
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02003546 drbd_err(device, "Error %d while broadcasting event. "
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003547 "Event seq:%u sib_reason:%u\n",
3548 err, seq, sib->sib_reason);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003549}