blob: 1cd47df44bdaf57d74c1ff3adede40835f1b1f29 [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
Lars Ellenbergf88c5d92014-03-27 14:10:55 +010026#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
Philipp Reisnerb411b362009-09-25 16:07:19 -070028#include <linux/module.h>
29#include <linux/drbd.h>
30#include <linux/in.h>
31#include <linux/fs.h>
32#include <linux/file.h>
33#include <linux/slab.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070034#include <linux/blkpg.h>
35#include <linux/cpumask.h>
36#include "drbd_int.h"
Andreas Gruenbachera3603a62011-05-30 11:47:37 +020037#include "drbd_protocol.h"
Philipp Reisner265be2d2010-05-31 10:14:17 +020038#include "drbd_req.h"
Philipp Reisnerb411b362009-09-25 16:07:19 -070039#include <asm/unaligned.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070040#include <linux/drbd_limits.h>
Philipp Reisner87f7be42010-06-11 13:56:33 +020041#include <linux/kthread.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070042
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010043#include <net/genetlink.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070044
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010045/* .doit */
46// int drbd_adm_create_resource(struct sk_buff *skb, struct genl_info *info);
47// int drbd_adm_delete_resource(struct sk_buff *skb, struct genl_info *info);
48
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +020049int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info);
50int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010051
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +020052int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info);
53int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +010054int drbd_adm_down(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010055
56int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info);
57int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info);
Lars Ellenbergf3990022011-03-23 14:31:09 +010058int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010059int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info);
60int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info);
Lars Ellenbergf3990022011-03-23 14:31:09 +010061int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010062int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info);
63int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info);
64int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info);
65int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info);
66int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info);
67int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info);
68int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info);
69int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info);
70int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info);
71int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info);
72int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info);
Lars Ellenbergf3990022011-03-23 14:31:09 +010073int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010074int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info);
75int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info);
76/* .dumpit */
77int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb);
78
79#include <linux/drbd_genl_api.h>
Andreas Gruenbacher01b39b52011-06-10 12:57:26 +020080#include "drbd_nla.h"
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010081#include <linux/genl_magic_func.h>
82
83/* used blkdev_get_by_path, to claim our meta data device(s) */
Philipp Reisnerb411b362009-09-25 16:07:19 -070084static char *drbd_m_holder = "Hands off! this is DRBD's meta data device.";
85
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010086static void drbd_adm_send_reply(struct sk_buff *skb, struct genl_info *info)
87{
88 genlmsg_end(skb, genlmsg_data(nlmsg_data(nlmsg_hdr(skb))));
89 if (genlmsg_reply(skb, info))
Lars Ellenbergf88c5d92014-03-27 14:10:55 +010090 pr_err("error sending genl reply\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -070091}
92
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010093/* Used on a fresh "drbd_adm_prepare"d reply_skb, this cannot fail: The only
94 * reason it could fail was no space in skb, and there are 4k available. */
Lars Ellenberga910b122014-04-28 18:43:21 +020095int drbd_msg_put_info(struct sk_buff *skb, const char *info)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010096{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010097 struct nlattr *nla;
98 int err = -EMSGSIZE;
Philipp Reisnerb411b362009-09-25 16:07:19 -070099
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100100 if (!info || !info[0])
101 return 0;
102
103 nla = nla_nest_start(skb, DRBD_NLA_CFG_REPLY);
104 if (!nla)
105 return err;
106
107 err = nla_put_string(skb, T_info_text, info);
108 if (err) {
109 nla_nest_cancel(skb, nla);
110 return err;
111 } else
112 nla_nest_end(skb, nla);
113 return 0;
114}
115
116/* This would be a good candidate for a "pre_doit" hook,
117 * and per-family private info->pointers.
118 * But we need to stay compatible with older kernels.
119 * If it returns successfully, adm_ctx members are valid.
Lars Ellenberg9e276872014-04-28 18:43:22 +0200120 *
121 * At this point, we still rely on the global genl_lock().
122 * If we want to avoid that, and allow "genl_family.parallel_ops", we may need
123 * to add additional synchronization against object destruction/modification.
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100124 */
125#define DRBD_ADM_NEED_MINOR 1
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +0200126#define DRBD_ADM_NEED_RESOURCE 2
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200127#define DRBD_ADM_NEED_CONNECTION 4
Lars Ellenberga910b122014-04-28 18:43:21 +0200128static int drbd_adm_prepare(struct drbd_config_context *adm_ctx,
129 struct sk_buff *skb, struct genl_info *info, unsigned flags)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100130{
131 struct drbd_genlmsghdr *d_in = info->userhdr;
132 const u8 cmd = info->genlhdr->cmd;
133 int err;
134
Lars Ellenberga910b122014-04-28 18:43:21 +0200135 memset(adm_ctx, 0, sizeof(*adm_ctx));
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100136
137 /* genl_rcv_msg only checks for CAP_NET_ADMIN on "GENL_ADMIN_PERM" :( */
Philipp Reisner98683652012-11-09 14:18:43 +0100138 if (cmd != DRBD_ADM_GET_STATUS && !capable(CAP_NET_ADMIN))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100139 return -EPERM;
140
Lars Ellenberga910b122014-04-28 18:43:21 +0200141 adm_ctx->reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
142 if (!adm_ctx->reply_skb) {
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200143 err = -ENOMEM;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100144 goto fail;
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200145 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100146
Lars Ellenberga910b122014-04-28 18:43:21 +0200147 adm_ctx->reply_dh = genlmsg_put_reply(adm_ctx->reply_skb,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100148 info, &drbd_genl_family, 0, cmd);
149 /* put of a few bytes into a fresh skb of >= 4k will always succeed.
150 * but anyways */
Lars Ellenberga910b122014-04-28 18:43:21 +0200151 if (!adm_ctx->reply_dh) {
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200152 err = -ENOMEM;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100153 goto fail;
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200154 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100155
Lars Ellenberga910b122014-04-28 18:43:21 +0200156 adm_ctx->reply_dh->minor = d_in->minor;
157 adm_ctx->reply_dh->ret_code = NO_ERROR;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100158
Lars Ellenberga910b122014-04-28 18:43:21 +0200159 adm_ctx->volume = VOLUME_UNSPECIFIED;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100160 if (info->attrs[DRBD_NLA_CFG_CONTEXT]) {
161 struct nlattr *nla;
162 /* parse and validate only */
Lars Ellenbergf3990022011-03-23 14:31:09 +0100163 err = drbd_cfg_context_from_attrs(NULL, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100164 if (err)
165 goto fail;
166
167 /* It was present, and valid,
168 * copy it over to the reply skb. */
Lars Ellenberga910b122014-04-28 18:43:21 +0200169 err = nla_put_nohdr(adm_ctx->reply_skb,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100170 info->attrs[DRBD_NLA_CFG_CONTEXT]->nla_len,
171 info->attrs[DRBD_NLA_CFG_CONTEXT]);
172 if (err)
173 goto fail;
174
Lars Ellenberg9e276872014-04-28 18:43:22 +0200175 /* and assign stuff to the adm_ctx */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100176 nla = nested_attr_tb[__nla_type(T_ctx_volume)];
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200177 if (nla)
Lars Ellenberga910b122014-04-28 18:43:21 +0200178 adm_ctx->volume = nla_get_u32(nla);
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +0200179 nla = nested_attr_tb[__nla_type(T_ctx_resource_name)];
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100180 if (nla)
Lars Ellenberga910b122014-04-28 18:43:21 +0200181 adm_ctx->resource_name = nla_data(nla);
182 adm_ctx->my_addr = nested_attr_tb[__nla_type(T_ctx_my_addr)];
183 adm_ctx->peer_addr = nested_attr_tb[__nla_type(T_ctx_peer_addr)];
184 if ((adm_ctx->my_addr &&
185 nla_len(adm_ctx->my_addr) > sizeof(adm_ctx->connection->my_addr)) ||
186 (adm_ctx->peer_addr &&
187 nla_len(adm_ctx->peer_addr) > sizeof(adm_ctx->connection->peer_addr))) {
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200188 err = -EINVAL;
189 goto fail;
190 }
191 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100192
Lars Ellenberga910b122014-04-28 18:43:21 +0200193 adm_ctx->minor = d_in->minor;
194 adm_ctx->device = minor_to_device(d_in->minor);
Lars Ellenberg9e276872014-04-28 18:43:22 +0200195
196 /* We are protected by the global genl_lock().
197 * But we may explicitly drop it/retake it in drbd_adm_set_role(),
198 * so make sure this object stays around. */
199 if (adm_ctx->device)
200 kref_get(&adm_ctx->device->kref);
201
Lars Ellenberga910b122014-04-28 18:43:21 +0200202 if (adm_ctx->resource_name) {
203 adm_ctx->resource = drbd_find_resource(adm_ctx->resource_name);
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +0200204 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100205
Lars Ellenberga910b122014-04-28 18:43:21 +0200206 if (!adm_ctx->device && (flags & DRBD_ADM_NEED_MINOR)) {
207 drbd_msg_put_info(adm_ctx->reply_skb, "unknown minor");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100208 return ERR_MINOR_INVALID;
209 }
Lars Ellenberga910b122014-04-28 18:43:21 +0200210 if (!adm_ctx->resource && (flags & DRBD_ADM_NEED_RESOURCE)) {
211 drbd_msg_put_info(adm_ctx->reply_skb, "unknown resource");
212 if (adm_ctx->resource_name)
Andreas Gruenbachera10f6b82011-06-09 01:26:16 +0200213 return ERR_RES_NOT_KNOWN;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100214 return ERR_INVALID_REQUEST;
215 }
216
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200217 if (flags & DRBD_ADM_NEED_CONNECTION) {
Lars Ellenberga910b122014-04-28 18:43:21 +0200218 if (adm_ctx->resource) {
219 drbd_msg_put_info(adm_ctx->reply_skb, "no resource name expected");
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200220 return ERR_INVALID_REQUEST;
221 }
Lars Ellenberga910b122014-04-28 18:43:21 +0200222 if (adm_ctx->device) {
223 drbd_msg_put_info(adm_ctx->reply_skb, "no minor number expected");
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200224 return ERR_INVALID_REQUEST;
225 }
Lars Ellenberga910b122014-04-28 18:43:21 +0200226 if (adm_ctx->my_addr && adm_ctx->peer_addr)
227 adm_ctx->connection = conn_get_by_addrs(nla_data(adm_ctx->my_addr),
228 nla_len(adm_ctx->my_addr),
229 nla_data(adm_ctx->peer_addr),
230 nla_len(adm_ctx->peer_addr));
231 if (!adm_ctx->connection) {
232 drbd_msg_put_info(adm_ctx->reply_skb, "unknown connection");
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200233 return ERR_INVALID_REQUEST;
234 }
235 }
236
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100237 /* some more paranoia, if the request was over-determined */
Lars Ellenberga910b122014-04-28 18:43:21 +0200238 if (adm_ctx->device && adm_ctx->resource &&
239 adm_ctx->device->resource != adm_ctx->resource) {
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +0200240 pr_warning("request: minor=%u, resource=%s; but that minor belongs to resource %s\n",
Lars Ellenberga910b122014-04-28 18:43:21 +0200241 adm_ctx->minor, adm_ctx->resource->name,
242 adm_ctx->device->resource->name);
243 drbd_msg_put_info(adm_ctx->reply_skb, "minor exists in different resource");
Lars Ellenberg527f4b22011-03-14 13:58:03 +0100244 return ERR_INVALID_REQUEST;
245 }
Lars Ellenberga910b122014-04-28 18:43:21 +0200246 if (adm_ctx->device &&
247 adm_ctx->volume != VOLUME_UNSPECIFIED &&
248 adm_ctx->volume != adm_ctx->device->vnr) {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100249 pr_warning("request: minor=%u, volume=%u; but that minor is volume %u in %s\n",
Lars Ellenberga910b122014-04-28 18:43:21 +0200250 adm_ctx->minor, adm_ctx->volume,
251 adm_ctx->device->vnr,
252 adm_ctx->device->resource->name);
253 drbd_msg_put_info(adm_ctx->reply_skb, "minor exists as different volume");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100254 return ERR_INVALID_REQUEST;
255 }
Philipp Reisner0ace9df2011-04-24 10:53:19 +0200256
Lars Ellenberg9e276872014-04-28 18:43:22 +0200257 /* still, provide adm_ctx->resource always, if possible. */
258 if (!adm_ctx->resource) {
259 adm_ctx->resource = adm_ctx->device ? adm_ctx->device->resource
260 : adm_ctx->connection ? adm_ctx->connection->resource : NULL;
261 if (adm_ctx->resource)
262 kref_get(&adm_ctx->resource->kref);
263 }
264
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100265 return NO_ERROR;
266
267fail:
Lars Ellenberga910b122014-04-28 18:43:21 +0200268 nlmsg_free(adm_ctx->reply_skb);
269 adm_ctx->reply_skb = NULL;
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200270 return err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100271}
272
Lars Ellenberga910b122014-04-28 18:43:21 +0200273static int drbd_adm_finish(struct drbd_config_context *adm_ctx,
274 struct genl_info *info, int retcode)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100275{
Lars Ellenberg9e276872014-04-28 18:43:22 +0200276 if (adm_ctx->device) {
277 kref_put(&adm_ctx->device->kref, drbd_destroy_device);
278 adm_ctx->device = NULL;
279 }
Lars Ellenberga910b122014-04-28 18:43:21 +0200280 if (adm_ctx->connection) {
281 kref_put(&adm_ctx->connection->kref, &drbd_destroy_connection);
282 adm_ctx->connection = NULL;
Philipp Reisner0ace9df2011-04-24 10:53:19 +0200283 }
Lars Ellenberga910b122014-04-28 18:43:21 +0200284 if (adm_ctx->resource) {
285 kref_put(&adm_ctx->resource->kref, drbd_destroy_resource);
286 adm_ctx->resource = NULL;
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +0200287 }
Philipp Reisner0ace9df2011-04-24 10:53:19 +0200288
Lars Ellenberga910b122014-04-28 18:43:21 +0200289 if (!adm_ctx->reply_skb)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100290 return -ENOMEM;
291
Lars Ellenberga910b122014-04-28 18:43:21 +0200292 adm_ctx->reply_dh->ret_code = retcode;
293 drbd_adm_send_reply(adm_ctx->reply_skb, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100294 return 0;
295}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700296
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200297static void setup_khelper_env(struct drbd_connection *connection, char **envp)
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100298{
299 char *afs;
300
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200301 /* FIXME: A future version will not allow this case. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200302 if (connection->my_addr_len == 0 || connection->peer_addr_len == 0)
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200303 return;
304
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200305 switch (((struct sockaddr *)&connection->peer_addr)->sa_family) {
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200306 case AF_INET6:
307 afs = "ipv6";
308 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200309 &((struct sockaddr_in6 *)&connection->peer_addr)->sin6_addr);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200310 break;
311 case AF_INET:
312 afs = "ipv4";
313 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200314 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200315 break;
316 default:
317 afs = "ssocks";
318 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200319 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100320 }
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200321 snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100322}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700323
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200324int drbd_khelper(struct drbd_device *device, char *cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700325{
326 char *envp[] = { "HOME=/",
327 "TERM=linux",
328 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100329 (char[20]) { }, /* address family */
330 (char[60]) { }, /* address */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700331 NULL };
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100332 char mb[12];
Philipp Reisnerb411b362009-09-25 16:07:19 -0700333 char *argv[] = {usermode_helper, cmd, mb, NULL };
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200334 struct drbd_connection *connection = first_peer_device(device)->connection;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100335 struct sib_info sib;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700336 int ret;
337
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200338 if (current == connection->worker.task)
339 set_bit(CALLBACK_PENDING, &connection->flags);
Lars Ellenbergc2ba6862012-06-14 15:14:06 +0200340
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200341 snprintf(mb, 12, "minor-%d", device_to_minor(device));
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200342 setup_khelper_env(connection, envp);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700343
Lars Ellenberg1090c052010-07-19 17:41:04 +0200344 /* The helper may take some time.
345 * write out any unsynced meta data changes now */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200346 drbd_md_sync(device);
Lars Ellenberg1090c052010-07-19 17:41:04 +0200347
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200348 drbd_info(device, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100349 sib.sib_reason = SIB_HELPER_PRE;
350 sib.helper_name = cmd;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200351 drbd_bcast_event(device, &sib);
Oleg Nesterov70834d32012-03-23 15:02:46 -0700352 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700353 if (ret)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200354 drbd_warn(device, "helper command: %s %s %s exit code %u (0x%x)\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -0700355 usermode_helper, cmd, mb,
356 (ret >> 8) & 0xff, ret);
357 else
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200358 drbd_info(device, "helper command: %s %s %s exit code %u (0x%x)\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -0700359 usermode_helper, cmd, mb,
360 (ret >> 8) & 0xff, ret);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100361 sib.sib_reason = SIB_HELPER_POST;
362 sib.helper_exit_code = ret;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200363 drbd_bcast_event(device, &sib);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700364
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200365 if (current == connection->worker.task)
366 clear_bit(CALLBACK_PENDING, &connection->flags);
Lars Ellenbergc2ba6862012-06-14 15:14:06 +0200367
Philipp Reisnerb411b362009-09-25 16:07:19 -0700368 if (ret < 0) /* Ignore any ERRNOs we got. */
369 ret = 0;
370
371 return ret;
372}
373
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200374static int conn_khelper(struct drbd_connection *connection, char *cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700375{
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100376 char *envp[] = { "HOME=/",
377 "TERM=linux",
378 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
379 (char[20]) { }, /* address family */
380 (char[60]) { }, /* address */
381 NULL };
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +0200382 char *resource_name = connection->resource->name;
383 char *argv[] = {usermode_helper, cmd, resource_name, NULL };
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100384 int ret;
385
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200386 setup_khelper_env(connection, envp);
387 conn_md_sync(connection);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100388
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200389 drbd_info(connection, "helper command: %s %s %s\n", usermode_helper, cmd, resource_name);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100390 /* TODO: conn_bcast_event() ?? */
391
Philipp Reisner98683652012-11-09 14:18:43 +0100392 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100393 if (ret)
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200394 drbd_warn(connection, "helper command: %s %s %s exit code %u (0x%x)\n",
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +0200395 usermode_helper, cmd, resource_name,
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100396 (ret >> 8) & 0xff, ret);
397 else
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200398 drbd_info(connection, "helper command: %s %s %s exit code %u (0x%x)\n",
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +0200399 usermode_helper, cmd, resource_name,
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100400 (ret >> 8) & 0xff, ret);
401 /* TODO: conn_bcast_event() ?? */
402
403 if (ret < 0) /* Ignore any ERRNOs we got. */
404 ret = 0;
405
406 return ret;
407}
408
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200409static enum drbd_fencing_p highest_fencing_policy(struct drbd_connection *connection)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700410{
Philipp Reisnercb703452011-03-24 11:03:07 +0100411 enum drbd_fencing_p fp = FP_NOT_AVAIL;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200412 struct drbd_peer_device *peer_device;
Philipp Reisnercb703452011-03-24 11:03:07 +0100413 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700414
Philipp Reisner695d08f2011-04-11 22:53:32 -0700415 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200416 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
417 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200418 if (get_ldev_if_state(device, D_CONSISTENT)) {
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200419 struct disk_conf *disk_conf =
420 rcu_dereference(peer_device->device->ldev->disk_conf);
421 fp = max_t(enum drbd_fencing_p, fp, disk_conf->fencing);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200422 put_ldev(device);
Philipp Reisnercb703452011-03-24 11:03:07 +0100423 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700424 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700425 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700426
Philipp Reisnerd7fe69c2014-04-28 18:43:13 +0200427 if (fp == FP_NOT_AVAIL) {
428 /* IO Suspending works on the whole resource.
429 Do it only for one device. */
430 vnr = 0;
431 peer_device = idr_get_next(&connection->peer_devices, &vnr);
432 drbd_change_state(peer_device->device, CS_VERBOSE | CS_HARD, NS(susp_fen, 0));
433 }
434
Philipp Reisnercb703452011-03-24 11:03:07 +0100435 return fp;
436}
437
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200438bool conn_try_outdate_peer(struct drbd_connection *connection)
Philipp Reisnercb703452011-03-24 11:03:07 +0100439{
Philipp Reisner28e448b2013-06-25 16:50:06 +0200440 unsigned int connect_cnt;
Philipp Reisnercb703452011-03-24 11:03:07 +0100441 union drbd_state mask = { };
442 union drbd_state val = { };
443 enum drbd_fencing_p fp;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700444 char *ex_to_string;
445 int r;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700446
Philipp Reisner02df6fe2014-04-28 18:43:33 +0200447 spin_lock_irq(&connection->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200448 if (connection->cstate >= C_WF_REPORT_PARAMS) {
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200449 drbd_err(connection, "Expected cstate < C_WF_REPORT_PARAMS\n");
Philipp Reisner02df6fe2014-04-28 18:43:33 +0200450 spin_unlock_irq(&connection->resource->req_lock);
Philipp Reisnercb703452011-03-24 11:03:07 +0100451 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700452 }
453
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200454 connect_cnt = connection->connect_cnt;
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200455 spin_unlock_irq(&connection->resource->req_lock);
Philipp Reisner28e448b2013-06-25 16:50:06 +0200456
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200457 fp = highest_fencing_policy(connection);
Philipp Reisnercb703452011-03-24 11:03:07 +0100458 switch (fp) {
459 case FP_NOT_AVAIL:
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200460 drbd_warn(connection, "Not fencing peer, I'm not even Consistent myself.\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100461 goto out;
462 case FP_DONT_CARE:
463 return true;
464 default: ;
465 }
466
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200467 r = conn_khelper(connection, "fence-peer");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700468
469 switch ((r>>8) & 0xff) {
470 case 3: /* peer is inconsistent */
471 ex_to_string = "peer is inconsistent or worse";
Philipp Reisnercb703452011-03-24 11:03:07 +0100472 mask.pdsk = D_MASK;
473 val.pdsk = D_INCONSISTENT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700474 break;
475 case 4: /* peer got outdated, or was already outdated */
476 ex_to_string = "peer was fenced";
Philipp Reisnercb703452011-03-24 11:03:07 +0100477 mask.pdsk = D_MASK;
478 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700479 break;
480 case 5: /* peer was down */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200481 if (conn_highest_disk(connection) == D_UP_TO_DATE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700482 /* we will(have) create(d) a new UUID anyways... */
483 ex_to_string = "peer is unreachable, assumed to be dead";
Philipp Reisnercb703452011-03-24 11:03:07 +0100484 mask.pdsk = D_MASK;
485 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700486 } else {
487 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
Philipp Reisnerb411b362009-09-25 16:07:19 -0700488 }
489 break;
490 case 6: /* Peer is primary, voluntarily outdate myself.
491 * This is useful when an unconnected R_SECONDARY is asked to
492 * become R_PRIMARY, but finds the other peer being active. */
493 ex_to_string = "peer is active";
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200494 drbd_warn(connection, "Peer is primary, outdating myself.\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100495 mask.disk = D_MASK;
496 val.disk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700497 break;
498 case 7:
499 if (fp != FP_STONITH)
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200500 drbd_err(connection, "fence-peer() = 7 && fencing != Stonith !!!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700501 ex_to_string = "peer was stonithed";
Philipp Reisnercb703452011-03-24 11:03:07 +0100502 mask.pdsk = D_MASK;
503 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700504 break;
505 default:
506 /* The script is broken ... */
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200507 drbd_err(connection, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
Philipp Reisnercb703452011-03-24 11:03:07 +0100508 return false; /* Eventually leave IO frozen */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700509 }
510
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200511 drbd_info(connection, "fence-peer helper returned %d (%s)\n",
Philipp Reisnercb703452011-03-24 11:03:07 +0100512 (r>>8) & 0xff, ex_to_string);
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200513
Philipp Reisnercb703452011-03-24 11:03:07 +0100514 out:
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200515
Philipp Reisnercb703452011-03-24 11:03:07 +0100516 /* Not using
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200517 conn_request_state(connection, mask, val, CS_VERBOSE);
Philipp Reisnercb703452011-03-24 11:03:07 +0100518 here, because we might were able to re-establish the connection in the
519 meantime. */
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200520 spin_lock_irq(&connection->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200521 if (connection->cstate < C_WF_REPORT_PARAMS && !test_bit(STATE_SENT, &connection->flags)) {
522 if (connection->connect_cnt != connect_cnt)
Philipp Reisner28e448b2013-06-25 16:50:06 +0200523 /* In case the connection was established and droped
524 while the fence-peer handler was running, ignore it */
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200525 drbd_info(connection, "Ignoring fence-peer exit code\n");
Philipp Reisner28e448b2013-06-25 16:50:06 +0200526 else
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200527 _conn_request_state(connection, mask, val, CS_VERBOSE);
Philipp Reisner28e448b2013-06-25 16:50:06 +0200528 }
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200529 spin_unlock_irq(&connection->resource->req_lock);
Philipp Reisnercb703452011-03-24 11:03:07 +0100530
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200531 return conn_highest_pdsk(connection) <= D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700532}
533
Philipp Reisner87f7be42010-06-11 13:56:33 +0200534static int _try_outdate_peer_async(void *data)
535{
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200536 struct drbd_connection *connection = (struct drbd_connection *)data;
Philipp Reisner87f7be42010-06-11 13:56:33 +0200537
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200538 conn_try_outdate_peer(connection);
Philipp Reisner21423fa2011-05-17 14:19:41 +0200539
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +0200540 kref_put(&connection->kref, drbd_destroy_connection);
Philipp Reisner87f7be42010-06-11 13:56:33 +0200541 return 0;
542}
543
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200544void conn_try_outdate_peer_async(struct drbd_connection *connection)
Philipp Reisner87f7be42010-06-11 13:56:33 +0200545{
546 struct task_struct *opa;
547
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200548 kref_get(&connection->kref);
Lars Ellenbergbbc1c5e2014-07-09 21:18:32 +0200549 /* We may just have force_sig()'ed this thread
550 * to get it out of some blocking network function.
551 * Clear signals; otherwise kthread_run(), which internally uses
552 * wait_on_completion_killable(), will mistake our pending signal
553 * for a new fatal signal and fail. */
554 flush_signals(current);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200555 opa = kthread_run(_try_outdate_peer_async, connection, "drbd_async_h");
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200556 if (IS_ERR(opa)) {
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200557 drbd_err(connection, "out of mem, failed to invoke fence-peer helper\n");
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +0200558 kref_put(&connection->kref, drbd_destroy_connection);
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200559 }
Philipp Reisner87f7be42010-06-11 13:56:33 +0200560}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700561
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100562enum drbd_state_rv
Lars Ellenberg44a4d552013-11-22 12:40:58 +0100563drbd_set_role(struct drbd_device *const device, enum drbd_role new_role, int force)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700564{
Lars Ellenberg44a4d552013-11-22 12:40:58 +0100565 struct drbd_peer_device *const peer_device = first_peer_device(device);
566 struct drbd_connection *const connection = peer_device ? peer_device->connection : NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700567 const int max_tries = 4;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100568 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200569 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700570 int try = 0;
571 int forced = 0;
572 union drbd_state mask, val;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700573
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +0200574 if (new_role == R_PRIMARY) {
575 struct drbd_connection *connection;
576
577 /* Detect dead peers as soon as possible. */
578
579 rcu_read_lock();
580 for_each_connection(connection, device->resource)
581 request_ping(connection);
582 rcu_read_unlock();
583 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700584
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200585 mutex_lock(device->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700586
587 mask.i = 0; mask.role = R_MASK;
588 val.i = 0; val.role = new_role;
589
590 while (try++ < max_tries) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200591 rv = _drbd_request_state(device, mask, val, CS_WAIT_COMPLETE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700592
593 /* in case we first succeeded to outdate,
594 * but now suddenly could establish a connection */
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100595 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700596 val.pdsk = 0;
597 mask.pdsk = 0;
598 continue;
599 }
600
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100601 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200602 (device->state.disk < D_UP_TO_DATE &&
603 device->state.disk >= D_INCONSISTENT)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700604 mask.disk = D_MASK;
605 val.disk = D_UP_TO_DATE;
606 forced = 1;
607 continue;
608 }
609
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100610 if (rv == SS_NO_UP_TO_DATE_DISK &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200611 device->state.disk == D_CONSISTENT && mask.pdsk == 0) {
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +0200612 D_ASSERT(device, device->state.pdsk == D_UNKNOWN);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700613
Lars Ellenberg44a4d552013-11-22 12:40:58 +0100614 if (conn_try_outdate_peer(connection)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700615 val.disk = D_UP_TO_DATE;
616 mask.disk = D_MASK;
617 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700618 continue;
619 }
620
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100621 if (rv == SS_NOTHING_TO_DO)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100622 goto out;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100623 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
Lars Ellenberg44a4d552013-11-22 12:40:58 +0100624 if (!conn_try_outdate_peer(connection) && force) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200625 drbd_warn(device, "Forced into split brain situation!\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100626 mask.pdsk = D_MASK;
627 val.pdsk = D_OUTDATED;
628
Philipp Reisnerb411b362009-09-25 16:07:19 -0700629 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700630 continue;
631 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100632 if (rv == SS_TWO_PRIMARIES) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700633 /* Maybe the peer is detected as dead very soon...
634 retry at most once more in this case. */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200635 int timeo;
636 rcu_read_lock();
Lars Ellenberg44a4d552013-11-22 12:40:58 +0100637 nc = rcu_dereference(connection->net_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +0200638 timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
639 rcu_read_unlock();
640 schedule_timeout_interruptible(timeo);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700641 if (try < max_tries)
642 try = max_tries - 1;
643 continue;
644 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100645 if (rv < SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200646 rv = _drbd_request_state(device, mask, val,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700647 CS_VERBOSE + CS_WAIT_COMPLETE);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100648 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100649 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700650 }
651 break;
652 }
653
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100654 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100655 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700656
657 if (forced)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200658 drbd_warn(device, "Forced to consider local data as UpToDate!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700659
660 /* Wait until nothing is on the fly :) */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200661 wait_event(device->misc_wait, atomic_read(&device->ap_pending_cnt) == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700662
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100663 /* FIXME also wait for all pending P_BARRIER_ACK? */
664
Philipp Reisnerb411b362009-09-25 16:07:19 -0700665 if (new_role == R_SECONDARY) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200666 if (get_ldev(device)) {
667 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
668 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700669 }
670 } else {
Lars Ellenberg66ce6db2014-02-05 06:17:01 +0100671 mutex_lock(&device->resource->conf_update);
Lars Ellenberg44a4d552013-11-22 12:40:58 +0100672 nc = connection->net_conf;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200673 if (nc)
Andreas Gruenbacher6139f602011-05-06 20:00:02 +0200674 nc->discard_my_data = 0; /* without copy; single bit op is atomic */
Lars Ellenberg66ce6db2014-02-05 06:17:01 +0100675 mutex_unlock(&device->resource->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +0200676
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200677 if (get_ldev(device)) {
678 if (((device->state.conn < C_CONNECTED ||
679 device->state.pdsk <= D_FAILED)
680 && device->ldev->md.uuid[UI_BITMAP] == 0) || forced)
681 drbd_uuid_new_current(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700682
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200683 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
684 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700685 }
686 }
687
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100688 /* writeout of activity log covered areas of the bitmap
689 * to stable storage done in after state change already */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700690
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200691 if (device->state.conn >= C_WF_REPORT_PARAMS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700692 /* if this was forced, we should consider sync */
693 if (forced)
Lars Ellenberg44a4d552013-11-22 12:40:58 +0100694 drbd_send_uuids(peer_device);
695 drbd_send_current_state(peer_device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700696 }
697
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200698 drbd_md_sync(device);
Lars Ellenberg720979f2014-02-05 06:28:08 +0100699 set_disk_ro(device->vdisk, new_role == R_SECONDARY);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200700 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100701out:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200702 mutex_unlock(device->state_mutex);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100703 return rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700704}
705
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100706static const char *from_attrs_err_to_txt(int err)
Lars Ellenbergef50a3e2010-09-01 14:39:30 +0200707{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100708 return err == -ENOMSG ? "required attribute missing" :
709 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
Lars Ellenbergf3990022011-03-23 14:31:09 +0100710 err == -EEXIST ? "can not change invariant setting" :
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100711 "invalid attribute value";
Lars Ellenbergef50a3e2010-09-01 14:39:30 +0200712}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700713
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100714int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700715{
Lars Ellenberga910b122014-04-28 18:43:21 +0200716 struct drbd_config_context adm_ctx;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100717 struct set_role_parms parms;
718 int err;
719 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700720
Lars Ellenberga910b122014-04-28 18:43:21 +0200721 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100722 if (!adm_ctx.reply_skb)
723 return retcode;
724 if (retcode != NO_ERROR)
725 goto out;
726
727 memset(&parms, 0, sizeof(parms));
728 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +0100729 err = set_role_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100730 if (err) {
731 retcode = ERR_MANDATORY_TAG;
Lars Ellenberga910b122014-04-28 18:43:21 +0200732 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100733 goto out;
734 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700735 }
Lars Ellenberg9e276872014-04-28 18:43:22 +0200736 genl_unlock();
737 mutex_lock(&adm_ctx.resource->adm_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700738
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100739 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200740 retcode = drbd_set_role(adm_ctx.device, R_PRIMARY, parms.assume_uptodate);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100741 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200742 retcode = drbd_set_role(adm_ctx.device, R_SECONDARY, 0);
Lars Ellenberg9e276872014-04-28 18:43:22 +0200743
744 mutex_unlock(&adm_ctx.resource->adm_mutex);
745 genl_lock();
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100746out:
Lars Ellenberga910b122014-04-28 18:43:21 +0200747 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700748 return 0;
749}
750
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100751/* Initializes the md.*_offset members, so we are able to find
752 * the on disk meta data.
753 *
754 * We currently have two possible layouts:
755 * external:
756 * |----------- md_size_sect ------------------|
757 * [ 4k superblock ][ activity log ][ Bitmap ]
758 * | al_offset == 8 |
759 * | bm_offset = al_offset + X |
760 * ==> bitmap sectors = md_size_sect - bm_offset
761 *
762 * internal:
763 * |----------- md_size_sect ------------------|
764 * [data.....][ Bitmap ][ activity log ][ 4k superblock ]
765 * | al_offset < 0 |
766 * | bm_offset = al_offset - Y |
767 * ==> bitmap sectors = Y = al_offset - bm_offset
768 *
769 * Activity log size used to be fixed 32kB,
770 * but is about to become configurable.
771 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200772static void drbd_md_set_sector_offsets(struct drbd_device *device,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700773 struct drbd_backing_dev *bdev)
774{
775 sector_t md_size_sect = 0;
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +0100776 unsigned int al_size_sect = bdev->md.al_size_4k * 8;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200777
Lars Ellenberg3a4d4eb2013-03-19 18:16:44 +0100778 bdev->md.md_offset = drbd_md_ss(bdev);
779
Lars Ellenberg68e41a42013-03-19 18:16:45 +0100780 switch (bdev->md.meta_dev_idx) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700781 default:
782 /* v07 style fixed size indexed meta data */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100783 bdev->md.md_size_sect = MD_128MB_SECT;
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100784 bdev->md.al_offset = MD_4kB_SECT;
785 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700786 break;
787 case DRBD_MD_INDEX_FLEX_EXT:
788 /* just occupy the full device; unit: sectors */
789 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100790 bdev->md.al_offset = MD_4kB_SECT;
791 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700792 break;
793 case DRBD_MD_INDEX_INTERNAL:
794 case DRBD_MD_INDEX_FLEX_INT:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700795 /* al size is still fixed */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100796 bdev->md.al_offset = -al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700797 /* we need (slightly less than) ~ this much bitmap sectors: */
798 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
799 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
800 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
801 md_size_sect = ALIGN(md_size_sect, 8);
802
803 /* plus the "drbd meta data super block",
804 * and the activity log; */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100805 md_size_sect += MD_4kB_SECT + al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700806
807 bdev->md.md_size_sect = md_size_sect;
808 /* bitmap offset is adjusted by 'super' block size */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100809 bdev->md.bm_offset = -md_size_sect + MD_4kB_SECT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700810 break;
811 }
812}
813
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100814/* input size is expected to be in KB */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700815char *ppsize(char *buf, unsigned long long size)
816{
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100817 /* Needs 9 bytes at max including trailing NUL:
818 * -1ULL ==> "16384 EB" */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700819 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
820 int base = 0;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100821 while (size >= 10000 && base < sizeof(units)-1) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700822 /* shift + round */
823 size = (size >> 10) + !!(size & (1<<9));
824 base++;
825 }
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100826 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700827
828 return buf;
829}
830
831/* there is still a theoretical deadlock when called from receiver
832 * on an D_INCONSISTENT R_PRIMARY:
833 * remote READ does inc_ap_bio, receiver would need to receive answer
834 * packet from remote to dec_ap_bio again.
835 * receiver receive_sizes(), comes here,
836 * waits for ap_bio_cnt == 0. -> deadlock.
837 * but this cannot happen, actually, because:
838 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
839 * (not connected, or bad/no disk on peer):
840 * see drbd_fail_request_early, ap_bio_cnt is zero.
841 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
842 * peer may not initiate a resize.
843 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100844/* Note these are not to be confused with
845 * drbd_adm_suspend_io/drbd_adm_resume_io,
846 * which are (sub) state changes triggered by admin (drbdsetup),
847 * and can be long lived.
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200848 * This changes an device->flag, is triggered by drbd internals,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100849 * and should be short-lived. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200850void drbd_suspend_io(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700851{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200852 set_bit(SUSPEND_IO, &device->flags);
853 if (drbd_suspended(device))
Philipp Reisner265be2d2010-05-31 10:14:17 +0200854 return;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200855 wait_event(device->misc_wait, !atomic_read(&device->ap_bio_cnt));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700856}
857
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200858void drbd_resume_io(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700859{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200860 clear_bit(SUSPEND_IO, &device->flags);
861 wake_up(&device->misc_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700862}
863
864/**
865 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200866 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700867 *
868 * Returns 0 on success, negative return values indicate errors.
869 * You should call drbd_md_sync() after calling this function.
870 */
Philipp Reisnerd752b262013-06-25 16:50:08 +0200871enum determine_dev_size
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200872drbd_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 -0700873{
874 sector_t prev_first_sect, prev_size; /* previous meta location */
Lars Ellenbergcccac982013-03-19 18:16:46 +0100875 sector_t la_size_sect, u_size;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200876 struct drbd_md *md = &device->ldev->md;
Philipp Reisnerd752b262013-06-25 16:50:08 +0200877 u32 prev_al_stripe_size_4k;
878 u32 prev_al_stripes;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700879 sector_t size;
880 char ppb[10];
Philipp Reisnerd752b262013-06-25 16:50:08 +0200881 void *buffer;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700882
883 int md_moved, la_size_changed;
Philipp Reisnere96c9632013-06-25 16:50:07 +0200884 enum determine_dev_size rv = DS_UNCHANGED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700885
886 /* race:
887 * application request passes inc_ap_bio,
888 * but then cannot get an AL-reference.
889 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
890 *
891 * to avoid that:
892 * Suspend IO right here.
893 * still lock the act_log to not trigger ASSERTs there.
894 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200895 drbd_suspend_io(device);
Lars Ellenberge37d2432014-04-01 23:53:30 +0200896 buffer = drbd_md_get_buffer(device, __func__); /* Lock meta-data IO */
Philipp Reisnerd752b262013-06-25 16:50:08 +0200897 if (!buffer) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200898 drbd_resume_io(device);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200899 return DS_ERROR;
900 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700901
902 /* no wait necessary anymore, actually we could assert that */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200903 wait_event(device->al_wait, lc_try_lock(device->act_log));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700904
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200905 prev_first_sect = drbd_md_first_sector(device->ldev);
906 prev_size = device->ldev->md.md_size_sect;
907 la_size_sect = device->ldev->md.la_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700908
Philipp Reisnerd752b262013-06-25 16:50:08 +0200909 if (rs) {
910 /* rs is non NULL if we should change the AL layout only */
911
912 prev_al_stripes = md->al_stripes;
913 prev_al_stripe_size_4k = md->al_stripe_size_4k;
914
915 md->al_stripes = rs->al_stripes;
916 md->al_stripe_size_4k = rs->al_stripe_size / 4;
917 md->al_size_4k = (u64)rs->al_stripes * rs->al_stripe_size / 4;
918 }
919
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200920 drbd_md_set_sector_offsets(device, device->ldev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700921
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200922 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200923 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200924 rcu_read_unlock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200925 size = drbd_new_dev_size(device, device->ldev, u_size, flags & DDSF_FORCED);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700926
Philipp Reisnerd752b262013-06-25 16:50:08 +0200927 if (size < la_size_sect) {
928 if (rs && u_size == 0) {
929 /* Remove "rs &&" later. This check should always be active, but
930 right now the receiver expects the permissive behavior */
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200931 drbd_warn(device, "Implicit shrink not allowed. "
Philipp Reisnerd752b262013-06-25 16:50:08 +0200932 "Use --size=%llus for explicit shrink.\n",
933 (unsigned long long)size);
934 rv = DS_ERROR_SHRINK;
935 }
936 if (u_size > size)
937 rv = DS_ERROR_SPACE_MD;
938 if (rv != DS_UNCHANGED)
939 goto err_out;
940 }
941
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200942 if (drbd_get_capacity(device->this_bdev) != size ||
943 drbd_bm_capacity(device) != size) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700944 int err;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200945 err = drbd_bm_resize(device, size, !(flags & DDSF_NO_RESYNC));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700946 if (unlikely(err)) {
947 /* currently there is only one error: ENOMEM! */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200948 size = drbd_bm_capacity(device)>>1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700949 if (size == 0) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200950 drbd_err(device, "OUT OF MEMORY! "
Philipp Reisnerb411b362009-09-25 16:07:19 -0700951 "Could not allocate bitmap!\n");
952 } else {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200953 drbd_err(device, "BM resizing failed. "
Philipp Reisnerb411b362009-09-25 16:07:19 -0700954 "Leaving size unchanged at size = %lu KB\n",
955 (unsigned long)size);
956 }
Philipp Reisnere96c9632013-06-25 16:50:07 +0200957 rv = DS_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700958 }
959 /* racy, see comments above. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200960 drbd_set_my_capacity(device, size);
961 device->ldev->md.la_size_sect = size;
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200962 drbd_info(device, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
Philipp Reisnerb411b362009-09-25 16:07:19 -0700963 (unsigned long long)size>>1);
964 }
Philipp Reisnerd752b262013-06-25 16:50:08 +0200965 if (rv <= DS_ERROR)
966 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700967
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200968 la_size_changed = (la_size_sect != device->ldev->md.la_size_sect);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700969
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200970 md_moved = prev_first_sect != drbd_md_first_sector(device->ldev)
971 || prev_size != device->ldev->md.md_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700972
Philipp Reisnerd752b262013-06-25 16:50:08 +0200973 if (la_size_changed || md_moved || rs) {
974 u32 prev_flags;
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100975
Lars Ellenbergfcb09672014-01-27 16:04:14 +0100976 /* We do some synchronous IO below, which may take some time.
977 * Clear the timer, to avoid scary "timer expired!" messages,
978 * "Superblock" is written out at least twice below, anyways. */
979 del_timer(&device->md_sync_timer);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200980 drbd_al_shrink(device); /* All extents inactive. */
Philipp Reisnerd752b262013-06-25 16:50:08 +0200981
982 prev_flags = md->flags;
983 md->flags &= ~MDF_PRIMARY_IND;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200984 drbd_md_write(device, buffer);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200985
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200986 drbd_info(device, "Writing the whole bitmap, %s\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -0700987 la_size_changed && md_moved ? "size changed and md moved" :
988 la_size_changed ? "size changed" : "md moved");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100989 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200990 drbd_bitmap_io(device, md_moved ? &drbd_bm_write_all : &drbd_bm_write,
Philipp Reisnerd752b262013-06-25 16:50:08 +0200991 "size changed", BM_LOCKED_MASK);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200992 drbd_initialize_al(device, buffer);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200993
994 md->flags = prev_flags;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200995 drbd_md_write(device, buffer);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200996
997 if (rs)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200998 drbd_info(device, "Changed AL layout to al-stripes = %d, al-stripe-size-kB = %d\n",
999 md->al_stripes, md->al_stripe_size_4k * 4);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001000 }
1001
Lars Ellenbergcccac982013-03-19 18:16:46 +01001002 if (size > la_size_sect)
Philipp Reisner57737ad2013-10-23 10:59:17 +02001003 rv = la_size_sect ? DS_GREW : DS_GREW_FROM_ZERO;
Lars Ellenbergcccac982013-03-19 18:16:46 +01001004 if (size < la_size_sect)
Philipp Reisnere96c9632013-06-25 16:50:07 +02001005 rv = DS_SHRUNK;
Philipp Reisnerd752b262013-06-25 16:50:08 +02001006
1007 if (0) {
1008 err_out:
1009 if (rs) {
1010 md->al_stripes = prev_al_stripes;
1011 md->al_stripe_size_4k = prev_al_stripe_size_4k;
1012 md->al_size_4k = (u64)prev_al_stripes * prev_al_stripe_size_4k;
1013
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001014 drbd_md_set_sector_offsets(device, device->ldev);
Philipp Reisnerd752b262013-06-25 16:50:08 +02001015 }
1016 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001017 lc_unlock(device->act_log);
1018 wake_up(&device->al_wait);
1019 drbd_md_put_buffer(device);
1020 drbd_resume_io(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001021
1022 return rv;
1023}
1024
1025sector_t
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001026drbd_new_dev_size(struct drbd_device *device, struct drbd_backing_dev *bdev,
Philipp Reisneref5e44a2011-05-03 13:27:43 +02001027 sector_t u_size, int assume_peer_has_space)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001028{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001029 sector_t p_size = device->p_size; /* partner's disk size. */
Lars Ellenbergcccac982013-03-19 18:16:46 +01001030 sector_t la_size_sect = bdev->md.la_size_sect; /* last agreed size. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001031 sector_t m_size; /* my size */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001032 sector_t size = 0;
1033
1034 m_size = drbd_get_max_capacity(bdev);
1035
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001036 if (device->state.conn < C_CONNECTED && assume_peer_has_space) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001037 drbd_warn(device, "Resize while not connected was forced by the user!\n");
Philipp Reisnera393db62009-12-22 13:35:52 +01001038 p_size = m_size;
1039 }
1040
Philipp Reisnerb411b362009-09-25 16:07:19 -07001041 if (p_size && m_size) {
1042 size = min_t(sector_t, p_size, m_size);
1043 } else {
Lars Ellenbergcccac982013-03-19 18:16:46 +01001044 if (la_size_sect) {
1045 size = la_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001046 if (m_size && m_size < size)
1047 size = m_size;
1048 if (p_size && p_size < size)
1049 size = p_size;
1050 } else {
1051 if (m_size)
1052 size = m_size;
1053 if (p_size)
1054 size = p_size;
1055 }
1056 }
1057
1058 if (size == 0)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001059 drbd_err(device, "Both nodes diskless!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001060
1061 if (u_size) {
1062 if (u_size > size)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001063 drbd_err(device, "Requested disk size is too big (%lu > %lu)\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001064 (unsigned long)u_size>>1, (unsigned long)size>>1);
1065 else
1066 size = u_size;
1067 }
1068
1069 return size;
1070}
1071
1072/**
1073 * drbd_check_al_size() - Ensures that the AL is of the right size
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001074 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001075 *
1076 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
1077 * failed, and 0 on success. You should call drbd_md_sync() after you called
1078 * this function.
1079 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001080static int drbd_check_al_size(struct drbd_device *device, struct disk_conf *dc)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001081{
1082 struct lru_cache *n, *t;
1083 struct lc_element *e;
1084 unsigned int in_use;
1085 int i;
1086
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001087 if (device->act_log &&
1088 device->act_log->nr_elements == dc->al_extents)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001089 return 0;
1090
1091 in_use = 0;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001092 t = device->act_log;
Lars Ellenberg7ad651b2011-02-21 13:21:03 +01001093 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
Lars Ellenbergf3990022011-03-23 14:31:09 +01001094 dc->al_extents, sizeof(struct lc_element), 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001095
1096 if (n == NULL) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001097 drbd_err(device, "Cannot allocate act_log lru!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001098 return -ENOMEM;
1099 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001100 spin_lock_irq(&device->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001101 if (t) {
1102 for (i = 0; i < t->nr_elements; i++) {
1103 e = lc_element_by_index(t, i);
1104 if (e->refcnt)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001105 drbd_err(device, "refcnt(%d)==%d\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001106 e->lc_number, e->refcnt);
1107 in_use += e->refcnt;
1108 }
1109 }
1110 if (!in_use)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001111 device->act_log = n;
1112 spin_unlock_irq(&device->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001113 if (in_use) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001114 drbd_err(device, "Activity log still in use!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001115 lc_destroy(n);
1116 return -EBUSY;
1117 } else {
1118 if (t)
1119 lc_destroy(t);
1120 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001121 drbd_md_mark_dirty(device); /* we changed device->act_log->nr_elemens */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001122 return 0;
1123}
1124
Philipp Reisner8fe39aa2013-11-22 13:22:13 +01001125static void drbd_setup_queue_param(struct drbd_device *device, struct drbd_backing_dev *bdev,
1126 unsigned int max_bio_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001127{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001128 struct request_queue * const q = device->rq_queue;
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001129 unsigned int max_hw_sectors = max_bio_size >> 9;
1130 unsigned int max_segments = 0;
Philipp Reisnerc1b31562014-04-28 18:43:32 +02001131 struct request_queue *b = NULL;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001132
Philipp Reisner8fe39aa2013-11-22 13:22:13 +01001133 if (bdev) {
1134 b = bdev->backing_bdev->bd_disk->queue;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001135
1136 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001137 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001138 max_segments = rcu_dereference(device->ldev->disk_conf)->max_bio_bvecs;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001139 rcu_read_unlock();
Philipp Reisnerc1b31562014-04-28 18:43:32 +02001140
1141 blk_set_stacking_limits(&q->limits);
1142 blk_queue_max_write_same_sectors(q, 0);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001143 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001144
Philipp Reisnerb411b362009-09-25 16:07:19 -07001145 blk_queue_logical_block_size(q, 512);
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001146 blk_queue_max_hw_sectors(q, max_hw_sectors);
1147 /* This is the workaround for "bio would need to, but cannot, be split" */
1148 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
1149 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001150
Philipp Reisnerc1b31562014-04-28 18:43:32 +02001151 if (b) {
Lars Ellenberg20c68fd2014-04-28 18:43:25 +02001152 struct drbd_connection *connection = first_peer_device(device)->connection;
1153
1154 if (blk_queue_discard(b) &&
1155 (connection->cstate < C_CONNECTED || connection->agreed_features & FF_TRIM)) {
Lars Ellenberg20c68fd2014-04-28 18:43:25 +02001156 /* For now, don't allow more than one activity log extent worth of data
1157 * to be discarded in one go. We may need to rework drbd_al_begin_io()
1158 * to allow for even larger discard ranges */
1159 q->limits.max_discard_sectors = DRBD_MAX_DISCARD_SECTORS;
1160
1161 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
1162 /* REALLY? Is stacking secdiscard "legal"? */
1163 if (blk_queue_secdiscard(b))
1164 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q);
1165 } else {
1166 q->limits.max_discard_sectors = 0;
1167 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
1168 queue_flag_clear_unlocked(QUEUE_FLAG_SECDISCARD, q);
1169 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001170
Philipp Reisner99432fc2011-05-20 16:39:13 +02001171 blk_queue_stack_limits(q, b);
1172
1173 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001174 drbd_info(device, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
Philipp Reisner99432fc2011-05-20 16:39:13 +02001175 q->backing_dev_info.ra_pages,
1176 b->backing_dev_info.ra_pages);
1177 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1178 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001179 }
1180}
1181
Philipp Reisner8fe39aa2013-11-22 13:22:13 +01001182void drbd_reconsider_max_bio_size(struct drbd_device *device, struct drbd_backing_dev *bdev)
Philipp Reisner99432fc2011-05-20 16:39:13 +02001183{
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001184 unsigned int now, new, local, peer;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001185
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001186 now = queue_max_hw_sectors(device->rq_queue) << 9;
1187 local = device->local_max_bio_size; /* Eventually last known value, from volatile memory */
1188 peer = device->peer_max_bio_size; /* Eventually last known value, from meta data */
Philipp Reisner99432fc2011-05-20 16:39:13 +02001189
Philipp Reisner8fe39aa2013-11-22 13:22:13 +01001190 if (bdev) {
1191 local = queue_max_hw_sectors(bdev->backing_bdev->bd_disk->queue) << 9;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001192 device->local_max_bio_size = local;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001193 }
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001194 local = min(local, DRBD_MAX_BIO_SIZE);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001195
1196 /* We may ignore peer limits if the peer is modern enough.
1197 Because new from 8.3.8 onwards the peer can use multiple
1198 BIOs for a single peer_request */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001199 if (device->state.conn >= C_WF_REPORT_PARAMS) {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001200 if (first_peer_device(device)->connection->agreed_pro_version < 94)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001201 peer = min(device->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
Philipp Reisner68093842011-06-30 15:43:06 +02001202 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001203 else if (first_peer_device(device)->connection->agreed_pro_version == 94)
Philipp Reisner99432fc2011-05-20 16:39:13 +02001204 peer = DRBD_MAX_SIZE_H80_PACKET;
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001205 else if (first_peer_device(device)->connection->agreed_pro_version < 100)
Philipp Reisner2ffca4f2011-06-30 15:43:06 +02001206 peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
1207 else
Philipp Reisner99432fc2011-05-20 16:39:13 +02001208 peer = DRBD_MAX_BIO_SIZE;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001209
Lars Ellenbergfa090e72014-04-28 18:43:27 +02001210 /* We may later detach and re-attach on a disconnected Primary.
1211 * Avoid this setting to jump back in that case.
1212 * We want to store what we know the peer DRBD can handle,
1213 * not what the peer IO backend can handle. */
1214 if (peer > device->peer_max_bio_size)
1215 device->peer_max_bio_size = peer;
1216 }
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001217 new = min(local, peer);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001218
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001219 if (device->state.role == R_PRIMARY && new < now)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001220 drbd_err(device, "ASSERT FAILED new < now; (%u < %u)\n", new, now);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001221
1222 if (new != now)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001223 drbd_info(device, "max BIO size = %u\n", new);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001224
Philipp Reisner8fe39aa2013-11-22 13:22:13 +01001225 drbd_setup_queue_param(device, bdev, new);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001226}
1227
Philipp Reisnera18e9d12011-04-24 11:09:55 +02001228/* Starts the worker thread */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001229static void conn_reconfig_start(struct drbd_connection *connection)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001230{
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001231 drbd_thread_start(&connection->worker);
Andreas Gruenbacherb5043c52011-07-28 15:56:02 +02001232 drbd_flush_workqueue(&connection->sender_work);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001233}
1234
Philipp Reisnera18e9d12011-04-24 11:09:55 +02001235/* if still unconfigured, stops worker again. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001236static void conn_reconfig_done(struct drbd_connection *connection)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001237{
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001238 bool stop_threads;
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001239 spin_lock_irq(&connection->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001240 stop_threads = conn_all_vols_unconf(connection) &&
1241 connection->cstate == C_STANDALONE;
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001242 spin_unlock_irq(&connection->resource->req_lock);
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001243 if (stop_threads) {
1244 /* asender is implicitly stopped by receiver
Philipp Reisner81fa2e62011-05-04 15:10:30 +02001245 * in conn_disconnect() */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001246 drbd_thread_stop(&connection->receiver);
1247 drbd_thread_stop(&connection->worker);
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001248 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001249}
1250
Philipp Reisner07782862010-08-31 12:00:50 +02001251/* Make sure IO is suspended before calling this function(). */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001252static void drbd_suspend_al(struct drbd_device *device)
Philipp Reisner07782862010-08-31 12:00:50 +02001253{
1254 int s = 0;
1255
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001256 if (!lc_try_lock(device->act_log)) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001257 drbd_warn(device, "Failed to lock al in drbd_suspend_al()\n");
Philipp Reisner07782862010-08-31 12:00:50 +02001258 return;
1259 }
1260
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001261 drbd_al_shrink(device);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001262 spin_lock_irq(&device->resource->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001263 if (device->state.conn < C_CONNECTED)
1264 s = !test_and_set_bit(AL_SUSPENDED, &device->flags);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001265 spin_unlock_irq(&device->resource->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001266 lc_unlock(device->act_log);
Philipp Reisner07782862010-08-31 12:00:50 +02001267
1268 if (s)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001269 drbd_info(device, "Suspended AL updates\n");
Philipp Reisner07782862010-08-31 12:00:50 +02001270}
1271
Lars Ellenberg5979e362011-04-27 21:09:55 +02001272
1273static bool should_set_defaults(struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001274{
Lars Ellenberg5979e362011-04-27 21:09:55 +02001275 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1276 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1277}
1278
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001279static unsigned int drbd_al_extents_max(struct drbd_backing_dev *bdev)
Philipp Reisnerd589a212011-05-04 10:06:52 +02001280{
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001281 /* This is limited by 16 bit "slot" numbers,
1282 * and by available on-disk context storage.
1283 *
1284 * Also (u16)~0 is special (denotes a "free" extent).
1285 *
1286 * One transaction occupies one 4kB on-disk block,
1287 * we have n such blocks in the on disk ring buffer,
1288 * the "current" transaction may fail (n-1),
1289 * and there is 919 slot numbers context information per transaction.
1290 *
1291 * 72 transaction blocks amounts to more than 2**16 context slots,
1292 * so cap there first.
1293 */
1294 const unsigned int max_al_nr = DRBD_AL_EXTENTS_MAX;
1295 const unsigned int sufficient_on_disk =
1296 (max_al_nr + AL_CONTEXT_PER_TRANSACTION -1)
1297 /AL_CONTEXT_PER_TRANSACTION;
Philipp Reisnerd589a212011-05-04 10:06:52 +02001298
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001299 unsigned int al_size_4k = bdev->md.al_size_4k;
1300
1301 if (al_size_4k > sufficient_on_disk)
1302 return max_al_nr;
1303
1304 return (al_size_4k - 1) * AL_CONTEXT_PER_TRANSACTION;
Philipp Reisnerd589a212011-05-04 10:06:52 +02001305}
1306
Lars Ellenberg70df7092013-12-20 11:17:02 +01001307static bool write_ordering_changed(struct disk_conf *a, struct disk_conf *b)
1308{
1309 return a->disk_barrier != b->disk_barrier ||
1310 a->disk_flushes != b->disk_flushes ||
1311 a->disk_drain != b->disk_drain;
1312}
1313
Lars Ellenbergf3990022011-03-23 14:31:09 +01001314int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1315{
Lars Ellenberga910b122014-04-28 18:43:21 +02001316 struct drbd_config_context adm_ctx;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001317 enum drbd_ret_code retcode;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001318 struct drbd_device *device;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001319 struct disk_conf *new_disk_conf, *old_disk_conf;
Philipp Reisner813472c2011-05-03 16:47:02 +02001320 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001321 int err, fifo_size;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001322
Lars Ellenberga910b122014-04-28 18:43:21 +02001323 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001324 if (!adm_ctx.reply_skb)
1325 return retcode;
1326 if (retcode != NO_ERROR)
Lars Ellenberg9e276872014-04-28 18:43:22 +02001327 goto finish;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001328
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001329 device = adm_ctx.device;
Lars Ellenberg9e276872014-04-28 18:43:22 +02001330 mutex_lock(&adm_ctx.resource->adm_mutex);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001331
1332 /* we also need a disk
1333 * to change the options on */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001334 if (!get_ldev(device)) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001335 retcode = ERR_NO_DISK;
1336 goto out;
1337 }
1338
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001339 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001340 if (!new_disk_conf) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001341 retcode = ERR_NOMEM;
1342 goto fail;
1343 }
1344
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001345 mutex_lock(&device->resource->conf_update);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001346 old_disk_conf = device->ldev->disk_conf;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001347 *new_disk_conf = *old_disk_conf;
Lars Ellenberg5979e362011-04-27 21:09:55 +02001348 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02001349 set_disk_conf_defaults(new_disk_conf);
Lars Ellenberg5979e362011-04-27 21:09:55 +02001350
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001351 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02001352 if (err && err != -ENOMSG) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001353 retcode = ERR_MANDATORY_TAG;
Lars Ellenberga910b122014-04-28 18:43:21 +02001354 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
Philipp Reisner8e229432013-08-01 10:21:47 +02001355 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001356 }
1357
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001358 if (!expect(new_disk_conf->resync_rate >= 1))
1359 new_disk_conf->resync_rate = 1;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001360
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001361 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1362 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001363 if (new_disk_conf->al_extents > drbd_al_extents_max(device->ldev))
1364 new_disk_conf->al_extents = drbd_al_extents_max(device->ldev);
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001365
1366 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1367 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001368
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001369 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001370 if (fifo_size != device->rs_plan_s->size) {
Philipp Reisner813472c2011-05-03 16:47:02 +02001371 new_plan = fifo_alloc(fifo_size);
1372 if (!new_plan) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001373 drbd_err(device, "kmalloc of fifo_buffer failed");
Lars Ellenbergf3990022011-03-23 14:31:09 +01001374 retcode = ERR_NOMEM;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001375 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001376 }
1377 }
1378
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001379 drbd_suspend_io(device);
1380 wait_event(device->al_wait, lc_try_lock(device->act_log));
1381 drbd_al_shrink(device);
1382 err = drbd_check_al_size(device, new_disk_conf);
1383 lc_unlock(device->act_log);
1384 wake_up(&device->al_wait);
1385 drbd_resume_io(device);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001386
1387 if (err) {
1388 retcode = ERR_NOMEM;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001389 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001390 }
1391
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001392 write_lock_irq(&global_state_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001393 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001394 if (retcode == NO_ERROR) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001395 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
1396 drbd_resync_after_changed(device);
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001397 }
1398 write_unlock_irq(&global_state_lock);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001399
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001400 if (retcode != NO_ERROR)
1401 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001402
Philipp Reisner813472c2011-05-03 16:47:02 +02001403 if (new_plan) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001404 old_plan = device->rs_plan_s;
1405 rcu_assign_pointer(device->rs_plan_s, new_plan);
Philipp Reisner9958c852011-05-03 16:19:31 +02001406 }
Philipp Reisner9958c852011-05-03 16:19:31 +02001407
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001408 mutex_unlock(&device->resource->conf_update);
Philipp Reisner27eb13e2012-03-30 14:12:15 +02001409
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001410 if (new_disk_conf->al_updates)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001411 device->ldev->md.flags &= ~MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001412 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001413 device->ldev->md.flags |= MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001414
Lars Ellenberg691631c2012-10-26 00:41:50 +02001415 if (new_disk_conf->md_flushes)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001416 clear_bit(MD_NO_FUA, &device->flags);
Lars Ellenberg691631c2012-10-26 00:41:50 +02001417 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001418 set_bit(MD_NO_FUA, &device->flags);
Lars Ellenberg691631c2012-10-26 00:41:50 +02001419
Lars Ellenberg70df7092013-12-20 11:17:02 +01001420 if (write_ordering_changed(old_disk_conf, new_disk_conf))
1421 drbd_bump_write_ordering(device->resource, NULL, WO_bdev_flush);
Philipp Reisner27eb13e2012-03-30 14:12:15 +02001422
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001423 drbd_md_sync(device);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001424
Andreas Gruenbacher69a22772011-08-09 00:47:13 +02001425 if (device->state.conn >= C_CONNECTED) {
1426 struct drbd_peer_device *peer_device;
1427
1428 for_each_peer_device(peer_device, device)
1429 drbd_send_sync_param(peer_device);
1430 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001431
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001432 synchronize_rcu();
1433 kfree(old_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02001434 kfree(old_plan);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001435 mod_timer(&device->request_timer, jiffies + HZ);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001436 goto success;
1437
1438fail_unlock:
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001439 mutex_unlock(&device->resource->conf_update);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001440 fail:
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001441 kfree(new_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02001442 kfree(new_plan);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001443success:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001444 put_ldev(device);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001445 out:
Lars Ellenberg9e276872014-04-28 18:43:22 +02001446 mutex_unlock(&adm_ctx.resource->adm_mutex);
1447 finish:
Lars Ellenberga910b122014-04-28 18:43:21 +02001448 drbd_adm_finish(&adm_ctx, info, retcode);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001449 return 0;
1450}
1451
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001452int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001453{
Lars Ellenberga910b122014-04-28 18:43:21 +02001454 struct drbd_config_context adm_ctx;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001455 struct drbd_device *device;
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001456 struct drbd_peer_device *peer_device;
1457 struct drbd_connection *connection;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001458 int err;
Andreas Gruenbacher116676c2010-12-08 13:33:11 +01001459 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001460 enum determine_dev_size dd;
1461 sector_t max_possible_sectors;
1462 sector_t min_md_device_sectors;
1463 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001464 struct disk_conf *new_disk_conf = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001465 struct block_device *bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001466 struct lru_cache *resync_lru = NULL;
Philipp Reisner9958c852011-05-03 16:19:31 +02001467 struct fifo_buffer *new_plan = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001468 union drbd_state ns, os;
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001469 enum drbd_state_rv rv;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001470 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001471
Lars Ellenberga910b122014-04-28 18:43:21 +02001472 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001473 if (!adm_ctx.reply_skb)
1474 return retcode;
1475 if (retcode != NO_ERROR)
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001476 goto finish;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001477
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001478 device = adm_ctx.device;
Lars Ellenberg9e276872014-04-28 18:43:22 +02001479 mutex_lock(&adm_ctx.resource->adm_mutex);
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001480 peer_device = first_peer_device(device);
1481 connection = peer_device ? peer_device->connection : NULL;
1482 conn_reconfig_start(connection);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001483
1484 /* if you want to reconfigure, please tear down first */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001485 if (device->state.disk > D_DISKLESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001486 retcode = ERR_DISK_CONFIGURED;
1487 goto fail;
1488 }
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001489 /* It may just now have detached because of IO error. Make sure
1490 * drbd_ldev_destroy is done already, we may end up here very fast,
1491 * e.g. if someone calls attach from the on-io-error handler,
1492 * to realize a "hot spare" feature (not that I'd recommend that) */
Lars Ellenberge334f552014-02-11 09:30:49 +01001493 wait_event(device->misc_wait, !test_bit(GOING_DISKLESS, &device->flags));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001494
Lars Ellenberg383606e2012-06-14 14:21:32 +02001495 /* make sure there is no leftover from previous force-detach attempts */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001496 clear_bit(FORCE_DETACH, &device->flags);
1497 clear_bit(WAS_IO_ERROR, &device->flags);
1498 clear_bit(WAS_READ_ERROR, &device->flags);
Lars Ellenberg383606e2012-06-14 14:21:32 +02001499
Lars Ellenberg0029d622012-06-14 18:02:52 +02001500 /* and no leftover from previously aborted resync or verify, either */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001501 device->rs_total = 0;
1502 device->rs_failed = 0;
1503 atomic_set(&device->rs_pending_cnt, 0);
Lars Ellenberg0029d622012-06-14 18:02:52 +02001504
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001505 /* allocation not in the IO path, drbdsetup context */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001506 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1507 if (!nbc) {
1508 retcode = ERR_NOMEM;
1509 goto fail;
1510 }
Philipp Reisner9f2247b2012-08-16 14:25:58 +02001511 spin_lock_init(&nbc->md.uuid_lock);
1512
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001513 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
1514 if (!new_disk_conf) {
1515 retcode = ERR_NOMEM;
1516 goto fail;
1517 }
1518 nbc->disk_conf = new_disk_conf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001519
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001520 set_disk_conf_defaults(new_disk_conf);
1521 err = disk_conf_from_attrs(new_disk_conf, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001522 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001523 retcode = ERR_MANDATORY_TAG;
Lars Ellenberga910b122014-04-28 18:43:21 +02001524 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001525 goto fail;
1526 }
1527
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001528 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1529 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
Philipp Reisnerd589a212011-05-04 10:06:52 +02001530
Philipp Reisner9958c852011-05-03 16:19:31 +02001531 new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
1532 if (!new_plan) {
1533 retcode = ERR_NOMEM;
1534 goto fail;
1535 }
1536
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001537 if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001538 retcode = ERR_MD_IDX_INVALID;
1539 goto fail;
1540 }
1541
Lars Ellenberga3f8f7d2013-03-27 14:08:43 +01001542 write_lock_irq(&global_state_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001543 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
Lars Ellenberga3f8f7d2013-03-27 14:08:43 +01001544 write_unlock_irq(&global_state_lock);
1545 if (retcode != NO_ERROR)
1546 goto fail;
1547
Philipp Reisner44ed1672011-04-19 17:10:19 +02001548 rcu_read_lock();
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001549 nc = rcu_dereference(connection->net_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001550 if (nc) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001551 if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02001552 rcu_read_unlock();
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001553 retcode = ERR_STONITH_AND_PROT_A;
1554 goto fail;
1555 }
1556 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02001557 rcu_read_unlock();
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001558
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001559 bdev = blkdev_get_by_path(new_disk_conf->backing_dev,
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001560 FMODE_READ | FMODE_WRITE | FMODE_EXCL, device);
Tejun Heoe525fd82010-11-13 11:55:17 +01001561 if (IS_ERR(bdev)) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001562 drbd_err(device, "open(\"%s\") failed with %ld\n", new_disk_conf->backing_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001563 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001564 retcode = ERR_OPEN_DISK;
1565 goto fail;
1566 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001567 nbc->backing_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001568
Tejun Heoe525fd82010-11-13 11:55:17 +01001569 /*
1570 * meta_dev_idx >= 0: external fixed size, possibly multiple
1571 * drbd sharing one meta device. TODO in that case, paranoia
1572 * check that [md_bdev, meta_dev_idx] is not yet used by some
1573 * other drbd minor! (if you use drbd.conf + drbdadm, that
1574 * should check it for you already; but if you don't, or
1575 * someone fooled it, we need to double check here)
1576 */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001577 bdev = blkdev_get_by_path(new_disk_conf->meta_dev,
Tejun Heod4d77622010-11-13 11:55:18 +01001578 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001579 (new_disk_conf->meta_dev_idx < 0) ?
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001580 (void *)device : (void *)drbd_m_holder);
Tejun Heoe525fd82010-11-13 11:55:17 +01001581 if (IS_ERR(bdev)) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001582 drbd_err(device, "open(\"%s\") failed with %ld\n", new_disk_conf->meta_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001583 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001584 retcode = ERR_OPEN_MD_DISK;
1585 goto fail;
1586 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001587 nbc->md_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001588
Tejun Heoe525fd82010-11-13 11:55:17 +01001589 if ((nbc->backing_bdev == nbc->md_bdev) !=
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001590 (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1591 new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001592 retcode = ERR_MD_IDX_INVALID;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001593 goto fail;
1594 }
1595
1596 resync_lru = lc_create("resync", drbd_bm_ext_cache,
Lars Ellenberg46a15bc2011-02-21 13:21:01 +01001597 1, 61, sizeof(struct bm_extent),
Philipp Reisnerb411b362009-09-25 16:07:19 -07001598 offsetof(struct bm_extent, lce));
1599 if (!resync_lru) {
1600 retcode = ERR_NOMEM;
Tejun Heoe525fd82010-11-13 11:55:17 +01001601 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001602 }
1603
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +01001604 /* Read our meta data super block early.
1605 * This also sets other on-disk offsets. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001606 retcode = drbd_md_read(device, nbc);
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +01001607 if (retcode != NO_ERROR)
1608 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001609
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001610 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1611 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
1612 if (new_disk_conf->al_extents > drbd_al_extents_max(nbc))
1613 new_disk_conf->al_extents = drbd_al_extents_max(nbc);
1614
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001615 if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001616 drbd_err(device, "max capacity %llu smaller than disk size %llu\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001617 (unsigned long long) drbd_get_max_capacity(nbc),
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001618 (unsigned long long) new_disk_conf->disk_size);
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001619 retcode = ERR_DISK_TOO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001620 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001621 }
1622
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001623 if (new_disk_conf->meta_dev_idx < 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001624 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1625 /* at least one MB, otherwise it does not make sense */
1626 min_md_device_sectors = (2<<10);
1627 } else {
1628 max_possible_sectors = DRBD_MAX_SECTORS;
Lars Ellenbergae8bf312013-03-19 18:16:43 +01001629 min_md_device_sectors = MD_128MB_SECT * (new_disk_conf->meta_dev_idx + 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001630 }
1631
Philipp Reisnerb411b362009-09-25 16:07:19 -07001632 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001633 retcode = ERR_MD_DISK_TOO_SMALL;
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001634 drbd_warn(device, "refusing attach: md-device too small, "
Philipp Reisnerb411b362009-09-25 16:07:19 -07001635 "at least %llu sectors needed for this meta-disk type\n",
1636 (unsigned long long) min_md_device_sectors);
Tejun Heoe525fd82010-11-13 11:55:17 +01001637 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001638 }
1639
1640 /* Make sure the new disk is big enough
1641 * (we may currently be R_PRIMARY with no local disk...) */
1642 if (drbd_get_max_capacity(nbc) <
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001643 drbd_get_capacity(device->this_bdev)) {
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001644 retcode = ERR_DISK_TOO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001645 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001646 }
1647
1648 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1649
Lars Ellenberg13529942009-10-12 19:07:49 +02001650 if (nbc->known_size > max_possible_sectors) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001651 drbd_warn(device, "==> truncating very big lower level device "
Lars Ellenberg13529942009-10-12 19:07:49 +02001652 "to currently maximum possible %llu sectors <==\n",
1653 (unsigned long long) max_possible_sectors);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001654 if (new_disk_conf->meta_dev_idx >= 0)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001655 drbd_warn(device, "==>> using internal or flexible "
Lars Ellenberg13529942009-10-12 19:07:49 +02001656 "meta data may help <<==\n");
1657 }
1658
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001659 drbd_suspend_io(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001660 /* also wait for the last barrier ack. */
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001661 /* FIXME see also https://daiquiri.linbit/cgi-bin/bugzilla/show_bug.cgi?id=171
1662 * We need a way to either ignore barrier acks for barriers sent before a device
1663 * was attached, or a way to wait for all pending barrier acks to come in.
1664 * As barriers are counted per resource,
1665 * we'd need to suspend io on all devices of a resource.
1666 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001667 wait_event(device->misc_wait, !atomic_read(&device->ap_pending_cnt) || drbd_suspended(device));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001668 /* and for any other previously queued work */
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001669 drbd_flush_workqueue(&connection->sender_work);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001670
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001671 rv = _drbd_request_state(device, NS(disk, D_ATTACHING), CS_VERBOSE);
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001672 retcode = rv; /* FIXME: Type mismatch. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001673 drbd_resume_io(device);
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001674 if (rv < SS_SUCCESS)
Tejun Heoe525fd82010-11-13 11:55:17 +01001675 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001676
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001677 if (!get_ldev_if_state(device, D_ATTACHING))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001678 goto force_diskless;
1679
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001680 if (!device->bitmap) {
1681 if (drbd_bm_init(device)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001682 retcode = ERR_NOMEM;
1683 goto force_diskless_dec;
1684 }
1685 }
1686
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001687 if (device->state.conn < C_CONNECTED &&
Philipp Reisnerbabea492014-04-28 18:43:34 +02001688 device->state.role == R_PRIMARY && device->ed_uuid &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001689 (device->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001690 drbd_err(device, "Can only attach to data with current UUID=%016llX\n",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001691 (unsigned long long)device->ed_uuid);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001692 retcode = ERR_DATA_NOT_CURRENT;
1693 goto force_diskless_dec;
1694 }
1695
1696 /* Since we are diskless, fix the activity log first... */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001697 if (drbd_check_al_size(device, new_disk_conf)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001698 retcode = ERR_NOMEM;
1699 goto force_diskless_dec;
1700 }
1701
1702 /* Prevent shrinking of consistent devices ! */
1703 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001704 drbd_new_dev_size(device, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001705 drbd_warn(device, "refusing to truncate a consistent device\n");
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001706 retcode = ERR_DISK_TOO_SMALL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001707 goto force_diskless_dec;
1708 }
1709
Philipp Reisnerb411b362009-09-25 16:07:19 -07001710 /* Reset the "barriers don't work" bits here, then force meta data to
1711 * be written, to ensure we determine if barriers are supported. */
Andreas Gruenbachere5440462011-05-04 15:25:35 +02001712 if (new_disk_conf->md_flushes)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001713 clear_bit(MD_NO_FUA, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001714 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001715 set_bit(MD_NO_FUA, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001716
1717 /* Point of no return reached.
1718 * Devices and memory are no longer released by error cleanup below.
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001719 * now device takes over responsibility, and the state engine should
Philipp Reisnerb411b362009-09-25 16:07:19 -07001720 * clean it up somewhere. */
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +02001721 D_ASSERT(device, device->ldev == NULL);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001722 device->ldev = nbc;
1723 device->resync = resync_lru;
1724 device->rs_plan_s = new_plan;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001725 nbc = NULL;
1726 resync_lru = NULL;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001727 new_disk_conf = NULL;
Philipp Reisner9958c852011-05-03 16:19:31 +02001728 new_plan = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001729
Philipp Reisner8fe39aa2013-11-22 13:22:13 +01001730 drbd_bump_write_ordering(device->resource, device->ldev, WO_bdev_flush);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001731
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001732 if (drbd_md_test_flag(device->ldev, MDF_CRASHED_PRIMARY))
1733 set_bit(CRASHED_PRIMARY, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001734 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001735 clear_bit(CRASHED_PRIMARY, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001736
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001737 if (drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
Andreas Gruenbacher6bbf53c2011-07-08 01:19:44 +02001738 !(device->state.role == R_PRIMARY && device->resource->susp_nod))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001739 set_bit(CRASHED_PRIMARY, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001740
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001741 device->send_cnt = 0;
1742 device->recv_cnt = 0;
1743 device->read_cnt = 0;
1744 device->writ_cnt = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001745
Philipp Reisner8fe39aa2013-11-22 13:22:13 +01001746 drbd_reconsider_max_bio_size(device, device->ldev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001747
1748 /* If I am currently not R_PRIMARY,
1749 * but meta data primary indicator is set,
1750 * I just now recover from a hard crash,
1751 * and have been R_PRIMARY before that crash.
1752 *
1753 * Now, if I had no connection before that crash
1754 * (have been degraded R_PRIMARY), chances are that
1755 * I won't find my peer now either.
1756 *
1757 * In that case, and _only_ in that case,
1758 * we use the degr-wfc-timeout instead of the default,
1759 * so we can automatically recover from a crash of a
1760 * degraded but active "cluster" after a certain timeout.
1761 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001762 clear_bit(USE_DEGR_WFC_T, &device->flags);
1763 if (device->state.role != R_PRIMARY &&
1764 drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
1765 !drbd_md_test_flag(device->ldev, MDF_CONNECTED_IND))
1766 set_bit(USE_DEGR_WFC_T, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001767
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001768 dd = drbd_determine_dev_size(device, 0, NULL);
Philipp Reisnerd752b262013-06-25 16:50:08 +02001769 if (dd <= DS_ERROR) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001770 retcode = ERR_NOMEM_BITMAP;
1771 goto force_diskless_dec;
Philipp Reisnere96c9632013-06-25 16:50:07 +02001772 } else if (dd == DS_GREW)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001773 set_bit(RESYNC_AFTER_NEG, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001774
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001775 if (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC) ||
1776 (test_bit(CRASHED_PRIMARY, &device->flags) &&
1777 drbd_md_test_flag(device->ldev, MDF_AL_DISABLED))) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001778 drbd_info(device, "Assuming that all blocks are out of sync "
Philipp Reisnerb411b362009-09-25 16:07:19 -07001779 "(aka FullSync)\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001780 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001781 "set_n_write from attaching", BM_LOCKED_MASK)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001782 retcode = ERR_IO_MD_DISK;
1783 goto force_diskless_dec;
1784 }
1785 } else {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001786 if (drbd_bitmap_io(device, &drbd_bm_read,
Andreas Gruenbacher22ab6a32010-12-13 01:44:11 +01001787 "read from attaching", BM_LOCKED_MASK)) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001788 retcode = ERR_IO_MD_DISK;
1789 goto force_diskless_dec;
1790 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001791 }
1792
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001793 if (_drbd_bm_total_weight(device) == drbd_bm_bits(device))
1794 drbd_suspend_al(device); /* IO is still suspended here... */
Philipp Reisner07782862010-08-31 12:00:50 +02001795
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001796 spin_lock_irq(&device->resource->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001797 os = drbd_read_state(device);
Philipp Reisner78bae592011-03-28 15:40:12 +02001798 ns = os;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001799 /* If MDF_CONSISTENT is not set go into inconsistent state,
1800 otherwise investigate MDF_WasUpToDate...
1801 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1802 otherwise into D_CONSISTENT state.
1803 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001804 if (drbd_md_test_flag(device->ldev, MDF_CONSISTENT)) {
1805 if (drbd_md_test_flag(device->ldev, MDF_WAS_UP_TO_DATE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001806 ns.disk = D_CONSISTENT;
1807 else
1808 ns.disk = D_OUTDATED;
1809 } else {
1810 ns.disk = D_INCONSISTENT;
1811 }
1812
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001813 if (drbd_md_test_flag(device->ldev, MDF_PEER_OUT_DATED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001814 ns.pdsk = D_OUTDATED;
1815
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001816 rcu_read_lock();
1817 if (ns.disk == D_CONSISTENT &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001818 (ns.pdsk == D_OUTDATED || rcu_dereference(device->ldev->disk_conf)->fencing == FP_DONT_CARE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001819 ns.disk = D_UP_TO_DATE;
1820
1821 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1822 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1823 this point, because drbd_request_state() modifies these
1824 flags. */
1825
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001826 if (rcu_dereference(device->ldev->disk_conf)->al_updates)
1827 device->ldev->md.flags &= ~MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001828 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001829 device->ldev->md.flags |= MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001830
1831 rcu_read_unlock();
1832
Philipp Reisnerb411b362009-09-25 16:07:19 -07001833 /* In case we are C_CONNECTED postpone any decision on the new disk
1834 state after the negotiation phase. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001835 if (device->state.conn == C_CONNECTED) {
1836 device->new_state_tmp.i = ns.i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001837 ns.i = os.i;
1838 ns.disk = D_NEGOTIATING;
Philipp Reisnerdc66c742010-06-02 14:31:29 +02001839
1840 /* We expect to receive up-to-date UUIDs soon.
1841 To avoid a race in receive_state, free p_uuid while
1842 holding req_lock. I.e. atomic with the state change */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001843 kfree(device->p_uuid);
1844 device->p_uuid = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001845 }
1846
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001847 rv = _drbd_set_state(device, ns, CS_VERBOSE, NULL);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001848 spin_unlock_irq(&device->resource->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001849
1850 if (rv < SS_SUCCESS)
1851 goto force_diskless_dec;
1852
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001853 mod_timer(&device->request_timer, jiffies + HZ);
Philipp Reisnercdfda632011-07-05 15:38:59 +02001854
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001855 if (device->state.role == R_PRIMARY)
1856 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001857 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001858 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001859
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001860 drbd_md_mark_dirty(device);
1861 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001862
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001863 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
1864 put_ldev(device);
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001865 conn_reconfig_done(connection);
Lars Ellenberg9e276872014-04-28 18:43:22 +02001866 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberga910b122014-04-28 18:43:21 +02001867 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001868 return 0;
1869
1870 force_diskless_dec:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001871 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001872 force_diskless:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001873 drbd_force_state(device, NS(disk, D_DISKLESS));
1874 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001875 fail:
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001876 conn_reconfig_done(connection);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001877 if (nbc) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001878 if (nbc->backing_bdev)
1879 blkdev_put(nbc->backing_bdev,
1880 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1881 if (nbc->md_bdev)
1882 blkdev_put(nbc->md_bdev,
1883 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001884 kfree(nbc);
1885 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001886 kfree(new_disk_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001887 lc_destroy(resync_lru);
Philipp Reisner9958c852011-05-03 16:19:31 +02001888 kfree(new_plan);
Lars Ellenberg9e276872014-04-28 18:43:22 +02001889 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001890 finish:
Lars Ellenberga910b122014-04-28 18:43:21 +02001891 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001892 return 0;
1893}
1894
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001895static int adm_detach(struct drbd_device *device, int force)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001896{
Philipp Reisner19f83c72011-03-29 14:21:03 +02001897 enum drbd_state_rv retcode;
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001898 int ret;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001899
Philipp Reisnercdfda632011-07-05 15:38:59 +02001900 if (force) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001901 set_bit(FORCE_DETACH, &device->flags);
1902 drbd_force_state(device, NS(disk, D_FAILED));
Philipp Reisnercdfda632011-07-05 15:38:59 +02001903 retcode = SS_SUCCESS;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001904 goto out;
1905 }
1906
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001907 drbd_suspend_io(device); /* so no-one is stuck in drbd_al_begin_io */
Lars Ellenberge37d2432014-04-01 23:53:30 +02001908 drbd_md_get_buffer(device, __func__); /* make sure there is no in-flight meta-data IO */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001909 retcode = drbd_request_state(device, NS(disk, D_FAILED));
1910 drbd_md_put_buffer(device);
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001911 /* D_FAILED will transition to DISKLESS. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001912 ret = wait_event_interruptible(device->misc_wait,
1913 device->state.disk != D_FAILED);
1914 drbd_resume_io(device);
Philipp Reisner9b2f61a2011-05-24 10:27:38 +02001915 if ((int)retcode == (int)SS_IS_DISKLESS)
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001916 retcode = SS_NOTHING_TO_DO;
1917 if (ret)
1918 retcode = ERR_INTR;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001919out:
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001920 return retcode;
1921}
1922
Philipp Reisnerb411b362009-09-25 16:07:19 -07001923/* Detaching the disk is a process in multiple stages. First we need to lock
1924 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1925 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1926 * internal references as well.
1927 * Only then we have finally detached. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001928int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001929{
Lars Ellenberga910b122014-04-28 18:43:21 +02001930 struct drbd_config_context adm_ctx;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001931 enum drbd_ret_code retcode;
Philipp Reisnercdfda632011-07-05 15:38:59 +02001932 struct detach_parms parms = { };
1933 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001934
Lars Ellenberga910b122014-04-28 18:43:21 +02001935 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001936 if (!adm_ctx.reply_skb)
1937 return retcode;
1938 if (retcode != NO_ERROR)
1939 goto out;
1940
Philipp Reisnercdfda632011-07-05 15:38:59 +02001941 if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
1942 err = detach_parms_from_attrs(&parms, info);
1943 if (err) {
1944 retcode = ERR_MANDATORY_TAG;
Lars Ellenberga910b122014-04-28 18:43:21 +02001945 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
Philipp Reisnercdfda632011-07-05 15:38:59 +02001946 goto out;
1947 }
1948 }
1949
Lars Ellenberg9e276872014-04-28 18:43:22 +02001950 mutex_lock(&adm_ctx.resource->adm_mutex);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001951 retcode = adm_detach(adm_ctx.device, parms.force_detach);
Lars Ellenberg9e276872014-04-28 18:43:22 +02001952 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001953out:
Lars Ellenberga910b122014-04-28 18:43:21 +02001954 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001955 return 0;
1956}
1957
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001958static bool conn_resync_running(struct drbd_connection *connection)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001959{
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001960 struct drbd_peer_device *peer_device;
Philipp Reisner695d08f2011-04-11 22:53:32 -07001961 bool rv = false;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001962 int vnr;
1963
Philipp Reisner695d08f2011-04-11 22:53:32 -07001964 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001965 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1966 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001967 if (device->state.conn == C_SYNC_SOURCE ||
1968 device->state.conn == C_SYNC_TARGET ||
1969 device->state.conn == C_PAUSED_SYNC_S ||
1970 device->state.conn == C_PAUSED_SYNC_T) {
Philipp Reisner695d08f2011-04-11 22:53:32 -07001971 rv = true;
1972 break;
1973 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001974 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001975 rcu_read_unlock();
1976
1977 return rv;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001978}
1979
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001980static bool conn_ov_running(struct drbd_connection *connection)
Lars Ellenbergf3990022011-03-23 14:31:09 +01001981{
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001982 struct drbd_peer_device *peer_device;
Philipp Reisner695d08f2011-04-11 22:53:32 -07001983 bool rv = false;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001984 int vnr;
1985
Philipp Reisner695d08f2011-04-11 22:53:32 -07001986 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001987 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1988 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001989 if (device->state.conn == C_VERIFY_S ||
1990 device->state.conn == C_VERIFY_T) {
Philipp Reisner695d08f2011-04-11 22:53:32 -07001991 rv = true;
1992 break;
1993 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001994 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001995 rcu_read_unlock();
1996
1997 return rv;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001998}
1999
Philipp Reisnercd643972011-04-13 18:00:59 -07002000static enum drbd_ret_code
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002001_check_net_options(struct drbd_connection *connection, struct net_conf *old_net_conf, struct net_conf *new_net_conf)
Philipp Reisnercd643972011-04-13 18:00:59 -07002002{
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002003 struct drbd_peer_device *peer_device;
Philipp Reisnercd643972011-04-13 18:00:59 -07002004 int i;
2005
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002006 if (old_net_conf && connection->cstate == C_WF_REPORT_PARAMS && connection->agreed_pro_version < 100) {
2007 if (new_net_conf->wire_protocol != old_net_conf->wire_protocol)
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02002008 return ERR_NEED_APV_100;
2009
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002010 if (new_net_conf->two_primaries != old_net_conf->two_primaries)
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02002011 return ERR_NEED_APV_100;
2012
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002013 if (strcmp(new_net_conf->integrity_alg, old_net_conf->integrity_alg))
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02002014 return ERR_NEED_APV_100;
2015 }
2016
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002017 if (!new_net_conf->two_primaries &&
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002018 conn_highest_role(connection) == R_PRIMARY &&
2019 conn_highest_peer(connection) == R_PRIMARY)
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02002020 return ERR_NEED_ALLOW_TWO_PRI;
Philipp Reisnerb032b6f2011-04-13 18:16:10 -07002021
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002022 if (new_net_conf->two_primaries &&
2023 (new_net_conf->wire_protocol != DRBD_PROT_C))
Philipp Reisnercd643972011-04-13 18:00:59 -07002024 return ERR_NOT_PROTO_C;
2025
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002026 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2027 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002028 if (get_ldev(device)) {
2029 enum drbd_fencing_p fp = rcu_dereference(device->ldev->disk_conf)->fencing;
2030 put_ldev(device);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002031 if (new_net_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
Philipp Reisnercd643972011-04-13 18:00:59 -07002032 return ERR_STONITH_AND_PROT_A;
Philipp Reisnercd643972011-04-13 18:00:59 -07002033 }
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002034 if (device->state.role == R_PRIMARY && new_net_conf->discard_my_data)
Lars Ellenbergeb120102012-08-01 12:46:20 +02002035 return ERR_DISCARD_IMPOSSIBLE;
Philipp Reisnercd643972011-04-13 18:00:59 -07002036 }
Philipp Reisnercd643972011-04-13 18:00:59 -07002037
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002038 if (new_net_conf->on_congestion != OC_BLOCK && new_net_conf->wire_protocol != DRBD_PROT_A)
Philipp Reisnercd643972011-04-13 18:00:59 -07002039 return ERR_CONG_NOT_PROTO_A;
2040
2041 return NO_ERROR;
2042}
2043
Philipp Reisner44ed1672011-04-19 17:10:19 +02002044static enum drbd_ret_code
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002045check_net_options(struct drbd_connection *connection, struct net_conf *new_net_conf)
Philipp Reisner44ed1672011-04-19 17:10:19 +02002046{
2047 static enum drbd_ret_code rv;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002048 struct drbd_peer_device *peer_device;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002049 int i;
2050
2051 rcu_read_lock();
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002052 rv = _check_net_options(connection, rcu_dereference(connection->net_conf), new_net_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +02002053 rcu_read_unlock();
2054
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002055 /* connection->volumes protected by genl_lock() here */
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002056 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2057 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002058 if (!device->bitmap) {
2059 if (drbd_bm_init(device))
Philipp Reisner44ed1672011-04-19 17:10:19 +02002060 return ERR_NOMEM;
2061 }
2062 }
2063
2064 return rv;
2065}
2066
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002067struct crypto {
2068 struct crypto_hash *verify_tfm;
2069 struct crypto_hash *csums_tfm;
2070 struct crypto_hash *cram_hmac_tfm;
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02002071 struct crypto_hash *integrity_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002072};
2073
2074static int
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002075alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002076{
2077 if (!tfm_name[0])
2078 return NO_ERROR;
2079
2080 *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
2081 if (IS_ERR(*tfm)) {
2082 *tfm = NULL;
2083 return err_alg;
2084 }
2085
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002086 return NO_ERROR;
2087}
2088
2089static enum drbd_ret_code
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002090alloc_crypto(struct crypto *crypto, struct net_conf *new_net_conf)
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002091{
Philipp Reisnerb411b362009-09-25 16:07:19 -07002092 char hmac_name[CRYPTO_MAX_ALG_NAME];
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002093 enum drbd_ret_code rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002094
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002095 rv = alloc_hash(&crypto->csums_tfm, new_net_conf->csums_alg,
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002096 ERR_CSUMS_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002097 if (rv != NO_ERROR)
2098 return rv;
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002099 rv = alloc_hash(&crypto->verify_tfm, new_net_conf->verify_alg,
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002100 ERR_VERIFY_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002101 if (rv != NO_ERROR)
2102 return rv;
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002103 rv = alloc_hash(&crypto->integrity_tfm, new_net_conf->integrity_alg,
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002104 ERR_INTEGRITY_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002105 if (rv != NO_ERROR)
2106 return rv;
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002107 if (new_net_conf->cram_hmac_alg[0] != 0) {
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002108 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002109 new_net_conf->cram_hmac_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002110
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002111 rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
2112 ERR_AUTH_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002113 }
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002114
2115 return rv;
2116}
2117
2118static void free_crypto(struct crypto *crypto)
2119{
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002120 crypto_free_hash(crypto->cram_hmac_tfm);
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02002121 crypto_free_hash(crypto->integrity_tfm);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002122 crypto_free_hash(crypto->csums_tfm);
2123 crypto_free_hash(crypto->verify_tfm);
2124}
2125
Lars Ellenbergf3990022011-03-23 14:31:09 +01002126int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
2127{
Lars Ellenberga910b122014-04-28 18:43:21 +02002128 struct drbd_config_context adm_ctx;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002129 enum drbd_ret_code retcode;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002130 struct drbd_connection *connection;
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002131 struct net_conf *old_net_conf, *new_net_conf = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002132 int err;
2133 int ovr; /* online verify running */
2134 int rsr; /* re-sync running */
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002135 struct crypto crypto = { };
Lars Ellenbergf3990022011-03-23 14:31:09 +01002136
Lars Ellenberga910b122014-04-28 18:43:21 +02002137 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_CONNECTION);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002138 if (!adm_ctx.reply_skb)
2139 return retcode;
2140 if (retcode != NO_ERROR)
Lars Ellenberg9e276872014-04-28 18:43:22 +02002141 goto finish;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002142
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002143 connection = adm_ctx.connection;
Lars Ellenberg9e276872014-04-28 18:43:22 +02002144 mutex_lock(&adm_ctx.resource->adm_mutex);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002145
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002146 new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
2147 if (!new_net_conf) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002148 retcode = ERR_NOMEM;
2149 goto out;
2150 }
2151
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002152 conn_reconfig_start(connection);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002153
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002154 mutex_lock(&connection->data.mutex);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002155 mutex_lock(&connection->resource->conf_update);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002156 old_net_conf = connection->net_conf;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002157
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002158 if (!old_net_conf) {
Lars Ellenberga910b122014-04-28 18:43:21 +02002159 drbd_msg_put_info(adm_ctx.reply_skb, "net conf missing, try connect");
Philipp Reisner44ed1672011-04-19 17:10:19 +02002160 retcode = ERR_INVALID_REQUEST;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002161 goto fail;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002162 }
2163
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002164 *new_net_conf = *old_net_conf;
Lars Ellenberg5979e362011-04-27 21:09:55 +02002165 if (should_set_defaults(info))
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002166 set_net_conf_defaults(new_net_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +02002167
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002168 err = net_conf_from_attrs_for_change(new_net_conf, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02002169 if (err && err != -ENOMSG) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002170 retcode = ERR_MANDATORY_TAG;
Lars Ellenberga910b122014-04-28 18:43:21 +02002171 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
Lars Ellenbergf3990022011-03-23 14:31:09 +01002172 goto fail;
2173 }
2174
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002175 retcode = check_net_options(connection, new_net_conf);
Philipp Reisnercd643972011-04-13 18:00:59 -07002176 if (retcode != NO_ERROR)
2177 goto fail;
2178
Lars Ellenbergf3990022011-03-23 14:31:09 +01002179 /* re-sync running */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002180 rsr = conn_resync_running(connection);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002181 if (rsr && strcmp(new_net_conf->csums_alg, old_net_conf->csums_alg)) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002182 retcode = ERR_CSUMS_RESYNC_RUNNING;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002183 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002184 }
2185
Lars Ellenbergf3990022011-03-23 14:31:09 +01002186 /* online verify running */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002187 ovr = conn_ov_running(connection);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002188 if (ovr && strcmp(new_net_conf->verify_alg, old_net_conf->verify_alg)) {
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002189 retcode = ERR_VERIFY_RUNNING;
2190 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002191 }
2192
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002193 retcode = alloc_crypto(&crypto, new_net_conf);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002194 if (retcode != NO_ERROR)
2195 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002196
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002197 rcu_assign_pointer(connection->net_conf, new_net_conf);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002198
2199 if (!rsr) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002200 crypto_free_hash(connection->csums_tfm);
2201 connection->csums_tfm = crypto.csums_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002202 crypto.csums_tfm = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002203 }
2204 if (!ovr) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002205 crypto_free_hash(connection->verify_tfm);
2206 connection->verify_tfm = crypto.verify_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002207 crypto.verify_tfm = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002208 }
2209
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002210 crypto_free_hash(connection->integrity_tfm);
2211 connection->integrity_tfm = crypto.integrity_tfm;
2212 if (connection->cstate >= C_WF_REPORT_PARAMS && connection->agreed_pro_version >= 100)
2213 /* Do this without trying to take connection->data.mutex again. */
2214 __drbd_send_protocol(connection, P_PROTOCOL_UPDATE);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002215
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002216 crypto_free_hash(connection->cram_hmac_tfm);
2217 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002218
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002219 mutex_unlock(&connection->resource->conf_update);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002220 mutex_unlock(&connection->data.mutex);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002221 synchronize_rcu();
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002222 kfree(old_net_conf);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002223
Andreas Gruenbacher69a22772011-08-09 00:47:13 +02002224 if (connection->cstate >= C_WF_REPORT_PARAMS) {
2225 struct drbd_peer_device *peer_device;
2226 int vnr;
2227
2228 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
2229 drbd_send_sync_param(peer_device);
2230 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01002231
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002232 goto done;
2233
Lars Ellenbergf3990022011-03-23 14:31:09 +01002234 fail:
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002235 mutex_unlock(&connection->resource->conf_update);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002236 mutex_unlock(&connection->data.mutex);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002237 free_crypto(&crypto);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002238 kfree(new_net_conf);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002239 done:
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002240 conn_reconfig_done(connection);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002241 out:
Lars Ellenberg9e276872014-04-28 18:43:22 +02002242 mutex_unlock(&adm_ctx.resource->adm_mutex);
2243 finish:
Lars Ellenberga910b122014-04-28 18:43:21 +02002244 drbd_adm_finish(&adm_ctx, info, retcode);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002245 return 0;
2246}
2247
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002248int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002249{
Lars Ellenberga910b122014-04-28 18:43:21 +02002250 struct drbd_config_context adm_ctx;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002251 struct drbd_peer_device *peer_device;
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002252 struct net_conf *old_net_conf, *new_net_conf = NULL;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002253 struct crypto crypto = { };
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002254 struct drbd_resource *resource;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002255 struct drbd_connection *connection;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002256 enum drbd_ret_code retcode;
2257 int i;
2258 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002259
Lars Ellenberga910b122014-04-28 18:43:21 +02002260 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002261
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002262 if (!adm_ctx.reply_skb)
2263 return retcode;
2264 if (retcode != NO_ERROR)
2265 goto out;
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002266 if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
Lars Ellenberga910b122014-04-28 18:43:21 +02002267 drbd_msg_put_info(adm_ctx.reply_skb, "connection endpoint(s) missing");
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002268 retcode = ERR_INVALID_REQUEST;
2269 goto out;
2270 }
2271
2272 /* No need for _rcu here. All reconfiguration is
2273 * strictly serialized on genl_lock(). We are protected against
2274 * concurrent reconfiguration/addition/deletion */
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002275 for_each_resource(resource, &drbd_resources) {
2276 for_each_connection(connection, resource) {
2277 if (nla_len(adm_ctx.my_addr) == connection->my_addr_len &&
2278 !memcmp(nla_data(adm_ctx.my_addr), &connection->my_addr,
2279 connection->my_addr_len)) {
2280 retcode = ERR_LOCAL_ADDR;
2281 goto out;
2282 }
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002283
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002284 if (nla_len(adm_ctx.peer_addr) == connection->peer_addr_len &&
2285 !memcmp(nla_data(adm_ctx.peer_addr), &connection->peer_addr,
2286 connection->peer_addr_len)) {
2287 retcode = ERR_PEER_ADDR;
2288 goto out;
2289 }
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002290 }
2291 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002292
Lars Ellenberg9e276872014-04-28 18:43:22 +02002293 mutex_lock(&adm_ctx.resource->adm_mutex);
Andreas Gruenbacher3ab706f2011-07-06 15:05:58 +02002294 connection = first_connection(adm_ctx.resource);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002295 conn_reconfig_start(connection);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002296
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002297 if (connection->cstate > C_STANDALONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002298 retcode = ERR_NET_CONFIGURED;
2299 goto fail;
2300 }
2301
Andreas Gruenbachera209b4a2011-08-17 12:43:25 +02002302 /* allocation not in the IO path, drbdsetup / netlink process context */
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002303 new_net_conf = kzalloc(sizeof(*new_net_conf), GFP_KERNEL);
2304 if (!new_net_conf) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002305 retcode = ERR_NOMEM;
2306 goto fail;
2307 }
2308
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002309 set_net_conf_defaults(new_net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002310
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002311 err = net_conf_from_attrs(new_net_conf, info);
Lars Ellenberg25e40932011-08-19 10:39:00 +02002312 if (err && err != -ENOMSG) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002313 retcode = ERR_MANDATORY_TAG;
Lars Ellenberga910b122014-04-28 18:43:21 +02002314 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002315 goto fail;
2316 }
2317
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002318 retcode = check_net_options(connection, new_net_conf);
Philipp Reisnercd643972011-04-13 18:00:59 -07002319 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002320 goto fail;
Philipp Reisner47ff2d02010-06-18 13:56:57 +02002321
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002322 retcode = alloc_crypto(&crypto, new_net_conf);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002323 if (retcode != NO_ERROR)
Philipp Reisner422028b2010-10-27 11:12:07 +02002324 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002325
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002326 ((char *)new_net_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002327
Andreas Gruenbacherb5043c52011-07-28 15:56:02 +02002328 drbd_flush_workqueue(&connection->sender_work);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002329
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002330 mutex_lock(&adm_ctx.resource->conf_update);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002331 old_net_conf = connection->net_conf;
2332 if (old_net_conf) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002333 retcode = ERR_NET_CONFIGURED;
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002334 mutex_unlock(&adm_ctx.resource->conf_update);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002335 goto fail;
2336 }
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002337 rcu_assign_pointer(connection->net_conf, new_net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002338
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002339 conn_free_crypto(connection);
2340 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
2341 connection->integrity_tfm = crypto.integrity_tfm;
2342 connection->csums_tfm = crypto.csums_tfm;
2343 connection->verify_tfm = crypto.verify_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002344
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002345 connection->my_addr_len = nla_len(adm_ctx.my_addr);
2346 memcpy(&connection->my_addr, nla_data(adm_ctx.my_addr), connection->my_addr_len);
2347 connection->peer_addr_len = nla_len(adm_ctx.peer_addr);
2348 memcpy(&connection->peer_addr, nla_data(adm_ctx.peer_addr), connection->peer_addr_len);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002349
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002350 mutex_unlock(&adm_ctx.resource->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002351
Philipp Reisner695d08f2011-04-11 22:53:32 -07002352 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002353 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2354 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002355 device->send_cnt = 0;
2356 device->recv_cnt = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002357 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07002358 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002359
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002360 retcode = conn_request_state(connection, NS(conn, C_UNCONNECTED), CS_VERBOSE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002361
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002362 conn_reconfig_done(connection);
Lars Ellenberg9e276872014-04-28 18:43:22 +02002363 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberga910b122014-04-28 18:43:21 +02002364 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002365 return 0;
2366
2367fail:
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002368 free_crypto(&crypto);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002369 kfree(new_net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002370
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002371 conn_reconfig_done(connection);
Lars Ellenberg9e276872014-04-28 18:43:22 +02002372 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002373out:
Lars Ellenberga910b122014-04-28 18:43:21 +02002374 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002375 return 0;
2376}
2377
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002378static enum drbd_state_rv conn_try_disconnect(struct drbd_connection *connection, bool force)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002379{
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002380 enum drbd_state_rv rv;
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002381
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002382 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002383 force ? CS_HARD : 0);
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002384
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002385 switch (rv) {
2386 case SS_NOTHING_TO_DO:
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002387 break;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002388 case SS_ALREADY_STANDALONE:
2389 return SS_SUCCESS;
2390 case SS_PRIMARY_NOP:
2391 /* Our state checking code wants to see the peer outdated. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002392 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING, pdsk, D_OUTDATED), 0);
Philipp Reisner2bd5ed52013-03-27 14:08:40 +01002393
2394 if (rv == SS_OUTDATE_WO_CONN) /* lost connection before graceful disconnect succeeded */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002395 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_VERBOSE);
Philipp Reisner2bd5ed52013-03-27 14:08:40 +01002396
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002397 break;
2398 case SS_CW_FAILED_BY_PEER:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002399 /* The peer probably wants to see us outdated. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002400 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING,
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002401 disk, D_OUTDATED), 0);
2402 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002403 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002404 CS_HARD);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002405 }
2406 break;
2407 default:;
2408 /* no special handling necessary */
2409 }
2410
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002411 if (rv >= SS_SUCCESS) {
2412 enum drbd_state_rv rv2;
2413 /* No one else can reconfigure the network while I am here.
2414 * The state handling only uses drbd_thread_stop_nowait(),
2415 * we want to really wait here until the receiver is no more.
2416 */
Andreas Gruenbacher9693da22011-06-21 13:58:18 +02002417 drbd_thread_stop(&connection->receiver);
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002418
2419 /* Race breaker. This additional state change request may be
2420 * necessary, if this was a forced disconnect during a receiver
2421 * restart. We may have "killed" the receiver thread just
Andreas Gruenbacher8fe60552011-07-22 11:04:36 +02002422 * after drbd_receiver() returned. Typically, we should be
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002423 * C_STANDALONE already, now, and this becomes a no-op.
2424 */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002425 rv2 = conn_request_state(connection, NS(conn, C_STANDALONE),
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002426 CS_VERBOSE | CS_HARD);
2427 if (rv2 < SS_SUCCESS)
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +02002428 drbd_err(connection,
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002429 "unexpected rv2=%d in conn_try_disconnect()\n",
2430 rv2);
2431 }
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002432 return rv;
2433}
2434
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002435int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002436{
Lars Ellenberga910b122014-04-28 18:43:21 +02002437 struct drbd_config_context adm_ctx;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002438 struct disconnect_parms parms;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002439 struct drbd_connection *connection;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002440 enum drbd_state_rv rv;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002441 enum drbd_ret_code retcode;
2442 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002443
Lars Ellenberga910b122014-04-28 18:43:21 +02002444 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_CONNECTION);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002445 if (!adm_ctx.reply_skb)
2446 return retcode;
2447 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002448 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002449
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002450 connection = adm_ctx.connection;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002451 memset(&parms, 0, sizeof(parms));
2452 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002453 err = disconnect_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002454 if (err) {
2455 retcode = ERR_MANDATORY_TAG;
Lars Ellenberga910b122014-04-28 18:43:21 +02002456 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002457 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002458 }
2459 }
2460
Lars Ellenberg9e276872014-04-28 18:43:22 +02002461 mutex_lock(&adm_ctx.resource->adm_mutex);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002462 rv = conn_try_disconnect(connection, parms.force_disconnect);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002463 if (rv < SS_SUCCESS)
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002464 retcode = rv; /* FIXME: Type mismatch. */
2465 else
2466 retcode = NO_ERROR;
Lars Ellenberg9e276872014-04-28 18:43:22 +02002467 mutex_unlock(&adm_ctx.resource->adm_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002468 fail:
Lars Ellenberga910b122014-04-28 18:43:21 +02002469 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002470 return 0;
2471}
2472
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002473void resync_after_online_grow(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002474{
2475 int iass; /* I am sync source */
2476
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02002477 drbd_info(device, "Resync of new storage after online grow\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002478 if (device->state.role != device->state.peer)
2479 iass = (device->state.role == R_PRIMARY);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002480 else
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002481 iass = test_bit(RESOLVE_CONFLICTS, &first_peer_device(device)->connection->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002482
2483 if (iass)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002484 drbd_start_resync(device, C_SYNC_SOURCE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002485 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002486 _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002487}
2488
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002489int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002490{
Lars Ellenberga910b122014-04-28 18:43:21 +02002491 struct drbd_config_context adm_ctx;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002492 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002493 struct resize_parms rs;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002494 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002495 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002496 enum determine_dev_size dd;
Philipp Reisnerd752b262013-06-25 16:50:08 +02002497 bool change_al_layout = false;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002498 enum dds_flags ddsf;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002499 sector_t u_size;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002500 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002501
Lars Ellenberga910b122014-04-28 18:43:21 +02002502 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002503 if (!adm_ctx.reply_skb)
2504 return retcode;
2505 if (retcode != NO_ERROR)
Lars Ellenberg9e276872014-04-28 18:43:22 +02002506 goto finish;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002507
Lars Ellenberg9e276872014-04-28 18:43:22 +02002508 mutex_lock(&adm_ctx.resource->adm_mutex);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002509 device = adm_ctx.device;
2510 if (!get_ldev(device)) {
Philipp Reisnerd752b262013-06-25 16:50:08 +02002511 retcode = ERR_NO_DISK;
2512 goto fail;
2513 }
2514
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002515 memset(&rs, 0, sizeof(struct resize_parms));
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002516 rs.al_stripes = device->ldev->md.al_stripes;
2517 rs.al_stripe_size = device->ldev->md.al_stripe_size_4k * 4;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002518 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002519 err = resize_parms_from_attrs(&rs, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002520 if (err) {
2521 retcode = ERR_MANDATORY_TAG;
Lars Ellenberga910b122014-04-28 18:43:21 +02002522 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
Philipp Reisnerd752b262013-06-25 16:50:08 +02002523 goto fail_ldev;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002524 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002525 }
2526
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002527 if (device->state.conn > C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002528 retcode = ERR_RESIZE_RESYNC;
Philipp Reisnerd752b262013-06-25 16:50:08 +02002529 goto fail_ldev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002530 }
2531
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002532 if (device->state.role == R_SECONDARY &&
2533 device->state.peer == R_SECONDARY) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002534 retcode = ERR_NO_PRIMARY;
Philipp Reisnerd752b262013-06-25 16:50:08 +02002535 goto fail_ldev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002536 }
2537
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002538 if (rs.no_resync && first_peer_device(device)->connection->agreed_pro_version < 93) {
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002539 retcode = ERR_NEED_APV_93;
Andreas Gruenbacher7b4e4d32011-09-28 22:15:04 +02002540 goto fail_ldev;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002541 }
2542
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002543 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002544 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002545 rcu_read_unlock();
2546 if (u_size != (sector_t)rs.resize_size) {
2547 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
2548 if (!new_disk_conf) {
2549 retcode = ERR_NOMEM;
Philipp Reisner9bcd2522011-09-29 13:00:14 +02002550 goto fail_ldev;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002551 }
2552 }
2553
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002554 if (device->ldev->md.al_stripes != rs.al_stripes ||
2555 device->ldev->md.al_stripe_size_4k != rs.al_stripe_size / 4) {
Philipp Reisnerd752b262013-06-25 16:50:08 +02002556 u32 al_size_k = rs.al_stripes * rs.al_stripe_size;
2557
2558 if (al_size_k > (16 * 1024 * 1024)) {
2559 retcode = ERR_MD_LAYOUT_TOO_BIG;
2560 goto fail_ldev;
2561 }
2562
2563 if (al_size_k < MD_32kB_SECT/2) {
2564 retcode = ERR_MD_LAYOUT_TOO_SMALL;
2565 goto fail_ldev;
2566 }
2567
Philipp Reisnercdc6af82014-04-28 18:43:15 +02002568 if (device->state.conn != C_CONNECTED && !rs.resize_force) {
Philipp Reisnerd752b262013-06-25 16:50:08 +02002569 retcode = ERR_MD_LAYOUT_CONNECTED;
2570 goto fail_ldev;
2571 }
2572
2573 change_al_layout = true;
2574 }
2575
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002576 if (device->ldev->known_size != drbd_get_capacity(device->ldev->backing_bdev))
2577 device->ldev->known_size = drbd_get_capacity(device->ldev->backing_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002578
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002579 if (new_disk_conf) {
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002580 mutex_lock(&device->resource->conf_update);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002581 old_disk_conf = device->ldev->disk_conf;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002582 *new_disk_conf = *old_disk_conf;
2583 new_disk_conf->disk_size = (sector_t)rs.resize_size;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002584 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002585 mutex_unlock(&device->resource->conf_update);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002586 synchronize_rcu();
2587 kfree(old_disk_conf);
2588 }
2589
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002590 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002591 dd = drbd_determine_dev_size(device, ddsf, change_al_layout ? &rs : NULL);
2592 drbd_md_sync(device);
2593 put_ldev(device);
Philipp Reisnere96c9632013-06-25 16:50:07 +02002594 if (dd == DS_ERROR) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002595 retcode = ERR_NOMEM_BITMAP;
2596 goto fail;
Philipp Reisnerd752b262013-06-25 16:50:08 +02002597 } else if (dd == DS_ERROR_SPACE_MD) {
2598 retcode = ERR_MD_LAYOUT_NO_FIT;
2599 goto fail;
2600 } else if (dd == DS_ERROR_SHRINK) {
2601 retcode = ERR_IMPLICIT_SHRINK;
2602 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002603 }
2604
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002605 if (device->state.conn == C_CONNECTED) {
Philipp Reisnere96c9632013-06-25 16:50:07 +02002606 if (dd == DS_GREW)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002607 set_bit(RESIZE_PENDING, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002608
Andreas Gruenbacher69a22772011-08-09 00:47:13 +02002609 drbd_send_uuids(first_peer_device(device));
2610 drbd_send_sizes(first_peer_device(device), 1, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002611 }
2612
2613 fail:
Lars Ellenberg9e276872014-04-28 18:43:22 +02002614 mutex_unlock(&adm_ctx.resource->adm_mutex);
2615 finish:
Lars Ellenberga910b122014-04-28 18:43:21 +02002616 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002617 return 0;
Andreas Gruenbacher7b4e4d32011-09-28 22:15:04 +02002618
2619 fail_ldev:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002620 put_ldev(device);
Andreas Gruenbacher7b4e4d32011-09-28 22:15:04 +02002621 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002622}
2623
Lars Ellenbergf3990022011-03-23 14:31:09 +01002624int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002625{
Lars Ellenberga910b122014-04-28 18:43:21 +02002626 struct drbd_config_context adm_ctx;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002627 enum drbd_ret_code retcode;
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002628 struct res_opts res_opts;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002629 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002630
Lars Ellenberga910b122014-04-28 18:43:21 +02002631 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002632 if (!adm_ctx.reply_skb)
2633 return retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002634 if (retcode != NO_ERROR)
2635 goto fail;
2636
Andreas Gruenbachereb6bea62011-06-21 16:11:28 +02002637 res_opts = adm_ctx.resource->res_opts;
Lars Ellenberg5979e362011-04-27 21:09:55 +02002638 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02002639 set_res_opts_defaults(&res_opts);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002640
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002641 err = res_opts_from_attrs(&res_opts, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02002642 if (err && err != -ENOMSG) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002643 retcode = ERR_MANDATORY_TAG;
Lars Ellenberga910b122014-04-28 18:43:21 +02002644 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002645 goto fail;
2646 }
2647
Lars Ellenberg9e276872014-04-28 18:43:22 +02002648 mutex_lock(&adm_ctx.resource->adm_mutex);
Andreas Gruenbachereb6bea62011-06-21 16:11:28 +02002649 err = set_resource_options(adm_ctx.resource, &res_opts);
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02002650 if (err) {
2651 retcode = ERR_INVALID_REQUEST;
2652 if (err == -ENOMEM)
Philipp Reisner778f2712010-07-06 11:14:00 +02002653 retcode = ERR_NOMEM;
Philipp Reisner778f2712010-07-06 11:14:00 +02002654 }
Lars Ellenberg9e276872014-04-28 18:43:22 +02002655 mutex_unlock(&adm_ctx.resource->adm_mutex);
Philipp Reisner778f2712010-07-06 11:14:00 +02002656
Philipp Reisnerb411b362009-09-25 16:07:19 -07002657fail:
Lars Ellenberga910b122014-04-28 18:43:21 +02002658 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002659 return 0;
2660}
2661
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002662int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002663{
Lars Ellenberga910b122014-04-28 18:43:21 +02002664 struct drbd_config_context adm_ctx;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002665 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002666 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2667
Lars Ellenberga910b122014-04-28 18:43:21 +02002668 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002669 if (!adm_ctx.reply_skb)
2670 return retcode;
2671 if (retcode != NO_ERROR)
2672 goto out;
2673
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002674 device = adm_ctx.device;
Philipp Reisner8fe39aa2013-11-22 13:22:13 +01002675 if (!get_ldev(device)) {
2676 retcode = ERR_NO_DISK;
2677 goto out;
2678 }
2679
2680 mutex_lock(&adm_ctx.resource->adm_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002681
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002682 /* If there is still bitmap IO pending, probably because of a previous
Lars Ellenberg7ee1fb92012-06-19 10:27:58 +02002683 * resync just being finished, wait for it before requesting a new resync.
2684 * Also wait for it's after_state_ch(). */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002685 drbd_suspend_io(device);
2686 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
Andreas Gruenbacherb5043c52011-07-28 15:56:02 +02002687 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002688
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002689 /* If we happen to be C_STANDALONE R_SECONDARY, just change to
2690 * D_INCONSISTENT, and set all bits in the bitmap. Otherwise,
2691 * try to start a resync handshake as sync target for full sync.
Philipp Reisner9376d9f2013-03-27 14:08:36 +01002692 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002693 if (device->state.conn == C_STANDALONE && device->state.role == R_SECONDARY) {
2694 retcode = drbd_request_state(device, NS(disk, D_INCONSISTENT));
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002695 if (retcode >= SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002696 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002697 "set_n_write from invalidate", BM_LOCKED_MASK))
2698 retcode = ERR_IO_MD_DISK;
2699 }
2700 } else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002701 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_T));
2702 drbd_resume_io(device);
Lars Ellenberg9e276872014-04-28 18:43:22 +02002703 mutex_unlock(&adm_ctx.resource->adm_mutex);
Philipp Reisner8fe39aa2013-11-22 13:22:13 +01002704 put_ldev(device);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002705out:
Lars Ellenberga910b122014-04-28 18:43:21 +02002706 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002707 return 0;
2708}
2709
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002710static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2711 union drbd_state mask, union drbd_state val)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002712{
Lars Ellenberga910b122014-04-28 18:43:21 +02002713 struct drbd_config_context adm_ctx;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002714 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002715
Lars Ellenberga910b122014-04-28 18:43:21 +02002716 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002717 if (!adm_ctx.reply_skb)
2718 return retcode;
2719 if (retcode != NO_ERROR)
2720 goto out;
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002721
Lars Ellenberg9e276872014-04-28 18:43:22 +02002722 mutex_lock(&adm_ctx.resource->adm_mutex);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002723 retcode = drbd_request_state(adm_ctx.device, mask, val);
Lars Ellenberg9e276872014-04-28 18:43:22 +02002724 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002725out:
Lars Ellenberga910b122014-04-28 18:43:21 +02002726 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002727 return 0;
2728}
2729
Philipp Reisner8fe39aa2013-11-22 13:22:13 +01002730static int drbd_bmio_set_susp_al(struct drbd_device *device) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002731{
2732 int rv;
2733
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002734 rv = drbd_bmio_set_n_write(device);
2735 drbd_suspend_al(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002736 return rv;
2737}
2738
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002739int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002740{
Lars Ellenberga910b122014-04-28 18:43:21 +02002741 struct drbd_config_context adm_ctx;
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002742 int retcode; /* drbd_ret_code, drbd_state_rv */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002743 struct drbd_device *device;
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002744
Lars Ellenberga910b122014-04-28 18:43:21 +02002745 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002746 if (!adm_ctx.reply_skb)
2747 return retcode;
2748 if (retcode != NO_ERROR)
2749 goto out;
2750
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002751 device = adm_ctx.device;
Philipp Reisner8fe39aa2013-11-22 13:22:13 +01002752 if (!get_ldev(device)) {
2753 retcode = ERR_NO_DISK;
2754 goto out;
2755 }
2756
2757 mutex_lock(&adm_ctx.resource->adm_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002758
2759 /* If there is still bitmap IO pending, probably because of a previous
Lars Ellenberg7ee1fb92012-06-19 10:27:58 +02002760 * resync just being finished, wait for it before requesting a new resync.
2761 * Also wait for it's after_state_ch(). */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002762 drbd_suspend_io(device);
2763 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
Andreas Gruenbacherb5043c52011-07-28 15:56:02 +02002764 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002765
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002766 /* If we happen to be C_STANDALONE R_PRIMARY, just set all bits
2767 * in the bitmap. Otherwise, try to start a resync handshake
2768 * as sync source for full sync.
2769 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002770 if (device->state.conn == C_STANDALONE && device->state.role == R_PRIMARY) {
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002771 /* The peer will get a resync upon connect anyways. Just make that
2772 into a full resync. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002773 retcode = drbd_request_state(device, NS(pdsk, D_INCONSISTENT));
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002774 if (retcode >= SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002775 if (drbd_bitmap_io(device, &drbd_bmio_set_susp_al,
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002776 "set_n_write from invalidate_peer",
2777 BM_LOCKED_SET_ALLOWED))
2778 retcode = ERR_IO_MD_DISK;
2779 }
2780 } else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002781 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_S));
2782 drbd_resume_io(device);
Lars Ellenberg9e276872014-04-28 18:43:22 +02002783 mutex_unlock(&adm_ctx.resource->adm_mutex);
Philipp Reisner8fe39aa2013-11-22 13:22:13 +01002784 put_ldev(device);
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002785out:
Lars Ellenberga910b122014-04-28 18:43:21 +02002786 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002787 return 0;
2788}
2789
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002790int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002791{
Lars Ellenberga910b122014-04-28 18:43:21 +02002792 struct drbd_config_context adm_ctx;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002793 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002794
Lars Ellenberga910b122014-04-28 18:43:21 +02002795 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002796 if (!adm_ctx.reply_skb)
2797 return retcode;
2798 if (retcode != NO_ERROR)
2799 goto out;
2800
Lars Ellenberg9e276872014-04-28 18:43:22 +02002801 mutex_lock(&adm_ctx.resource->adm_mutex);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002802 if (drbd_request_state(adm_ctx.device, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002803 retcode = ERR_PAUSE_IS_SET;
Lars Ellenberg9e276872014-04-28 18:43:22 +02002804 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002805out:
Lars Ellenberga910b122014-04-28 18:43:21 +02002806 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002807 return 0;
2808}
2809
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002810int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002811{
Lars Ellenberga910b122014-04-28 18:43:21 +02002812 struct drbd_config_context adm_ctx;
Philipp Reisnerda9fbc22011-03-29 10:52:01 +02002813 union drbd_dev_state s;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002814 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002815
Lars Ellenberga910b122014-04-28 18:43:21 +02002816 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002817 if (!adm_ctx.reply_skb)
2818 return retcode;
2819 if (retcode != NO_ERROR)
2820 goto out;
2821
Lars Ellenberg9e276872014-04-28 18:43:22 +02002822 mutex_lock(&adm_ctx.resource->adm_mutex);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002823 if (drbd_request_state(adm_ctx.device, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2824 s = adm_ctx.device->state;
Philipp Reisnercd88d032011-01-20 11:46:41 +01002825 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2826 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2827 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2828 } else {
2829 retcode = ERR_PAUSE_IS_CLEAR;
2830 }
2831 }
Lars Ellenberg9e276872014-04-28 18:43:22 +02002832 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002833out:
Lars Ellenberga910b122014-04-28 18:43:21 +02002834 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002835 return 0;
2836}
2837
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002838int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002839{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002840 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002841}
2842
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002843int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002844{
Lars Ellenberga910b122014-04-28 18:43:21 +02002845 struct drbd_config_context adm_ctx;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002846 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002847 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2848
Lars Ellenberga910b122014-04-28 18:43:21 +02002849 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002850 if (!adm_ctx.reply_skb)
2851 return retcode;
2852 if (retcode != NO_ERROR)
2853 goto out;
2854
Lars Ellenberg9e276872014-04-28 18:43:22 +02002855 mutex_lock(&adm_ctx.resource->adm_mutex);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002856 device = adm_ctx.device;
2857 if (test_bit(NEW_CUR_UUID, &device->flags)) {
2858 drbd_uuid_new_current(device);
2859 clear_bit(NEW_CUR_UUID, &device->flags);
Philipp Reisner43a51822010-06-11 11:26:34 +02002860 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002861 drbd_suspend_io(device);
2862 retcode = drbd_request_state(device, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002863 if (retcode == SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002864 if (device->state.conn < C_CONNECTED)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002865 tl_clear(first_peer_device(device)->connection);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002866 if (device->state.disk == D_DISKLESS || device->state.disk == D_FAILED)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002867 tl_restart(first_peer_device(device)->connection, FAIL_FROZEN_DISK_IO);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002868 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002869 drbd_resume_io(device);
Lars Ellenberg9e276872014-04-28 18:43:22 +02002870 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002871out:
Lars Ellenberga910b122014-04-28 18:43:21 +02002872 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002873 return 0;
2874}
2875
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002876int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002877{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002878 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002879}
2880
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002881static int nla_put_drbd_cfg_context(struct sk_buff *skb,
2882 struct drbd_resource *resource,
2883 struct drbd_connection *connection,
2884 struct drbd_device *device)
Lars Ellenberg543cc102011-03-10 22:18:18 +01002885{
2886 struct nlattr *nla;
2887 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
2888 if (!nla)
2889 goto nla_put_failure;
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002890 if (device &&
2891 nla_put_u32(skb, T_ctx_volume, device->vnr))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002892 goto nla_put_failure;
Andreas Gruenbacherf597f6b2014-02-19 10:49:07 +01002893 if (nla_put_string(skb, T_ctx_resource_name, resource->name))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002894 goto nla_put_failure;
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002895 if (connection) {
2896 if (connection->my_addr_len &&
2897 nla_put(skb, T_ctx_my_addr, connection->my_addr_len, &connection->my_addr))
2898 goto nla_put_failure;
2899 if (connection->peer_addr_len &&
2900 nla_put(skb, T_ctx_peer_addr, connection->peer_addr_len, &connection->peer_addr))
2901 goto nla_put_failure;
2902 }
Lars Ellenberg543cc102011-03-10 22:18:18 +01002903 nla_nest_end(skb, nla);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002904 return 0;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002905
2906nla_put_failure:
2907 if (nla)
2908 nla_nest_cancel(skb, nla);
2909 return -EMSGSIZE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002910}
2911
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002912/*
2913 * Return the connection of @resource if @resource has exactly one connection.
2914 */
2915static struct drbd_connection *the_only_connection(struct drbd_resource *resource)
2916{
2917 struct list_head *connections = &resource->connections;
2918
2919 if (list_empty(connections) || connections->next->next != connections)
2920 return NULL;
2921 return list_first_entry(&resource->connections, struct drbd_connection, connections);
2922}
2923
Lars Ellenberg8ce953a2014-02-27 09:46:18 +01002924static int nla_put_status_info(struct sk_buff *skb, struct drbd_device *device,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002925 const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002926{
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002927 struct drbd_resource *resource = device->resource;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002928 struct state_info *si = NULL; /* for sizeof(si->member); */
2929 struct nlattr *nla;
2930 int got_ldev;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002931 int err = 0;
2932 int exclude_sensitive;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002933
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002934 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2935 * to. So we better exclude_sensitive information.
2936 *
2937 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2938 * in the context of the requesting user process. Exclude sensitive
2939 * information, unless current has superuser.
2940 *
2941 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2942 * relies on the current implementation of netlink_dump(), which
2943 * executes the dump callback successively from netlink_recvmsg(),
2944 * always in the context of the receiving process */
2945 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002946
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002947 got_ldev = get_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002948
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002949 /* We need to add connection name and volume number information still.
2950 * Minor number is in drbd_genlmsghdr. */
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002951 if (nla_put_drbd_cfg_context(skb, resource, the_only_connection(resource), device))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002952 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002953
Andreas Gruenbachereb6bea62011-06-21 16:11:28 +02002954 if (res_opts_to_skb(skb, &device->resource->res_opts, exclude_sensitive))
Lars Ellenbergf3990022011-03-23 14:31:09 +01002955 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002956
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002957 rcu_read_lock();
Andreas Gruenbacherf9eb7bf2013-06-25 16:50:05 +02002958 if (got_ldev) {
2959 struct disk_conf *disk_conf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002960
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002961 disk_conf = rcu_dereference(device->ldev->disk_conf);
Andreas Gruenbacherf9eb7bf2013-06-25 16:50:05 +02002962 err = disk_conf_to_skb(skb, disk_conf, exclude_sensitive);
2963 }
2964 if (!err) {
2965 struct net_conf *nc;
2966
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002967 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
Andreas Gruenbacherf9eb7bf2013-06-25 16:50:05 +02002968 if (nc)
2969 err = net_conf_to_skb(skb, nc, exclude_sensitive);
2970 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002971 rcu_read_unlock();
2972 if (err)
2973 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002974
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002975 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
2976 if (!nla)
2977 goto nla_put_failure;
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002978 if (nla_put_u32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY) ||
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002979 nla_put_u32(skb, T_current_state, device->state.i) ||
2980 nla_put_u64(skb, T_ed_uuid, device->ed_uuid) ||
2981 nla_put_u64(skb, T_capacity, drbd_get_capacity(device->this_bdev)) ||
2982 nla_put_u64(skb, T_send_cnt, device->send_cnt) ||
2983 nla_put_u64(skb, T_recv_cnt, device->recv_cnt) ||
2984 nla_put_u64(skb, T_read_cnt, device->read_cnt) ||
2985 nla_put_u64(skb, T_writ_cnt, device->writ_cnt) ||
2986 nla_put_u64(skb, T_al_writ_cnt, device->al_writ_cnt) ||
2987 nla_put_u64(skb, T_bm_writ_cnt, device->bm_writ_cnt) ||
2988 nla_put_u32(skb, T_ap_bio_cnt, atomic_read(&device->ap_bio_cnt)) ||
2989 nla_put_u32(skb, T_ap_pending_cnt, atomic_read(&device->ap_pending_cnt)) ||
2990 nla_put_u32(skb, T_rs_pending_cnt, atomic_read(&device->rs_pending_cnt)))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002991 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002992
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002993 if (got_ldev) {
Philipp Reisner39a1aa72012-08-08 21:19:09 +02002994 int err;
2995
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002996 spin_lock_irq(&device->ldev->md.uuid_lock);
2997 err = nla_put(skb, T_uuids, sizeof(si->uuids), device->ldev->md.uuid);
2998 spin_unlock_irq(&device->ldev->md.uuid_lock);
Philipp Reisner39a1aa72012-08-08 21:19:09 +02002999
3000 if (err)
3001 goto nla_put_failure;
3002
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003003 if (nla_put_u32(skb, T_disk_flags, device->ldev->md.flags) ||
3004 nla_put_u64(skb, T_bits_total, drbd_bm_bits(device)) ||
3005 nla_put_u64(skb, T_bits_oos, drbd_bm_total_weight(device)))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02003006 goto nla_put_failure;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003007 if (C_SYNC_SOURCE <= device->state.conn &&
3008 C_PAUSED_SYNC_T >= device->state.conn) {
3009 if (nla_put_u64(skb, T_bits_rs_total, device->rs_total) ||
3010 nla_put_u64(skb, T_bits_rs_failed, device->rs_failed))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02003011 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003012 }
3013 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003014
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003015 if (sib) {
3016 switch(sib->sib_reason) {
3017 case SIB_SYNC_PROGRESS:
3018 case SIB_GET_STATUS_REPLY:
3019 break;
3020 case SIB_STATE_CHANGE:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02003021 if (nla_put_u32(skb, T_prev_state, sib->os.i) ||
3022 nla_put_u32(skb, T_new_state, sib->ns.i))
3023 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003024 break;
3025 case SIB_HELPER_POST:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02003026 if (nla_put_u32(skb, T_helper_exit_code,
3027 sib->helper_exit_code))
3028 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003029 /* fall through */
3030 case SIB_HELPER_PRE:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02003031 if (nla_put_string(skb, T_helper, sib->helper_name))
3032 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003033 break;
3034 }
3035 }
3036 nla_nest_end(skb, nla);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003037
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003038 if (0)
3039nla_put_failure:
3040 err = -EMSGSIZE;
3041 if (got_ldev)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003042 put_ldev(device);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003043 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003044}
3045
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003046int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003047{
Lars Ellenberga910b122014-04-28 18:43:21 +02003048 struct drbd_config_context adm_ctx;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003049 enum drbd_ret_code retcode;
3050 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003051
Lars Ellenberga910b122014-04-28 18:43:21 +02003052 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003053 if (!adm_ctx.reply_skb)
3054 return retcode;
3055 if (retcode != NO_ERROR)
3056 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003057
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003058 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.device, NULL);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003059 if (err) {
3060 nlmsg_free(adm_ctx.reply_skb);
3061 return err;
3062 }
3063out:
Lars Ellenberga910b122014-04-28 18:43:21 +02003064 drbd_adm_finish(&adm_ctx, info, retcode);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003065 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003066}
3067
Rashika Kheria4b7a5302013-12-19 15:17:33 +05303068static int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003069{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003070 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003071 struct drbd_genlmsghdr *dh;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003072 struct drbd_resource *pos = (struct drbd_resource *)cb->args[0];
3073 struct drbd_resource *resource = NULL;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003074 struct drbd_resource *tmp;
Lars Ellenberg543cc102011-03-10 22:18:18 +01003075 unsigned volume = cb->args[1];
Philipp Reisnerb411b362009-09-25 16:07:19 -07003076
Lars Ellenberg543cc102011-03-10 22:18:18 +01003077 /* Open coded, deferred, iteration:
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003078 * for_each_resource_safe(resource, tmp, &drbd_resources) {
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02003079 * connection = "first connection of resource or undefined";
3080 * idr_for_each_entry(&resource->devices, device, i) {
Lars Ellenberg543cc102011-03-10 22:18:18 +01003081 * ...
3082 * }
3083 * }
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003084 * where resource is cb->args[0];
Lars Ellenberg543cc102011-03-10 22:18:18 +01003085 * and i is cb->args[1];
3086 *
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003087 * cb->args[2] indicates if we shall loop over all resources,
3088 * or just dump all volumes of a single resource.
3089 *
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003090 * This may miss entries inserted after this dump started,
3091 * or entries deleted before they are reached.
Lars Ellenberg543cc102011-03-10 22:18:18 +01003092 *
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003093 * We need to make sure the device won't disappear while
Lars Ellenberg543cc102011-03-10 22:18:18 +01003094 * we are looking at it, and revalidate our iterators
3095 * on each iteration.
3096 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003097
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003098 /* synchronize with conn_create()/drbd_destroy_connection() */
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003099 rcu_read_lock();
Lars Ellenberg543cc102011-03-10 22:18:18 +01003100 /* revalidate iterator position */
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003101 for_each_resource_rcu(tmp, &drbd_resources) {
Lars Ellenberg543cc102011-03-10 22:18:18 +01003102 if (pos == NULL) {
3103 /* first iteration */
3104 pos = tmp;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003105 resource = pos;
Lars Ellenberg543cc102011-03-10 22:18:18 +01003106 break;
3107 }
3108 if (tmp == pos) {
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003109 resource = pos;
Lars Ellenberg543cc102011-03-10 22:18:18 +01003110 break;
3111 }
3112 }
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003113 if (resource) {
3114next_resource:
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02003115 device = idr_get_next(&resource->devices, &volume);
3116 if (!device) {
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003117 /* No more volumes to dump on this resource.
3118 * Advance resource iterator. */
3119 pos = list_entry_rcu(resource->resources.next,
3120 struct drbd_resource, resources);
3121 /* Did we dump any volume of this resource yet? */
Lars Ellenberg543cc102011-03-10 22:18:18 +01003122 if (volume != 0) {
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003123 /* If we reached the end of the list,
3124 * or only a single resource dump was requested,
3125 * we are done. */
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003126 if (&pos->resources == &drbd_resources || cb->args[2])
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003127 goto out;
Lars Ellenberg543cc102011-03-10 22:18:18 +01003128 volume = 0;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003129 resource = pos;
3130 goto next_resource;
Lars Ellenberg543cc102011-03-10 22:18:18 +01003131 }
3132 }
3133
Philipp Reisner98683652012-11-09 14:18:43 +01003134 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003135 cb->nlh->nlmsg_seq, &drbd_genl_family,
3136 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
3137 if (!dh)
Lars Ellenberg543cc102011-03-10 22:18:18 +01003138 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003139
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02003140 if (!device) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003141 /* This is a connection without a single volume.
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003142 * Suprisingly enough, it may have a network
3143 * configuration. */
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02003144 struct drbd_connection *connection;
3145
Lars Ellenberg543cc102011-03-10 22:18:18 +01003146 dh->minor = -1U;
3147 dh->ret_code = NO_ERROR;
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02003148 connection = the_only_connection(resource);
3149 if (nla_put_drbd_cfg_context(skb, resource, connection, NULL))
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003150 goto cancel;
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02003151 if (connection) {
3152 struct net_conf *nc;
3153
3154 nc = rcu_dereference(connection->net_conf);
3155 if (nc && net_conf_to_skb(skb, nc, 1) != 0)
3156 goto cancel;
3157 }
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003158 goto done;
Lars Ellenberg543cc102011-03-10 22:18:18 +01003159 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003160
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +02003161 D_ASSERT(device, device->vnr == volume);
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02003162 D_ASSERT(device, device->resource == resource);
Lars Ellenberg543cc102011-03-10 22:18:18 +01003163
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003164 dh->minor = device_to_minor(device);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003165 dh->ret_code = NO_ERROR;
3166
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003167 if (nla_put_status_info(skb, device, NULL)) {
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003168cancel:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003169 genlmsg_cancel(skb, dh);
Lars Ellenberg543cc102011-03-10 22:18:18 +01003170 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003171 }
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003172done:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003173 genlmsg_end(skb, dh);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003174 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003175
Lars Ellenberg543cc102011-03-10 22:18:18 +01003176out:
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003177 rcu_read_unlock();
Lars Ellenberg543cc102011-03-10 22:18:18 +01003178 /* where to start the next iteration */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003179 cb->args[0] = (long)pos;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003180 cb->args[1] = (pos == resource) ? volume + 1 : 0;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003181
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003182 /* No more resources/volumes/minors found results in an empty skb.
Lars Ellenberg543cc102011-03-10 22:18:18 +01003183 * Which will terminate the dump. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003184 return skb->len;
3185}
3186
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003187/*
3188 * Request status of all resources, or of all volumes within a single resource.
3189 *
3190 * This is a dump, as the answer may not fit in a single reply skb otherwise.
3191 * Which means we cannot use the family->attrbuf or other such members, because
3192 * dump is NOT protected by the genl_lock(). During dump, we only have access
3193 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
3194 *
3195 * Once things are setup properly, we call into get_one_status().
Philipp Reisnerb411b362009-09-25 16:07:19 -07003196 */
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003197int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003198{
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003199 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
3200 struct nlattr *nla;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003201 const char *resource_name;
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003202 struct drbd_resource *resource;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003203 int maxtype;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003204
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003205 /* Is this a followup call? */
3206 if (cb->args[0]) {
3207 /* ... of a single resource dump,
3208 * and the resource iterator has been advanced already? */
3209 if (cb->args[2] && cb->args[2] != cb->args[0])
3210 return 0; /* DONE. */
3211 goto dump;
3212 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003213
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003214 /* First call (from netlink_dump_start). We need to figure out
3215 * which resource(s) the user wants us to dump. */
3216 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
3217 nlmsg_attrlen(cb->nlh, hdrlen),
3218 DRBD_NLA_CFG_CONTEXT);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003219
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003220 /* No explicit context given. Dump all. */
3221 if (!nla)
3222 goto dump;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003223 maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
3224 nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
3225 if (IS_ERR(nla))
3226 return PTR_ERR(nla);
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003227 /* context given, but no name present? */
3228 if (!nla)
3229 return -EINVAL;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003230 resource_name = nla_data(nla);
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003231 if (!*resource_name)
3232 return -ENODEV;
3233 resource = drbd_find_resource(resource_name);
3234 if (!resource)
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003235 return -ENODEV;
3236
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003237 kref_put(&resource->kref, drbd_destroy_resource); /* get_one_status() revalidates the resource */
Philipp Reisner0ace9df2011-04-24 10:53:19 +02003238
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003239 /* prime iterators, and set "filter" mode mark:
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003240 * only dump this connection. */
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003241 cb->args[0] = (long)resource;
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003242 /* cb->args[1] = 0; passed in this way. */
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003243 cb->args[2] = (long)resource;
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003244
3245dump:
3246 return get_one_status(skb, cb);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003247}
3248
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003249int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003250{
Lars Ellenberga910b122014-04-28 18:43:21 +02003251 struct drbd_config_context adm_ctx;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003252 enum drbd_ret_code retcode;
3253 struct timeout_parms tp;
3254 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003255
Lars Ellenberga910b122014-04-28 18:43:21 +02003256 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003257 if (!adm_ctx.reply_skb)
3258 return retcode;
3259 if (retcode != NO_ERROR)
3260 goto out;
3261
3262 tp.timeout_type =
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003263 adm_ctx.device->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
3264 test_bit(USE_DEGR_WFC_T, &adm_ctx.device->flags) ? UT_DEGRADED :
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003265 UT_DEFAULT;
3266
3267 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
3268 if (err) {
3269 nlmsg_free(adm_ctx.reply_skb);
3270 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003271 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003272out:
Lars Ellenberga910b122014-04-28 18:43:21 +02003273 drbd_adm_finish(&adm_ctx, info, retcode);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003274 return 0;
3275}
Lars Ellenberg873b0d52011-01-21 22:53:48 +01003276
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003277int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
3278{
Lars Ellenberga910b122014-04-28 18:43:21 +02003279 struct drbd_config_context adm_ctx;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003280 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003281 enum drbd_ret_code retcode;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003282 struct start_ov_parms parms;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003283
Lars Ellenberga910b122014-04-28 18:43:21 +02003284 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003285 if (!adm_ctx.reply_skb)
3286 return retcode;
3287 if (retcode != NO_ERROR)
3288 goto out;
3289
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003290 device = adm_ctx.device;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003291
3292 /* resume from last known position, if possible */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003293 parms.ov_start_sector = device->ov_start_sector;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003294 parms.ov_stop_sector = ULLONG_MAX;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003295 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01003296 int err = start_ov_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003297 if (err) {
3298 retcode = ERR_MANDATORY_TAG;
Lars Ellenberga910b122014-04-28 18:43:21 +02003299 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003300 goto out;
3301 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003302 }
Lars Ellenberg9e276872014-04-28 18:43:22 +02003303 mutex_lock(&adm_ctx.resource->adm_mutex);
3304
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003305 /* w_make_ov_request expects position to be aligned */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003306 device->ov_start_sector = parms.ov_start_sector & ~(BM_SECT_PER_BIT-1);
3307 device->ov_stop_sector = parms.ov_stop_sector;
Lars Ellenberg873b0d52011-01-21 22:53:48 +01003308
3309 /* If there is still bitmap IO pending, e.g. previous resync or verify
3310 * just being finished, wait for it before requesting a new resync. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003311 drbd_suspend_io(device);
3312 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
3313 retcode = drbd_request_state(device, NS(conn, C_VERIFY_S));
3314 drbd_resume_io(device);
Lars Ellenberg9e276872014-04-28 18:43:22 +02003315
3316 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003317out:
Lars Ellenberga910b122014-04-28 18:43:21 +02003318 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003319 return 0;
3320}
3321
3322
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003323int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003324{
Lars Ellenberga910b122014-04-28 18:43:21 +02003325 struct drbd_config_context adm_ctx;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003326 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003327 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003328 int skip_initial_sync = 0;
3329 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003330 struct new_c_uuid_parms args;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003331
Lars Ellenberga910b122014-04-28 18:43:21 +02003332 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003333 if (!adm_ctx.reply_skb)
3334 return retcode;
3335 if (retcode != NO_ERROR)
3336 goto out_nolock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003337
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003338 device = adm_ctx.device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003339 memset(&args, 0, sizeof(args));
3340 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01003341 err = new_c_uuid_parms_from_attrs(&args, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003342 if (err) {
3343 retcode = ERR_MANDATORY_TAG;
Lars Ellenberga910b122014-04-28 18:43:21 +02003344 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003345 goto out_nolock;
3346 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003347 }
3348
Lars Ellenberg9e276872014-04-28 18:43:22 +02003349 mutex_lock(&adm_ctx.resource->adm_mutex);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003350 mutex_lock(device->state_mutex); /* Protects us against serialized state changes. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003351
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003352 if (!get_ldev(device)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003353 retcode = ERR_NO_DISK;
3354 goto out;
3355 }
3356
3357 /* this is "skip initial sync", assume to be clean */
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02003358 if (device->state.conn == C_CONNECTED &&
3359 first_peer_device(device)->connection->agreed_pro_version >= 90 &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003360 device->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02003361 drbd_info(device, "Preparing to skip initial sync\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003362 skip_initial_sync = 1;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003363 } else if (device->state.conn != C_STANDALONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003364 retcode = ERR_CONNECTED;
3365 goto out_dec;
3366 }
3367
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003368 drbd_uuid_set(device, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
3369 drbd_uuid_new_current(device); /* New current, previous to UI_BITMAP */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003370
3371 if (args.clear_bm) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003372 err = drbd_bitmap_io(device, &drbd_bmio_clear_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003373 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003374 if (err) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02003375 drbd_err(device, "Writing bitmap failed with %d\n", err);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003376 retcode = ERR_IO_MD_DISK;
3377 }
3378 if (skip_initial_sync) {
Andreas Gruenbacher69a22772011-08-09 00:47:13 +02003379 drbd_send_uuids_skip_initial_sync(first_peer_device(device));
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003380 _drbd_uuid_set(device, UI_BITMAP, 0);
3381 drbd_print_uuids(device, "cleared bitmap UUID");
Andreas Gruenbacher05008132011-07-07 14:19:42 +02003382 spin_lock_irq(&device->resource->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003383 _drbd_set_state(_NS2(device, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
Philipp Reisnerb411b362009-09-25 16:07:19 -07003384 CS_VERBOSE, NULL);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02003385 spin_unlock_irq(&device->resource->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003386 }
3387 }
3388
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003389 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003390out_dec:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003391 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003392out:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003393 mutex_unlock(device->state_mutex);
Lars Ellenberg9e276872014-04-28 18:43:22 +02003394 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003395out_nolock:
Lars Ellenberga910b122014-04-28 18:43:21 +02003396 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003397 return 0;
3398}
3399
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003400static enum drbd_ret_code
Lars Ellenberga910b122014-04-28 18:43:21 +02003401drbd_check_resource_name(struct drbd_config_context *adm_ctx)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003402{
Lars Ellenberga910b122014-04-28 18:43:21 +02003403 const char *name = adm_ctx->resource_name;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003404 if (!name || !name[0]) {
Lars Ellenberga910b122014-04-28 18:43:21 +02003405 drbd_msg_put_info(adm_ctx->reply_skb, "resource name missing");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003406 return ERR_MANDATORY_TAG;
3407 }
3408 /* if we want to use these in sysfs/configfs/debugfs some day,
3409 * we must not allow slashes */
3410 if (strchr(name, '/')) {
Lars Ellenberga910b122014-04-28 18:43:21 +02003411 drbd_msg_put_info(adm_ctx->reply_skb, "invalid resource name");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003412 return ERR_INVALID_REQUEST;
3413 }
3414 return NO_ERROR;
3415}
Philipp Reisnerb411b362009-09-25 16:07:19 -07003416
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003417int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003418{
Lars Ellenberga910b122014-04-28 18:43:21 +02003419 struct drbd_config_context adm_ctx;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003420 enum drbd_ret_code retcode;
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003421 struct res_opts res_opts;
3422 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003423
Lars Ellenberga910b122014-04-28 18:43:21 +02003424 retcode = drbd_adm_prepare(&adm_ctx, skb, info, 0);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003425 if (!adm_ctx.reply_skb)
3426 return retcode;
3427 if (retcode != NO_ERROR)
3428 goto out;
3429
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003430 set_res_opts_defaults(&res_opts);
3431 err = res_opts_from_attrs(&res_opts, info);
3432 if (err && err != -ENOMSG) {
3433 retcode = ERR_MANDATORY_TAG;
Lars Ellenberga910b122014-04-28 18:43:21 +02003434 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003435 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003436 }
3437
Lars Ellenberga910b122014-04-28 18:43:21 +02003438 retcode = drbd_check_resource_name(&adm_ctx);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003439 if (retcode != NO_ERROR)
3440 goto out;
3441
Andreas Gruenbacher5c661042011-07-05 18:26:00 +02003442 if (adm_ctx.resource) {
Lars Ellenberg38f19612011-03-14 13:22:35 +01003443 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
3444 retcode = ERR_INVALID_REQUEST;
Lars Ellenberga910b122014-04-28 18:43:21 +02003445 drbd_msg_put_info(adm_ctx.reply_skb, "resource exists");
Lars Ellenberg38f19612011-03-14 13:22:35 +01003446 }
3447 /* else: still NO_ERROR */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003448 goto out;
Philipp Reisner9f5180e2009-10-06 09:30:14 +02003449 }
3450
Lars Ellenberg9e276872014-04-28 18:43:22 +02003451 /* not yet safe for genl_family.parallel_ops */
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003452 if (!conn_create(adm_ctx.resource_name, &res_opts))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003453 retcode = ERR_NOMEM;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003454out:
Lars Ellenberga910b122014-04-28 18:43:21 +02003455 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003456 return 0;
3457}
3458
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003459int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003460{
Lars Ellenberga910b122014-04-28 18:43:21 +02003461 struct drbd_config_context adm_ctx;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003462 struct drbd_genlmsghdr *dh = info->userhdr;
3463 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003464
Lars Ellenberga910b122014-04-28 18:43:21 +02003465 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003466 if (!adm_ctx.reply_skb)
3467 return retcode;
3468 if (retcode != NO_ERROR)
3469 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003470
Andreas Gruenbacherf2257a52011-07-14 16:00:40 +02003471 if (dh->minor > MINORMASK) {
Lars Ellenberga910b122014-04-28 18:43:21 +02003472 drbd_msg_put_info(adm_ctx.reply_skb, "requested minor out of range");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003473 retcode = ERR_INVALID_REQUEST;
3474 goto out;
3475 }
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +02003476 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
Lars Ellenberga910b122014-04-28 18:43:21 +02003477 drbd_msg_put_info(adm_ctx.reply_skb, "requested volume id out of range");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003478 retcode = ERR_INVALID_REQUEST;
3479 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05003480 }
3481
Lars Ellenberg38f19612011-03-14 13:22:35 +01003482 /* drbd_adm_prepare made sure already
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02003483 * that first_peer_device(device)->connection and device->vnr match the request. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003484 if (adm_ctx.device) {
Lars Ellenberg38f19612011-03-14 13:22:35 +01003485 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
3486 retcode = ERR_MINOR_EXISTS;
3487 /* else: still NO_ERROR */
3488 goto out;
3489 }
3490
Lars Ellenberg9e276872014-04-28 18:43:22 +02003491 mutex_lock(&adm_ctx.resource->adm_mutex);
Lars Ellenberga910b122014-04-28 18:43:21 +02003492 retcode = drbd_create_device(&adm_ctx, dh->minor);
Lars Ellenberg9e276872014-04-28 18:43:22 +02003493 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003494out:
Lars Ellenberga910b122014-04-28 18:43:21 +02003495 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003496 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003497}
3498
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003499static enum drbd_ret_code adm_del_minor(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003500{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003501 if (device->state.disk == D_DISKLESS &&
3502 /* no need to be device->state.conn == C_STANDALONE &&
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003503 * we may want to delete a minor from a live replication group.
3504 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003505 device->state.role == R_SECONDARY) {
3506 _drbd_request_state(device, NS(conn, C_WF_REPORT_PARAMS),
Philipp Reisner369bea62011-07-06 23:04:44 +02003507 CS_VERBOSE + CS_WAIT_COMPLETE);
Andreas Gruenbacherf82795d2011-07-03 23:32:26 +02003508 drbd_delete_device(device);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003509 return NO_ERROR;
3510 } else
3511 return ERR_MINOR_CONFIGURED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003512}
3513
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003514int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05003515{
Lars Ellenberga910b122014-04-28 18:43:21 +02003516 struct drbd_config_context adm_ctx;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003517 enum drbd_ret_code retcode;
3518
Lars Ellenberga910b122014-04-28 18:43:21 +02003519 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003520 if (!adm_ctx.reply_skb)
3521 return retcode;
3522 if (retcode != NO_ERROR)
3523 goto out;
3524
Lars Ellenberg9e276872014-04-28 18:43:22 +02003525 mutex_lock(&adm_ctx.resource->adm_mutex);
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003526 retcode = adm_del_minor(adm_ctx.device);
Lars Ellenberg9e276872014-04-28 18:43:22 +02003527 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003528out:
Lars Ellenberga910b122014-04-28 18:43:21 +02003529 drbd_adm_finish(&adm_ctx, info, retcode);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003530 return 0;
3531}
3532
3533int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
3534{
Lars Ellenberga910b122014-04-28 18:43:21 +02003535 struct drbd_config_context adm_ctx;
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003536 struct drbd_resource *resource;
3537 struct drbd_connection *connection;
3538 struct drbd_device *device;
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003539 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003540 unsigned i;
3541
Lars Ellenberga910b122014-04-28 18:43:21 +02003542 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003543 if (!adm_ctx.reply_skb)
3544 return retcode;
3545 if (retcode != NO_ERROR)
Lars Ellenberg9e276872014-04-28 18:43:22 +02003546 goto finish;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003547
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003548 resource = adm_ctx.resource;
Lars Ellenberg9e276872014-04-28 18:43:22 +02003549 mutex_lock(&resource->adm_mutex);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003550 /* demote */
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003551 for_each_connection(connection, resource) {
3552 struct drbd_peer_device *peer_device;
3553
3554 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
3555 retcode = drbd_set_role(peer_device->device, R_SECONDARY, 0);
3556 if (retcode < SS_SUCCESS) {
Lars Ellenberga910b122014-04-28 18:43:21 +02003557 drbd_msg_put_info(adm_ctx.reply_skb, "failed to demote");
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003558 goto out;
3559 }
3560 }
3561
3562 retcode = conn_try_disconnect(connection, 0);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003563 if (retcode < SS_SUCCESS) {
Lars Ellenberga910b122014-04-28 18:43:21 +02003564 drbd_msg_put_info(adm_ctx.reply_skb, "failed to disconnect");
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003565 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003566 }
3567 }
3568
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003569 /* detach */
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003570 idr_for_each_entry(&resource->devices, device, i) {
3571 retcode = adm_detach(device, 0);
Lars Ellenberg27012382012-07-24 10:13:55 +02003572 if (retcode < SS_SUCCESS || retcode > NO_ERROR) {
Lars Ellenberga910b122014-04-28 18:43:21 +02003573 drbd_msg_put_info(adm_ctx.reply_skb, "failed to detach");
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003574 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003575 }
3576 }
3577
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003578 /* If we reach this, all volumes (of this connection) are Secondary,
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003579 * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003580 * actually stopped, state handling only does drbd_thread_stop_nowait(). */
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003581 for_each_connection(connection, resource)
3582 drbd_thread_stop(&connection->worker);
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003583
3584 /* Now, nothing can fail anymore */
3585
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003586 /* delete volumes */
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003587 idr_for_each_entry(&resource->devices, device, i) {
3588 retcode = adm_del_minor(device);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003589 if (retcode != NO_ERROR) {
3590 /* "can not happen" */
Lars Ellenberga910b122014-04-28 18:43:21 +02003591 drbd_msg_put_info(adm_ctx.reply_skb, "failed to delete volume");
Philipp Reisneref356262011-04-13 14:21:29 -07003592 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003593 }
3594 }
3595
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003596 list_del_rcu(&resource->resources);
3597 synchronize_rcu();
3598 drbd_free_resource(resource);
3599 retcode = NO_ERROR;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003600out:
Lars Ellenberg9e276872014-04-28 18:43:22 +02003601 mutex_unlock(&resource->adm_mutex);
3602finish:
Lars Ellenberga910b122014-04-28 18:43:21 +02003603 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003604 return 0;
3605}
3606
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003607int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05003608{
Lars Ellenberga910b122014-04-28 18:43:21 +02003609 struct drbd_config_context adm_ctx;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003610 struct drbd_resource *resource;
3611 struct drbd_connection *connection;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003612 enum drbd_ret_code retcode;
3613
Lars Ellenberga910b122014-04-28 18:43:21 +02003614 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003615 if (!adm_ctx.reply_skb)
3616 return retcode;
3617 if (retcode != NO_ERROR)
Lars Ellenberg9e276872014-04-28 18:43:22 +02003618 goto finish;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003619
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003620 resource = adm_ctx.resource;
Lars Ellenberg9e276872014-04-28 18:43:22 +02003621 mutex_lock(&resource->adm_mutex);
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003622 for_each_connection(connection, resource) {
3623 if (connection->cstate > C_STANDALONE) {
3624 retcode = ERR_NET_CONFIGURED;
3625 goto out;
3626 }
3627 }
3628 if (!idr_is_empty(&resource->devices)) {
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003629 retcode = ERR_RES_IN_USE;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003630 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05003631 }
3632
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003633 list_del_rcu(&resource->resources);
3634 for_each_connection(connection, resource)
3635 drbd_thread_stop(&connection->worker);
3636 synchronize_rcu();
3637 drbd_free_resource(resource);
3638 retcode = NO_ERROR;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003639out:
Lars Ellenberg9e276872014-04-28 18:43:22 +02003640 mutex_unlock(&resource->adm_mutex);
3641finish:
Lars Ellenberga910b122014-04-28 18:43:21 +02003642 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003643 return 0;
3644}
3645
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003646void drbd_bcast_event(struct drbd_device *device, const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003647{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003648 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
3649 struct sk_buff *msg;
3650 struct drbd_genlmsghdr *d_out;
3651 unsigned seq;
3652 int err = -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003653
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003654 seq = atomic_inc_return(&drbd_genl_seq);
3655 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
3656 if (!msg)
3657 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003658
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003659 err = -EMSGSIZE;
3660 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
3661 if (!d_out) /* cannot happen, but anyways. */
3662 goto nla_put_failure;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003663 d_out->minor = device_to_minor(device);
Andreas Gruenbacher6f9b5f82011-05-06 01:03:32 +02003664 d_out->ret_code = NO_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003665
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003666 if (nla_put_status_info(msg, device, sib))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003667 goto nla_put_failure;
3668 genlmsg_end(msg, d_out);
3669 err = drbd_genl_multicast_events(msg, 0);
3670 /* msg has been consumed or freed in netlink_broadcast() */
3671 if (err && err != -ESRCH)
3672 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003673
Philipp Reisnerb411b362009-09-25 16:07:19 -07003674 return;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003675
3676nla_put_failure:
3677 nlmsg_free(msg);
3678failed:
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02003679 drbd_err(device, "Error %d while broadcasting event. "
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003680 "Event seq:%u sib_reason:%u\n",
3681 err, seq, sib->sib_reason);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003682}