blob: 8ebcf88b0c1b01566622d81e4d93e79119bc60e1 [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);
219 if (adm_ctx.resource) {
220 adm_ctx.connection = first_connection(adm_ctx.resource);
221 kref_get(&adm_ctx.connection->kref);
222 }
223 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100224
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200225 if (!adm_ctx.device && (flags & DRBD_ADM_NEED_MINOR)) {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100226 drbd_msg_put_info("unknown minor");
227 return ERR_MINOR_INVALID;
228 }
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +0200229 if (!adm_ctx.resource && (flags & DRBD_ADM_NEED_RESOURCE)) {
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +0200230 drbd_msg_put_info("unknown resource");
Andreas Gruenbachera10f6b82011-06-09 01:26:16 +0200231 if (adm_ctx.resource_name)
232 return ERR_RES_NOT_KNOWN;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100233 return ERR_INVALID_REQUEST;
234 }
235
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200236 if (flags & DRBD_ADM_NEED_CONNECTION) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200237 if (adm_ctx.connection && !(flags & DRBD_ADM_NEED_RESOURCE)) {
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200238 drbd_msg_put_info("no resource name expected");
239 return ERR_INVALID_REQUEST;
240 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200241 if (adm_ctx.device) {
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200242 drbd_msg_put_info("no minor number expected");
243 return ERR_INVALID_REQUEST;
244 }
245 if (adm_ctx.my_addr && adm_ctx.peer_addr)
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200246 adm_ctx.connection = conn_get_by_addrs(nla_data(adm_ctx.my_addr),
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200247 nla_len(adm_ctx.my_addr),
248 nla_data(adm_ctx.peer_addr),
249 nla_len(adm_ctx.peer_addr));
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200250 if (!adm_ctx.connection) {
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200251 drbd_msg_put_info("unknown connection");
252 return ERR_INVALID_REQUEST;
253 }
254 }
255
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100256 /* some more paranoia, if the request was over-determined */
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +0200257 if (adm_ctx.device && adm_ctx.resource &&
258 adm_ctx.device->resource != adm_ctx.resource) {
259 pr_warning("request: minor=%u, resource=%s; but that minor belongs to resource %s\n",
260 adm_ctx.minor, adm_ctx.resource->name,
Andreas Gruenbacherd8628a82011-06-09 01:38:00 +0200261 adm_ctx.device->resource->name);
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +0200262 drbd_msg_put_info("minor exists in different resource");
Lars Ellenberg527f4b22011-03-14 13:58:03 +0100263 return ERR_INVALID_REQUEST;
264 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200265 if (adm_ctx.device &&
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100266 adm_ctx.volume != VOLUME_UNSPECIFIED &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200267 adm_ctx.volume != adm_ctx.device->vnr) {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100268 pr_warning("request: minor=%u, volume=%u; but that minor is volume %u in %s\n",
269 adm_ctx.minor, adm_ctx.volume,
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +0200270 adm_ctx.device->vnr,
Andreas Gruenbacherd8628a82011-06-09 01:38:00 +0200271 adm_ctx.device->resource->name);
Lars Ellenberg527f4b22011-03-14 13:58:03 +0100272 drbd_msg_put_info("minor exists as different volume");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100273 return ERR_INVALID_REQUEST;
274 }
Philipp Reisner0ace9df2011-04-24 10:53:19 +0200275
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100276 return NO_ERROR;
277
278fail:
279 nlmsg_free(adm_ctx.reply_skb);
280 adm_ctx.reply_skb = NULL;
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200281 return err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100282}
283
284static int drbd_adm_finish(struct genl_info *info, int retcode)
285{
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200286 if (adm_ctx.connection) {
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +0200287 kref_put(&adm_ctx.connection->kref, drbd_destroy_connection);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200288 adm_ctx.connection = NULL;
Philipp Reisner0ace9df2011-04-24 10:53:19 +0200289 }
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +0200290 if (adm_ctx.resource) {
291 kref_put(&adm_ctx.resource->kref, drbd_destroy_resource);
292 adm_ctx.resource = NULL;
293 }
Philipp Reisner0ace9df2011-04-24 10:53:19 +0200294
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100295 if (!adm_ctx.reply_skb)
296 return -ENOMEM;
297
298 adm_ctx.reply_dh->ret_code = retcode;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100299 drbd_adm_send_reply(adm_ctx.reply_skb, info);
300 return 0;
301}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700302
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200303static void setup_khelper_env(struct drbd_connection *connection, char **envp)
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100304{
305 char *afs;
306
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200307 /* FIXME: A future version will not allow this case. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200308 if (connection->my_addr_len == 0 || connection->peer_addr_len == 0)
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200309 return;
310
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200311 switch (((struct sockaddr *)&connection->peer_addr)->sa_family) {
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200312 case AF_INET6:
313 afs = "ipv6";
314 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200315 &((struct sockaddr_in6 *)&connection->peer_addr)->sin6_addr);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200316 break;
317 case AF_INET:
318 afs = "ipv4";
319 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200320 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200321 break;
322 default:
323 afs = "ssocks";
324 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200325 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100326 }
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200327 snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100328}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700329
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200330int drbd_khelper(struct drbd_device *device, char *cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700331{
332 char *envp[] = { "HOME=/",
333 "TERM=linux",
334 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100335 (char[20]) { }, /* address family */
336 (char[60]) { }, /* address */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700337 NULL };
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100338 char mb[12];
Philipp Reisnerb411b362009-09-25 16:07:19 -0700339 char *argv[] = {usermode_helper, cmd, mb, NULL };
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200340 struct drbd_connection *connection = first_peer_device(device)->connection;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100341 struct sib_info sib;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700342 int ret;
343
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200344 if (current == connection->worker.task)
345 set_bit(CALLBACK_PENDING, &connection->flags);
Lars Ellenbergc2ba6862012-06-14 15:14:06 +0200346
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200347 snprintf(mb, 12, "minor-%d", device_to_minor(device));
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200348 setup_khelper_env(connection, envp);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700349
Lars Ellenberg1090c052010-07-19 17:41:04 +0200350 /* The helper may take some time.
351 * write out any unsynced meta data changes now */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200352 drbd_md_sync(device);
Lars Ellenberg1090c052010-07-19 17:41:04 +0200353
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200354 drbd_info(device, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100355 sib.sib_reason = SIB_HELPER_PRE;
356 sib.helper_name = cmd;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200357 drbd_bcast_event(device, &sib);
Oleg Nesterov70834d32012-03-23 15:02:46 -0700358 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700359 if (ret)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200360 drbd_warn(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);
363 else
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200364 drbd_info(device, "helper command: %s %s %s exit code %u (0x%x)\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -0700365 usermode_helper, cmd, mb,
366 (ret >> 8) & 0xff, ret);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100367 sib.sib_reason = SIB_HELPER_POST;
368 sib.helper_exit_code = ret;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200369 drbd_bcast_event(device, &sib);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700370
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200371 if (current == connection->worker.task)
372 clear_bit(CALLBACK_PENDING, &connection->flags);
Lars Ellenbergc2ba6862012-06-14 15:14:06 +0200373
Philipp Reisnerb411b362009-09-25 16:07:19 -0700374 if (ret < 0) /* Ignore any ERRNOs we got. */
375 ret = 0;
376
377 return ret;
378}
379
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200380static int conn_khelper(struct drbd_connection *connection, char *cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700381{
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100382 char *envp[] = { "HOME=/",
383 "TERM=linux",
384 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
385 (char[20]) { }, /* address family */
386 (char[60]) { }, /* address */
387 NULL };
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +0200388 char *resource_name = connection->resource->name;
389 char *argv[] = {usermode_helper, cmd, resource_name, NULL };
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100390 int ret;
391
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200392 setup_khelper_env(connection, envp);
393 conn_md_sync(connection);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100394
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200395 drbd_info(connection, "helper command: %s %s %s\n", usermode_helper, cmd, resource_name);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100396 /* TODO: conn_bcast_event() ?? */
397
Philipp Reisner98683652012-11-09 14:18:43 +0100398 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100399 if (ret)
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200400 drbd_warn(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 else
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200404 drbd_info(connection, "helper command: %s %s %s exit code %u (0x%x)\n",
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +0200405 usermode_helper, cmd, resource_name,
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100406 (ret >> 8) & 0xff, ret);
407 /* TODO: conn_bcast_event() ?? */
408
409 if (ret < 0) /* Ignore any ERRNOs we got. */
410 ret = 0;
411
412 return ret;
413}
414
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200415static enum drbd_fencing_p highest_fencing_policy(struct drbd_connection *connection)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700416{
Philipp Reisnercb703452011-03-24 11:03:07 +0100417 enum drbd_fencing_p fp = FP_NOT_AVAIL;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200418 struct drbd_peer_device *peer_device;
Philipp Reisnercb703452011-03-24 11:03:07 +0100419 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700420
Philipp Reisner695d08f2011-04-11 22:53:32 -0700421 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200422 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
423 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200424 if (get_ldev_if_state(device, D_CONSISTENT)) {
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200425 struct disk_conf *disk_conf =
426 rcu_dereference(peer_device->device->ldev->disk_conf);
427 fp = max_t(enum drbd_fencing_p, fp, disk_conf->fencing);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200428 put_ldev(device);
Philipp Reisnercb703452011-03-24 11:03:07 +0100429 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700430 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700431 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700432
Philipp Reisnercb703452011-03-24 11:03:07 +0100433 return fp;
434}
435
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200436bool conn_try_outdate_peer(struct drbd_connection *connection)
Philipp Reisnercb703452011-03-24 11:03:07 +0100437{
Philipp Reisner28e448b2013-06-25 16:50:06 +0200438 unsigned int connect_cnt;
Philipp Reisnercb703452011-03-24 11:03:07 +0100439 union drbd_state mask = { };
440 union drbd_state val = { };
441 enum drbd_fencing_p fp;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700442 char *ex_to_string;
443 int r;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700444
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200445 if (connection->cstate >= C_WF_REPORT_PARAMS) {
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200446 drbd_err(connection, "Expected cstate < C_WF_REPORT_PARAMS\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100447 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700448 }
449
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200450 spin_lock_irq(&connection->req_lock);
451 connect_cnt = connection->connect_cnt;
452 spin_unlock_irq(&connection->req_lock);
Philipp Reisner28e448b2013-06-25 16:50:06 +0200453
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200454 fp = highest_fencing_policy(connection);
Philipp Reisnercb703452011-03-24 11:03:07 +0100455 switch (fp) {
456 case FP_NOT_AVAIL:
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200457 drbd_warn(connection, "Not fencing peer, I'm not even Consistent myself.\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100458 goto out;
459 case FP_DONT_CARE:
460 return true;
461 default: ;
462 }
463
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200464 r = conn_khelper(connection, "fence-peer");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700465
466 switch ((r>>8) & 0xff) {
467 case 3: /* peer is inconsistent */
468 ex_to_string = "peer is inconsistent or worse";
Philipp Reisnercb703452011-03-24 11:03:07 +0100469 mask.pdsk = D_MASK;
470 val.pdsk = D_INCONSISTENT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700471 break;
472 case 4: /* peer got outdated, or was already outdated */
473 ex_to_string = "peer was fenced";
Philipp Reisnercb703452011-03-24 11:03:07 +0100474 mask.pdsk = D_MASK;
475 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700476 break;
477 case 5: /* peer was down */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200478 if (conn_highest_disk(connection) == D_UP_TO_DATE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700479 /* we will(have) create(d) a new UUID anyways... */
480 ex_to_string = "peer is unreachable, assumed to be dead";
Philipp Reisnercb703452011-03-24 11:03:07 +0100481 mask.pdsk = D_MASK;
482 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700483 } else {
484 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
Philipp Reisnerb411b362009-09-25 16:07:19 -0700485 }
486 break;
487 case 6: /* Peer is primary, voluntarily outdate myself.
488 * This is useful when an unconnected R_SECONDARY is asked to
489 * become R_PRIMARY, but finds the other peer being active. */
490 ex_to_string = "peer is active";
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200491 drbd_warn(connection, "Peer is primary, outdating myself.\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100492 mask.disk = D_MASK;
493 val.disk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700494 break;
495 case 7:
496 if (fp != FP_STONITH)
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200497 drbd_err(connection, "fence-peer() = 7 && fencing != Stonith !!!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700498 ex_to_string = "peer was stonithed";
Philipp Reisnercb703452011-03-24 11:03:07 +0100499 mask.pdsk = D_MASK;
500 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700501 break;
502 default:
503 /* The script is broken ... */
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200504 drbd_err(connection, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
Philipp Reisnercb703452011-03-24 11:03:07 +0100505 return false; /* Eventually leave IO frozen */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700506 }
507
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200508 drbd_info(connection, "fence-peer helper returned %d (%s)\n",
Philipp Reisnercb703452011-03-24 11:03:07 +0100509 (r>>8) & 0xff, ex_to_string);
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200510
Philipp Reisnercb703452011-03-24 11:03:07 +0100511 out:
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200512
Philipp Reisnercb703452011-03-24 11:03:07 +0100513 /* Not using
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200514 conn_request_state(connection, mask, val, CS_VERBOSE);
Philipp Reisnercb703452011-03-24 11:03:07 +0100515 here, because we might were able to re-establish the connection in the
516 meantime. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200517 spin_lock_irq(&connection->req_lock);
518 if (connection->cstate < C_WF_REPORT_PARAMS && !test_bit(STATE_SENT, &connection->flags)) {
519 if (connection->connect_cnt != connect_cnt)
Philipp Reisner28e448b2013-06-25 16:50:06 +0200520 /* In case the connection was established and droped
521 while the fence-peer handler was running, ignore it */
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200522 drbd_info(connection, "Ignoring fence-peer exit code\n");
Philipp Reisner28e448b2013-06-25 16:50:06 +0200523 else
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200524 _conn_request_state(connection, mask, val, CS_VERBOSE);
Philipp Reisner28e448b2013-06-25 16:50:06 +0200525 }
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200526 spin_unlock_irq(&connection->req_lock);
Philipp Reisnercb703452011-03-24 11:03:07 +0100527
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200528 return conn_highest_pdsk(connection) <= D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700529}
530
Philipp Reisner87f7be42010-06-11 13:56:33 +0200531static int _try_outdate_peer_async(void *data)
532{
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200533 struct drbd_connection *connection = (struct drbd_connection *)data;
Philipp Reisner87f7be42010-06-11 13:56:33 +0200534
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200535 conn_try_outdate_peer(connection);
Philipp Reisner21423fa2011-05-17 14:19:41 +0200536
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +0200537 kref_put(&connection->kref, drbd_destroy_connection);
Philipp Reisner87f7be42010-06-11 13:56:33 +0200538 return 0;
539}
540
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200541void conn_try_outdate_peer_async(struct drbd_connection *connection)
Philipp Reisner87f7be42010-06-11 13:56:33 +0200542{
543 struct task_struct *opa;
544
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200545 kref_get(&connection->kref);
546 opa = kthread_run(_try_outdate_peer_async, connection, "drbd_async_h");
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200547 if (IS_ERR(opa)) {
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200548 drbd_err(connection, "out of mem, failed to invoke fence-peer helper\n");
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +0200549 kref_put(&connection->kref, drbd_destroy_connection);
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200550 }
Philipp Reisner87f7be42010-06-11 13:56:33 +0200551}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700552
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100553enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200554drbd_set_role(struct drbd_device *device, enum drbd_role new_role, int force)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700555{
556 const int max_tries = 4;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100557 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200558 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700559 int try = 0;
560 int forced = 0;
561 union drbd_state mask, val;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700562
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +0200563 if (new_role == R_PRIMARY) {
564 struct drbd_connection *connection;
565
566 /* Detect dead peers as soon as possible. */
567
568 rcu_read_lock();
569 for_each_connection(connection, device->resource)
570 request_ping(connection);
571 rcu_read_unlock();
572 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700573
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200574 mutex_lock(device->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700575
576 mask.i = 0; mask.role = R_MASK;
577 val.i = 0; val.role = new_role;
578
579 while (try++ < max_tries) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200580 rv = _drbd_request_state(device, mask, val, CS_WAIT_COMPLETE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700581
582 /* in case we first succeeded to outdate,
583 * but now suddenly could establish a connection */
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100584 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700585 val.pdsk = 0;
586 mask.pdsk = 0;
587 continue;
588 }
589
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100590 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200591 (device->state.disk < D_UP_TO_DATE &&
592 device->state.disk >= D_INCONSISTENT)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700593 mask.disk = D_MASK;
594 val.disk = D_UP_TO_DATE;
595 forced = 1;
596 continue;
597 }
598
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100599 if (rv == SS_NO_UP_TO_DATE_DISK &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200600 device->state.disk == D_CONSISTENT && mask.pdsk == 0) {
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +0200601 D_ASSERT(device, device->state.pdsk == D_UNKNOWN);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700602
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200603 if (conn_try_outdate_peer(first_peer_device(device)->connection)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700604 val.disk = D_UP_TO_DATE;
605 mask.disk = D_MASK;
606 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700607 continue;
608 }
609
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100610 if (rv == SS_NOTHING_TO_DO)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100611 goto out;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100612 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200613 if (!conn_try_outdate_peer(first_peer_device(device)->connection) && force) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200614 drbd_warn(device, "Forced into split brain situation!\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100615 mask.pdsk = D_MASK;
616 val.pdsk = D_OUTDATED;
617
Philipp Reisnerb411b362009-09-25 16:07:19 -0700618 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700619 continue;
620 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100621 if (rv == SS_TWO_PRIMARIES) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700622 /* Maybe the peer is detected as dead very soon...
623 retry at most once more in this case. */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200624 int timeo;
625 rcu_read_lock();
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200626 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +0200627 timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
628 rcu_read_unlock();
629 schedule_timeout_interruptible(timeo);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700630 if (try < max_tries)
631 try = max_tries - 1;
632 continue;
633 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100634 if (rv < SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200635 rv = _drbd_request_state(device, mask, val,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700636 CS_VERBOSE + CS_WAIT_COMPLETE);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100637 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100638 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700639 }
640 break;
641 }
642
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100643 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100644 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700645
646 if (forced)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200647 drbd_warn(device, "Forced to consider local data as UpToDate!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700648
649 /* Wait until nothing is on the fly :) */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200650 wait_event(device->misc_wait, atomic_read(&device->ap_pending_cnt) == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700651
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100652 /* FIXME also wait for all pending P_BARRIER_ACK? */
653
Philipp Reisnerb411b362009-09-25 16:07:19 -0700654 if (new_role == R_SECONDARY) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200655 set_disk_ro(device->vdisk, true);
656 if (get_ldev(device)) {
657 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
658 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700659 }
660 } else {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200661 mutex_lock(&first_peer_device(device)->connection->conf_update);
662 nc = first_peer_device(device)->connection->net_conf;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200663 if (nc)
Andreas Gruenbacher6139f602011-05-06 20:00:02 +0200664 nc->discard_my_data = 0; /* without copy; single bit op is atomic */
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200665 mutex_unlock(&first_peer_device(device)->connection->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +0200666
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200667 set_disk_ro(device->vdisk, false);
668 if (get_ldev(device)) {
669 if (((device->state.conn < C_CONNECTED ||
670 device->state.pdsk <= D_FAILED)
671 && device->ldev->md.uuid[UI_BITMAP] == 0) || forced)
672 drbd_uuid_new_current(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700673
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200674 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
675 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700676 }
677 }
678
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100679 /* writeout of activity log covered areas of the bitmap
680 * to stable storage done in after state change already */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700681
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200682 if (device->state.conn >= C_WF_REPORT_PARAMS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700683 /* if this was forced, we should consider sync */
684 if (forced)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200685 drbd_send_uuids(device);
686 drbd_send_current_state(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700687 }
688
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200689 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700690
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200691 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100692out:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200693 mutex_unlock(device->state_mutex);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100694 return rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700695}
696
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100697static const char *from_attrs_err_to_txt(int err)
Lars Ellenbergef50a3e2010-09-01 14:39:30 +0200698{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100699 return err == -ENOMSG ? "required attribute missing" :
700 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
Lars Ellenbergf3990022011-03-23 14:31:09 +0100701 err == -EEXIST ? "can not change invariant setting" :
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100702 "invalid attribute value";
Lars Ellenbergef50a3e2010-09-01 14:39:30 +0200703}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700704
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100705int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700706{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100707 struct set_role_parms parms;
708 int err;
709 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700710
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100711 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
712 if (!adm_ctx.reply_skb)
713 return retcode;
714 if (retcode != NO_ERROR)
715 goto out;
716
717 memset(&parms, 0, sizeof(parms));
718 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +0100719 err = set_role_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100720 if (err) {
721 retcode = ERR_MANDATORY_TAG;
722 drbd_msg_put_info(from_attrs_err_to_txt(err));
723 goto out;
724 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700725 }
726
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100727 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200728 retcode = drbd_set_role(adm_ctx.device, R_PRIMARY, parms.assume_uptodate);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100729 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200730 retcode = drbd_set_role(adm_ctx.device, R_SECONDARY, 0);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100731out:
732 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700733 return 0;
734}
735
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100736/* Initializes the md.*_offset members, so we are able to find
737 * the on disk meta data.
738 *
739 * We currently have two possible layouts:
740 * external:
741 * |----------- md_size_sect ------------------|
742 * [ 4k superblock ][ activity log ][ Bitmap ]
743 * | al_offset == 8 |
744 * | bm_offset = al_offset + X |
745 * ==> bitmap sectors = md_size_sect - bm_offset
746 *
747 * internal:
748 * |----------- md_size_sect ------------------|
749 * [data.....][ Bitmap ][ activity log ][ 4k superblock ]
750 * | al_offset < 0 |
751 * | bm_offset = al_offset - Y |
752 * ==> bitmap sectors = Y = al_offset - bm_offset
753 *
754 * Activity log size used to be fixed 32kB,
755 * but is about to become configurable.
756 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200757static void drbd_md_set_sector_offsets(struct drbd_device *device,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700758 struct drbd_backing_dev *bdev)
759{
760 sector_t md_size_sect = 0;
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +0100761 unsigned int al_size_sect = bdev->md.al_size_4k * 8;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200762
Lars Ellenberg3a4d4eb2013-03-19 18:16:44 +0100763 bdev->md.md_offset = drbd_md_ss(bdev);
764
Lars Ellenberg68e41a42013-03-19 18:16:45 +0100765 switch (bdev->md.meta_dev_idx) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700766 default:
767 /* v07 style fixed size indexed meta data */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100768 bdev->md.md_size_sect = MD_128MB_SECT;
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100769 bdev->md.al_offset = MD_4kB_SECT;
770 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700771 break;
772 case DRBD_MD_INDEX_FLEX_EXT:
773 /* just occupy the full device; unit: sectors */
774 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100775 bdev->md.al_offset = MD_4kB_SECT;
776 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700777 break;
778 case DRBD_MD_INDEX_INTERNAL:
779 case DRBD_MD_INDEX_FLEX_INT:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700780 /* al size is still fixed */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100781 bdev->md.al_offset = -al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700782 /* we need (slightly less than) ~ this much bitmap sectors: */
783 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
784 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
785 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
786 md_size_sect = ALIGN(md_size_sect, 8);
787
788 /* plus the "drbd meta data super block",
789 * and the activity log; */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100790 md_size_sect += MD_4kB_SECT + al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700791
792 bdev->md.md_size_sect = md_size_sect;
793 /* bitmap offset is adjusted by 'super' block size */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100794 bdev->md.bm_offset = -md_size_sect + MD_4kB_SECT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700795 break;
796 }
797}
798
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100799/* input size is expected to be in KB */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700800char *ppsize(char *buf, unsigned long long size)
801{
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100802 /* Needs 9 bytes at max including trailing NUL:
803 * -1ULL ==> "16384 EB" */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700804 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
805 int base = 0;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100806 while (size >= 10000 && base < sizeof(units)-1) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700807 /* shift + round */
808 size = (size >> 10) + !!(size & (1<<9));
809 base++;
810 }
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100811 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700812
813 return buf;
814}
815
816/* there is still a theoretical deadlock when called from receiver
817 * on an D_INCONSISTENT R_PRIMARY:
818 * remote READ does inc_ap_bio, receiver would need to receive answer
819 * packet from remote to dec_ap_bio again.
820 * receiver receive_sizes(), comes here,
821 * waits for ap_bio_cnt == 0. -> deadlock.
822 * but this cannot happen, actually, because:
823 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
824 * (not connected, or bad/no disk on peer):
825 * see drbd_fail_request_early, ap_bio_cnt is zero.
826 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
827 * peer may not initiate a resize.
828 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100829/* Note these are not to be confused with
830 * drbd_adm_suspend_io/drbd_adm_resume_io,
831 * which are (sub) state changes triggered by admin (drbdsetup),
832 * and can be long lived.
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200833 * This changes an device->flag, is triggered by drbd internals,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100834 * and should be short-lived. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200835void drbd_suspend_io(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700836{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200837 set_bit(SUSPEND_IO, &device->flags);
838 if (drbd_suspended(device))
Philipp Reisner265be2d2010-05-31 10:14:17 +0200839 return;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200840 wait_event(device->misc_wait, !atomic_read(&device->ap_bio_cnt));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700841}
842
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200843void drbd_resume_io(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700844{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200845 clear_bit(SUSPEND_IO, &device->flags);
846 wake_up(&device->misc_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700847}
848
849/**
850 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200851 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700852 *
853 * Returns 0 on success, negative return values indicate errors.
854 * You should call drbd_md_sync() after calling this function.
855 */
Philipp Reisnerd752b262013-06-25 16:50:08 +0200856enum determine_dev_size
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200857drbd_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 -0700858{
859 sector_t prev_first_sect, prev_size; /* previous meta location */
Lars Ellenbergcccac982013-03-19 18:16:46 +0100860 sector_t la_size_sect, u_size;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200861 struct drbd_md *md = &device->ldev->md;
Philipp Reisnerd752b262013-06-25 16:50:08 +0200862 u32 prev_al_stripe_size_4k;
863 u32 prev_al_stripes;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700864 sector_t size;
865 char ppb[10];
Philipp Reisnerd752b262013-06-25 16:50:08 +0200866 void *buffer;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700867
868 int md_moved, la_size_changed;
Philipp Reisnere96c9632013-06-25 16:50:07 +0200869 enum determine_dev_size rv = DS_UNCHANGED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700870
871 /* race:
872 * application request passes inc_ap_bio,
873 * but then cannot get an AL-reference.
874 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
875 *
876 * to avoid that:
877 * Suspend IO right here.
878 * still lock the act_log to not trigger ASSERTs there.
879 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200880 drbd_suspend_io(device);
881 buffer = drbd_md_get_buffer(device); /* Lock meta-data IO */
Philipp Reisnerd752b262013-06-25 16:50:08 +0200882 if (!buffer) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200883 drbd_resume_io(device);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200884 return DS_ERROR;
885 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700886
887 /* no wait necessary anymore, actually we could assert that */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200888 wait_event(device->al_wait, lc_try_lock(device->act_log));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700889
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200890 prev_first_sect = drbd_md_first_sector(device->ldev);
891 prev_size = device->ldev->md.md_size_sect;
892 la_size_sect = device->ldev->md.la_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700893
Philipp Reisnerd752b262013-06-25 16:50:08 +0200894 if (rs) {
895 /* rs is non NULL if we should change the AL layout only */
896
897 prev_al_stripes = md->al_stripes;
898 prev_al_stripe_size_4k = md->al_stripe_size_4k;
899
900 md->al_stripes = rs->al_stripes;
901 md->al_stripe_size_4k = rs->al_stripe_size / 4;
902 md->al_size_4k = (u64)rs->al_stripes * rs->al_stripe_size / 4;
903 }
904
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200905 drbd_md_set_sector_offsets(device, device->ldev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700906
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200907 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200908 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200909 rcu_read_unlock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200910 size = drbd_new_dev_size(device, device->ldev, u_size, flags & DDSF_FORCED);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700911
Philipp Reisnerd752b262013-06-25 16:50:08 +0200912 if (size < la_size_sect) {
913 if (rs && u_size == 0) {
914 /* Remove "rs &&" later. This check should always be active, but
915 right now the receiver expects the permissive behavior */
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200916 drbd_warn(device, "Implicit shrink not allowed. "
Philipp Reisnerd752b262013-06-25 16:50:08 +0200917 "Use --size=%llus for explicit shrink.\n",
918 (unsigned long long)size);
919 rv = DS_ERROR_SHRINK;
920 }
921 if (u_size > size)
922 rv = DS_ERROR_SPACE_MD;
923 if (rv != DS_UNCHANGED)
924 goto err_out;
925 }
926
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200927 if (drbd_get_capacity(device->this_bdev) != size ||
928 drbd_bm_capacity(device) != size) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700929 int err;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200930 err = drbd_bm_resize(device, size, !(flags & DDSF_NO_RESYNC));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700931 if (unlikely(err)) {
932 /* currently there is only one error: ENOMEM! */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200933 size = drbd_bm_capacity(device)>>1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700934 if (size == 0) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200935 drbd_err(device, "OUT OF MEMORY! "
Philipp Reisnerb411b362009-09-25 16:07:19 -0700936 "Could not allocate bitmap!\n");
937 } else {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200938 drbd_err(device, "BM resizing failed. "
Philipp Reisnerb411b362009-09-25 16:07:19 -0700939 "Leaving size unchanged at size = %lu KB\n",
940 (unsigned long)size);
941 }
Philipp Reisnere96c9632013-06-25 16:50:07 +0200942 rv = DS_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700943 }
944 /* racy, see comments above. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200945 drbd_set_my_capacity(device, size);
946 device->ldev->md.la_size_sect = size;
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200947 drbd_info(device, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
Philipp Reisnerb411b362009-09-25 16:07:19 -0700948 (unsigned long long)size>>1);
949 }
Philipp Reisnerd752b262013-06-25 16:50:08 +0200950 if (rv <= DS_ERROR)
951 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700952
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200953 la_size_changed = (la_size_sect != device->ldev->md.la_size_sect);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700954
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200955 md_moved = prev_first_sect != drbd_md_first_sector(device->ldev)
956 || prev_size != device->ldev->md.md_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700957
Philipp Reisnerd752b262013-06-25 16:50:08 +0200958 if (la_size_changed || md_moved || rs) {
959 u32 prev_flags;
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100960
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200961 drbd_al_shrink(device); /* All extents inactive. */
Philipp Reisnerd752b262013-06-25 16:50:08 +0200962
963 prev_flags = md->flags;
964 md->flags &= ~MDF_PRIMARY_IND;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200965 drbd_md_write(device, buffer);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200966
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200967 drbd_info(device, "Writing the whole bitmap, %s\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -0700968 la_size_changed && md_moved ? "size changed and md moved" :
969 la_size_changed ? "size changed" : "md moved");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100970 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200971 drbd_bitmap_io(device, md_moved ? &drbd_bm_write_all : &drbd_bm_write,
Philipp Reisnerd752b262013-06-25 16:50:08 +0200972 "size changed", BM_LOCKED_MASK);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200973 drbd_initialize_al(device, buffer);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200974
975 md->flags = prev_flags;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200976 drbd_md_write(device, buffer);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200977
978 if (rs)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200979 drbd_info(device, "Changed AL layout to al-stripes = %d, al-stripe-size-kB = %d\n",
980 md->al_stripes, md->al_stripe_size_4k * 4);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700981 }
982
Lars Ellenbergcccac982013-03-19 18:16:46 +0100983 if (size > la_size_sect)
Philipp Reisner57737ad2013-10-23 10:59:17 +0200984 rv = la_size_sect ? DS_GREW : DS_GREW_FROM_ZERO;
Lars Ellenbergcccac982013-03-19 18:16:46 +0100985 if (size < la_size_sect)
Philipp Reisnere96c9632013-06-25 16:50:07 +0200986 rv = DS_SHRUNK;
Philipp Reisnerd752b262013-06-25 16:50:08 +0200987
988 if (0) {
989 err_out:
990 if (rs) {
991 md->al_stripes = prev_al_stripes;
992 md->al_stripe_size_4k = prev_al_stripe_size_4k;
993 md->al_size_4k = (u64)prev_al_stripes * prev_al_stripe_size_4k;
994
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200995 drbd_md_set_sector_offsets(device, device->ldev);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200996 }
997 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200998 lc_unlock(device->act_log);
999 wake_up(&device->al_wait);
1000 drbd_md_put_buffer(device);
1001 drbd_resume_io(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001002
1003 return rv;
1004}
1005
1006sector_t
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001007drbd_new_dev_size(struct drbd_device *device, struct drbd_backing_dev *bdev,
Philipp Reisneref5e44a2011-05-03 13:27:43 +02001008 sector_t u_size, int assume_peer_has_space)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001009{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001010 sector_t p_size = device->p_size; /* partner's disk size. */
Lars Ellenbergcccac982013-03-19 18:16:46 +01001011 sector_t la_size_sect = bdev->md.la_size_sect; /* last agreed size. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001012 sector_t m_size; /* my size */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001013 sector_t size = 0;
1014
1015 m_size = drbd_get_max_capacity(bdev);
1016
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001017 if (device->state.conn < C_CONNECTED && assume_peer_has_space) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001018 drbd_warn(device, "Resize while not connected was forced by the user!\n");
Philipp Reisnera393db62009-12-22 13:35:52 +01001019 p_size = m_size;
1020 }
1021
Philipp Reisnerb411b362009-09-25 16:07:19 -07001022 if (p_size && m_size) {
1023 size = min_t(sector_t, p_size, m_size);
1024 } else {
Lars Ellenbergcccac982013-03-19 18:16:46 +01001025 if (la_size_sect) {
1026 size = la_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001027 if (m_size && m_size < size)
1028 size = m_size;
1029 if (p_size && p_size < size)
1030 size = p_size;
1031 } else {
1032 if (m_size)
1033 size = m_size;
1034 if (p_size)
1035 size = p_size;
1036 }
1037 }
1038
1039 if (size == 0)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001040 drbd_err(device, "Both nodes diskless!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001041
1042 if (u_size) {
1043 if (u_size > size)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001044 drbd_err(device, "Requested disk size is too big (%lu > %lu)\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001045 (unsigned long)u_size>>1, (unsigned long)size>>1);
1046 else
1047 size = u_size;
1048 }
1049
1050 return size;
1051}
1052
1053/**
1054 * drbd_check_al_size() - Ensures that the AL is of the right size
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001055 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001056 *
1057 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
1058 * failed, and 0 on success. You should call drbd_md_sync() after you called
1059 * this function.
1060 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001061static int drbd_check_al_size(struct drbd_device *device, struct disk_conf *dc)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001062{
1063 struct lru_cache *n, *t;
1064 struct lc_element *e;
1065 unsigned int in_use;
1066 int i;
1067
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001068 if (device->act_log &&
1069 device->act_log->nr_elements == dc->al_extents)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001070 return 0;
1071
1072 in_use = 0;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001073 t = device->act_log;
Lars Ellenberg7ad651b2011-02-21 13:21:03 +01001074 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
Lars Ellenbergf3990022011-03-23 14:31:09 +01001075 dc->al_extents, sizeof(struct lc_element), 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001076
1077 if (n == NULL) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001078 drbd_err(device, "Cannot allocate act_log lru!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001079 return -ENOMEM;
1080 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001081 spin_lock_irq(&device->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001082 if (t) {
1083 for (i = 0; i < t->nr_elements; i++) {
1084 e = lc_element_by_index(t, i);
1085 if (e->refcnt)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001086 drbd_err(device, "refcnt(%d)==%d\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001087 e->lc_number, e->refcnt);
1088 in_use += e->refcnt;
1089 }
1090 }
1091 if (!in_use)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001092 device->act_log = n;
1093 spin_unlock_irq(&device->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001094 if (in_use) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001095 drbd_err(device, "Activity log still in use!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001096 lc_destroy(n);
1097 return -EBUSY;
1098 } else {
1099 if (t)
1100 lc_destroy(t);
1101 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001102 drbd_md_mark_dirty(device); /* we changed device->act_log->nr_elemens */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001103 return 0;
1104}
1105
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001106static void drbd_setup_queue_param(struct drbd_device *device, unsigned int max_bio_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001107{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001108 struct request_queue * const q = device->rq_queue;
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001109 unsigned int max_hw_sectors = max_bio_size >> 9;
1110 unsigned int max_segments = 0;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001111
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001112 if (get_ldev_if_state(device, D_ATTACHING)) {
1113 struct request_queue * const b = device->ldev->backing_bdev->bd_disk->queue;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001114
1115 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001116 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001117 max_segments = rcu_dereference(device->ldev->disk_conf)->max_bio_bvecs;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001118 rcu_read_unlock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001119 put_ldev(device);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001120 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001121
Philipp Reisnerb411b362009-09-25 16:07:19 -07001122 blk_queue_logical_block_size(q, 512);
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001123 blk_queue_max_hw_sectors(q, max_hw_sectors);
1124 /* This is the workaround for "bio would need to, but cannot, be split" */
1125 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
1126 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001127
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001128 if (get_ldev_if_state(device, D_ATTACHING)) {
1129 struct request_queue * const b = device->ldev->backing_bdev->bd_disk->queue;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001130
Philipp Reisner99432fc2011-05-20 16:39:13 +02001131 blk_queue_stack_limits(q, b);
1132
1133 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001134 drbd_info(device, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
Philipp Reisner99432fc2011-05-20 16:39:13 +02001135 q->backing_dev_info.ra_pages,
1136 b->backing_dev_info.ra_pages);
1137 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1138 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001139 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001140 }
1141}
1142
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001143void drbd_reconsider_max_bio_size(struct drbd_device *device)
Philipp Reisner99432fc2011-05-20 16:39:13 +02001144{
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001145 unsigned int now, new, local, peer;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001146
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001147 now = queue_max_hw_sectors(device->rq_queue) << 9;
1148 local = device->local_max_bio_size; /* Eventually last known value, from volatile memory */
1149 peer = device->peer_max_bio_size; /* Eventually last known value, from meta data */
Philipp Reisner99432fc2011-05-20 16:39:13 +02001150
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001151 if (get_ldev_if_state(device, D_ATTACHING)) {
1152 local = queue_max_hw_sectors(device->ldev->backing_bdev->bd_disk->queue) << 9;
1153 device->local_max_bio_size = local;
1154 put_ldev(device);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001155 }
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001156 local = min(local, DRBD_MAX_BIO_SIZE);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001157
1158 /* We may ignore peer limits if the peer is modern enough.
1159 Because new from 8.3.8 onwards the peer can use multiple
1160 BIOs for a single peer_request */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001161 if (device->state.conn >= C_WF_REPORT_PARAMS) {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001162 if (first_peer_device(device)->connection->agreed_pro_version < 94)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001163 peer = min(device->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
Philipp Reisner68093842011-06-30 15:43:06 +02001164 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001165 else if (first_peer_device(device)->connection->agreed_pro_version == 94)
Philipp Reisner99432fc2011-05-20 16:39:13 +02001166 peer = DRBD_MAX_SIZE_H80_PACKET;
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001167 else if (first_peer_device(device)->connection->agreed_pro_version < 100)
Philipp Reisner2ffca4f2011-06-30 15:43:06 +02001168 peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
1169 else
Philipp Reisner99432fc2011-05-20 16:39:13 +02001170 peer = DRBD_MAX_BIO_SIZE;
1171 }
1172
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001173 new = min(local, peer);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001174
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001175 if (device->state.role == R_PRIMARY && new < now)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001176 drbd_err(device, "ASSERT FAILED new < now; (%u < %u)\n", new, now);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001177
1178 if (new != now)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001179 drbd_info(device, "max BIO size = %u\n", new);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001180
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001181 drbd_setup_queue_param(device, new);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001182}
1183
Philipp Reisnera18e9d12011-04-24 11:09:55 +02001184/* Starts the worker thread */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001185static void conn_reconfig_start(struct drbd_connection *connection)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001186{
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001187 drbd_thread_start(&connection->worker);
1188 conn_flush_workqueue(connection);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001189}
1190
Philipp Reisnera18e9d12011-04-24 11:09:55 +02001191/* if still unconfigured, stops worker again. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001192static void conn_reconfig_done(struct drbd_connection *connection)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001193{
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001194 bool stop_threads;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001195 spin_lock_irq(&connection->req_lock);
1196 stop_threads = conn_all_vols_unconf(connection) &&
1197 connection->cstate == C_STANDALONE;
1198 spin_unlock_irq(&connection->req_lock);
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001199 if (stop_threads) {
1200 /* asender is implicitly stopped by receiver
Philipp Reisner81fa2e62011-05-04 15:10:30 +02001201 * in conn_disconnect() */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001202 drbd_thread_stop(&connection->receiver);
1203 drbd_thread_stop(&connection->worker);
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001204 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001205}
1206
Philipp Reisner07782862010-08-31 12:00:50 +02001207/* Make sure IO is suspended before calling this function(). */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001208static void drbd_suspend_al(struct drbd_device *device)
Philipp Reisner07782862010-08-31 12:00:50 +02001209{
1210 int s = 0;
1211
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001212 if (!lc_try_lock(device->act_log)) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001213 drbd_warn(device, "Failed to lock al in drbd_suspend_al()\n");
Philipp Reisner07782862010-08-31 12:00:50 +02001214 return;
1215 }
1216
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001217 drbd_al_shrink(device);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001218 spin_lock_irq(&first_peer_device(device)->connection->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001219 if (device->state.conn < C_CONNECTED)
1220 s = !test_and_set_bit(AL_SUSPENDED, &device->flags);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001221 spin_unlock_irq(&first_peer_device(device)->connection->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001222 lc_unlock(device->act_log);
Philipp Reisner07782862010-08-31 12:00:50 +02001223
1224 if (s)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001225 drbd_info(device, "Suspended AL updates\n");
Philipp Reisner07782862010-08-31 12:00:50 +02001226}
1227
Lars Ellenberg5979e362011-04-27 21:09:55 +02001228
1229static bool should_set_defaults(struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001230{
Lars Ellenberg5979e362011-04-27 21:09:55 +02001231 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1232 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1233}
1234
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001235static unsigned int drbd_al_extents_max(struct drbd_backing_dev *bdev)
Philipp Reisnerd589a212011-05-04 10:06:52 +02001236{
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001237 /* This is limited by 16 bit "slot" numbers,
1238 * and by available on-disk context storage.
1239 *
1240 * Also (u16)~0 is special (denotes a "free" extent).
1241 *
1242 * One transaction occupies one 4kB on-disk block,
1243 * we have n such blocks in the on disk ring buffer,
1244 * the "current" transaction may fail (n-1),
1245 * and there is 919 slot numbers context information per transaction.
1246 *
1247 * 72 transaction blocks amounts to more than 2**16 context slots,
1248 * so cap there first.
1249 */
1250 const unsigned int max_al_nr = DRBD_AL_EXTENTS_MAX;
1251 const unsigned int sufficient_on_disk =
1252 (max_al_nr + AL_CONTEXT_PER_TRANSACTION -1)
1253 /AL_CONTEXT_PER_TRANSACTION;
Philipp Reisnerd589a212011-05-04 10:06:52 +02001254
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001255 unsigned int al_size_4k = bdev->md.al_size_4k;
1256
1257 if (al_size_4k > sufficient_on_disk)
1258 return max_al_nr;
1259
1260 return (al_size_4k - 1) * AL_CONTEXT_PER_TRANSACTION;
Philipp Reisnerd589a212011-05-04 10:06:52 +02001261}
1262
Lars Ellenbergf3990022011-03-23 14:31:09 +01001263int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1264{
1265 enum drbd_ret_code retcode;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001266 struct drbd_device *device;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001267 struct disk_conf *new_disk_conf, *old_disk_conf;
Philipp Reisner813472c2011-05-03 16:47:02 +02001268 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001269 int err, fifo_size;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001270
1271 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1272 if (!adm_ctx.reply_skb)
1273 return retcode;
1274 if (retcode != NO_ERROR)
1275 goto out;
1276
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001277 device = adm_ctx.device;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001278
1279 /* we also need a disk
1280 * to change the options on */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001281 if (!get_ldev(device)) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001282 retcode = ERR_NO_DISK;
1283 goto out;
1284 }
1285
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001286 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001287 if (!new_disk_conf) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001288 retcode = ERR_NOMEM;
1289 goto fail;
1290 }
1291
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001292 mutex_lock(&first_peer_device(device)->connection->conf_update);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001293 old_disk_conf = device->ldev->disk_conf;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001294 *new_disk_conf = *old_disk_conf;
Lars Ellenberg5979e362011-04-27 21:09:55 +02001295 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02001296 set_disk_conf_defaults(new_disk_conf);
Lars Ellenberg5979e362011-04-27 21:09:55 +02001297
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001298 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02001299 if (err && err != -ENOMSG) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001300 retcode = ERR_MANDATORY_TAG;
1301 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisner8e229432013-08-01 10:21:47 +02001302 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001303 }
1304
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001305 if (!expect(new_disk_conf->resync_rate >= 1))
1306 new_disk_conf->resync_rate = 1;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001307
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001308 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1309 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001310 if (new_disk_conf->al_extents > drbd_al_extents_max(device->ldev))
1311 new_disk_conf->al_extents = drbd_al_extents_max(device->ldev);
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001312
1313 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1314 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001315
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001316 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001317 if (fifo_size != device->rs_plan_s->size) {
Philipp Reisner813472c2011-05-03 16:47:02 +02001318 new_plan = fifo_alloc(fifo_size);
1319 if (!new_plan) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001320 drbd_err(device, "kmalloc of fifo_buffer failed");
Lars Ellenbergf3990022011-03-23 14:31:09 +01001321 retcode = ERR_NOMEM;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001322 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001323 }
1324 }
1325
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001326 drbd_suspend_io(device);
1327 wait_event(device->al_wait, lc_try_lock(device->act_log));
1328 drbd_al_shrink(device);
1329 err = drbd_check_al_size(device, new_disk_conf);
1330 lc_unlock(device->act_log);
1331 wake_up(&device->al_wait);
1332 drbd_resume_io(device);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001333
1334 if (err) {
1335 retcode = ERR_NOMEM;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001336 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001337 }
1338
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001339 write_lock_irq(&global_state_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001340 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001341 if (retcode == NO_ERROR) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001342 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
1343 drbd_resync_after_changed(device);
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001344 }
1345 write_unlock_irq(&global_state_lock);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001346
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001347 if (retcode != NO_ERROR)
1348 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001349
Philipp Reisner813472c2011-05-03 16:47:02 +02001350 if (new_plan) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001351 old_plan = device->rs_plan_s;
1352 rcu_assign_pointer(device->rs_plan_s, new_plan);
Philipp Reisner9958c852011-05-03 16:19:31 +02001353 }
Philipp Reisner9958c852011-05-03 16:19:31 +02001354
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001355 mutex_unlock(&first_peer_device(device)->connection->conf_update);
Philipp Reisner27eb13e2012-03-30 14:12:15 +02001356
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001357 if (new_disk_conf->al_updates)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001358 device->ldev->md.flags &= ~MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001359 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001360 device->ldev->md.flags |= MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001361
Lars Ellenberg691631c2012-10-26 00:41:50 +02001362 if (new_disk_conf->md_flushes)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001363 clear_bit(MD_NO_FUA, &device->flags);
Lars Ellenberg691631c2012-10-26 00:41:50 +02001364 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001365 set_bit(MD_NO_FUA, &device->flags);
Lars Ellenberg691631c2012-10-26 00:41:50 +02001366
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001367 drbd_bump_write_ordering(first_peer_device(device)->connection, WO_bdev_flush);
Philipp Reisner27eb13e2012-03-30 14:12:15 +02001368
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001369 drbd_md_sync(device);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001370
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001371 if (device->state.conn >= C_CONNECTED)
1372 drbd_send_sync_param(device);
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 Gruenbachera6b32bc2011-05-31 14:33:49 +02001381 mutex_unlock(&first_peer_device(device)->connection->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 Gruenbacherb30ab792011-07-03 13:26:43 +02001603 drbd_flush_workqueue(device);
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 Gruenbachera6b32bc2011-05-31 14:33:49 +02001672 !(device->state.role == R_PRIMARY &&
1673 first_peer_device(device)->connection->susp_nod))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001674 set_bit(CRASHED_PRIMARY, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001675
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001676 device->send_cnt = 0;
1677 device->recv_cnt = 0;
1678 device->read_cnt = 0;
1679 device->writ_cnt = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001680
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001681 drbd_reconsider_max_bio_size(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001682
1683 /* If I am currently not R_PRIMARY,
1684 * but meta data primary indicator is set,
1685 * I just now recover from a hard crash,
1686 * and have been R_PRIMARY before that crash.
1687 *
1688 * Now, if I had no connection before that crash
1689 * (have been degraded R_PRIMARY), chances are that
1690 * I won't find my peer now either.
1691 *
1692 * In that case, and _only_ in that case,
1693 * we use the degr-wfc-timeout instead of the default,
1694 * so we can automatically recover from a crash of a
1695 * degraded but active "cluster" after a certain timeout.
1696 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001697 clear_bit(USE_DEGR_WFC_T, &device->flags);
1698 if (device->state.role != R_PRIMARY &&
1699 drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
1700 !drbd_md_test_flag(device->ldev, MDF_CONNECTED_IND))
1701 set_bit(USE_DEGR_WFC_T, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001702
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001703 dd = drbd_determine_dev_size(device, 0, NULL);
Philipp Reisnerd752b262013-06-25 16:50:08 +02001704 if (dd <= DS_ERROR) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001705 retcode = ERR_NOMEM_BITMAP;
1706 goto force_diskless_dec;
Philipp Reisnere96c9632013-06-25 16:50:07 +02001707 } else if (dd == DS_GREW)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001708 set_bit(RESYNC_AFTER_NEG, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001709
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001710 if (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC) ||
1711 (test_bit(CRASHED_PRIMARY, &device->flags) &&
1712 drbd_md_test_flag(device->ldev, MDF_AL_DISABLED))) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001713 drbd_info(device, "Assuming that all blocks are out of sync "
Philipp Reisnerb411b362009-09-25 16:07:19 -07001714 "(aka FullSync)\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001715 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001716 "set_n_write from attaching", BM_LOCKED_MASK)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001717 retcode = ERR_IO_MD_DISK;
1718 goto force_diskless_dec;
1719 }
1720 } else {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001721 if (drbd_bitmap_io(device, &drbd_bm_read,
Andreas Gruenbacher22ab6a32010-12-13 01:44:11 +01001722 "read from attaching", BM_LOCKED_MASK)) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001723 retcode = ERR_IO_MD_DISK;
1724 goto force_diskless_dec;
1725 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001726 }
1727
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001728 if (_drbd_bm_total_weight(device) == drbd_bm_bits(device))
1729 drbd_suspend_al(device); /* IO is still suspended here... */
Philipp Reisner07782862010-08-31 12:00:50 +02001730
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001731 spin_lock_irq(&first_peer_device(device)->connection->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001732 os = drbd_read_state(device);
Philipp Reisner78bae592011-03-28 15:40:12 +02001733 ns = os;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001734 /* If MDF_CONSISTENT is not set go into inconsistent state,
1735 otherwise investigate MDF_WasUpToDate...
1736 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1737 otherwise into D_CONSISTENT state.
1738 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001739 if (drbd_md_test_flag(device->ldev, MDF_CONSISTENT)) {
1740 if (drbd_md_test_flag(device->ldev, MDF_WAS_UP_TO_DATE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001741 ns.disk = D_CONSISTENT;
1742 else
1743 ns.disk = D_OUTDATED;
1744 } else {
1745 ns.disk = D_INCONSISTENT;
1746 }
1747
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001748 if (drbd_md_test_flag(device->ldev, MDF_PEER_OUT_DATED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001749 ns.pdsk = D_OUTDATED;
1750
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001751 rcu_read_lock();
1752 if (ns.disk == D_CONSISTENT &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001753 (ns.pdsk == D_OUTDATED || rcu_dereference(device->ldev->disk_conf)->fencing == FP_DONT_CARE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001754 ns.disk = D_UP_TO_DATE;
1755
1756 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1757 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1758 this point, because drbd_request_state() modifies these
1759 flags. */
1760
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001761 if (rcu_dereference(device->ldev->disk_conf)->al_updates)
1762 device->ldev->md.flags &= ~MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001763 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001764 device->ldev->md.flags |= MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001765
1766 rcu_read_unlock();
1767
Philipp Reisnerb411b362009-09-25 16:07:19 -07001768 /* In case we are C_CONNECTED postpone any decision on the new disk
1769 state after the negotiation phase. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001770 if (device->state.conn == C_CONNECTED) {
1771 device->new_state_tmp.i = ns.i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001772 ns.i = os.i;
1773 ns.disk = D_NEGOTIATING;
Philipp Reisnerdc66c742010-06-02 14:31:29 +02001774
1775 /* We expect to receive up-to-date UUIDs soon.
1776 To avoid a race in receive_state, free p_uuid while
1777 holding req_lock. I.e. atomic with the state change */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001778 kfree(device->p_uuid);
1779 device->p_uuid = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001780 }
1781
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001782 rv = _drbd_set_state(device, ns, CS_VERBOSE, NULL);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001783 spin_unlock_irq(&first_peer_device(device)->connection->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001784
1785 if (rv < SS_SUCCESS)
1786 goto force_diskless_dec;
1787
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001788 mod_timer(&device->request_timer, jiffies + HZ);
Philipp Reisnercdfda632011-07-05 15:38:59 +02001789
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001790 if (device->state.role == R_PRIMARY)
1791 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001792 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001793 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001794
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001795 drbd_md_mark_dirty(device);
1796 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001797
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001798 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
1799 put_ldev(device);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001800 conn_reconfig_done(first_peer_device(device)->connection);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001801 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001802 return 0;
1803
1804 force_diskless_dec:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001805 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001806 force_diskless:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001807 drbd_force_state(device, NS(disk, D_DISKLESS));
1808 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001809 fail:
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001810 conn_reconfig_done(first_peer_device(device)->connection);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001811 if (nbc) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001812 if (nbc->backing_bdev)
1813 blkdev_put(nbc->backing_bdev,
1814 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1815 if (nbc->md_bdev)
1816 blkdev_put(nbc->md_bdev,
1817 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001818 kfree(nbc);
1819 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001820 kfree(new_disk_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001821 lc_destroy(resync_lru);
Philipp Reisner9958c852011-05-03 16:19:31 +02001822 kfree(new_plan);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001823
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001824 finish:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001825 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001826 return 0;
1827}
1828
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001829static int adm_detach(struct drbd_device *device, int force)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001830{
Philipp Reisner19f83c72011-03-29 14:21:03 +02001831 enum drbd_state_rv retcode;
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001832 int ret;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001833
Philipp Reisnercdfda632011-07-05 15:38:59 +02001834 if (force) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001835 set_bit(FORCE_DETACH, &device->flags);
1836 drbd_force_state(device, NS(disk, D_FAILED));
Philipp Reisnercdfda632011-07-05 15:38:59 +02001837 retcode = SS_SUCCESS;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001838 goto out;
1839 }
1840
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001841 drbd_suspend_io(device); /* so no-one is stuck in drbd_al_begin_io */
1842 drbd_md_get_buffer(device); /* make sure there is no in-flight meta-data IO */
1843 retcode = drbd_request_state(device, NS(disk, D_FAILED));
1844 drbd_md_put_buffer(device);
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001845 /* D_FAILED will transition to DISKLESS. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001846 ret = wait_event_interruptible(device->misc_wait,
1847 device->state.disk != D_FAILED);
1848 drbd_resume_io(device);
Philipp Reisner9b2f61a2011-05-24 10:27:38 +02001849 if ((int)retcode == (int)SS_IS_DISKLESS)
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001850 retcode = SS_NOTHING_TO_DO;
1851 if (ret)
1852 retcode = ERR_INTR;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001853out:
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001854 return retcode;
1855}
1856
Philipp Reisnerb411b362009-09-25 16:07:19 -07001857/* Detaching the disk is a process in multiple stages. First we need to lock
1858 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1859 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1860 * internal references as well.
1861 * Only then we have finally detached. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001862int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001863{
1864 enum drbd_ret_code retcode;
Philipp Reisnercdfda632011-07-05 15:38:59 +02001865 struct detach_parms parms = { };
1866 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001867
1868 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1869 if (!adm_ctx.reply_skb)
1870 return retcode;
1871 if (retcode != NO_ERROR)
1872 goto out;
1873
Philipp Reisnercdfda632011-07-05 15:38:59 +02001874 if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
1875 err = detach_parms_from_attrs(&parms, info);
1876 if (err) {
1877 retcode = ERR_MANDATORY_TAG;
1878 drbd_msg_put_info(from_attrs_err_to_txt(err));
1879 goto out;
1880 }
1881 }
1882
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001883 retcode = adm_detach(adm_ctx.device, parms.force_detach);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001884out:
1885 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001886 return 0;
1887}
1888
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001889static bool conn_resync_running(struct drbd_connection *connection)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001890{
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001891 struct drbd_peer_device *peer_device;
Philipp Reisner695d08f2011-04-11 22:53:32 -07001892 bool rv = false;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001893 int vnr;
1894
Philipp Reisner695d08f2011-04-11 22:53:32 -07001895 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001896 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1897 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001898 if (device->state.conn == C_SYNC_SOURCE ||
1899 device->state.conn == C_SYNC_TARGET ||
1900 device->state.conn == C_PAUSED_SYNC_S ||
1901 device->state.conn == C_PAUSED_SYNC_T) {
Philipp Reisner695d08f2011-04-11 22:53:32 -07001902 rv = true;
1903 break;
1904 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001905 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001906 rcu_read_unlock();
1907
1908 return rv;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001909}
1910
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001911static bool conn_ov_running(struct drbd_connection *connection)
Lars Ellenbergf3990022011-03-23 14:31:09 +01001912{
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001913 struct drbd_peer_device *peer_device;
Philipp Reisner695d08f2011-04-11 22:53:32 -07001914 bool rv = false;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001915 int vnr;
1916
Philipp Reisner695d08f2011-04-11 22:53:32 -07001917 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001918 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1919 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001920 if (device->state.conn == C_VERIFY_S ||
1921 device->state.conn == C_VERIFY_T) {
Philipp Reisner695d08f2011-04-11 22:53:32 -07001922 rv = true;
1923 break;
1924 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001925 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001926 rcu_read_unlock();
1927
1928 return rv;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001929}
1930
Philipp Reisnercd643972011-04-13 18:00:59 -07001931static enum drbd_ret_code
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001932_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 -07001933{
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001934 struct drbd_peer_device *peer_device;
Philipp Reisnercd643972011-04-13 18:00:59 -07001935 int i;
1936
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001937 if (old_net_conf && connection->cstate == C_WF_REPORT_PARAMS && connection->agreed_pro_version < 100) {
1938 if (new_net_conf->wire_protocol != old_net_conf->wire_protocol)
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02001939 return ERR_NEED_APV_100;
1940
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001941 if (new_net_conf->two_primaries != old_net_conf->two_primaries)
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02001942 return ERR_NEED_APV_100;
1943
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001944 if (strcmp(new_net_conf->integrity_alg, old_net_conf->integrity_alg))
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02001945 return ERR_NEED_APV_100;
1946 }
1947
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001948 if (!new_net_conf->two_primaries &&
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001949 conn_highest_role(connection) == R_PRIMARY &&
1950 conn_highest_peer(connection) == R_PRIMARY)
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02001951 return ERR_NEED_ALLOW_TWO_PRI;
Philipp Reisnerb032b6f2011-04-13 18:16:10 -07001952
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001953 if (new_net_conf->two_primaries &&
1954 (new_net_conf->wire_protocol != DRBD_PROT_C))
Philipp Reisnercd643972011-04-13 18:00:59 -07001955 return ERR_NOT_PROTO_C;
1956
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001957 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
1958 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001959 if (get_ldev(device)) {
1960 enum drbd_fencing_p fp = rcu_dereference(device->ldev->disk_conf)->fencing;
1961 put_ldev(device);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001962 if (new_net_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
Philipp Reisnercd643972011-04-13 18:00:59 -07001963 return ERR_STONITH_AND_PROT_A;
Philipp Reisnercd643972011-04-13 18:00:59 -07001964 }
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001965 if (device->state.role == R_PRIMARY && new_net_conf->discard_my_data)
Lars Ellenbergeb120102012-08-01 12:46:20 +02001966 return ERR_DISCARD_IMPOSSIBLE;
Philipp Reisnercd643972011-04-13 18:00:59 -07001967 }
Philipp Reisnercd643972011-04-13 18:00:59 -07001968
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001969 if (new_net_conf->on_congestion != OC_BLOCK && new_net_conf->wire_protocol != DRBD_PROT_A)
Philipp Reisnercd643972011-04-13 18:00:59 -07001970 return ERR_CONG_NOT_PROTO_A;
1971
1972 return NO_ERROR;
1973}
1974
Philipp Reisner44ed1672011-04-19 17:10:19 +02001975static enum drbd_ret_code
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001976check_net_options(struct drbd_connection *connection, struct net_conf *new_net_conf)
Philipp Reisner44ed1672011-04-19 17:10:19 +02001977{
1978 static enum drbd_ret_code rv;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001979 struct drbd_peer_device *peer_device;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001980 int i;
1981
1982 rcu_read_lock();
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001983 rv = _check_net_options(connection, rcu_dereference(connection->net_conf), new_net_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001984 rcu_read_unlock();
1985
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001986 /* connection->volumes protected by genl_lock() here */
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001987 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
1988 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001989 if (!device->bitmap) {
1990 if (drbd_bm_init(device))
Philipp Reisner44ed1672011-04-19 17:10:19 +02001991 return ERR_NOMEM;
1992 }
1993 }
1994
1995 return rv;
1996}
1997
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001998struct crypto {
1999 struct crypto_hash *verify_tfm;
2000 struct crypto_hash *csums_tfm;
2001 struct crypto_hash *cram_hmac_tfm;
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02002002 struct crypto_hash *integrity_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002003};
2004
2005static int
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002006alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002007{
2008 if (!tfm_name[0])
2009 return NO_ERROR;
2010
2011 *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
2012 if (IS_ERR(*tfm)) {
2013 *tfm = NULL;
2014 return err_alg;
2015 }
2016
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002017 return NO_ERROR;
2018}
2019
2020static enum drbd_ret_code
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002021alloc_crypto(struct crypto *crypto, struct net_conf *new_net_conf)
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002022{
Philipp Reisnerb411b362009-09-25 16:07:19 -07002023 char hmac_name[CRYPTO_MAX_ALG_NAME];
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002024 enum drbd_ret_code rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002025
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002026 rv = alloc_hash(&crypto->csums_tfm, new_net_conf->csums_alg,
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002027 ERR_CSUMS_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002028 if (rv != NO_ERROR)
2029 return rv;
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002030 rv = alloc_hash(&crypto->verify_tfm, new_net_conf->verify_alg,
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002031 ERR_VERIFY_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002032 if (rv != NO_ERROR)
2033 return rv;
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002034 rv = alloc_hash(&crypto->integrity_tfm, new_net_conf->integrity_alg,
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002035 ERR_INTEGRITY_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002036 if (rv != NO_ERROR)
2037 return rv;
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002038 if (new_net_conf->cram_hmac_alg[0] != 0) {
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002039 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002040 new_net_conf->cram_hmac_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002041
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002042 rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
2043 ERR_AUTH_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002044 }
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002045
2046 return rv;
2047}
2048
2049static void free_crypto(struct crypto *crypto)
2050{
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002051 crypto_free_hash(crypto->cram_hmac_tfm);
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02002052 crypto_free_hash(crypto->integrity_tfm);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002053 crypto_free_hash(crypto->csums_tfm);
2054 crypto_free_hash(crypto->verify_tfm);
2055}
2056
Lars Ellenbergf3990022011-03-23 14:31:09 +01002057int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
2058{
2059 enum drbd_ret_code retcode;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002060 struct drbd_connection *connection;
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002061 struct net_conf *old_net_conf, *new_net_conf = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002062 int err;
2063 int ovr; /* online verify running */
2064 int rsr; /* re-sync running */
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002065 struct crypto crypto = { };
Lars Ellenbergf3990022011-03-23 14:31:09 +01002066
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002067 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002068 if (!adm_ctx.reply_skb)
2069 return retcode;
2070 if (retcode != NO_ERROR)
2071 goto out;
2072
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002073 connection = adm_ctx.connection;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002074
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002075 new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
2076 if (!new_net_conf) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002077 retcode = ERR_NOMEM;
2078 goto out;
2079 }
2080
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002081 conn_reconfig_start(connection);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002082
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002083 mutex_lock(&connection->data.mutex);
2084 mutex_lock(&connection->conf_update);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002085 old_net_conf = connection->net_conf;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002086
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002087 if (!old_net_conf) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02002088 drbd_msg_put_info("net conf missing, try connect");
2089 retcode = ERR_INVALID_REQUEST;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002090 goto fail;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002091 }
2092
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002093 *new_net_conf = *old_net_conf;
Lars Ellenberg5979e362011-04-27 21:09:55 +02002094 if (should_set_defaults(info))
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002095 set_net_conf_defaults(new_net_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +02002096
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002097 err = net_conf_from_attrs_for_change(new_net_conf, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02002098 if (err && err != -ENOMSG) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002099 retcode = ERR_MANDATORY_TAG;
2100 drbd_msg_put_info(from_attrs_err_to_txt(err));
2101 goto fail;
2102 }
2103
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002104 retcode = check_net_options(connection, new_net_conf);
Philipp Reisnercd643972011-04-13 18:00:59 -07002105 if (retcode != NO_ERROR)
2106 goto fail;
2107
Lars Ellenbergf3990022011-03-23 14:31:09 +01002108 /* re-sync running */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002109 rsr = conn_resync_running(connection);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002110 if (rsr && strcmp(new_net_conf->csums_alg, old_net_conf->csums_alg)) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002111 retcode = ERR_CSUMS_RESYNC_RUNNING;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002112 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002113 }
2114
Lars Ellenbergf3990022011-03-23 14:31:09 +01002115 /* online verify running */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002116 ovr = conn_ov_running(connection);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002117 if (ovr && strcmp(new_net_conf->verify_alg, old_net_conf->verify_alg)) {
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002118 retcode = ERR_VERIFY_RUNNING;
2119 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002120 }
2121
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002122 retcode = alloc_crypto(&crypto, new_net_conf);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002123 if (retcode != NO_ERROR)
2124 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002125
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002126 rcu_assign_pointer(connection->net_conf, new_net_conf);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002127
2128 if (!rsr) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002129 crypto_free_hash(connection->csums_tfm);
2130 connection->csums_tfm = crypto.csums_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002131 crypto.csums_tfm = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002132 }
2133 if (!ovr) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002134 crypto_free_hash(connection->verify_tfm);
2135 connection->verify_tfm = crypto.verify_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002136 crypto.verify_tfm = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002137 }
2138
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002139 crypto_free_hash(connection->integrity_tfm);
2140 connection->integrity_tfm = crypto.integrity_tfm;
2141 if (connection->cstate >= C_WF_REPORT_PARAMS && connection->agreed_pro_version >= 100)
2142 /* Do this without trying to take connection->data.mutex again. */
2143 __drbd_send_protocol(connection, P_PROTOCOL_UPDATE);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002144
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002145 crypto_free_hash(connection->cram_hmac_tfm);
2146 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002147
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002148 mutex_unlock(&connection->conf_update);
2149 mutex_unlock(&connection->data.mutex);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002150 synchronize_rcu();
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002151 kfree(old_net_conf);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002152
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002153 if (connection->cstate >= C_WF_REPORT_PARAMS)
2154 drbd_send_sync_param(minor_to_device(conn_lowest_minor(connection)));
Lars Ellenbergf3990022011-03-23 14:31:09 +01002155
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002156 goto done;
2157
Lars Ellenbergf3990022011-03-23 14:31:09 +01002158 fail:
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002159 mutex_unlock(&connection->conf_update);
2160 mutex_unlock(&connection->data.mutex);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002161 free_crypto(&crypto);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002162 kfree(new_net_conf);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002163 done:
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002164 conn_reconfig_done(connection);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002165 out:
2166 drbd_adm_finish(info, retcode);
2167 return 0;
2168}
2169
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002170int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002171{
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002172 struct drbd_peer_device *peer_device;
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002173 struct net_conf *old_net_conf, *new_net_conf = NULL;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002174 struct crypto crypto = { };
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002175 struct drbd_resource *resource;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002176 struct drbd_connection *connection;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002177 enum drbd_ret_code retcode;
2178 int i;
2179 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002180
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02002181 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002182
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002183 if (!adm_ctx.reply_skb)
2184 return retcode;
2185 if (retcode != NO_ERROR)
2186 goto out;
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002187 if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
2188 drbd_msg_put_info("connection endpoint(s) missing");
2189 retcode = ERR_INVALID_REQUEST;
2190 goto out;
2191 }
2192
2193 /* No need for _rcu here. All reconfiguration is
2194 * strictly serialized on genl_lock(). We are protected against
2195 * concurrent reconfiguration/addition/deletion */
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002196 for_each_resource(resource, &drbd_resources) {
2197 for_each_connection(connection, resource) {
2198 if (nla_len(adm_ctx.my_addr) == connection->my_addr_len &&
2199 !memcmp(nla_data(adm_ctx.my_addr), &connection->my_addr,
2200 connection->my_addr_len)) {
2201 retcode = ERR_LOCAL_ADDR;
2202 goto out;
2203 }
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002204
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002205 if (nla_len(adm_ctx.peer_addr) == connection->peer_addr_len &&
2206 !memcmp(nla_data(adm_ctx.peer_addr), &connection->peer_addr,
2207 connection->peer_addr_len)) {
2208 retcode = ERR_PEER_ADDR;
2209 goto out;
2210 }
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002211 }
2212 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002213
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002214 connection = adm_ctx.connection;
2215 conn_reconfig_start(connection);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002216
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002217 if (connection->cstate > C_STANDALONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002218 retcode = ERR_NET_CONFIGURED;
2219 goto fail;
2220 }
2221
Andreas Gruenbachera209b4a2011-08-17 12:43:25 +02002222 /* allocation not in the IO path, drbdsetup / netlink process context */
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002223 new_net_conf = kzalloc(sizeof(*new_net_conf), GFP_KERNEL);
2224 if (!new_net_conf) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002225 retcode = ERR_NOMEM;
2226 goto fail;
2227 }
2228
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002229 set_net_conf_defaults(new_net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002230
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002231 err = net_conf_from_attrs(new_net_conf, info);
Lars Ellenberg25e40932011-08-19 10:39:00 +02002232 if (err && err != -ENOMSG) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002233 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002234 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002235 goto fail;
2236 }
2237
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002238 retcode = check_net_options(connection, new_net_conf);
Philipp Reisnercd643972011-04-13 18:00:59 -07002239 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002240 goto fail;
Philipp Reisner47ff2d02010-06-18 13:56:57 +02002241
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002242 retcode = alloc_crypto(&crypto, new_net_conf);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002243 if (retcode != NO_ERROR)
Philipp Reisner422028b2010-10-27 11:12:07 +02002244 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002245
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002246 ((char *)new_net_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002247
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002248 conn_flush_workqueue(connection);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002249
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002250 mutex_lock(&connection->conf_update);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002251 old_net_conf = connection->net_conf;
2252 if (old_net_conf) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002253 retcode = ERR_NET_CONFIGURED;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002254 mutex_unlock(&connection->conf_update);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002255 goto fail;
2256 }
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002257 rcu_assign_pointer(connection->net_conf, new_net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002258
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002259 conn_free_crypto(connection);
2260 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
2261 connection->integrity_tfm = crypto.integrity_tfm;
2262 connection->csums_tfm = crypto.csums_tfm;
2263 connection->verify_tfm = crypto.verify_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002264
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002265 connection->my_addr_len = nla_len(adm_ctx.my_addr);
2266 memcpy(&connection->my_addr, nla_data(adm_ctx.my_addr), connection->my_addr_len);
2267 connection->peer_addr_len = nla_len(adm_ctx.peer_addr);
2268 memcpy(&connection->peer_addr, nla_data(adm_ctx.peer_addr), connection->peer_addr_len);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002269
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002270 mutex_unlock(&connection->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002271
Philipp Reisner695d08f2011-04-11 22:53:32 -07002272 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002273 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2274 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002275 device->send_cnt = 0;
2276 device->recv_cnt = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002277 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07002278 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002279
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002280 retcode = conn_request_state(connection, NS(conn, C_UNCONNECTED), CS_VERBOSE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002281
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002282 conn_reconfig_done(connection);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002283 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002284 return 0;
2285
2286fail:
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002287 free_crypto(&crypto);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002288 kfree(new_net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002289
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002290 conn_reconfig_done(connection);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002291out:
2292 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002293 return 0;
2294}
2295
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002296static enum drbd_state_rv conn_try_disconnect(struct drbd_connection *connection, bool force)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002297{
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002298 enum drbd_state_rv rv;
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002299
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002300 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002301 force ? CS_HARD : 0);
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002302
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002303 switch (rv) {
2304 case SS_NOTHING_TO_DO:
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002305 break;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002306 case SS_ALREADY_STANDALONE:
2307 return SS_SUCCESS;
2308 case SS_PRIMARY_NOP:
2309 /* Our state checking code wants to see the peer outdated. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002310 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING, pdsk, D_OUTDATED), 0);
Philipp Reisner2bd5ed52013-03-27 14:08:40 +01002311
2312 if (rv == SS_OUTDATE_WO_CONN) /* lost connection before graceful disconnect succeeded */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002313 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_VERBOSE);
Philipp Reisner2bd5ed52013-03-27 14:08:40 +01002314
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002315 break;
2316 case SS_CW_FAILED_BY_PEER:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002317 /* The peer probably wants to see us outdated. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002318 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING,
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002319 disk, D_OUTDATED), 0);
2320 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002321 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002322 CS_HARD);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002323 }
2324 break;
2325 default:;
2326 /* no special handling necessary */
2327 }
2328
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002329 if (rv >= SS_SUCCESS) {
2330 enum drbd_state_rv rv2;
2331 /* No one else can reconfigure the network while I am here.
2332 * The state handling only uses drbd_thread_stop_nowait(),
2333 * we want to really wait here until the receiver is no more.
2334 */
Andreas Gruenbacher9693da22011-06-21 13:58:18 +02002335 drbd_thread_stop(&connection->receiver);
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002336
2337 /* Race breaker. This additional state change request may be
2338 * necessary, if this was a forced disconnect during a receiver
2339 * restart. We may have "killed" the receiver thread just
2340 * after drbdd_init() returned. Typically, we should be
2341 * C_STANDALONE already, now, and this becomes a no-op.
2342 */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002343 rv2 = conn_request_state(connection, NS(conn, C_STANDALONE),
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002344 CS_VERBOSE | CS_HARD);
2345 if (rv2 < SS_SUCCESS)
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +02002346 drbd_err(connection,
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002347 "unexpected rv2=%d in conn_try_disconnect()\n",
2348 rv2);
2349 }
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002350 return rv;
2351}
2352
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002353int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002354{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002355 struct disconnect_parms parms;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002356 struct drbd_connection *connection;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002357 enum drbd_state_rv rv;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002358 enum drbd_ret_code retcode;
2359 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002360
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002361 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002362 if (!adm_ctx.reply_skb)
2363 return retcode;
2364 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002365 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002366
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002367 connection = adm_ctx.connection;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002368 memset(&parms, 0, sizeof(parms));
2369 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002370 err = disconnect_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002371 if (err) {
2372 retcode = ERR_MANDATORY_TAG;
2373 drbd_msg_put_info(from_attrs_err_to_txt(err));
2374 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002375 }
2376 }
2377
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002378 rv = conn_try_disconnect(connection, parms.force_disconnect);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002379 if (rv < SS_SUCCESS)
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002380 retcode = rv; /* FIXME: Type mismatch. */
2381 else
2382 retcode = NO_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002383 fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002384 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002385 return 0;
2386}
2387
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002388void resync_after_online_grow(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002389{
2390 int iass; /* I am sync source */
2391
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02002392 drbd_info(device, "Resync of new storage after online grow\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002393 if (device->state.role != device->state.peer)
2394 iass = (device->state.role == R_PRIMARY);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002395 else
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002396 iass = test_bit(RESOLVE_CONFLICTS, &first_peer_device(device)->connection->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002397
2398 if (iass)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002399 drbd_start_resync(device, C_SYNC_SOURCE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002400 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002401 _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002402}
2403
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002404int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002405{
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002406 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002407 struct resize_parms rs;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002408 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002409 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002410 enum determine_dev_size dd;
Philipp Reisnerd752b262013-06-25 16:50:08 +02002411 bool change_al_layout = false;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002412 enum dds_flags ddsf;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002413 sector_t u_size;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002414 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002415
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002416 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2417 if (!adm_ctx.reply_skb)
2418 return retcode;
2419 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002420 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002421
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002422 device = adm_ctx.device;
2423 if (!get_ldev(device)) {
Philipp Reisnerd752b262013-06-25 16:50:08 +02002424 retcode = ERR_NO_DISK;
2425 goto fail;
2426 }
2427
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002428 memset(&rs, 0, sizeof(struct resize_parms));
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002429 rs.al_stripes = device->ldev->md.al_stripes;
2430 rs.al_stripe_size = device->ldev->md.al_stripe_size_4k * 4;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002431 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002432 err = resize_parms_from_attrs(&rs, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002433 if (err) {
2434 retcode = ERR_MANDATORY_TAG;
2435 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerd752b262013-06-25 16:50:08 +02002436 goto fail_ldev;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002437 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002438 }
2439
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002440 if (device->state.conn > C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002441 retcode = ERR_RESIZE_RESYNC;
Philipp Reisnerd752b262013-06-25 16:50:08 +02002442 goto fail_ldev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002443 }
2444
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002445 if (device->state.role == R_SECONDARY &&
2446 device->state.peer == R_SECONDARY) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002447 retcode = ERR_NO_PRIMARY;
Philipp Reisnerd752b262013-06-25 16:50:08 +02002448 goto fail_ldev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002449 }
2450
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002451 if (rs.no_resync && first_peer_device(device)->connection->agreed_pro_version < 93) {
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002452 retcode = ERR_NEED_APV_93;
Andreas Gruenbacher7b4e4d32011-09-28 22:15:04 +02002453 goto fail_ldev;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002454 }
2455
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002456 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002457 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002458 rcu_read_unlock();
2459 if (u_size != (sector_t)rs.resize_size) {
2460 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
2461 if (!new_disk_conf) {
2462 retcode = ERR_NOMEM;
Philipp Reisner9bcd2522011-09-29 13:00:14 +02002463 goto fail_ldev;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002464 }
2465 }
2466
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002467 if (device->ldev->md.al_stripes != rs.al_stripes ||
2468 device->ldev->md.al_stripe_size_4k != rs.al_stripe_size / 4) {
Philipp Reisnerd752b262013-06-25 16:50:08 +02002469 u32 al_size_k = rs.al_stripes * rs.al_stripe_size;
2470
2471 if (al_size_k > (16 * 1024 * 1024)) {
2472 retcode = ERR_MD_LAYOUT_TOO_BIG;
2473 goto fail_ldev;
2474 }
2475
2476 if (al_size_k < MD_32kB_SECT/2) {
2477 retcode = ERR_MD_LAYOUT_TOO_SMALL;
2478 goto fail_ldev;
2479 }
2480
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002481 if (device->state.conn != C_CONNECTED) {
Philipp Reisnerd752b262013-06-25 16:50:08 +02002482 retcode = ERR_MD_LAYOUT_CONNECTED;
2483 goto fail_ldev;
2484 }
2485
2486 change_al_layout = true;
2487 }
2488
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002489 if (device->ldev->known_size != drbd_get_capacity(device->ldev->backing_bdev))
2490 device->ldev->known_size = drbd_get_capacity(device->ldev->backing_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002491
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002492 if (new_disk_conf) {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002493 mutex_lock(&first_peer_device(device)->connection->conf_update);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002494 old_disk_conf = device->ldev->disk_conf;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002495 *new_disk_conf = *old_disk_conf;
2496 new_disk_conf->disk_size = (sector_t)rs.resize_size;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002497 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002498 mutex_unlock(&first_peer_device(device)->connection->conf_update);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002499 synchronize_rcu();
2500 kfree(old_disk_conf);
2501 }
2502
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002503 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002504 dd = drbd_determine_dev_size(device, ddsf, change_al_layout ? &rs : NULL);
2505 drbd_md_sync(device);
2506 put_ldev(device);
Philipp Reisnere96c9632013-06-25 16:50:07 +02002507 if (dd == DS_ERROR) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002508 retcode = ERR_NOMEM_BITMAP;
2509 goto fail;
Philipp Reisnerd752b262013-06-25 16:50:08 +02002510 } else if (dd == DS_ERROR_SPACE_MD) {
2511 retcode = ERR_MD_LAYOUT_NO_FIT;
2512 goto fail;
2513 } else if (dd == DS_ERROR_SHRINK) {
2514 retcode = ERR_IMPLICIT_SHRINK;
2515 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002516 }
2517
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002518 if (device->state.conn == C_CONNECTED) {
Philipp Reisnere96c9632013-06-25 16:50:07 +02002519 if (dd == DS_GREW)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002520 set_bit(RESIZE_PENDING, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002521
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002522 drbd_send_uuids(device);
2523 drbd_send_sizes(device, 1, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002524 }
2525
2526 fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002527 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002528 return 0;
Andreas Gruenbacher7b4e4d32011-09-28 22:15:04 +02002529
2530 fail_ldev:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002531 put_ldev(device);
Andreas Gruenbacher7b4e4d32011-09-28 22:15:04 +02002532 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002533}
2534
Lars Ellenbergf3990022011-03-23 14:31:09 +01002535int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002536{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002537 enum drbd_ret_code retcode;
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002538 struct res_opts res_opts;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002539 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002540
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02002541 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002542 if (!adm_ctx.reply_skb)
2543 return retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002544 if (retcode != NO_ERROR)
2545 goto fail;
2546
Andreas Gruenbachereb6bea62011-06-21 16:11:28 +02002547 res_opts = adm_ctx.resource->res_opts;
Lars Ellenberg5979e362011-04-27 21:09:55 +02002548 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02002549 set_res_opts_defaults(&res_opts);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002550
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002551 err = res_opts_from_attrs(&res_opts, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02002552 if (err && err != -ENOMSG) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002553 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002554 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002555 goto fail;
2556 }
2557
Andreas Gruenbachereb6bea62011-06-21 16:11:28 +02002558 err = set_resource_options(adm_ctx.resource, &res_opts);
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02002559 if (err) {
2560 retcode = ERR_INVALID_REQUEST;
2561 if (err == -ENOMEM)
Philipp Reisner778f2712010-07-06 11:14:00 +02002562 retcode = ERR_NOMEM;
Philipp Reisner778f2712010-07-06 11:14:00 +02002563 }
2564
Philipp Reisnerb411b362009-09-25 16:07:19 -07002565fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002566 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002567 return 0;
2568}
2569
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002570int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002571{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002572 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002573 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2574
2575 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2576 if (!adm_ctx.reply_skb)
2577 return retcode;
2578 if (retcode != NO_ERROR)
2579 goto out;
2580
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002581 device = adm_ctx.device;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002582
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002583 /* If there is still bitmap IO pending, probably because of a previous
Lars Ellenberg7ee1fb92012-06-19 10:27:58 +02002584 * resync just being finished, wait for it before requesting a new resync.
2585 * Also wait for it's after_state_ch(). */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002586 drbd_suspend_io(device);
2587 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
2588 drbd_flush_workqueue(device);
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002589
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002590 /* If we happen to be C_STANDALONE R_SECONDARY, just change to
2591 * D_INCONSISTENT, and set all bits in the bitmap. Otherwise,
2592 * try to start a resync handshake as sync target for full sync.
Philipp Reisner9376d9f2013-03-27 14:08:36 +01002593 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002594 if (device->state.conn == C_STANDALONE && device->state.role == R_SECONDARY) {
2595 retcode = drbd_request_state(device, NS(disk, D_INCONSISTENT));
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002596 if (retcode >= SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002597 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002598 "set_n_write from invalidate", BM_LOCKED_MASK))
2599 retcode = ERR_IO_MD_DISK;
2600 }
2601 } else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002602 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_T));
2603 drbd_resume_io(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002604
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002605out:
2606 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002607 return 0;
2608}
2609
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002610static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2611 union drbd_state mask, union drbd_state val)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002612{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002613 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002614
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002615 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2616 if (!adm_ctx.reply_skb)
2617 return retcode;
2618 if (retcode != NO_ERROR)
2619 goto out;
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002620
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002621 retcode = drbd_request_state(adm_ctx.device, mask, val);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002622out:
2623 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002624 return 0;
2625}
2626
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002627static int drbd_bmio_set_susp_al(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002628{
2629 int rv;
2630
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002631 rv = drbd_bmio_set_n_write(device);
2632 drbd_suspend_al(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002633 return rv;
2634}
2635
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002636int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002637{
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002638 int retcode; /* drbd_ret_code, drbd_state_rv */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002639 struct drbd_device *device;
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002640
2641 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2642 if (!adm_ctx.reply_skb)
2643 return retcode;
2644 if (retcode != NO_ERROR)
2645 goto out;
2646
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002647 device = adm_ctx.device;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002648
2649 /* If there is still bitmap IO pending, probably because of a previous
Lars Ellenberg7ee1fb92012-06-19 10:27:58 +02002650 * resync just being finished, wait for it before requesting a new resync.
2651 * Also wait for it's after_state_ch(). */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002652 drbd_suspend_io(device);
2653 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
2654 drbd_flush_workqueue(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002655
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002656 /* If we happen to be C_STANDALONE R_PRIMARY, just set all bits
2657 * in the bitmap. Otherwise, try to start a resync handshake
2658 * as sync source for full sync.
2659 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002660 if (device->state.conn == C_STANDALONE && device->state.role == R_PRIMARY) {
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002661 /* The peer will get a resync upon connect anyways. Just make that
2662 into a full resync. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002663 retcode = drbd_request_state(device, NS(pdsk, D_INCONSISTENT));
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002664 if (retcode >= SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002665 if (drbd_bitmap_io(device, &drbd_bmio_set_susp_al,
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002666 "set_n_write from invalidate_peer",
2667 BM_LOCKED_SET_ALLOWED))
2668 retcode = ERR_IO_MD_DISK;
2669 }
2670 } else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002671 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_S));
2672 drbd_resume_io(device);
Philipp Reisner07782862010-08-31 12:00:50 +02002673
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002674out:
2675 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002676 return 0;
2677}
2678
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002679int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002680{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002681 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002682
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002683 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2684 if (!adm_ctx.reply_skb)
2685 return retcode;
2686 if (retcode != NO_ERROR)
2687 goto out;
2688
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002689 if (drbd_request_state(adm_ctx.device, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002690 retcode = ERR_PAUSE_IS_SET;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002691out:
2692 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002693 return 0;
2694}
2695
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002696int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002697{
Philipp Reisnerda9fbc22011-03-29 10:52:01 +02002698 union drbd_dev_state s;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002699 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002700
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002701 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2702 if (!adm_ctx.reply_skb)
2703 return retcode;
2704 if (retcode != NO_ERROR)
2705 goto out;
2706
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002707 if (drbd_request_state(adm_ctx.device, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2708 s = adm_ctx.device->state;
Philipp Reisnercd88d032011-01-20 11:46:41 +01002709 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2710 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2711 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2712 } else {
2713 retcode = ERR_PAUSE_IS_CLEAR;
2714 }
2715 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002716
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002717out:
2718 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002719 return 0;
2720}
2721
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002722int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002723{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002724 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002725}
2726
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002727int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002728{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002729 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002730 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2731
2732 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2733 if (!adm_ctx.reply_skb)
2734 return retcode;
2735 if (retcode != NO_ERROR)
2736 goto out;
2737
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002738 device = adm_ctx.device;
2739 if (test_bit(NEW_CUR_UUID, &device->flags)) {
2740 drbd_uuid_new_current(device);
2741 clear_bit(NEW_CUR_UUID, &device->flags);
Philipp Reisner43a51822010-06-11 11:26:34 +02002742 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002743 drbd_suspend_io(device);
2744 retcode = drbd_request_state(device, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002745 if (retcode == SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002746 if (device->state.conn < C_CONNECTED)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002747 tl_clear(first_peer_device(device)->connection);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002748 if (device->state.disk == D_DISKLESS || device->state.disk == D_FAILED)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002749 tl_restart(first_peer_device(device)->connection, FAIL_FROZEN_DISK_IO);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002750 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002751 drbd_resume_io(device);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002752
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002753out:
2754 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002755 return 0;
2756}
2757
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002758int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002759{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002760 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002761}
2762
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002763static int nla_put_drbd_cfg_context(struct sk_buff *skb,
2764 struct drbd_resource *resource,
2765 struct drbd_connection *connection,
2766 struct drbd_device *device)
Lars Ellenberg543cc102011-03-10 22:18:18 +01002767{
2768 struct nlattr *nla;
2769 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
2770 if (!nla)
2771 goto nla_put_failure;
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002772 if (device &&
2773 nla_put_u32(skb, T_ctx_volume, device->vnr))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002774 goto nla_put_failure;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002775 if (nla_put_string(skb, T_ctx_resource_name, connection->resource->name))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002776 goto nla_put_failure;
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002777 if (connection) {
2778 if (connection->my_addr_len &&
2779 nla_put(skb, T_ctx_my_addr, connection->my_addr_len, &connection->my_addr))
2780 goto nla_put_failure;
2781 if (connection->peer_addr_len &&
2782 nla_put(skb, T_ctx_peer_addr, connection->peer_addr_len, &connection->peer_addr))
2783 goto nla_put_failure;
2784 }
Lars Ellenberg543cc102011-03-10 22:18:18 +01002785 nla_nest_end(skb, nla);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002786 return 0;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002787
2788nla_put_failure:
2789 if (nla)
2790 nla_nest_cancel(skb, nla);
2791 return -EMSGSIZE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002792}
2793
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002794/*
2795 * Return the connection of @resource if @resource has exactly one connection.
2796 */
2797static struct drbd_connection *the_only_connection(struct drbd_resource *resource)
2798{
2799 struct list_head *connections = &resource->connections;
2800
2801 if (list_empty(connections) || connections->next->next != connections)
2802 return NULL;
2803 return list_first_entry(&resource->connections, struct drbd_connection, connections);
2804}
2805
2806int nla_put_status_info(struct sk_buff *skb, struct drbd_device *device,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002807 const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002808{
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002809 struct drbd_resource *resource = device->resource;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002810 struct state_info *si = NULL; /* for sizeof(si->member); */
2811 struct nlattr *nla;
2812 int got_ldev;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002813 int err = 0;
2814 int exclude_sensitive;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002815
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002816 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2817 * to. So we better exclude_sensitive information.
2818 *
2819 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2820 * in the context of the requesting user process. Exclude sensitive
2821 * information, unless current has superuser.
2822 *
2823 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2824 * relies on the current implementation of netlink_dump(), which
2825 * executes the dump callback successively from netlink_recvmsg(),
2826 * always in the context of the receiving process */
2827 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002828
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002829 got_ldev = get_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002830
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002831 /* We need to add connection name and volume number information still.
2832 * Minor number is in drbd_genlmsghdr. */
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002833 if (nla_put_drbd_cfg_context(skb, resource, the_only_connection(resource), device))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002834 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002835
Andreas Gruenbachereb6bea62011-06-21 16:11:28 +02002836 if (res_opts_to_skb(skb, &device->resource->res_opts, exclude_sensitive))
Lars Ellenbergf3990022011-03-23 14:31:09 +01002837 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002838
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002839 rcu_read_lock();
Andreas Gruenbacherf9eb7bf2013-06-25 16:50:05 +02002840 if (got_ldev) {
2841 struct disk_conf *disk_conf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002842
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002843 disk_conf = rcu_dereference(device->ldev->disk_conf);
Andreas Gruenbacherf9eb7bf2013-06-25 16:50:05 +02002844 err = disk_conf_to_skb(skb, disk_conf, exclude_sensitive);
2845 }
2846 if (!err) {
2847 struct net_conf *nc;
2848
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002849 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
Andreas Gruenbacherf9eb7bf2013-06-25 16:50:05 +02002850 if (nc)
2851 err = net_conf_to_skb(skb, nc, exclude_sensitive);
2852 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002853 rcu_read_unlock();
2854 if (err)
2855 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002856
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002857 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
2858 if (!nla)
2859 goto nla_put_failure;
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002860 if (nla_put_u32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY) ||
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002861 nla_put_u32(skb, T_current_state, device->state.i) ||
2862 nla_put_u64(skb, T_ed_uuid, device->ed_uuid) ||
2863 nla_put_u64(skb, T_capacity, drbd_get_capacity(device->this_bdev)) ||
2864 nla_put_u64(skb, T_send_cnt, device->send_cnt) ||
2865 nla_put_u64(skb, T_recv_cnt, device->recv_cnt) ||
2866 nla_put_u64(skb, T_read_cnt, device->read_cnt) ||
2867 nla_put_u64(skb, T_writ_cnt, device->writ_cnt) ||
2868 nla_put_u64(skb, T_al_writ_cnt, device->al_writ_cnt) ||
2869 nla_put_u64(skb, T_bm_writ_cnt, device->bm_writ_cnt) ||
2870 nla_put_u32(skb, T_ap_bio_cnt, atomic_read(&device->ap_bio_cnt)) ||
2871 nla_put_u32(skb, T_ap_pending_cnt, atomic_read(&device->ap_pending_cnt)) ||
2872 nla_put_u32(skb, T_rs_pending_cnt, atomic_read(&device->rs_pending_cnt)))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002873 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002874
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002875 if (got_ldev) {
Philipp Reisner39a1aa7f2012-08-08 21:19:09 +02002876 int err;
2877
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002878 spin_lock_irq(&device->ldev->md.uuid_lock);
2879 err = nla_put(skb, T_uuids, sizeof(si->uuids), device->ldev->md.uuid);
2880 spin_unlock_irq(&device->ldev->md.uuid_lock);
Philipp Reisner39a1aa7f2012-08-08 21:19:09 +02002881
2882 if (err)
2883 goto nla_put_failure;
2884
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002885 if (nla_put_u32(skb, T_disk_flags, device->ldev->md.flags) ||
2886 nla_put_u64(skb, T_bits_total, drbd_bm_bits(device)) ||
2887 nla_put_u64(skb, T_bits_oos, drbd_bm_total_weight(device)))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002888 goto nla_put_failure;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002889 if (C_SYNC_SOURCE <= device->state.conn &&
2890 C_PAUSED_SYNC_T >= device->state.conn) {
2891 if (nla_put_u64(skb, T_bits_rs_total, device->rs_total) ||
2892 nla_put_u64(skb, T_bits_rs_failed, device->rs_failed))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002893 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002894 }
2895 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002896
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002897 if (sib) {
2898 switch(sib->sib_reason) {
2899 case SIB_SYNC_PROGRESS:
2900 case SIB_GET_STATUS_REPLY:
2901 break;
2902 case SIB_STATE_CHANGE:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002903 if (nla_put_u32(skb, T_prev_state, sib->os.i) ||
2904 nla_put_u32(skb, T_new_state, sib->ns.i))
2905 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002906 break;
2907 case SIB_HELPER_POST:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002908 if (nla_put_u32(skb, T_helper_exit_code,
2909 sib->helper_exit_code))
2910 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002911 /* fall through */
2912 case SIB_HELPER_PRE:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002913 if (nla_put_string(skb, T_helper, sib->helper_name))
2914 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002915 break;
2916 }
2917 }
2918 nla_nest_end(skb, nla);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002919
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002920 if (0)
2921nla_put_failure:
2922 err = -EMSGSIZE;
2923 if (got_ldev)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002924 put_ldev(device);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002925 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002926}
2927
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002928int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002929{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002930 enum drbd_ret_code retcode;
2931 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002932
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002933 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2934 if (!adm_ctx.reply_skb)
2935 return retcode;
2936 if (retcode != NO_ERROR)
2937 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002938
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002939 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.device, NULL);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002940 if (err) {
2941 nlmsg_free(adm_ctx.reply_skb);
2942 return err;
2943 }
2944out:
2945 drbd_adm_finish(info, retcode);
2946 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002947}
2948
Rashika Kheria4b7a5302013-12-19 15:17:33 +05302949static int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002950{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002951 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002952 struct drbd_genlmsghdr *dh;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002953 struct drbd_resource *pos = (struct drbd_resource *)cb->args[0];
2954 struct drbd_resource *resource = NULL;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002955 struct drbd_resource *tmp;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002956 unsigned volume = cb->args[1];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002957
Lars Ellenberg543cc102011-03-10 22:18:18 +01002958 /* Open coded, deferred, iteration:
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002959 * for_each_resource_safe(resource, tmp, &drbd_resources) {
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002960 * connection = "first connection of resource or undefined";
2961 * idr_for_each_entry(&resource->devices, device, i) {
Lars Ellenberg543cc102011-03-10 22:18:18 +01002962 * ...
2963 * }
2964 * }
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002965 * where resource is cb->args[0];
Lars Ellenberg543cc102011-03-10 22:18:18 +01002966 * and i is cb->args[1];
2967 *
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002968 * cb->args[2] indicates if we shall loop over all resources,
2969 * or just dump all volumes of a single resource.
2970 *
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002971 * This may miss entries inserted after this dump started,
2972 * or entries deleted before they are reached.
Lars Ellenberg543cc102011-03-10 22:18:18 +01002973 *
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002974 * We need to make sure the device won't disappear while
Lars Ellenberg543cc102011-03-10 22:18:18 +01002975 * we are looking at it, and revalidate our iterators
2976 * on each iteration.
2977 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002978
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02002979 /* synchronize with conn_create()/drbd_destroy_connection() */
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02002980 rcu_read_lock();
Lars Ellenberg543cc102011-03-10 22:18:18 +01002981 /* revalidate iterator position */
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002982 for_each_resource_rcu(tmp, &drbd_resources) {
Lars Ellenberg543cc102011-03-10 22:18:18 +01002983 if (pos == NULL) {
2984 /* first iteration */
2985 pos = tmp;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002986 resource = pos;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002987 break;
2988 }
2989 if (tmp == pos) {
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002990 resource = pos;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002991 break;
2992 }
2993 }
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002994 if (resource) {
2995next_resource:
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002996 device = idr_get_next(&resource->devices, &volume);
2997 if (!device) {
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002998 /* No more volumes to dump on this resource.
2999 * Advance resource iterator. */
3000 pos = list_entry_rcu(resource->resources.next,
3001 struct drbd_resource, resources);
3002 /* Did we dump any volume of this resource yet? */
Lars Ellenberg543cc102011-03-10 22:18:18 +01003003 if (volume != 0) {
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003004 /* If we reached the end of the list,
3005 * or only a single resource dump was requested,
3006 * we are done. */
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003007 if (&pos->resources == &drbd_resources || cb->args[2])
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003008 goto out;
Lars Ellenberg543cc102011-03-10 22:18:18 +01003009 volume = 0;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003010 resource = pos;
3011 goto next_resource;
Lars Ellenberg543cc102011-03-10 22:18:18 +01003012 }
3013 }
3014
Philipp Reisner98683652012-11-09 14:18:43 +01003015 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003016 cb->nlh->nlmsg_seq, &drbd_genl_family,
3017 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
3018 if (!dh)
Lars Ellenberg543cc102011-03-10 22:18:18 +01003019 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003020
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02003021 if (!device) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003022 /* This is a connection without a single volume.
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003023 * Suprisingly enough, it may have a network
3024 * configuration. */
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02003025 struct drbd_connection *connection;
3026
Lars Ellenberg543cc102011-03-10 22:18:18 +01003027 dh->minor = -1U;
3028 dh->ret_code = NO_ERROR;
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02003029 connection = the_only_connection(resource);
3030 if (nla_put_drbd_cfg_context(skb, resource, connection, NULL))
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003031 goto cancel;
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02003032 if (connection) {
3033 struct net_conf *nc;
3034
3035 nc = rcu_dereference(connection->net_conf);
3036 if (nc && net_conf_to_skb(skb, nc, 1) != 0)
3037 goto cancel;
3038 }
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003039 goto done;
Lars Ellenberg543cc102011-03-10 22:18:18 +01003040 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003041
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +02003042 D_ASSERT(device, device->vnr == volume);
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02003043 D_ASSERT(device, device->resource == resource);
Lars Ellenberg543cc102011-03-10 22:18:18 +01003044
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003045 dh->minor = device_to_minor(device);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003046 dh->ret_code = NO_ERROR;
3047
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003048 if (nla_put_status_info(skb, device, NULL)) {
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003049cancel:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003050 genlmsg_cancel(skb, dh);
Lars Ellenberg543cc102011-03-10 22:18:18 +01003051 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003052 }
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003053done:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003054 genlmsg_end(skb, dh);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003055 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003056
Lars Ellenberg543cc102011-03-10 22:18:18 +01003057out:
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003058 rcu_read_unlock();
Lars Ellenberg543cc102011-03-10 22:18:18 +01003059 /* where to start the next iteration */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003060 cb->args[0] = (long)pos;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003061 cb->args[1] = (pos == resource) ? volume + 1 : 0;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003062
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003063 /* No more resources/volumes/minors found results in an empty skb.
Lars Ellenberg543cc102011-03-10 22:18:18 +01003064 * Which will terminate the dump. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003065 return skb->len;
3066}
3067
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003068/*
3069 * Request status of all resources, or of all volumes within a single resource.
3070 *
3071 * This is a dump, as the answer may not fit in a single reply skb otherwise.
3072 * Which means we cannot use the family->attrbuf or other such members, because
3073 * dump is NOT protected by the genl_lock(). During dump, we only have access
3074 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
3075 *
3076 * Once things are setup properly, we call into get_one_status().
Philipp Reisnerb411b362009-09-25 16:07:19 -07003077 */
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003078int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003079{
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003080 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
3081 struct nlattr *nla;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003082 const char *resource_name;
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003083 struct drbd_resource *resource;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003084 int maxtype;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003085
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003086 /* Is this a followup call? */
3087 if (cb->args[0]) {
3088 /* ... of a single resource dump,
3089 * and the resource iterator has been advanced already? */
3090 if (cb->args[2] && cb->args[2] != cb->args[0])
3091 return 0; /* DONE. */
3092 goto dump;
3093 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003094
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003095 /* First call (from netlink_dump_start). We need to figure out
3096 * which resource(s) the user wants us to dump. */
3097 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
3098 nlmsg_attrlen(cb->nlh, hdrlen),
3099 DRBD_NLA_CFG_CONTEXT);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003100
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003101 /* No explicit context given. Dump all. */
3102 if (!nla)
3103 goto dump;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003104 maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
3105 nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
3106 if (IS_ERR(nla))
3107 return PTR_ERR(nla);
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003108 /* context given, but no name present? */
3109 if (!nla)
3110 return -EINVAL;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003111 resource_name = nla_data(nla);
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003112 if (!*resource_name)
3113 return -ENODEV;
3114 resource = drbd_find_resource(resource_name);
3115 if (!resource)
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003116 return -ENODEV;
3117
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003118 kref_put(&resource->kref, drbd_destroy_resource); /* get_one_status() revalidates the resource */
Philipp Reisner0ace9df2011-04-24 10:53:19 +02003119
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003120 /* prime iterators, and set "filter" mode mark:
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003121 * only dump this connection. */
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003122 cb->args[0] = (long)resource;
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003123 /* cb->args[1] = 0; passed in this way. */
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003124 cb->args[2] = (long)resource;
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003125
3126dump:
3127 return get_one_status(skb, cb);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003128}
3129
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003130int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003131{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003132 enum drbd_ret_code retcode;
3133 struct timeout_parms tp;
3134 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003135
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003136 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3137 if (!adm_ctx.reply_skb)
3138 return retcode;
3139 if (retcode != NO_ERROR)
3140 goto out;
3141
3142 tp.timeout_type =
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003143 adm_ctx.device->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
3144 test_bit(USE_DEGR_WFC_T, &adm_ctx.device->flags) ? UT_DEGRADED :
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003145 UT_DEFAULT;
3146
3147 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
3148 if (err) {
3149 nlmsg_free(adm_ctx.reply_skb);
3150 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003151 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003152out:
3153 drbd_adm_finish(info, retcode);
3154 return 0;
3155}
Lars Ellenberg873b0d52011-01-21 22:53:48 +01003156
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003157int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
3158{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003159 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003160 enum drbd_ret_code retcode;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003161 struct start_ov_parms parms;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003162
3163 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3164 if (!adm_ctx.reply_skb)
3165 return retcode;
3166 if (retcode != NO_ERROR)
3167 goto out;
3168
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003169 device = adm_ctx.device;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003170
3171 /* resume from last known position, if possible */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003172 parms.ov_start_sector = device->ov_start_sector;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003173 parms.ov_stop_sector = ULLONG_MAX;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003174 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01003175 int err = start_ov_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003176 if (err) {
3177 retcode = ERR_MANDATORY_TAG;
3178 drbd_msg_put_info(from_attrs_err_to_txt(err));
3179 goto out;
3180 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003181 }
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003182 /* w_make_ov_request expects position to be aligned */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003183 device->ov_start_sector = parms.ov_start_sector & ~(BM_SECT_PER_BIT-1);
3184 device->ov_stop_sector = parms.ov_stop_sector;
Lars Ellenberg873b0d52011-01-21 22:53:48 +01003185
3186 /* If there is still bitmap IO pending, e.g. previous resync or verify
3187 * just being finished, wait for it before requesting a new resync. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003188 drbd_suspend_io(device);
3189 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
3190 retcode = drbd_request_state(device, NS(conn, C_VERIFY_S));
3191 drbd_resume_io(device);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003192out:
3193 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003194 return 0;
3195}
3196
3197
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003198int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003199{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003200 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003201 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003202 int skip_initial_sync = 0;
3203 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003204 struct new_c_uuid_parms args;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003205
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003206 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3207 if (!adm_ctx.reply_skb)
3208 return retcode;
3209 if (retcode != NO_ERROR)
3210 goto out_nolock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003211
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003212 device = adm_ctx.device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003213 memset(&args, 0, sizeof(args));
3214 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01003215 err = new_c_uuid_parms_from_attrs(&args, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003216 if (err) {
3217 retcode = ERR_MANDATORY_TAG;
3218 drbd_msg_put_info(from_attrs_err_to_txt(err));
3219 goto out_nolock;
3220 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003221 }
3222
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003223 mutex_lock(device->state_mutex); /* Protects us against serialized state changes. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003224
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003225 if (!get_ldev(device)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003226 retcode = ERR_NO_DISK;
3227 goto out;
3228 }
3229
3230 /* this is "skip initial sync", assume to be clean */
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02003231 if (device->state.conn == C_CONNECTED &&
3232 first_peer_device(device)->connection->agreed_pro_version >= 90 &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003233 device->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02003234 drbd_info(device, "Preparing to skip initial sync\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003235 skip_initial_sync = 1;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003236 } else if (device->state.conn != C_STANDALONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003237 retcode = ERR_CONNECTED;
3238 goto out_dec;
3239 }
3240
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003241 drbd_uuid_set(device, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
3242 drbd_uuid_new_current(device); /* New current, previous to UI_BITMAP */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003243
3244 if (args.clear_bm) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003245 err = drbd_bitmap_io(device, &drbd_bmio_clear_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003246 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003247 if (err) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02003248 drbd_err(device, "Writing bitmap failed with %d\n", err);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003249 retcode = ERR_IO_MD_DISK;
3250 }
3251 if (skip_initial_sync) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003252 drbd_send_uuids_skip_initial_sync(device);
3253 _drbd_uuid_set(device, UI_BITMAP, 0);
3254 drbd_print_uuids(device, "cleared bitmap UUID");
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02003255 spin_lock_irq(&first_peer_device(device)->connection->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003256 _drbd_set_state(_NS2(device, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
Philipp Reisnerb411b362009-09-25 16:07:19 -07003257 CS_VERBOSE, NULL);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02003258 spin_unlock_irq(&first_peer_device(device)->connection->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003259 }
3260 }
3261
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003262 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003263out_dec:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003264 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003265out:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003266 mutex_unlock(device->state_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003267out_nolock:
3268 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003269 return 0;
3270}
3271
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003272static enum drbd_ret_code
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003273drbd_check_resource_name(const char *name)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003274{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003275 if (!name || !name[0]) {
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003276 drbd_msg_put_info("resource name missing");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003277 return ERR_MANDATORY_TAG;
3278 }
3279 /* if we want to use these in sysfs/configfs/debugfs some day,
3280 * we must not allow slashes */
3281 if (strchr(name, '/')) {
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003282 drbd_msg_put_info("invalid resource name");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003283 return ERR_INVALID_REQUEST;
3284 }
3285 return NO_ERROR;
3286}
Philipp Reisnerb411b362009-09-25 16:07:19 -07003287
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003288int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003289{
3290 enum drbd_ret_code retcode;
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003291 struct res_opts res_opts;
3292 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003293
3294 retcode = drbd_adm_prepare(skb, info, 0);
3295 if (!adm_ctx.reply_skb)
3296 return retcode;
3297 if (retcode != NO_ERROR)
3298 goto out;
3299
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003300 set_res_opts_defaults(&res_opts);
3301 err = res_opts_from_attrs(&res_opts, info);
3302 if (err && err != -ENOMSG) {
3303 retcode = ERR_MANDATORY_TAG;
3304 drbd_msg_put_info(from_attrs_err_to_txt(err));
3305 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003306 }
3307
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003308 retcode = drbd_check_resource_name(adm_ctx.resource_name);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003309 if (retcode != NO_ERROR)
3310 goto out;
3311
Andreas Gruenbacher5c661042011-07-05 18:26:00 +02003312 if (adm_ctx.resource) {
Lars Ellenberg38f19612011-03-14 13:22:35 +01003313 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
3314 retcode = ERR_INVALID_REQUEST;
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003315 drbd_msg_put_info("resource exists");
Lars Ellenberg38f19612011-03-14 13:22:35 +01003316 }
3317 /* else: still NO_ERROR */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003318 goto out;
Philipp Reisner9f5180e2009-10-06 09:30:14 +02003319 }
3320
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003321 if (!conn_create(adm_ctx.resource_name, &res_opts))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003322 retcode = ERR_NOMEM;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003323out:
3324 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003325 return 0;
3326}
3327
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003328int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003329{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003330 struct drbd_genlmsghdr *dh = info->userhdr;
3331 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003332
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02003333 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003334 if (!adm_ctx.reply_skb)
3335 return retcode;
3336 if (retcode != NO_ERROR)
3337 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003338
Andreas Gruenbacherf2257a52011-07-14 16:00:40 +02003339 if (dh->minor > MINORMASK) {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003340 drbd_msg_put_info("requested minor out of range");
3341 retcode = ERR_INVALID_REQUEST;
3342 goto out;
3343 }
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +02003344 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003345 drbd_msg_put_info("requested volume id out of range");
3346 retcode = ERR_INVALID_REQUEST;
3347 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05003348 }
3349
Lars Ellenberg38f19612011-03-14 13:22:35 +01003350 /* drbd_adm_prepare made sure already
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02003351 * that first_peer_device(device)->connection and device->vnr match the request. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003352 if (adm_ctx.device) {
Lars Ellenberg38f19612011-03-14 13:22:35 +01003353 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
3354 retcode = ERR_MINOR_EXISTS;
3355 /* else: still NO_ERROR */
3356 goto out;
3357 }
3358
Andreas Gruenbacher59515a22011-07-06 14:20:49 +02003359 retcode = drbd_create_device(adm_ctx.resource, dh->minor, adm_ctx.volume);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003360out:
3361 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003362 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003363}
3364
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003365static enum drbd_ret_code adm_del_minor(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003366{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003367 if (device->state.disk == D_DISKLESS &&
3368 /* no need to be device->state.conn == C_STANDALONE &&
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003369 * we may want to delete a minor from a live replication group.
3370 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003371 device->state.role == R_SECONDARY) {
3372 _drbd_request_state(device, NS(conn, C_WF_REPORT_PARAMS),
Philipp Reisner369bea62011-07-06 23:04:44 +02003373 CS_VERBOSE + CS_WAIT_COMPLETE);
Andreas Gruenbacherf82795d2011-07-03 23:32:26 +02003374 drbd_delete_device(device);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003375 return NO_ERROR;
3376 } else
3377 return ERR_MINOR_CONFIGURED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003378}
3379
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003380int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05003381{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003382 enum drbd_ret_code retcode;
3383
3384 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3385 if (!adm_ctx.reply_skb)
3386 return retcode;
3387 if (retcode != NO_ERROR)
3388 goto out;
3389
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003390 retcode = adm_del_minor(adm_ctx.device);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003391out:
3392 drbd_adm_finish(info, retcode);
3393 return 0;
3394}
3395
3396int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
3397{
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003398 struct drbd_resource *resource;
3399 struct drbd_connection *connection;
3400 struct drbd_device *device;
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003401 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003402 unsigned i;
3403
Andreas Gruenbachera10f6b82011-06-09 01:26:16 +02003404 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003405 if (!adm_ctx.reply_skb)
3406 return retcode;
3407 if (retcode != NO_ERROR)
3408 goto out;
3409
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003410 resource = adm_ctx.resource;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003411 /* demote */
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003412 for_each_connection(connection, resource) {
3413 struct drbd_peer_device *peer_device;
3414
3415 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
3416 retcode = drbd_set_role(peer_device->device, R_SECONDARY, 0);
3417 if (retcode < SS_SUCCESS) {
3418 drbd_msg_put_info("failed to demote");
3419 goto out;
3420 }
3421 }
3422
3423 retcode = conn_try_disconnect(connection, 0);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003424 if (retcode < SS_SUCCESS) {
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003425 drbd_msg_put_info("failed to disconnect");
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003426 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003427 }
3428 }
3429
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003430 /* detach */
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003431 idr_for_each_entry(&resource->devices, device, i) {
3432 retcode = adm_detach(device, 0);
Lars Ellenberg27012382012-07-24 10:13:55 +02003433 if (retcode < SS_SUCCESS || retcode > NO_ERROR) {
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003434 drbd_msg_put_info("failed to detach");
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003435 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003436 }
3437 }
3438
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003439 /* If we reach this, all volumes (of this connection) are Secondary,
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003440 * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003441 * actually stopped, state handling only does drbd_thread_stop_nowait(). */
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003442 for_each_connection(connection, resource)
3443 drbd_thread_stop(&connection->worker);
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003444
3445 /* Now, nothing can fail anymore */
3446
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003447 /* delete volumes */
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003448 idr_for_each_entry(&resource->devices, device, i) {
3449 retcode = adm_del_minor(device);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003450 if (retcode != NO_ERROR) {
3451 /* "can not happen" */
3452 drbd_msg_put_info("failed to delete volume");
Philipp Reisneref356262011-04-13 14:21:29 -07003453 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003454 }
3455 }
3456
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003457 list_del_rcu(&resource->resources);
3458 synchronize_rcu();
3459 drbd_free_resource(resource);
3460 retcode = NO_ERROR;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003461
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003462out:
3463 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003464 return 0;
3465}
3466
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003467int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05003468{
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003469 struct drbd_resource *resource;
3470 struct drbd_connection *connection;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003471 enum drbd_ret_code retcode;
3472
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02003473 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003474 if (!adm_ctx.reply_skb)
3475 return retcode;
3476 if (retcode != NO_ERROR)
3477 goto out;
3478
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003479 resource = adm_ctx.resource;
3480 for_each_connection(connection, resource) {
3481 if (connection->cstate > C_STANDALONE) {
3482 retcode = ERR_NET_CONFIGURED;
3483 goto out;
3484 }
3485 }
3486 if (!idr_is_empty(&resource->devices)) {
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003487 retcode = ERR_RES_IN_USE;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003488 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05003489 }
3490
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003491 list_del_rcu(&resource->resources);
3492 for_each_connection(connection, resource)
3493 drbd_thread_stop(&connection->worker);
3494 synchronize_rcu();
3495 drbd_free_resource(resource);
3496 retcode = NO_ERROR;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003497out:
3498 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003499 return 0;
3500}
3501
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003502void drbd_bcast_event(struct drbd_device *device, const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003503{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003504 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
3505 struct sk_buff *msg;
3506 struct drbd_genlmsghdr *d_out;
3507 unsigned seq;
3508 int err = -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003509
Philipp Reisneref86b772012-12-06 10:34:34 +01003510 if (sib->sib_reason == SIB_SYNC_PROGRESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003511 if (time_after(jiffies, device->rs_last_bcast + HZ))
3512 device->rs_last_bcast = jiffies;
Philipp Reisneref86b772012-12-06 10:34:34 +01003513 else
3514 return;
3515 }
Philipp Reisner328e0f122012-10-19 14:37:47 +02003516
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003517 seq = atomic_inc_return(&drbd_genl_seq);
3518 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
3519 if (!msg)
3520 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003521
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003522 err = -EMSGSIZE;
3523 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
3524 if (!d_out) /* cannot happen, but anyways. */
3525 goto nla_put_failure;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003526 d_out->minor = device_to_minor(device);
Andreas Gruenbacher6f9b5f82011-05-06 01:03:32 +02003527 d_out->ret_code = NO_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003528
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003529 if (nla_put_status_info(msg, device, sib))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003530 goto nla_put_failure;
3531 genlmsg_end(msg, d_out);
3532 err = drbd_genl_multicast_events(msg, 0);
3533 /* msg has been consumed or freed in netlink_broadcast() */
3534 if (err && err != -ESRCH)
3535 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003536
Philipp Reisnerb411b362009-09-25 16:07:19 -07003537 return;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003538
3539nla_put_failure:
3540 nlmsg_free(msg);
3541failed:
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02003542 drbd_err(device, "Error %d while broadcasting event. "
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003543 "Event seq:%u sib_reason:%u\n",
3544 err, seq, sib->sib_reason);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003545}