blob: 49a0f2ae64548760ac3cc503ef5964b0d18194ed [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
Philipp Reisnerb411b362009-09-25 16:07:19 -0700354 dev_info(DEV, "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)
360 dev_warn(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
361 usermode_helper, cmd, mb,
362 (ret >> 8) & 0xff, ret);
363 else
364 dev_info(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
365 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 Gruenbacher77c556f2011-06-08 22:17:38 +0200395 conn_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 Gruenbacherbde89a92011-05-30 16:32:41 +0200400 conn_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 Gruenbacherbde89a92011-05-30 16:32:41 +0200404 conn_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 Gruenbacherb30ab792011-07-03 13:26:43 +0200418 struct drbd_device *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 Gruenbacherbde89a92011-05-30 16:32:41 +0200422 idr_for_each_entry(&connection->volumes, device, vnr) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200423 if (get_ldev_if_state(device, D_CONSISTENT)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200424 fp = max_t(enum drbd_fencing_p, fp,
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200425 rcu_dereference(device->ldev->disk_conf)->fencing);
426 put_ldev(device);
Philipp Reisnercb703452011-03-24 11:03:07 +0100427 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700428 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700429 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700430
Philipp Reisnercb703452011-03-24 11:03:07 +0100431 return fp;
432}
433
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200434bool conn_try_outdate_peer(struct drbd_connection *connection)
Philipp Reisnercb703452011-03-24 11:03:07 +0100435{
Philipp Reisner28e448b2013-06-25 16:50:06 +0200436 unsigned int connect_cnt;
Philipp Reisnercb703452011-03-24 11:03:07 +0100437 union drbd_state mask = { };
438 union drbd_state val = { };
439 enum drbd_fencing_p fp;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700440 char *ex_to_string;
441 int r;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700442
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200443 if (connection->cstate >= C_WF_REPORT_PARAMS) {
444 conn_err(connection, "Expected cstate < C_WF_REPORT_PARAMS\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100445 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700446 }
447
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200448 spin_lock_irq(&connection->req_lock);
449 connect_cnt = connection->connect_cnt;
450 spin_unlock_irq(&connection->req_lock);
Philipp Reisner28e448b2013-06-25 16:50:06 +0200451
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200452 fp = highest_fencing_policy(connection);
Philipp Reisnercb703452011-03-24 11:03:07 +0100453 switch (fp) {
454 case FP_NOT_AVAIL:
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200455 conn_warn(connection, "Not fencing peer, I'm not even Consistent myself.\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100456 goto out;
457 case FP_DONT_CARE:
458 return true;
459 default: ;
460 }
461
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200462 r = conn_khelper(connection, "fence-peer");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700463
464 switch ((r>>8) & 0xff) {
465 case 3: /* peer is inconsistent */
466 ex_to_string = "peer is inconsistent or worse";
Philipp Reisnercb703452011-03-24 11:03:07 +0100467 mask.pdsk = D_MASK;
468 val.pdsk = D_INCONSISTENT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700469 break;
470 case 4: /* peer got outdated, or was already outdated */
471 ex_to_string = "peer was fenced";
Philipp Reisnercb703452011-03-24 11:03:07 +0100472 mask.pdsk = D_MASK;
473 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700474 break;
475 case 5: /* peer was down */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200476 if (conn_highest_disk(connection) == D_UP_TO_DATE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700477 /* we will(have) create(d) a new UUID anyways... */
478 ex_to_string = "peer is unreachable, assumed to be dead";
Philipp Reisnercb703452011-03-24 11:03:07 +0100479 mask.pdsk = D_MASK;
480 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700481 } else {
482 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
Philipp Reisnerb411b362009-09-25 16:07:19 -0700483 }
484 break;
485 case 6: /* Peer is primary, voluntarily outdate myself.
486 * This is useful when an unconnected R_SECONDARY is asked to
487 * become R_PRIMARY, but finds the other peer being active. */
488 ex_to_string = "peer is active";
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200489 conn_warn(connection, "Peer is primary, outdating myself.\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100490 mask.disk = D_MASK;
491 val.disk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700492 break;
493 case 7:
494 if (fp != FP_STONITH)
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200495 conn_err(connection, "fence-peer() = 7 && fencing != Stonith !!!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700496 ex_to_string = "peer was stonithed";
Philipp Reisnercb703452011-03-24 11:03:07 +0100497 mask.pdsk = D_MASK;
498 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700499 break;
500 default:
501 /* The script is broken ... */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200502 conn_err(connection, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
Philipp Reisnercb703452011-03-24 11:03:07 +0100503 return false; /* Eventually leave IO frozen */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700504 }
505
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200506 conn_info(connection, "fence-peer helper returned %d (%s)\n",
Philipp Reisnercb703452011-03-24 11:03:07 +0100507 (r>>8) & 0xff, ex_to_string);
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200508
Philipp Reisnercb703452011-03-24 11:03:07 +0100509 out:
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200510
Philipp Reisnercb703452011-03-24 11:03:07 +0100511 /* Not using
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200512 conn_request_state(connection, mask, val, CS_VERBOSE);
Philipp Reisnercb703452011-03-24 11:03:07 +0100513 here, because we might were able to re-establish the connection in the
514 meantime. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200515 spin_lock_irq(&connection->req_lock);
516 if (connection->cstate < C_WF_REPORT_PARAMS && !test_bit(STATE_SENT, &connection->flags)) {
517 if (connection->connect_cnt != connect_cnt)
Philipp Reisner28e448b2013-06-25 16:50:06 +0200518 /* In case the connection was established and droped
519 while the fence-peer handler was running, ignore it */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200520 conn_info(connection, "Ignoring fence-peer exit code\n");
Philipp Reisner28e448b2013-06-25 16:50:06 +0200521 else
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200522 _conn_request_state(connection, mask, val, CS_VERBOSE);
Philipp Reisner28e448b2013-06-25 16:50:06 +0200523 }
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200524 spin_unlock_irq(&connection->req_lock);
Philipp Reisnercb703452011-03-24 11:03:07 +0100525
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200526 return conn_highest_pdsk(connection) <= D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700527}
528
Philipp Reisner87f7be42010-06-11 13:56:33 +0200529static int _try_outdate_peer_async(void *data)
530{
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200531 struct drbd_connection *connection = (struct drbd_connection *)data;
Philipp Reisner87f7be42010-06-11 13:56:33 +0200532
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200533 conn_try_outdate_peer(connection);
Philipp Reisner21423fa2011-05-17 14:19:41 +0200534
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +0200535 kref_put(&connection->kref, drbd_destroy_connection);
Philipp Reisner87f7be42010-06-11 13:56:33 +0200536 return 0;
537}
538
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200539void conn_try_outdate_peer_async(struct drbd_connection *connection)
Philipp Reisner87f7be42010-06-11 13:56:33 +0200540{
541 struct task_struct *opa;
542
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200543 kref_get(&connection->kref);
544 opa = kthread_run(_try_outdate_peer_async, connection, "drbd_async_h");
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200545 if (IS_ERR(opa)) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200546 conn_err(connection, "out of mem, failed to invoke fence-peer helper\n");
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +0200547 kref_put(&connection->kref, drbd_destroy_connection);
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200548 }
Philipp Reisner87f7be42010-06-11 13:56:33 +0200549}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700550
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100551enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200552drbd_set_role(struct drbd_device *device, enum drbd_role new_role, int force)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700553{
554 const int max_tries = 4;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100555 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200556 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700557 int try = 0;
558 int forced = 0;
559 union drbd_state mask, val;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700560
561 if (new_role == R_PRIMARY)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200562 request_ping(first_peer_device(device)->connection); /* Detect a dead peer ASAP */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700563
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200564 mutex_lock(device->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700565
566 mask.i = 0; mask.role = R_MASK;
567 val.i = 0; val.role = new_role;
568
569 while (try++ < max_tries) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200570 rv = _drbd_request_state(device, mask, val, CS_WAIT_COMPLETE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700571
572 /* in case we first succeeded to outdate,
573 * but now suddenly could establish a connection */
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100574 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700575 val.pdsk = 0;
576 mask.pdsk = 0;
577 continue;
578 }
579
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100580 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200581 (device->state.disk < D_UP_TO_DATE &&
582 device->state.disk >= D_INCONSISTENT)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700583 mask.disk = D_MASK;
584 val.disk = D_UP_TO_DATE;
585 forced = 1;
586 continue;
587 }
588
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100589 if (rv == SS_NO_UP_TO_DATE_DISK &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200590 device->state.disk == D_CONSISTENT && mask.pdsk == 0) {
591 D_ASSERT(device->state.pdsk == D_UNKNOWN);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700592
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200593 if (conn_try_outdate_peer(first_peer_device(device)->connection)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700594 val.disk = D_UP_TO_DATE;
595 mask.disk = D_MASK;
596 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700597 continue;
598 }
599
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100600 if (rv == SS_NOTHING_TO_DO)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100601 goto out;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100602 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200603 if (!conn_try_outdate_peer(first_peer_device(device)->connection) && force) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700604 dev_warn(DEV, "Forced into split brain situation!\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100605 mask.pdsk = D_MASK;
606 val.pdsk = D_OUTDATED;
607
Philipp Reisnerb411b362009-09-25 16:07:19 -0700608 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700609 continue;
610 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100611 if (rv == SS_TWO_PRIMARIES) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700612 /* Maybe the peer is detected as dead very soon...
613 retry at most once more in this case. */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200614 int timeo;
615 rcu_read_lock();
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200616 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +0200617 timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
618 rcu_read_unlock();
619 schedule_timeout_interruptible(timeo);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700620 if (try < max_tries)
621 try = max_tries - 1;
622 continue;
623 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100624 if (rv < SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200625 rv = _drbd_request_state(device, mask, val,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700626 CS_VERBOSE + CS_WAIT_COMPLETE);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100627 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100628 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700629 }
630 break;
631 }
632
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100633 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100634 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700635
636 if (forced)
637 dev_warn(DEV, "Forced to consider local data as UpToDate!\n");
638
639 /* Wait until nothing is on the fly :) */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200640 wait_event(device->misc_wait, atomic_read(&device->ap_pending_cnt) == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700641
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100642 /* FIXME also wait for all pending P_BARRIER_ACK? */
643
Philipp Reisnerb411b362009-09-25 16:07:19 -0700644 if (new_role == R_SECONDARY) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200645 set_disk_ro(device->vdisk, true);
646 if (get_ldev(device)) {
647 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
648 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700649 }
650 } else {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200651 mutex_lock(&first_peer_device(device)->connection->conf_update);
652 nc = first_peer_device(device)->connection->net_conf;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200653 if (nc)
Andreas Gruenbacher6139f602011-05-06 20:00:02 +0200654 nc->discard_my_data = 0; /* without copy; single bit op is atomic */
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200655 mutex_unlock(&first_peer_device(device)->connection->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +0200656
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200657 set_disk_ro(device->vdisk, false);
658 if (get_ldev(device)) {
659 if (((device->state.conn < C_CONNECTED ||
660 device->state.pdsk <= D_FAILED)
661 && device->ldev->md.uuid[UI_BITMAP] == 0) || forced)
662 drbd_uuid_new_current(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700663
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200664 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
665 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700666 }
667 }
668
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100669 /* writeout of activity log covered areas of the bitmap
670 * to stable storage done in after state change already */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700671
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200672 if (device->state.conn >= C_WF_REPORT_PARAMS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700673 /* if this was forced, we should consider sync */
674 if (forced)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200675 drbd_send_uuids(device);
676 drbd_send_current_state(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700677 }
678
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200679 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700680
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200681 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100682out:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200683 mutex_unlock(device->state_mutex);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100684 return rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700685}
686
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100687static const char *from_attrs_err_to_txt(int err)
Lars Ellenbergef50a3e2010-09-01 14:39:30 +0200688{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100689 return err == -ENOMSG ? "required attribute missing" :
690 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
Lars Ellenbergf3990022011-03-23 14:31:09 +0100691 err == -EEXIST ? "can not change invariant setting" :
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100692 "invalid attribute value";
Lars Ellenbergef50a3e2010-09-01 14:39:30 +0200693}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700694
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100695int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700696{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100697 struct set_role_parms parms;
698 int err;
699 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700700
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100701 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
702 if (!adm_ctx.reply_skb)
703 return retcode;
704 if (retcode != NO_ERROR)
705 goto out;
706
707 memset(&parms, 0, sizeof(parms));
708 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +0100709 err = set_role_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100710 if (err) {
711 retcode = ERR_MANDATORY_TAG;
712 drbd_msg_put_info(from_attrs_err_to_txt(err));
713 goto out;
714 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700715 }
716
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100717 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200718 retcode = drbd_set_role(adm_ctx.device, R_PRIMARY, parms.assume_uptodate);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100719 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200720 retcode = drbd_set_role(adm_ctx.device, R_SECONDARY, 0);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100721out:
722 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700723 return 0;
724}
725
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100726/* Initializes the md.*_offset members, so we are able to find
727 * the on disk meta data.
728 *
729 * We currently have two possible layouts:
730 * external:
731 * |----------- md_size_sect ------------------|
732 * [ 4k superblock ][ activity log ][ Bitmap ]
733 * | al_offset == 8 |
734 * | bm_offset = al_offset + X |
735 * ==> bitmap sectors = md_size_sect - bm_offset
736 *
737 * internal:
738 * |----------- md_size_sect ------------------|
739 * [data.....][ Bitmap ][ activity log ][ 4k superblock ]
740 * | al_offset < 0 |
741 * | bm_offset = al_offset - Y |
742 * ==> bitmap sectors = Y = al_offset - bm_offset
743 *
744 * Activity log size used to be fixed 32kB,
745 * but is about to become configurable.
746 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200747static void drbd_md_set_sector_offsets(struct drbd_device *device,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700748 struct drbd_backing_dev *bdev)
749{
750 sector_t md_size_sect = 0;
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +0100751 unsigned int al_size_sect = bdev->md.al_size_4k * 8;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200752
Lars Ellenberg3a4d4eb2013-03-19 18:16:44 +0100753 bdev->md.md_offset = drbd_md_ss(bdev);
754
Lars Ellenberg68e41a42013-03-19 18:16:45 +0100755 switch (bdev->md.meta_dev_idx) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700756 default:
757 /* v07 style fixed size indexed meta data */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100758 bdev->md.md_size_sect = MD_128MB_SECT;
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100759 bdev->md.al_offset = MD_4kB_SECT;
760 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700761 break;
762 case DRBD_MD_INDEX_FLEX_EXT:
763 /* just occupy the full device; unit: sectors */
764 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100765 bdev->md.al_offset = MD_4kB_SECT;
766 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700767 break;
768 case DRBD_MD_INDEX_INTERNAL:
769 case DRBD_MD_INDEX_FLEX_INT:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700770 /* al size is still fixed */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100771 bdev->md.al_offset = -al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700772 /* we need (slightly less than) ~ this much bitmap sectors: */
773 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
774 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
775 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
776 md_size_sect = ALIGN(md_size_sect, 8);
777
778 /* plus the "drbd meta data super block",
779 * and the activity log; */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100780 md_size_sect += MD_4kB_SECT + al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700781
782 bdev->md.md_size_sect = md_size_sect;
783 /* bitmap offset is adjusted by 'super' block size */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100784 bdev->md.bm_offset = -md_size_sect + MD_4kB_SECT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700785 break;
786 }
787}
788
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100789/* input size is expected to be in KB */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700790char *ppsize(char *buf, unsigned long long size)
791{
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100792 /* Needs 9 bytes at max including trailing NUL:
793 * -1ULL ==> "16384 EB" */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700794 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
795 int base = 0;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100796 while (size >= 10000 && base < sizeof(units)-1) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700797 /* shift + round */
798 size = (size >> 10) + !!(size & (1<<9));
799 base++;
800 }
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100801 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700802
803 return buf;
804}
805
806/* there is still a theoretical deadlock when called from receiver
807 * on an D_INCONSISTENT R_PRIMARY:
808 * remote READ does inc_ap_bio, receiver would need to receive answer
809 * packet from remote to dec_ap_bio again.
810 * receiver receive_sizes(), comes here,
811 * waits for ap_bio_cnt == 0. -> deadlock.
812 * but this cannot happen, actually, because:
813 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
814 * (not connected, or bad/no disk on peer):
815 * see drbd_fail_request_early, ap_bio_cnt is zero.
816 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
817 * peer may not initiate a resize.
818 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100819/* Note these are not to be confused with
820 * drbd_adm_suspend_io/drbd_adm_resume_io,
821 * which are (sub) state changes triggered by admin (drbdsetup),
822 * and can be long lived.
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200823 * This changes an device->flag, is triggered by drbd internals,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100824 * and should be short-lived. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200825void drbd_suspend_io(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700826{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200827 set_bit(SUSPEND_IO, &device->flags);
828 if (drbd_suspended(device))
Philipp Reisner265be2d2010-05-31 10:14:17 +0200829 return;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200830 wait_event(device->misc_wait, !atomic_read(&device->ap_bio_cnt));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700831}
832
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200833void drbd_resume_io(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700834{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200835 clear_bit(SUSPEND_IO, &device->flags);
836 wake_up(&device->misc_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700837}
838
839/**
840 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200841 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700842 *
843 * Returns 0 on success, negative return values indicate errors.
844 * You should call drbd_md_sync() after calling this function.
845 */
Philipp Reisnerd752b262013-06-25 16:50:08 +0200846enum determine_dev_size
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200847drbd_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 -0700848{
849 sector_t prev_first_sect, prev_size; /* previous meta location */
Lars Ellenbergcccac982013-03-19 18:16:46 +0100850 sector_t la_size_sect, u_size;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200851 struct drbd_md *md = &device->ldev->md;
Philipp Reisnerd752b262013-06-25 16:50:08 +0200852 u32 prev_al_stripe_size_4k;
853 u32 prev_al_stripes;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700854 sector_t size;
855 char ppb[10];
Philipp Reisnerd752b262013-06-25 16:50:08 +0200856 void *buffer;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700857
858 int md_moved, la_size_changed;
Philipp Reisnere96c9632013-06-25 16:50:07 +0200859 enum determine_dev_size rv = DS_UNCHANGED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700860
861 /* race:
862 * application request passes inc_ap_bio,
863 * but then cannot get an AL-reference.
864 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
865 *
866 * to avoid that:
867 * Suspend IO right here.
868 * still lock the act_log to not trigger ASSERTs there.
869 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200870 drbd_suspend_io(device);
871 buffer = drbd_md_get_buffer(device); /* Lock meta-data IO */
Philipp Reisnerd752b262013-06-25 16:50:08 +0200872 if (!buffer) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200873 drbd_resume_io(device);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200874 return DS_ERROR;
875 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700876
877 /* no wait necessary anymore, actually we could assert that */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200878 wait_event(device->al_wait, lc_try_lock(device->act_log));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700879
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200880 prev_first_sect = drbd_md_first_sector(device->ldev);
881 prev_size = device->ldev->md.md_size_sect;
882 la_size_sect = device->ldev->md.la_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700883
Philipp Reisnerd752b262013-06-25 16:50:08 +0200884 if (rs) {
885 /* rs is non NULL if we should change the AL layout only */
886
887 prev_al_stripes = md->al_stripes;
888 prev_al_stripe_size_4k = md->al_stripe_size_4k;
889
890 md->al_stripes = rs->al_stripes;
891 md->al_stripe_size_4k = rs->al_stripe_size / 4;
892 md->al_size_4k = (u64)rs->al_stripes * rs->al_stripe_size / 4;
893 }
894
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200895 drbd_md_set_sector_offsets(device, device->ldev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700896
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200897 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200898 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200899 rcu_read_unlock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200900 size = drbd_new_dev_size(device, device->ldev, u_size, flags & DDSF_FORCED);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700901
Philipp Reisnerd752b262013-06-25 16:50:08 +0200902 if (size < la_size_sect) {
903 if (rs && u_size == 0) {
904 /* Remove "rs &&" later. This check should always be active, but
905 right now the receiver expects the permissive behavior */
906 dev_warn(DEV, "Implicit shrink not allowed. "
907 "Use --size=%llus for explicit shrink.\n",
908 (unsigned long long)size);
909 rv = DS_ERROR_SHRINK;
910 }
911 if (u_size > size)
912 rv = DS_ERROR_SPACE_MD;
913 if (rv != DS_UNCHANGED)
914 goto err_out;
915 }
916
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200917 if (drbd_get_capacity(device->this_bdev) != size ||
918 drbd_bm_capacity(device) != size) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700919 int err;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200920 err = drbd_bm_resize(device, size, !(flags & DDSF_NO_RESYNC));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700921 if (unlikely(err)) {
922 /* currently there is only one error: ENOMEM! */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200923 size = drbd_bm_capacity(device)>>1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700924 if (size == 0) {
925 dev_err(DEV, "OUT OF MEMORY! "
926 "Could not allocate bitmap!\n");
927 } else {
928 dev_err(DEV, "BM resizing failed. "
929 "Leaving size unchanged at size = %lu KB\n",
930 (unsigned long)size);
931 }
Philipp Reisnere96c9632013-06-25 16:50:07 +0200932 rv = DS_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700933 }
934 /* racy, see comments above. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200935 drbd_set_my_capacity(device, size);
936 device->ldev->md.la_size_sect = size;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700937 dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
938 (unsigned long long)size>>1);
939 }
Philipp Reisnerd752b262013-06-25 16:50:08 +0200940 if (rv <= DS_ERROR)
941 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700942
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200943 la_size_changed = (la_size_sect != device->ldev->md.la_size_sect);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700944
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200945 md_moved = prev_first_sect != drbd_md_first_sector(device->ldev)
946 || prev_size != device->ldev->md.md_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700947
Philipp Reisnerd752b262013-06-25 16:50:08 +0200948 if (la_size_changed || md_moved || rs) {
949 u32 prev_flags;
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100950
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200951 drbd_al_shrink(device); /* All extents inactive. */
Philipp Reisnerd752b262013-06-25 16:50:08 +0200952
953 prev_flags = md->flags;
954 md->flags &= ~MDF_PRIMARY_IND;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200955 drbd_md_write(device, buffer);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200956
Philipp Reisnerb411b362009-09-25 16:07:19 -0700957 dev_info(DEV, "Writing the whole bitmap, %s\n",
958 la_size_changed && md_moved ? "size changed and md moved" :
959 la_size_changed ? "size changed" : "md moved");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100960 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200961 drbd_bitmap_io(device, md_moved ? &drbd_bm_write_all : &drbd_bm_write,
Philipp Reisnerd752b262013-06-25 16:50:08 +0200962 "size changed", BM_LOCKED_MASK);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200963 drbd_initialize_al(device, buffer);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200964
965 md->flags = prev_flags;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200966 drbd_md_write(device, buffer);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200967
968 if (rs)
969 dev_info(DEV, "Changed AL layout to al-stripes = %d, al-stripe-size-kB = %d\n",
970 md->al_stripes, md->al_stripe_size_4k * 4);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700971 }
972
Lars Ellenbergcccac982013-03-19 18:16:46 +0100973 if (size > la_size_sect)
Philipp Reisner57737ad2013-10-23 10:59:17 +0200974 rv = la_size_sect ? DS_GREW : DS_GREW_FROM_ZERO;
Lars Ellenbergcccac982013-03-19 18:16:46 +0100975 if (size < la_size_sect)
Philipp Reisnere96c9632013-06-25 16:50:07 +0200976 rv = DS_SHRUNK;
Philipp Reisnerd752b262013-06-25 16:50:08 +0200977
978 if (0) {
979 err_out:
980 if (rs) {
981 md->al_stripes = prev_al_stripes;
982 md->al_stripe_size_4k = prev_al_stripe_size_4k;
983 md->al_size_4k = (u64)prev_al_stripes * prev_al_stripe_size_4k;
984
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200985 drbd_md_set_sector_offsets(device, device->ldev);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200986 }
987 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200988 lc_unlock(device->act_log);
989 wake_up(&device->al_wait);
990 drbd_md_put_buffer(device);
991 drbd_resume_io(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700992
993 return rv;
994}
995
996sector_t
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200997drbd_new_dev_size(struct drbd_device *device, struct drbd_backing_dev *bdev,
Philipp Reisneref5e44a2011-05-03 13:27:43 +0200998 sector_t u_size, int assume_peer_has_space)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700999{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001000 sector_t p_size = device->p_size; /* partner's disk size. */
Lars Ellenbergcccac982013-03-19 18:16:46 +01001001 sector_t la_size_sect = bdev->md.la_size_sect; /* last agreed size. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001002 sector_t m_size; /* my size */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001003 sector_t size = 0;
1004
1005 m_size = drbd_get_max_capacity(bdev);
1006
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001007 if (device->state.conn < C_CONNECTED && assume_peer_has_space) {
Philipp Reisnera393db62009-12-22 13:35:52 +01001008 dev_warn(DEV, "Resize while not connected was forced by the user!\n");
1009 p_size = m_size;
1010 }
1011
Philipp Reisnerb411b362009-09-25 16:07:19 -07001012 if (p_size && m_size) {
1013 size = min_t(sector_t, p_size, m_size);
1014 } else {
Lars Ellenbergcccac982013-03-19 18:16:46 +01001015 if (la_size_sect) {
1016 size = la_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001017 if (m_size && m_size < size)
1018 size = m_size;
1019 if (p_size && p_size < size)
1020 size = p_size;
1021 } else {
1022 if (m_size)
1023 size = m_size;
1024 if (p_size)
1025 size = p_size;
1026 }
1027 }
1028
1029 if (size == 0)
1030 dev_err(DEV, "Both nodes diskless!\n");
1031
1032 if (u_size) {
1033 if (u_size > size)
1034 dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n",
1035 (unsigned long)u_size>>1, (unsigned long)size>>1);
1036 else
1037 size = u_size;
1038 }
1039
1040 return size;
1041}
1042
1043/**
1044 * drbd_check_al_size() - Ensures that the AL is of the right size
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001045 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001046 *
1047 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
1048 * failed, and 0 on success. You should call drbd_md_sync() after you called
1049 * this function.
1050 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001051static int drbd_check_al_size(struct drbd_device *device, struct disk_conf *dc)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001052{
1053 struct lru_cache *n, *t;
1054 struct lc_element *e;
1055 unsigned int in_use;
1056 int i;
1057
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001058 if (device->act_log &&
1059 device->act_log->nr_elements == dc->al_extents)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001060 return 0;
1061
1062 in_use = 0;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001063 t = device->act_log;
Lars Ellenberg7ad651b2011-02-21 13:21:03 +01001064 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
Lars Ellenbergf3990022011-03-23 14:31:09 +01001065 dc->al_extents, sizeof(struct lc_element), 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001066
1067 if (n == NULL) {
1068 dev_err(DEV, "Cannot allocate act_log lru!\n");
1069 return -ENOMEM;
1070 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001071 spin_lock_irq(&device->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001072 if (t) {
1073 for (i = 0; i < t->nr_elements; i++) {
1074 e = lc_element_by_index(t, i);
1075 if (e->refcnt)
1076 dev_err(DEV, "refcnt(%d)==%d\n",
1077 e->lc_number, e->refcnt);
1078 in_use += e->refcnt;
1079 }
1080 }
1081 if (!in_use)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001082 device->act_log = n;
1083 spin_unlock_irq(&device->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001084 if (in_use) {
1085 dev_err(DEV, "Activity log still in use!\n");
1086 lc_destroy(n);
1087 return -EBUSY;
1088 } else {
1089 if (t)
1090 lc_destroy(t);
1091 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001092 drbd_md_mark_dirty(device); /* we changed device->act_log->nr_elemens */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001093 return 0;
1094}
1095
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001096static void drbd_setup_queue_param(struct drbd_device *device, unsigned int max_bio_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001097{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001098 struct request_queue * const q = device->rq_queue;
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001099 unsigned int max_hw_sectors = max_bio_size >> 9;
1100 unsigned int max_segments = 0;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001101
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001102 if (get_ldev_if_state(device, D_ATTACHING)) {
1103 struct request_queue * const b = device->ldev->backing_bdev->bd_disk->queue;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001104
1105 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001106 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001107 max_segments = rcu_dereference(device->ldev->disk_conf)->max_bio_bvecs;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001108 rcu_read_unlock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001109 put_ldev(device);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001110 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001111
Philipp Reisnerb411b362009-09-25 16:07:19 -07001112 blk_queue_logical_block_size(q, 512);
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001113 blk_queue_max_hw_sectors(q, max_hw_sectors);
1114 /* This is the workaround for "bio would need to, but cannot, be split" */
1115 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
1116 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001117
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001118 if (get_ldev_if_state(device, D_ATTACHING)) {
1119 struct request_queue * const b = device->ldev->backing_bdev->bd_disk->queue;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001120
Philipp Reisner99432fc2011-05-20 16:39:13 +02001121 blk_queue_stack_limits(q, b);
1122
1123 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
1124 dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
1125 q->backing_dev_info.ra_pages,
1126 b->backing_dev_info.ra_pages);
1127 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1128 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001129 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001130 }
1131}
1132
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001133void drbd_reconsider_max_bio_size(struct drbd_device *device)
Philipp Reisner99432fc2011-05-20 16:39:13 +02001134{
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001135 unsigned int now, new, local, peer;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001136
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001137 now = queue_max_hw_sectors(device->rq_queue) << 9;
1138 local = device->local_max_bio_size; /* Eventually last known value, from volatile memory */
1139 peer = device->peer_max_bio_size; /* Eventually last known value, from meta data */
Philipp Reisner99432fc2011-05-20 16:39:13 +02001140
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001141 if (get_ldev_if_state(device, D_ATTACHING)) {
1142 local = queue_max_hw_sectors(device->ldev->backing_bdev->bd_disk->queue) << 9;
1143 device->local_max_bio_size = local;
1144 put_ldev(device);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001145 }
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001146 local = min(local, DRBD_MAX_BIO_SIZE);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001147
1148 /* We may ignore peer limits if the peer is modern enough.
1149 Because new from 8.3.8 onwards the peer can use multiple
1150 BIOs for a single peer_request */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001151 if (device->state.conn >= C_WF_REPORT_PARAMS) {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001152 if (first_peer_device(device)->connection->agreed_pro_version < 94)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001153 peer = min(device->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
Philipp Reisner68093842011-06-30 15:43:06 +02001154 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001155 else if (first_peer_device(device)->connection->agreed_pro_version == 94)
Philipp Reisner99432fc2011-05-20 16:39:13 +02001156 peer = DRBD_MAX_SIZE_H80_PACKET;
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001157 else if (first_peer_device(device)->connection->agreed_pro_version < 100)
Philipp Reisner2ffca4f2011-06-30 15:43:06 +02001158 peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
1159 else
Philipp Reisner99432fc2011-05-20 16:39:13 +02001160 peer = DRBD_MAX_BIO_SIZE;
1161 }
1162
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001163 new = min(local, peer);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001164
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001165 if (device->state.role == R_PRIMARY && new < now)
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001166 dev_err(DEV, "ASSERT FAILED new < now; (%u < %u)\n", new, now);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001167
1168 if (new != now)
1169 dev_info(DEV, "max BIO size = %u\n", new);
1170
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001171 drbd_setup_queue_param(device, new);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001172}
1173
Philipp Reisnera18e9d12011-04-24 11:09:55 +02001174/* Starts the worker thread */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001175static void conn_reconfig_start(struct drbd_connection *connection)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001176{
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001177 drbd_thread_start(&connection->worker);
1178 conn_flush_workqueue(connection);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001179}
1180
Philipp Reisnera18e9d12011-04-24 11:09:55 +02001181/* if still unconfigured, stops worker again. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001182static void conn_reconfig_done(struct drbd_connection *connection)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001183{
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001184 bool stop_threads;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001185 spin_lock_irq(&connection->req_lock);
1186 stop_threads = conn_all_vols_unconf(connection) &&
1187 connection->cstate == C_STANDALONE;
1188 spin_unlock_irq(&connection->req_lock);
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001189 if (stop_threads) {
1190 /* asender is implicitly stopped by receiver
Philipp Reisner81fa2e62011-05-04 15:10:30 +02001191 * in conn_disconnect() */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001192 drbd_thread_stop(&connection->receiver);
1193 drbd_thread_stop(&connection->worker);
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001194 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001195}
1196
Philipp Reisner07782862010-08-31 12:00:50 +02001197/* Make sure IO is suspended before calling this function(). */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001198static void drbd_suspend_al(struct drbd_device *device)
Philipp Reisner07782862010-08-31 12:00:50 +02001199{
1200 int s = 0;
1201
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001202 if (!lc_try_lock(device->act_log)) {
Philipp Reisner07782862010-08-31 12:00:50 +02001203 dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n");
1204 return;
1205 }
1206
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001207 drbd_al_shrink(device);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001208 spin_lock_irq(&first_peer_device(device)->connection->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001209 if (device->state.conn < C_CONNECTED)
1210 s = !test_and_set_bit(AL_SUSPENDED, &device->flags);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001211 spin_unlock_irq(&first_peer_device(device)->connection->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001212 lc_unlock(device->act_log);
Philipp Reisner07782862010-08-31 12:00:50 +02001213
1214 if (s)
1215 dev_info(DEV, "Suspended AL updates\n");
1216}
1217
Lars Ellenberg5979e362011-04-27 21:09:55 +02001218
1219static bool should_set_defaults(struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001220{
Lars Ellenberg5979e362011-04-27 21:09:55 +02001221 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1222 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1223}
1224
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001225static unsigned int drbd_al_extents_max(struct drbd_backing_dev *bdev)
Philipp Reisnerd589a212011-05-04 10:06:52 +02001226{
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001227 /* This is limited by 16 bit "slot" numbers,
1228 * and by available on-disk context storage.
1229 *
1230 * Also (u16)~0 is special (denotes a "free" extent).
1231 *
1232 * One transaction occupies one 4kB on-disk block,
1233 * we have n such blocks in the on disk ring buffer,
1234 * the "current" transaction may fail (n-1),
1235 * and there is 919 slot numbers context information per transaction.
1236 *
1237 * 72 transaction blocks amounts to more than 2**16 context slots,
1238 * so cap there first.
1239 */
1240 const unsigned int max_al_nr = DRBD_AL_EXTENTS_MAX;
1241 const unsigned int sufficient_on_disk =
1242 (max_al_nr + AL_CONTEXT_PER_TRANSACTION -1)
1243 /AL_CONTEXT_PER_TRANSACTION;
Philipp Reisnerd589a212011-05-04 10:06:52 +02001244
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001245 unsigned int al_size_4k = bdev->md.al_size_4k;
1246
1247 if (al_size_4k > sufficient_on_disk)
1248 return max_al_nr;
1249
1250 return (al_size_4k - 1) * AL_CONTEXT_PER_TRANSACTION;
Philipp Reisnerd589a212011-05-04 10:06:52 +02001251}
1252
Lars Ellenbergf3990022011-03-23 14:31:09 +01001253int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1254{
1255 enum drbd_ret_code retcode;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001256 struct drbd_device *device;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001257 struct disk_conf *new_disk_conf, *old_disk_conf;
Philipp Reisner813472c2011-05-03 16:47:02 +02001258 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001259 int err, fifo_size;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001260
1261 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1262 if (!adm_ctx.reply_skb)
1263 return retcode;
1264 if (retcode != NO_ERROR)
1265 goto out;
1266
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001267 device = adm_ctx.device;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001268
1269 /* we also need a disk
1270 * to change the options on */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001271 if (!get_ldev(device)) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001272 retcode = ERR_NO_DISK;
1273 goto out;
1274 }
1275
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001276 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001277 if (!new_disk_conf) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001278 retcode = ERR_NOMEM;
1279 goto fail;
1280 }
1281
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001282 mutex_lock(&first_peer_device(device)->connection->conf_update);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001283 old_disk_conf = device->ldev->disk_conf;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001284 *new_disk_conf = *old_disk_conf;
Lars Ellenberg5979e362011-04-27 21:09:55 +02001285 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02001286 set_disk_conf_defaults(new_disk_conf);
Lars Ellenberg5979e362011-04-27 21:09:55 +02001287
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001288 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02001289 if (err && err != -ENOMSG) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001290 retcode = ERR_MANDATORY_TAG;
1291 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisner8e229432013-08-01 10:21:47 +02001292 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001293 }
1294
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001295 if (!expect(new_disk_conf->resync_rate >= 1))
1296 new_disk_conf->resync_rate = 1;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001297
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001298 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1299 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001300 if (new_disk_conf->al_extents > drbd_al_extents_max(device->ldev))
1301 new_disk_conf->al_extents = drbd_al_extents_max(device->ldev);
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001302
1303 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1304 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001305
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001306 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001307 if (fifo_size != device->rs_plan_s->size) {
Philipp Reisner813472c2011-05-03 16:47:02 +02001308 new_plan = fifo_alloc(fifo_size);
1309 if (!new_plan) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001310 dev_err(DEV, "kmalloc of fifo_buffer failed");
1311 retcode = ERR_NOMEM;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001312 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001313 }
1314 }
1315
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001316 drbd_suspend_io(device);
1317 wait_event(device->al_wait, lc_try_lock(device->act_log));
1318 drbd_al_shrink(device);
1319 err = drbd_check_al_size(device, new_disk_conf);
1320 lc_unlock(device->act_log);
1321 wake_up(&device->al_wait);
1322 drbd_resume_io(device);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001323
1324 if (err) {
1325 retcode = ERR_NOMEM;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001326 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001327 }
1328
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001329 write_lock_irq(&global_state_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001330 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001331 if (retcode == NO_ERROR) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001332 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
1333 drbd_resync_after_changed(device);
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001334 }
1335 write_unlock_irq(&global_state_lock);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001336
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001337 if (retcode != NO_ERROR)
1338 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001339
Philipp Reisner813472c2011-05-03 16:47:02 +02001340 if (new_plan) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001341 old_plan = device->rs_plan_s;
1342 rcu_assign_pointer(device->rs_plan_s, new_plan);
Philipp Reisner9958c852011-05-03 16:19:31 +02001343 }
Philipp Reisner9958c852011-05-03 16:19:31 +02001344
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001345 mutex_unlock(&first_peer_device(device)->connection->conf_update);
Philipp Reisner27eb13e2012-03-30 14:12:15 +02001346
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001347 if (new_disk_conf->al_updates)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001348 device->ldev->md.flags &= ~MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001349 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001350 device->ldev->md.flags |= MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001351
Lars Ellenberg691631c2012-10-26 00:41:50 +02001352 if (new_disk_conf->md_flushes)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001353 clear_bit(MD_NO_FUA, &device->flags);
Lars Ellenberg691631c2012-10-26 00:41:50 +02001354 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001355 set_bit(MD_NO_FUA, &device->flags);
Lars Ellenberg691631c2012-10-26 00:41:50 +02001356
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001357 drbd_bump_write_ordering(first_peer_device(device)->connection, WO_bdev_flush);
Philipp Reisner27eb13e2012-03-30 14:12:15 +02001358
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001359 drbd_md_sync(device);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001360
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001361 if (device->state.conn >= C_CONNECTED)
1362 drbd_send_sync_param(device);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001363
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001364 synchronize_rcu();
1365 kfree(old_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02001366 kfree(old_plan);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001367 mod_timer(&device->request_timer, jiffies + HZ);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001368 goto success;
1369
1370fail_unlock:
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001371 mutex_unlock(&first_peer_device(device)->connection->conf_update);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001372 fail:
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001373 kfree(new_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02001374 kfree(new_plan);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001375success:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001376 put_ldev(device);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001377 out:
1378 drbd_adm_finish(info, retcode);
1379 return 0;
1380}
1381
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001382int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001383{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001384 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001385 int err;
Andreas Gruenbacher116676c2010-12-08 13:33:11 +01001386 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001387 enum determine_dev_size dd;
1388 sector_t max_possible_sectors;
1389 sector_t min_md_device_sectors;
1390 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001391 struct disk_conf *new_disk_conf = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001392 struct block_device *bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001393 struct lru_cache *resync_lru = NULL;
Philipp Reisner9958c852011-05-03 16:19:31 +02001394 struct fifo_buffer *new_plan = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001395 union drbd_state ns, os;
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001396 enum drbd_state_rv rv;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001397 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001398
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001399 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1400 if (!adm_ctx.reply_skb)
1401 return retcode;
1402 if (retcode != NO_ERROR)
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001403 goto finish;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001404
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001405 device = adm_ctx.device;
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001406 conn_reconfig_start(first_peer_device(device)->connection);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001407
1408 /* if you want to reconfigure, please tear down first */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001409 if (device->state.disk > D_DISKLESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001410 retcode = ERR_DISK_CONFIGURED;
1411 goto fail;
1412 }
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001413 /* It may just now have detached because of IO error. Make sure
1414 * drbd_ldev_destroy is done already, we may end up here very fast,
1415 * e.g. if someone calls attach from the on-io-error handler,
1416 * to realize a "hot spare" feature (not that I'd recommend that) */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001417 wait_event(device->misc_wait, !atomic_read(&device->local_cnt));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001418
Lars Ellenberg383606e2012-06-14 14:21:32 +02001419 /* make sure there is no leftover from previous force-detach attempts */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001420 clear_bit(FORCE_DETACH, &device->flags);
1421 clear_bit(WAS_IO_ERROR, &device->flags);
1422 clear_bit(WAS_READ_ERROR, &device->flags);
Lars Ellenberg383606e2012-06-14 14:21:32 +02001423
Lars Ellenberg0029d622012-06-14 18:02:52 +02001424 /* and no leftover from previously aborted resync or verify, either */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001425 device->rs_total = 0;
1426 device->rs_failed = 0;
1427 atomic_set(&device->rs_pending_cnt, 0);
Lars Ellenberg0029d622012-06-14 18:02:52 +02001428
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001429 /* allocation not in the IO path, drbdsetup context */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001430 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1431 if (!nbc) {
1432 retcode = ERR_NOMEM;
1433 goto fail;
1434 }
Philipp Reisner9f2247b2012-08-16 14:25:58 +02001435 spin_lock_init(&nbc->md.uuid_lock);
1436
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001437 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
1438 if (!new_disk_conf) {
1439 retcode = ERR_NOMEM;
1440 goto fail;
1441 }
1442 nbc->disk_conf = new_disk_conf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001443
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001444 set_disk_conf_defaults(new_disk_conf);
1445 err = disk_conf_from_attrs(new_disk_conf, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001446 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001447 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001448 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001449 goto fail;
1450 }
1451
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001452 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1453 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
Philipp Reisnerd589a212011-05-04 10:06:52 +02001454
Philipp Reisner9958c852011-05-03 16:19:31 +02001455 new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
1456 if (!new_plan) {
1457 retcode = ERR_NOMEM;
1458 goto fail;
1459 }
1460
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001461 if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001462 retcode = ERR_MD_IDX_INVALID;
1463 goto fail;
1464 }
1465
Lars Ellenberga3f8f7d2013-03-27 14:08:43 +01001466 write_lock_irq(&global_state_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001467 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
Lars Ellenberga3f8f7d2013-03-27 14:08:43 +01001468 write_unlock_irq(&global_state_lock);
1469 if (retcode != NO_ERROR)
1470 goto fail;
1471
Philipp Reisner44ed1672011-04-19 17:10:19 +02001472 rcu_read_lock();
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001473 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001474 if (nc) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001475 if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02001476 rcu_read_unlock();
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001477 retcode = ERR_STONITH_AND_PROT_A;
1478 goto fail;
1479 }
1480 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02001481 rcu_read_unlock();
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001482
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001483 bdev = blkdev_get_by_path(new_disk_conf->backing_dev,
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001484 FMODE_READ | FMODE_WRITE | FMODE_EXCL, device);
Tejun Heoe525fd82010-11-13 11:55:17 +01001485 if (IS_ERR(bdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001486 dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->backing_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001487 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001488 retcode = ERR_OPEN_DISK;
1489 goto fail;
1490 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001491 nbc->backing_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001492
Tejun Heoe525fd82010-11-13 11:55:17 +01001493 /*
1494 * meta_dev_idx >= 0: external fixed size, possibly multiple
1495 * drbd sharing one meta device. TODO in that case, paranoia
1496 * check that [md_bdev, meta_dev_idx] is not yet used by some
1497 * other drbd minor! (if you use drbd.conf + drbdadm, that
1498 * should check it for you already; but if you don't, or
1499 * someone fooled it, we need to double check here)
1500 */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001501 bdev = blkdev_get_by_path(new_disk_conf->meta_dev,
Tejun Heod4d77622010-11-13 11:55:18 +01001502 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001503 (new_disk_conf->meta_dev_idx < 0) ?
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001504 (void *)device : (void *)drbd_m_holder);
Tejun Heoe525fd82010-11-13 11:55:17 +01001505 if (IS_ERR(bdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001506 dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->meta_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001507 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001508 retcode = ERR_OPEN_MD_DISK;
1509 goto fail;
1510 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001511 nbc->md_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001512
Tejun Heoe525fd82010-11-13 11:55:17 +01001513 if ((nbc->backing_bdev == nbc->md_bdev) !=
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001514 (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1515 new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001516 retcode = ERR_MD_IDX_INVALID;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001517 goto fail;
1518 }
1519
1520 resync_lru = lc_create("resync", drbd_bm_ext_cache,
Lars Ellenberg46a15bc2011-02-21 13:21:01 +01001521 1, 61, sizeof(struct bm_extent),
Philipp Reisnerb411b362009-09-25 16:07:19 -07001522 offsetof(struct bm_extent, lce));
1523 if (!resync_lru) {
1524 retcode = ERR_NOMEM;
Tejun Heoe525fd82010-11-13 11:55:17 +01001525 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001526 }
1527
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +01001528 /* Read our meta data super block early.
1529 * This also sets other on-disk offsets. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001530 retcode = drbd_md_read(device, nbc);
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +01001531 if (retcode != NO_ERROR)
1532 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001533
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001534 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1535 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
1536 if (new_disk_conf->al_extents > drbd_al_extents_max(nbc))
1537 new_disk_conf->al_extents = drbd_al_extents_max(nbc);
1538
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001539 if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001540 dev_err(DEV, "max capacity %llu smaller than disk size %llu\n",
1541 (unsigned long long) drbd_get_max_capacity(nbc),
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001542 (unsigned long long) new_disk_conf->disk_size);
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001543 retcode = ERR_DISK_TOO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001544 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001545 }
1546
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001547 if (new_disk_conf->meta_dev_idx < 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001548 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1549 /* at least one MB, otherwise it does not make sense */
1550 min_md_device_sectors = (2<<10);
1551 } else {
1552 max_possible_sectors = DRBD_MAX_SECTORS;
Lars Ellenbergae8bf312013-03-19 18:16:43 +01001553 min_md_device_sectors = MD_128MB_SECT * (new_disk_conf->meta_dev_idx + 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001554 }
1555
Philipp Reisnerb411b362009-09-25 16:07:19 -07001556 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001557 retcode = ERR_MD_DISK_TOO_SMALL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001558 dev_warn(DEV, "refusing attach: md-device too small, "
1559 "at least %llu sectors needed for this meta-disk type\n",
1560 (unsigned long long) min_md_device_sectors);
Tejun Heoe525fd82010-11-13 11:55:17 +01001561 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001562 }
1563
1564 /* Make sure the new disk is big enough
1565 * (we may currently be R_PRIMARY with no local disk...) */
1566 if (drbd_get_max_capacity(nbc) <
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001567 drbd_get_capacity(device->this_bdev)) {
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001568 retcode = ERR_DISK_TOO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001569 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001570 }
1571
1572 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1573
Lars Ellenberg13529942009-10-12 19:07:49 +02001574 if (nbc->known_size > max_possible_sectors) {
1575 dev_warn(DEV, "==> truncating very big lower level device "
1576 "to currently maximum possible %llu sectors <==\n",
1577 (unsigned long long) max_possible_sectors);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001578 if (new_disk_conf->meta_dev_idx >= 0)
Lars Ellenberg13529942009-10-12 19:07:49 +02001579 dev_warn(DEV, "==>> using internal or flexible "
1580 "meta data may help <<==\n");
1581 }
1582
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001583 drbd_suspend_io(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001584 /* also wait for the last barrier ack. */
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001585 /* FIXME see also https://daiquiri.linbit/cgi-bin/bugzilla/show_bug.cgi?id=171
1586 * We need a way to either ignore barrier acks for barriers sent before a device
1587 * was attached, or a way to wait for all pending barrier acks to come in.
1588 * As barriers are counted per resource,
1589 * we'd need to suspend io on all devices of a resource.
1590 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001591 wait_event(device->misc_wait, !atomic_read(&device->ap_pending_cnt) || drbd_suspended(device));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001592 /* and for any other previously queued work */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001593 drbd_flush_workqueue(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001594
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001595 rv = _drbd_request_state(device, NS(disk, D_ATTACHING), CS_VERBOSE);
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001596 retcode = rv; /* FIXME: Type mismatch. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001597 drbd_resume_io(device);
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001598 if (rv < SS_SUCCESS)
Tejun Heoe525fd82010-11-13 11:55:17 +01001599 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001600
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001601 if (!get_ldev_if_state(device, D_ATTACHING))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001602 goto force_diskless;
1603
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001604 if (!device->bitmap) {
1605 if (drbd_bm_init(device)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001606 retcode = ERR_NOMEM;
1607 goto force_diskless_dec;
1608 }
1609 }
1610
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001611 if (device->state.conn < C_CONNECTED &&
1612 device->state.role == R_PRIMARY &&
1613 (device->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001614 dev_err(DEV, "Can only attach to data with current UUID=%016llX\n",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001615 (unsigned long long)device->ed_uuid);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001616 retcode = ERR_DATA_NOT_CURRENT;
1617 goto force_diskless_dec;
1618 }
1619
1620 /* Since we are diskless, fix the activity log first... */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001621 if (drbd_check_al_size(device, new_disk_conf)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001622 retcode = ERR_NOMEM;
1623 goto force_diskless_dec;
1624 }
1625
1626 /* Prevent shrinking of consistent devices ! */
1627 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001628 drbd_new_dev_size(device, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001629 dev_warn(DEV, "refusing to truncate a consistent device\n");
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001630 retcode = ERR_DISK_TOO_SMALL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001631 goto force_diskless_dec;
1632 }
1633
Philipp Reisnerb411b362009-09-25 16:07:19 -07001634 /* Reset the "barriers don't work" bits here, then force meta data to
1635 * be written, to ensure we determine if barriers are supported. */
Andreas Gruenbachere5440462011-05-04 15:25:35 +02001636 if (new_disk_conf->md_flushes)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001637 clear_bit(MD_NO_FUA, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001638 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001639 set_bit(MD_NO_FUA, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001640
1641 /* Point of no return reached.
1642 * Devices and memory are no longer released by error cleanup below.
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001643 * now device takes over responsibility, and the state engine should
Philipp Reisnerb411b362009-09-25 16:07:19 -07001644 * clean it up somewhere. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001645 D_ASSERT(device->ldev == NULL);
1646 device->ldev = nbc;
1647 device->resync = resync_lru;
1648 device->rs_plan_s = new_plan;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001649 nbc = NULL;
1650 resync_lru = NULL;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001651 new_disk_conf = NULL;
Philipp Reisner9958c852011-05-03 16:19:31 +02001652 new_plan = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001653
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001654 drbd_bump_write_ordering(first_peer_device(device)->connection, WO_bdev_flush);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001655
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001656 if (drbd_md_test_flag(device->ldev, MDF_CRASHED_PRIMARY))
1657 set_bit(CRASHED_PRIMARY, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001658 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001659 clear_bit(CRASHED_PRIMARY, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001660
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001661 if (drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001662 !(device->state.role == R_PRIMARY &&
1663 first_peer_device(device)->connection->susp_nod))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001664 set_bit(CRASHED_PRIMARY, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001665
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001666 device->send_cnt = 0;
1667 device->recv_cnt = 0;
1668 device->read_cnt = 0;
1669 device->writ_cnt = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001670
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001671 drbd_reconsider_max_bio_size(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001672
1673 /* If I am currently not R_PRIMARY,
1674 * but meta data primary indicator is set,
1675 * I just now recover from a hard crash,
1676 * and have been R_PRIMARY before that crash.
1677 *
1678 * Now, if I had no connection before that crash
1679 * (have been degraded R_PRIMARY), chances are that
1680 * I won't find my peer now either.
1681 *
1682 * In that case, and _only_ in that case,
1683 * we use the degr-wfc-timeout instead of the default,
1684 * so we can automatically recover from a crash of a
1685 * degraded but active "cluster" after a certain timeout.
1686 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001687 clear_bit(USE_DEGR_WFC_T, &device->flags);
1688 if (device->state.role != R_PRIMARY &&
1689 drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
1690 !drbd_md_test_flag(device->ldev, MDF_CONNECTED_IND))
1691 set_bit(USE_DEGR_WFC_T, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001692
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001693 dd = drbd_determine_dev_size(device, 0, NULL);
Philipp Reisnerd752b262013-06-25 16:50:08 +02001694 if (dd <= DS_ERROR) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001695 retcode = ERR_NOMEM_BITMAP;
1696 goto force_diskless_dec;
Philipp Reisnere96c9632013-06-25 16:50:07 +02001697 } else if (dd == DS_GREW)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001698 set_bit(RESYNC_AFTER_NEG, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001699
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001700 if (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC) ||
1701 (test_bit(CRASHED_PRIMARY, &device->flags) &&
1702 drbd_md_test_flag(device->ldev, MDF_AL_DISABLED))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001703 dev_info(DEV, "Assuming that all blocks are out of sync "
1704 "(aka FullSync)\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001705 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001706 "set_n_write from attaching", BM_LOCKED_MASK)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001707 retcode = ERR_IO_MD_DISK;
1708 goto force_diskless_dec;
1709 }
1710 } else {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001711 if (drbd_bitmap_io(device, &drbd_bm_read,
Andreas Gruenbacher22ab6a32010-12-13 01:44:11 +01001712 "read from attaching", BM_LOCKED_MASK)) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001713 retcode = ERR_IO_MD_DISK;
1714 goto force_diskless_dec;
1715 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001716 }
1717
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001718 if (_drbd_bm_total_weight(device) == drbd_bm_bits(device))
1719 drbd_suspend_al(device); /* IO is still suspended here... */
Philipp Reisner07782862010-08-31 12:00:50 +02001720
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001721 spin_lock_irq(&first_peer_device(device)->connection->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001722 os = drbd_read_state(device);
Philipp Reisner78bae592011-03-28 15:40:12 +02001723 ns = os;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001724 /* If MDF_CONSISTENT is not set go into inconsistent state,
1725 otherwise investigate MDF_WasUpToDate...
1726 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1727 otherwise into D_CONSISTENT state.
1728 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001729 if (drbd_md_test_flag(device->ldev, MDF_CONSISTENT)) {
1730 if (drbd_md_test_flag(device->ldev, MDF_WAS_UP_TO_DATE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001731 ns.disk = D_CONSISTENT;
1732 else
1733 ns.disk = D_OUTDATED;
1734 } else {
1735 ns.disk = D_INCONSISTENT;
1736 }
1737
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001738 if (drbd_md_test_flag(device->ldev, MDF_PEER_OUT_DATED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001739 ns.pdsk = D_OUTDATED;
1740
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001741 rcu_read_lock();
1742 if (ns.disk == D_CONSISTENT &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001743 (ns.pdsk == D_OUTDATED || rcu_dereference(device->ldev->disk_conf)->fencing == FP_DONT_CARE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001744 ns.disk = D_UP_TO_DATE;
1745
1746 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1747 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1748 this point, because drbd_request_state() modifies these
1749 flags. */
1750
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001751 if (rcu_dereference(device->ldev->disk_conf)->al_updates)
1752 device->ldev->md.flags &= ~MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001753 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001754 device->ldev->md.flags |= MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001755
1756 rcu_read_unlock();
1757
Philipp Reisnerb411b362009-09-25 16:07:19 -07001758 /* In case we are C_CONNECTED postpone any decision on the new disk
1759 state after the negotiation phase. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001760 if (device->state.conn == C_CONNECTED) {
1761 device->new_state_tmp.i = ns.i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001762 ns.i = os.i;
1763 ns.disk = D_NEGOTIATING;
Philipp Reisnerdc66c742010-06-02 14:31:29 +02001764
1765 /* We expect to receive up-to-date UUIDs soon.
1766 To avoid a race in receive_state, free p_uuid while
1767 holding req_lock. I.e. atomic with the state change */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001768 kfree(device->p_uuid);
1769 device->p_uuid = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001770 }
1771
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001772 rv = _drbd_set_state(device, ns, CS_VERBOSE, NULL);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001773 spin_unlock_irq(&first_peer_device(device)->connection->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001774
1775 if (rv < SS_SUCCESS)
1776 goto force_diskless_dec;
1777
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001778 mod_timer(&device->request_timer, jiffies + HZ);
Philipp Reisnercdfda632011-07-05 15:38:59 +02001779
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001780 if (device->state.role == R_PRIMARY)
1781 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001782 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001783 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001784
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001785 drbd_md_mark_dirty(device);
1786 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001787
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001788 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
1789 put_ldev(device);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001790 conn_reconfig_done(first_peer_device(device)->connection);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001791 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001792 return 0;
1793
1794 force_diskless_dec:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001795 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001796 force_diskless:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001797 drbd_force_state(device, NS(disk, D_DISKLESS));
1798 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001799 fail:
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001800 conn_reconfig_done(first_peer_device(device)->connection);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001801 if (nbc) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001802 if (nbc->backing_bdev)
1803 blkdev_put(nbc->backing_bdev,
1804 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1805 if (nbc->md_bdev)
1806 blkdev_put(nbc->md_bdev,
1807 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001808 kfree(nbc);
1809 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001810 kfree(new_disk_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001811 lc_destroy(resync_lru);
Philipp Reisner9958c852011-05-03 16:19:31 +02001812 kfree(new_plan);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001813
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001814 finish:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001815 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001816 return 0;
1817}
1818
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001819static int adm_detach(struct drbd_device *device, int force)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001820{
Philipp Reisner19f83c72011-03-29 14:21:03 +02001821 enum drbd_state_rv retcode;
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001822 int ret;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001823
Philipp Reisnercdfda632011-07-05 15:38:59 +02001824 if (force) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001825 set_bit(FORCE_DETACH, &device->flags);
1826 drbd_force_state(device, NS(disk, D_FAILED));
Philipp Reisnercdfda632011-07-05 15:38:59 +02001827 retcode = SS_SUCCESS;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001828 goto out;
1829 }
1830
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001831 drbd_suspend_io(device); /* so no-one is stuck in drbd_al_begin_io */
1832 drbd_md_get_buffer(device); /* make sure there is no in-flight meta-data IO */
1833 retcode = drbd_request_state(device, NS(disk, D_FAILED));
1834 drbd_md_put_buffer(device);
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001835 /* D_FAILED will transition to DISKLESS. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001836 ret = wait_event_interruptible(device->misc_wait,
1837 device->state.disk != D_FAILED);
1838 drbd_resume_io(device);
Philipp Reisner9b2f61a2011-05-24 10:27:38 +02001839 if ((int)retcode == (int)SS_IS_DISKLESS)
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001840 retcode = SS_NOTHING_TO_DO;
1841 if (ret)
1842 retcode = ERR_INTR;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001843out:
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001844 return retcode;
1845}
1846
Philipp Reisnerb411b362009-09-25 16:07:19 -07001847/* Detaching the disk is a process in multiple stages. First we need to lock
1848 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1849 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1850 * internal references as well.
1851 * Only then we have finally detached. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001852int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001853{
1854 enum drbd_ret_code retcode;
Philipp Reisnercdfda632011-07-05 15:38:59 +02001855 struct detach_parms parms = { };
1856 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001857
1858 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1859 if (!adm_ctx.reply_skb)
1860 return retcode;
1861 if (retcode != NO_ERROR)
1862 goto out;
1863
Philipp Reisnercdfda632011-07-05 15:38:59 +02001864 if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
1865 err = detach_parms_from_attrs(&parms, info);
1866 if (err) {
1867 retcode = ERR_MANDATORY_TAG;
1868 drbd_msg_put_info(from_attrs_err_to_txt(err));
1869 goto out;
1870 }
1871 }
1872
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001873 retcode = adm_detach(adm_ctx.device, parms.force_detach);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001874out:
1875 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001876 return 0;
1877}
1878
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001879static bool conn_resync_running(struct drbd_connection *connection)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001880{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001881 struct drbd_device *device;
Philipp Reisner695d08f2011-04-11 22:53:32 -07001882 bool rv = false;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001883 int vnr;
1884
Philipp Reisner695d08f2011-04-11 22:53:32 -07001885 rcu_read_lock();
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001886 idr_for_each_entry(&connection->volumes, device, vnr) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001887 if (device->state.conn == C_SYNC_SOURCE ||
1888 device->state.conn == C_SYNC_TARGET ||
1889 device->state.conn == C_PAUSED_SYNC_S ||
1890 device->state.conn == C_PAUSED_SYNC_T) {
Philipp Reisner695d08f2011-04-11 22:53:32 -07001891 rv = true;
1892 break;
1893 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001894 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001895 rcu_read_unlock();
1896
1897 return rv;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001898}
1899
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001900static bool conn_ov_running(struct drbd_connection *connection)
Lars Ellenbergf3990022011-03-23 14:31:09 +01001901{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001902 struct drbd_device *device;
Philipp Reisner695d08f2011-04-11 22:53:32 -07001903 bool rv = false;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001904 int vnr;
1905
Philipp Reisner695d08f2011-04-11 22:53:32 -07001906 rcu_read_lock();
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001907 idr_for_each_entry(&connection->volumes, device, vnr) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001908 if (device->state.conn == C_VERIFY_S ||
1909 device->state.conn == C_VERIFY_T) {
Philipp Reisner695d08f2011-04-11 22:53:32 -07001910 rv = true;
1911 break;
1912 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001913 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001914 rcu_read_unlock();
1915
1916 return rv;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001917}
1918
Philipp Reisnercd643972011-04-13 18:00:59 -07001919static enum drbd_ret_code
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001920_check_net_options(struct drbd_connection *connection, struct net_conf *old_conf, struct net_conf *new_conf)
Philipp Reisnercd643972011-04-13 18:00:59 -07001921{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001922 struct drbd_device *device;
Philipp Reisnercd643972011-04-13 18:00:59 -07001923 int i;
1924
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001925 if (old_conf && connection->cstate == C_WF_REPORT_PARAMS && connection->agreed_pro_version < 100) {
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02001926 if (new_conf->wire_protocol != old_conf->wire_protocol)
1927 return ERR_NEED_APV_100;
1928
1929 if (new_conf->two_primaries != old_conf->two_primaries)
1930 return ERR_NEED_APV_100;
1931
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02001932 if (strcmp(new_conf->integrity_alg, old_conf->integrity_alg))
1933 return ERR_NEED_APV_100;
1934 }
1935
1936 if (!new_conf->two_primaries &&
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001937 conn_highest_role(connection) == R_PRIMARY &&
1938 conn_highest_peer(connection) == R_PRIMARY)
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02001939 return ERR_NEED_ALLOW_TWO_PRI;
Philipp Reisnerb032b6f2011-04-13 18:16:10 -07001940
Philipp Reisnercd643972011-04-13 18:00:59 -07001941 if (new_conf->two_primaries &&
1942 (new_conf->wire_protocol != DRBD_PROT_C))
1943 return ERR_NOT_PROTO_C;
1944
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001945 idr_for_each_entry(&connection->volumes, device, i) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001946 if (get_ldev(device)) {
1947 enum drbd_fencing_p fp = rcu_dereference(device->ldev->disk_conf)->fencing;
1948 put_ldev(device);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001949 if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
Philipp Reisnercd643972011-04-13 18:00:59 -07001950 return ERR_STONITH_AND_PROT_A;
Philipp Reisnercd643972011-04-13 18:00:59 -07001951 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001952 if (device->state.role == R_PRIMARY && new_conf->discard_my_data)
Lars Ellenbergeb120102012-08-01 12:46:20 +02001953 return ERR_DISCARD_IMPOSSIBLE;
Philipp Reisnercd643972011-04-13 18:00:59 -07001954 }
Philipp Reisnercd643972011-04-13 18:00:59 -07001955
1956 if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A)
1957 return ERR_CONG_NOT_PROTO_A;
1958
1959 return NO_ERROR;
1960}
1961
Philipp Reisner44ed1672011-04-19 17:10:19 +02001962static enum drbd_ret_code
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001963check_net_options(struct drbd_connection *connection, struct net_conf *new_conf)
Philipp Reisner44ed1672011-04-19 17:10:19 +02001964{
1965 static enum drbd_ret_code rv;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001966 struct drbd_device *device;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001967 int i;
1968
1969 rcu_read_lock();
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001970 rv = _check_net_options(connection, rcu_dereference(connection->net_conf), new_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001971 rcu_read_unlock();
1972
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001973 /* connection->volumes protected by genl_lock() here */
1974 idr_for_each_entry(&connection->volumes, device, i) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001975 if (!device->bitmap) {
1976 if (drbd_bm_init(device))
Philipp Reisner44ed1672011-04-19 17:10:19 +02001977 return ERR_NOMEM;
1978 }
1979 }
1980
1981 return rv;
1982}
1983
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001984struct crypto {
1985 struct crypto_hash *verify_tfm;
1986 struct crypto_hash *csums_tfm;
1987 struct crypto_hash *cram_hmac_tfm;
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02001988 struct crypto_hash *integrity_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001989};
1990
1991static int
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02001992alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001993{
1994 if (!tfm_name[0])
1995 return NO_ERROR;
1996
1997 *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
1998 if (IS_ERR(*tfm)) {
1999 *tfm = NULL;
2000 return err_alg;
2001 }
2002
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002003 return NO_ERROR;
2004}
2005
2006static enum drbd_ret_code
2007alloc_crypto(struct crypto *crypto, struct net_conf *new_conf)
2008{
Philipp Reisnerb411b362009-09-25 16:07:19 -07002009 char hmac_name[CRYPTO_MAX_ALG_NAME];
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002010 enum drbd_ret_code rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002011
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002012 rv = alloc_hash(&crypto->csums_tfm, new_conf->csums_alg,
2013 ERR_CSUMS_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002014 if (rv != NO_ERROR)
2015 return rv;
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002016 rv = alloc_hash(&crypto->verify_tfm, new_conf->verify_alg,
2017 ERR_VERIFY_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002018 if (rv != NO_ERROR)
2019 return rv;
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002020 rv = alloc_hash(&crypto->integrity_tfm, new_conf->integrity_alg,
2021 ERR_INTEGRITY_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002022 if (rv != NO_ERROR)
2023 return rv;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002024 if (new_conf->cram_hmac_alg[0] != 0) {
2025 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
2026 new_conf->cram_hmac_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002027
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002028 rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
2029 ERR_AUTH_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002030 }
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002031
2032 return rv;
2033}
2034
2035static void free_crypto(struct crypto *crypto)
2036{
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002037 crypto_free_hash(crypto->cram_hmac_tfm);
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02002038 crypto_free_hash(crypto->integrity_tfm);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002039 crypto_free_hash(crypto->csums_tfm);
2040 crypto_free_hash(crypto->verify_tfm);
2041}
2042
Lars Ellenbergf3990022011-03-23 14:31:09 +01002043int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
2044{
2045 enum drbd_ret_code retcode;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002046 struct drbd_connection *connection;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002047 struct net_conf *old_conf, *new_conf = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002048 int err;
2049 int ovr; /* online verify running */
2050 int rsr; /* re-sync running */
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002051 struct crypto crypto = { };
Lars Ellenbergf3990022011-03-23 14:31:09 +01002052
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002053 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002054 if (!adm_ctx.reply_skb)
2055 return retcode;
2056 if (retcode != NO_ERROR)
2057 goto out;
2058
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002059 connection = adm_ctx.connection;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002060
2061 new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
2062 if (!new_conf) {
2063 retcode = ERR_NOMEM;
2064 goto out;
2065 }
2066
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002067 conn_reconfig_start(connection);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002068
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002069 mutex_lock(&connection->data.mutex);
2070 mutex_lock(&connection->conf_update);
2071 old_conf = connection->net_conf;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002072
2073 if (!old_conf) {
2074 drbd_msg_put_info("net conf missing, try connect");
2075 retcode = ERR_INVALID_REQUEST;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002076 goto fail;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002077 }
2078
2079 *new_conf = *old_conf;
Lars Ellenberg5979e362011-04-27 21:09:55 +02002080 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02002081 set_net_conf_defaults(new_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +02002082
Lars Ellenbergf3990022011-03-23 14:31:09 +01002083 err = net_conf_from_attrs_for_change(new_conf, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02002084 if (err && err != -ENOMSG) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002085 retcode = ERR_MANDATORY_TAG;
2086 drbd_msg_put_info(from_attrs_err_to_txt(err));
2087 goto fail;
2088 }
2089
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002090 retcode = check_net_options(connection, new_conf);
Philipp Reisnercd643972011-04-13 18:00:59 -07002091 if (retcode != NO_ERROR)
2092 goto fail;
2093
Lars Ellenbergf3990022011-03-23 14:31:09 +01002094 /* re-sync running */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002095 rsr = conn_resync_running(connection);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002096 if (rsr && strcmp(new_conf->csums_alg, old_conf->csums_alg)) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002097 retcode = ERR_CSUMS_RESYNC_RUNNING;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002098 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002099 }
2100
Lars Ellenbergf3990022011-03-23 14:31:09 +01002101 /* online verify running */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002102 ovr = conn_ov_running(connection);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002103 if (ovr && strcmp(new_conf->verify_alg, old_conf->verify_alg)) {
2104 retcode = ERR_VERIFY_RUNNING;
2105 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002106 }
2107
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002108 retcode = alloc_crypto(&crypto, new_conf);
2109 if (retcode != NO_ERROR)
2110 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002111
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002112 rcu_assign_pointer(connection->net_conf, new_conf);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002113
2114 if (!rsr) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002115 crypto_free_hash(connection->csums_tfm);
2116 connection->csums_tfm = crypto.csums_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002117 crypto.csums_tfm = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002118 }
2119 if (!ovr) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002120 crypto_free_hash(connection->verify_tfm);
2121 connection->verify_tfm = crypto.verify_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002122 crypto.verify_tfm = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002123 }
2124
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002125 crypto_free_hash(connection->integrity_tfm);
2126 connection->integrity_tfm = crypto.integrity_tfm;
2127 if (connection->cstate >= C_WF_REPORT_PARAMS && connection->agreed_pro_version >= 100)
2128 /* Do this without trying to take connection->data.mutex again. */
2129 __drbd_send_protocol(connection, P_PROTOCOL_UPDATE);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002130
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002131 crypto_free_hash(connection->cram_hmac_tfm);
2132 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002133
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002134 mutex_unlock(&connection->conf_update);
2135 mutex_unlock(&connection->data.mutex);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002136 synchronize_rcu();
2137 kfree(old_conf);
2138
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002139 if (connection->cstate >= C_WF_REPORT_PARAMS)
2140 drbd_send_sync_param(minor_to_device(conn_lowest_minor(connection)));
Lars Ellenbergf3990022011-03-23 14:31:09 +01002141
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002142 goto done;
2143
Lars Ellenbergf3990022011-03-23 14:31:09 +01002144 fail:
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002145 mutex_unlock(&connection->conf_update);
2146 mutex_unlock(&connection->data.mutex);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002147 free_crypto(&crypto);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002148 kfree(new_conf);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002149 done:
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002150 conn_reconfig_done(connection);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002151 out:
2152 drbd_adm_finish(info, retcode);
2153 return 0;
2154}
2155
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002156int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002157{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002158 struct drbd_device *device;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002159 struct net_conf *old_conf, *new_conf = NULL;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002160 struct crypto crypto = { };
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002161 struct drbd_resource *resource;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002162 struct drbd_connection *connection;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002163 enum drbd_ret_code retcode;
2164 int i;
2165 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002166
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02002167 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002168
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002169 if (!adm_ctx.reply_skb)
2170 return retcode;
2171 if (retcode != NO_ERROR)
2172 goto out;
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002173 if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
2174 drbd_msg_put_info("connection endpoint(s) missing");
2175 retcode = ERR_INVALID_REQUEST;
2176 goto out;
2177 }
2178
2179 /* No need for _rcu here. All reconfiguration is
2180 * strictly serialized on genl_lock(). We are protected against
2181 * concurrent reconfiguration/addition/deletion */
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002182 for_each_resource(resource, &drbd_resources) {
2183 for_each_connection(connection, resource) {
2184 if (nla_len(adm_ctx.my_addr) == connection->my_addr_len &&
2185 !memcmp(nla_data(adm_ctx.my_addr), &connection->my_addr,
2186 connection->my_addr_len)) {
2187 retcode = ERR_LOCAL_ADDR;
2188 goto out;
2189 }
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002190
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002191 if (nla_len(adm_ctx.peer_addr) == connection->peer_addr_len &&
2192 !memcmp(nla_data(adm_ctx.peer_addr), &connection->peer_addr,
2193 connection->peer_addr_len)) {
2194 retcode = ERR_PEER_ADDR;
2195 goto out;
2196 }
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002197 }
2198 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002199
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002200 connection = adm_ctx.connection;
2201 conn_reconfig_start(connection);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002202
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002203 if (connection->cstate > C_STANDALONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002204 retcode = ERR_NET_CONFIGURED;
2205 goto fail;
2206 }
2207
Andreas Gruenbachera209b4a2011-08-17 12:43:25 +02002208 /* allocation not in the IO path, drbdsetup / netlink process context */
Lars Ellenberg5979e362011-04-27 21:09:55 +02002209 new_conf = kzalloc(sizeof(*new_conf), GFP_KERNEL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002210 if (!new_conf) {
2211 retcode = ERR_NOMEM;
2212 goto fail;
2213 }
2214
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02002215 set_net_conf_defaults(new_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002216
Lars Ellenbergf3990022011-03-23 14:31:09 +01002217 err = net_conf_from_attrs(new_conf, info);
Lars Ellenberg25e40932011-08-19 10:39:00 +02002218 if (err && err != -ENOMSG) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002219 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002220 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002221 goto fail;
2222 }
2223
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002224 retcode = check_net_options(connection, new_conf);
Philipp Reisnercd643972011-04-13 18:00:59 -07002225 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002226 goto fail;
Philipp Reisner47ff2d02010-06-18 13:56:57 +02002227
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002228 retcode = alloc_crypto(&crypto, new_conf);
2229 if (retcode != NO_ERROR)
Philipp Reisner422028b2010-10-27 11:12:07 +02002230 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002231
2232 ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
2233
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002234 conn_flush_workqueue(connection);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002235
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002236 mutex_lock(&connection->conf_update);
2237 old_conf = connection->net_conf;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002238 if (old_conf) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002239 retcode = ERR_NET_CONFIGURED;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002240 mutex_unlock(&connection->conf_update);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002241 goto fail;
2242 }
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002243 rcu_assign_pointer(connection->net_conf, new_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002244
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002245 conn_free_crypto(connection);
2246 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
2247 connection->integrity_tfm = crypto.integrity_tfm;
2248 connection->csums_tfm = crypto.csums_tfm;
2249 connection->verify_tfm = crypto.verify_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002250
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002251 connection->my_addr_len = nla_len(adm_ctx.my_addr);
2252 memcpy(&connection->my_addr, nla_data(adm_ctx.my_addr), connection->my_addr_len);
2253 connection->peer_addr_len = nla_len(adm_ctx.peer_addr);
2254 memcpy(&connection->peer_addr, nla_data(adm_ctx.peer_addr), connection->peer_addr_len);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002255
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002256 mutex_unlock(&connection->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002257
Philipp Reisner695d08f2011-04-11 22:53:32 -07002258 rcu_read_lock();
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002259 idr_for_each_entry(&connection->volumes, device, i) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002260 device->send_cnt = 0;
2261 device->recv_cnt = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002262 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07002263 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002264
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002265 retcode = conn_request_state(connection, NS(conn, C_UNCONNECTED), CS_VERBOSE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002266
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002267 conn_reconfig_done(connection);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002268 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002269 return 0;
2270
2271fail:
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002272 free_crypto(&crypto);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002273 kfree(new_conf);
2274
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002275 conn_reconfig_done(connection);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002276out:
2277 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002278 return 0;
2279}
2280
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002281static enum drbd_state_rv conn_try_disconnect(struct drbd_connection *connection, bool force)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002282{
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002283 enum drbd_state_rv rv;
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002284
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002285 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002286 force ? CS_HARD : 0);
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002287
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002288 switch (rv) {
2289 case SS_NOTHING_TO_DO:
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002290 break;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002291 case SS_ALREADY_STANDALONE:
2292 return SS_SUCCESS;
2293 case SS_PRIMARY_NOP:
2294 /* Our state checking code wants to see the peer outdated. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002295 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING, pdsk, D_OUTDATED), 0);
Philipp Reisner2bd5ed52013-03-27 14:08:40 +01002296
2297 if (rv == SS_OUTDATE_WO_CONN) /* lost connection before graceful disconnect succeeded */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002298 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_VERBOSE);
Philipp Reisner2bd5ed52013-03-27 14:08:40 +01002299
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002300 break;
2301 case SS_CW_FAILED_BY_PEER:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002302 /* The peer probably wants to see us outdated. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002303 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING,
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002304 disk, D_OUTDATED), 0);
2305 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002306 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002307 CS_HARD);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002308 }
2309 break;
2310 default:;
2311 /* no special handling necessary */
2312 }
2313
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002314 if (rv >= SS_SUCCESS) {
2315 enum drbd_state_rv rv2;
2316 /* No one else can reconfigure the network while I am here.
2317 * The state handling only uses drbd_thread_stop_nowait(),
2318 * we want to really wait here until the receiver is no more.
2319 */
Andreas Gruenbacher9693da22011-06-21 13:58:18 +02002320 drbd_thread_stop(&connection->receiver);
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002321
2322 /* Race breaker. This additional state change request may be
2323 * necessary, if this was a forced disconnect during a receiver
2324 * restart. We may have "killed" the receiver thread just
2325 * after drbdd_init() returned. Typically, we should be
2326 * C_STANDALONE already, now, and this becomes a no-op.
2327 */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002328 rv2 = conn_request_state(connection, NS(conn, C_STANDALONE),
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002329 CS_VERBOSE | CS_HARD);
2330 if (rv2 < SS_SUCCESS)
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002331 conn_err(connection,
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002332 "unexpected rv2=%d in conn_try_disconnect()\n",
2333 rv2);
2334 }
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002335 return rv;
2336}
2337
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002338int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002339{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002340 struct disconnect_parms parms;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002341 struct drbd_connection *connection;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002342 enum drbd_state_rv rv;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002343 enum drbd_ret_code retcode;
2344 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002345
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002346 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002347 if (!adm_ctx.reply_skb)
2348 return retcode;
2349 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002350 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002351
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002352 connection = adm_ctx.connection;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002353 memset(&parms, 0, sizeof(parms));
2354 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002355 err = disconnect_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002356 if (err) {
2357 retcode = ERR_MANDATORY_TAG;
2358 drbd_msg_put_info(from_attrs_err_to_txt(err));
2359 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002360 }
2361 }
2362
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002363 rv = conn_try_disconnect(connection, parms.force_disconnect);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002364 if (rv < SS_SUCCESS)
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002365 retcode = rv; /* FIXME: Type mismatch. */
2366 else
2367 retcode = NO_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002368 fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002369 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002370 return 0;
2371}
2372
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002373void resync_after_online_grow(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002374{
2375 int iass; /* I am sync source */
2376
2377 dev_info(DEV, "Resync of new storage after online grow\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002378 if (device->state.role != device->state.peer)
2379 iass = (device->state.role == R_PRIMARY);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002380 else
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002381 iass = test_bit(RESOLVE_CONFLICTS, &first_peer_device(device)->connection->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002382
2383 if (iass)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002384 drbd_start_resync(device, C_SYNC_SOURCE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002385 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002386 _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002387}
2388
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002389int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002390{
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002391 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002392 struct resize_parms rs;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002393 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002394 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002395 enum determine_dev_size dd;
Philipp Reisnerd752b262013-06-25 16:50:08 +02002396 bool change_al_layout = false;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002397 enum dds_flags ddsf;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002398 sector_t u_size;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002399 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002400
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002401 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2402 if (!adm_ctx.reply_skb)
2403 return retcode;
2404 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002405 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002406
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002407 device = adm_ctx.device;
2408 if (!get_ldev(device)) {
Philipp Reisnerd752b262013-06-25 16:50:08 +02002409 retcode = ERR_NO_DISK;
2410 goto fail;
2411 }
2412
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002413 memset(&rs, 0, sizeof(struct resize_parms));
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002414 rs.al_stripes = device->ldev->md.al_stripes;
2415 rs.al_stripe_size = device->ldev->md.al_stripe_size_4k * 4;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002416 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002417 err = resize_parms_from_attrs(&rs, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002418 if (err) {
2419 retcode = ERR_MANDATORY_TAG;
2420 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerd752b262013-06-25 16:50:08 +02002421 goto fail_ldev;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002422 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002423 }
2424
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002425 if (device->state.conn > C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002426 retcode = ERR_RESIZE_RESYNC;
Philipp Reisnerd752b262013-06-25 16:50:08 +02002427 goto fail_ldev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002428 }
2429
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002430 if (device->state.role == R_SECONDARY &&
2431 device->state.peer == R_SECONDARY) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002432 retcode = ERR_NO_PRIMARY;
Philipp Reisnerd752b262013-06-25 16:50:08 +02002433 goto fail_ldev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002434 }
2435
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002436 if (rs.no_resync && first_peer_device(device)->connection->agreed_pro_version < 93) {
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002437 retcode = ERR_NEED_APV_93;
Andreas Gruenbacher7b4e4d32011-09-28 22:15:04 +02002438 goto fail_ldev;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002439 }
2440
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002441 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002442 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002443 rcu_read_unlock();
2444 if (u_size != (sector_t)rs.resize_size) {
2445 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
2446 if (!new_disk_conf) {
2447 retcode = ERR_NOMEM;
Philipp Reisner9bcd2522011-09-29 13:00:14 +02002448 goto fail_ldev;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002449 }
2450 }
2451
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002452 if (device->ldev->md.al_stripes != rs.al_stripes ||
2453 device->ldev->md.al_stripe_size_4k != rs.al_stripe_size / 4) {
Philipp Reisnerd752b262013-06-25 16:50:08 +02002454 u32 al_size_k = rs.al_stripes * rs.al_stripe_size;
2455
2456 if (al_size_k > (16 * 1024 * 1024)) {
2457 retcode = ERR_MD_LAYOUT_TOO_BIG;
2458 goto fail_ldev;
2459 }
2460
2461 if (al_size_k < MD_32kB_SECT/2) {
2462 retcode = ERR_MD_LAYOUT_TOO_SMALL;
2463 goto fail_ldev;
2464 }
2465
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002466 if (device->state.conn != C_CONNECTED) {
Philipp Reisnerd752b262013-06-25 16:50:08 +02002467 retcode = ERR_MD_LAYOUT_CONNECTED;
2468 goto fail_ldev;
2469 }
2470
2471 change_al_layout = true;
2472 }
2473
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002474 if (device->ldev->known_size != drbd_get_capacity(device->ldev->backing_bdev))
2475 device->ldev->known_size = drbd_get_capacity(device->ldev->backing_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002476
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002477 if (new_disk_conf) {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002478 mutex_lock(&first_peer_device(device)->connection->conf_update);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002479 old_disk_conf = device->ldev->disk_conf;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002480 *new_disk_conf = *old_disk_conf;
2481 new_disk_conf->disk_size = (sector_t)rs.resize_size;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002482 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002483 mutex_unlock(&first_peer_device(device)->connection->conf_update);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002484 synchronize_rcu();
2485 kfree(old_disk_conf);
2486 }
2487
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002488 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002489 dd = drbd_determine_dev_size(device, ddsf, change_al_layout ? &rs : NULL);
2490 drbd_md_sync(device);
2491 put_ldev(device);
Philipp Reisnere96c9632013-06-25 16:50:07 +02002492 if (dd == DS_ERROR) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002493 retcode = ERR_NOMEM_BITMAP;
2494 goto fail;
Philipp Reisnerd752b262013-06-25 16:50:08 +02002495 } else if (dd == DS_ERROR_SPACE_MD) {
2496 retcode = ERR_MD_LAYOUT_NO_FIT;
2497 goto fail;
2498 } else if (dd == DS_ERROR_SHRINK) {
2499 retcode = ERR_IMPLICIT_SHRINK;
2500 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002501 }
2502
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002503 if (device->state.conn == C_CONNECTED) {
Philipp Reisnere96c9632013-06-25 16:50:07 +02002504 if (dd == DS_GREW)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002505 set_bit(RESIZE_PENDING, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002506
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002507 drbd_send_uuids(device);
2508 drbd_send_sizes(device, 1, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002509 }
2510
2511 fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002512 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002513 return 0;
Andreas Gruenbacher7b4e4d32011-09-28 22:15:04 +02002514
2515 fail_ldev:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002516 put_ldev(device);
Andreas Gruenbacher7b4e4d32011-09-28 22:15:04 +02002517 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002518}
2519
Lars Ellenbergf3990022011-03-23 14:31:09 +01002520int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002521{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002522 enum drbd_ret_code retcode;
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002523 struct res_opts res_opts;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002524 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002525
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02002526 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002527 if (!adm_ctx.reply_skb)
2528 return retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002529 if (retcode != NO_ERROR)
2530 goto fail;
2531
Andreas Gruenbachereb6bea62011-06-21 16:11:28 +02002532 res_opts = adm_ctx.resource->res_opts;
Lars Ellenberg5979e362011-04-27 21:09:55 +02002533 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02002534 set_res_opts_defaults(&res_opts);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002535
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002536 err = res_opts_from_attrs(&res_opts, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02002537 if (err && err != -ENOMSG) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002538 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002539 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002540 goto fail;
2541 }
2542
Andreas Gruenbachereb6bea62011-06-21 16:11:28 +02002543 err = set_resource_options(adm_ctx.resource, &res_opts);
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02002544 if (err) {
2545 retcode = ERR_INVALID_REQUEST;
2546 if (err == -ENOMEM)
Philipp Reisner778f2712010-07-06 11:14:00 +02002547 retcode = ERR_NOMEM;
Philipp Reisner778f2712010-07-06 11:14:00 +02002548 }
2549
Philipp Reisnerb411b362009-09-25 16:07:19 -07002550fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002551 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002552 return 0;
2553}
2554
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002555int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002556{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002557 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002558 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2559
2560 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2561 if (!adm_ctx.reply_skb)
2562 return retcode;
2563 if (retcode != NO_ERROR)
2564 goto out;
2565
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002566 device = adm_ctx.device;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002567
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002568 /* If there is still bitmap IO pending, probably because of a previous
Lars Ellenberg7ee1fb92012-06-19 10:27:58 +02002569 * resync just being finished, wait for it before requesting a new resync.
2570 * Also wait for it's after_state_ch(). */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002571 drbd_suspend_io(device);
2572 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
2573 drbd_flush_workqueue(device);
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002574
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002575 /* If we happen to be C_STANDALONE R_SECONDARY, just change to
2576 * D_INCONSISTENT, and set all bits in the bitmap. Otherwise,
2577 * try to start a resync handshake as sync target for full sync.
Philipp Reisner9376d9f2013-03-27 14:08:36 +01002578 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002579 if (device->state.conn == C_STANDALONE && device->state.role == R_SECONDARY) {
2580 retcode = drbd_request_state(device, NS(disk, D_INCONSISTENT));
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002581 if (retcode >= SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002582 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002583 "set_n_write from invalidate", BM_LOCKED_MASK))
2584 retcode = ERR_IO_MD_DISK;
2585 }
2586 } else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002587 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_T));
2588 drbd_resume_io(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002589
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002590out:
2591 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002592 return 0;
2593}
2594
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002595static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2596 union drbd_state mask, union drbd_state val)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002597{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002598 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002599
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002600 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2601 if (!adm_ctx.reply_skb)
2602 return retcode;
2603 if (retcode != NO_ERROR)
2604 goto out;
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002605
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002606 retcode = drbd_request_state(adm_ctx.device, mask, val);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002607out:
2608 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002609 return 0;
2610}
2611
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002612static int drbd_bmio_set_susp_al(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002613{
2614 int rv;
2615
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002616 rv = drbd_bmio_set_n_write(device);
2617 drbd_suspend_al(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002618 return rv;
2619}
2620
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002621int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002622{
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002623 int retcode; /* drbd_ret_code, drbd_state_rv */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002624 struct drbd_device *device;
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002625
2626 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2627 if (!adm_ctx.reply_skb)
2628 return retcode;
2629 if (retcode != NO_ERROR)
2630 goto out;
2631
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002632 device = adm_ctx.device;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002633
2634 /* If there is still bitmap IO pending, probably because of a previous
Lars Ellenberg7ee1fb92012-06-19 10:27:58 +02002635 * resync just being finished, wait for it before requesting a new resync.
2636 * Also wait for it's after_state_ch(). */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002637 drbd_suspend_io(device);
2638 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
2639 drbd_flush_workqueue(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002640
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002641 /* If we happen to be C_STANDALONE R_PRIMARY, just set all bits
2642 * in the bitmap. Otherwise, try to start a resync handshake
2643 * as sync source for full sync.
2644 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002645 if (device->state.conn == C_STANDALONE && device->state.role == R_PRIMARY) {
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002646 /* The peer will get a resync upon connect anyways. Just make that
2647 into a full resync. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002648 retcode = drbd_request_state(device, NS(pdsk, D_INCONSISTENT));
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002649 if (retcode >= SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002650 if (drbd_bitmap_io(device, &drbd_bmio_set_susp_al,
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002651 "set_n_write from invalidate_peer",
2652 BM_LOCKED_SET_ALLOWED))
2653 retcode = ERR_IO_MD_DISK;
2654 }
2655 } else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002656 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_S));
2657 drbd_resume_io(device);
Philipp Reisner07782862010-08-31 12:00:50 +02002658
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002659out:
2660 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002661 return 0;
2662}
2663
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002664int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002665{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002666 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002667
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002668 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2669 if (!adm_ctx.reply_skb)
2670 return retcode;
2671 if (retcode != NO_ERROR)
2672 goto out;
2673
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002674 if (drbd_request_state(adm_ctx.device, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002675 retcode = ERR_PAUSE_IS_SET;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002676out:
2677 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002678 return 0;
2679}
2680
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002681int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002682{
Philipp Reisnerda9fbc22011-03-29 10:52:01 +02002683 union drbd_dev_state s;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002684 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002685
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002686 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2687 if (!adm_ctx.reply_skb)
2688 return retcode;
2689 if (retcode != NO_ERROR)
2690 goto out;
2691
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002692 if (drbd_request_state(adm_ctx.device, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2693 s = adm_ctx.device->state;
Philipp Reisnercd88d032011-01-20 11:46:41 +01002694 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2695 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2696 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2697 } else {
2698 retcode = ERR_PAUSE_IS_CLEAR;
2699 }
2700 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002701
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002702out:
2703 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002704 return 0;
2705}
2706
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002707int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002708{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002709 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002710}
2711
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002712int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002713{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002714 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002715 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2716
2717 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2718 if (!adm_ctx.reply_skb)
2719 return retcode;
2720 if (retcode != NO_ERROR)
2721 goto out;
2722
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002723 device = adm_ctx.device;
2724 if (test_bit(NEW_CUR_UUID, &device->flags)) {
2725 drbd_uuid_new_current(device);
2726 clear_bit(NEW_CUR_UUID, &device->flags);
Philipp Reisner43a51822010-06-11 11:26:34 +02002727 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002728 drbd_suspend_io(device);
2729 retcode = drbd_request_state(device, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002730 if (retcode == SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002731 if (device->state.conn < C_CONNECTED)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002732 tl_clear(first_peer_device(device)->connection);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002733 if (device->state.disk == D_DISKLESS || device->state.disk == D_FAILED)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002734 tl_restart(first_peer_device(device)->connection, FAIL_FROZEN_DISK_IO);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002735 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002736 drbd_resume_io(device);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002737
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002738out:
2739 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002740 return 0;
2741}
2742
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002743int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002744{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002745 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002746}
2747
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002748static int nla_put_drbd_cfg_context(struct sk_buff *skb, struct drbd_connection *connection, unsigned vnr)
Lars Ellenberg543cc102011-03-10 22:18:18 +01002749{
2750 struct nlattr *nla;
2751 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
2752 if (!nla)
2753 goto nla_put_failure;
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002754 if (vnr != VOLUME_UNSPECIFIED &&
2755 nla_put_u32(skb, T_ctx_volume, vnr))
2756 goto nla_put_failure;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002757 if (nla_put_string(skb, T_ctx_resource_name, connection->resource->name))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002758 goto nla_put_failure;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002759 if (connection->my_addr_len &&
2760 nla_put(skb, T_ctx_my_addr, connection->my_addr_len, &connection->my_addr))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002761 goto nla_put_failure;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002762 if (connection->peer_addr_len &&
2763 nla_put(skb, T_ctx_peer_addr, connection->peer_addr_len, &connection->peer_addr))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002764 goto nla_put_failure;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002765 nla_nest_end(skb, nla);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002766 return 0;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002767
2768nla_put_failure:
2769 if (nla)
2770 nla_nest_cancel(skb, nla);
2771 return -EMSGSIZE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002772}
2773
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002774static int nla_put_status_info(struct sk_buff *skb, struct drbd_device *device,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002775 const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002776{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002777 struct state_info *si = NULL; /* for sizeof(si->member); */
2778 struct nlattr *nla;
2779 int got_ldev;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002780 int err = 0;
2781 int exclude_sensitive;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002782
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002783 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2784 * to. So we better exclude_sensitive information.
2785 *
2786 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2787 * in the context of the requesting user process. Exclude sensitive
2788 * information, unless current has superuser.
2789 *
2790 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2791 * relies on the current implementation of netlink_dump(), which
2792 * executes the dump callback successively from netlink_recvmsg(),
2793 * always in the context of the receiving process */
2794 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002795
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002796 got_ldev = get_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002797
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002798 /* We need to add connection name and volume number information still.
2799 * Minor number is in drbd_genlmsghdr. */
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002800 if (nla_put_drbd_cfg_context(skb, first_peer_device(device)->connection, device->vnr))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002801 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002802
Andreas Gruenbachereb6bea62011-06-21 16:11:28 +02002803 if (res_opts_to_skb(skb, &device->resource->res_opts, exclude_sensitive))
Lars Ellenbergf3990022011-03-23 14:31:09 +01002804 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002805
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002806 rcu_read_lock();
Andreas Gruenbacherf9eb7bf2013-06-25 16:50:05 +02002807 if (got_ldev) {
2808 struct disk_conf *disk_conf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002809
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002810 disk_conf = rcu_dereference(device->ldev->disk_conf);
Andreas Gruenbacherf9eb7bf2013-06-25 16:50:05 +02002811 err = disk_conf_to_skb(skb, disk_conf, exclude_sensitive);
2812 }
2813 if (!err) {
2814 struct net_conf *nc;
2815
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002816 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
Andreas Gruenbacherf9eb7bf2013-06-25 16:50:05 +02002817 if (nc)
2818 err = net_conf_to_skb(skb, nc, exclude_sensitive);
2819 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002820 rcu_read_unlock();
2821 if (err)
2822 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002823
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002824 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
2825 if (!nla)
2826 goto nla_put_failure;
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002827 if (nla_put_u32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY) ||
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002828 nla_put_u32(skb, T_current_state, device->state.i) ||
2829 nla_put_u64(skb, T_ed_uuid, device->ed_uuid) ||
2830 nla_put_u64(skb, T_capacity, drbd_get_capacity(device->this_bdev)) ||
2831 nla_put_u64(skb, T_send_cnt, device->send_cnt) ||
2832 nla_put_u64(skb, T_recv_cnt, device->recv_cnt) ||
2833 nla_put_u64(skb, T_read_cnt, device->read_cnt) ||
2834 nla_put_u64(skb, T_writ_cnt, device->writ_cnt) ||
2835 nla_put_u64(skb, T_al_writ_cnt, device->al_writ_cnt) ||
2836 nla_put_u64(skb, T_bm_writ_cnt, device->bm_writ_cnt) ||
2837 nla_put_u32(skb, T_ap_bio_cnt, atomic_read(&device->ap_bio_cnt)) ||
2838 nla_put_u32(skb, T_ap_pending_cnt, atomic_read(&device->ap_pending_cnt)) ||
2839 nla_put_u32(skb, T_rs_pending_cnt, atomic_read(&device->rs_pending_cnt)))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002840 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002841
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002842 if (got_ldev) {
Philipp Reisner39a1aa7f2012-08-08 21:19:09 +02002843 int err;
2844
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002845 spin_lock_irq(&device->ldev->md.uuid_lock);
2846 err = nla_put(skb, T_uuids, sizeof(si->uuids), device->ldev->md.uuid);
2847 spin_unlock_irq(&device->ldev->md.uuid_lock);
Philipp Reisner39a1aa7f2012-08-08 21:19:09 +02002848
2849 if (err)
2850 goto nla_put_failure;
2851
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002852 if (nla_put_u32(skb, T_disk_flags, device->ldev->md.flags) ||
2853 nla_put_u64(skb, T_bits_total, drbd_bm_bits(device)) ||
2854 nla_put_u64(skb, T_bits_oos, drbd_bm_total_weight(device)))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002855 goto nla_put_failure;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002856 if (C_SYNC_SOURCE <= device->state.conn &&
2857 C_PAUSED_SYNC_T >= device->state.conn) {
2858 if (nla_put_u64(skb, T_bits_rs_total, device->rs_total) ||
2859 nla_put_u64(skb, T_bits_rs_failed, device->rs_failed))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002860 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002861 }
2862 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002863
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002864 if (sib) {
2865 switch(sib->sib_reason) {
2866 case SIB_SYNC_PROGRESS:
2867 case SIB_GET_STATUS_REPLY:
2868 break;
2869 case SIB_STATE_CHANGE:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002870 if (nla_put_u32(skb, T_prev_state, sib->os.i) ||
2871 nla_put_u32(skb, T_new_state, sib->ns.i))
2872 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002873 break;
2874 case SIB_HELPER_POST:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002875 if (nla_put_u32(skb, T_helper_exit_code,
2876 sib->helper_exit_code))
2877 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002878 /* fall through */
2879 case SIB_HELPER_PRE:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002880 if (nla_put_string(skb, T_helper, sib->helper_name))
2881 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002882 break;
2883 }
2884 }
2885 nla_nest_end(skb, nla);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002886
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002887 if (0)
2888nla_put_failure:
2889 err = -EMSGSIZE;
2890 if (got_ldev)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002891 put_ldev(device);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002892 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002893}
2894
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002895int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002896{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002897 enum drbd_ret_code retcode;
2898 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002899
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002900 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2901 if (!adm_ctx.reply_skb)
2902 return retcode;
2903 if (retcode != NO_ERROR)
2904 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002905
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002906 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.device, NULL);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002907 if (err) {
2908 nlmsg_free(adm_ctx.reply_skb);
2909 return err;
2910 }
2911out:
2912 drbd_adm_finish(info, retcode);
2913 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002914}
2915
Rashika Kheria4b7a5302013-12-19 15:17:33 +05302916static int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002917{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002918 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002919 struct drbd_genlmsghdr *dh;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002920 struct drbd_resource *pos = (struct drbd_resource *)cb->args[0];
2921 struct drbd_resource *resource = NULL;
2922 struct drbd_connection *connection;
2923 struct drbd_resource *tmp;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002924 unsigned volume = cb->args[1];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002925
Lars Ellenberg543cc102011-03-10 22:18:18 +01002926 /* Open coded, deferred, iteration:
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002927 * for_each_resource_safe(resource, tmp, &drbd_resources) {
2928 * connection = "first connection of resource";
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002929 * idr_for_each_entry(&connection->volumes, device, i) {
Lars Ellenberg543cc102011-03-10 22:18:18 +01002930 * ...
2931 * }
2932 * }
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002933 * where resource is cb->args[0];
Lars Ellenberg543cc102011-03-10 22:18:18 +01002934 * and i is cb->args[1];
2935 *
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002936 * cb->args[2] indicates if we shall loop over all resources,
2937 * or just dump all volumes of a single resource.
2938 *
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002939 * This may miss entries inserted after this dump started,
2940 * or entries deleted before they are reached.
Lars Ellenberg543cc102011-03-10 22:18:18 +01002941 *
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002942 * We need to make sure the device won't disappear while
Lars Ellenberg543cc102011-03-10 22:18:18 +01002943 * we are looking at it, and revalidate our iterators
2944 * on each iteration.
2945 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002946
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02002947 /* synchronize with conn_create()/drbd_destroy_connection() */
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02002948 rcu_read_lock();
Lars Ellenberg543cc102011-03-10 22:18:18 +01002949 /* revalidate iterator position */
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002950 for_each_resource_rcu(tmp, &drbd_resources) {
Lars Ellenberg543cc102011-03-10 22:18:18 +01002951 if (pos == NULL) {
2952 /* first iteration */
2953 pos = tmp;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002954 resource = pos;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002955 break;
2956 }
2957 if (tmp == pos) {
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002958 resource = pos;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002959 break;
2960 }
2961 }
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002962 if (resource) {
2963next_resource:
2964 connection = first_connection(resource);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002965 device = idr_get_next(&connection->volumes, &volume);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002966 if (!device) {
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002967 /* No more volumes to dump on this resource.
2968 * Advance resource iterator. */
2969 pos = list_entry_rcu(resource->resources.next,
2970 struct drbd_resource, resources);
2971 /* Did we dump any volume of this resource yet? */
Lars Ellenberg543cc102011-03-10 22:18:18 +01002972 if (volume != 0) {
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002973 /* If we reached the end of the list,
2974 * or only a single resource dump was requested,
2975 * we are done. */
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002976 if (&pos->resources == &drbd_resources || cb->args[2])
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002977 goto out;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002978 volume = 0;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002979 resource = pos;
2980 goto next_resource;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002981 }
2982 }
2983
Philipp Reisner98683652012-11-09 14:18:43 +01002984 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002985 cb->nlh->nlmsg_seq, &drbd_genl_family,
2986 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
2987 if (!dh)
Lars Ellenberg543cc102011-03-10 22:18:18 +01002988 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002989
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002990 if (!device) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002991 /* This is a connection without a single volume.
Lars Ellenberg367d675d2011-07-11 23:49:55 +02002992 * Suprisingly enough, it may have a network
2993 * configuration. */
2994 struct net_conf *nc;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002995 dh->minor = -1U;
2996 dh->ret_code = NO_ERROR;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002997 if (nla_put_drbd_cfg_context(skb, connection, VOLUME_UNSPECIFIED))
Lars Ellenberg367d675d2011-07-11 23:49:55 +02002998 goto cancel;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002999 nc = rcu_dereference(connection->net_conf);
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003000 if (nc && net_conf_to_skb(skb, nc, 1) != 0)
3001 goto cancel;
3002 goto done;
Lars Ellenberg543cc102011-03-10 22:18:18 +01003003 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003004
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003005 D_ASSERT(device->vnr == volume);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02003006 D_ASSERT(first_peer_device(device)->connection == connection);
Lars Ellenberg543cc102011-03-10 22:18:18 +01003007
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003008 dh->minor = device_to_minor(device);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003009 dh->ret_code = NO_ERROR;
3010
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003011 if (nla_put_status_info(skb, device, NULL)) {
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003012cancel:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003013 genlmsg_cancel(skb, dh);
Lars Ellenberg543cc102011-03-10 22:18:18 +01003014 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003015 }
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003016done:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003017 genlmsg_end(skb, dh);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003018 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003019
Lars Ellenberg543cc102011-03-10 22:18:18 +01003020out:
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003021 rcu_read_unlock();
Lars Ellenberg543cc102011-03-10 22:18:18 +01003022 /* where to start the next iteration */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003023 cb->args[0] = (long)pos;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003024 cb->args[1] = (pos == resource) ? volume + 1 : 0;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003025
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003026 /* No more resources/volumes/minors found results in an empty skb.
Lars Ellenberg543cc102011-03-10 22:18:18 +01003027 * Which will terminate the dump. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003028 return skb->len;
3029}
3030
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003031/*
3032 * Request status of all resources, or of all volumes within a single resource.
3033 *
3034 * This is a dump, as the answer may not fit in a single reply skb otherwise.
3035 * Which means we cannot use the family->attrbuf or other such members, because
3036 * dump is NOT protected by the genl_lock(). During dump, we only have access
3037 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
3038 *
3039 * Once things are setup properly, we call into get_one_status().
Philipp Reisnerb411b362009-09-25 16:07:19 -07003040 */
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003041int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003042{
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003043 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
3044 struct nlattr *nla;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003045 const char *resource_name;
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003046 struct drbd_resource *resource;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003047 int maxtype;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003048
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003049 /* Is this a followup call? */
3050 if (cb->args[0]) {
3051 /* ... of a single resource dump,
3052 * and the resource iterator has been advanced already? */
3053 if (cb->args[2] && cb->args[2] != cb->args[0])
3054 return 0; /* DONE. */
3055 goto dump;
3056 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003057
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003058 /* First call (from netlink_dump_start). We need to figure out
3059 * which resource(s) the user wants us to dump. */
3060 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
3061 nlmsg_attrlen(cb->nlh, hdrlen),
3062 DRBD_NLA_CFG_CONTEXT);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003063
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003064 /* No explicit context given. Dump all. */
3065 if (!nla)
3066 goto dump;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003067 maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
3068 nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
3069 if (IS_ERR(nla))
3070 return PTR_ERR(nla);
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003071 /* context given, but no name present? */
3072 if (!nla)
3073 return -EINVAL;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003074 resource_name = nla_data(nla);
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003075 if (!*resource_name)
3076 return -ENODEV;
3077 resource = drbd_find_resource(resource_name);
3078 if (!resource)
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003079 return -ENODEV;
3080
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003081 kref_put(&resource->kref, drbd_destroy_resource); /* get_one_status() revalidates the resource */
Philipp Reisner0ace9df2011-04-24 10:53:19 +02003082
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003083 /* prime iterators, and set "filter" mode mark:
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003084 * only dump this connection. */
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003085 cb->args[0] = (long)resource;
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003086 /* cb->args[1] = 0; passed in this way. */
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003087 cb->args[2] = (long)resource;
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003088
3089dump:
3090 return get_one_status(skb, cb);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003091}
3092
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003093int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003094{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003095 enum drbd_ret_code retcode;
3096 struct timeout_parms tp;
3097 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003098
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003099 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3100 if (!adm_ctx.reply_skb)
3101 return retcode;
3102 if (retcode != NO_ERROR)
3103 goto out;
3104
3105 tp.timeout_type =
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003106 adm_ctx.device->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
3107 test_bit(USE_DEGR_WFC_T, &adm_ctx.device->flags) ? UT_DEGRADED :
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003108 UT_DEFAULT;
3109
3110 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
3111 if (err) {
3112 nlmsg_free(adm_ctx.reply_skb);
3113 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003114 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003115out:
3116 drbd_adm_finish(info, retcode);
3117 return 0;
3118}
Lars Ellenberg873b0d52011-01-21 22:53:48 +01003119
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003120int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
3121{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003122 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003123 enum drbd_ret_code retcode;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003124 struct start_ov_parms parms;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003125
3126 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3127 if (!adm_ctx.reply_skb)
3128 return retcode;
3129 if (retcode != NO_ERROR)
3130 goto out;
3131
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003132 device = adm_ctx.device;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003133
3134 /* resume from last known position, if possible */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003135 parms.ov_start_sector = device->ov_start_sector;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003136 parms.ov_stop_sector = ULLONG_MAX;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003137 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01003138 int err = start_ov_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003139 if (err) {
3140 retcode = ERR_MANDATORY_TAG;
3141 drbd_msg_put_info(from_attrs_err_to_txt(err));
3142 goto out;
3143 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003144 }
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003145 /* w_make_ov_request expects position to be aligned */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003146 device->ov_start_sector = parms.ov_start_sector & ~(BM_SECT_PER_BIT-1);
3147 device->ov_stop_sector = parms.ov_stop_sector;
Lars Ellenberg873b0d52011-01-21 22:53:48 +01003148
3149 /* If there is still bitmap IO pending, e.g. previous resync or verify
3150 * just being finished, wait for it before requesting a new resync. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003151 drbd_suspend_io(device);
3152 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
3153 retcode = drbd_request_state(device, NS(conn, C_VERIFY_S));
3154 drbd_resume_io(device);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003155out:
3156 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003157 return 0;
3158}
3159
3160
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003161int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003162{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003163 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003164 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003165 int skip_initial_sync = 0;
3166 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003167 struct new_c_uuid_parms args;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003168
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003169 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3170 if (!adm_ctx.reply_skb)
3171 return retcode;
3172 if (retcode != NO_ERROR)
3173 goto out_nolock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003174
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003175 device = adm_ctx.device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003176 memset(&args, 0, sizeof(args));
3177 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01003178 err = new_c_uuid_parms_from_attrs(&args, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003179 if (err) {
3180 retcode = ERR_MANDATORY_TAG;
3181 drbd_msg_put_info(from_attrs_err_to_txt(err));
3182 goto out_nolock;
3183 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003184 }
3185
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003186 mutex_lock(device->state_mutex); /* Protects us against serialized state changes. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003187
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003188 if (!get_ldev(device)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003189 retcode = ERR_NO_DISK;
3190 goto out;
3191 }
3192
3193 /* this is "skip initial sync", assume to be clean */
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02003194 if (device->state.conn == C_CONNECTED &&
3195 first_peer_device(device)->connection->agreed_pro_version >= 90 &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003196 device->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003197 dev_info(DEV, "Preparing to skip initial sync\n");
3198 skip_initial_sync = 1;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003199 } else if (device->state.conn != C_STANDALONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003200 retcode = ERR_CONNECTED;
3201 goto out_dec;
3202 }
3203
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003204 drbd_uuid_set(device, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
3205 drbd_uuid_new_current(device); /* New current, previous to UI_BITMAP */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003206
3207 if (args.clear_bm) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003208 err = drbd_bitmap_io(device, &drbd_bmio_clear_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003209 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003210 if (err) {
3211 dev_err(DEV, "Writing bitmap failed with %d\n",err);
3212 retcode = ERR_IO_MD_DISK;
3213 }
3214 if (skip_initial_sync) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003215 drbd_send_uuids_skip_initial_sync(device);
3216 _drbd_uuid_set(device, UI_BITMAP, 0);
3217 drbd_print_uuids(device, "cleared bitmap UUID");
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02003218 spin_lock_irq(&first_peer_device(device)->connection->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003219 _drbd_set_state(_NS2(device, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
Philipp Reisnerb411b362009-09-25 16:07:19 -07003220 CS_VERBOSE, NULL);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02003221 spin_unlock_irq(&first_peer_device(device)->connection->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003222 }
3223 }
3224
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003225 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003226out_dec:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003227 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003228out:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003229 mutex_unlock(device->state_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003230out_nolock:
3231 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003232 return 0;
3233}
3234
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003235static enum drbd_ret_code
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003236drbd_check_resource_name(const char *name)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003237{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003238 if (!name || !name[0]) {
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003239 drbd_msg_put_info("resource name missing");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003240 return ERR_MANDATORY_TAG;
3241 }
3242 /* if we want to use these in sysfs/configfs/debugfs some day,
3243 * we must not allow slashes */
3244 if (strchr(name, '/')) {
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003245 drbd_msg_put_info("invalid resource name");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003246 return ERR_INVALID_REQUEST;
3247 }
3248 return NO_ERROR;
3249}
Philipp Reisnerb411b362009-09-25 16:07:19 -07003250
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003251int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003252{
3253 enum drbd_ret_code retcode;
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003254 struct res_opts res_opts;
3255 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003256
3257 retcode = drbd_adm_prepare(skb, info, 0);
3258 if (!adm_ctx.reply_skb)
3259 return retcode;
3260 if (retcode != NO_ERROR)
3261 goto out;
3262
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003263 set_res_opts_defaults(&res_opts);
3264 err = res_opts_from_attrs(&res_opts, info);
3265 if (err && err != -ENOMSG) {
3266 retcode = ERR_MANDATORY_TAG;
3267 drbd_msg_put_info(from_attrs_err_to_txt(err));
3268 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003269 }
3270
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003271 retcode = drbd_check_resource_name(adm_ctx.resource_name);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003272 if (retcode != NO_ERROR)
3273 goto out;
3274
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003275 if (adm_ctx.connection) {
Lars Ellenberg38f19612011-03-14 13:22:35 +01003276 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
3277 retcode = ERR_INVALID_REQUEST;
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003278 drbd_msg_put_info("resource exists");
Lars Ellenberg38f19612011-03-14 13:22:35 +01003279 }
3280 /* else: still NO_ERROR */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003281 goto out;
Philipp Reisner9f5180e2009-10-06 09:30:14 +02003282 }
3283
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003284 if (!conn_create(adm_ctx.resource_name, &res_opts))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003285 retcode = ERR_NOMEM;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003286out:
3287 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003288 return 0;
3289}
3290
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003291int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003292{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003293 struct drbd_genlmsghdr *dh = info->userhdr;
3294 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003295
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02003296 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003297 if (!adm_ctx.reply_skb)
3298 return retcode;
3299 if (retcode != NO_ERROR)
3300 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003301
Andreas Gruenbacherf2257a52011-07-14 16:00:40 +02003302 if (dh->minor > MINORMASK) {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003303 drbd_msg_put_info("requested minor out of range");
3304 retcode = ERR_INVALID_REQUEST;
3305 goto out;
3306 }
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +02003307 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003308 drbd_msg_put_info("requested volume id out of range");
3309 retcode = ERR_INVALID_REQUEST;
3310 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05003311 }
3312
Lars Ellenberg38f19612011-03-14 13:22:35 +01003313 /* drbd_adm_prepare made sure already
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02003314 * that first_peer_device(device)->connection and device->vnr match the request. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003315 if (adm_ctx.device) {
Lars Ellenberg38f19612011-03-14 13:22:35 +01003316 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
3317 retcode = ERR_MINOR_EXISTS;
3318 /* else: still NO_ERROR */
3319 goto out;
3320 }
3321
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02003322 retcode = drbd_create_minor(adm_ctx.connection, dh->minor, adm_ctx.volume);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003323out:
3324 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003325 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003326}
3327
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003328static enum drbd_ret_code adm_del_minor(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003329{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003330 if (device->state.disk == D_DISKLESS &&
3331 /* no need to be device->state.conn == C_STANDALONE &&
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003332 * we may want to delete a minor from a live replication group.
3333 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003334 device->state.role == R_SECONDARY) {
3335 _drbd_request_state(device, NS(conn, C_WF_REPORT_PARAMS),
Philipp Reisner369bea62011-07-06 23:04:44 +02003336 CS_VERBOSE + CS_WAIT_COMPLETE);
Andreas Gruenbacher803ea132011-06-09 01:40:48 +02003337 drbd_delete_minor(device);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003338 return NO_ERROR;
3339 } else
3340 return ERR_MINOR_CONFIGURED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003341}
3342
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003343int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05003344{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003345 enum drbd_ret_code retcode;
3346
3347 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3348 if (!adm_ctx.reply_skb)
3349 return retcode;
3350 if (retcode != NO_ERROR)
3351 goto out;
3352
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003353 retcode = adm_del_minor(adm_ctx.device);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003354out:
3355 drbd_adm_finish(info, retcode);
3356 return 0;
3357}
3358
3359int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
3360{
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003361 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003362 struct drbd_device *device;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003363 unsigned i;
3364
Andreas Gruenbachera10f6b82011-06-09 01:26:16 +02003365 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003366 if (!adm_ctx.reply_skb)
3367 return retcode;
3368 if (retcode != NO_ERROR)
3369 goto out;
3370
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003371 /* demote */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003372 idr_for_each_entry(&adm_ctx.connection->volumes, device, i) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003373 retcode = drbd_set_role(device, R_SECONDARY, 0);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003374 if (retcode < SS_SUCCESS) {
3375 drbd_msg_put_info("failed to demote");
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003376 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003377 }
3378 }
3379
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003380 retcode = conn_try_disconnect(adm_ctx.connection, 0);
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003381 if (retcode < SS_SUCCESS) {
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003382 drbd_msg_put_info("failed to disconnect");
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003383 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003384 }
3385
3386 /* detach */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003387 idr_for_each_entry(&adm_ctx.connection->volumes, device, i) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003388 retcode = adm_detach(device, 0);
Lars Ellenberg27012382012-07-24 10:13:55 +02003389 if (retcode < SS_SUCCESS || retcode > NO_ERROR) {
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003390 drbd_msg_put_info("failed to detach");
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003391 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003392 }
3393 }
3394
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003395 /* If we reach this, all volumes (of this connection) are Secondary,
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003396 * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003397 * actually stopped, state handling only does drbd_thread_stop_nowait(). */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003398 drbd_thread_stop(&adm_ctx.connection->worker);
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003399
3400 /* Now, nothing can fail anymore */
3401
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003402 /* delete volumes */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003403 idr_for_each_entry(&adm_ctx.connection->volumes, device, i) {
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003404 retcode = adm_del_minor(device);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003405 if (retcode != NO_ERROR) {
3406 /* "can not happen" */
3407 drbd_msg_put_info("failed to delete volume");
Philipp Reisneref356262011-04-13 14:21:29 -07003408 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003409 }
3410 }
3411
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003412 /* delete connection */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003413 if (conn_lowest_minor(adm_ctx.connection) < 0) {
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003414 struct drbd_resource *resource = adm_ctx.connection->resource;
3415
3416 list_del_rcu(&resource->resources);
Philipp Reisnerec0bddb2011-05-04 15:47:01 +02003417 synchronize_rcu();
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003418 drbd_free_resource(resource);
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02003419
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003420 retcode = NO_ERROR;
3421 } else {
3422 /* "can not happen" */
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003423 retcode = ERR_RES_IN_USE;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003424 drbd_msg_put_info("failed to delete connection");
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003425 }
Philipp Reisneref356262011-04-13 14:21:29 -07003426 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003427out:
3428 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003429 return 0;
3430}
3431
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003432int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05003433{
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003434 struct drbd_resource *resource;
3435 struct drbd_connection *connection;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003436 enum drbd_ret_code retcode;
3437
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02003438 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003439 if (!adm_ctx.reply_skb)
3440 return retcode;
3441 if (retcode != NO_ERROR)
3442 goto out;
3443
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003444 resource = adm_ctx.resource;
3445 for_each_connection(connection, resource) {
3446 if (connection->cstate > C_STANDALONE) {
3447 retcode = ERR_NET_CONFIGURED;
3448 goto out;
3449 }
3450 }
3451 if (!idr_is_empty(&resource->devices)) {
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003452 retcode = ERR_RES_IN_USE;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003453 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05003454 }
3455
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003456 list_del_rcu(&resource->resources);
3457 for_each_connection(connection, resource)
3458 drbd_thread_stop(&connection->worker);
3459 synchronize_rcu();
3460 drbd_free_resource(resource);
3461 retcode = NO_ERROR;
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 Gruenbacherb30ab792011-07-03 13:26:43 +02003467void drbd_bcast_event(struct drbd_device *device, const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003468{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003469 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
3470 struct sk_buff *msg;
3471 struct drbd_genlmsghdr *d_out;
3472 unsigned seq;
3473 int err = -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003474
Philipp Reisneref86b772012-12-06 10:34:34 +01003475 if (sib->sib_reason == SIB_SYNC_PROGRESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003476 if (time_after(jiffies, device->rs_last_bcast + HZ))
3477 device->rs_last_bcast = jiffies;
Philipp Reisneref86b772012-12-06 10:34:34 +01003478 else
3479 return;
3480 }
Philipp Reisner328e0f122012-10-19 14:37:47 +02003481
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003482 seq = atomic_inc_return(&drbd_genl_seq);
3483 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
3484 if (!msg)
3485 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003486
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003487 err = -EMSGSIZE;
3488 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
3489 if (!d_out) /* cannot happen, but anyways. */
3490 goto nla_put_failure;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003491 d_out->minor = device_to_minor(device);
Andreas Gruenbacher6f9b5f82011-05-06 01:03:32 +02003492 d_out->ret_code = NO_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003493
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003494 if (nla_put_status_info(msg, device, sib))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003495 goto nla_put_failure;
3496 genlmsg_end(msg, d_out);
3497 err = drbd_genl_multicast_events(msg, 0);
3498 /* msg has been consumed or freed in netlink_broadcast() */
3499 if (err && err != -ESRCH)
3500 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003501
Philipp Reisnerb411b362009-09-25 16:07:19 -07003502 return;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003503
3504nla_put_failure:
3505 nlmsg_free(msg);
3506failed:
3507 dev_err(DEV, "Error %d while broadcasting event. "
3508 "Event seq:%u sib_reason:%u\n",
3509 err, seq, sib->sib_reason);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003510}