blob: c352c61f74c344a3c4e604047b70fee94424ec7d [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 Gruenbacherc06ece62011-06-21 17:23:59 +0200418 struct drbd_peer_device *peer_device;
Philipp Reisnercb703452011-03-24 11:03:07 +0100419 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700420
Philipp Reisner695d08f2011-04-11 22:53:32 -0700421 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200422 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
423 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200424 if (get_ldev_if_state(device, D_CONSISTENT)) {
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200425 struct disk_conf *disk_conf =
426 rcu_dereference(peer_device->device->ldev->disk_conf);
427 fp = max_t(enum drbd_fencing_p, fp, disk_conf->fencing);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200428 put_ldev(device);
Philipp Reisnercb703452011-03-24 11:03:07 +0100429 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700430 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700431 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700432
Philipp Reisnercb703452011-03-24 11:03:07 +0100433 return fp;
434}
435
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200436bool conn_try_outdate_peer(struct drbd_connection *connection)
Philipp Reisnercb703452011-03-24 11:03:07 +0100437{
Philipp Reisner28e448b2013-06-25 16:50:06 +0200438 unsigned int connect_cnt;
Philipp Reisnercb703452011-03-24 11:03:07 +0100439 union drbd_state mask = { };
440 union drbd_state val = { };
441 enum drbd_fencing_p fp;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700442 char *ex_to_string;
443 int r;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700444
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200445 if (connection->cstate >= C_WF_REPORT_PARAMS) {
446 conn_err(connection, "Expected cstate < C_WF_REPORT_PARAMS\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100447 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700448 }
449
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200450 spin_lock_irq(&connection->req_lock);
451 connect_cnt = connection->connect_cnt;
452 spin_unlock_irq(&connection->req_lock);
Philipp Reisner28e448b2013-06-25 16:50:06 +0200453
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200454 fp = highest_fencing_policy(connection);
Philipp Reisnercb703452011-03-24 11:03:07 +0100455 switch (fp) {
456 case FP_NOT_AVAIL:
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200457 conn_warn(connection, "Not fencing peer, I'm not even Consistent myself.\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100458 goto out;
459 case FP_DONT_CARE:
460 return true;
461 default: ;
462 }
463
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200464 r = conn_khelper(connection, "fence-peer");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700465
466 switch ((r>>8) & 0xff) {
467 case 3: /* peer is inconsistent */
468 ex_to_string = "peer is inconsistent or worse";
Philipp Reisnercb703452011-03-24 11:03:07 +0100469 mask.pdsk = D_MASK;
470 val.pdsk = D_INCONSISTENT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700471 break;
472 case 4: /* peer got outdated, or was already outdated */
473 ex_to_string = "peer was fenced";
Philipp Reisnercb703452011-03-24 11:03:07 +0100474 mask.pdsk = D_MASK;
475 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700476 break;
477 case 5: /* peer was down */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200478 if (conn_highest_disk(connection) == D_UP_TO_DATE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700479 /* we will(have) create(d) a new UUID anyways... */
480 ex_to_string = "peer is unreachable, assumed to be dead";
Philipp Reisnercb703452011-03-24 11:03:07 +0100481 mask.pdsk = D_MASK;
482 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700483 } else {
484 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
Philipp Reisnerb411b362009-09-25 16:07:19 -0700485 }
486 break;
487 case 6: /* Peer is primary, voluntarily outdate myself.
488 * This is useful when an unconnected R_SECONDARY is asked to
489 * become R_PRIMARY, but finds the other peer being active. */
490 ex_to_string = "peer is active";
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200491 conn_warn(connection, "Peer is primary, outdating myself.\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100492 mask.disk = D_MASK;
493 val.disk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700494 break;
495 case 7:
496 if (fp != FP_STONITH)
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200497 conn_err(connection, "fence-peer() = 7 && fencing != Stonith !!!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700498 ex_to_string = "peer was stonithed";
Philipp Reisnercb703452011-03-24 11:03:07 +0100499 mask.pdsk = D_MASK;
500 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700501 break;
502 default:
503 /* The script is broken ... */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200504 conn_err(connection, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
Philipp Reisnercb703452011-03-24 11:03:07 +0100505 return false; /* Eventually leave IO frozen */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700506 }
507
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200508 conn_info(connection, "fence-peer helper returned %d (%s)\n",
Philipp Reisnercb703452011-03-24 11:03:07 +0100509 (r>>8) & 0xff, ex_to_string);
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200510
Philipp Reisnercb703452011-03-24 11:03:07 +0100511 out:
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200512
Philipp Reisnercb703452011-03-24 11:03:07 +0100513 /* Not using
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200514 conn_request_state(connection, mask, val, CS_VERBOSE);
Philipp Reisnercb703452011-03-24 11:03:07 +0100515 here, because we might were able to re-establish the connection in the
516 meantime. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200517 spin_lock_irq(&connection->req_lock);
518 if (connection->cstate < C_WF_REPORT_PARAMS && !test_bit(STATE_SENT, &connection->flags)) {
519 if (connection->connect_cnt != connect_cnt)
Philipp Reisner28e448b2013-06-25 16:50:06 +0200520 /* In case the connection was established and droped
521 while the fence-peer handler was running, ignore it */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200522 conn_info(connection, "Ignoring fence-peer exit code\n");
Philipp Reisner28e448b2013-06-25 16:50:06 +0200523 else
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200524 _conn_request_state(connection, mask, val, CS_VERBOSE);
Philipp Reisner28e448b2013-06-25 16:50:06 +0200525 }
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200526 spin_unlock_irq(&connection->req_lock);
Philipp Reisnercb703452011-03-24 11:03:07 +0100527
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200528 return conn_highest_pdsk(connection) <= D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700529}
530
Philipp Reisner87f7be42010-06-11 13:56:33 +0200531static int _try_outdate_peer_async(void *data)
532{
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200533 struct drbd_connection *connection = (struct drbd_connection *)data;
Philipp Reisner87f7be42010-06-11 13:56:33 +0200534
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200535 conn_try_outdate_peer(connection);
Philipp Reisner21423fa2011-05-17 14:19:41 +0200536
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +0200537 kref_put(&connection->kref, drbd_destroy_connection);
Philipp Reisner87f7be42010-06-11 13:56:33 +0200538 return 0;
539}
540
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200541void conn_try_outdate_peer_async(struct drbd_connection *connection)
Philipp Reisner87f7be42010-06-11 13:56:33 +0200542{
543 struct task_struct *opa;
544
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200545 kref_get(&connection->kref);
546 opa = kthread_run(_try_outdate_peer_async, connection, "drbd_async_h");
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200547 if (IS_ERR(opa)) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200548 conn_err(connection, "out of mem, failed to invoke fence-peer helper\n");
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +0200549 kref_put(&connection->kref, drbd_destroy_connection);
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200550 }
Philipp Reisner87f7be42010-06-11 13:56:33 +0200551}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700552
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100553enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200554drbd_set_role(struct drbd_device *device, enum drbd_role new_role, int force)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700555{
556 const int max_tries = 4;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100557 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200558 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700559 int try = 0;
560 int forced = 0;
561 union drbd_state mask, val;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700562
563 if (new_role == R_PRIMARY)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200564 request_ping(first_peer_device(device)->connection); /* Detect a dead peer ASAP */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700565
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200566 mutex_lock(device->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700567
568 mask.i = 0; mask.role = R_MASK;
569 val.i = 0; val.role = new_role;
570
571 while (try++ < max_tries) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200572 rv = _drbd_request_state(device, mask, val, CS_WAIT_COMPLETE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700573
574 /* in case we first succeeded to outdate,
575 * but now suddenly could establish a connection */
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100576 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700577 val.pdsk = 0;
578 mask.pdsk = 0;
579 continue;
580 }
581
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100582 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200583 (device->state.disk < D_UP_TO_DATE &&
584 device->state.disk >= D_INCONSISTENT)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700585 mask.disk = D_MASK;
586 val.disk = D_UP_TO_DATE;
587 forced = 1;
588 continue;
589 }
590
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100591 if (rv == SS_NO_UP_TO_DATE_DISK &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200592 device->state.disk == D_CONSISTENT && mask.pdsk == 0) {
593 D_ASSERT(device->state.pdsk == D_UNKNOWN);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700594
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200595 if (conn_try_outdate_peer(first_peer_device(device)->connection)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700596 val.disk = D_UP_TO_DATE;
597 mask.disk = D_MASK;
598 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700599 continue;
600 }
601
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100602 if (rv == SS_NOTHING_TO_DO)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100603 goto out;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100604 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200605 if (!conn_try_outdate_peer(first_peer_device(device)->connection) && force) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700606 dev_warn(DEV, "Forced into split brain situation!\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100607 mask.pdsk = D_MASK;
608 val.pdsk = D_OUTDATED;
609
Philipp Reisnerb411b362009-09-25 16:07:19 -0700610 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700611 continue;
612 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100613 if (rv == SS_TWO_PRIMARIES) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700614 /* Maybe the peer is detected as dead very soon...
615 retry at most once more in this case. */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200616 int timeo;
617 rcu_read_lock();
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200618 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +0200619 timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
620 rcu_read_unlock();
621 schedule_timeout_interruptible(timeo);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700622 if (try < max_tries)
623 try = max_tries - 1;
624 continue;
625 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100626 if (rv < SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200627 rv = _drbd_request_state(device, mask, val,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700628 CS_VERBOSE + CS_WAIT_COMPLETE);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100629 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100630 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700631 }
632 break;
633 }
634
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100635 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100636 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700637
638 if (forced)
639 dev_warn(DEV, "Forced to consider local data as UpToDate!\n");
640
641 /* Wait until nothing is on the fly :) */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200642 wait_event(device->misc_wait, atomic_read(&device->ap_pending_cnt) == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700643
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100644 /* FIXME also wait for all pending P_BARRIER_ACK? */
645
Philipp Reisnerb411b362009-09-25 16:07:19 -0700646 if (new_role == R_SECONDARY) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200647 set_disk_ro(device->vdisk, true);
648 if (get_ldev(device)) {
649 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
650 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700651 }
652 } else {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200653 mutex_lock(&first_peer_device(device)->connection->conf_update);
654 nc = first_peer_device(device)->connection->net_conf;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200655 if (nc)
Andreas Gruenbacher6139f602011-05-06 20:00:02 +0200656 nc->discard_my_data = 0; /* without copy; single bit op is atomic */
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200657 mutex_unlock(&first_peer_device(device)->connection->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +0200658
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200659 set_disk_ro(device->vdisk, false);
660 if (get_ldev(device)) {
661 if (((device->state.conn < C_CONNECTED ||
662 device->state.pdsk <= D_FAILED)
663 && device->ldev->md.uuid[UI_BITMAP] == 0) || forced)
664 drbd_uuid_new_current(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700665
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200666 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
667 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700668 }
669 }
670
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100671 /* writeout of activity log covered areas of the bitmap
672 * to stable storage done in after state change already */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700673
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200674 if (device->state.conn >= C_WF_REPORT_PARAMS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700675 /* if this was forced, we should consider sync */
676 if (forced)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200677 drbd_send_uuids(device);
678 drbd_send_current_state(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700679 }
680
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200681 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700682
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200683 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100684out:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200685 mutex_unlock(device->state_mutex);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100686 return rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700687}
688
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100689static const char *from_attrs_err_to_txt(int err)
Lars Ellenbergef50a3e2010-09-01 14:39:30 +0200690{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100691 return err == -ENOMSG ? "required attribute missing" :
692 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
Lars Ellenbergf3990022011-03-23 14:31:09 +0100693 err == -EEXIST ? "can not change invariant setting" :
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100694 "invalid attribute value";
Lars Ellenbergef50a3e2010-09-01 14:39:30 +0200695}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700696
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100697int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700698{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100699 struct set_role_parms parms;
700 int err;
701 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700702
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100703 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
704 if (!adm_ctx.reply_skb)
705 return retcode;
706 if (retcode != NO_ERROR)
707 goto out;
708
709 memset(&parms, 0, sizeof(parms));
710 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +0100711 err = set_role_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100712 if (err) {
713 retcode = ERR_MANDATORY_TAG;
714 drbd_msg_put_info(from_attrs_err_to_txt(err));
715 goto out;
716 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700717 }
718
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100719 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200720 retcode = drbd_set_role(adm_ctx.device, R_PRIMARY, parms.assume_uptodate);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100721 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200722 retcode = drbd_set_role(adm_ctx.device, R_SECONDARY, 0);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100723out:
724 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700725 return 0;
726}
727
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100728/* Initializes the md.*_offset members, so we are able to find
729 * the on disk meta data.
730 *
731 * We currently have two possible layouts:
732 * external:
733 * |----------- md_size_sect ------------------|
734 * [ 4k superblock ][ activity log ][ Bitmap ]
735 * | al_offset == 8 |
736 * | bm_offset = al_offset + X |
737 * ==> bitmap sectors = md_size_sect - bm_offset
738 *
739 * internal:
740 * |----------- md_size_sect ------------------|
741 * [data.....][ Bitmap ][ activity log ][ 4k superblock ]
742 * | al_offset < 0 |
743 * | bm_offset = al_offset - Y |
744 * ==> bitmap sectors = Y = al_offset - bm_offset
745 *
746 * Activity log size used to be fixed 32kB,
747 * but is about to become configurable.
748 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200749static void drbd_md_set_sector_offsets(struct drbd_device *device,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700750 struct drbd_backing_dev *bdev)
751{
752 sector_t md_size_sect = 0;
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +0100753 unsigned int al_size_sect = bdev->md.al_size_4k * 8;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200754
Lars Ellenberg3a4d4eb2013-03-19 18:16:44 +0100755 bdev->md.md_offset = drbd_md_ss(bdev);
756
Lars Ellenberg68e41a42013-03-19 18:16:45 +0100757 switch (bdev->md.meta_dev_idx) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700758 default:
759 /* v07 style fixed size indexed meta data */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100760 bdev->md.md_size_sect = MD_128MB_SECT;
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100761 bdev->md.al_offset = MD_4kB_SECT;
762 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700763 break;
764 case DRBD_MD_INDEX_FLEX_EXT:
765 /* just occupy the full device; unit: sectors */
766 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100767 bdev->md.al_offset = MD_4kB_SECT;
768 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700769 break;
770 case DRBD_MD_INDEX_INTERNAL:
771 case DRBD_MD_INDEX_FLEX_INT:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700772 /* al size is still fixed */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100773 bdev->md.al_offset = -al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700774 /* we need (slightly less than) ~ this much bitmap sectors: */
775 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
776 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
777 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
778 md_size_sect = ALIGN(md_size_sect, 8);
779
780 /* plus the "drbd meta data super block",
781 * and the activity log; */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100782 md_size_sect += MD_4kB_SECT + al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700783
784 bdev->md.md_size_sect = md_size_sect;
785 /* bitmap offset is adjusted by 'super' block size */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100786 bdev->md.bm_offset = -md_size_sect + MD_4kB_SECT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700787 break;
788 }
789}
790
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100791/* input size is expected to be in KB */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700792char *ppsize(char *buf, unsigned long long size)
793{
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100794 /* Needs 9 bytes at max including trailing NUL:
795 * -1ULL ==> "16384 EB" */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700796 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
797 int base = 0;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100798 while (size >= 10000 && base < sizeof(units)-1) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700799 /* shift + round */
800 size = (size >> 10) + !!(size & (1<<9));
801 base++;
802 }
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100803 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700804
805 return buf;
806}
807
808/* there is still a theoretical deadlock when called from receiver
809 * on an D_INCONSISTENT R_PRIMARY:
810 * remote READ does inc_ap_bio, receiver would need to receive answer
811 * packet from remote to dec_ap_bio again.
812 * receiver receive_sizes(), comes here,
813 * waits for ap_bio_cnt == 0. -> deadlock.
814 * but this cannot happen, actually, because:
815 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
816 * (not connected, or bad/no disk on peer):
817 * see drbd_fail_request_early, ap_bio_cnt is zero.
818 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
819 * peer may not initiate a resize.
820 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100821/* Note these are not to be confused with
822 * drbd_adm_suspend_io/drbd_adm_resume_io,
823 * which are (sub) state changes triggered by admin (drbdsetup),
824 * and can be long lived.
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200825 * This changes an device->flag, is triggered by drbd internals,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100826 * and should be short-lived. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200827void drbd_suspend_io(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700828{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200829 set_bit(SUSPEND_IO, &device->flags);
830 if (drbd_suspended(device))
Philipp Reisner265be2d2010-05-31 10:14:17 +0200831 return;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200832 wait_event(device->misc_wait, !atomic_read(&device->ap_bio_cnt));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700833}
834
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200835void drbd_resume_io(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700836{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200837 clear_bit(SUSPEND_IO, &device->flags);
838 wake_up(&device->misc_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700839}
840
841/**
842 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200843 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700844 *
845 * Returns 0 on success, negative return values indicate errors.
846 * You should call drbd_md_sync() after calling this function.
847 */
Philipp Reisnerd752b262013-06-25 16:50:08 +0200848enum determine_dev_size
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200849drbd_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 -0700850{
851 sector_t prev_first_sect, prev_size; /* previous meta location */
Lars Ellenbergcccac982013-03-19 18:16:46 +0100852 sector_t la_size_sect, u_size;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200853 struct drbd_md *md = &device->ldev->md;
Philipp Reisnerd752b262013-06-25 16:50:08 +0200854 u32 prev_al_stripe_size_4k;
855 u32 prev_al_stripes;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700856 sector_t size;
857 char ppb[10];
Philipp Reisnerd752b262013-06-25 16:50:08 +0200858 void *buffer;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700859
860 int md_moved, la_size_changed;
Philipp Reisnere96c9632013-06-25 16:50:07 +0200861 enum determine_dev_size rv = DS_UNCHANGED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700862
863 /* race:
864 * application request passes inc_ap_bio,
865 * but then cannot get an AL-reference.
866 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
867 *
868 * to avoid that:
869 * Suspend IO right here.
870 * still lock the act_log to not trigger ASSERTs there.
871 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200872 drbd_suspend_io(device);
873 buffer = drbd_md_get_buffer(device); /* Lock meta-data IO */
Philipp Reisnerd752b262013-06-25 16:50:08 +0200874 if (!buffer) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200875 drbd_resume_io(device);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200876 return DS_ERROR;
877 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700878
879 /* no wait necessary anymore, actually we could assert that */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200880 wait_event(device->al_wait, lc_try_lock(device->act_log));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700881
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200882 prev_first_sect = drbd_md_first_sector(device->ldev);
883 prev_size = device->ldev->md.md_size_sect;
884 la_size_sect = device->ldev->md.la_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700885
Philipp Reisnerd752b262013-06-25 16:50:08 +0200886 if (rs) {
887 /* rs is non NULL if we should change the AL layout only */
888
889 prev_al_stripes = md->al_stripes;
890 prev_al_stripe_size_4k = md->al_stripe_size_4k;
891
892 md->al_stripes = rs->al_stripes;
893 md->al_stripe_size_4k = rs->al_stripe_size / 4;
894 md->al_size_4k = (u64)rs->al_stripes * rs->al_stripe_size / 4;
895 }
896
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200897 drbd_md_set_sector_offsets(device, device->ldev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700898
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200899 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200900 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200901 rcu_read_unlock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200902 size = drbd_new_dev_size(device, device->ldev, u_size, flags & DDSF_FORCED);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700903
Philipp Reisnerd752b262013-06-25 16:50:08 +0200904 if (size < la_size_sect) {
905 if (rs && u_size == 0) {
906 /* Remove "rs &&" later. This check should always be active, but
907 right now the receiver expects the permissive behavior */
908 dev_warn(DEV, "Implicit shrink not allowed. "
909 "Use --size=%llus for explicit shrink.\n",
910 (unsigned long long)size);
911 rv = DS_ERROR_SHRINK;
912 }
913 if (u_size > size)
914 rv = DS_ERROR_SPACE_MD;
915 if (rv != DS_UNCHANGED)
916 goto err_out;
917 }
918
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200919 if (drbd_get_capacity(device->this_bdev) != size ||
920 drbd_bm_capacity(device) != size) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700921 int err;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200922 err = drbd_bm_resize(device, size, !(flags & DDSF_NO_RESYNC));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700923 if (unlikely(err)) {
924 /* currently there is only one error: ENOMEM! */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200925 size = drbd_bm_capacity(device)>>1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700926 if (size == 0) {
927 dev_err(DEV, "OUT OF MEMORY! "
928 "Could not allocate bitmap!\n");
929 } else {
930 dev_err(DEV, "BM resizing failed. "
931 "Leaving size unchanged at size = %lu KB\n",
932 (unsigned long)size);
933 }
Philipp Reisnere96c9632013-06-25 16:50:07 +0200934 rv = DS_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700935 }
936 /* racy, see comments above. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200937 drbd_set_my_capacity(device, size);
938 device->ldev->md.la_size_sect = size;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700939 dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
940 (unsigned long long)size>>1);
941 }
Philipp Reisnerd752b262013-06-25 16:50:08 +0200942 if (rv <= DS_ERROR)
943 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700944
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200945 la_size_changed = (la_size_sect != device->ldev->md.la_size_sect);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700946
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200947 md_moved = prev_first_sect != drbd_md_first_sector(device->ldev)
948 || prev_size != device->ldev->md.md_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700949
Philipp Reisnerd752b262013-06-25 16:50:08 +0200950 if (la_size_changed || md_moved || rs) {
951 u32 prev_flags;
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100952
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200953 drbd_al_shrink(device); /* All extents inactive. */
Philipp Reisnerd752b262013-06-25 16:50:08 +0200954
955 prev_flags = md->flags;
956 md->flags &= ~MDF_PRIMARY_IND;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200957 drbd_md_write(device, buffer);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200958
Philipp Reisnerb411b362009-09-25 16:07:19 -0700959 dev_info(DEV, "Writing the whole bitmap, %s\n",
960 la_size_changed && md_moved ? "size changed and md moved" :
961 la_size_changed ? "size changed" : "md moved");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100962 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200963 drbd_bitmap_io(device, md_moved ? &drbd_bm_write_all : &drbd_bm_write,
Philipp Reisnerd752b262013-06-25 16:50:08 +0200964 "size changed", BM_LOCKED_MASK);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200965 drbd_initialize_al(device, buffer);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200966
967 md->flags = prev_flags;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200968 drbd_md_write(device, buffer);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200969
970 if (rs)
971 dev_info(DEV, "Changed AL layout to al-stripes = %d, al-stripe-size-kB = %d\n",
972 md->al_stripes, md->al_stripe_size_4k * 4);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700973 }
974
Lars Ellenbergcccac982013-03-19 18:16:46 +0100975 if (size > la_size_sect)
Philipp Reisner57737ad2013-10-23 10:59:17 +0200976 rv = la_size_sect ? DS_GREW : DS_GREW_FROM_ZERO;
Lars Ellenbergcccac982013-03-19 18:16:46 +0100977 if (size < la_size_sect)
Philipp Reisnere96c9632013-06-25 16:50:07 +0200978 rv = DS_SHRUNK;
Philipp Reisnerd752b262013-06-25 16:50:08 +0200979
980 if (0) {
981 err_out:
982 if (rs) {
983 md->al_stripes = prev_al_stripes;
984 md->al_stripe_size_4k = prev_al_stripe_size_4k;
985 md->al_size_4k = (u64)prev_al_stripes * prev_al_stripe_size_4k;
986
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200987 drbd_md_set_sector_offsets(device, device->ldev);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200988 }
989 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200990 lc_unlock(device->act_log);
991 wake_up(&device->al_wait);
992 drbd_md_put_buffer(device);
993 drbd_resume_io(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700994
995 return rv;
996}
997
998sector_t
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200999drbd_new_dev_size(struct drbd_device *device, struct drbd_backing_dev *bdev,
Philipp Reisneref5e44a2011-05-03 13:27:43 +02001000 sector_t u_size, int assume_peer_has_space)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001001{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001002 sector_t p_size = device->p_size; /* partner's disk size. */
Lars Ellenbergcccac982013-03-19 18:16:46 +01001003 sector_t la_size_sect = bdev->md.la_size_sect; /* last agreed size. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001004 sector_t m_size; /* my size */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001005 sector_t size = 0;
1006
1007 m_size = drbd_get_max_capacity(bdev);
1008
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001009 if (device->state.conn < C_CONNECTED && assume_peer_has_space) {
Philipp Reisnera393db62009-12-22 13:35:52 +01001010 dev_warn(DEV, "Resize while not connected was forced by the user!\n");
1011 p_size = m_size;
1012 }
1013
Philipp Reisnerb411b362009-09-25 16:07:19 -07001014 if (p_size && m_size) {
1015 size = min_t(sector_t, p_size, m_size);
1016 } else {
Lars Ellenbergcccac982013-03-19 18:16:46 +01001017 if (la_size_sect) {
1018 size = la_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001019 if (m_size && m_size < size)
1020 size = m_size;
1021 if (p_size && p_size < size)
1022 size = p_size;
1023 } else {
1024 if (m_size)
1025 size = m_size;
1026 if (p_size)
1027 size = p_size;
1028 }
1029 }
1030
1031 if (size == 0)
1032 dev_err(DEV, "Both nodes diskless!\n");
1033
1034 if (u_size) {
1035 if (u_size > size)
1036 dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n",
1037 (unsigned long)u_size>>1, (unsigned long)size>>1);
1038 else
1039 size = u_size;
1040 }
1041
1042 return size;
1043}
1044
1045/**
1046 * drbd_check_al_size() - Ensures that the AL is of the right size
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001047 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001048 *
1049 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
1050 * failed, and 0 on success. You should call drbd_md_sync() after you called
1051 * this function.
1052 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001053static int drbd_check_al_size(struct drbd_device *device, struct disk_conf *dc)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001054{
1055 struct lru_cache *n, *t;
1056 struct lc_element *e;
1057 unsigned int in_use;
1058 int i;
1059
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001060 if (device->act_log &&
1061 device->act_log->nr_elements == dc->al_extents)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001062 return 0;
1063
1064 in_use = 0;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001065 t = device->act_log;
Lars Ellenberg7ad651b2011-02-21 13:21:03 +01001066 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
Lars Ellenbergf3990022011-03-23 14:31:09 +01001067 dc->al_extents, sizeof(struct lc_element), 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001068
1069 if (n == NULL) {
1070 dev_err(DEV, "Cannot allocate act_log lru!\n");
1071 return -ENOMEM;
1072 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001073 spin_lock_irq(&device->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001074 if (t) {
1075 for (i = 0; i < t->nr_elements; i++) {
1076 e = lc_element_by_index(t, i);
1077 if (e->refcnt)
1078 dev_err(DEV, "refcnt(%d)==%d\n",
1079 e->lc_number, e->refcnt);
1080 in_use += e->refcnt;
1081 }
1082 }
1083 if (!in_use)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001084 device->act_log = n;
1085 spin_unlock_irq(&device->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001086 if (in_use) {
1087 dev_err(DEV, "Activity log still in use!\n");
1088 lc_destroy(n);
1089 return -EBUSY;
1090 } else {
1091 if (t)
1092 lc_destroy(t);
1093 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001094 drbd_md_mark_dirty(device); /* we changed device->act_log->nr_elemens */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001095 return 0;
1096}
1097
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001098static void drbd_setup_queue_param(struct drbd_device *device, unsigned int max_bio_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001099{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001100 struct request_queue * const q = device->rq_queue;
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001101 unsigned int max_hw_sectors = max_bio_size >> 9;
1102 unsigned int max_segments = 0;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001103
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001104 if (get_ldev_if_state(device, D_ATTACHING)) {
1105 struct request_queue * const b = device->ldev->backing_bdev->bd_disk->queue;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001106
1107 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001108 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001109 max_segments = rcu_dereference(device->ldev->disk_conf)->max_bio_bvecs;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001110 rcu_read_unlock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001111 put_ldev(device);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001112 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001113
Philipp Reisnerb411b362009-09-25 16:07:19 -07001114 blk_queue_logical_block_size(q, 512);
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001115 blk_queue_max_hw_sectors(q, max_hw_sectors);
1116 /* This is the workaround for "bio would need to, but cannot, be split" */
1117 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
1118 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001119
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001120 if (get_ldev_if_state(device, D_ATTACHING)) {
1121 struct request_queue * const b = device->ldev->backing_bdev->bd_disk->queue;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001122
Philipp Reisner99432fc2011-05-20 16:39:13 +02001123 blk_queue_stack_limits(q, b);
1124
1125 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
1126 dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
1127 q->backing_dev_info.ra_pages,
1128 b->backing_dev_info.ra_pages);
1129 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1130 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001131 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001132 }
1133}
1134
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001135void drbd_reconsider_max_bio_size(struct drbd_device *device)
Philipp Reisner99432fc2011-05-20 16:39:13 +02001136{
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001137 unsigned int now, new, local, peer;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001138
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001139 now = queue_max_hw_sectors(device->rq_queue) << 9;
1140 local = device->local_max_bio_size; /* Eventually last known value, from volatile memory */
1141 peer = device->peer_max_bio_size; /* Eventually last known value, from meta data */
Philipp Reisner99432fc2011-05-20 16:39:13 +02001142
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001143 if (get_ldev_if_state(device, D_ATTACHING)) {
1144 local = queue_max_hw_sectors(device->ldev->backing_bdev->bd_disk->queue) << 9;
1145 device->local_max_bio_size = local;
1146 put_ldev(device);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001147 }
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001148 local = min(local, DRBD_MAX_BIO_SIZE);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001149
1150 /* We may ignore peer limits if the peer is modern enough.
1151 Because new from 8.3.8 onwards the peer can use multiple
1152 BIOs for a single peer_request */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001153 if (device->state.conn >= C_WF_REPORT_PARAMS) {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001154 if (first_peer_device(device)->connection->agreed_pro_version < 94)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001155 peer = min(device->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
Philipp Reisner68093842011-06-30 15:43:06 +02001156 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001157 else if (first_peer_device(device)->connection->agreed_pro_version == 94)
Philipp Reisner99432fc2011-05-20 16:39:13 +02001158 peer = DRBD_MAX_SIZE_H80_PACKET;
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001159 else if (first_peer_device(device)->connection->agreed_pro_version < 100)
Philipp Reisner2ffca4f2011-06-30 15:43:06 +02001160 peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
1161 else
Philipp Reisner99432fc2011-05-20 16:39:13 +02001162 peer = DRBD_MAX_BIO_SIZE;
1163 }
1164
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001165 new = min(local, peer);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001166
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001167 if (device->state.role == R_PRIMARY && new < now)
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001168 dev_err(DEV, "ASSERT FAILED new < now; (%u < %u)\n", new, now);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001169
1170 if (new != now)
1171 dev_info(DEV, "max BIO size = %u\n", new);
1172
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001173 drbd_setup_queue_param(device, new);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001174}
1175
Philipp Reisnera18e9d12011-04-24 11:09:55 +02001176/* Starts the worker thread */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001177static void conn_reconfig_start(struct drbd_connection *connection)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001178{
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001179 drbd_thread_start(&connection->worker);
1180 conn_flush_workqueue(connection);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001181}
1182
Philipp Reisnera18e9d12011-04-24 11:09:55 +02001183/* if still unconfigured, stops worker again. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001184static void conn_reconfig_done(struct drbd_connection *connection)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001185{
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001186 bool stop_threads;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001187 spin_lock_irq(&connection->req_lock);
1188 stop_threads = conn_all_vols_unconf(connection) &&
1189 connection->cstate == C_STANDALONE;
1190 spin_unlock_irq(&connection->req_lock);
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001191 if (stop_threads) {
1192 /* asender is implicitly stopped by receiver
Philipp Reisner81fa2e62011-05-04 15:10:30 +02001193 * in conn_disconnect() */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001194 drbd_thread_stop(&connection->receiver);
1195 drbd_thread_stop(&connection->worker);
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001196 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001197}
1198
Philipp Reisner07782862010-08-31 12:00:50 +02001199/* Make sure IO is suspended before calling this function(). */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001200static void drbd_suspend_al(struct drbd_device *device)
Philipp Reisner07782862010-08-31 12:00:50 +02001201{
1202 int s = 0;
1203
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001204 if (!lc_try_lock(device->act_log)) {
Philipp Reisner07782862010-08-31 12:00:50 +02001205 dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n");
1206 return;
1207 }
1208
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001209 drbd_al_shrink(device);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001210 spin_lock_irq(&first_peer_device(device)->connection->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001211 if (device->state.conn < C_CONNECTED)
1212 s = !test_and_set_bit(AL_SUSPENDED, &device->flags);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001213 spin_unlock_irq(&first_peer_device(device)->connection->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001214 lc_unlock(device->act_log);
Philipp Reisner07782862010-08-31 12:00:50 +02001215
1216 if (s)
1217 dev_info(DEV, "Suspended AL updates\n");
1218}
1219
Lars Ellenberg5979e362011-04-27 21:09:55 +02001220
1221static bool should_set_defaults(struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001222{
Lars Ellenberg5979e362011-04-27 21:09:55 +02001223 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1224 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1225}
1226
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001227static unsigned int drbd_al_extents_max(struct drbd_backing_dev *bdev)
Philipp Reisnerd589a212011-05-04 10:06:52 +02001228{
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001229 /* This is limited by 16 bit "slot" numbers,
1230 * and by available on-disk context storage.
1231 *
1232 * Also (u16)~0 is special (denotes a "free" extent).
1233 *
1234 * One transaction occupies one 4kB on-disk block,
1235 * we have n such blocks in the on disk ring buffer,
1236 * the "current" transaction may fail (n-1),
1237 * and there is 919 slot numbers context information per transaction.
1238 *
1239 * 72 transaction blocks amounts to more than 2**16 context slots,
1240 * so cap there first.
1241 */
1242 const unsigned int max_al_nr = DRBD_AL_EXTENTS_MAX;
1243 const unsigned int sufficient_on_disk =
1244 (max_al_nr + AL_CONTEXT_PER_TRANSACTION -1)
1245 /AL_CONTEXT_PER_TRANSACTION;
Philipp Reisnerd589a212011-05-04 10:06:52 +02001246
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001247 unsigned int al_size_4k = bdev->md.al_size_4k;
1248
1249 if (al_size_4k > sufficient_on_disk)
1250 return max_al_nr;
1251
1252 return (al_size_4k - 1) * AL_CONTEXT_PER_TRANSACTION;
Philipp Reisnerd589a212011-05-04 10:06:52 +02001253}
1254
Lars Ellenbergf3990022011-03-23 14:31:09 +01001255int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1256{
1257 enum drbd_ret_code retcode;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001258 struct drbd_device *device;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001259 struct disk_conf *new_disk_conf, *old_disk_conf;
Philipp Reisner813472c2011-05-03 16:47:02 +02001260 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001261 int err, fifo_size;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001262
1263 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1264 if (!adm_ctx.reply_skb)
1265 return retcode;
1266 if (retcode != NO_ERROR)
1267 goto out;
1268
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001269 device = adm_ctx.device;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001270
1271 /* we also need a disk
1272 * to change the options on */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001273 if (!get_ldev(device)) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001274 retcode = ERR_NO_DISK;
1275 goto out;
1276 }
1277
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001278 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001279 if (!new_disk_conf) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001280 retcode = ERR_NOMEM;
1281 goto fail;
1282 }
1283
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001284 mutex_lock(&first_peer_device(device)->connection->conf_update);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001285 old_disk_conf = device->ldev->disk_conf;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001286 *new_disk_conf = *old_disk_conf;
Lars Ellenberg5979e362011-04-27 21:09:55 +02001287 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02001288 set_disk_conf_defaults(new_disk_conf);
Lars Ellenberg5979e362011-04-27 21:09:55 +02001289
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001290 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02001291 if (err && err != -ENOMSG) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001292 retcode = ERR_MANDATORY_TAG;
1293 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisner8e229432013-08-01 10:21:47 +02001294 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001295 }
1296
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001297 if (!expect(new_disk_conf->resync_rate >= 1))
1298 new_disk_conf->resync_rate = 1;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001299
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001300 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1301 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001302 if (new_disk_conf->al_extents > drbd_al_extents_max(device->ldev))
1303 new_disk_conf->al_extents = drbd_al_extents_max(device->ldev);
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001304
1305 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1306 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001307
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001308 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001309 if (fifo_size != device->rs_plan_s->size) {
Philipp Reisner813472c2011-05-03 16:47:02 +02001310 new_plan = fifo_alloc(fifo_size);
1311 if (!new_plan) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001312 dev_err(DEV, "kmalloc of fifo_buffer failed");
1313 retcode = ERR_NOMEM;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001314 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001315 }
1316 }
1317
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001318 drbd_suspend_io(device);
1319 wait_event(device->al_wait, lc_try_lock(device->act_log));
1320 drbd_al_shrink(device);
1321 err = drbd_check_al_size(device, new_disk_conf);
1322 lc_unlock(device->act_log);
1323 wake_up(&device->al_wait);
1324 drbd_resume_io(device);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001325
1326 if (err) {
1327 retcode = ERR_NOMEM;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001328 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001329 }
1330
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001331 write_lock_irq(&global_state_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001332 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001333 if (retcode == NO_ERROR) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001334 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
1335 drbd_resync_after_changed(device);
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001336 }
1337 write_unlock_irq(&global_state_lock);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001338
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001339 if (retcode != NO_ERROR)
1340 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001341
Philipp Reisner813472c2011-05-03 16:47:02 +02001342 if (new_plan) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001343 old_plan = device->rs_plan_s;
1344 rcu_assign_pointer(device->rs_plan_s, new_plan);
Philipp Reisner9958c852011-05-03 16:19:31 +02001345 }
Philipp Reisner9958c852011-05-03 16:19:31 +02001346
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001347 mutex_unlock(&first_peer_device(device)->connection->conf_update);
Philipp Reisner27eb13e2012-03-30 14:12:15 +02001348
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001349 if (new_disk_conf->al_updates)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001350 device->ldev->md.flags &= ~MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001351 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001352 device->ldev->md.flags |= MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001353
Lars Ellenberg691631c2012-10-26 00:41:50 +02001354 if (new_disk_conf->md_flushes)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001355 clear_bit(MD_NO_FUA, &device->flags);
Lars Ellenberg691631c2012-10-26 00:41:50 +02001356 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001357 set_bit(MD_NO_FUA, &device->flags);
Lars Ellenberg691631c2012-10-26 00:41:50 +02001358
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001359 drbd_bump_write_ordering(first_peer_device(device)->connection, WO_bdev_flush);
Philipp Reisner27eb13e2012-03-30 14:12:15 +02001360
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001361 drbd_md_sync(device);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001362
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001363 if (device->state.conn >= C_CONNECTED)
1364 drbd_send_sync_param(device);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001365
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001366 synchronize_rcu();
1367 kfree(old_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02001368 kfree(old_plan);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001369 mod_timer(&device->request_timer, jiffies + HZ);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001370 goto success;
1371
1372fail_unlock:
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001373 mutex_unlock(&first_peer_device(device)->connection->conf_update);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001374 fail:
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001375 kfree(new_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02001376 kfree(new_plan);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001377success:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001378 put_ldev(device);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001379 out:
1380 drbd_adm_finish(info, retcode);
1381 return 0;
1382}
1383
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001384int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001385{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001386 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001387 int err;
Andreas Gruenbacher116676c2010-12-08 13:33:11 +01001388 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001389 enum determine_dev_size dd;
1390 sector_t max_possible_sectors;
1391 sector_t min_md_device_sectors;
1392 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001393 struct disk_conf *new_disk_conf = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001394 struct block_device *bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001395 struct lru_cache *resync_lru = NULL;
Philipp Reisner9958c852011-05-03 16:19:31 +02001396 struct fifo_buffer *new_plan = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001397 union drbd_state ns, os;
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001398 enum drbd_state_rv rv;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001399 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001400
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001401 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1402 if (!adm_ctx.reply_skb)
1403 return retcode;
1404 if (retcode != NO_ERROR)
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001405 goto finish;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001406
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001407 device = adm_ctx.device;
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001408 conn_reconfig_start(first_peer_device(device)->connection);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001409
1410 /* if you want to reconfigure, please tear down first */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001411 if (device->state.disk > D_DISKLESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001412 retcode = ERR_DISK_CONFIGURED;
1413 goto fail;
1414 }
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001415 /* It may just now have detached because of IO error. Make sure
1416 * drbd_ldev_destroy is done already, we may end up here very fast,
1417 * e.g. if someone calls attach from the on-io-error handler,
1418 * to realize a "hot spare" feature (not that I'd recommend that) */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001419 wait_event(device->misc_wait, !atomic_read(&device->local_cnt));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001420
Lars Ellenberg383606e2012-06-14 14:21:32 +02001421 /* make sure there is no leftover from previous force-detach attempts */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001422 clear_bit(FORCE_DETACH, &device->flags);
1423 clear_bit(WAS_IO_ERROR, &device->flags);
1424 clear_bit(WAS_READ_ERROR, &device->flags);
Lars Ellenberg383606e2012-06-14 14:21:32 +02001425
Lars Ellenberg0029d622012-06-14 18:02:52 +02001426 /* and no leftover from previously aborted resync or verify, either */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001427 device->rs_total = 0;
1428 device->rs_failed = 0;
1429 atomic_set(&device->rs_pending_cnt, 0);
Lars Ellenberg0029d622012-06-14 18:02:52 +02001430
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001431 /* allocation not in the IO path, drbdsetup context */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001432 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1433 if (!nbc) {
1434 retcode = ERR_NOMEM;
1435 goto fail;
1436 }
Philipp Reisner9f2247b2012-08-16 14:25:58 +02001437 spin_lock_init(&nbc->md.uuid_lock);
1438
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001439 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
1440 if (!new_disk_conf) {
1441 retcode = ERR_NOMEM;
1442 goto fail;
1443 }
1444 nbc->disk_conf = new_disk_conf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001445
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001446 set_disk_conf_defaults(new_disk_conf);
1447 err = disk_conf_from_attrs(new_disk_conf, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001448 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001449 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001450 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001451 goto fail;
1452 }
1453
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001454 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1455 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
Philipp Reisnerd589a212011-05-04 10:06:52 +02001456
Philipp Reisner9958c852011-05-03 16:19:31 +02001457 new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
1458 if (!new_plan) {
1459 retcode = ERR_NOMEM;
1460 goto fail;
1461 }
1462
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001463 if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001464 retcode = ERR_MD_IDX_INVALID;
1465 goto fail;
1466 }
1467
Lars Ellenberga3f8f7d2013-03-27 14:08:43 +01001468 write_lock_irq(&global_state_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001469 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
Lars Ellenberga3f8f7d2013-03-27 14:08:43 +01001470 write_unlock_irq(&global_state_lock);
1471 if (retcode != NO_ERROR)
1472 goto fail;
1473
Philipp Reisner44ed1672011-04-19 17:10:19 +02001474 rcu_read_lock();
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001475 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001476 if (nc) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001477 if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02001478 rcu_read_unlock();
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001479 retcode = ERR_STONITH_AND_PROT_A;
1480 goto fail;
1481 }
1482 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02001483 rcu_read_unlock();
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001484
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001485 bdev = blkdev_get_by_path(new_disk_conf->backing_dev,
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001486 FMODE_READ | FMODE_WRITE | FMODE_EXCL, device);
Tejun Heoe525fd82010-11-13 11:55:17 +01001487 if (IS_ERR(bdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001488 dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->backing_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001489 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001490 retcode = ERR_OPEN_DISK;
1491 goto fail;
1492 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001493 nbc->backing_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001494
Tejun Heoe525fd82010-11-13 11:55:17 +01001495 /*
1496 * meta_dev_idx >= 0: external fixed size, possibly multiple
1497 * drbd sharing one meta device. TODO in that case, paranoia
1498 * check that [md_bdev, meta_dev_idx] is not yet used by some
1499 * other drbd minor! (if you use drbd.conf + drbdadm, that
1500 * should check it for you already; but if you don't, or
1501 * someone fooled it, we need to double check here)
1502 */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001503 bdev = blkdev_get_by_path(new_disk_conf->meta_dev,
Tejun Heod4d77622010-11-13 11:55:18 +01001504 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001505 (new_disk_conf->meta_dev_idx < 0) ?
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001506 (void *)device : (void *)drbd_m_holder);
Tejun Heoe525fd82010-11-13 11:55:17 +01001507 if (IS_ERR(bdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001508 dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->meta_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001509 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001510 retcode = ERR_OPEN_MD_DISK;
1511 goto fail;
1512 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001513 nbc->md_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001514
Tejun Heoe525fd82010-11-13 11:55:17 +01001515 if ((nbc->backing_bdev == nbc->md_bdev) !=
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001516 (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1517 new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001518 retcode = ERR_MD_IDX_INVALID;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001519 goto fail;
1520 }
1521
1522 resync_lru = lc_create("resync", drbd_bm_ext_cache,
Lars Ellenberg46a15bc2011-02-21 13:21:01 +01001523 1, 61, sizeof(struct bm_extent),
Philipp Reisnerb411b362009-09-25 16:07:19 -07001524 offsetof(struct bm_extent, lce));
1525 if (!resync_lru) {
1526 retcode = ERR_NOMEM;
Tejun Heoe525fd82010-11-13 11:55:17 +01001527 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001528 }
1529
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +01001530 /* Read our meta data super block early.
1531 * This also sets other on-disk offsets. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001532 retcode = drbd_md_read(device, nbc);
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +01001533 if (retcode != NO_ERROR)
1534 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001535
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001536 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1537 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
1538 if (new_disk_conf->al_extents > drbd_al_extents_max(nbc))
1539 new_disk_conf->al_extents = drbd_al_extents_max(nbc);
1540
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001541 if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001542 dev_err(DEV, "max capacity %llu smaller than disk size %llu\n",
1543 (unsigned long long) drbd_get_max_capacity(nbc),
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001544 (unsigned long long) new_disk_conf->disk_size);
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001545 retcode = ERR_DISK_TOO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001546 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001547 }
1548
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001549 if (new_disk_conf->meta_dev_idx < 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001550 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1551 /* at least one MB, otherwise it does not make sense */
1552 min_md_device_sectors = (2<<10);
1553 } else {
1554 max_possible_sectors = DRBD_MAX_SECTORS;
Lars Ellenbergae8bf312013-03-19 18:16:43 +01001555 min_md_device_sectors = MD_128MB_SECT * (new_disk_conf->meta_dev_idx + 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001556 }
1557
Philipp Reisnerb411b362009-09-25 16:07:19 -07001558 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001559 retcode = ERR_MD_DISK_TOO_SMALL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001560 dev_warn(DEV, "refusing attach: md-device too small, "
1561 "at least %llu sectors needed for this meta-disk type\n",
1562 (unsigned long long) min_md_device_sectors);
Tejun Heoe525fd82010-11-13 11:55:17 +01001563 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001564 }
1565
1566 /* Make sure the new disk is big enough
1567 * (we may currently be R_PRIMARY with no local disk...) */
1568 if (drbd_get_max_capacity(nbc) <
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001569 drbd_get_capacity(device->this_bdev)) {
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001570 retcode = ERR_DISK_TOO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001571 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001572 }
1573
1574 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1575
Lars Ellenberg13529942009-10-12 19:07:49 +02001576 if (nbc->known_size > max_possible_sectors) {
1577 dev_warn(DEV, "==> truncating very big lower level device "
1578 "to currently maximum possible %llu sectors <==\n",
1579 (unsigned long long) max_possible_sectors);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001580 if (new_disk_conf->meta_dev_idx >= 0)
Lars Ellenberg13529942009-10-12 19:07:49 +02001581 dev_warn(DEV, "==>> using internal or flexible "
1582 "meta data may help <<==\n");
1583 }
1584
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001585 drbd_suspend_io(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001586 /* also wait for the last barrier ack. */
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001587 /* FIXME see also https://daiquiri.linbit/cgi-bin/bugzilla/show_bug.cgi?id=171
1588 * We need a way to either ignore barrier acks for barriers sent before a device
1589 * was attached, or a way to wait for all pending barrier acks to come in.
1590 * As barriers are counted per resource,
1591 * we'd need to suspend io on all devices of a resource.
1592 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001593 wait_event(device->misc_wait, !atomic_read(&device->ap_pending_cnt) || drbd_suspended(device));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001594 /* and for any other previously queued work */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001595 drbd_flush_workqueue(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001596
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001597 rv = _drbd_request_state(device, NS(disk, D_ATTACHING), CS_VERBOSE);
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001598 retcode = rv; /* FIXME: Type mismatch. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001599 drbd_resume_io(device);
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001600 if (rv < SS_SUCCESS)
Tejun Heoe525fd82010-11-13 11:55:17 +01001601 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001602
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001603 if (!get_ldev_if_state(device, D_ATTACHING))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001604 goto force_diskless;
1605
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001606 if (!device->bitmap) {
1607 if (drbd_bm_init(device)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001608 retcode = ERR_NOMEM;
1609 goto force_diskless_dec;
1610 }
1611 }
1612
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001613 if (device->state.conn < C_CONNECTED &&
1614 device->state.role == R_PRIMARY &&
1615 (device->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001616 dev_err(DEV, "Can only attach to data with current UUID=%016llX\n",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001617 (unsigned long long)device->ed_uuid);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001618 retcode = ERR_DATA_NOT_CURRENT;
1619 goto force_diskless_dec;
1620 }
1621
1622 /* Since we are diskless, fix the activity log first... */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001623 if (drbd_check_al_size(device, new_disk_conf)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001624 retcode = ERR_NOMEM;
1625 goto force_diskless_dec;
1626 }
1627
1628 /* Prevent shrinking of consistent devices ! */
1629 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001630 drbd_new_dev_size(device, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001631 dev_warn(DEV, "refusing to truncate a consistent device\n");
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001632 retcode = ERR_DISK_TOO_SMALL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001633 goto force_diskless_dec;
1634 }
1635
Philipp Reisnerb411b362009-09-25 16:07:19 -07001636 /* Reset the "barriers don't work" bits here, then force meta data to
1637 * be written, to ensure we determine if barriers are supported. */
Andreas Gruenbachere5440462011-05-04 15:25:35 +02001638 if (new_disk_conf->md_flushes)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001639 clear_bit(MD_NO_FUA, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001640 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001641 set_bit(MD_NO_FUA, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001642
1643 /* Point of no return reached.
1644 * Devices and memory are no longer released by error cleanup below.
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001645 * now device takes over responsibility, and the state engine should
Philipp Reisnerb411b362009-09-25 16:07:19 -07001646 * clean it up somewhere. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001647 D_ASSERT(device->ldev == NULL);
1648 device->ldev = nbc;
1649 device->resync = resync_lru;
1650 device->rs_plan_s = new_plan;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001651 nbc = NULL;
1652 resync_lru = NULL;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001653 new_disk_conf = NULL;
Philipp Reisner9958c852011-05-03 16:19:31 +02001654 new_plan = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001655
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001656 drbd_bump_write_ordering(first_peer_device(device)->connection, WO_bdev_flush);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001657
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001658 if (drbd_md_test_flag(device->ldev, MDF_CRASHED_PRIMARY))
1659 set_bit(CRASHED_PRIMARY, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001660 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001661 clear_bit(CRASHED_PRIMARY, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001662
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001663 if (drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001664 !(device->state.role == R_PRIMARY &&
1665 first_peer_device(device)->connection->susp_nod))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001666 set_bit(CRASHED_PRIMARY, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001667
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001668 device->send_cnt = 0;
1669 device->recv_cnt = 0;
1670 device->read_cnt = 0;
1671 device->writ_cnt = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001672
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001673 drbd_reconsider_max_bio_size(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001674
1675 /* If I am currently not R_PRIMARY,
1676 * but meta data primary indicator is set,
1677 * I just now recover from a hard crash,
1678 * and have been R_PRIMARY before that crash.
1679 *
1680 * Now, if I had no connection before that crash
1681 * (have been degraded R_PRIMARY), chances are that
1682 * I won't find my peer now either.
1683 *
1684 * In that case, and _only_ in that case,
1685 * we use the degr-wfc-timeout instead of the default,
1686 * so we can automatically recover from a crash of a
1687 * degraded but active "cluster" after a certain timeout.
1688 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001689 clear_bit(USE_DEGR_WFC_T, &device->flags);
1690 if (device->state.role != R_PRIMARY &&
1691 drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
1692 !drbd_md_test_flag(device->ldev, MDF_CONNECTED_IND))
1693 set_bit(USE_DEGR_WFC_T, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001694
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001695 dd = drbd_determine_dev_size(device, 0, NULL);
Philipp Reisnerd752b262013-06-25 16:50:08 +02001696 if (dd <= DS_ERROR) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001697 retcode = ERR_NOMEM_BITMAP;
1698 goto force_diskless_dec;
Philipp Reisnere96c9632013-06-25 16:50:07 +02001699 } else if (dd == DS_GREW)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001700 set_bit(RESYNC_AFTER_NEG, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001701
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001702 if (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC) ||
1703 (test_bit(CRASHED_PRIMARY, &device->flags) &&
1704 drbd_md_test_flag(device->ldev, MDF_AL_DISABLED))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001705 dev_info(DEV, "Assuming that all blocks are out of sync "
1706 "(aka FullSync)\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001707 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001708 "set_n_write from attaching", BM_LOCKED_MASK)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001709 retcode = ERR_IO_MD_DISK;
1710 goto force_diskless_dec;
1711 }
1712 } else {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001713 if (drbd_bitmap_io(device, &drbd_bm_read,
Andreas Gruenbacher22ab6a32010-12-13 01:44:11 +01001714 "read from attaching", BM_LOCKED_MASK)) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001715 retcode = ERR_IO_MD_DISK;
1716 goto force_diskless_dec;
1717 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001718 }
1719
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001720 if (_drbd_bm_total_weight(device) == drbd_bm_bits(device))
1721 drbd_suspend_al(device); /* IO is still suspended here... */
Philipp Reisner07782862010-08-31 12:00:50 +02001722
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001723 spin_lock_irq(&first_peer_device(device)->connection->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001724 os = drbd_read_state(device);
Philipp Reisner78bae592011-03-28 15:40:12 +02001725 ns = os;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001726 /* If MDF_CONSISTENT is not set go into inconsistent state,
1727 otherwise investigate MDF_WasUpToDate...
1728 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1729 otherwise into D_CONSISTENT state.
1730 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001731 if (drbd_md_test_flag(device->ldev, MDF_CONSISTENT)) {
1732 if (drbd_md_test_flag(device->ldev, MDF_WAS_UP_TO_DATE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001733 ns.disk = D_CONSISTENT;
1734 else
1735 ns.disk = D_OUTDATED;
1736 } else {
1737 ns.disk = D_INCONSISTENT;
1738 }
1739
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001740 if (drbd_md_test_flag(device->ldev, MDF_PEER_OUT_DATED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001741 ns.pdsk = D_OUTDATED;
1742
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001743 rcu_read_lock();
1744 if (ns.disk == D_CONSISTENT &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001745 (ns.pdsk == D_OUTDATED || rcu_dereference(device->ldev->disk_conf)->fencing == FP_DONT_CARE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001746 ns.disk = D_UP_TO_DATE;
1747
1748 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1749 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1750 this point, because drbd_request_state() modifies these
1751 flags. */
1752
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001753 if (rcu_dereference(device->ldev->disk_conf)->al_updates)
1754 device->ldev->md.flags &= ~MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001755 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001756 device->ldev->md.flags |= MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001757
1758 rcu_read_unlock();
1759
Philipp Reisnerb411b362009-09-25 16:07:19 -07001760 /* In case we are C_CONNECTED postpone any decision on the new disk
1761 state after the negotiation phase. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001762 if (device->state.conn == C_CONNECTED) {
1763 device->new_state_tmp.i = ns.i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001764 ns.i = os.i;
1765 ns.disk = D_NEGOTIATING;
Philipp Reisnerdc66c742010-06-02 14:31:29 +02001766
1767 /* We expect to receive up-to-date UUIDs soon.
1768 To avoid a race in receive_state, free p_uuid while
1769 holding req_lock. I.e. atomic with the state change */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001770 kfree(device->p_uuid);
1771 device->p_uuid = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001772 }
1773
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001774 rv = _drbd_set_state(device, ns, CS_VERBOSE, NULL);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001775 spin_unlock_irq(&first_peer_device(device)->connection->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001776
1777 if (rv < SS_SUCCESS)
1778 goto force_diskless_dec;
1779
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001780 mod_timer(&device->request_timer, jiffies + HZ);
Philipp Reisnercdfda632011-07-05 15:38:59 +02001781
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001782 if (device->state.role == R_PRIMARY)
1783 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001784 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001785 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001786
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001787 drbd_md_mark_dirty(device);
1788 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001789
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001790 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
1791 put_ldev(device);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001792 conn_reconfig_done(first_peer_device(device)->connection);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001793 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001794 return 0;
1795
1796 force_diskless_dec:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001797 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001798 force_diskless:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001799 drbd_force_state(device, NS(disk, D_DISKLESS));
1800 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001801 fail:
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001802 conn_reconfig_done(first_peer_device(device)->connection);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001803 if (nbc) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001804 if (nbc->backing_bdev)
1805 blkdev_put(nbc->backing_bdev,
1806 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1807 if (nbc->md_bdev)
1808 blkdev_put(nbc->md_bdev,
1809 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001810 kfree(nbc);
1811 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001812 kfree(new_disk_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001813 lc_destroy(resync_lru);
Philipp Reisner9958c852011-05-03 16:19:31 +02001814 kfree(new_plan);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001815
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001816 finish:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001817 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001818 return 0;
1819}
1820
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001821static int adm_detach(struct drbd_device *device, int force)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001822{
Philipp Reisner19f83c72011-03-29 14:21:03 +02001823 enum drbd_state_rv retcode;
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001824 int ret;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001825
Philipp Reisnercdfda632011-07-05 15:38:59 +02001826 if (force) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001827 set_bit(FORCE_DETACH, &device->flags);
1828 drbd_force_state(device, NS(disk, D_FAILED));
Philipp Reisnercdfda632011-07-05 15:38:59 +02001829 retcode = SS_SUCCESS;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001830 goto out;
1831 }
1832
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001833 drbd_suspend_io(device); /* so no-one is stuck in drbd_al_begin_io */
1834 drbd_md_get_buffer(device); /* make sure there is no in-flight meta-data IO */
1835 retcode = drbd_request_state(device, NS(disk, D_FAILED));
1836 drbd_md_put_buffer(device);
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001837 /* D_FAILED will transition to DISKLESS. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001838 ret = wait_event_interruptible(device->misc_wait,
1839 device->state.disk != D_FAILED);
1840 drbd_resume_io(device);
Philipp Reisner9b2f61a2011-05-24 10:27:38 +02001841 if ((int)retcode == (int)SS_IS_DISKLESS)
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001842 retcode = SS_NOTHING_TO_DO;
1843 if (ret)
1844 retcode = ERR_INTR;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001845out:
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001846 return retcode;
1847}
1848
Philipp Reisnerb411b362009-09-25 16:07:19 -07001849/* Detaching the disk is a process in multiple stages. First we need to lock
1850 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1851 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1852 * internal references as well.
1853 * Only then we have finally detached. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001854int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001855{
1856 enum drbd_ret_code retcode;
Philipp Reisnercdfda632011-07-05 15:38:59 +02001857 struct detach_parms parms = { };
1858 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001859
1860 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1861 if (!adm_ctx.reply_skb)
1862 return retcode;
1863 if (retcode != NO_ERROR)
1864 goto out;
1865
Philipp Reisnercdfda632011-07-05 15:38:59 +02001866 if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
1867 err = detach_parms_from_attrs(&parms, info);
1868 if (err) {
1869 retcode = ERR_MANDATORY_TAG;
1870 drbd_msg_put_info(from_attrs_err_to_txt(err));
1871 goto out;
1872 }
1873 }
1874
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001875 retcode = adm_detach(adm_ctx.device, parms.force_detach);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001876out:
1877 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001878 return 0;
1879}
1880
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001881static bool conn_resync_running(struct drbd_connection *connection)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001882{
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001883 struct drbd_peer_device *peer_device;
Philipp Reisner695d08f2011-04-11 22:53:32 -07001884 bool rv = false;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001885 int vnr;
1886
Philipp Reisner695d08f2011-04-11 22:53:32 -07001887 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001888 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1889 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001890 if (device->state.conn == C_SYNC_SOURCE ||
1891 device->state.conn == C_SYNC_TARGET ||
1892 device->state.conn == C_PAUSED_SYNC_S ||
1893 device->state.conn == C_PAUSED_SYNC_T) {
Philipp Reisner695d08f2011-04-11 22:53:32 -07001894 rv = true;
1895 break;
1896 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001897 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001898 rcu_read_unlock();
1899
1900 return rv;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001901}
1902
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001903static bool conn_ov_running(struct drbd_connection *connection)
Lars Ellenbergf3990022011-03-23 14:31:09 +01001904{
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001905 struct drbd_peer_device *peer_device;
Philipp Reisner695d08f2011-04-11 22:53:32 -07001906 bool rv = false;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001907 int vnr;
1908
Philipp Reisner695d08f2011-04-11 22:53:32 -07001909 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001910 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1911 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001912 if (device->state.conn == C_VERIFY_S ||
1913 device->state.conn == C_VERIFY_T) {
Philipp Reisner695d08f2011-04-11 22:53:32 -07001914 rv = true;
1915 break;
1916 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001917 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001918 rcu_read_unlock();
1919
1920 return rv;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001921}
1922
Philipp Reisnercd643972011-04-13 18:00:59 -07001923static enum drbd_ret_code
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001924_check_net_options(struct drbd_connection *connection, struct net_conf *old_conf, struct net_conf *new_conf)
Philipp Reisnercd643972011-04-13 18:00:59 -07001925{
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001926 struct drbd_peer_device *peer_device;
Philipp Reisnercd643972011-04-13 18:00:59 -07001927 int i;
1928
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001929 if (old_conf && connection->cstate == C_WF_REPORT_PARAMS && connection->agreed_pro_version < 100) {
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02001930 if (new_conf->wire_protocol != old_conf->wire_protocol)
1931 return ERR_NEED_APV_100;
1932
1933 if (new_conf->two_primaries != old_conf->two_primaries)
1934 return ERR_NEED_APV_100;
1935
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02001936 if (strcmp(new_conf->integrity_alg, old_conf->integrity_alg))
1937 return ERR_NEED_APV_100;
1938 }
1939
1940 if (!new_conf->two_primaries &&
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001941 conn_highest_role(connection) == R_PRIMARY &&
1942 conn_highest_peer(connection) == R_PRIMARY)
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02001943 return ERR_NEED_ALLOW_TWO_PRI;
Philipp Reisnerb032b6f2011-04-13 18:16:10 -07001944
Philipp Reisnercd643972011-04-13 18:00:59 -07001945 if (new_conf->two_primaries &&
1946 (new_conf->wire_protocol != DRBD_PROT_C))
1947 return ERR_NOT_PROTO_C;
1948
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001949 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
1950 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001951 if (get_ldev(device)) {
1952 enum drbd_fencing_p fp = rcu_dereference(device->ldev->disk_conf)->fencing;
1953 put_ldev(device);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001954 if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
Philipp Reisnercd643972011-04-13 18:00:59 -07001955 return ERR_STONITH_AND_PROT_A;
Philipp Reisnercd643972011-04-13 18:00:59 -07001956 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001957 if (device->state.role == R_PRIMARY && new_conf->discard_my_data)
Lars Ellenbergeb120102012-08-01 12:46:20 +02001958 return ERR_DISCARD_IMPOSSIBLE;
Philipp Reisnercd643972011-04-13 18:00:59 -07001959 }
Philipp Reisnercd643972011-04-13 18:00:59 -07001960
1961 if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A)
1962 return ERR_CONG_NOT_PROTO_A;
1963
1964 return NO_ERROR;
1965}
1966
Philipp Reisner44ed1672011-04-19 17:10:19 +02001967static enum drbd_ret_code
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001968check_net_options(struct drbd_connection *connection, struct net_conf *new_conf)
Philipp Reisner44ed1672011-04-19 17:10:19 +02001969{
1970 static enum drbd_ret_code rv;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001971 struct drbd_peer_device *peer_device;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001972 int i;
1973
1974 rcu_read_lock();
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001975 rv = _check_net_options(connection, rcu_dereference(connection->net_conf), new_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001976 rcu_read_unlock();
1977
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001978 /* connection->volumes protected by genl_lock() here */
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001979 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
1980 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001981 if (!device->bitmap) {
1982 if (drbd_bm_init(device))
Philipp Reisner44ed1672011-04-19 17:10:19 +02001983 return ERR_NOMEM;
1984 }
1985 }
1986
1987 return rv;
1988}
1989
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001990struct crypto {
1991 struct crypto_hash *verify_tfm;
1992 struct crypto_hash *csums_tfm;
1993 struct crypto_hash *cram_hmac_tfm;
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02001994 struct crypto_hash *integrity_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001995};
1996
1997static int
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02001998alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02001999{
2000 if (!tfm_name[0])
2001 return NO_ERROR;
2002
2003 *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
2004 if (IS_ERR(*tfm)) {
2005 *tfm = NULL;
2006 return err_alg;
2007 }
2008
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002009 return NO_ERROR;
2010}
2011
2012static enum drbd_ret_code
2013alloc_crypto(struct crypto *crypto, struct net_conf *new_conf)
2014{
Philipp Reisnerb411b362009-09-25 16:07:19 -07002015 char hmac_name[CRYPTO_MAX_ALG_NAME];
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002016 enum drbd_ret_code rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002017
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002018 rv = alloc_hash(&crypto->csums_tfm, new_conf->csums_alg,
2019 ERR_CSUMS_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002020 if (rv != NO_ERROR)
2021 return rv;
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002022 rv = alloc_hash(&crypto->verify_tfm, new_conf->verify_alg,
2023 ERR_VERIFY_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002024 if (rv != NO_ERROR)
2025 return rv;
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002026 rv = alloc_hash(&crypto->integrity_tfm, new_conf->integrity_alg,
2027 ERR_INTEGRITY_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002028 if (rv != NO_ERROR)
2029 return rv;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002030 if (new_conf->cram_hmac_alg[0] != 0) {
2031 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
2032 new_conf->cram_hmac_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002033
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002034 rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
2035 ERR_AUTH_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002036 }
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002037
2038 return rv;
2039}
2040
2041static void free_crypto(struct crypto *crypto)
2042{
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002043 crypto_free_hash(crypto->cram_hmac_tfm);
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02002044 crypto_free_hash(crypto->integrity_tfm);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002045 crypto_free_hash(crypto->csums_tfm);
2046 crypto_free_hash(crypto->verify_tfm);
2047}
2048
Lars Ellenbergf3990022011-03-23 14:31:09 +01002049int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
2050{
2051 enum drbd_ret_code retcode;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002052 struct drbd_connection *connection;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002053 struct net_conf *old_conf, *new_conf = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002054 int err;
2055 int ovr; /* online verify running */
2056 int rsr; /* re-sync running */
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002057 struct crypto crypto = { };
Lars Ellenbergf3990022011-03-23 14:31:09 +01002058
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002059 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002060 if (!adm_ctx.reply_skb)
2061 return retcode;
2062 if (retcode != NO_ERROR)
2063 goto out;
2064
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002065 connection = adm_ctx.connection;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002066
2067 new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
2068 if (!new_conf) {
2069 retcode = ERR_NOMEM;
2070 goto out;
2071 }
2072
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002073 conn_reconfig_start(connection);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002074
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002075 mutex_lock(&connection->data.mutex);
2076 mutex_lock(&connection->conf_update);
2077 old_conf = connection->net_conf;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002078
2079 if (!old_conf) {
2080 drbd_msg_put_info("net conf missing, try connect");
2081 retcode = ERR_INVALID_REQUEST;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002082 goto fail;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002083 }
2084
2085 *new_conf = *old_conf;
Lars Ellenberg5979e362011-04-27 21:09:55 +02002086 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02002087 set_net_conf_defaults(new_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +02002088
Lars Ellenbergf3990022011-03-23 14:31:09 +01002089 err = net_conf_from_attrs_for_change(new_conf, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02002090 if (err && err != -ENOMSG) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002091 retcode = ERR_MANDATORY_TAG;
2092 drbd_msg_put_info(from_attrs_err_to_txt(err));
2093 goto fail;
2094 }
2095
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002096 retcode = check_net_options(connection, new_conf);
Philipp Reisnercd643972011-04-13 18:00:59 -07002097 if (retcode != NO_ERROR)
2098 goto fail;
2099
Lars Ellenbergf3990022011-03-23 14:31:09 +01002100 /* re-sync running */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002101 rsr = conn_resync_running(connection);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002102 if (rsr && strcmp(new_conf->csums_alg, old_conf->csums_alg)) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002103 retcode = ERR_CSUMS_RESYNC_RUNNING;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002104 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002105 }
2106
Lars Ellenbergf3990022011-03-23 14:31:09 +01002107 /* online verify running */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002108 ovr = conn_ov_running(connection);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002109 if (ovr && strcmp(new_conf->verify_alg, old_conf->verify_alg)) {
2110 retcode = ERR_VERIFY_RUNNING;
2111 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002112 }
2113
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002114 retcode = alloc_crypto(&crypto, new_conf);
2115 if (retcode != NO_ERROR)
2116 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002117
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002118 rcu_assign_pointer(connection->net_conf, new_conf);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002119
2120 if (!rsr) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002121 crypto_free_hash(connection->csums_tfm);
2122 connection->csums_tfm = crypto.csums_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002123 crypto.csums_tfm = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002124 }
2125 if (!ovr) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002126 crypto_free_hash(connection->verify_tfm);
2127 connection->verify_tfm = crypto.verify_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002128 crypto.verify_tfm = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002129 }
2130
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002131 crypto_free_hash(connection->integrity_tfm);
2132 connection->integrity_tfm = crypto.integrity_tfm;
2133 if (connection->cstate >= C_WF_REPORT_PARAMS && connection->agreed_pro_version >= 100)
2134 /* Do this without trying to take connection->data.mutex again. */
2135 __drbd_send_protocol(connection, P_PROTOCOL_UPDATE);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002136
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002137 crypto_free_hash(connection->cram_hmac_tfm);
2138 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002139
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002140 mutex_unlock(&connection->conf_update);
2141 mutex_unlock(&connection->data.mutex);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002142 synchronize_rcu();
2143 kfree(old_conf);
2144
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002145 if (connection->cstate >= C_WF_REPORT_PARAMS)
2146 drbd_send_sync_param(minor_to_device(conn_lowest_minor(connection)));
Lars Ellenbergf3990022011-03-23 14:31:09 +01002147
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002148 goto done;
2149
Lars Ellenbergf3990022011-03-23 14:31:09 +01002150 fail:
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002151 mutex_unlock(&connection->conf_update);
2152 mutex_unlock(&connection->data.mutex);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002153 free_crypto(&crypto);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002154 kfree(new_conf);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002155 done:
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002156 conn_reconfig_done(connection);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002157 out:
2158 drbd_adm_finish(info, retcode);
2159 return 0;
2160}
2161
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002162int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002163{
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002164 struct drbd_peer_device *peer_device;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002165 struct net_conf *old_conf, *new_conf = NULL;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002166 struct crypto crypto = { };
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002167 struct drbd_resource *resource;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002168 struct drbd_connection *connection;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002169 enum drbd_ret_code retcode;
2170 int i;
2171 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002172
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02002173 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002174
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002175 if (!adm_ctx.reply_skb)
2176 return retcode;
2177 if (retcode != NO_ERROR)
2178 goto out;
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002179 if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
2180 drbd_msg_put_info("connection endpoint(s) missing");
2181 retcode = ERR_INVALID_REQUEST;
2182 goto out;
2183 }
2184
2185 /* No need for _rcu here. All reconfiguration is
2186 * strictly serialized on genl_lock(). We are protected against
2187 * concurrent reconfiguration/addition/deletion */
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002188 for_each_resource(resource, &drbd_resources) {
2189 for_each_connection(connection, resource) {
2190 if (nla_len(adm_ctx.my_addr) == connection->my_addr_len &&
2191 !memcmp(nla_data(adm_ctx.my_addr), &connection->my_addr,
2192 connection->my_addr_len)) {
2193 retcode = ERR_LOCAL_ADDR;
2194 goto out;
2195 }
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002196
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002197 if (nla_len(adm_ctx.peer_addr) == connection->peer_addr_len &&
2198 !memcmp(nla_data(adm_ctx.peer_addr), &connection->peer_addr,
2199 connection->peer_addr_len)) {
2200 retcode = ERR_PEER_ADDR;
2201 goto out;
2202 }
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002203 }
2204 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002205
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002206 connection = adm_ctx.connection;
2207 conn_reconfig_start(connection);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002208
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002209 if (connection->cstate > C_STANDALONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002210 retcode = ERR_NET_CONFIGURED;
2211 goto fail;
2212 }
2213
Andreas Gruenbachera209b4a2011-08-17 12:43:25 +02002214 /* allocation not in the IO path, drbdsetup / netlink process context */
Lars Ellenberg5979e362011-04-27 21:09:55 +02002215 new_conf = kzalloc(sizeof(*new_conf), GFP_KERNEL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002216 if (!new_conf) {
2217 retcode = ERR_NOMEM;
2218 goto fail;
2219 }
2220
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02002221 set_net_conf_defaults(new_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002222
Lars Ellenbergf3990022011-03-23 14:31:09 +01002223 err = net_conf_from_attrs(new_conf, info);
Lars Ellenberg25e40932011-08-19 10:39:00 +02002224 if (err && err != -ENOMSG) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002225 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002226 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002227 goto fail;
2228 }
2229
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002230 retcode = check_net_options(connection, new_conf);
Philipp Reisnercd643972011-04-13 18:00:59 -07002231 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002232 goto fail;
Philipp Reisner47ff2d02010-06-18 13:56:57 +02002233
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002234 retcode = alloc_crypto(&crypto, new_conf);
2235 if (retcode != NO_ERROR)
Philipp Reisner422028b2010-10-27 11:12:07 +02002236 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002237
2238 ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
2239
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002240 conn_flush_workqueue(connection);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002241
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002242 mutex_lock(&connection->conf_update);
2243 old_conf = connection->net_conf;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002244 if (old_conf) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002245 retcode = ERR_NET_CONFIGURED;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002246 mutex_unlock(&connection->conf_update);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002247 goto fail;
2248 }
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002249 rcu_assign_pointer(connection->net_conf, new_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002250
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002251 conn_free_crypto(connection);
2252 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
2253 connection->integrity_tfm = crypto.integrity_tfm;
2254 connection->csums_tfm = crypto.csums_tfm;
2255 connection->verify_tfm = crypto.verify_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002256
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002257 connection->my_addr_len = nla_len(adm_ctx.my_addr);
2258 memcpy(&connection->my_addr, nla_data(adm_ctx.my_addr), connection->my_addr_len);
2259 connection->peer_addr_len = nla_len(adm_ctx.peer_addr);
2260 memcpy(&connection->peer_addr, nla_data(adm_ctx.peer_addr), connection->peer_addr_len);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002261
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002262 mutex_unlock(&connection->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002263
Philipp Reisner695d08f2011-04-11 22:53:32 -07002264 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002265 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2266 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002267 device->send_cnt = 0;
2268 device->recv_cnt = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002269 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07002270 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002271
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002272 retcode = conn_request_state(connection, NS(conn, C_UNCONNECTED), CS_VERBOSE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002273
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002274 conn_reconfig_done(connection);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002275 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002276 return 0;
2277
2278fail:
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002279 free_crypto(&crypto);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002280 kfree(new_conf);
2281
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002282 conn_reconfig_done(connection);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002283out:
2284 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002285 return 0;
2286}
2287
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002288static enum drbd_state_rv conn_try_disconnect(struct drbd_connection *connection, bool force)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002289{
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002290 enum drbd_state_rv rv;
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002291
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002292 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002293 force ? CS_HARD : 0);
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002294
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002295 switch (rv) {
2296 case SS_NOTHING_TO_DO:
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002297 break;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002298 case SS_ALREADY_STANDALONE:
2299 return SS_SUCCESS;
2300 case SS_PRIMARY_NOP:
2301 /* Our state checking code wants to see the peer outdated. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002302 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING, pdsk, D_OUTDATED), 0);
Philipp Reisner2bd5ed52013-03-27 14:08:40 +01002303
2304 if (rv == SS_OUTDATE_WO_CONN) /* lost connection before graceful disconnect succeeded */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002305 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_VERBOSE);
Philipp Reisner2bd5ed52013-03-27 14:08:40 +01002306
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002307 break;
2308 case SS_CW_FAILED_BY_PEER:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002309 /* The peer probably wants to see us outdated. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002310 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING,
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002311 disk, D_OUTDATED), 0);
2312 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002313 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002314 CS_HARD);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002315 }
2316 break;
2317 default:;
2318 /* no special handling necessary */
2319 }
2320
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002321 if (rv >= SS_SUCCESS) {
2322 enum drbd_state_rv rv2;
2323 /* No one else can reconfigure the network while I am here.
2324 * The state handling only uses drbd_thread_stop_nowait(),
2325 * we want to really wait here until the receiver is no more.
2326 */
Andreas Gruenbacher9693da22011-06-21 13:58:18 +02002327 drbd_thread_stop(&connection->receiver);
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002328
2329 /* Race breaker. This additional state change request may be
2330 * necessary, if this was a forced disconnect during a receiver
2331 * restart. We may have "killed" the receiver thread just
2332 * after drbdd_init() returned. Typically, we should be
2333 * C_STANDALONE already, now, and this becomes a no-op.
2334 */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002335 rv2 = conn_request_state(connection, NS(conn, C_STANDALONE),
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002336 CS_VERBOSE | CS_HARD);
2337 if (rv2 < SS_SUCCESS)
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002338 conn_err(connection,
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002339 "unexpected rv2=%d in conn_try_disconnect()\n",
2340 rv2);
2341 }
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002342 return rv;
2343}
2344
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002345int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002346{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002347 struct disconnect_parms parms;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002348 struct drbd_connection *connection;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002349 enum drbd_state_rv rv;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002350 enum drbd_ret_code retcode;
2351 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002352
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002353 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002354 if (!adm_ctx.reply_skb)
2355 return retcode;
2356 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002357 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002358
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002359 connection = adm_ctx.connection;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002360 memset(&parms, 0, sizeof(parms));
2361 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002362 err = disconnect_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002363 if (err) {
2364 retcode = ERR_MANDATORY_TAG;
2365 drbd_msg_put_info(from_attrs_err_to_txt(err));
2366 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002367 }
2368 }
2369
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002370 rv = conn_try_disconnect(connection, parms.force_disconnect);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002371 if (rv < SS_SUCCESS)
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002372 retcode = rv; /* FIXME: Type mismatch. */
2373 else
2374 retcode = NO_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002375 fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002376 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002377 return 0;
2378}
2379
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002380void resync_after_online_grow(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002381{
2382 int iass; /* I am sync source */
2383
2384 dev_info(DEV, "Resync of new storage after online grow\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002385 if (device->state.role != device->state.peer)
2386 iass = (device->state.role == R_PRIMARY);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002387 else
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002388 iass = test_bit(RESOLVE_CONFLICTS, &first_peer_device(device)->connection->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002389
2390 if (iass)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002391 drbd_start_resync(device, C_SYNC_SOURCE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002392 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002393 _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002394}
2395
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002396int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002397{
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002398 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002399 struct resize_parms rs;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002400 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002401 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002402 enum determine_dev_size dd;
Philipp Reisnerd752b262013-06-25 16:50:08 +02002403 bool change_al_layout = false;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002404 enum dds_flags ddsf;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002405 sector_t u_size;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002406 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002407
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002408 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2409 if (!adm_ctx.reply_skb)
2410 return retcode;
2411 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002412 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002413
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002414 device = adm_ctx.device;
2415 if (!get_ldev(device)) {
Philipp Reisnerd752b262013-06-25 16:50:08 +02002416 retcode = ERR_NO_DISK;
2417 goto fail;
2418 }
2419
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002420 memset(&rs, 0, sizeof(struct resize_parms));
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002421 rs.al_stripes = device->ldev->md.al_stripes;
2422 rs.al_stripe_size = device->ldev->md.al_stripe_size_4k * 4;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002423 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002424 err = resize_parms_from_attrs(&rs, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002425 if (err) {
2426 retcode = ERR_MANDATORY_TAG;
2427 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerd752b262013-06-25 16:50:08 +02002428 goto fail_ldev;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002429 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002430 }
2431
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002432 if (device->state.conn > C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002433 retcode = ERR_RESIZE_RESYNC;
Philipp Reisnerd752b262013-06-25 16:50:08 +02002434 goto fail_ldev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002435 }
2436
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002437 if (device->state.role == R_SECONDARY &&
2438 device->state.peer == R_SECONDARY) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002439 retcode = ERR_NO_PRIMARY;
Philipp Reisnerd752b262013-06-25 16:50:08 +02002440 goto fail_ldev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002441 }
2442
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002443 if (rs.no_resync && first_peer_device(device)->connection->agreed_pro_version < 93) {
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002444 retcode = ERR_NEED_APV_93;
Andreas Gruenbacher7b4e4d32011-09-28 22:15:04 +02002445 goto fail_ldev;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002446 }
2447
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002448 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002449 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002450 rcu_read_unlock();
2451 if (u_size != (sector_t)rs.resize_size) {
2452 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
2453 if (!new_disk_conf) {
2454 retcode = ERR_NOMEM;
Philipp Reisner9bcd2522011-09-29 13:00:14 +02002455 goto fail_ldev;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002456 }
2457 }
2458
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002459 if (device->ldev->md.al_stripes != rs.al_stripes ||
2460 device->ldev->md.al_stripe_size_4k != rs.al_stripe_size / 4) {
Philipp Reisnerd752b262013-06-25 16:50:08 +02002461 u32 al_size_k = rs.al_stripes * rs.al_stripe_size;
2462
2463 if (al_size_k > (16 * 1024 * 1024)) {
2464 retcode = ERR_MD_LAYOUT_TOO_BIG;
2465 goto fail_ldev;
2466 }
2467
2468 if (al_size_k < MD_32kB_SECT/2) {
2469 retcode = ERR_MD_LAYOUT_TOO_SMALL;
2470 goto fail_ldev;
2471 }
2472
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002473 if (device->state.conn != C_CONNECTED) {
Philipp Reisnerd752b262013-06-25 16:50:08 +02002474 retcode = ERR_MD_LAYOUT_CONNECTED;
2475 goto fail_ldev;
2476 }
2477
2478 change_al_layout = true;
2479 }
2480
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002481 if (device->ldev->known_size != drbd_get_capacity(device->ldev->backing_bdev))
2482 device->ldev->known_size = drbd_get_capacity(device->ldev->backing_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002483
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002484 if (new_disk_conf) {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002485 mutex_lock(&first_peer_device(device)->connection->conf_update);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002486 old_disk_conf = device->ldev->disk_conf;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002487 *new_disk_conf = *old_disk_conf;
2488 new_disk_conf->disk_size = (sector_t)rs.resize_size;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002489 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002490 mutex_unlock(&first_peer_device(device)->connection->conf_update);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002491 synchronize_rcu();
2492 kfree(old_disk_conf);
2493 }
2494
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002495 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002496 dd = drbd_determine_dev_size(device, ddsf, change_al_layout ? &rs : NULL);
2497 drbd_md_sync(device);
2498 put_ldev(device);
Philipp Reisnere96c9632013-06-25 16:50:07 +02002499 if (dd == DS_ERROR) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002500 retcode = ERR_NOMEM_BITMAP;
2501 goto fail;
Philipp Reisnerd752b262013-06-25 16:50:08 +02002502 } else if (dd == DS_ERROR_SPACE_MD) {
2503 retcode = ERR_MD_LAYOUT_NO_FIT;
2504 goto fail;
2505 } else if (dd == DS_ERROR_SHRINK) {
2506 retcode = ERR_IMPLICIT_SHRINK;
2507 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002508 }
2509
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002510 if (device->state.conn == C_CONNECTED) {
Philipp Reisnere96c9632013-06-25 16:50:07 +02002511 if (dd == DS_GREW)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002512 set_bit(RESIZE_PENDING, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002513
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002514 drbd_send_uuids(device);
2515 drbd_send_sizes(device, 1, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002516 }
2517
2518 fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002519 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002520 return 0;
Andreas Gruenbacher7b4e4d32011-09-28 22:15:04 +02002521
2522 fail_ldev:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002523 put_ldev(device);
Andreas Gruenbacher7b4e4d32011-09-28 22:15:04 +02002524 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002525}
2526
Lars Ellenbergf3990022011-03-23 14:31:09 +01002527int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002528{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002529 enum drbd_ret_code retcode;
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002530 struct res_opts res_opts;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002531 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002532
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02002533 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002534 if (!adm_ctx.reply_skb)
2535 return retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002536 if (retcode != NO_ERROR)
2537 goto fail;
2538
Andreas Gruenbachereb6bea62011-06-21 16:11:28 +02002539 res_opts = adm_ctx.resource->res_opts;
Lars Ellenberg5979e362011-04-27 21:09:55 +02002540 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02002541 set_res_opts_defaults(&res_opts);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002542
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002543 err = res_opts_from_attrs(&res_opts, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02002544 if (err && err != -ENOMSG) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002545 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002546 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002547 goto fail;
2548 }
2549
Andreas Gruenbachereb6bea62011-06-21 16:11:28 +02002550 err = set_resource_options(adm_ctx.resource, &res_opts);
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02002551 if (err) {
2552 retcode = ERR_INVALID_REQUEST;
2553 if (err == -ENOMEM)
Philipp Reisner778f2712010-07-06 11:14:00 +02002554 retcode = ERR_NOMEM;
Philipp Reisner778f2712010-07-06 11:14:00 +02002555 }
2556
Philipp Reisnerb411b362009-09-25 16:07:19 -07002557fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002558 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002559 return 0;
2560}
2561
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002562int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002563{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002564 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002565 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2566
2567 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2568 if (!adm_ctx.reply_skb)
2569 return retcode;
2570 if (retcode != NO_ERROR)
2571 goto out;
2572
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002573 device = adm_ctx.device;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002574
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002575 /* If there is still bitmap IO pending, probably because of a previous
Lars Ellenberg7ee1fb92012-06-19 10:27:58 +02002576 * resync just being finished, wait for it before requesting a new resync.
2577 * Also wait for it's after_state_ch(). */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002578 drbd_suspend_io(device);
2579 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
2580 drbd_flush_workqueue(device);
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002581
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002582 /* If we happen to be C_STANDALONE R_SECONDARY, just change to
2583 * D_INCONSISTENT, and set all bits in the bitmap. Otherwise,
2584 * try to start a resync handshake as sync target for full sync.
Philipp Reisner9376d9f2013-03-27 14:08:36 +01002585 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002586 if (device->state.conn == C_STANDALONE && device->state.role == R_SECONDARY) {
2587 retcode = drbd_request_state(device, NS(disk, D_INCONSISTENT));
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002588 if (retcode >= SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002589 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002590 "set_n_write from invalidate", BM_LOCKED_MASK))
2591 retcode = ERR_IO_MD_DISK;
2592 }
2593 } else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002594 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_T));
2595 drbd_resume_io(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002596
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002597out:
2598 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002599 return 0;
2600}
2601
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002602static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2603 union drbd_state mask, union drbd_state val)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002604{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002605 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002606
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002607 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2608 if (!adm_ctx.reply_skb)
2609 return retcode;
2610 if (retcode != NO_ERROR)
2611 goto out;
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002612
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002613 retcode = drbd_request_state(adm_ctx.device, mask, val);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002614out:
2615 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002616 return 0;
2617}
2618
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002619static int drbd_bmio_set_susp_al(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002620{
2621 int rv;
2622
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002623 rv = drbd_bmio_set_n_write(device);
2624 drbd_suspend_al(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002625 return rv;
2626}
2627
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002628int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002629{
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002630 int retcode; /* drbd_ret_code, drbd_state_rv */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002631 struct drbd_device *device;
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002632
2633 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2634 if (!adm_ctx.reply_skb)
2635 return retcode;
2636 if (retcode != NO_ERROR)
2637 goto out;
2638
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002639 device = adm_ctx.device;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002640
2641 /* If there is still bitmap IO pending, probably because of a previous
Lars Ellenberg7ee1fb92012-06-19 10:27:58 +02002642 * resync just being finished, wait for it before requesting a new resync.
2643 * Also wait for it's after_state_ch(). */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002644 drbd_suspend_io(device);
2645 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
2646 drbd_flush_workqueue(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002647
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002648 /* If we happen to be C_STANDALONE R_PRIMARY, just set all bits
2649 * in the bitmap. Otherwise, try to start a resync handshake
2650 * as sync source for full sync.
2651 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002652 if (device->state.conn == C_STANDALONE && device->state.role == R_PRIMARY) {
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002653 /* The peer will get a resync upon connect anyways. Just make that
2654 into a full resync. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002655 retcode = drbd_request_state(device, NS(pdsk, D_INCONSISTENT));
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002656 if (retcode >= SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002657 if (drbd_bitmap_io(device, &drbd_bmio_set_susp_al,
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002658 "set_n_write from invalidate_peer",
2659 BM_LOCKED_SET_ALLOWED))
2660 retcode = ERR_IO_MD_DISK;
2661 }
2662 } else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002663 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_S));
2664 drbd_resume_io(device);
Philipp Reisner07782862010-08-31 12:00:50 +02002665
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002666out:
2667 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002668 return 0;
2669}
2670
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002671int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002672{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002673 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002674
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002675 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2676 if (!adm_ctx.reply_skb)
2677 return retcode;
2678 if (retcode != NO_ERROR)
2679 goto out;
2680
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002681 if (drbd_request_state(adm_ctx.device, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002682 retcode = ERR_PAUSE_IS_SET;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002683out:
2684 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002685 return 0;
2686}
2687
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002688int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002689{
Philipp Reisnerda9fbc22011-03-29 10:52:01 +02002690 union drbd_dev_state s;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002691 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002692
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002693 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2694 if (!adm_ctx.reply_skb)
2695 return retcode;
2696 if (retcode != NO_ERROR)
2697 goto out;
2698
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002699 if (drbd_request_state(adm_ctx.device, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2700 s = adm_ctx.device->state;
Philipp Reisnercd88d032011-01-20 11:46:41 +01002701 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2702 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2703 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2704 } else {
2705 retcode = ERR_PAUSE_IS_CLEAR;
2706 }
2707 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002708
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002709out:
2710 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002711 return 0;
2712}
2713
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002714int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002715{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002716 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002717}
2718
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002719int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002720{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002721 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002722 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2723
2724 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2725 if (!adm_ctx.reply_skb)
2726 return retcode;
2727 if (retcode != NO_ERROR)
2728 goto out;
2729
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002730 device = adm_ctx.device;
2731 if (test_bit(NEW_CUR_UUID, &device->flags)) {
2732 drbd_uuid_new_current(device);
2733 clear_bit(NEW_CUR_UUID, &device->flags);
Philipp Reisner43a51822010-06-11 11:26:34 +02002734 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002735 drbd_suspend_io(device);
2736 retcode = drbd_request_state(device, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002737 if (retcode == SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002738 if (device->state.conn < C_CONNECTED)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002739 tl_clear(first_peer_device(device)->connection);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002740 if (device->state.disk == D_DISKLESS || device->state.disk == D_FAILED)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002741 tl_restart(first_peer_device(device)->connection, FAIL_FROZEN_DISK_IO);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002742 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002743 drbd_resume_io(device);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002744
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002745out:
2746 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002747 return 0;
2748}
2749
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002750int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002751{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002752 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002753}
2754
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002755static int nla_put_drbd_cfg_context(struct sk_buff *skb, struct drbd_connection *connection, unsigned vnr)
Lars Ellenberg543cc102011-03-10 22:18:18 +01002756{
2757 struct nlattr *nla;
2758 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
2759 if (!nla)
2760 goto nla_put_failure;
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002761 if (vnr != VOLUME_UNSPECIFIED &&
2762 nla_put_u32(skb, T_ctx_volume, vnr))
2763 goto nla_put_failure;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002764 if (nla_put_string(skb, T_ctx_resource_name, connection->resource->name))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002765 goto nla_put_failure;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002766 if (connection->my_addr_len &&
2767 nla_put(skb, T_ctx_my_addr, connection->my_addr_len, &connection->my_addr))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002768 goto nla_put_failure;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002769 if (connection->peer_addr_len &&
2770 nla_put(skb, T_ctx_peer_addr, connection->peer_addr_len, &connection->peer_addr))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002771 goto nla_put_failure;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002772 nla_nest_end(skb, nla);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002773 return 0;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002774
2775nla_put_failure:
2776 if (nla)
2777 nla_nest_cancel(skb, nla);
2778 return -EMSGSIZE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002779}
2780
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002781static int nla_put_status_info(struct sk_buff *skb, struct drbd_device *device,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002782 const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002783{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002784 struct state_info *si = NULL; /* for sizeof(si->member); */
2785 struct nlattr *nla;
2786 int got_ldev;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002787 int err = 0;
2788 int exclude_sensitive;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002789
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002790 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2791 * to. So we better exclude_sensitive information.
2792 *
2793 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2794 * in the context of the requesting user process. Exclude sensitive
2795 * information, unless current has superuser.
2796 *
2797 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2798 * relies on the current implementation of netlink_dump(), which
2799 * executes the dump callback successively from netlink_recvmsg(),
2800 * always in the context of the receiving process */
2801 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002802
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002803 got_ldev = get_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002804
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002805 /* We need to add connection name and volume number information still.
2806 * Minor number is in drbd_genlmsghdr. */
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002807 if (nla_put_drbd_cfg_context(skb, first_peer_device(device)->connection, device->vnr))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002808 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002809
Andreas Gruenbachereb6bea62011-06-21 16:11:28 +02002810 if (res_opts_to_skb(skb, &device->resource->res_opts, exclude_sensitive))
Lars Ellenbergf3990022011-03-23 14:31:09 +01002811 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002812
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002813 rcu_read_lock();
Andreas Gruenbacherf9eb7bf2013-06-25 16:50:05 +02002814 if (got_ldev) {
2815 struct disk_conf *disk_conf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002816
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002817 disk_conf = rcu_dereference(device->ldev->disk_conf);
Andreas Gruenbacherf9eb7bf2013-06-25 16:50:05 +02002818 err = disk_conf_to_skb(skb, disk_conf, exclude_sensitive);
2819 }
2820 if (!err) {
2821 struct net_conf *nc;
2822
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002823 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
Andreas Gruenbacherf9eb7bf2013-06-25 16:50:05 +02002824 if (nc)
2825 err = net_conf_to_skb(skb, nc, exclude_sensitive);
2826 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002827 rcu_read_unlock();
2828 if (err)
2829 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002830
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002831 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
2832 if (!nla)
2833 goto nla_put_failure;
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002834 if (nla_put_u32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY) ||
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002835 nla_put_u32(skb, T_current_state, device->state.i) ||
2836 nla_put_u64(skb, T_ed_uuid, device->ed_uuid) ||
2837 nla_put_u64(skb, T_capacity, drbd_get_capacity(device->this_bdev)) ||
2838 nla_put_u64(skb, T_send_cnt, device->send_cnt) ||
2839 nla_put_u64(skb, T_recv_cnt, device->recv_cnt) ||
2840 nla_put_u64(skb, T_read_cnt, device->read_cnt) ||
2841 nla_put_u64(skb, T_writ_cnt, device->writ_cnt) ||
2842 nla_put_u64(skb, T_al_writ_cnt, device->al_writ_cnt) ||
2843 nla_put_u64(skb, T_bm_writ_cnt, device->bm_writ_cnt) ||
2844 nla_put_u32(skb, T_ap_bio_cnt, atomic_read(&device->ap_bio_cnt)) ||
2845 nla_put_u32(skb, T_ap_pending_cnt, atomic_read(&device->ap_pending_cnt)) ||
2846 nla_put_u32(skb, T_rs_pending_cnt, atomic_read(&device->rs_pending_cnt)))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002847 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002848
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002849 if (got_ldev) {
Philipp Reisner39a1aa72012-08-08 21:19:09 +02002850 int err;
2851
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002852 spin_lock_irq(&device->ldev->md.uuid_lock);
2853 err = nla_put(skb, T_uuids, sizeof(si->uuids), device->ldev->md.uuid);
2854 spin_unlock_irq(&device->ldev->md.uuid_lock);
Philipp Reisner39a1aa72012-08-08 21:19:09 +02002855
2856 if (err)
2857 goto nla_put_failure;
2858
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002859 if (nla_put_u32(skb, T_disk_flags, device->ldev->md.flags) ||
2860 nla_put_u64(skb, T_bits_total, drbd_bm_bits(device)) ||
2861 nla_put_u64(skb, T_bits_oos, drbd_bm_total_weight(device)))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002862 goto nla_put_failure;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002863 if (C_SYNC_SOURCE <= device->state.conn &&
2864 C_PAUSED_SYNC_T >= device->state.conn) {
2865 if (nla_put_u64(skb, T_bits_rs_total, device->rs_total) ||
2866 nla_put_u64(skb, T_bits_rs_failed, device->rs_failed))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002867 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002868 }
2869 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002870
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002871 if (sib) {
2872 switch(sib->sib_reason) {
2873 case SIB_SYNC_PROGRESS:
2874 case SIB_GET_STATUS_REPLY:
2875 break;
2876 case SIB_STATE_CHANGE:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002877 if (nla_put_u32(skb, T_prev_state, sib->os.i) ||
2878 nla_put_u32(skb, T_new_state, sib->ns.i))
2879 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002880 break;
2881 case SIB_HELPER_POST:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002882 if (nla_put_u32(skb, T_helper_exit_code,
2883 sib->helper_exit_code))
2884 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002885 /* fall through */
2886 case SIB_HELPER_PRE:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002887 if (nla_put_string(skb, T_helper, sib->helper_name))
2888 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002889 break;
2890 }
2891 }
2892 nla_nest_end(skb, nla);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002893
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002894 if (0)
2895nla_put_failure:
2896 err = -EMSGSIZE;
2897 if (got_ldev)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002898 put_ldev(device);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002899 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002900}
2901
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002902int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002903{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002904 enum drbd_ret_code retcode;
2905 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002906
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002907 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2908 if (!adm_ctx.reply_skb)
2909 return retcode;
2910 if (retcode != NO_ERROR)
2911 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002912
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002913 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.device, NULL);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002914 if (err) {
2915 nlmsg_free(adm_ctx.reply_skb);
2916 return err;
2917 }
2918out:
2919 drbd_adm_finish(info, retcode);
2920 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002921}
2922
Rashika Kheria4b7a5302013-12-19 15:17:33 +05302923static int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002924{
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002925 struct drbd_peer_device *peer_device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002926 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002927 struct drbd_genlmsghdr *dh;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002928 struct drbd_resource *pos = (struct drbd_resource *)cb->args[0];
2929 struct drbd_resource *resource = NULL;
2930 struct drbd_connection *connection;
2931 struct drbd_resource *tmp;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002932 unsigned volume = cb->args[1];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002933
Lars Ellenberg543cc102011-03-10 22:18:18 +01002934 /* Open coded, deferred, iteration:
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002935 * for_each_resource_safe(resource, tmp, &drbd_resources) {
2936 * connection = "first connection of resource";
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002937 * idr_for_each_entry(&connection->peer_devices, peer_device, i) {
Lars Ellenberg543cc102011-03-10 22:18:18 +01002938 * ...
2939 * }
2940 * }
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002941 * where resource is cb->args[0];
Lars Ellenberg543cc102011-03-10 22:18:18 +01002942 * and i is cb->args[1];
2943 *
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002944 * cb->args[2] indicates if we shall loop over all resources,
2945 * or just dump all volumes of a single resource.
2946 *
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002947 * This may miss entries inserted after this dump started,
2948 * or entries deleted before they are reached.
Lars Ellenberg543cc102011-03-10 22:18:18 +01002949 *
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002950 * We need to make sure the device won't disappear while
Lars Ellenberg543cc102011-03-10 22:18:18 +01002951 * we are looking at it, and revalidate our iterators
2952 * on each iteration.
2953 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002954
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02002955 /* synchronize with conn_create()/drbd_destroy_connection() */
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02002956 rcu_read_lock();
Lars Ellenberg543cc102011-03-10 22:18:18 +01002957 /* revalidate iterator position */
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002958 for_each_resource_rcu(tmp, &drbd_resources) {
Lars Ellenberg543cc102011-03-10 22:18:18 +01002959 if (pos == NULL) {
2960 /* first iteration */
2961 pos = tmp;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002962 resource = pos;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002963 break;
2964 }
2965 if (tmp == pos) {
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002966 resource = pos;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002967 break;
2968 }
2969 }
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002970 if (resource) {
2971next_resource:
2972 connection = first_connection(resource);
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002973 peer_device = idr_get_next(&connection->peer_devices, &volume);
2974 if (!peer_device) {
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002975 /* No more volumes to dump on this resource.
2976 * Advance resource iterator. */
2977 pos = list_entry_rcu(resource->resources.next,
2978 struct drbd_resource, resources);
2979 /* Did we dump any volume of this resource yet? */
Lars Ellenberg543cc102011-03-10 22:18:18 +01002980 if (volume != 0) {
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002981 /* If we reached the end of the list,
2982 * or only a single resource dump was requested,
2983 * we are done. */
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002984 if (&pos->resources == &drbd_resources || cb->args[2])
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002985 goto out;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002986 volume = 0;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002987 resource = pos;
2988 goto next_resource;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002989 }
2990 }
2991
Philipp Reisner98683652012-11-09 14:18:43 +01002992 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002993 cb->nlh->nlmsg_seq, &drbd_genl_family,
2994 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
2995 if (!dh)
Lars Ellenberg543cc102011-03-10 22:18:18 +01002996 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002997
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002998 if (!peer_device) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002999 /* This is a connection without a single volume.
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003000 * Suprisingly enough, it may have a network
3001 * configuration. */
3002 struct net_conf *nc;
Lars Ellenberg543cc102011-03-10 22:18:18 +01003003 dh->minor = -1U;
3004 dh->ret_code = NO_ERROR;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003005 if (nla_put_drbd_cfg_context(skb, connection, VOLUME_UNSPECIFIED))
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003006 goto cancel;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003007 nc = rcu_dereference(connection->net_conf);
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003008 if (nc && net_conf_to_skb(skb, nc, 1) != 0)
3009 goto cancel;
3010 goto done;
Lars Ellenberg543cc102011-03-10 22:18:18 +01003011 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003012
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02003013 device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003014 D_ASSERT(device->vnr == volume);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02003015 D_ASSERT(first_peer_device(device)->connection == connection);
Lars Ellenberg543cc102011-03-10 22:18:18 +01003016
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003017 dh->minor = device_to_minor(device);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003018 dh->ret_code = NO_ERROR;
3019
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003020 if (nla_put_status_info(skb, device, NULL)) {
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003021cancel:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003022 genlmsg_cancel(skb, dh);
Lars Ellenberg543cc102011-03-10 22:18:18 +01003023 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003024 }
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003025done:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003026 genlmsg_end(skb, dh);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003027 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003028
Lars Ellenberg543cc102011-03-10 22:18:18 +01003029out:
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003030 rcu_read_unlock();
Lars Ellenberg543cc102011-03-10 22:18:18 +01003031 /* where to start the next iteration */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003032 cb->args[0] = (long)pos;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003033 cb->args[1] = (pos == resource) ? volume + 1 : 0;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003034
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003035 /* No more resources/volumes/minors found results in an empty skb.
Lars Ellenberg543cc102011-03-10 22:18:18 +01003036 * Which will terminate the dump. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003037 return skb->len;
3038}
3039
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003040/*
3041 * Request status of all resources, or of all volumes within a single resource.
3042 *
3043 * This is a dump, as the answer may not fit in a single reply skb otherwise.
3044 * Which means we cannot use the family->attrbuf or other such members, because
3045 * dump is NOT protected by the genl_lock(). During dump, we only have access
3046 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
3047 *
3048 * Once things are setup properly, we call into get_one_status().
Philipp Reisnerb411b362009-09-25 16:07:19 -07003049 */
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003050int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003051{
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003052 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
3053 struct nlattr *nla;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003054 const char *resource_name;
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003055 struct drbd_resource *resource;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003056 int maxtype;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003057
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003058 /* Is this a followup call? */
3059 if (cb->args[0]) {
3060 /* ... of a single resource dump,
3061 * and the resource iterator has been advanced already? */
3062 if (cb->args[2] && cb->args[2] != cb->args[0])
3063 return 0; /* DONE. */
3064 goto dump;
3065 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003066
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003067 /* First call (from netlink_dump_start). We need to figure out
3068 * which resource(s) the user wants us to dump. */
3069 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
3070 nlmsg_attrlen(cb->nlh, hdrlen),
3071 DRBD_NLA_CFG_CONTEXT);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003072
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003073 /* No explicit context given. Dump all. */
3074 if (!nla)
3075 goto dump;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003076 maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
3077 nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
3078 if (IS_ERR(nla))
3079 return PTR_ERR(nla);
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003080 /* context given, but no name present? */
3081 if (!nla)
3082 return -EINVAL;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003083 resource_name = nla_data(nla);
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003084 if (!*resource_name)
3085 return -ENODEV;
3086 resource = drbd_find_resource(resource_name);
3087 if (!resource)
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003088 return -ENODEV;
3089
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003090 kref_put(&resource->kref, drbd_destroy_resource); /* get_one_status() revalidates the resource */
Philipp Reisner0ace9df2011-04-24 10:53:19 +02003091
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003092 /* prime iterators, and set "filter" mode mark:
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003093 * only dump this connection. */
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003094 cb->args[0] = (long)resource;
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003095 /* cb->args[1] = 0; passed in this way. */
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003096 cb->args[2] = (long)resource;
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003097
3098dump:
3099 return get_one_status(skb, cb);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003100}
3101
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003102int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003103{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003104 enum drbd_ret_code retcode;
3105 struct timeout_parms tp;
3106 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003107
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003108 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3109 if (!adm_ctx.reply_skb)
3110 return retcode;
3111 if (retcode != NO_ERROR)
3112 goto out;
3113
3114 tp.timeout_type =
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003115 adm_ctx.device->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
3116 test_bit(USE_DEGR_WFC_T, &adm_ctx.device->flags) ? UT_DEGRADED :
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003117 UT_DEFAULT;
3118
3119 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
3120 if (err) {
3121 nlmsg_free(adm_ctx.reply_skb);
3122 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003123 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003124out:
3125 drbd_adm_finish(info, retcode);
3126 return 0;
3127}
Lars Ellenberg873b0d52011-01-21 22:53:48 +01003128
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003129int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
3130{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003131 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003132 enum drbd_ret_code retcode;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003133 struct start_ov_parms parms;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003134
3135 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3136 if (!adm_ctx.reply_skb)
3137 return retcode;
3138 if (retcode != NO_ERROR)
3139 goto out;
3140
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003141 device = adm_ctx.device;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003142
3143 /* resume from last known position, if possible */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003144 parms.ov_start_sector = device->ov_start_sector;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003145 parms.ov_stop_sector = ULLONG_MAX;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003146 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01003147 int err = start_ov_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003148 if (err) {
3149 retcode = ERR_MANDATORY_TAG;
3150 drbd_msg_put_info(from_attrs_err_to_txt(err));
3151 goto out;
3152 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003153 }
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003154 /* w_make_ov_request expects position to be aligned */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003155 device->ov_start_sector = parms.ov_start_sector & ~(BM_SECT_PER_BIT-1);
3156 device->ov_stop_sector = parms.ov_stop_sector;
Lars Ellenberg873b0d52011-01-21 22:53:48 +01003157
3158 /* If there is still bitmap IO pending, e.g. previous resync or verify
3159 * just being finished, wait for it before requesting a new resync. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003160 drbd_suspend_io(device);
3161 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
3162 retcode = drbd_request_state(device, NS(conn, C_VERIFY_S));
3163 drbd_resume_io(device);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003164out:
3165 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003166 return 0;
3167}
3168
3169
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003170int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003171{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003172 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003173 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003174 int skip_initial_sync = 0;
3175 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003176 struct new_c_uuid_parms args;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003177
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003178 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3179 if (!adm_ctx.reply_skb)
3180 return retcode;
3181 if (retcode != NO_ERROR)
3182 goto out_nolock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003183
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003184 device = adm_ctx.device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003185 memset(&args, 0, sizeof(args));
3186 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01003187 err = new_c_uuid_parms_from_attrs(&args, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003188 if (err) {
3189 retcode = ERR_MANDATORY_TAG;
3190 drbd_msg_put_info(from_attrs_err_to_txt(err));
3191 goto out_nolock;
3192 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003193 }
3194
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003195 mutex_lock(device->state_mutex); /* Protects us against serialized state changes. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003196
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003197 if (!get_ldev(device)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003198 retcode = ERR_NO_DISK;
3199 goto out;
3200 }
3201
3202 /* this is "skip initial sync", assume to be clean */
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02003203 if (device->state.conn == C_CONNECTED &&
3204 first_peer_device(device)->connection->agreed_pro_version >= 90 &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003205 device->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003206 dev_info(DEV, "Preparing to skip initial sync\n");
3207 skip_initial_sync = 1;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003208 } else if (device->state.conn != C_STANDALONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003209 retcode = ERR_CONNECTED;
3210 goto out_dec;
3211 }
3212
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003213 drbd_uuid_set(device, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
3214 drbd_uuid_new_current(device); /* New current, previous to UI_BITMAP */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003215
3216 if (args.clear_bm) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003217 err = drbd_bitmap_io(device, &drbd_bmio_clear_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003218 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003219 if (err) {
3220 dev_err(DEV, "Writing bitmap failed with %d\n",err);
3221 retcode = ERR_IO_MD_DISK;
3222 }
3223 if (skip_initial_sync) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003224 drbd_send_uuids_skip_initial_sync(device);
3225 _drbd_uuid_set(device, UI_BITMAP, 0);
3226 drbd_print_uuids(device, "cleared bitmap UUID");
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02003227 spin_lock_irq(&first_peer_device(device)->connection->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003228 _drbd_set_state(_NS2(device, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
Philipp Reisnerb411b362009-09-25 16:07:19 -07003229 CS_VERBOSE, NULL);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02003230 spin_unlock_irq(&first_peer_device(device)->connection->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003231 }
3232 }
3233
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003234 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003235out_dec:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003236 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003237out:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003238 mutex_unlock(device->state_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003239out_nolock:
3240 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003241 return 0;
3242}
3243
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003244static enum drbd_ret_code
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003245drbd_check_resource_name(const char *name)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003246{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003247 if (!name || !name[0]) {
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003248 drbd_msg_put_info("resource name missing");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003249 return ERR_MANDATORY_TAG;
3250 }
3251 /* if we want to use these in sysfs/configfs/debugfs some day,
3252 * we must not allow slashes */
3253 if (strchr(name, '/')) {
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003254 drbd_msg_put_info("invalid resource name");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003255 return ERR_INVALID_REQUEST;
3256 }
3257 return NO_ERROR;
3258}
Philipp Reisnerb411b362009-09-25 16:07:19 -07003259
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003260int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003261{
3262 enum drbd_ret_code retcode;
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003263 struct res_opts res_opts;
3264 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003265
3266 retcode = drbd_adm_prepare(skb, info, 0);
3267 if (!adm_ctx.reply_skb)
3268 return retcode;
3269 if (retcode != NO_ERROR)
3270 goto out;
3271
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003272 set_res_opts_defaults(&res_opts);
3273 err = res_opts_from_attrs(&res_opts, info);
3274 if (err && err != -ENOMSG) {
3275 retcode = ERR_MANDATORY_TAG;
3276 drbd_msg_put_info(from_attrs_err_to_txt(err));
3277 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003278 }
3279
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003280 retcode = drbd_check_resource_name(adm_ctx.resource_name);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003281 if (retcode != NO_ERROR)
3282 goto out;
3283
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003284 if (adm_ctx.connection) {
Lars Ellenberg38f19612011-03-14 13:22:35 +01003285 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
3286 retcode = ERR_INVALID_REQUEST;
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003287 drbd_msg_put_info("resource exists");
Lars Ellenberg38f19612011-03-14 13:22:35 +01003288 }
3289 /* else: still NO_ERROR */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003290 goto out;
Philipp Reisner9f5180e2009-10-06 09:30:14 +02003291 }
3292
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003293 if (!conn_create(adm_ctx.resource_name, &res_opts))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003294 retcode = ERR_NOMEM;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003295out:
3296 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003297 return 0;
3298}
3299
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003300int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003301{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003302 struct drbd_genlmsghdr *dh = info->userhdr;
3303 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003304
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02003305 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003306 if (!adm_ctx.reply_skb)
3307 return retcode;
3308 if (retcode != NO_ERROR)
3309 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003310
Andreas Gruenbacherf2257a52011-07-14 16:00:40 +02003311 if (dh->minor > MINORMASK) {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003312 drbd_msg_put_info("requested minor out of range");
3313 retcode = ERR_INVALID_REQUEST;
3314 goto out;
3315 }
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +02003316 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003317 drbd_msg_put_info("requested volume id out of range");
3318 retcode = ERR_INVALID_REQUEST;
3319 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05003320 }
3321
Lars Ellenberg38f19612011-03-14 13:22:35 +01003322 /* drbd_adm_prepare made sure already
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02003323 * that first_peer_device(device)->connection and device->vnr match the request. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003324 if (adm_ctx.device) {
Lars Ellenberg38f19612011-03-14 13:22:35 +01003325 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
3326 retcode = ERR_MINOR_EXISTS;
3327 /* else: still NO_ERROR */
3328 goto out;
3329 }
3330
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02003331 retcode = drbd_create_minor(adm_ctx.connection, dh->minor, adm_ctx.volume);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003332out:
3333 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003334 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003335}
3336
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003337static enum drbd_ret_code adm_del_minor(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003338{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003339 if (device->state.disk == D_DISKLESS &&
3340 /* no need to be device->state.conn == C_STANDALONE &&
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003341 * we may want to delete a minor from a live replication group.
3342 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003343 device->state.role == R_SECONDARY) {
3344 _drbd_request_state(device, NS(conn, C_WF_REPORT_PARAMS),
Philipp Reisner369bea62011-07-06 23:04:44 +02003345 CS_VERBOSE + CS_WAIT_COMPLETE);
Andreas Gruenbacher803ea132011-06-09 01:40:48 +02003346 drbd_delete_minor(device);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003347 return NO_ERROR;
3348 } else
3349 return ERR_MINOR_CONFIGURED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003350}
3351
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003352int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05003353{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003354 enum drbd_ret_code retcode;
3355
3356 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3357 if (!adm_ctx.reply_skb)
3358 return retcode;
3359 if (retcode != NO_ERROR)
3360 goto out;
3361
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003362 retcode = adm_del_minor(adm_ctx.device);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003363out:
3364 drbd_adm_finish(info, retcode);
3365 return 0;
3366}
3367
3368int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
3369{
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003370 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02003371 struct drbd_peer_device *peer_device;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003372 unsigned i;
3373
Andreas Gruenbachera10f6b82011-06-09 01:26:16 +02003374 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003375 if (!adm_ctx.reply_skb)
3376 return retcode;
3377 if (retcode != NO_ERROR)
3378 goto out;
3379
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003380 /* demote */
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02003381 idr_for_each_entry(&adm_ctx.connection->peer_devices, peer_device, i) {
3382 retcode = drbd_set_role(peer_device->device, R_SECONDARY, 0);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003383 if (retcode < SS_SUCCESS) {
3384 drbd_msg_put_info("failed to demote");
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003385 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003386 }
3387 }
3388
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003389 retcode = conn_try_disconnect(adm_ctx.connection, 0);
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003390 if (retcode < SS_SUCCESS) {
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003391 drbd_msg_put_info("failed to disconnect");
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003392 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003393 }
3394
3395 /* detach */
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02003396 idr_for_each_entry(&adm_ctx.connection->peer_devices, peer_device, i) {
3397 retcode = adm_detach(peer_device->device, 0);
Lars Ellenberg27012382012-07-24 10:13:55 +02003398 if (retcode < SS_SUCCESS || retcode > NO_ERROR) {
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003399 drbd_msg_put_info("failed to detach");
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003400 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003401 }
3402 }
3403
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003404 /* If we reach this, all volumes (of this connection) are Secondary,
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003405 * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003406 * actually stopped, state handling only does drbd_thread_stop_nowait(). */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003407 drbd_thread_stop(&adm_ctx.connection->worker);
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003408
3409 /* Now, nothing can fail anymore */
3410
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003411 /* delete volumes */
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02003412 idr_for_each_entry(&adm_ctx.connection->peer_devices, peer_device, i) {
3413 retcode = adm_del_minor(peer_device->device);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003414 if (retcode != NO_ERROR) {
3415 /* "can not happen" */
3416 drbd_msg_put_info("failed to delete volume");
Philipp Reisneref356262011-04-13 14:21:29 -07003417 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003418 }
3419 }
3420
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003421 /* delete connection */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003422 if (conn_lowest_minor(adm_ctx.connection) < 0) {
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003423 struct drbd_resource *resource = adm_ctx.connection->resource;
3424
3425 list_del_rcu(&resource->resources);
Philipp Reisnerec0bddb2011-05-04 15:47:01 +02003426 synchronize_rcu();
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003427 drbd_free_resource(resource);
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02003428
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003429 retcode = NO_ERROR;
3430 } else {
3431 /* "can not happen" */
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003432 retcode = ERR_RES_IN_USE;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003433 drbd_msg_put_info("failed to delete connection");
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003434 }
Philipp Reisneref356262011-04-13 14:21:29 -07003435 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003436out:
3437 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003438 return 0;
3439}
3440
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003441int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05003442{
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003443 struct drbd_resource *resource;
3444 struct drbd_connection *connection;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003445 enum drbd_ret_code retcode;
3446
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +02003447 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003448 if (!adm_ctx.reply_skb)
3449 return retcode;
3450 if (retcode != NO_ERROR)
3451 goto out;
3452
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003453 resource = adm_ctx.resource;
3454 for_each_connection(connection, resource) {
3455 if (connection->cstate > C_STANDALONE) {
3456 retcode = ERR_NET_CONFIGURED;
3457 goto out;
3458 }
3459 }
3460 if (!idr_is_empty(&resource->devices)) {
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003461 retcode = ERR_RES_IN_USE;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003462 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05003463 }
3464
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003465 list_del_rcu(&resource->resources);
3466 for_each_connection(connection, resource)
3467 drbd_thread_stop(&connection->worker);
3468 synchronize_rcu();
3469 drbd_free_resource(resource);
3470 retcode = NO_ERROR;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003471out:
3472 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003473 return 0;
3474}
3475
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003476void drbd_bcast_event(struct drbd_device *device, const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003477{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003478 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
3479 struct sk_buff *msg;
3480 struct drbd_genlmsghdr *d_out;
3481 unsigned seq;
3482 int err = -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003483
Philipp Reisneref86b772012-12-06 10:34:34 +01003484 if (sib->sib_reason == SIB_SYNC_PROGRESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003485 if (time_after(jiffies, device->rs_last_bcast + HZ))
3486 device->rs_last_bcast = jiffies;
Philipp Reisneref86b772012-12-06 10:34:34 +01003487 else
3488 return;
3489 }
Philipp Reisner328e0f12012-10-19 14:37:47 +02003490
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003491 seq = atomic_inc_return(&drbd_genl_seq);
3492 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
3493 if (!msg)
3494 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003495
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003496 err = -EMSGSIZE;
3497 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
3498 if (!d_out) /* cannot happen, but anyways. */
3499 goto nla_put_failure;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003500 d_out->minor = device_to_minor(device);
Andreas Gruenbacher6f9b5f82011-05-06 01:03:32 +02003501 d_out->ret_code = NO_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003502
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003503 if (nla_put_status_info(msg, device, sib))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003504 goto nla_put_failure;
3505 genlmsg_end(msg, d_out);
3506 err = drbd_genl_multicast_events(msg, 0);
3507 /* msg has been consumed or freed in netlink_broadcast() */
3508 if (err && err != -ESRCH)
3509 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003510
Philipp Reisnerb411b362009-09-25 16:07:19 -07003511 return;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003512
3513nla_put_failure:
3514 nlmsg_free(msg);
3515failed:
3516 dev_err(DEV, "Error %d while broadcasting event. "
3517 "Event seq:%u sib_reason:%u\n",
3518 err, seq, sib->sib_reason);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003519}