]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/block/drbd/drbd_nl.c
ipsec: Use skcipher and ahash when probing algorithms
[mirror_ubuntu-zesty-kernel.git] / drivers / block / drbd / drbd_nl.c
CommitLineData
b411b363
PR
1/*
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
f88c5d90
LE
26#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
b411b363
PR
28#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>
b411b363
PR
34#include <linux/blkpg.h>
35#include <linux/cpumask.h>
36#include "drbd_int.h"
a3603a6e 37#include "drbd_protocol.h"
265be2d0 38#include "drbd_req.h"
a2972846 39#include "drbd_state_change.h"
b411b363 40#include <asm/unaligned.h>
b411b363 41#include <linux/drbd_limits.h>
87f7be4c 42#include <linux/kthread.h>
b411b363 43
3b98c0c2
LE
44#include <net/genetlink.h>
45
46/* .doit */
47// int drbd_adm_create_resource(struct sk_buff *skb, struct genl_info *info);
48// int drbd_adm_delete_resource(struct sk_buff *skb, struct genl_info *info);
49
05a10ec7
AG
50int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info);
51int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info);
3b98c0c2 52
789c1b62
AG
53int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info);
54int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info);
85f75dd7 55int drbd_adm_down(struct sk_buff *skb, struct genl_info *info);
3b98c0c2
LE
56
57int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info);
58int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info);
f399002e 59int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info);
3b98c0c2
LE
60int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info);
61int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info);
f399002e 62int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info);
3b98c0c2
LE
63int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info);
64int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info);
65int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info);
66int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info);
67int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info);
68int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info);
69int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info);
70int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info);
71int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info);
72int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info);
73int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info);
f399002e 74int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info);
3b98c0c2
LE
75int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info);
76int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info);
77/* .dumpit */
78int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb);
a55bbd37
AG
79int drbd_adm_dump_resources(struct sk_buff *skb, struct netlink_callback *cb);
80int drbd_adm_dump_devices(struct sk_buff *skb, struct netlink_callback *cb);
81int drbd_adm_dump_devices_done(struct netlink_callback *cb);
82int drbd_adm_dump_connections(struct sk_buff *skb, struct netlink_callback *cb);
83int drbd_adm_dump_connections_done(struct netlink_callback *cb);
84int drbd_adm_dump_peer_devices(struct sk_buff *skb, struct netlink_callback *cb);
85int drbd_adm_dump_peer_devices_done(struct netlink_callback *cb);
a2972846 86int drbd_adm_get_initial_state(struct sk_buff *skb, struct netlink_callback *cb);
3b98c0c2
LE
87
88#include <linux/drbd_genl_api.h>
01b39b50 89#include "drbd_nla.h"
3b98c0c2
LE
90#include <linux/genl_magic_func.h>
91
a2972846
AG
92static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
93static atomic_t notify_genl_seq = ATOMIC_INIT(2); /* two. */
94
95DEFINE_MUTEX(notification_mutex);
96
3b98c0c2 97/* used blkdev_get_by_path, to claim our meta data device(s) */
b411b363
PR
98static char *drbd_m_holder = "Hands off! this is DRBD's meta data device.";
99
3b98c0c2
LE
100static void drbd_adm_send_reply(struct sk_buff *skb, struct genl_info *info)
101{
102 genlmsg_end(skb, genlmsg_data(nlmsg_data(nlmsg_hdr(skb))));
103 if (genlmsg_reply(skb, info))
f88c5d90 104 pr_err("error sending genl reply\n");
b411b363 105}
3b98c0c2
LE
106
107/* Used on a fresh "drbd_adm_prepare"d reply_skb, this cannot fail: The only
108 * reason it could fail was no space in skb, and there are 4k available. */
f221f4bc 109static int drbd_msg_put_info(struct sk_buff *skb, const char *info)
3b98c0c2 110{
3b98c0c2
LE
111 struct nlattr *nla;
112 int err = -EMSGSIZE;
113
114 if (!info || !info[0])
115 return 0;
116
117 nla = nla_nest_start(skb, DRBD_NLA_CFG_REPLY);
118 if (!nla)
119 return err;
120
121 err = nla_put_string(skb, T_info_text, info);
122 if (err) {
123 nla_nest_cancel(skb, nla);
124 return err;
125 } else
126 nla_nest_end(skb, nla);
127 return 0;
b411b363
PR
128}
129
3b98c0c2
LE
130/* This would be a good candidate for a "pre_doit" hook,
131 * and per-family private info->pointers.
132 * But we need to stay compatible with older kernels.
133 * If it returns successfully, adm_ctx members are valid.
9e276872
LE
134 *
135 * At this point, we still rely on the global genl_lock().
136 * If we want to avoid that, and allow "genl_family.parallel_ops", we may need
137 * to add additional synchronization against object destruction/modification.
3b98c0c2
LE
138 */
139#define DRBD_ADM_NEED_MINOR 1
44e52cfa 140#define DRBD_ADM_NEED_RESOURCE 2
089c075d 141#define DRBD_ADM_NEED_CONNECTION 4
a910b123
LE
142static int drbd_adm_prepare(struct drbd_config_context *adm_ctx,
143 struct sk_buff *skb, struct genl_info *info, unsigned flags)
3b98c0c2
LE
144{
145 struct drbd_genlmsghdr *d_in = info->userhdr;
146 const u8 cmd = info->genlhdr->cmd;
147 int err;
148
a910b123 149 memset(adm_ctx, 0, sizeof(*adm_ctx));
3b98c0c2
LE
150
151 /* genl_rcv_msg only checks for CAP_NET_ADMIN on "GENL_ADMIN_PERM" :( */
98683650 152 if (cmd != DRBD_ADM_GET_STATUS && !capable(CAP_NET_ADMIN))
3b98c0c2
LE
153 return -EPERM;
154
a910b123
LE
155 adm_ctx->reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
156 if (!adm_ctx->reply_skb) {
1e2a2551 157 err = -ENOMEM;
3b98c0c2 158 goto fail;
1e2a2551 159 }
3b98c0c2 160
a910b123 161 adm_ctx->reply_dh = genlmsg_put_reply(adm_ctx->reply_skb,
3b98c0c2
LE
162 info, &drbd_genl_family, 0, cmd);
163 /* put of a few bytes into a fresh skb of >= 4k will always succeed.
164 * but anyways */
a910b123 165 if (!adm_ctx->reply_dh) {
1e2a2551 166 err = -ENOMEM;
3b98c0c2 167 goto fail;
1e2a2551 168 }
3b98c0c2 169
a910b123
LE
170 adm_ctx->reply_dh->minor = d_in->minor;
171 adm_ctx->reply_dh->ret_code = NO_ERROR;
3b98c0c2 172
a910b123 173 adm_ctx->volume = VOLUME_UNSPECIFIED;
3b98c0c2
LE
174 if (info->attrs[DRBD_NLA_CFG_CONTEXT]) {
175 struct nlattr *nla;
176 /* parse and validate only */
f399002e 177 err = drbd_cfg_context_from_attrs(NULL, info);
3b98c0c2
LE
178 if (err)
179 goto fail;
180
181 /* It was present, and valid,
182 * copy it over to the reply skb. */
a910b123 183 err = nla_put_nohdr(adm_ctx->reply_skb,
3b98c0c2
LE
184 info->attrs[DRBD_NLA_CFG_CONTEXT]->nla_len,
185 info->attrs[DRBD_NLA_CFG_CONTEXT]);
186 if (err)
187 goto fail;
188
9e276872 189 /* and assign stuff to the adm_ctx */
3b98c0c2 190 nla = nested_attr_tb[__nla_type(T_ctx_volume)];
089c075d 191 if (nla)
a910b123 192 adm_ctx->volume = nla_get_u32(nla);
7c3063cc 193 nla = nested_attr_tb[__nla_type(T_ctx_resource_name)];
3b98c0c2 194 if (nla)
a910b123
LE
195 adm_ctx->resource_name = nla_data(nla);
196 adm_ctx->my_addr = nested_attr_tb[__nla_type(T_ctx_my_addr)];
197 adm_ctx->peer_addr = nested_attr_tb[__nla_type(T_ctx_peer_addr)];
198 if ((adm_ctx->my_addr &&
199 nla_len(adm_ctx->my_addr) > sizeof(adm_ctx->connection->my_addr)) ||
200 (adm_ctx->peer_addr &&
201 nla_len(adm_ctx->peer_addr) > sizeof(adm_ctx->connection->peer_addr))) {
089c075d
AG
202 err = -EINVAL;
203 goto fail;
204 }
205 }
3b98c0c2 206
a910b123
LE
207 adm_ctx->minor = d_in->minor;
208 adm_ctx->device = minor_to_device(d_in->minor);
9e276872
LE
209
210 /* We are protected by the global genl_lock().
211 * But we may explicitly drop it/retake it in drbd_adm_set_role(),
212 * so make sure this object stays around. */
213 if (adm_ctx->device)
214 kref_get(&adm_ctx->device->kref);
215
a910b123
LE
216 if (adm_ctx->resource_name) {
217 adm_ctx->resource = drbd_find_resource(adm_ctx->resource_name);
4bc76048 218 }
3b98c0c2 219
a910b123
LE
220 if (!adm_ctx->device && (flags & DRBD_ADM_NEED_MINOR)) {
221 drbd_msg_put_info(adm_ctx->reply_skb, "unknown minor");
3b98c0c2
LE
222 return ERR_MINOR_INVALID;
223 }
a910b123
LE
224 if (!adm_ctx->resource && (flags & DRBD_ADM_NEED_RESOURCE)) {
225 drbd_msg_put_info(adm_ctx->reply_skb, "unknown resource");
226 if (adm_ctx->resource_name)
a10f6b8a 227 return ERR_RES_NOT_KNOWN;
3b98c0c2
LE
228 return ERR_INVALID_REQUEST;
229 }
230
089c075d 231 if (flags & DRBD_ADM_NEED_CONNECTION) {
a910b123
LE
232 if (adm_ctx->resource) {
233 drbd_msg_put_info(adm_ctx->reply_skb, "no resource name expected");
089c075d
AG
234 return ERR_INVALID_REQUEST;
235 }
a910b123
LE
236 if (adm_ctx->device) {
237 drbd_msg_put_info(adm_ctx->reply_skb, "no minor number expected");
089c075d
AG
238 return ERR_INVALID_REQUEST;
239 }
a910b123
LE
240 if (adm_ctx->my_addr && adm_ctx->peer_addr)
241 adm_ctx->connection = conn_get_by_addrs(nla_data(adm_ctx->my_addr),
242 nla_len(adm_ctx->my_addr),
243 nla_data(adm_ctx->peer_addr),
244 nla_len(adm_ctx->peer_addr));
245 if (!adm_ctx->connection) {
246 drbd_msg_put_info(adm_ctx->reply_skb, "unknown connection");
089c075d
AG
247 return ERR_INVALID_REQUEST;
248 }
249 }
250
3b98c0c2 251 /* some more paranoia, if the request was over-determined */
a910b123
LE
252 if (adm_ctx->device && adm_ctx->resource &&
253 adm_ctx->device->resource != adm_ctx->resource) {
4bc76048 254 pr_warning("request: minor=%u, resource=%s; but that minor belongs to resource %s\n",
a910b123
LE
255 adm_ctx->minor, adm_ctx->resource->name,
256 adm_ctx->device->resource->name);
257 drbd_msg_put_info(adm_ctx->reply_skb, "minor exists in different resource");
527f4b24
LE
258 return ERR_INVALID_REQUEST;
259 }
a910b123
LE
260 if (adm_ctx->device &&
261 adm_ctx->volume != VOLUME_UNSPECIFIED &&
262 adm_ctx->volume != adm_ctx->device->vnr) {
3b98c0c2 263 pr_warning("request: minor=%u, volume=%u; but that minor is volume %u in %s\n",
a910b123
LE
264 adm_ctx->minor, adm_ctx->volume,
265 adm_ctx->device->vnr,
266 adm_ctx->device->resource->name);
267 drbd_msg_put_info(adm_ctx->reply_skb, "minor exists as different volume");
3b98c0c2
LE
268 return ERR_INVALID_REQUEST;
269 }
0ace9dfa 270
9e276872
LE
271 /* still, provide adm_ctx->resource always, if possible. */
272 if (!adm_ctx->resource) {
273 adm_ctx->resource = adm_ctx->device ? adm_ctx->device->resource
274 : adm_ctx->connection ? adm_ctx->connection->resource : NULL;
275 if (adm_ctx->resource)
276 kref_get(&adm_ctx->resource->kref);
277 }
278
3b98c0c2
LE
279 return NO_ERROR;
280
281fail:
a910b123
LE
282 nlmsg_free(adm_ctx->reply_skb);
283 adm_ctx->reply_skb = NULL;
1e2a2551 284 return err;
3b98c0c2
LE
285}
286
a910b123
LE
287static int drbd_adm_finish(struct drbd_config_context *adm_ctx,
288 struct genl_info *info, int retcode)
3b98c0c2 289{
9e276872
LE
290 if (adm_ctx->device) {
291 kref_put(&adm_ctx->device->kref, drbd_destroy_device);
292 adm_ctx->device = NULL;
293 }
a910b123
LE
294 if (adm_ctx->connection) {
295 kref_put(&adm_ctx->connection->kref, &drbd_destroy_connection);
296 adm_ctx->connection = NULL;
0ace9dfa 297 }
a910b123
LE
298 if (adm_ctx->resource) {
299 kref_put(&adm_ctx->resource->kref, drbd_destroy_resource);
300 adm_ctx->resource = NULL;
4bc76048 301 }
0ace9dfa 302
a910b123 303 if (!adm_ctx->reply_skb)
3b98c0c2
LE
304 return -ENOMEM;
305
a910b123
LE
306 adm_ctx->reply_dh->ret_code = retcode;
307 drbd_adm_send_reply(adm_ctx->reply_skb, info);
3b98c0c2
LE
308 return 0;
309}
b411b363 310
bde89a9e 311static void setup_khelper_env(struct drbd_connection *connection, char **envp)
b411b363 312{
6b75dced 313 char *afs;
b411b363 314
089c075d 315 /* FIXME: A future version will not allow this case. */
bde89a9e 316 if (connection->my_addr_len == 0 || connection->peer_addr_len == 0)
089c075d
AG
317 return;
318
bde89a9e 319 switch (((struct sockaddr *)&connection->peer_addr)->sa_family) {
089c075d
AG
320 case AF_INET6:
321 afs = "ipv6";
322 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
bde89a9e 323 &((struct sockaddr_in6 *)&connection->peer_addr)->sin6_addr);
b411b363 324 break;
089c075d
AG
325 case AF_INET:
326 afs = "ipv4";
327 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
bde89a9e 328 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
b411b363 329 break;
089c075d
AG
330 default:
331 afs = "ssocks";
332 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
bde89a9e 333 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
b411b363 334 }
089c075d 335 snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
6b75dced 336}
b411b363 337
b30ab791 338int drbd_khelper(struct drbd_device *device, char *cmd)
b411b363
PR
339{
340 char *envp[] = { "HOME=/",
341 "TERM=linux",
342 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
6b75dced
PR
343 (char[20]) { }, /* address family */
344 (char[60]) { }, /* address */
b411b363 345 NULL };
6b75dced 346 char mb[12];
b411b363 347 char *argv[] = {usermode_helper, cmd, mb, NULL };
a6b32bc3 348 struct drbd_connection *connection = first_peer_device(device)->connection;
6b75dced 349 struct sib_info sib;
b411b363
PR
350 int ret;
351
bde89a9e
AG
352 if (current == connection->worker.task)
353 set_bit(CALLBACK_PENDING, &connection->flags);
c2ba686f 354
b30ab791 355 snprintf(mb, 12, "minor-%d", device_to_minor(device));
bde89a9e 356 setup_khelper_env(connection, envp);
b411b363 357
1090c056
LE
358 /* The helper may take some time.
359 * write out any unsynced meta data changes now */
b30ab791 360 drbd_md_sync(device);
1090c056 361
d0180171 362 drbd_info(device, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
3b98c0c2
LE
363 sib.sib_reason = SIB_HELPER_PRE;
364 sib.helper_name = cmd;
b30ab791 365 drbd_bcast_event(device, &sib);
a2972846 366 notify_helper(NOTIFY_CALL, device, connection, cmd, 0);
70834d30 367 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
b411b363 368 if (ret)
d0180171 369 drbd_warn(device, "helper command: %s %s %s exit code %u (0x%x)\n",
b411b363
PR
370 usermode_helper, cmd, mb,
371 (ret >> 8) & 0xff, ret);
372 else
d0180171 373 drbd_info(device, "helper command: %s %s %s exit code %u (0x%x)\n",
b411b363
PR
374 usermode_helper, cmd, mb,
375 (ret >> 8) & 0xff, ret);
3b98c0c2
LE
376 sib.sib_reason = SIB_HELPER_POST;
377 sib.helper_exit_code = ret;
b30ab791 378 drbd_bcast_event(device, &sib);
a2972846 379 notify_helper(NOTIFY_RESPONSE, device, connection, cmd, ret);
b411b363 380
bde89a9e
AG
381 if (current == connection->worker.task)
382 clear_bit(CALLBACK_PENDING, &connection->flags);
b411b363
PR
383
384 if (ret < 0) /* Ignore any ERRNOs we got. */
385 ret = 0;
386
387 return ret;
388}
389
bde89a9e 390static int conn_khelper(struct drbd_connection *connection, char *cmd)
6b75dced
PR
391{
392 char *envp[] = { "HOME=/",
393 "TERM=linux",
394 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
395 (char[20]) { }, /* address family */
396 (char[60]) { }, /* address */
397 NULL };
77c556f6
AG
398 char *resource_name = connection->resource->name;
399 char *argv[] = {usermode_helper, cmd, resource_name, NULL };
6b75dced
PR
400 int ret;
401
bde89a9e
AG
402 setup_khelper_env(connection, envp);
403 conn_md_sync(connection);
6b75dced 404
1ec861eb 405 drbd_info(connection, "helper command: %s %s %s\n", usermode_helper, cmd, resource_name);
6b75dced 406 /* TODO: conn_bcast_event() ?? */
a2972846 407 notify_helper(NOTIFY_CALL, NULL, connection, cmd, 0);
6b75dced 408
98683650 409 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
6b75dced 410 if (ret)
1ec861eb 411 drbd_warn(connection, "helper command: %s %s %s exit code %u (0x%x)\n",
77c556f6 412 usermode_helper, cmd, resource_name,
6b75dced
PR
413 (ret >> 8) & 0xff, ret);
414 else
1ec861eb 415 drbd_info(connection, "helper command: %s %s %s exit code %u (0x%x)\n",
77c556f6 416 usermode_helper, cmd, resource_name,
6b75dced
PR
417 (ret >> 8) & 0xff, ret);
418 /* TODO: conn_bcast_event() ?? */
a2972846 419 notify_helper(NOTIFY_RESPONSE, NULL, connection, cmd, ret);
c2ba686f 420
b411b363
PR
421 if (ret < 0) /* Ignore any ERRNOs we got. */
422 ret = 0;
423
424 return ret;
425}
426
bde89a9e 427static enum drbd_fencing_p highest_fencing_policy(struct drbd_connection *connection)
b411b363 428{
cb703454 429 enum drbd_fencing_p fp = FP_NOT_AVAIL;
c06ece6b 430 struct drbd_peer_device *peer_device;
cb703454
PR
431 int vnr;
432
695d08fa 433 rcu_read_lock();
c06ece6b
AG
434 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
435 struct drbd_device *device = peer_device->device;
b30ab791 436 if (get_ldev_if_state(device, D_CONSISTENT)) {
c06ece6b
AG
437 struct disk_conf *disk_conf =
438 rcu_dereference(peer_device->device->ldev->disk_conf);
439 fp = max_t(enum drbd_fencing_p, fp, disk_conf->fencing);
b30ab791 440 put_ldev(device);
cb703454
PR
441 }
442 }
695d08fa 443 rcu_read_unlock();
cb703454 444
d7fe69c6
PR
445 if (fp == FP_NOT_AVAIL) {
446 /* IO Suspending works on the whole resource.
447 Do it only for one device. */
448 vnr = 0;
449 peer_device = idr_get_next(&connection->peer_devices, &vnr);
450 drbd_change_state(peer_device->device, CS_VERBOSE | CS_HARD, NS(susp_fen, 0));
451 }
452
cb703454
PR
453 return fp;
454}
455
bde89a9e 456bool conn_try_outdate_peer(struct drbd_connection *connection)
b411b363 457{
28e448bb 458 unsigned int connect_cnt;
cb703454
PR
459 union drbd_state mask = { };
460 union drbd_state val = { };
461 enum drbd_fencing_p fp;
b411b363
PR
462 char *ex_to_string;
463 int r;
b411b363 464
02df6fe1 465 spin_lock_irq(&connection->resource->req_lock);
bde89a9e 466 if (connection->cstate >= C_WF_REPORT_PARAMS) {
1ec861eb 467 drbd_err(connection, "Expected cstate < C_WF_REPORT_PARAMS\n");
02df6fe1 468 spin_unlock_irq(&connection->resource->req_lock);
cb703454
PR
469 return false;
470 }
b411b363 471
bde89a9e 472 connect_cnt = connection->connect_cnt;
0500813f 473 spin_unlock_irq(&connection->resource->req_lock);
28e448bb 474
bde89a9e 475 fp = highest_fencing_policy(connection);
cb703454
PR
476 switch (fp) {
477 case FP_NOT_AVAIL:
1ec861eb 478 drbd_warn(connection, "Not fencing peer, I'm not even Consistent myself.\n");
fb22c402 479 goto out;
cb703454
PR
480 case FP_DONT_CARE:
481 return true;
482 default: ;
b411b363
PR
483 }
484
bde89a9e 485 r = conn_khelper(connection, "fence-peer");
b411b363
PR
486
487 switch ((r>>8) & 0xff) {
488 case 3: /* peer is inconsistent */
489 ex_to_string = "peer is inconsistent or worse";
cb703454
PR
490 mask.pdsk = D_MASK;
491 val.pdsk = D_INCONSISTENT;
b411b363
PR
492 break;
493 case 4: /* peer got outdated, or was already outdated */
494 ex_to_string = "peer was fenced";
cb703454
PR
495 mask.pdsk = D_MASK;
496 val.pdsk = D_OUTDATED;
b411b363
PR
497 break;
498 case 5: /* peer was down */
bde89a9e 499 if (conn_highest_disk(connection) == D_UP_TO_DATE) {
b411b363
PR
500 /* we will(have) create(d) a new UUID anyways... */
501 ex_to_string = "peer is unreachable, assumed to be dead";
cb703454
PR
502 mask.pdsk = D_MASK;
503 val.pdsk = D_OUTDATED;
b411b363
PR
504 } else {
505 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
b411b363
PR
506 }
507 break;
508 case 6: /* Peer is primary, voluntarily outdate myself.
509 * This is useful when an unconnected R_SECONDARY is asked to
510 * become R_PRIMARY, but finds the other peer being active. */
511 ex_to_string = "peer is active";
1ec861eb 512 drbd_warn(connection, "Peer is primary, outdating myself.\n");
cb703454
PR
513 mask.disk = D_MASK;
514 val.disk = D_OUTDATED;
b411b363
PR
515 break;
516 case 7:
517 if (fp != FP_STONITH)
1ec861eb 518 drbd_err(connection, "fence-peer() = 7 && fencing != Stonith !!!\n");
b411b363 519 ex_to_string = "peer was stonithed";
cb703454
PR
520 mask.pdsk = D_MASK;
521 val.pdsk = D_OUTDATED;
b411b363
PR
522 break;
523 default:
524 /* The script is broken ... */
1ec861eb 525 drbd_err(connection, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
cb703454 526 return false; /* Eventually leave IO frozen */
b411b363
PR
527 }
528
1ec861eb 529 drbd_info(connection, "fence-peer helper returned %d (%s)\n",
cb703454 530 (r>>8) & 0xff, ex_to_string);
fb22c402 531
cb703454 532 out:
fb22c402 533
cb703454 534 /* Not using
bde89a9e 535 conn_request_state(connection, mask, val, CS_VERBOSE);
cb703454
PR
536 here, because we might were able to re-establish the connection in the
537 meantime. */
0500813f 538 spin_lock_irq(&connection->resource->req_lock);
bde89a9e
AG
539 if (connection->cstate < C_WF_REPORT_PARAMS && !test_bit(STATE_SENT, &connection->flags)) {
540 if (connection->connect_cnt != connect_cnt)
28e448bb
PR
541 /* In case the connection was established and droped
542 while the fence-peer handler was running, ignore it */
1ec861eb 543 drbd_info(connection, "Ignoring fence-peer exit code\n");
28e448bb 544 else
bde89a9e 545 _conn_request_state(connection, mask, val, CS_VERBOSE);
28e448bb 546 }
0500813f 547 spin_unlock_irq(&connection->resource->req_lock);
cb703454 548
bde89a9e 549 return conn_highest_pdsk(connection) <= D_OUTDATED;
b411b363
PR
550}
551
87f7be4c
PR
552static int _try_outdate_peer_async(void *data)
553{
bde89a9e 554 struct drbd_connection *connection = (struct drbd_connection *)data;
87f7be4c 555
bde89a9e 556 conn_try_outdate_peer(connection);
87f7be4c 557
05a10ec7 558 kref_put(&connection->kref, drbd_destroy_connection);
87f7be4c
PR
559 return 0;
560}
561
bde89a9e 562void conn_try_outdate_peer_async(struct drbd_connection *connection)
87f7be4c
PR
563{
564 struct task_struct *opa;
565
bde89a9e 566 kref_get(&connection->kref);
bbc1c5e8
LE
567 /* We may just have force_sig()'ed this thread
568 * to get it out of some blocking network function.
569 * Clear signals; otherwise kthread_run(), which internally uses
570 * wait_on_completion_killable(), will mistake our pending signal
571 * for a new fatal signal and fail. */
572 flush_signals(current);
bde89a9e 573 opa = kthread_run(_try_outdate_peer_async, connection, "drbd_async_h");
9dc9fbb3 574 if (IS_ERR(opa)) {
1ec861eb 575 drbd_err(connection, "out of mem, failed to invoke fence-peer helper\n");
05a10ec7 576 kref_put(&connection->kref, drbd_destroy_connection);
9dc9fbb3 577 }
87f7be4c 578}
b411b363 579
bf885f8a 580enum drbd_state_rv
44a4d551 581drbd_set_role(struct drbd_device *const device, enum drbd_role new_role, int force)
b411b363 582{
44a4d551
LE
583 struct drbd_peer_device *const peer_device = first_peer_device(device);
584 struct drbd_connection *const connection = peer_device ? peer_device->connection : NULL;
b411b363 585 const int max_tries = 4;
bf885f8a 586 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
44ed167d 587 struct net_conf *nc;
b411b363
PR
588 int try = 0;
589 int forced = 0;
590 union drbd_state mask, val;
b411b363 591
b6f85ef9
AG
592 if (new_role == R_PRIMARY) {
593 struct drbd_connection *connection;
594
595 /* Detect dead peers as soon as possible. */
596
597 rcu_read_lock();
598 for_each_connection(connection, device->resource)
599 request_ping(connection);
600 rcu_read_unlock();
601 }
b411b363 602
b30ab791 603 mutex_lock(device->state_mutex);
b411b363
PR
604
605 mask.i = 0; mask.role = R_MASK;
606 val.i = 0; val.role = new_role;
607
608 while (try++ < max_tries) {
a8821531 609 rv = _drbd_request_state_holding_state_mutex(device, mask, val, CS_WAIT_COMPLETE);
b411b363
PR
610
611 /* in case we first succeeded to outdate,
612 * but now suddenly could establish a connection */
bf885f8a 613 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
b411b363
PR
614 val.pdsk = 0;
615 mask.pdsk = 0;
616 continue;
617 }
618
bf885f8a 619 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
b30ab791
AG
620 (device->state.disk < D_UP_TO_DATE &&
621 device->state.disk >= D_INCONSISTENT)) {
b411b363
PR
622 mask.disk = D_MASK;
623 val.disk = D_UP_TO_DATE;
624 forced = 1;
625 continue;
626 }
627
bf885f8a 628 if (rv == SS_NO_UP_TO_DATE_DISK &&
b30ab791 629 device->state.disk == D_CONSISTENT && mask.pdsk == 0) {
0b0ba1ef 630 D_ASSERT(device, device->state.pdsk == D_UNKNOWN);
b411b363 631
44a4d551 632 if (conn_try_outdate_peer(connection)) {
b411b363
PR
633 val.disk = D_UP_TO_DATE;
634 mask.disk = D_MASK;
635 }
b411b363
PR
636 continue;
637 }
638
bf885f8a 639 if (rv == SS_NOTHING_TO_DO)
3b98c0c2 640 goto out;
bf885f8a 641 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
44a4d551 642 if (!conn_try_outdate_peer(connection) && force) {
d0180171 643 drbd_warn(device, "Forced into split brain situation!\n");
cb703454
PR
644 mask.pdsk = D_MASK;
645 val.pdsk = D_OUTDATED;
b411b363 646
cb703454 647 }
b411b363
PR
648 continue;
649 }
bf885f8a 650 if (rv == SS_TWO_PRIMARIES) {
b411b363
PR
651 /* Maybe the peer is detected as dead very soon...
652 retry at most once more in this case. */
44ed167d
PR
653 int timeo;
654 rcu_read_lock();
44a4d551 655 nc = rcu_dereference(connection->net_conf);
44ed167d
PR
656 timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
657 rcu_read_unlock();
658 schedule_timeout_interruptible(timeo);
b411b363
PR
659 if (try < max_tries)
660 try = max_tries - 1;
661 continue;
662 }
bf885f8a 663 if (rv < SS_SUCCESS) {
b30ab791 664 rv = _drbd_request_state(device, mask, val,
b411b363 665 CS_VERBOSE + CS_WAIT_COMPLETE);
bf885f8a 666 if (rv < SS_SUCCESS)
3b98c0c2 667 goto out;
b411b363
PR
668 }
669 break;
670 }
671
bf885f8a 672 if (rv < SS_SUCCESS)
3b98c0c2 673 goto out;
b411b363
PR
674
675 if (forced)
d0180171 676 drbd_warn(device, "Forced to consider local data as UpToDate!\n");
b411b363
PR
677
678 /* Wait until nothing is on the fly :) */
b30ab791 679 wait_event(device->misc_wait, atomic_read(&device->ap_pending_cnt) == 0);
b411b363 680
b6dd1a89
LE
681 /* FIXME also wait for all pending P_BARRIER_ACK? */
682
b411b363 683 if (new_role == R_SECONDARY) {
b30ab791
AG
684 if (get_ldev(device)) {
685 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
686 put_ldev(device);
b411b363
PR
687 }
688 } else {
66ce6dbc 689 mutex_lock(&device->resource->conf_update);
44a4d551 690 nc = connection->net_conf;
44ed167d 691 if (nc)
6139f60d 692 nc->discard_my_data = 0; /* without copy; single bit op is atomic */
66ce6dbc 693 mutex_unlock(&device->resource->conf_update);
91fd4dad 694
b30ab791
AG
695 if (get_ldev(device)) {
696 if (((device->state.conn < C_CONNECTED ||
697 device->state.pdsk <= D_FAILED)
698 && device->ldev->md.uuid[UI_BITMAP] == 0) || forced)
699 drbd_uuid_new_current(device);
b411b363 700
b30ab791
AG
701 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
702 put_ldev(device);
b411b363
PR
703 }
704 }
705
19f843aa
LE
706 /* writeout of activity log covered areas of the bitmap
707 * to stable storage done in after state change already */
b411b363 708
b30ab791 709 if (device->state.conn >= C_WF_REPORT_PARAMS) {
b411b363
PR
710 /* if this was forced, we should consider sync */
711 if (forced)
44a4d551
LE
712 drbd_send_uuids(peer_device);
713 drbd_send_current_state(peer_device);
b411b363
PR
714 }
715
b30ab791 716 drbd_md_sync(device);
720979fb 717 set_disk_ro(device->vdisk, new_role == R_SECONDARY);
b30ab791 718 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
3b98c0c2 719out:
b30ab791 720 mutex_unlock(device->state_mutex);
bf885f8a 721 return rv;
b411b363
PR
722}
723
3b98c0c2 724static const char *from_attrs_err_to_txt(int err)
ef50a3e3 725{
3b98c0c2
LE
726 return err == -ENOMSG ? "required attribute missing" :
727 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
f399002e 728 err == -EEXIST ? "can not change invariant setting" :
3b98c0c2 729 "invalid attribute value";
ef50a3e3 730}
b411b363 731
3b98c0c2 732int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
b411b363 733{
a910b123 734 struct drbd_config_context adm_ctx;
3b98c0c2
LE
735 struct set_role_parms parms;
736 int err;
737 enum drbd_ret_code retcode;
b411b363 738
a910b123 739 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
740 if (!adm_ctx.reply_skb)
741 return retcode;
742 if (retcode != NO_ERROR)
743 goto out;
b411b363 744
3b98c0c2
LE
745 memset(&parms, 0, sizeof(parms));
746 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
f399002e 747 err = set_role_parms_from_attrs(&parms, info);
3b98c0c2
LE
748 if (err) {
749 retcode = ERR_MANDATORY_TAG;
a910b123 750 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
3b98c0c2
LE
751 goto out;
752 }
753 }
9e276872
LE
754 genl_unlock();
755 mutex_lock(&adm_ctx.resource->adm_mutex);
b411b363 756
3b98c0c2 757 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
b30ab791 758 retcode = drbd_set_role(adm_ctx.device, R_PRIMARY, parms.assume_uptodate);
3b98c0c2 759 else
b30ab791 760 retcode = drbd_set_role(adm_ctx.device, R_SECONDARY, 0);
9e276872
LE
761
762 mutex_unlock(&adm_ctx.resource->adm_mutex);
763 genl_lock();
3b98c0c2 764out:
a910b123 765 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
766 return 0;
767}
768
ae8bf312
LE
769/* Initializes the md.*_offset members, so we are able to find
770 * the on disk meta data.
771 *
772 * We currently have two possible layouts:
773 * external:
774 * |----------- md_size_sect ------------------|
775 * [ 4k superblock ][ activity log ][ Bitmap ]
776 * | al_offset == 8 |
777 * | bm_offset = al_offset + X |
778 * ==> bitmap sectors = md_size_sect - bm_offset
779 *
780 * internal:
781 * |----------- md_size_sect ------------------|
782 * [data.....][ Bitmap ][ activity log ][ 4k superblock ]
783 * | al_offset < 0 |
784 * | bm_offset = al_offset - Y |
785 * ==> bitmap sectors = Y = al_offset - bm_offset
786 *
787 * Activity log size used to be fixed 32kB,
788 * but is about to become configurable.
789 */
b30ab791 790static void drbd_md_set_sector_offsets(struct drbd_device *device,
b411b363
PR
791 struct drbd_backing_dev *bdev)
792{
793 sector_t md_size_sect = 0;
c04ccaa6 794 unsigned int al_size_sect = bdev->md.al_size_4k * 8;
daeda1cc 795
3a4d4eb3
LE
796 bdev->md.md_offset = drbd_md_ss(bdev);
797
68e41a43 798 switch (bdev->md.meta_dev_idx) {
b411b363
PR
799 default:
800 /* v07 style fixed size indexed meta data */
ae8bf312 801 bdev->md.md_size_sect = MD_128MB_SECT;
ae8bf312
LE
802 bdev->md.al_offset = MD_4kB_SECT;
803 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
b411b363
PR
804 break;
805 case DRBD_MD_INDEX_FLEX_EXT:
806 /* just occupy the full device; unit: sectors */
807 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
ae8bf312
LE
808 bdev->md.al_offset = MD_4kB_SECT;
809 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
b411b363
PR
810 break;
811 case DRBD_MD_INDEX_INTERNAL:
812 case DRBD_MD_INDEX_FLEX_INT:
b411b363 813 /* al size is still fixed */
ae8bf312 814 bdev->md.al_offset = -al_size_sect;
b411b363
PR
815 /* we need (slightly less than) ~ this much bitmap sectors: */
816 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
817 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
818 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
819 md_size_sect = ALIGN(md_size_sect, 8);
820
821 /* plus the "drbd meta data super block",
822 * and the activity log; */
ae8bf312 823 md_size_sect += MD_4kB_SECT + al_size_sect;
b411b363
PR
824
825 bdev->md.md_size_sect = md_size_sect;
826 /* bitmap offset is adjusted by 'super' block size */
ae8bf312 827 bdev->md.bm_offset = -md_size_sect + MD_4kB_SECT;
b411b363
PR
828 break;
829 }
830}
831
4b0715f0 832/* input size is expected to be in KB */
b411b363
PR
833char *ppsize(char *buf, unsigned long long size)
834{
4b0715f0
LE
835 /* Needs 9 bytes at max including trailing NUL:
836 * -1ULL ==> "16384 EB" */
b411b363
PR
837 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
838 int base = 0;
4b0715f0 839 while (size >= 10000 && base < sizeof(units)-1) {
b411b363
PR
840 /* shift + round */
841 size = (size >> 10) + !!(size & (1<<9));
842 base++;
843 }
4b0715f0 844 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
b411b363
PR
845
846 return buf;
847}
848
849/* there is still a theoretical deadlock when called from receiver
850 * on an D_INCONSISTENT R_PRIMARY:
851 * remote READ does inc_ap_bio, receiver would need to receive answer
852 * packet from remote to dec_ap_bio again.
853 * receiver receive_sizes(), comes here,
854 * waits for ap_bio_cnt == 0. -> deadlock.
855 * but this cannot happen, actually, because:
856 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
857 * (not connected, or bad/no disk on peer):
858 * see drbd_fail_request_early, ap_bio_cnt is zero.
859 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
860 * peer may not initiate a resize.
861 */
3b98c0c2
LE
862/* Note these are not to be confused with
863 * drbd_adm_suspend_io/drbd_adm_resume_io,
864 * which are (sub) state changes triggered by admin (drbdsetup),
865 * and can be long lived.
b30ab791 866 * This changes an device->flag, is triggered by drbd internals,
3b98c0c2 867 * and should be short-lived. */
7dbb4386
PR
868/* It needs to be a counter, since multiple threads might
869 independently suspend and resume IO. */
b30ab791 870void drbd_suspend_io(struct drbd_device *device)
b411b363 871{
7dbb4386 872 atomic_inc(&device->suspend_cnt);
b30ab791 873 if (drbd_suspended(device))
265be2d0 874 return;
b30ab791 875 wait_event(device->misc_wait, !atomic_read(&device->ap_bio_cnt));
b411b363
PR
876}
877
b30ab791 878void drbd_resume_io(struct drbd_device *device)
b411b363 879{
7dbb4386
PR
880 if (atomic_dec_and_test(&device->suspend_cnt))
881 wake_up(&device->misc_wait);
b411b363
PR
882}
883
884/**
885 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
b30ab791 886 * @device: DRBD device.
b411b363
PR
887 *
888 * Returns 0 on success, negative return values indicate errors.
889 * You should call drbd_md_sync() after calling this function.
890 */
d752b269 891enum determine_dev_size
b30ab791 892drbd_determine_dev_size(struct drbd_device *device, enum dds_flags flags, struct resize_parms *rs) __must_hold(local)
b411b363 893{
8011e249
LE
894 struct md_offsets_and_sizes {
895 u64 last_agreed_sect;
896 u64 md_offset;
897 s32 al_offset;
898 s32 bm_offset;
899 u32 md_size_sect;
900
901 u32 al_stripes;
902 u32 al_stripe_size_4k;
903 } prev;
904 sector_t u_size, size;
b30ab791 905 struct drbd_md *md = &device->ldev->md;
b411b363 906 char ppb[10];
d752b269 907 void *buffer;
b411b363
PR
908
909 int md_moved, la_size_changed;
e96c9633 910 enum determine_dev_size rv = DS_UNCHANGED;
b411b363 911
5f7c0124
LE
912 /* We may change the on-disk offsets of our meta data below. Lock out
913 * anything that may cause meta data IO, to avoid acting on incomplete
914 * layout changes or scribbling over meta data that is in the process
915 * of being moved.
b411b363 916 *
5f7c0124
LE
917 * Move is not exactly correct, btw, currently we have all our meta
918 * data in core memory, to "move" it we just write it all out, there
919 * are no reads. */
b30ab791 920 drbd_suspend_io(device);
e37d2438 921 buffer = drbd_md_get_buffer(device, __func__); /* Lock meta-data IO */
d752b269 922 if (!buffer) {
b30ab791 923 drbd_resume_io(device);
d752b269
PR
924 return DS_ERROR;
925 }
b411b363 926
8011e249
LE
927 /* remember current offset and sizes */
928 prev.last_agreed_sect = md->la_size_sect;
929 prev.md_offset = md->md_offset;
930 prev.al_offset = md->al_offset;
931 prev.bm_offset = md->bm_offset;
932 prev.md_size_sect = md->md_size_sect;
933 prev.al_stripes = md->al_stripes;
934 prev.al_stripe_size_4k = md->al_stripe_size_4k;
b411b363 935
d752b269
PR
936 if (rs) {
937 /* rs is non NULL if we should change the AL layout only */
d752b269
PR
938 md->al_stripes = rs->al_stripes;
939 md->al_stripe_size_4k = rs->al_stripe_size / 4;
940 md->al_size_4k = (u64)rs->al_stripes * rs->al_stripe_size / 4;
941 }
942
b30ab791 943 drbd_md_set_sector_offsets(device, device->ldev);
b411b363 944
daeda1cc 945 rcu_read_lock();
b30ab791 946 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
daeda1cc 947 rcu_read_unlock();
b30ab791 948 size = drbd_new_dev_size(device, device->ldev, u_size, flags & DDSF_FORCED);
b411b363 949
8011e249 950 if (size < prev.last_agreed_sect) {
d752b269
PR
951 if (rs && u_size == 0) {
952 /* Remove "rs &&" later. This check should always be active, but
953 right now the receiver expects the permissive behavior */
d0180171 954 drbd_warn(device, "Implicit shrink not allowed. "
d752b269
PR
955 "Use --size=%llus for explicit shrink.\n",
956 (unsigned long long)size);
957 rv = DS_ERROR_SHRINK;
958 }
959 if (u_size > size)
960 rv = DS_ERROR_SPACE_MD;
961 if (rv != DS_UNCHANGED)
962 goto err_out;
963 }
964
b30ab791
AG
965 if (drbd_get_capacity(device->this_bdev) != size ||
966 drbd_bm_capacity(device) != size) {
b411b363 967 int err;
b30ab791 968 err = drbd_bm_resize(device, size, !(flags & DDSF_NO_RESYNC));
b411b363
PR
969 if (unlikely(err)) {
970 /* currently there is only one error: ENOMEM! */
8011e249 971 size = drbd_bm_capacity(device);
b411b363 972 if (size == 0) {
d0180171 973 drbd_err(device, "OUT OF MEMORY! "
b411b363
PR
974 "Could not allocate bitmap!\n");
975 } else {
d0180171 976 drbd_err(device, "BM resizing failed. "
8011e249 977 "Leaving size unchanged\n");
b411b363 978 }
e96c9633 979 rv = DS_ERROR;
b411b363
PR
980 }
981 /* racy, see comments above. */
b30ab791 982 drbd_set_my_capacity(device, size);
8011e249 983 md->la_size_sect = size;
d0180171 984 drbd_info(device, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
b411b363
PR
985 (unsigned long long)size>>1);
986 }
d752b269
PR
987 if (rv <= DS_ERROR)
988 goto err_out;
b411b363 989
8011e249 990 la_size_changed = (prev.last_agreed_sect != md->la_size_sect);
b411b363 991
8011e249
LE
992 md_moved = prev.md_offset != md->md_offset
993 || prev.md_size_sect != md->md_size_sect;
b411b363 994
d752b269
PR
995 if (la_size_changed || md_moved || rs) {
996 u32 prev_flags;
24dccabb 997
fcb09674
LE
998 /* We do some synchronous IO below, which may take some time.
999 * Clear the timer, to avoid scary "timer expired!" messages,
1000 * "Superblock" is written out at least twice below, anyways. */
1001 del_timer(&device->md_sync_timer);
d752b269 1002
5f7c0124
LE
1003 /* We won't change the "al-extents" setting, we just may need
1004 * to move the on-disk location of the activity log ringbuffer.
1005 * Lock for transaction is good enough, it may well be "dirty"
1006 * or even "starving". */
1007 wait_event(device->al_wait, lc_try_lock_for_transaction(device->act_log));
1008
1009 /* mark current on-disk bitmap and activity log as unreliable */
d752b269 1010 prev_flags = md->flags;
5f7c0124 1011 md->flags |= MDF_FULL_SYNC | MDF_AL_DISABLED;
b30ab791 1012 drbd_md_write(device, buffer);
d752b269 1013
5f7c0124
LE
1014 drbd_al_initialize(device, buffer);
1015
d0180171 1016 drbd_info(device, "Writing the whole bitmap, %s\n",
b411b363
PR
1017 la_size_changed && md_moved ? "size changed and md moved" :
1018 la_size_changed ? "size changed" : "md moved");
20ceb2b2 1019 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
b30ab791 1020 drbd_bitmap_io(device, md_moved ? &drbd_bm_write_all : &drbd_bm_write,
d752b269 1021 "size changed", BM_LOCKED_MASK);
d752b269 1022
5f7c0124
LE
1023 /* on-disk bitmap and activity log is authoritative again
1024 * (unless there was an IO error meanwhile...) */
d752b269 1025 md->flags = prev_flags;
b30ab791 1026 drbd_md_write(device, buffer);
d752b269
PR
1027
1028 if (rs)
d0180171
AG
1029 drbd_info(device, "Changed AL layout to al-stripes = %d, al-stripe-size-kB = %d\n",
1030 md->al_stripes, md->al_stripe_size_4k * 4);
b411b363
PR
1031 }
1032
8011e249
LE
1033 if (size > prev.last_agreed_sect)
1034 rv = prev.last_agreed_sect ? DS_GREW : DS_GREW_FROM_ZERO;
1035 if (size < prev.last_agreed_sect)
e96c9633 1036 rv = DS_SHRUNK;
d752b269
PR
1037
1038 if (0) {
1039 err_out:
8011e249
LE
1040 /* restore previous offset and sizes */
1041 md->la_size_sect = prev.last_agreed_sect;
1042 md->md_offset = prev.md_offset;
1043 md->al_offset = prev.al_offset;
1044 md->bm_offset = prev.bm_offset;
1045 md->md_size_sect = prev.md_size_sect;
1046 md->al_stripes = prev.al_stripes;
1047 md->al_stripe_size_4k = prev.al_stripe_size_4k;
1048 md->al_size_4k = (u64)prev.al_stripes * prev.al_stripe_size_4k;
d752b269 1049 }
b30ab791
AG
1050 lc_unlock(device->act_log);
1051 wake_up(&device->al_wait);
1052 drbd_md_put_buffer(device);
1053 drbd_resume_io(device);
b411b363
PR
1054
1055 return rv;
1056}
1057
1058sector_t
b30ab791 1059drbd_new_dev_size(struct drbd_device *device, struct drbd_backing_dev *bdev,
ef5e44a6 1060 sector_t u_size, int assume_peer_has_space)
b411b363 1061{
b30ab791 1062 sector_t p_size = device->p_size; /* partner's disk size. */
cccac985 1063 sector_t la_size_sect = bdev->md.la_size_sect; /* last agreed size. */
b411b363 1064 sector_t m_size; /* my size */
b411b363
PR
1065 sector_t size = 0;
1066
1067 m_size = drbd_get_max_capacity(bdev);
1068
b30ab791 1069 if (device->state.conn < C_CONNECTED && assume_peer_has_space) {
d0180171 1070 drbd_warn(device, "Resize while not connected was forced by the user!\n");
a393db6f
PR
1071 p_size = m_size;
1072 }
1073
b411b363
PR
1074 if (p_size && m_size) {
1075 size = min_t(sector_t, p_size, m_size);
1076 } else {
cccac985
LE
1077 if (la_size_sect) {
1078 size = la_size_sect;
b411b363
PR
1079 if (m_size && m_size < size)
1080 size = m_size;
1081 if (p_size && p_size < size)
1082 size = p_size;
1083 } else {
1084 if (m_size)
1085 size = m_size;
1086 if (p_size)
1087 size = p_size;
1088 }
1089 }
1090
1091 if (size == 0)
d0180171 1092 drbd_err(device, "Both nodes diskless!\n");
b411b363
PR
1093
1094 if (u_size) {
1095 if (u_size > size)
d0180171 1096 drbd_err(device, "Requested disk size is too big (%lu > %lu)\n",
b411b363
PR
1097 (unsigned long)u_size>>1, (unsigned long)size>>1);
1098 else
1099 size = u_size;
1100 }
1101
1102 return size;
1103}
1104
1105/**
1106 * drbd_check_al_size() - Ensures that the AL is of the right size
b30ab791 1107 * @device: DRBD device.
b411b363
PR
1108 *
1109 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
1110 * failed, and 0 on success. You should call drbd_md_sync() after you called
1111 * this function.
1112 */
b30ab791 1113static int drbd_check_al_size(struct drbd_device *device, struct disk_conf *dc)
b411b363
PR
1114{
1115 struct lru_cache *n, *t;
1116 struct lc_element *e;
1117 unsigned int in_use;
1118 int i;
1119
b30ab791
AG
1120 if (device->act_log &&
1121 device->act_log->nr_elements == dc->al_extents)
b411b363
PR
1122 return 0;
1123
1124 in_use = 0;
b30ab791 1125 t = device->act_log;
7ad651b5 1126 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
f399002e 1127 dc->al_extents, sizeof(struct lc_element), 0);
b411b363
PR
1128
1129 if (n == NULL) {
d0180171 1130 drbd_err(device, "Cannot allocate act_log lru!\n");
b411b363
PR
1131 return -ENOMEM;
1132 }
b30ab791 1133 spin_lock_irq(&device->al_lock);
b411b363
PR
1134 if (t) {
1135 for (i = 0; i < t->nr_elements; i++) {
1136 e = lc_element_by_index(t, i);
1137 if (e->refcnt)
d0180171 1138 drbd_err(device, "refcnt(%d)==%d\n",
b411b363
PR
1139 e->lc_number, e->refcnt);
1140 in_use += e->refcnt;
1141 }
1142 }
1143 if (!in_use)
b30ab791
AG
1144 device->act_log = n;
1145 spin_unlock_irq(&device->al_lock);
b411b363 1146 if (in_use) {
d0180171 1147 drbd_err(device, "Activity log still in use!\n");
b411b363
PR
1148 lc_destroy(n);
1149 return -EBUSY;
1150 } else {
d01efcee 1151 lc_destroy(t);
b411b363 1152 }
b30ab791 1153 drbd_md_mark_dirty(device); /* we changed device->act_log->nr_elemens */
b411b363
PR
1154 return 0;
1155}
1156
8fe39aac
PR
1157static void drbd_setup_queue_param(struct drbd_device *device, struct drbd_backing_dev *bdev,
1158 unsigned int max_bio_size)
b411b363 1159{
b30ab791 1160 struct request_queue * const q = device->rq_queue;
db141b2f
LE
1161 unsigned int max_hw_sectors = max_bio_size >> 9;
1162 unsigned int max_segments = 0;
c1b3156f 1163 struct request_queue *b = NULL;
99432fcc 1164
8fe39aac
PR
1165 if (bdev) {
1166 b = bdev->backing_bdev->bd_disk->queue;
99432fcc
PR
1167
1168 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
daeda1cc 1169 rcu_read_lock();
b30ab791 1170 max_segments = rcu_dereference(device->ldev->disk_conf)->max_bio_bvecs;
daeda1cc 1171 rcu_read_unlock();
c1b3156f
PR
1172
1173 blk_set_stacking_limits(&q->limits);
1174 blk_queue_max_write_same_sectors(q, 0);
99432fcc 1175 }
b411b363 1176
b411b363 1177 blk_queue_logical_block_size(q, 512);
1816a2b4
LE
1178 blk_queue_max_hw_sectors(q, max_hw_sectors);
1179 /* This is the workaround for "bio would need to, but cannot, be split" */
1180 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
1181 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
b411b363 1182
c1b3156f 1183 if (b) {
20c68fde
LE
1184 struct drbd_connection *connection = first_peer_device(device)->connection;
1185
088b7052
LE
1186 blk_queue_max_discard_sectors(q, DRBD_MAX_DISCARD_SECTORS);
1187
20c68fde
LE
1188 if (blk_queue_discard(b) &&
1189 (connection->cstate < C_CONNECTED || connection->agreed_features & FF_TRIM)) {
088b7052
LE
1190 /* We don't care, stacking below should fix it for the local device.
1191 * Whether or not it is a suitable granularity on the remote device
1192 * is not our problem, really. If you care, you need to
1193 * use devices with similar topology on all peers. */
1194 q->limits.discard_granularity = 512;
20c68fde 1195 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
20c68fde 1196 } else {
2bb4cd5c 1197 blk_queue_max_discard_sectors(q, 0);
20c68fde 1198 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
088b7052 1199 q->limits.discard_granularity = 0;
20c68fde 1200 }
99432fcc
PR
1201
1202 blk_queue_stack_limits(q, b);
1203
1204 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
d0180171 1205 drbd_info(device, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
99432fcc
PR
1206 q->backing_dev_info.ra_pages,
1207 b->backing_dev_info.ra_pages);
1208 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1209 }
99432fcc 1210 }
088b7052
LE
1211 /* To avoid confusion, if this queue does not support discard, clear
1212 * max_discard_sectors, which is what lsblk -D reports to the user. */
1213 if (!blk_queue_discard(q)) {
1214 blk_queue_max_discard_sectors(q, 0);
1215 q->limits.discard_granularity = 0;
1216 }
99432fcc
PR
1217}
1218
8fe39aac 1219void drbd_reconsider_max_bio_size(struct drbd_device *device, struct drbd_backing_dev *bdev)
99432fcc 1220{
db141b2f 1221 unsigned int now, new, local, peer;
99432fcc 1222
b30ab791
AG
1223 now = queue_max_hw_sectors(device->rq_queue) << 9;
1224 local = device->local_max_bio_size; /* Eventually last known value, from volatile memory */
1225 peer = device->peer_max_bio_size; /* Eventually last known value, from meta data */
b411b363 1226
8fe39aac
PR
1227 if (bdev) {
1228 local = queue_max_hw_sectors(bdev->backing_bdev->bd_disk->queue) << 9;
b30ab791 1229 device->local_max_bio_size = local;
b411b363 1230 }
db141b2f 1231 local = min(local, DRBD_MAX_BIO_SIZE);
99432fcc
PR
1232
1233 /* We may ignore peer limits if the peer is modern enough.
1234 Because new from 8.3.8 onwards the peer can use multiple
1235 BIOs for a single peer_request */
b30ab791 1236 if (device->state.conn >= C_WF_REPORT_PARAMS) {
a6b32bc3 1237 if (first_peer_device(device)->connection->agreed_pro_version < 94)
b30ab791 1238 peer = min(device->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
6809384c 1239 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
a6b32bc3 1240 else if (first_peer_device(device)->connection->agreed_pro_version == 94)
99432fcc 1241 peer = DRBD_MAX_SIZE_H80_PACKET;
a6b32bc3 1242 else if (first_peer_device(device)->connection->agreed_pro_version < 100)
2ffca4f3
PR
1243 peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
1244 else
99432fcc 1245 peer = DRBD_MAX_BIO_SIZE;
99432fcc 1246
fa090e70
LE
1247 /* We may later detach and re-attach on a disconnected Primary.
1248 * Avoid this setting to jump back in that case.
1249 * We want to store what we know the peer DRBD can handle,
1250 * not what the peer IO backend can handle. */
1251 if (peer > device->peer_max_bio_size)
1252 device->peer_max_bio_size = peer;
1253 }
db141b2f 1254 new = min(local, peer);
99432fcc 1255
b30ab791 1256 if (device->state.role == R_PRIMARY && new < now)
d0180171 1257 drbd_err(device, "ASSERT FAILED new < now; (%u < %u)\n", new, now);
99432fcc
PR
1258
1259 if (new != now)
d0180171 1260 drbd_info(device, "max BIO size = %u\n", new);
99432fcc 1261
8fe39aac 1262 drbd_setup_queue_param(device, bdev, new);
b411b363
PR
1263}
1264
a18e9d1e 1265/* Starts the worker thread */
bde89a9e 1266static void conn_reconfig_start(struct drbd_connection *connection)
b411b363 1267{
bde89a9e 1268 drbd_thread_start(&connection->worker);
b5043c5e 1269 drbd_flush_workqueue(&connection->sender_work);
b411b363
PR
1270}
1271
a18e9d1e 1272/* if still unconfigured, stops worker again. */
bde89a9e 1273static void conn_reconfig_done(struct drbd_connection *connection)
b411b363 1274{
992d6e91 1275 bool stop_threads;
0500813f 1276 spin_lock_irq(&connection->resource->req_lock);
bde89a9e
AG
1277 stop_threads = conn_all_vols_unconf(connection) &&
1278 connection->cstate == C_STANDALONE;
0500813f 1279 spin_unlock_irq(&connection->resource->req_lock);
992d6e91 1280 if (stop_threads) {
668700b4
PR
1281 /* ack_receiver thread and ack_sender workqueue are implicitly
1282 * stopped by receiver in conn_disconnect() */
bde89a9e
AG
1283 drbd_thread_stop(&connection->receiver);
1284 drbd_thread_stop(&connection->worker);
992d6e91 1285 }
b411b363
PR
1286}
1287
0778286a 1288/* Make sure IO is suspended before calling this function(). */
b30ab791 1289static void drbd_suspend_al(struct drbd_device *device)
0778286a
PR
1290{
1291 int s = 0;
1292
b30ab791 1293 if (!lc_try_lock(device->act_log)) {
d0180171 1294 drbd_warn(device, "Failed to lock al in drbd_suspend_al()\n");
0778286a
PR
1295 return;
1296 }
1297
b30ab791 1298 drbd_al_shrink(device);
0500813f 1299 spin_lock_irq(&device->resource->req_lock);
b30ab791
AG
1300 if (device->state.conn < C_CONNECTED)
1301 s = !test_and_set_bit(AL_SUSPENDED, &device->flags);
0500813f 1302 spin_unlock_irq(&device->resource->req_lock);
b30ab791 1303 lc_unlock(device->act_log);
0778286a
PR
1304
1305 if (s)
d0180171 1306 drbd_info(device, "Suspended AL updates\n");
0778286a
PR
1307}
1308
5979e361
LE
1309
1310static bool should_set_defaults(struct genl_info *info)
1311{
1312 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1313 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1314}
1315
5bbcf5e6 1316static unsigned int drbd_al_extents_max(struct drbd_backing_dev *bdev)
d589a21e 1317{
5bbcf5e6
LE
1318 /* This is limited by 16 bit "slot" numbers,
1319 * and by available on-disk context storage.
1320 *
1321 * Also (u16)~0 is special (denotes a "free" extent).
1322 *
1323 * One transaction occupies one 4kB on-disk block,
1324 * we have n such blocks in the on disk ring buffer,
1325 * the "current" transaction may fail (n-1),
1326 * and there is 919 slot numbers context information per transaction.
1327 *
1328 * 72 transaction blocks amounts to more than 2**16 context slots,
1329 * so cap there first.
1330 */
1331 const unsigned int max_al_nr = DRBD_AL_EXTENTS_MAX;
1332 const unsigned int sufficient_on_disk =
1333 (max_al_nr + AL_CONTEXT_PER_TRANSACTION -1)
1334 /AL_CONTEXT_PER_TRANSACTION;
d589a21e 1335
5bbcf5e6
LE
1336 unsigned int al_size_4k = bdev->md.al_size_4k;
1337
1338 if (al_size_4k > sufficient_on_disk)
1339 return max_al_nr;
1340
1341 return (al_size_4k - 1) * AL_CONTEXT_PER_TRANSACTION;
d589a21e
PR
1342}
1343
70df7092
LE
1344static bool write_ordering_changed(struct disk_conf *a, struct disk_conf *b)
1345{
1346 return a->disk_barrier != b->disk_barrier ||
1347 a->disk_flushes != b->disk_flushes ||
1348 a->disk_drain != b->disk_drain;
1349}
1350
f399002e
LE
1351int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1352{
a910b123 1353 struct drbd_config_context adm_ctx;
f399002e 1354 enum drbd_ret_code retcode;
b30ab791 1355 struct drbd_device *device;
daeda1cc 1356 struct disk_conf *new_disk_conf, *old_disk_conf;
813472ce 1357 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
f399002e 1358 int err, fifo_size;
f399002e 1359
a910b123 1360 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
f399002e
LE
1361 if (!adm_ctx.reply_skb)
1362 return retcode;
1363 if (retcode != NO_ERROR)
9e276872 1364 goto finish;
f399002e 1365
b30ab791 1366 device = adm_ctx.device;
9e276872 1367 mutex_lock(&adm_ctx.resource->adm_mutex);
f399002e
LE
1368
1369 /* we also need a disk
1370 * to change the options on */
b30ab791 1371 if (!get_ldev(device)) {
f399002e
LE
1372 retcode = ERR_NO_DISK;
1373 goto out;
1374 }
1375
daeda1cc 1376 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
5ecc72c3 1377 if (!new_disk_conf) {
f399002e
LE
1378 retcode = ERR_NOMEM;
1379 goto fail;
1380 }
1381
0500813f 1382 mutex_lock(&device->resource->conf_update);
b30ab791 1383 old_disk_conf = device->ldev->disk_conf;
daeda1cc 1384 *new_disk_conf = *old_disk_conf;
5979e361 1385 if (should_set_defaults(info))
b966b5dd 1386 set_disk_conf_defaults(new_disk_conf);
5979e361 1387
5ecc72c3 1388 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
c75b9b10 1389 if (err && err != -ENOMSG) {
f399002e 1390 retcode = ERR_MANDATORY_TAG;
a910b123 1391 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
8e229434 1392 goto fail_unlock;
f399002e
LE
1393 }
1394
5ecc72c3
LE
1395 if (!expect(new_disk_conf->resync_rate >= 1))
1396 new_disk_conf->resync_rate = 1;
f399002e 1397
5bbcf5e6
LE
1398 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1399 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
b30ab791
AG
1400 if (new_disk_conf->al_extents > drbd_al_extents_max(device->ldev))
1401 new_disk_conf->al_extents = drbd_al_extents_max(device->ldev);
5bbcf5e6
LE
1402
1403 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1404 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
f399002e 1405
5ecc72c3 1406 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
b30ab791 1407 if (fifo_size != device->rs_plan_s->size) {
813472ce
PR
1408 new_plan = fifo_alloc(fifo_size);
1409 if (!new_plan) {
d0180171 1410 drbd_err(device, "kmalloc of fifo_buffer failed");
f399002e 1411 retcode = ERR_NOMEM;
daeda1cc 1412 goto fail_unlock;
f399002e
LE
1413 }
1414 }
1415
b30ab791
AG
1416 drbd_suspend_io(device);
1417 wait_event(device->al_wait, lc_try_lock(device->act_log));
1418 drbd_al_shrink(device);
1419 err = drbd_check_al_size(device, new_disk_conf);
1420 lc_unlock(device->act_log);
1421 wake_up(&device->al_wait);
1422 drbd_resume_io(device);
f399002e
LE
1423
1424 if (err) {
1425 retcode = ERR_NOMEM;
daeda1cc 1426 goto fail_unlock;
f399002e
LE
1427 }
1428
28bc3b8c 1429 lock_all_resources();
b30ab791 1430 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
dc97b708 1431 if (retcode == NO_ERROR) {
b30ab791
AG
1432 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
1433 drbd_resync_after_changed(device);
dc97b708 1434 }
28bc3b8c 1435 unlock_all_resources();
f399002e 1436
daeda1cc
PR
1437 if (retcode != NO_ERROR)
1438 goto fail_unlock;
f399002e 1439
813472ce 1440 if (new_plan) {
b30ab791
AG
1441 old_plan = device->rs_plan_s;
1442 rcu_assign_pointer(device->rs_plan_s, new_plan);
9958c857 1443 }
9958c857 1444
0500813f 1445 mutex_unlock(&device->resource->conf_update);
27eb13e9 1446
9a51ab1c 1447 if (new_disk_conf->al_updates)
b30ab791 1448 device->ldev->md.flags &= ~MDF_AL_DISABLED;
9a51ab1c 1449 else
b30ab791 1450 device->ldev->md.flags |= MDF_AL_DISABLED;
9a51ab1c 1451
691631c0 1452 if (new_disk_conf->md_flushes)
b30ab791 1453 clear_bit(MD_NO_FUA, &device->flags);
691631c0 1454 else
b30ab791 1455 set_bit(MD_NO_FUA, &device->flags);
691631c0 1456
70df7092 1457 if (write_ordering_changed(old_disk_conf, new_disk_conf))
f6ba8636 1458 drbd_bump_write_ordering(device->resource, NULL, WO_BDEV_FLUSH);
27eb13e9 1459
b30ab791 1460 drbd_md_sync(device);
f399002e 1461
69a22773
AG
1462 if (device->state.conn >= C_CONNECTED) {
1463 struct drbd_peer_device *peer_device;
1464
1465 for_each_peer_device(peer_device, device)
1466 drbd_send_sync_param(peer_device);
1467 }
f399002e 1468
daeda1cc
PR
1469 synchronize_rcu();
1470 kfree(old_disk_conf);
813472ce 1471 kfree(old_plan);
b30ab791 1472 mod_timer(&device->request_timer, jiffies + HZ);
daeda1cc
PR
1473 goto success;
1474
1475fail_unlock:
0500813f 1476 mutex_unlock(&device->resource->conf_update);
f399002e 1477 fail:
5ecc72c3 1478 kfree(new_disk_conf);
813472ce 1479 kfree(new_plan);
daeda1cc 1480success:
b30ab791 1481 put_ldev(device);
f399002e 1482 out:
9e276872
LE
1483 mutex_unlock(&adm_ctx.resource->adm_mutex);
1484 finish:
a910b123 1485 drbd_adm_finish(&adm_ctx, info, retcode);
f399002e
LE
1486 return 0;
1487}
1488
63a7c8ad
LE
1489static struct block_device *open_backing_dev(struct drbd_device *device,
1490 const char *bdev_path, void *claim_ptr, bool do_bd_link)
1491{
1492 struct block_device *bdev;
1493 int err = 0;
1494
1495 bdev = blkdev_get_by_path(bdev_path,
1496 FMODE_READ | FMODE_WRITE | FMODE_EXCL, claim_ptr);
1497 if (IS_ERR(bdev)) {
1498 drbd_err(device, "open(\"%s\") failed with %ld\n",
1499 bdev_path, PTR_ERR(bdev));
1500 return bdev;
1501 }
1502
1503 if (!do_bd_link)
1504 return bdev;
1505
1506 err = bd_link_disk_holder(bdev, device->vdisk);
1507 if (err) {
1508 blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1509 drbd_err(device, "bd_link_disk_holder(\"%s\", ...) failed with %d\n",
1510 bdev_path, err);
1511 bdev = ERR_PTR(err);
1512 }
1513 return bdev;
1514}
1515
1516static int open_backing_devices(struct drbd_device *device,
1517 struct disk_conf *new_disk_conf,
1518 struct drbd_backing_dev *nbc)
1519{
1520 struct block_device *bdev;
1521
1522 bdev = open_backing_dev(device, new_disk_conf->backing_dev, device, true);
1523 if (IS_ERR(bdev))
1524 return ERR_OPEN_DISK;
1525 nbc->backing_bdev = bdev;
1526
1527 /*
1528 * meta_dev_idx >= 0: external fixed size, possibly multiple
1529 * drbd sharing one meta device. TODO in that case, paranoia
1530 * check that [md_bdev, meta_dev_idx] is not yet used by some
1531 * other drbd minor! (if you use drbd.conf + drbdadm, that
1532 * should check it for you already; but if you don't, or
1533 * someone fooled it, we need to double check here)
1534 */
1535 bdev = open_backing_dev(device, new_disk_conf->meta_dev,
1536 /* claim ptr: device, if claimed exclusively; shared drbd_m_holder,
1537 * if potentially shared with other drbd minors */
1538 (new_disk_conf->meta_dev_idx < 0) ? (void*)device : (void*)drbd_m_holder,
1539 /* avoid double bd_claim_by_disk() for the same (source,target) tuple,
1540 * as would happen with internal metadata. */
1541 (new_disk_conf->meta_dev_idx != DRBD_MD_INDEX_FLEX_INT &&
1542 new_disk_conf->meta_dev_idx != DRBD_MD_INDEX_INTERNAL));
1543 if (IS_ERR(bdev))
1544 return ERR_OPEN_MD_DISK;
1545 nbc->md_bdev = bdev;
1546 return NO_ERROR;
1547}
1548
1549static void close_backing_dev(struct drbd_device *device, struct block_device *bdev,
1550 bool do_bd_unlink)
1551{
1552 if (!bdev)
1553 return;
1554 if (do_bd_unlink)
1555 bd_unlink_disk_holder(bdev, device->vdisk);
1556 blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1557}
1558
1559void drbd_backing_dev_free(struct drbd_device *device, struct drbd_backing_dev *ldev)
1560{
1561 if (ldev == NULL)
1562 return;
1563
1564 close_backing_dev(device, ldev->md_bdev, ldev->md_bdev != ldev->backing_bdev);
1565 close_backing_dev(device, ldev->backing_bdev, true);
1566
1567 kfree(ldev->disk_conf);
1568 kfree(ldev);
1569}
1570
3b98c0c2 1571int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
b411b363 1572{
a910b123 1573 struct drbd_config_context adm_ctx;
b30ab791 1574 struct drbd_device *device;
44a4d551
LE
1575 struct drbd_peer_device *peer_device;
1576 struct drbd_connection *connection;
3b98c0c2 1577 int err;
116676ca 1578 enum drbd_ret_code retcode;
b411b363
PR
1579 enum determine_dev_size dd;
1580 sector_t max_possible_sectors;
1581 sector_t min_md_device_sectors;
1582 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
daeda1cc 1583 struct disk_conf *new_disk_conf = NULL;
b411b363 1584 struct lru_cache *resync_lru = NULL;
9958c857 1585 struct fifo_buffer *new_plan = NULL;
b411b363 1586 union drbd_state ns, os;
f2024e7c 1587 enum drbd_state_rv rv;
44ed167d 1588 struct net_conf *nc;
b411b363 1589
a910b123 1590 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
1591 if (!adm_ctx.reply_skb)
1592 return retcode;
1593 if (retcode != NO_ERROR)
40cbf085 1594 goto finish;
b411b363 1595
b30ab791 1596 device = adm_ctx.device;
9e276872 1597 mutex_lock(&adm_ctx.resource->adm_mutex);
44a4d551 1598 peer_device = first_peer_device(device);
3b8a44f8 1599 connection = peer_device->connection;
44a4d551 1600 conn_reconfig_start(connection);
b411b363
PR
1601
1602 /* if you want to reconfigure, please tear down first */
b30ab791 1603 if (device->state.disk > D_DISKLESS) {
b411b363
PR
1604 retcode = ERR_DISK_CONFIGURED;
1605 goto fail;
1606 }
82f59cc6
LE
1607 /* It may just now have detached because of IO error. Make sure
1608 * drbd_ldev_destroy is done already, we may end up here very fast,
1609 * e.g. if someone calls attach from the on-io-error handler,
1610 * to realize a "hot spare" feature (not that I'd recommend that) */
e334f550 1611 wait_event(device->misc_wait, !test_bit(GOING_DISKLESS, &device->flags));
b411b363 1612
383606e0 1613 /* make sure there is no leftover from previous force-detach attempts */
b30ab791
AG
1614 clear_bit(FORCE_DETACH, &device->flags);
1615 clear_bit(WAS_IO_ERROR, &device->flags);
1616 clear_bit(WAS_READ_ERROR, &device->flags);
383606e0 1617
0029d624 1618 /* and no leftover from previously aborted resync or verify, either */
b30ab791
AG
1619 device->rs_total = 0;
1620 device->rs_failed = 0;
1621 atomic_set(&device->rs_pending_cnt, 0);
0029d624 1622
3b98c0c2 1623 /* allocation not in the IO path, drbdsetup context */
b411b363
PR
1624 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1625 if (!nbc) {
1626 retcode = ERR_NOMEM;
1627 goto fail;
1628 }
9f2247bb
PR
1629 spin_lock_init(&nbc->md.uuid_lock);
1630
daeda1cc
PR
1631 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
1632 if (!new_disk_conf) {
1633 retcode = ERR_NOMEM;
b411b363
PR
1634 goto fail;
1635 }
daeda1cc 1636 nbc->disk_conf = new_disk_conf;
b411b363 1637
daeda1cc
PR
1638 set_disk_conf_defaults(new_disk_conf);
1639 err = disk_conf_from_attrs(new_disk_conf, info);
3b98c0c2 1640 if (err) {
b411b363 1641 retcode = ERR_MANDATORY_TAG;
a910b123 1642 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
b411b363
PR
1643 goto fail;
1644 }
1645
5bbcf5e6
LE
1646 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1647 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
d589a21e 1648
9958c857
PR
1649 new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
1650 if (!new_plan) {
1651 retcode = ERR_NOMEM;
1652 goto fail;
1653 }
1654
daeda1cc 1655 if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
b411b363
PR
1656 retcode = ERR_MD_IDX_INVALID;
1657 goto fail;
1658 }
1659
44ed167d 1660 rcu_read_lock();
44a4d551 1661 nc = rcu_dereference(connection->net_conf);
44ed167d 1662 if (nc) {
daeda1cc 1663 if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
44ed167d 1664 rcu_read_unlock();
47ff2d0a 1665 retcode = ERR_STONITH_AND_PROT_A;
28bc3b8c 1666 goto fail;
47ff2d0a
PR
1667 }
1668 }
44ed167d 1669 rcu_read_unlock();
47ff2d0a 1670
63a7c8ad
LE
1671 retcode = open_backing_devices(device, new_disk_conf, nbc);
1672 if (retcode != NO_ERROR)
28bc3b8c 1673 goto fail;
b411b363 1674
e525fd89 1675 if ((nbc->backing_bdev == nbc->md_bdev) !=
daeda1cc
PR
1676 (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1677 new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
e525fd89 1678 retcode = ERR_MD_IDX_INVALID;
28bc3b8c 1679 goto fail;
b411b363
PR
1680 }
1681
1682 resync_lru = lc_create("resync", drbd_bm_ext_cache,
46a15bc3 1683 1, 61, sizeof(struct bm_extent),
b411b363
PR
1684 offsetof(struct bm_extent, lce));
1685 if (!resync_lru) {
1686 retcode = ERR_NOMEM;
28bc3b8c 1687 goto fail;
b411b363
PR
1688 }
1689
c04ccaa6
LE
1690 /* Read our meta data super block early.
1691 * This also sets other on-disk offsets. */
b30ab791 1692 retcode = drbd_md_read(device, nbc);
c04ccaa6 1693 if (retcode != NO_ERROR)
28bc3b8c 1694 goto fail;
b411b363 1695
5bbcf5e6
LE
1696 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1697 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
1698 if (new_disk_conf->al_extents > drbd_al_extents_max(nbc))
1699 new_disk_conf->al_extents = drbd_al_extents_max(nbc);
1700
daeda1cc 1701 if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
d0180171 1702 drbd_err(device, "max capacity %llu smaller than disk size %llu\n",
b411b363 1703 (unsigned long long) drbd_get_max_capacity(nbc),
daeda1cc 1704 (unsigned long long) new_disk_conf->disk_size);
7948bcdc 1705 retcode = ERR_DISK_TOO_SMALL;
28bc3b8c 1706 goto fail;
b411b363
PR
1707 }
1708
daeda1cc 1709 if (new_disk_conf->meta_dev_idx < 0) {
b411b363
PR
1710 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1711 /* at least one MB, otherwise it does not make sense */
1712 min_md_device_sectors = (2<<10);
1713 } else {
1714 max_possible_sectors = DRBD_MAX_SECTORS;
ae8bf312 1715 min_md_device_sectors = MD_128MB_SECT * (new_disk_conf->meta_dev_idx + 1);
b411b363
PR
1716 }
1717
b411b363 1718 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
7948bcdc 1719 retcode = ERR_MD_DISK_TOO_SMALL;
d0180171 1720 drbd_warn(device, "refusing attach: md-device too small, "
b411b363
PR
1721 "at least %llu sectors needed for this meta-disk type\n",
1722 (unsigned long long) min_md_device_sectors);
28bc3b8c 1723 goto fail;
b411b363
PR
1724 }
1725
1726 /* Make sure the new disk is big enough
1727 * (we may currently be R_PRIMARY with no local disk...) */
1728 if (drbd_get_max_capacity(nbc) <
b30ab791 1729 drbd_get_capacity(device->this_bdev)) {
7948bcdc 1730 retcode = ERR_DISK_TOO_SMALL;
28bc3b8c 1731 goto fail;
b411b363
PR
1732 }
1733
1734 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1735
1352994b 1736 if (nbc->known_size > max_possible_sectors) {
d0180171 1737 drbd_warn(device, "==> truncating very big lower level device "
1352994b
LE
1738 "to currently maximum possible %llu sectors <==\n",
1739 (unsigned long long) max_possible_sectors);
daeda1cc 1740 if (new_disk_conf->meta_dev_idx >= 0)
d0180171 1741 drbd_warn(device, "==>> using internal or flexible "
1352994b
LE
1742 "meta data may help <<==\n");
1743 }
1744
b30ab791 1745 drbd_suspend_io(device);
b411b363 1746 /* also wait for the last barrier ack. */
b6dd1a89
LE
1747 /* FIXME see also https://daiquiri.linbit/cgi-bin/bugzilla/show_bug.cgi?id=171
1748 * We need a way to either ignore barrier acks for barriers sent before a device
1749 * was attached, or a way to wait for all pending barrier acks to come in.
1750 * As barriers are counted per resource,
1751 * we'd need to suspend io on all devices of a resource.
1752 */
b30ab791 1753 wait_event(device->misc_wait, !atomic_read(&device->ap_pending_cnt) || drbd_suspended(device));
b411b363 1754 /* and for any other previously queued work */
44a4d551 1755 drbd_flush_workqueue(&connection->sender_work);
b411b363 1756
b30ab791 1757 rv = _drbd_request_state(device, NS(disk, D_ATTACHING), CS_VERBOSE);
f2024e7c 1758 retcode = rv; /* FIXME: Type mismatch. */
b30ab791 1759 drbd_resume_io(device);
f2024e7c 1760 if (rv < SS_SUCCESS)
28bc3b8c 1761 goto fail;
b411b363 1762
b30ab791 1763 if (!get_ldev_if_state(device, D_ATTACHING))
b411b363
PR
1764 goto force_diskless;
1765
b30ab791
AG
1766 if (!device->bitmap) {
1767 if (drbd_bm_init(device)) {
b411b363
PR
1768 retcode = ERR_NOMEM;
1769 goto force_diskless_dec;
1770 }
1771 }
1772
b30ab791 1773 if (device->state.conn < C_CONNECTED &&
babea49e 1774 device->state.role == R_PRIMARY && device->ed_uuid &&
b30ab791 1775 (device->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
d0180171 1776 drbd_err(device, "Can only attach to data with current UUID=%016llX\n",
b30ab791 1777 (unsigned long long)device->ed_uuid);
b411b363
PR
1778 retcode = ERR_DATA_NOT_CURRENT;
1779 goto force_diskless_dec;
1780 }
1781
1782 /* Since we are diskless, fix the activity log first... */
b30ab791 1783 if (drbd_check_al_size(device, new_disk_conf)) {
b411b363
PR
1784 retcode = ERR_NOMEM;
1785 goto force_diskless_dec;
1786 }
1787
1788 /* Prevent shrinking of consistent devices ! */
1789 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
b30ab791 1790 drbd_new_dev_size(device, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
d0180171 1791 drbd_warn(device, "refusing to truncate a consistent device\n");
7948bcdc 1792 retcode = ERR_DISK_TOO_SMALL;
b411b363
PR
1793 goto force_diskless_dec;
1794 }
1795
28bc3b8c
AG
1796 lock_all_resources();
1797 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
1798 if (retcode != NO_ERROR) {
1799 unlock_all_resources();
1800 goto force_diskless_dec;
1801 }
1802
b411b363
PR
1803 /* Reset the "barriers don't work" bits here, then force meta data to
1804 * be written, to ensure we determine if barriers are supported. */
e544046a 1805 if (new_disk_conf->md_flushes)
b30ab791 1806 clear_bit(MD_NO_FUA, &device->flags);
b411b363 1807 else
b30ab791 1808 set_bit(MD_NO_FUA, &device->flags);
b411b363
PR
1809
1810 /* Point of no return reached.
1811 * Devices and memory are no longer released by error cleanup below.
b30ab791 1812 * now device takes over responsibility, and the state engine should
b411b363 1813 * clean it up somewhere. */
0b0ba1ef 1814 D_ASSERT(device, device->ldev == NULL);
b30ab791
AG
1815 device->ldev = nbc;
1816 device->resync = resync_lru;
1817 device->rs_plan_s = new_plan;
b411b363
PR
1818 nbc = NULL;
1819 resync_lru = NULL;
daeda1cc 1820 new_disk_conf = NULL;
9958c857 1821 new_plan = NULL;
b411b363 1822
1ec317d3 1823 drbd_resync_after_changed(device);
f6ba8636 1824 drbd_bump_write_ordering(device->resource, device->ldev, WO_BDEV_FLUSH);
28bc3b8c 1825 unlock_all_resources();
b411b363 1826
b30ab791
AG
1827 if (drbd_md_test_flag(device->ldev, MDF_CRASHED_PRIMARY))
1828 set_bit(CRASHED_PRIMARY, &device->flags);
b411b363 1829 else
b30ab791 1830 clear_bit(CRASHED_PRIMARY, &device->flags);
b411b363 1831
b30ab791 1832 if (drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
6bbf53ca 1833 !(device->state.role == R_PRIMARY && device->resource->susp_nod))
b30ab791 1834 set_bit(CRASHED_PRIMARY, &device->flags);
b411b363 1835
b30ab791
AG
1836 device->send_cnt = 0;
1837 device->recv_cnt = 0;
1838 device->read_cnt = 0;
1839 device->writ_cnt = 0;
b411b363 1840
8fe39aac 1841 drbd_reconsider_max_bio_size(device, device->ldev);
b411b363
PR
1842
1843 /* If I am currently not R_PRIMARY,
1844 * but meta data primary indicator is set,
1845 * I just now recover from a hard crash,
1846 * and have been R_PRIMARY before that crash.
1847 *
1848 * Now, if I had no connection before that crash
1849 * (have been degraded R_PRIMARY), chances are that
1850 * I won't find my peer now either.
1851 *
1852 * In that case, and _only_ in that case,
1853 * we use the degr-wfc-timeout instead of the default,
1854 * so we can automatically recover from a crash of a
1855 * degraded but active "cluster" after a certain timeout.
1856 */
b30ab791
AG
1857 clear_bit(USE_DEGR_WFC_T, &device->flags);
1858 if (device->state.role != R_PRIMARY &&
1859 drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
1860 !drbd_md_test_flag(device->ldev, MDF_CONNECTED_IND))
1861 set_bit(USE_DEGR_WFC_T, &device->flags);
b411b363 1862
b30ab791 1863 dd = drbd_determine_dev_size(device, 0, NULL);
d752b269 1864 if (dd <= DS_ERROR) {
b411b363
PR
1865 retcode = ERR_NOMEM_BITMAP;
1866 goto force_diskless_dec;
e96c9633 1867 } else if (dd == DS_GREW)
b30ab791 1868 set_bit(RESYNC_AFTER_NEG, &device->flags);
b411b363 1869
b30ab791
AG
1870 if (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC) ||
1871 (test_bit(CRASHED_PRIMARY, &device->flags) &&
1872 drbd_md_test_flag(device->ldev, MDF_AL_DISABLED))) {
d0180171 1873 drbd_info(device, "Assuming that all blocks are out of sync "
b411b363 1874 "(aka FullSync)\n");
b30ab791 1875 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
20ceb2b2 1876 "set_n_write from attaching", BM_LOCKED_MASK)) {
b411b363
PR
1877 retcode = ERR_IO_MD_DISK;
1878 goto force_diskless_dec;
1879 }
1880 } else {
b30ab791 1881 if (drbd_bitmap_io(device, &drbd_bm_read,
22ab6a30 1882 "read from attaching", BM_LOCKED_MASK)) {
19f843aa
LE
1883 retcode = ERR_IO_MD_DISK;
1884 goto force_diskless_dec;
1885 }
b411b363
PR
1886 }
1887
b30ab791
AG
1888 if (_drbd_bm_total_weight(device) == drbd_bm_bits(device))
1889 drbd_suspend_al(device); /* IO is still suspended here... */
0778286a 1890
0500813f 1891 spin_lock_irq(&device->resource->req_lock);
b30ab791 1892 os = drbd_read_state(device);
78bae59b 1893 ns = os;
b411b363
PR
1894 /* If MDF_CONSISTENT is not set go into inconsistent state,
1895 otherwise investigate MDF_WasUpToDate...
1896 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1897 otherwise into D_CONSISTENT state.
1898 */
b30ab791
AG
1899 if (drbd_md_test_flag(device->ldev, MDF_CONSISTENT)) {
1900 if (drbd_md_test_flag(device->ldev, MDF_WAS_UP_TO_DATE))
b411b363
PR
1901 ns.disk = D_CONSISTENT;
1902 else
1903 ns.disk = D_OUTDATED;
1904 } else {
1905 ns.disk = D_INCONSISTENT;
1906 }
1907
b30ab791 1908 if (drbd_md_test_flag(device->ldev, MDF_PEER_OUT_DATED))
b411b363
PR
1909 ns.pdsk = D_OUTDATED;
1910
daeda1cc
PR
1911 rcu_read_lock();
1912 if (ns.disk == D_CONSISTENT &&
b30ab791 1913 (ns.pdsk == D_OUTDATED || rcu_dereference(device->ldev->disk_conf)->fencing == FP_DONT_CARE))
b411b363
PR
1914 ns.disk = D_UP_TO_DATE;
1915
1916 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1917 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1918 this point, because drbd_request_state() modifies these
1919 flags. */
1920
b30ab791
AG
1921 if (rcu_dereference(device->ldev->disk_conf)->al_updates)
1922 device->ldev->md.flags &= ~MDF_AL_DISABLED;
9a51ab1c 1923 else
b30ab791 1924 device->ldev->md.flags |= MDF_AL_DISABLED;
9a51ab1c
PR
1925
1926 rcu_read_unlock();
1927
b411b363
PR
1928 /* In case we are C_CONNECTED postpone any decision on the new disk
1929 state after the negotiation phase. */
b30ab791
AG
1930 if (device->state.conn == C_CONNECTED) {
1931 device->new_state_tmp.i = ns.i;
b411b363
PR
1932 ns.i = os.i;
1933 ns.disk = D_NEGOTIATING;
dc66c74d
PR
1934
1935 /* We expect to receive up-to-date UUIDs soon.
1936 To avoid a race in receive_state, free p_uuid while
1937 holding req_lock. I.e. atomic with the state change */
b30ab791
AG
1938 kfree(device->p_uuid);
1939 device->p_uuid = NULL;
b411b363
PR
1940 }
1941
b30ab791 1942 rv = _drbd_set_state(device, ns, CS_VERBOSE, NULL);
0500813f 1943 spin_unlock_irq(&device->resource->req_lock);
b411b363
PR
1944
1945 if (rv < SS_SUCCESS)
1946 goto force_diskless_dec;
1947
b30ab791 1948 mod_timer(&device->request_timer, jiffies + HZ);
cdfda633 1949
b30ab791
AG
1950 if (device->state.role == R_PRIMARY)
1951 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
b411b363 1952 else
b30ab791 1953 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
b411b363 1954
b30ab791
AG
1955 drbd_md_mark_dirty(device);
1956 drbd_md_sync(device);
b411b363 1957
b30ab791
AG
1958 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
1959 put_ldev(device);
44a4d551 1960 conn_reconfig_done(connection);
9e276872 1961 mutex_unlock(&adm_ctx.resource->adm_mutex);
a910b123 1962 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
1963 return 0;
1964
1965 force_diskless_dec:
b30ab791 1966 put_ldev(device);
b411b363 1967 force_diskless:
b30ab791
AG
1968 drbd_force_state(device, NS(disk, D_DISKLESS));
1969 drbd_md_sync(device);
b411b363 1970 fail:
44a4d551 1971 conn_reconfig_done(connection);
b411b363 1972 if (nbc) {
63a7c8ad
LE
1973 close_backing_dev(device, nbc->md_bdev, nbc->md_bdev != nbc->backing_bdev);
1974 close_backing_dev(device, nbc->backing_bdev, true);
b411b363
PR
1975 kfree(nbc);
1976 }
daeda1cc 1977 kfree(new_disk_conf);
b411b363 1978 lc_destroy(resync_lru);
9958c857 1979 kfree(new_plan);
9e276872 1980 mutex_unlock(&adm_ctx.resource->adm_mutex);
40cbf085 1981 finish:
a910b123 1982 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
1983 return 0;
1984}
1985
b30ab791 1986static int adm_detach(struct drbd_device *device, int force)
b411b363 1987{
19f83c76 1988 enum drbd_state_rv retcode;
6434f404 1989 void *buffer;
9a0d9d03 1990 int ret;
02ee8f95 1991
cdfda633 1992 if (force) {
b30ab791
AG
1993 set_bit(FORCE_DETACH, &device->flags);
1994 drbd_force_state(device, NS(disk, D_FAILED));
cdfda633 1995 retcode = SS_SUCCESS;
02ee8f95
PR
1996 goto out;
1997 }
1998
b30ab791 1999 drbd_suspend_io(device); /* so no-one is stuck in drbd_al_begin_io */
6434f404
LE
2000 buffer = drbd_md_get_buffer(device, __func__); /* make sure there is no in-flight meta-data IO */
2001 if (buffer) {
2002 retcode = drbd_request_state(device, NS(disk, D_FAILED));
2003 drbd_md_put_buffer(device);
2004 } else /* already <= D_FAILED */
2005 retcode = SS_NOTHING_TO_DO;
9a0d9d03 2006 /* D_FAILED will transition to DISKLESS. */
05a72772 2007 drbd_resume_io(device);
b30ab791
AG
2008 ret = wait_event_interruptible(device->misc_wait,
2009 device->state.disk != D_FAILED);
9b2f61ae 2010 if ((int)retcode == (int)SS_IS_DISKLESS)
9a0d9d03
LE
2011 retcode = SS_NOTHING_TO_DO;
2012 if (ret)
2013 retcode = ERR_INTR;
02ee8f95 2014out:
85f75dd7 2015 return retcode;
b411b363
PR
2016}
2017
82f59cc6
LE
2018/* Detaching the disk is a process in multiple stages. First we need to lock
2019 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
2020 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
2021 * internal references as well.
2022 * Only then we have finally detached. */
3b98c0c2 2023int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
b411b363 2024{
a910b123 2025 struct drbd_config_context adm_ctx;
116676ca 2026 enum drbd_ret_code retcode;
cdfda633
PR
2027 struct detach_parms parms = { };
2028 int err;
b411b363 2029
a910b123 2030 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
2031 if (!adm_ctx.reply_skb)
2032 return retcode;
2033 if (retcode != NO_ERROR)
2034 goto out;
b411b363 2035
cdfda633
PR
2036 if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
2037 err = detach_parms_from_attrs(&parms, info);
2038 if (err) {
2039 retcode = ERR_MANDATORY_TAG;
a910b123 2040 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
cdfda633
PR
2041 goto out;
2042 }
b411b363
PR
2043 }
2044
9e276872 2045 mutex_lock(&adm_ctx.resource->adm_mutex);
b30ab791 2046 retcode = adm_detach(adm_ctx.device, parms.force_detach);
9e276872 2047 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 2048out:
a910b123 2049 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2050 return 0;
2051}
b411b363 2052
bde89a9e 2053static bool conn_resync_running(struct drbd_connection *connection)
f399002e 2054{
c06ece6b 2055 struct drbd_peer_device *peer_device;
695d08fa 2056 bool rv = false;
f399002e
LE
2057 int vnr;
2058
695d08fa 2059 rcu_read_lock();
c06ece6b
AG
2060 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
2061 struct drbd_device *device = peer_device->device;
b30ab791
AG
2062 if (device->state.conn == C_SYNC_SOURCE ||
2063 device->state.conn == C_SYNC_TARGET ||
2064 device->state.conn == C_PAUSED_SYNC_S ||
2065 device->state.conn == C_PAUSED_SYNC_T) {
695d08fa
PR
2066 rv = true;
2067 break;
2068 }
b411b363 2069 }
695d08fa 2070 rcu_read_unlock();
b411b363 2071
695d08fa 2072 return rv;
f399002e 2073}
47ff2d0a 2074
bde89a9e 2075static bool conn_ov_running(struct drbd_connection *connection)
f399002e 2076{
c06ece6b 2077 struct drbd_peer_device *peer_device;
695d08fa 2078 bool rv = false;
f399002e
LE
2079 int vnr;
2080
695d08fa 2081 rcu_read_lock();
c06ece6b
AG
2082 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
2083 struct drbd_device *device = peer_device->device;
b30ab791
AG
2084 if (device->state.conn == C_VERIFY_S ||
2085 device->state.conn == C_VERIFY_T) {
695d08fa
PR
2086 rv = true;
2087 break;
47ff2d0a
PR
2088 }
2089 }
695d08fa 2090 rcu_read_unlock();
b411b363 2091
695d08fa 2092 return rv;
f399002e 2093}
422028b1 2094
cd64397c 2095static enum drbd_ret_code
270eb5c9 2096_check_net_options(struct drbd_connection *connection, struct net_conf *old_net_conf, struct net_conf *new_net_conf)
cd64397c 2097{
c06ece6b 2098 struct drbd_peer_device *peer_device;
cd64397c 2099 int i;
b411b363 2100
270eb5c9
AG
2101 if (old_net_conf && connection->cstate == C_WF_REPORT_PARAMS && connection->agreed_pro_version < 100) {
2102 if (new_net_conf->wire_protocol != old_net_conf->wire_protocol)
dcb20d1a 2103 return ERR_NEED_APV_100;
b411b363 2104
270eb5c9 2105 if (new_net_conf->two_primaries != old_net_conf->two_primaries)
dcb20d1a
PR
2106 return ERR_NEED_APV_100;
2107
270eb5c9 2108 if (strcmp(new_net_conf->integrity_alg, old_net_conf->integrity_alg))
dcb20d1a 2109 return ERR_NEED_APV_100;
b411b363
PR
2110 }
2111
270eb5c9 2112 if (!new_net_conf->two_primaries &&
bde89a9e
AG
2113 conn_highest_role(connection) == R_PRIMARY &&
2114 conn_highest_peer(connection) == R_PRIMARY)
dcb20d1a 2115 return ERR_NEED_ALLOW_TWO_PRI;
b411b363 2116
270eb5c9
AG
2117 if (new_net_conf->two_primaries &&
2118 (new_net_conf->wire_protocol != DRBD_PROT_C))
cd64397c
PR
2119 return ERR_NOT_PROTO_C;
2120
c06ece6b
AG
2121 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2122 struct drbd_device *device = peer_device->device;
b30ab791
AG
2123 if (get_ldev(device)) {
2124 enum drbd_fencing_p fp = rcu_dereference(device->ldev->disk_conf)->fencing;
2125 put_ldev(device);
270eb5c9 2126 if (new_net_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
cd64397c 2127 return ERR_STONITH_AND_PROT_A;
b411b363 2128 }
270eb5c9 2129 if (device->state.role == R_PRIMARY && new_net_conf->discard_my_data)
eb12010e 2130 return ERR_DISCARD_IMPOSSIBLE;
b411b363
PR
2131 }
2132
270eb5c9 2133 if (new_net_conf->on_congestion != OC_BLOCK && new_net_conf->wire_protocol != DRBD_PROT_A)
cd64397c 2134 return ERR_CONG_NOT_PROTO_A;
b411b363 2135
cd64397c
PR
2136 return NO_ERROR;
2137}
b411b363 2138
44ed167d 2139static enum drbd_ret_code
270eb5c9 2140check_net_options(struct drbd_connection *connection, struct net_conf *new_net_conf)
44ed167d
PR
2141{
2142 static enum drbd_ret_code rv;
c06ece6b 2143 struct drbd_peer_device *peer_device;
44ed167d 2144 int i;
b411b363 2145
44ed167d 2146 rcu_read_lock();
270eb5c9 2147 rv = _check_net_options(connection, rcu_dereference(connection->net_conf), new_net_conf);
44ed167d 2148 rcu_read_unlock();
b411b363 2149
179e20b8 2150 /* connection->peer_devices protected by genl_lock() here */
c06ece6b
AG
2151 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2152 struct drbd_device *device = peer_device->device;
b30ab791
AG
2153 if (!device->bitmap) {
2154 if (drbd_bm_init(device))
44ed167d 2155 return ERR_NOMEM;
b411b363
PR
2156 }
2157 }
2158
44ed167d
PR
2159 return rv;
2160}
b411b363 2161
0fd0ea06
PR
2162struct crypto {
2163 struct crypto_hash *verify_tfm;
2164 struct crypto_hash *csums_tfm;
2165 struct crypto_hash *cram_hmac_tfm;
8d412fc6 2166 struct crypto_hash *integrity_tfm;
0fd0ea06 2167};
b411b363 2168
0fd0ea06 2169static int
4b6ad6d4 2170alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
0fd0ea06
PR
2171{
2172 if (!tfm_name[0])
2173 return NO_ERROR;
b411b363 2174
0fd0ea06
PR
2175 *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
2176 if (IS_ERR(*tfm)) {
2177 *tfm = NULL;
2178 return err_alg;
b411b363 2179 }
b411b363 2180
0fd0ea06
PR
2181 return NO_ERROR;
2182}
b411b363 2183
0fd0ea06 2184static enum drbd_ret_code
270eb5c9 2185alloc_crypto(struct crypto *crypto, struct net_conf *new_net_conf)
0fd0ea06
PR
2186{
2187 char hmac_name[CRYPTO_MAX_ALG_NAME];
2188 enum drbd_ret_code rv;
0fd0ea06 2189
270eb5c9 2190 rv = alloc_hash(&crypto->csums_tfm, new_net_conf->csums_alg,
4b6ad6d4 2191 ERR_CSUMS_ALG);
0fd0ea06
PR
2192 if (rv != NO_ERROR)
2193 return rv;
270eb5c9 2194 rv = alloc_hash(&crypto->verify_tfm, new_net_conf->verify_alg,
4b6ad6d4 2195 ERR_VERIFY_ALG);
0fd0ea06
PR
2196 if (rv != NO_ERROR)
2197 return rv;
270eb5c9 2198 rv = alloc_hash(&crypto->integrity_tfm, new_net_conf->integrity_alg,
4b6ad6d4 2199 ERR_INTEGRITY_ALG);
0fd0ea06
PR
2200 if (rv != NO_ERROR)
2201 return rv;
270eb5c9 2202 if (new_net_conf->cram_hmac_alg[0] != 0) {
0fd0ea06 2203 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
270eb5c9 2204 new_net_conf->cram_hmac_alg);
b411b363 2205
4b6ad6d4
AG
2206 rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
2207 ERR_AUTH_ALG);
b411b363
PR
2208 }
2209
0fd0ea06
PR
2210 return rv;
2211}
b411b363 2212
0fd0ea06
PR
2213static void free_crypto(struct crypto *crypto)
2214{
0fd0ea06 2215 crypto_free_hash(crypto->cram_hmac_tfm);
8d412fc6 2216 crypto_free_hash(crypto->integrity_tfm);
0fd0ea06
PR
2217 crypto_free_hash(crypto->csums_tfm);
2218 crypto_free_hash(crypto->verify_tfm);
2219}
b411b363 2220
f399002e
LE
2221int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
2222{
a910b123 2223 struct drbd_config_context adm_ctx;
f399002e 2224 enum drbd_ret_code retcode;
bde89a9e 2225 struct drbd_connection *connection;
270eb5c9 2226 struct net_conf *old_net_conf, *new_net_conf = NULL;
f399002e
LE
2227 int err;
2228 int ovr; /* online verify running */
2229 int rsr; /* re-sync running */
0fd0ea06 2230 struct crypto crypto = { };
b411b363 2231
a910b123 2232 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_CONNECTION);
f399002e
LE
2233 if (!adm_ctx.reply_skb)
2234 return retcode;
2235 if (retcode != NO_ERROR)
9e276872 2236 goto finish;
b411b363 2237
bde89a9e 2238 connection = adm_ctx.connection;
9e276872 2239 mutex_lock(&adm_ctx.resource->adm_mutex);
b411b363 2240
270eb5c9
AG
2241 new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
2242 if (!new_net_conf) {
f399002e
LE
2243 retcode = ERR_NOMEM;
2244 goto out;
2245 }
b411b363 2246
bde89a9e 2247 conn_reconfig_start(connection);
b411b363 2248
bde89a9e 2249 mutex_lock(&connection->data.mutex);
0500813f 2250 mutex_lock(&connection->resource->conf_update);
270eb5c9 2251 old_net_conf = connection->net_conf;
2561b9c1 2252
270eb5c9 2253 if (!old_net_conf) {
a910b123 2254 drbd_msg_put_info(adm_ctx.reply_skb, "net conf missing, try connect");
f399002e 2255 retcode = ERR_INVALID_REQUEST;
2561b9c1
PR
2256 goto fail;
2257 }
2258
270eb5c9 2259 *new_net_conf = *old_net_conf;
5979e361 2260 if (should_set_defaults(info))
270eb5c9 2261 set_net_conf_defaults(new_net_conf);
f399002e 2262
270eb5c9 2263 err = net_conf_from_attrs_for_change(new_net_conf, info);
c75b9b10 2264 if (err && err != -ENOMSG) {
f399002e 2265 retcode = ERR_MANDATORY_TAG;
a910b123 2266 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
f399002e 2267 goto fail;
2561b9c1 2268 }
b411b363 2269
270eb5c9 2270 retcode = check_net_options(connection, new_net_conf);
cd64397c
PR
2271 if (retcode != NO_ERROR)
2272 goto fail;
b411b363 2273
f399002e 2274 /* re-sync running */
bde89a9e 2275 rsr = conn_resync_running(connection);
270eb5c9 2276 if (rsr && strcmp(new_net_conf->csums_alg, old_net_conf->csums_alg)) {
f399002e 2277 retcode = ERR_CSUMS_RESYNC_RUNNING;
91fd4dad 2278 goto fail;
b411b363
PR
2279 }
2280
f399002e 2281 /* online verify running */
bde89a9e 2282 ovr = conn_ov_running(connection);
270eb5c9 2283 if (ovr && strcmp(new_net_conf->verify_alg, old_net_conf->verify_alg)) {
0fd0ea06 2284 retcode = ERR_VERIFY_RUNNING;
b411b363 2285 goto fail;
f399002e 2286 }
b411b363 2287
270eb5c9 2288 retcode = alloc_crypto(&crypto, new_net_conf);
0fd0ea06 2289 if (retcode != NO_ERROR)
b411b363 2290 goto fail;
f399002e 2291
270eb5c9 2292 rcu_assign_pointer(connection->net_conf, new_net_conf);
f399002e
LE
2293
2294 if (!rsr) {
bde89a9e
AG
2295 crypto_free_hash(connection->csums_tfm);
2296 connection->csums_tfm = crypto.csums_tfm;
0fd0ea06 2297 crypto.csums_tfm = NULL;
f399002e
LE
2298 }
2299 if (!ovr) {
bde89a9e
AG
2300 crypto_free_hash(connection->verify_tfm);
2301 connection->verify_tfm = crypto.verify_tfm;
0fd0ea06 2302 crypto.verify_tfm = NULL;
b411b363
PR
2303 }
2304
bde89a9e
AG
2305 crypto_free_hash(connection->integrity_tfm);
2306 connection->integrity_tfm = crypto.integrity_tfm;
2307 if (connection->cstate >= C_WF_REPORT_PARAMS && connection->agreed_pro_version >= 100)
2308 /* Do this without trying to take connection->data.mutex again. */
2309 __drbd_send_protocol(connection, P_PROTOCOL_UPDATE);
0fd0ea06 2310
bde89a9e
AG
2311 crypto_free_hash(connection->cram_hmac_tfm);
2312 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
0fd0ea06 2313
0500813f 2314 mutex_unlock(&connection->resource->conf_update);
bde89a9e 2315 mutex_unlock(&connection->data.mutex);
91fd4dad 2316 synchronize_rcu();
270eb5c9 2317 kfree(old_net_conf);
91fd4dad 2318
69a22773
AG
2319 if (connection->cstate >= C_WF_REPORT_PARAMS) {
2320 struct drbd_peer_device *peer_device;
2321 int vnr;
2322
2323 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
2324 drbd_send_sync_param(peer_device);
2325 }
f399002e 2326
91fd4dad
PR
2327 goto done;
2328
b411b363 2329 fail:
0500813f 2330 mutex_unlock(&connection->resource->conf_update);
bde89a9e 2331 mutex_unlock(&connection->data.mutex);
0fd0ea06 2332 free_crypto(&crypto);
270eb5c9 2333 kfree(new_net_conf);
91fd4dad 2334 done:
bde89a9e 2335 conn_reconfig_done(connection);
f399002e 2336 out:
9e276872
LE
2337 mutex_unlock(&adm_ctx.resource->adm_mutex);
2338 finish:
a910b123 2339 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2340 return 0;
2341}
2342
a2972846
AG
2343static void connection_to_info(struct connection_info *info,
2344 struct drbd_connection *connection)
2345{
2346 info->conn_connection_state = connection->cstate;
2347 info->conn_role = conn_highest_peer(connection);
2348}
2349
2350static void peer_device_to_info(struct peer_device_info *info,
2351 struct drbd_peer_device *peer_device)
2352{
2353 struct drbd_device *device = peer_device->device;
2354
2355 info->peer_repl_state =
2356 max_t(enum drbd_conns, C_WF_REPORT_PARAMS, device->state.conn);
2357 info->peer_disk_state = device->state.pdsk;
2358 info->peer_resync_susp_user = device->state.user_isp;
2359 info->peer_resync_susp_peer = device->state.peer_isp;
2360 info->peer_resync_susp_dependency = device->state.aftr_isp;
2361}
2362
3b98c0c2 2363int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
b411b363 2364{
a2972846
AG
2365 struct connection_info connection_info;
2366 enum drbd_notification_type flags;
2367 unsigned int peer_devices = 0;
a910b123 2368 struct drbd_config_context adm_ctx;
c06ece6b 2369 struct drbd_peer_device *peer_device;
270eb5c9 2370 struct net_conf *old_net_conf, *new_net_conf = NULL;
0fd0ea06 2371 struct crypto crypto = { };
77c556f6 2372 struct drbd_resource *resource;
bde89a9e 2373 struct drbd_connection *connection;
3b98c0c2
LE
2374 enum drbd_ret_code retcode;
2375 int i;
2376 int err;
b411b363 2377
a910b123 2378 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
b411b363 2379
3b98c0c2
LE
2380 if (!adm_ctx.reply_skb)
2381 return retcode;
2382 if (retcode != NO_ERROR)
2383 goto out;
089c075d 2384 if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
a910b123 2385 drbd_msg_put_info(adm_ctx.reply_skb, "connection endpoint(s) missing");
089c075d
AG
2386 retcode = ERR_INVALID_REQUEST;
2387 goto out;
2388 }
b411b363 2389
089c075d
AG
2390 /* No need for _rcu here. All reconfiguration is
2391 * strictly serialized on genl_lock(). We are protected against
2392 * concurrent reconfiguration/addition/deletion */
77c556f6
AG
2393 for_each_resource(resource, &drbd_resources) {
2394 for_each_connection(connection, resource) {
2395 if (nla_len(adm_ctx.my_addr) == connection->my_addr_len &&
2396 !memcmp(nla_data(adm_ctx.my_addr), &connection->my_addr,
2397 connection->my_addr_len)) {
2398 retcode = ERR_LOCAL_ADDR;
2399 goto out;
2400 }
b411b363 2401
77c556f6
AG
2402 if (nla_len(adm_ctx.peer_addr) == connection->peer_addr_len &&
2403 !memcmp(nla_data(adm_ctx.peer_addr), &connection->peer_addr,
2404 connection->peer_addr_len)) {
2405 retcode = ERR_PEER_ADDR;
2406 goto out;
2407 }
089c075d 2408 }
b411b363
PR
2409 }
2410
9e276872 2411 mutex_lock(&adm_ctx.resource->adm_mutex);
3ab706fe 2412 connection = first_connection(adm_ctx.resource);
bde89a9e 2413 conn_reconfig_start(connection);
b411b363 2414
bde89a9e 2415 if (connection->cstate > C_STANDALONE) {
b411b363 2416 retcode = ERR_NET_CONFIGURED;
b411b363
PR
2417 goto fail;
2418 }
2419
a209b4ae 2420 /* allocation not in the IO path, drbdsetup / netlink process context */
270eb5c9
AG
2421 new_net_conf = kzalloc(sizeof(*new_net_conf), GFP_KERNEL);
2422 if (!new_net_conf) {
b411b363 2423 retcode = ERR_NOMEM;
b411b363
PR
2424 goto fail;
2425 }
2426
270eb5c9 2427 set_net_conf_defaults(new_net_conf);
b411b363 2428
270eb5c9 2429 err = net_conf_from_attrs(new_net_conf, info);
25e40932 2430 if (err && err != -ENOMSG) {
b411b363 2431 retcode = ERR_MANDATORY_TAG;
a910b123 2432 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
b411b363
PR
2433 goto fail;
2434 }
2435
270eb5c9 2436 retcode = check_net_options(connection, new_net_conf);
cd64397c 2437 if (retcode != NO_ERROR)
422028b1 2438 goto fail;
b411b363 2439
270eb5c9 2440 retcode = alloc_crypto(&crypto, new_net_conf);
0fd0ea06
PR
2441 if (retcode != NO_ERROR)
2442 goto fail;
b411b363 2443
270eb5c9 2444 ((char *)new_net_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
7b4e4d31 2445
b5043c5e 2446 drbd_flush_workqueue(&connection->sender_work);
b411b363 2447
0500813f 2448 mutex_lock(&adm_ctx.resource->conf_update);
270eb5c9
AG
2449 old_net_conf = connection->net_conf;
2450 if (old_net_conf) {
b411b363 2451 retcode = ERR_NET_CONFIGURED;
0500813f 2452 mutex_unlock(&adm_ctx.resource->conf_update);
b411b363
PR
2453 goto fail;
2454 }
270eb5c9 2455 rcu_assign_pointer(connection->net_conf, new_net_conf);
b411b363 2456
bde89a9e
AG
2457 conn_free_crypto(connection);
2458 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
2459 connection->integrity_tfm = crypto.integrity_tfm;
2460 connection->csums_tfm = crypto.csums_tfm;
2461 connection->verify_tfm = crypto.verify_tfm;
b411b363 2462
bde89a9e
AG
2463 connection->my_addr_len = nla_len(adm_ctx.my_addr);
2464 memcpy(&connection->my_addr, nla_data(adm_ctx.my_addr), connection->my_addr_len);
2465 connection->peer_addr_len = nla_len(adm_ctx.peer_addr);
2466 memcpy(&connection->peer_addr, nla_data(adm_ctx.peer_addr), connection->peer_addr_len);
b411b363 2467
a2972846
AG
2468 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2469 peer_devices++;
2470 }
2471
2472 connection_to_info(&connection_info, connection);
2473 flags = (peer_devices--) ? NOTIFY_CONTINUES : 0;
2474 mutex_lock(&notification_mutex);
2475 notify_connection_state(NULL, 0, connection, &connection_info, NOTIFY_CREATE | flags);
2476 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2477 struct peer_device_info peer_device_info;
2478
2479 peer_device_to_info(&peer_device_info, peer_device);
2480 flags = (peer_devices--) ? NOTIFY_CONTINUES : 0;
2481 notify_peer_device_state(NULL, 0, peer_device, &peer_device_info, NOTIFY_CREATE | flags);
2482 }
2483 mutex_unlock(&notification_mutex);
0500813f 2484 mutex_unlock(&adm_ctx.resource->conf_update);
b411b363 2485
695d08fa 2486 rcu_read_lock();
c06ece6b
AG
2487 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2488 struct drbd_device *device = peer_device->device;
b30ab791
AG
2489 device->send_cnt = 0;
2490 device->recv_cnt = 0;
b411b363 2491 }
695d08fa 2492 rcu_read_unlock();
b411b363 2493
bde89a9e 2494 retcode = conn_request_state(connection, NS(conn, C_UNCONNECTED), CS_VERBOSE);
b411b363 2495
bde89a9e 2496 conn_reconfig_done(connection);
9e276872 2497 mutex_unlock(&adm_ctx.resource->adm_mutex);
a910b123 2498 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363 2499 return 0;
b411b363 2500
b411b363 2501fail:
0fd0ea06 2502 free_crypto(&crypto);
270eb5c9 2503 kfree(new_net_conf);
b411b363 2504
bde89a9e 2505 conn_reconfig_done(connection);
9e276872 2506 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 2507out:
a910b123 2508 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2509 return 0;
2510}
2511
bde89a9e 2512static enum drbd_state_rv conn_try_disconnect(struct drbd_connection *connection, bool force)
85f75dd7
LE
2513{
2514 enum drbd_state_rv rv;
85f75dd7 2515
bde89a9e 2516 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
f3dfa40a 2517 force ? CS_HARD : 0);
85f75dd7
LE
2518
2519 switch (rv) {
2520 case SS_NOTHING_TO_DO:
f3dfa40a 2521 break;
85f75dd7
LE
2522 case SS_ALREADY_STANDALONE:
2523 return SS_SUCCESS;
2524 case SS_PRIMARY_NOP:
2525 /* Our state checking code wants to see the peer outdated. */
bde89a9e 2526 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING, pdsk, D_OUTDATED), 0);
2bd5ed5d
PR
2527
2528 if (rv == SS_OUTDATE_WO_CONN) /* lost connection before graceful disconnect succeeded */
bde89a9e 2529 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_VERBOSE);
2bd5ed5d 2530
85f75dd7
LE
2531 break;
2532 case SS_CW_FAILED_BY_PEER:
2533 /* The peer probably wants to see us outdated. */
bde89a9e 2534 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING,
85f75dd7
LE
2535 disk, D_OUTDATED), 0);
2536 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
bde89a9e 2537 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
f3dfa40a 2538 CS_HARD);
b411b363 2539 }
85f75dd7
LE
2540 break;
2541 default:;
2542 /* no special handling necessary */
2543 }
2544
f3dfa40a
LE
2545 if (rv >= SS_SUCCESS) {
2546 enum drbd_state_rv rv2;
2547 /* No one else can reconfigure the network while I am here.
2548 * The state handling only uses drbd_thread_stop_nowait(),
2549 * we want to really wait here until the receiver is no more.
2550 */
9693da23 2551 drbd_thread_stop(&connection->receiver);
f3dfa40a
LE
2552
2553 /* Race breaker. This additional state change request may be
2554 * necessary, if this was a forced disconnect during a receiver
2555 * restart. We may have "killed" the receiver thread just
8fe60551 2556 * after drbd_receiver() returned. Typically, we should be
f3dfa40a
LE
2557 * C_STANDALONE already, now, and this becomes a no-op.
2558 */
bde89a9e 2559 rv2 = conn_request_state(connection, NS(conn, C_STANDALONE),
f3dfa40a
LE
2560 CS_VERBOSE | CS_HARD);
2561 if (rv2 < SS_SUCCESS)
1ec861eb 2562 drbd_err(connection,
f3dfa40a
LE
2563 "unexpected rv2=%d in conn_try_disconnect()\n",
2564 rv2);
a2972846
AG
2565 /* Unlike in DRBD 9, the state engine has generated
2566 * NOTIFY_DESTROY events before clearing connection->net_conf. */
b411b363 2567 }
85f75dd7
LE
2568 return rv;
2569}
b411b363 2570
3b98c0c2 2571int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
b411b363 2572{
a910b123 2573 struct drbd_config_context adm_ctx;
3b98c0c2 2574 struct disconnect_parms parms;
bde89a9e 2575 struct drbd_connection *connection;
85f75dd7 2576 enum drbd_state_rv rv;
3b98c0c2
LE
2577 enum drbd_ret_code retcode;
2578 int err;
2561b9c1 2579
a910b123 2580 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_CONNECTION);
3b98c0c2
LE
2581 if (!adm_ctx.reply_skb)
2582 return retcode;
2583 if (retcode != NO_ERROR)
2561b9c1 2584 goto fail;
b411b363 2585
bde89a9e 2586 connection = adm_ctx.connection;
3b98c0c2
LE
2587 memset(&parms, 0, sizeof(parms));
2588 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
f399002e 2589 err = disconnect_parms_from_attrs(&parms, info);
3b98c0c2
LE
2590 if (err) {
2591 retcode = ERR_MANDATORY_TAG;
a910b123 2592 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
b411b363
PR
2593 goto fail;
2594 }
2595 }
2596
9e276872 2597 mutex_lock(&adm_ctx.resource->adm_mutex);
bde89a9e 2598 rv = conn_try_disconnect(connection, parms.force_disconnect);
85f75dd7 2599 if (rv < SS_SUCCESS)
f3dfa40a
LE
2600 retcode = rv; /* FIXME: Type mismatch. */
2601 else
2602 retcode = NO_ERROR;
9e276872 2603 mutex_unlock(&adm_ctx.resource->adm_mutex);
b411b363 2604 fail:
a910b123 2605 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2606 return 0;
2607}
2608
b30ab791 2609void resync_after_online_grow(struct drbd_device *device)
b411b363
PR
2610{
2611 int iass; /* I am sync source */
2612
d0180171 2613 drbd_info(device, "Resync of new storage after online grow\n");
b30ab791
AG
2614 if (device->state.role != device->state.peer)
2615 iass = (device->state.role == R_PRIMARY);
b411b363 2616 else
a6b32bc3 2617 iass = test_bit(RESOLVE_CONFLICTS, &first_peer_device(device)->connection->flags);
b411b363
PR
2618
2619 if (iass)
b30ab791 2620 drbd_start_resync(device, C_SYNC_SOURCE);
b411b363 2621 else
b30ab791 2622 _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
b411b363
PR
2623}
2624
3b98c0c2 2625int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
b411b363 2626{
a910b123 2627 struct drbd_config_context adm_ctx;
daeda1cc 2628 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
3b98c0c2 2629 struct resize_parms rs;
b30ab791 2630 struct drbd_device *device;
3b98c0c2 2631 enum drbd_ret_code retcode;
b411b363 2632 enum determine_dev_size dd;
d752b269 2633 bool change_al_layout = false;
6495d2c6 2634 enum dds_flags ddsf;
daeda1cc 2635 sector_t u_size;
3b98c0c2 2636 int err;
b411b363 2637
a910b123 2638 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
2639 if (!adm_ctx.reply_skb)
2640 return retcode;
2641 if (retcode != NO_ERROR)
9e276872 2642 goto finish;
3b98c0c2 2643
9e276872 2644 mutex_lock(&adm_ctx.resource->adm_mutex);
b30ab791
AG
2645 device = adm_ctx.device;
2646 if (!get_ldev(device)) {
d752b269
PR
2647 retcode = ERR_NO_DISK;
2648 goto fail;
2649 }
2650
3b98c0c2 2651 memset(&rs, 0, sizeof(struct resize_parms));
b30ab791
AG
2652 rs.al_stripes = device->ldev->md.al_stripes;
2653 rs.al_stripe_size = device->ldev->md.al_stripe_size_4k * 4;
3b98c0c2 2654 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
f399002e 2655 err = resize_parms_from_attrs(&rs, info);
b411b363 2656 if (err) {
3b98c0c2 2657 retcode = ERR_MANDATORY_TAG;
a910b123 2658 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
d752b269 2659 goto fail_ldev;
b411b363
PR
2660 }
2661 }
2662
b30ab791 2663 if (device->state.conn > C_CONNECTED) {
b411b363 2664 retcode = ERR_RESIZE_RESYNC;
d752b269 2665 goto fail_ldev;
b411b363 2666 }
b411b363 2667
b30ab791
AG
2668 if (device->state.role == R_SECONDARY &&
2669 device->state.peer == R_SECONDARY) {
b411b363 2670 retcode = ERR_NO_PRIMARY;
d752b269 2671 goto fail_ldev;
b411b363 2672 }
b411b363 2673
a6b32bc3 2674 if (rs.no_resync && first_peer_device(device)->connection->agreed_pro_version < 93) {
6495d2c6 2675 retcode = ERR_NEED_APV_93;
9bcd2521 2676 goto fail_ldev;
6495d2c6
PR
2677 }
2678
daeda1cc 2679 rcu_read_lock();
b30ab791 2680 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
daeda1cc
PR
2681 rcu_read_unlock();
2682 if (u_size != (sector_t)rs.resize_size) {
2683 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
2684 if (!new_disk_conf) {
778f271d 2685 retcode = ERR_NOMEM;
9bcd2521 2686 goto fail_ldev;
778f271d
PR
2687 }
2688 }
2689
b30ab791
AG
2690 if (device->ldev->md.al_stripes != rs.al_stripes ||
2691 device->ldev->md.al_stripe_size_4k != rs.al_stripe_size / 4) {
d752b269
PR
2692 u32 al_size_k = rs.al_stripes * rs.al_stripe_size;
2693
2694 if (al_size_k > (16 * 1024 * 1024)) {
2695 retcode = ERR_MD_LAYOUT_TOO_BIG;
2696 goto fail_ldev;
2697 }
2698
2699 if (al_size_k < MD_32kB_SECT/2) {
2700 retcode = ERR_MD_LAYOUT_TOO_SMALL;
2701 goto fail_ldev;
2702 }
2703
cdc6af8d 2704 if (device->state.conn != C_CONNECTED && !rs.resize_force) {
d752b269
PR
2705 retcode = ERR_MD_LAYOUT_CONNECTED;
2706 goto fail_ldev;
2707 }
2708
2709 change_al_layout = true;
2710 }
2711
b30ab791
AG
2712 if (device->ldev->known_size != drbd_get_capacity(device->ldev->backing_bdev))
2713 device->ldev->known_size = drbd_get_capacity(device->ldev->backing_bdev);
b411b363 2714
daeda1cc 2715 if (new_disk_conf) {
0500813f 2716 mutex_lock(&device->resource->conf_update);
b30ab791 2717 old_disk_conf = device->ldev->disk_conf;
daeda1cc
PR
2718 *new_disk_conf = *old_disk_conf;
2719 new_disk_conf->disk_size = (sector_t)rs.resize_size;
b30ab791 2720 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
0500813f 2721 mutex_unlock(&device->resource->conf_update);
daeda1cc
PR
2722 synchronize_rcu();
2723 kfree(old_disk_conf);
70644786 2724 new_disk_conf = NULL;
b411b363
PR
2725 }
2726
6495d2c6 2727 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
b30ab791
AG
2728 dd = drbd_determine_dev_size(device, ddsf, change_al_layout ? &rs : NULL);
2729 drbd_md_sync(device);
2730 put_ldev(device);
e96c9633 2731 if (dd == DS_ERROR) {
b411b363
PR
2732 retcode = ERR_NOMEM_BITMAP;
2733 goto fail;
d752b269
PR
2734 } else if (dd == DS_ERROR_SPACE_MD) {
2735 retcode = ERR_MD_LAYOUT_NO_FIT;
2736 goto fail;
2737 } else if (dd == DS_ERROR_SHRINK) {
2738 retcode = ERR_IMPLICIT_SHRINK;
2739 goto fail;
b411b363 2740 }
778f271d 2741
b30ab791 2742 if (device->state.conn == C_CONNECTED) {
e96c9633 2743 if (dd == DS_GREW)
b30ab791 2744 set_bit(RESIZE_PENDING, &device->flags);
b411b363 2745
69a22773
AG
2746 drbd_send_uuids(first_peer_device(device));
2747 drbd_send_sizes(first_peer_device(device), 1, ddsf);
778f271d
PR
2748 }
2749
b411b363 2750 fail:
9e276872
LE
2751 mutex_unlock(&adm_ctx.resource->adm_mutex);
2752 finish:
a910b123 2753 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363 2754 return 0;
b411b363 2755
9bcd2521 2756 fail_ldev:
b30ab791 2757 put_ldev(device);
70644786 2758 kfree(new_disk_conf);
9bcd2521 2759 goto fail;
b411b363 2760}
b411b363 2761
f399002e 2762int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
b411b363 2763{
a910b123 2764 struct drbd_config_context adm_ctx;
3b98c0c2 2765 enum drbd_ret_code retcode;
b57a1e27 2766 struct res_opts res_opts;
f399002e 2767 int err;
b411b363 2768
a910b123 2769 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
3b98c0c2
LE
2770 if (!adm_ctx.reply_skb)
2771 return retcode;
2772 if (retcode != NO_ERROR)
2773 goto fail;
b411b363 2774
eb6bea67 2775 res_opts = adm_ctx.resource->res_opts;
5979e361 2776 if (should_set_defaults(info))
b966b5dd 2777 set_res_opts_defaults(&res_opts);
b411b363 2778
b57a1e27 2779 err = res_opts_from_attrs(&res_opts, info);
c75b9b10 2780 if (err && err != -ENOMSG) {
b411b363 2781 retcode = ERR_MANDATORY_TAG;
a910b123 2782 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
b411b363
PR
2783 goto fail;
2784 }
2785
9e276872 2786 mutex_lock(&adm_ctx.resource->adm_mutex);
eb6bea67 2787 err = set_resource_options(adm_ctx.resource, &res_opts);
afbbfa88
AG
2788 if (err) {
2789 retcode = ERR_INVALID_REQUEST;
2790 if (err == -ENOMEM)
2791 retcode = ERR_NOMEM;
b411b363 2792 }
9e276872 2793 mutex_unlock(&adm_ctx.resource->adm_mutex);
b411b363 2794
b411b363 2795fail:
a910b123 2796 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2797 return 0;
2798}
2799
3b98c0c2 2800int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
b411b363 2801{
a910b123 2802 struct drbd_config_context adm_ctx;
b30ab791 2803 struct drbd_device *device;
3b98c0c2
LE
2804 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2805
a910b123 2806 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
2807 if (!adm_ctx.reply_skb)
2808 return retcode;
2809 if (retcode != NO_ERROR)
2810 goto out;
2811
b30ab791 2812 device = adm_ctx.device;
8fe39aac
PR
2813 if (!get_ldev(device)) {
2814 retcode = ERR_NO_DISK;
2815 goto out;
2816 }
2817
2818 mutex_lock(&adm_ctx.resource->adm_mutex);
b411b363 2819
194bfb32 2820 /* If there is still bitmap IO pending, probably because of a previous
7ee1fb93
LE
2821 * resync just being finished, wait for it before requesting a new resync.
2822 * Also wait for it's after_state_ch(). */
b30ab791
AG
2823 drbd_suspend_io(device);
2824 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
b5043c5e 2825 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
194bfb32 2826
0b2dafcd
PR
2827 /* If we happen to be C_STANDALONE R_SECONDARY, just change to
2828 * D_INCONSISTENT, and set all bits in the bitmap. Otherwise,
2829 * try to start a resync handshake as sync target for full sync.
9376d9f8 2830 */
b30ab791
AG
2831 if (device->state.conn == C_STANDALONE && device->state.role == R_SECONDARY) {
2832 retcode = drbd_request_state(device, NS(disk, D_INCONSISTENT));
0b2dafcd 2833 if (retcode >= SS_SUCCESS) {
b30ab791 2834 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
0b2dafcd
PR
2835 "set_n_write from invalidate", BM_LOCKED_MASK))
2836 retcode = ERR_IO_MD_DISK;
2837 }
2838 } else
b30ab791
AG
2839 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_T));
2840 drbd_resume_io(device);
9e276872 2841 mutex_unlock(&adm_ctx.resource->adm_mutex);
8fe39aac 2842 put_ldev(device);
3b98c0c2 2843out:
a910b123 2844 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2845 return 0;
2846}
2847
3b98c0c2
LE
2848static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2849 union drbd_state mask, union drbd_state val)
b411b363 2850{
a910b123 2851 struct drbd_config_context adm_ctx;
3b98c0c2 2852 enum drbd_ret_code retcode;
194bfb32 2853
a910b123 2854 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
2855 if (!adm_ctx.reply_skb)
2856 return retcode;
2857 if (retcode != NO_ERROR)
2858 goto out;
b411b363 2859
9e276872 2860 mutex_lock(&adm_ctx.resource->adm_mutex);
b30ab791 2861 retcode = drbd_request_state(adm_ctx.device, mask, val);
9e276872 2862 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 2863out:
a910b123 2864 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2865 return 0;
2866}
2867
8fe39aac 2868static int drbd_bmio_set_susp_al(struct drbd_device *device) __must_hold(local)
0778286a
PR
2869{
2870 int rv;
2871
b30ab791
AG
2872 rv = drbd_bmio_set_n_write(device);
2873 drbd_suspend_al(device);
0778286a
PR
2874 return rv;
2875}
2876
3b98c0c2 2877int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
b411b363 2878{
a910b123 2879 struct drbd_config_context adm_ctx;
25b0d6c8 2880 int retcode; /* drbd_ret_code, drbd_state_rv */
b30ab791 2881 struct drbd_device *device;
25b0d6c8 2882
a910b123 2883 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
25b0d6c8
PR
2884 if (!adm_ctx.reply_skb)
2885 return retcode;
2886 if (retcode != NO_ERROR)
2887 goto out;
2888
b30ab791 2889 device = adm_ctx.device;
8fe39aac
PR
2890 if (!get_ldev(device)) {
2891 retcode = ERR_NO_DISK;
2892 goto out;
2893 }
2894
2895 mutex_lock(&adm_ctx.resource->adm_mutex);
b411b363 2896
194bfb32 2897 /* If there is still bitmap IO pending, probably because of a previous
7ee1fb93
LE
2898 * resync just being finished, wait for it before requesting a new resync.
2899 * Also wait for it's after_state_ch(). */
b30ab791
AG
2900 drbd_suspend_io(device);
2901 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
b5043c5e 2902 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
194bfb32 2903
0b2dafcd
PR
2904 /* If we happen to be C_STANDALONE R_PRIMARY, just set all bits
2905 * in the bitmap. Otherwise, try to start a resync handshake
2906 * as sync source for full sync.
2907 */
b30ab791 2908 if (device->state.conn == C_STANDALONE && device->state.role == R_PRIMARY) {
0b2dafcd
PR
2909 /* The peer will get a resync upon connect anyways. Just make that
2910 into a full resync. */
b30ab791 2911 retcode = drbd_request_state(device, NS(pdsk, D_INCONSISTENT));
0b2dafcd 2912 if (retcode >= SS_SUCCESS) {
b30ab791 2913 if (drbd_bitmap_io(device, &drbd_bmio_set_susp_al,
0b2dafcd
PR
2914 "set_n_write from invalidate_peer",
2915 BM_LOCKED_SET_ALLOWED))
2916 retcode = ERR_IO_MD_DISK;
2917 }
2918 } else
b30ab791
AG
2919 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_S));
2920 drbd_resume_io(device);
9e276872 2921 mutex_unlock(&adm_ctx.resource->adm_mutex);
8fe39aac 2922 put_ldev(device);
25b0d6c8 2923out:
a910b123 2924 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2925 return 0;
2926}
2927
3b98c0c2 2928int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
b411b363 2929{
a910b123 2930 struct drbd_config_context adm_ctx;
3b98c0c2 2931 enum drbd_ret_code retcode;
b411b363 2932
a910b123 2933 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
2934 if (!adm_ctx.reply_skb)
2935 return retcode;
2936 if (retcode != NO_ERROR)
2937 goto out;
b411b363 2938
9e276872 2939 mutex_lock(&adm_ctx.resource->adm_mutex);
b30ab791 2940 if (drbd_request_state(adm_ctx.device, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
3b98c0c2 2941 retcode = ERR_PAUSE_IS_SET;
9e276872 2942 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 2943out:
a910b123 2944 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2945 return 0;
2946}
2947
3b98c0c2 2948int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
b411b363 2949{
a910b123 2950 struct drbd_config_context adm_ctx;
da9fbc27 2951 union drbd_dev_state s;
3b98c0c2
LE
2952 enum drbd_ret_code retcode;
2953
a910b123 2954 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
2955 if (!adm_ctx.reply_skb)
2956 return retcode;
2957 if (retcode != NO_ERROR)
2958 goto out;
b411b363 2959
9e276872 2960 mutex_lock(&adm_ctx.resource->adm_mutex);
b30ab791
AG
2961 if (drbd_request_state(adm_ctx.device, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2962 s = adm_ctx.device->state;
cd88d030
PR
2963 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2964 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2965 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2966 } else {
2967 retcode = ERR_PAUSE_IS_CLEAR;
2968 }
2969 }
9e276872 2970 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 2971out:
a910b123 2972 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2973 return 0;
2974}
2975
3b98c0c2 2976int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
b411b363 2977{
3b98c0c2 2978 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
b411b363
PR
2979}
2980
3b98c0c2 2981int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
b411b363 2982{
a910b123 2983 struct drbd_config_context adm_ctx;
b30ab791 2984 struct drbd_device *device;
3b98c0c2
LE
2985 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2986
a910b123 2987 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
2988 if (!adm_ctx.reply_skb)
2989 return retcode;
2990 if (retcode != NO_ERROR)
2991 goto out;
2992
9e276872 2993 mutex_lock(&adm_ctx.resource->adm_mutex);
b30ab791
AG
2994 device = adm_ctx.device;
2995 if (test_bit(NEW_CUR_UUID, &device->flags)) {
9fa48269
LE
2996 if (get_ldev_if_state(device, D_ATTACHING)) {
2997 drbd_uuid_new_current(device);
2998 put_ldev(device);
2999 } else {
3000 /* This is effectively a multi-stage "forced down".
3001 * The NEW_CUR_UUID bit is supposedly only set, if we
3002 * lost the replication connection, and are configured
3003 * to freeze IO and wait for some fence-peer handler.
3004 * So we still don't have a replication connection.
3005 * And now we don't have a local disk either. After
3006 * resume, we will fail all pending and new IO, because
3007 * we don't have any data anymore. Which means we will
3008 * eventually be able to terminate all users of this
3009 * device, and then take it down. By bumping the
3010 * "effective" data uuid, we make sure that you really
3011 * need to tear down before you reconfigure, we will
3012 * the refuse to re-connect or re-attach (because no
3013 * matching real data uuid exists).
3014 */
3015 u64 val;
3016 get_random_bytes(&val, sizeof(u64));
3017 drbd_set_ed_uuid(device, val);
3018 drbd_warn(device, "Resumed without access to data; please tear down before attempting to re-configure.\n");
3019 }
b30ab791 3020 clear_bit(NEW_CUR_UUID, &device->flags);
43a5182c 3021 }
b30ab791
AG
3022 drbd_suspend_io(device);
3023 retcode = drbd_request_state(device, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
3b98c0c2 3024 if (retcode == SS_SUCCESS) {
b30ab791 3025 if (device->state.conn < C_CONNECTED)
a6b32bc3 3026 tl_clear(first_peer_device(device)->connection);
b30ab791 3027 if (device->state.disk == D_DISKLESS || device->state.disk == D_FAILED)
a6b32bc3 3028 tl_restart(first_peer_device(device)->connection, FAIL_FROZEN_DISK_IO);
265be2d0 3029 }
b30ab791 3030 drbd_resume_io(device);
9e276872 3031 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 3032out:
a910b123 3033 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
3034 return 0;
3035}
3036
3b98c0c2 3037int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
b411b363 3038{
3b98c0c2 3039 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
b411b363
PR
3040}
3041
251b8f8e
AG
3042static int nla_put_drbd_cfg_context(struct sk_buff *skb,
3043 struct drbd_resource *resource,
3044 struct drbd_connection *connection,
3045 struct drbd_device *device)
b411b363 3046{
543cc10b
LE
3047 struct nlattr *nla;
3048 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
3049 if (!nla)
3050 goto nla_put_failure;
251b8f8e
AG
3051 if (device &&
3052 nla_put_u32(skb, T_ctx_volume, device->vnr))
26ec9287 3053 goto nla_put_failure;
f597f6b8 3054 if (nla_put_string(skb, T_ctx_resource_name, resource->name))
26ec9287 3055 goto nla_put_failure;
251b8f8e
AG
3056 if (connection) {
3057 if (connection->my_addr_len &&
3058 nla_put(skb, T_ctx_my_addr, connection->my_addr_len, &connection->my_addr))
3059 goto nla_put_failure;
3060 if (connection->peer_addr_len &&
3061 nla_put(skb, T_ctx_peer_addr, connection->peer_addr_len, &connection->peer_addr))
3062 goto nla_put_failure;
3063 }
543cc10b
LE
3064 nla_nest_end(skb, nla);
3065 return 0;
b411b363 3066
543cc10b
LE
3067nla_put_failure:
3068 if (nla)
3069 nla_nest_cancel(skb, nla);
3070 return -EMSGSIZE;
3071}
b411b363 3072
a55bbd37
AG
3073/*
3074 * The generic netlink dump callbacks are called outside the genl_lock(), so
3075 * they cannot use the simple attribute parsing code which uses global
3076 * attribute tables.
3077 */
3078static struct nlattr *find_cfg_context_attr(const struct nlmsghdr *nlh, int attr)
3079{
3080 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
3081 const int maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
3082 struct nlattr *nla;
3083
3084 nla = nla_find(nlmsg_attrdata(nlh, hdrlen), nlmsg_attrlen(nlh, hdrlen),
3085 DRBD_NLA_CFG_CONTEXT);
3086 if (!nla)
3087 return NULL;
3088 return drbd_nla_find_nested(maxtype, nla, __nla_type(attr));
3089}
3090
3091static void resource_to_info(struct resource_info *, struct drbd_resource *);
3092
3093int drbd_adm_dump_resources(struct sk_buff *skb, struct netlink_callback *cb)
3094{
3095 struct drbd_genlmsghdr *dh;
3096 struct drbd_resource *resource;
3097 struct resource_info resource_info;
3098 struct resource_statistics resource_statistics;
3099 int err;
3100
3101 rcu_read_lock();
3102 if (cb->args[0]) {
3103 for_each_resource_rcu(resource, &drbd_resources)
3104 if (resource == (struct drbd_resource *)cb->args[0])
3105 goto found_resource;
3106 err = 0; /* resource was probably deleted */
3107 goto out;
3108 }
3109 resource = list_entry(&drbd_resources,
3110 struct drbd_resource, resources);
3111
3112found_resource:
3113 list_for_each_entry_continue_rcu(resource, &drbd_resources, resources) {
3114 goto put_result;
3115 }
3116 err = 0;
3117 goto out;
3118
3119put_result:
3120 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3121 cb->nlh->nlmsg_seq, &drbd_genl_family,
3122 NLM_F_MULTI, DRBD_ADM_GET_RESOURCES);
3123 err = -ENOMEM;
3124 if (!dh)
3125 goto out;
3126 dh->minor = -1U;
3127 dh->ret_code = NO_ERROR;
3128 err = nla_put_drbd_cfg_context(skb, resource, NULL, NULL);
3129 if (err)
3130 goto out;
3131 err = res_opts_to_skb(skb, &resource->res_opts, !capable(CAP_SYS_ADMIN));
3132 if (err)
3133 goto out;
3134 resource_to_info(&resource_info, resource);
3135 err = resource_info_to_skb(skb, &resource_info, !capable(CAP_SYS_ADMIN));
3136 if (err)
3137 goto out;
3138 resource_statistics.res_stat_write_ordering = resource->write_ordering;
3139 err = resource_statistics_to_skb(skb, &resource_statistics, !capable(CAP_SYS_ADMIN));
3140 if (err)
3141 goto out;
3142 cb->args[0] = (long)resource;
3143 genlmsg_end(skb, dh);
3144 err = 0;
3145
3146out:
3147 rcu_read_unlock();
3148 if (err)
3149 return err;
3150 return skb->len;
3151}
3152
3153static void device_to_statistics(struct device_statistics *s,
3154 struct drbd_device *device)
3155{
3156 memset(s, 0, sizeof(*s));
3157 s->dev_upper_blocked = !may_inc_ap_bio(device);
3158 if (get_ldev(device)) {
3159 struct drbd_md *md = &device->ldev->md;
3160 u64 *history_uuids = (u64 *)s->history_uuids;
3161 struct request_queue *q;
3162 int n;
3163
3164 spin_lock_irq(&md->uuid_lock);
3165 s->dev_current_uuid = md->uuid[UI_CURRENT];
3166 BUILD_BUG_ON(sizeof(s->history_uuids) < UI_HISTORY_END - UI_HISTORY_START + 1);
3167 for (n = 0; n < UI_HISTORY_END - UI_HISTORY_START + 1; n++)
3168 history_uuids[n] = md->uuid[UI_HISTORY_START + n];
3169 for (; n < HISTORY_UUIDS; n++)
3170 history_uuids[n] = 0;
3171 s->history_uuids_len = HISTORY_UUIDS;
3172 spin_unlock_irq(&md->uuid_lock);
3173
3174 s->dev_disk_flags = md->flags;
3175 q = bdev_get_queue(device->ldev->backing_bdev);
3176 s->dev_lower_blocked =
3177 bdi_congested(&q->backing_dev_info,
3178 (1 << WB_async_congested) |
3179 (1 << WB_sync_congested));
3180 put_ldev(device);
3181 }
3182 s->dev_size = drbd_get_capacity(device->this_bdev);
3183 s->dev_read = device->read_cnt;
3184 s->dev_write = device->writ_cnt;
3185 s->dev_al_writes = device->al_writ_cnt;
3186 s->dev_bm_writes = device->bm_writ_cnt;
3187 s->dev_upper_pending = atomic_read(&device->ap_bio_cnt);
3188 s->dev_lower_pending = atomic_read(&device->local_cnt);
3189 s->dev_al_suspended = test_bit(AL_SUSPENDED, &device->flags);
3190 s->dev_exposed_data_uuid = device->ed_uuid;
3191}
3192
3193static int put_resource_in_arg0(struct netlink_callback *cb, int holder_nr)
3194{
3195 if (cb->args[0]) {
3196 struct drbd_resource *resource =
3197 (struct drbd_resource *)cb->args[0];
3198 kref_put(&resource->kref, drbd_destroy_resource);
3199 }
3200
3201 return 0;
3202}
3203
3204int drbd_adm_dump_devices_done(struct netlink_callback *cb) {
3205 return put_resource_in_arg0(cb, 7);
3206}
3207
3208static void device_to_info(struct device_info *, struct drbd_device *);
3209
3210int drbd_adm_dump_devices(struct sk_buff *skb, struct netlink_callback *cb)
3211{
3212 struct nlattr *resource_filter;
3213 struct drbd_resource *resource;
3214 struct drbd_device *uninitialized_var(device);
3215 int minor, err, retcode;
3216 struct drbd_genlmsghdr *dh;
3217 struct device_info device_info;
3218 struct device_statistics device_statistics;
3219 struct idr *idr_to_search;
3220
3221 resource = (struct drbd_resource *)cb->args[0];
3222 if (!cb->args[0] && !cb->args[1]) {
3223 resource_filter = find_cfg_context_attr(cb->nlh, T_ctx_resource_name);
3224 if (resource_filter) {
3225 retcode = ERR_RES_NOT_KNOWN;
3226 resource = drbd_find_resource(nla_data(resource_filter));
3227 if (!resource)
3228 goto put_result;
3229 cb->args[0] = (long)resource;
3230 }
3231 }
3232
3233 rcu_read_lock();
3234 minor = cb->args[1];
3235 idr_to_search = resource ? &resource->devices : &drbd_devices;
3236 device = idr_get_next(idr_to_search, &minor);
3237 if (!device) {
3238 err = 0;
3239 goto out;
3240 }
3241 idr_for_each_entry_continue(idr_to_search, device, minor) {
3242 retcode = NO_ERROR;
3243 goto put_result; /* only one iteration */
3244 }
3245 err = 0;
3246 goto out; /* no more devices */
3247
3248put_result:
3249 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3250 cb->nlh->nlmsg_seq, &drbd_genl_family,
3251 NLM_F_MULTI, DRBD_ADM_GET_DEVICES);
3252 err = -ENOMEM;
3253 if (!dh)
3254 goto out;
3255 dh->ret_code = retcode;
3256 dh->minor = -1U;
3257 if (retcode == NO_ERROR) {
3258 dh->minor = device->minor;
3259 err = nla_put_drbd_cfg_context(skb, device->resource, NULL, device);
3260 if (err)
3261 goto out;
3262 if (get_ldev(device)) {
3263 struct disk_conf *disk_conf =
3264 rcu_dereference(device->ldev->disk_conf);
3265
3266 err = disk_conf_to_skb(skb, disk_conf, !capable(CAP_SYS_ADMIN));
3267 put_ldev(device);
3268 if (err)
3269 goto out;
3270 }
3271 device_to_info(&device_info, device);
3272 err = device_info_to_skb(skb, &device_info, !capable(CAP_SYS_ADMIN));
3273 if (err)
3274 goto out;
3275
3276 device_to_statistics(&device_statistics, device);
3277 err = device_statistics_to_skb(skb, &device_statistics, !capable(CAP_SYS_ADMIN));
3278 if (err)
3279 goto out;
3280 cb->args[1] = minor + 1;
3281 }
3282 genlmsg_end(skb, dh);
3283 err = 0;
3284
3285out:
3286 rcu_read_unlock();
3287 if (err)
3288 return err;
3289 return skb->len;
3290}
3291
3292int drbd_adm_dump_connections_done(struct netlink_callback *cb)
3293{
3294 return put_resource_in_arg0(cb, 6);
3295}
3296
3297enum { SINGLE_RESOURCE, ITERATE_RESOURCES };
3298
3299int drbd_adm_dump_connections(struct sk_buff *skb, struct netlink_callback *cb)
3300{
3301 struct nlattr *resource_filter;
3302 struct drbd_resource *resource = NULL, *next_resource;
3303 struct drbd_connection *uninitialized_var(connection);
3304 int err = 0, retcode;
3305 struct drbd_genlmsghdr *dh;
3306 struct connection_info connection_info;
3307 struct connection_statistics connection_statistics;
3308
3309 rcu_read_lock();
3310 resource = (struct drbd_resource *)cb->args[0];
3311 if (!cb->args[0]) {
3312 resource_filter = find_cfg_context_attr(cb->nlh, T_ctx_resource_name);
3313 if (resource_filter) {
3314 retcode = ERR_RES_NOT_KNOWN;
3315 resource = drbd_find_resource(nla_data(resource_filter));
3316 if (!resource)
3317 goto put_result;
3318 cb->args[0] = (long)resource;
3319 cb->args[1] = SINGLE_RESOURCE;
3320 }
3321 }
3322 if (!resource) {
3323 if (list_empty(&drbd_resources))
3324 goto out;
3325 resource = list_first_entry(&drbd_resources, struct drbd_resource, resources);
3326 kref_get(&resource->kref);
3327 cb->args[0] = (long)resource;
3328 cb->args[1] = ITERATE_RESOURCES;
3329 }
3330
3331 next_resource:
3332 rcu_read_unlock();
3333 mutex_lock(&resource->conf_update);
3334 rcu_read_lock();
3335 if (cb->args[2]) {
3336 for_each_connection_rcu(connection, resource)
3337 if (connection == (struct drbd_connection *)cb->args[2])
3338 goto found_connection;
3339 /* connection was probably deleted */
3340 goto no_more_connections;
3341 }
3342 connection = list_entry(&resource->connections, struct drbd_connection, connections);
3343
3344found_connection:
3345 list_for_each_entry_continue_rcu(connection, &resource->connections, connections) {
3346 if (!has_net_conf(connection))
3347 continue;
3348 retcode = NO_ERROR;
3349 goto put_result; /* only one iteration */
3350 }
3351
3352no_more_connections:
3353 if (cb->args[1] == ITERATE_RESOURCES) {
3354 for_each_resource_rcu(next_resource, &drbd_resources) {
3355 if (next_resource == resource)
3356 goto found_resource;
3357 }
3358 /* resource was probably deleted */
3359 }
3360 goto out;
3361
3362found_resource:
3363 list_for_each_entry_continue_rcu(next_resource, &drbd_resources, resources) {
3364 mutex_unlock(&resource->conf_update);
3365 kref_put(&resource->kref, drbd_destroy_resource);
3366 resource = next_resource;
3367 kref_get(&resource->kref);
3368 cb->args[0] = (long)resource;
3369 cb->args[2] = 0;
3370 goto next_resource;
3371 }
3372 goto out; /* no more resources */
3373
3374put_result:
3375 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3376 cb->nlh->nlmsg_seq, &drbd_genl_family,
3377 NLM_F_MULTI, DRBD_ADM_GET_CONNECTIONS);
3378 err = -ENOMEM;
3379 if (!dh)
3380 goto out;
3381 dh->ret_code = retcode;
3382 dh->minor = -1U;
3383 if (retcode == NO_ERROR) {
3384 struct net_conf *net_conf;
3385
3386 err = nla_put_drbd_cfg_context(skb, resource, connection, NULL);
3387 if (err)
3388 goto out;
3389 net_conf = rcu_dereference(connection->net_conf);
3390 if (net_conf) {
3391 err = net_conf_to_skb(skb, net_conf, !capable(CAP_SYS_ADMIN));
3392 if (err)
3393 goto out;
3394 }
3395 connection_to_info(&connection_info, connection);
3396 err = connection_info_to_skb(skb, &connection_info, !capable(CAP_SYS_ADMIN));
3397 if (err)
3398 goto out;
3399 connection_statistics.conn_congested = test_bit(NET_CONGESTED, &connection->flags);
3400 err = connection_statistics_to_skb(skb, &connection_statistics, !capable(CAP_SYS_ADMIN));
3401 if (err)
3402 goto out;
3403 cb->args[2] = (long)connection;
3404 }
3405 genlmsg_end(skb, dh);
3406 err = 0;
3407
3408out:
3409 rcu_read_unlock();
3410 if (resource)
3411 mutex_unlock(&resource->conf_update);
3412 if (err)
3413 return err;
3414 return skb->len;
3415}
3416
3417enum mdf_peer_flag {
3418 MDF_PEER_CONNECTED = 1 << 0,
3419 MDF_PEER_OUTDATED = 1 << 1,
3420 MDF_PEER_FENCING = 1 << 2,
3421 MDF_PEER_FULL_SYNC = 1 << 3,
3422};
3423
3424static void peer_device_to_statistics(struct peer_device_statistics *s,
3425 struct drbd_peer_device *peer_device)
3426{
3427 struct drbd_device *device = peer_device->device;
3428
3429 memset(s, 0, sizeof(*s));
3430 s->peer_dev_received = device->recv_cnt;
3431 s->peer_dev_sent = device->send_cnt;
3432 s->peer_dev_pending = atomic_read(&device->ap_pending_cnt) +
3433 atomic_read(&device->rs_pending_cnt);
3434 s->peer_dev_unacked = atomic_read(&device->unacked_cnt);
3435 s->peer_dev_out_of_sync = drbd_bm_total_weight(device) << (BM_BLOCK_SHIFT - 9);
3436 s->peer_dev_resync_failed = device->rs_failed << (BM_BLOCK_SHIFT - 9);
3437 if (get_ldev(device)) {
3438 struct drbd_md *md = &device->ldev->md;
3439
3440 spin_lock_irq(&md->uuid_lock);
3441 s->peer_dev_bitmap_uuid = md->uuid[UI_BITMAP];
3442 spin_unlock_irq(&md->uuid_lock);
3443 s->peer_dev_flags =
3444 (drbd_md_test_flag(device->ldev, MDF_CONNECTED_IND) ?
3445 MDF_PEER_CONNECTED : 0) +
3446 (drbd_md_test_flag(device->ldev, MDF_CONSISTENT) &&
3447 !drbd_md_test_flag(device->ldev, MDF_WAS_UP_TO_DATE) ?
3448 MDF_PEER_OUTDATED : 0) +
3449 /* FIXME: MDF_PEER_FENCING? */
3450 (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC) ?
3451 MDF_PEER_FULL_SYNC : 0);
3452 put_ldev(device);
3453 }
3454}
3455
3456int drbd_adm_dump_peer_devices_done(struct netlink_callback *cb)
3457{
3458 return put_resource_in_arg0(cb, 9);
3459}
3460
3461int drbd_adm_dump_peer_devices(struct sk_buff *skb, struct netlink_callback *cb)
3462{
3463 struct nlattr *resource_filter;
3464 struct drbd_resource *resource;
3465 struct drbd_device *uninitialized_var(device);
3466 struct drbd_peer_device *peer_device = NULL;
3467 int minor, err, retcode;
3468 struct drbd_genlmsghdr *dh;
3469 struct idr *idr_to_search;
3470
3471 resource = (struct drbd_resource *)cb->args[0];
3472 if (!cb->args[0] && !cb->args[1]) {
3473 resource_filter = find_cfg_context_attr(cb->nlh, T_ctx_resource_name);
3474 if (resource_filter) {
3475 retcode = ERR_RES_NOT_KNOWN;
3476 resource = drbd_find_resource(nla_data(resource_filter));
3477 if (!resource)
3478 goto put_result;
3479 }
3480 cb->args[0] = (long)resource;
3481 }
3482
3483 rcu_read_lock();
3484 minor = cb->args[1];
3485 idr_to_search = resource ? &resource->devices : &drbd_devices;
3486 device = idr_find(idr_to_search, minor);
3487 if (!device) {
3488next_device:
3489 minor++;
3490 cb->args[2] = 0;
3491 device = idr_get_next(idr_to_search, &minor);
3492 if (!device) {
3493 err = 0;
3494 goto out;
3495 }
3496 }
3497 if (cb->args[2]) {
3498 for_each_peer_device(peer_device, device)
3499 if (peer_device == (struct drbd_peer_device *)cb->args[2])
3500 goto found_peer_device;
3501 /* peer device was probably deleted */
3502 goto next_device;
3503 }
3504 /* Make peer_device point to the list head (not the first entry). */
3505 peer_device = list_entry(&device->peer_devices, struct drbd_peer_device, peer_devices);
3506
3507found_peer_device:
3508 list_for_each_entry_continue_rcu(peer_device, &device->peer_devices, peer_devices) {
3509 if (!has_net_conf(peer_device->connection))
3510 continue;
3511 retcode = NO_ERROR;
3512 goto put_result; /* only one iteration */
3513 }
3514 goto next_device;
3515
3516put_result:
3517 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3518 cb->nlh->nlmsg_seq, &drbd_genl_family,
3519 NLM_F_MULTI, DRBD_ADM_GET_PEER_DEVICES);
3520 err = -ENOMEM;
3521 if (!dh)
3522 goto out;
3523 dh->ret_code = retcode;
3524 dh->minor = -1U;
3525 if (retcode == NO_ERROR) {
3526 struct peer_device_info peer_device_info;
3527 struct peer_device_statistics peer_device_statistics;
3528
3529 dh->minor = minor;
3530 err = nla_put_drbd_cfg_context(skb, device->resource, peer_device->connection, device);
3531 if (err)
3532 goto out;
3533 peer_device_to_info(&peer_device_info, peer_device);
3534 err = peer_device_info_to_skb(skb, &peer_device_info, !capable(CAP_SYS_ADMIN));
3535 if (err)
3536 goto out;
3537 peer_device_to_statistics(&peer_device_statistics, peer_device);
3538 err = peer_device_statistics_to_skb(skb, &peer_device_statistics, !capable(CAP_SYS_ADMIN));
3539 if (err)
3540 goto out;
3541 cb->args[1] = minor;
3542 cb->args[2] = (long)peer_device;
3543 }
3544 genlmsg_end(skb, dh);
3545 err = 0;
3546
3547out:
3548 rcu_read_unlock();
3549 if (err)
3550 return err;
3551 return skb->len;
3552}
251b8f8e
AG
3553/*
3554 * Return the connection of @resource if @resource has exactly one connection.
3555 */
3556static struct drbd_connection *the_only_connection(struct drbd_resource *resource)
3557{
3558 struct list_head *connections = &resource->connections;
3559
3560 if (list_empty(connections) || connections->next->next != connections)
3561 return NULL;
3562 return list_first_entry(&resource->connections, struct drbd_connection, connections);
3563}
3564
8ce953aa 3565static int nla_put_status_info(struct sk_buff *skb, struct drbd_device *device,
3b98c0c2 3566 const struct sib_info *sib)
b411b363 3567{
251b8f8e 3568 struct drbd_resource *resource = device->resource;
3b98c0c2
LE
3569 struct state_info *si = NULL; /* for sizeof(si->member); */
3570 struct nlattr *nla;
3571 int got_ldev;
3b98c0c2
LE
3572 int err = 0;
3573 int exclude_sensitive;
3574
3575 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
3576 * to. So we better exclude_sensitive information.
3577 *
3578 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
3579 * in the context of the requesting user process. Exclude sensitive
3580 * information, unless current has superuser.
3581 *
3582 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
3583 * relies on the current implementation of netlink_dump(), which
3584 * executes the dump callback successively from netlink_recvmsg(),
3585 * always in the context of the receiving process */
3586 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
3587
b30ab791 3588 got_ldev = get_ldev(device);
3b98c0c2
LE
3589
3590 /* We need to add connection name and volume number information still.
3591 * Minor number is in drbd_genlmsghdr. */
251b8f8e 3592 if (nla_put_drbd_cfg_context(skb, resource, the_only_connection(resource), device))
3b98c0c2 3593 goto nla_put_failure;
3b98c0c2 3594
eb6bea67 3595 if (res_opts_to_skb(skb, &device->resource->res_opts, exclude_sensitive))
f399002e
LE
3596 goto nla_put_failure;
3597
daeda1cc 3598 rcu_read_lock();
f9eb7bf4
AG
3599 if (got_ldev) {
3600 struct disk_conf *disk_conf;
44ed167d 3601
b30ab791 3602 disk_conf = rcu_dereference(device->ldev->disk_conf);
f9eb7bf4
AG
3603 err = disk_conf_to_skb(skb, disk_conf, exclude_sensitive);
3604 }
3605 if (!err) {
3606 struct net_conf *nc;
3607
a6b32bc3 3608 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
f9eb7bf4
AG
3609 if (nc)
3610 err = net_conf_to_skb(skb, nc, exclude_sensitive);
3611 }
44ed167d
PR
3612 rcu_read_unlock();
3613 if (err)
3614 goto nla_put_failure;
3b98c0c2 3615
3b98c0c2
LE
3616 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
3617 if (!nla)
3618 goto nla_put_failure;
26ec9287 3619 if (nla_put_u32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY) ||
b30ab791
AG
3620 nla_put_u32(skb, T_current_state, device->state.i) ||
3621 nla_put_u64(skb, T_ed_uuid, device->ed_uuid) ||
3622 nla_put_u64(skb, T_capacity, drbd_get_capacity(device->this_bdev)) ||
3623 nla_put_u64(skb, T_send_cnt, device->send_cnt) ||
3624 nla_put_u64(skb, T_recv_cnt, device->recv_cnt) ||
3625 nla_put_u64(skb, T_read_cnt, device->read_cnt) ||
3626 nla_put_u64(skb, T_writ_cnt, device->writ_cnt) ||
3627 nla_put_u64(skb, T_al_writ_cnt, device->al_writ_cnt) ||
3628 nla_put_u64(skb, T_bm_writ_cnt, device->bm_writ_cnt) ||
3629 nla_put_u32(skb, T_ap_bio_cnt, atomic_read(&device->ap_bio_cnt)) ||
3630 nla_put_u32(skb, T_ap_pending_cnt, atomic_read(&device->ap_pending_cnt)) ||
3631 nla_put_u32(skb, T_rs_pending_cnt, atomic_read(&device->rs_pending_cnt)))
26ec9287 3632 goto nla_put_failure;
3b98c0c2
LE
3633
3634 if (got_ldev) {
39a1aa7f 3635 int err;
b411b363 3636
b30ab791
AG
3637 spin_lock_irq(&device->ldev->md.uuid_lock);
3638 err = nla_put(skb, T_uuids, sizeof(si->uuids), device->ldev->md.uuid);
3639 spin_unlock_irq(&device->ldev->md.uuid_lock);
39a1aa7f
PR
3640
3641 if (err)
3642 goto nla_put_failure;
3643
b30ab791
AG
3644 if (nla_put_u32(skb, T_disk_flags, device->ldev->md.flags) ||
3645 nla_put_u64(skb, T_bits_total, drbd_bm_bits(device)) ||
3646 nla_put_u64(skb, T_bits_oos, drbd_bm_total_weight(device)))
26ec9287 3647 goto nla_put_failure;
b30ab791
AG
3648 if (C_SYNC_SOURCE <= device->state.conn &&
3649 C_PAUSED_SYNC_T >= device->state.conn) {
3650 if (nla_put_u64(skb, T_bits_rs_total, device->rs_total) ||
3651 nla_put_u64(skb, T_bits_rs_failed, device->rs_failed))
26ec9287 3652 goto nla_put_failure;
3b98c0c2 3653 }
b411b363 3654 }
b411b363 3655
3b98c0c2
LE
3656 if (sib) {
3657 switch(sib->sib_reason) {
3658 case SIB_SYNC_PROGRESS:
3659 case SIB_GET_STATUS_REPLY:
3660 break;
3661 case SIB_STATE_CHANGE:
26ec9287
AG
3662 if (nla_put_u32(skb, T_prev_state, sib->os.i) ||
3663 nla_put_u32(skb, T_new_state, sib->ns.i))
3664 goto nla_put_failure;
3b98c0c2
LE
3665 break;
3666 case SIB_HELPER_POST:
26ec9287
AG
3667 if (nla_put_u32(skb, T_helper_exit_code,
3668 sib->helper_exit_code))
3669 goto nla_put_failure;
3b98c0c2
LE
3670 /* fall through */
3671 case SIB_HELPER_PRE:
26ec9287
AG
3672 if (nla_put_string(skb, T_helper, sib->helper_name))
3673 goto nla_put_failure;
3b98c0c2
LE
3674 break;
3675 }
b411b363 3676 }
3b98c0c2 3677 nla_nest_end(skb, nla);
b411b363 3678
3b98c0c2
LE
3679 if (0)
3680nla_put_failure:
3681 err = -EMSGSIZE;
3682 if (got_ldev)
b30ab791 3683 put_ldev(device);
3b98c0c2 3684 return err;
b411b363
PR
3685}
3686
3b98c0c2 3687int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
b411b363 3688{
a910b123 3689 struct drbd_config_context adm_ctx;
3b98c0c2
LE
3690 enum drbd_ret_code retcode;
3691 int err;
b411b363 3692
a910b123 3693 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
3694 if (!adm_ctx.reply_skb)
3695 return retcode;
3696 if (retcode != NO_ERROR)
3697 goto out;
b411b363 3698
b30ab791 3699 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.device, NULL);
3b98c0c2
LE
3700 if (err) {
3701 nlmsg_free(adm_ctx.reply_skb);
3702 return err;
b411b363 3703 }
3b98c0c2 3704out:
a910b123 3705 drbd_adm_finish(&adm_ctx, info, retcode);
3b98c0c2 3706 return 0;
b411b363
PR
3707}
3708
4b7a530f 3709static int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
b411b363 3710{
b30ab791 3711 struct drbd_device *device;
3b98c0c2 3712 struct drbd_genlmsghdr *dh;
77c556f6
AG
3713 struct drbd_resource *pos = (struct drbd_resource *)cb->args[0];
3714 struct drbd_resource *resource = NULL;
77c556f6 3715 struct drbd_resource *tmp;
543cc10b
LE
3716 unsigned volume = cb->args[1];
3717
3718 /* Open coded, deferred, iteration:
77c556f6 3719 * for_each_resource_safe(resource, tmp, &drbd_resources) {
251b8f8e
AG
3720 * connection = "first connection of resource or undefined";
3721 * idr_for_each_entry(&resource->devices, device, i) {
543cc10b
LE
3722 * ...
3723 * }
3724 * }
77c556f6 3725 * where resource is cb->args[0];
543cc10b
LE
3726 * and i is cb->args[1];
3727 *
71932efc
LE
3728 * cb->args[2] indicates if we shall loop over all resources,
3729 * or just dump all volumes of a single resource.
3730 *
3b98c0c2
LE
3731 * This may miss entries inserted after this dump started,
3732 * or entries deleted before they are reached.
543cc10b 3733 *
b30ab791 3734 * We need to make sure the device won't disappear while
543cc10b
LE
3735 * we are looking at it, and revalidate our iterators
3736 * on each iteration.
3737 */
b411b363 3738
05a10ec7 3739 /* synchronize with conn_create()/drbd_destroy_connection() */
c141ebda 3740 rcu_read_lock();
543cc10b 3741 /* revalidate iterator position */
77c556f6 3742 for_each_resource_rcu(tmp, &drbd_resources) {
543cc10b
LE
3743 if (pos == NULL) {
3744 /* first iteration */
3745 pos = tmp;
77c556f6 3746 resource = pos;
543cc10b
LE
3747 break;
3748 }
3749 if (tmp == pos) {
77c556f6 3750 resource = pos;
543cc10b
LE
3751 break;
3752 }
b411b363 3753 }
77c556f6
AG
3754 if (resource) {
3755next_resource:
251b8f8e
AG
3756 device = idr_get_next(&resource->devices, &volume);
3757 if (!device) {
77c556f6
AG
3758 /* No more volumes to dump on this resource.
3759 * Advance resource iterator. */
3760 pos = list_entry_rcu(resource->resources.next,
3761 struct drbd_resource, resources);
3762 /* Did we dump any volume of this resource yet? */
543cc10b 3763 if (volume != 0) {
71932efc
LE
3764 /* If we reached the end of the list,
3765 * or only a single resource dump was requested,
3766 * we are done. */
77c556f6 3767 if (&pos->resources == &drbd_resources || cb->args[2])
71932efc 3768 goto out;
543cc10b 3769 volume = 0;
77c556f6
AG
3770 resource = pos;
3771 goto next_resource;
543cc10b
LE
3772 }
3773 }
3774
98683650 3775 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3b98c0c2
LE
3776 cb->nlh->nlmsg_seq, &drbd_genl_family,
3777 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
3778 if (!dh)
543cc10b
LE
3779 goto out;
3780
251b8f8e 3781 if (!device) {
bde89a9e 3782 /* This is a connection without a single volume.
367d675d
LE
3783 * Suprisingly enough, it may have a network
3784 * configuration. */
251b8f8e
AG
3785 struct drbd_connection *connection;
3786
543cc10b
LE
3787 dh->minor = -1U;
3788 dh->ret_code = NO_ERROR;
251b8f8e
AG
3789 connection = the_only_connection(resource);
3790 if (nla_put_drbd_cfg_context(skb, resource, connection, NULL))
367d675d 3791 goto cancel;
251b8f8e
AG
3792 if (connection) {
3793 struct net_conf *nc;
3794
3795 nc = rcu_dereference(connection->net_conf);
3796 if (nc && net_conf_to_skb(skb, nc, 1) != 0)
3797 goto cancel;
3798 }
367d675d 3799 goto done;
543cc10b 3800 }
b411b363 3801
0b0ba1ef 3802 D_ASSERT(device, device->vnr == volume);
251b8f8e 3803 D_ASSERT(device, device->resource == resource);
3b98c0c2 3804
b30ab791 3805 dh->minor = device_to_minor(device);
3b98c0c2
LE
3806 dh->ret_code = NO_ERROR;
3807
b30ab791 3808 if (nla_put_status_info(skb, device, NULL)) {
367d675d 3809cancel:
3b98c0c2 3810 genlmsg_cancel(skb, dh);
543cc10b 3811 goto out;
3b98c0c2 3812 }
367d675d 3813done:
3b98c0c2 3814 genlmsg_end(skb, dh);
bde89a9e 3815 }
b411b363 3816
543cc10b 3817out:
c141ebda 3818 rcu_read_unlock();
543cc10b 3819 /* where to start the next iteration */
bde89a9e 3820 cb->args[0] = (long)pos;
77c556f6 3821 cb->args[1] = (pos == resource) ? volume + 1 : 0;
b411b363 3822
77c556f6 3823 /* No more resources/volumes/minors found results in an empty skb.
543cc10b 3824 * Which will terminate the dump. */
3b98c0c2 3825 return skb->len;
b411b363
PR
3826}
3827
71932efc
LE
3828/*
3829 * Request status of all resources, or of all volumes within a single resource.
3830 *
3831 * This is a dump, as the answer may not fit in a single reply skb otherwise.
3832 * Which means we cannot use the family->attrbuf or other such members, because
3833 * dump is NOT protected by the genl_lock(). During dump, we only have access
3834 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
3835 *
3836 * Once things are setup properly, we call into get_one_status().
b411b363 3837 */
71932efc 3838int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
b411b363 3839{
71932efc
LE
3840 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
3841 struct nlattr *nla;
7c3063cc 3842 const char *resource_name;
4bc76048 3843 struct drbd_resource *resource;
7c3063cc 3844 int maxtype;
71932efc
LE
3845
3846 /* Is this a followup call? */
3847 if (cb->args[0]) {
3848 /* ... of a single resource dump,
3849 * and the resource iterator has been advanced already? */
3850 if (cb->args[2] && cb->args[2] != cb->args[0])
3851 return 0; /* DONE. */
3852 goto dump;
3853 }
3854
3855 /* First call (from netlink_dump_start). We need to figure out
3856 * which resource(s) the user wants us to dump. */
3857 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
3858 nlmsg_attrlen(cb->nlh, hdrlen),
3859 DRBD_NLA_CFG_CONTEXT);
3860
3861 /* No explicit context given. Dump all. */
3862 if (!nla)
3863 goto dump;
7c3063cc
AG
3864 maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
3865 nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
3866 if (IS_ERR(nla))
3867 return PTR_ERR(nla);
71932efc
LE
3868 /* context given, but no name present? */
3869 if (!nla)
3870 return -EINVAL;
7c3063cc 3871 resource_name = nla_data(nla);
4bc76048
AG
3872 if (!*resource_name)
3873 return -ENODEV;
3874 resource = drbd_find_resource(resource_name);
3875 if (!resource)
71932efc
LE
3876 return -ENODEV;
3877
4bc76048 3878 kref_put(&resource->kref, drbd_destroy_resource); /* get_one_status() revalidates the resource */
0ace9dfa 3879
71932efc 3880 /* prime iterators, and set "filter" mode mark:
bde89a9e 3881 * only dump this connection. */
4bc76048 3882 cb->args[0] = (long)resource;
71932efc 3883 /* cb->args[1] = 0; passed in this way. */
4bc76048 3884 cb->args[2] = (long)resource;
71932efc
LE
3885
3886dump:
3887 return get_one_status(skb, cb);
3888}
b411b363 3889
3b98c0c2 3890int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
b411b363 3891{
a910b123 3892 struct drbd_config_context adm_ctx;
3b98c0c2
LE
3893 enum drbd_ret_code retcode;
3894 struct timeout_parms tp;
3895 int err;
b411b363 3896
a910b123 3897 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
3898 if (!adm_ctx.reply_skb)
3899 return retcode;
3900 if (retcode != NO_ERROR)
3901 goto out;
b411b363 3902
3b98c0c2 3903 tp.timeout_type =
b30ab791
AG
3904 adm_ctx.device->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
3905 test_bit(USE_DEGR_WFC_T, &adm_ctx.device->flags) ? UT_DEGRADED :
3b98c0c2 3906 UT_DEFAULT;
b411b363 3907
3b98c0c2
LE
3908 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
3909 if (err) {
3910 nlmsg_free(adm_ctx.reply_skb);
3911 return err;
3912 }
3913out:
a910b123 3914 drbd_adm_finish(&adm_ctx, info, retcode);
3b98c0c2 3915 return 0;
b411b363
PR
3916}
3917
3b98c0c2 3918int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
b411b363 3919{
a910b123 3920 struct drbd_config_context adm_ctx;
b30ab791 3921 struct drbd_device *device;
3b98c0c2 3922 enum drbd_ret_code retcode;
58ffa580 3923 struct start_ov_parms parms;
b411b363 3924
a910b123 3925 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
3926 if (!adm_ctx.reply_skb)
3927 return retcode;
3928 if (retcode != NO_ERROR)
3929 goto out;
873b0d5f 3930
b30ab791 3931 device = adm_ctx.device;
58ffa580
LE
3932
3933 /* resume from last known position, if possible */
b30ab791 3934 parms.ov_start_sector = device->ov_start_sector;
58ffa580 3935 parms.ov_stop_sector = ULLONG_MAX;
3b98c0c2 3936 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
f399002e 3937 int err = start_ov_parms_from_attrs(&parms, info);
3b98c0c2
LE
3938 if (err) {
3939 retcode = ERR_MANDATORY_TAG;
a910b123 3940 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
3b98c0c2
LE
3941 goto out;
3942 }
b411b363 3943 }
9e276872
LE
3944 mutex_lock(&adm_ctx.resource->adm_mutex);
3945
58ffa580 3946 /* w_make_ov_request expects position to be aligned */
b30ab791
AG
3947 device->ov_start_sector = parms.ov_start_sector & ~(BM_SECT_PER_BIT-1);
3948 device->ov_stop_sector = parms.ov_stop_sector;
873b0d5f
LE
3949
3950 /* If there is still bitmap IO pending, e.g. previous resync or verify
3951 * just being finished, wait for it before requesting a new resync. */
b30ab791
AG
3952 drbd_suspend_io(device);
3953 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
3954 retcode = drbd_request_state(device, NS(conn, C_VERIFY_S));
3955 drbd_resume_io(device);
9e276872
LE
3956
3957 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 3958out:
a910b123 3959 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
3960 return 0;
3961}
3962
3963
3b98c0c2 3964int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
b411b363 3965{
a910b123 3966 struct drbd_config_context adm_ctx;
b30ab791 3967 struct drbd_device *device;
3b98c0c2 3968 enum drbd_ret_code retcode;
b411b363
PR
3969 int skip_initial_sync = 0;
3970 int err;
3b98c0c2 3971 struct new_c_uuid_parms args;
b411b363 3972
a910b123 3973 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
3974 if (!adm_ctx.reply_skb)
3975 return retcode;
3976 if (retcode != NO_ERROR)
3977 goto out_nolock;
b411b363 3978
b30ab791 3979 device = adm_ctx.device;
3b98c0c2
LE
3980 memset(&args, 0, sizeof(args));
3981 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
f399002e 3982 err = new_c_uuid_parms_from_attrs(&args, info);
3b98c0c2
LE
3983 if (err) {
3984 retcode = ERR_MANDATORY_TAG;
a910b123 3985 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
3b98c0c2
LE
3986 goto out_nolock;
3987 }
b411b363
PR
3988 }
3989
9e276872 3990 mutex_lock(&adm_ctx.resource->adm_mutex);
b30ab791 3991 mutex_lock(device->state_mutex); /* Protects us against serialized state changes. */
b411b363 3992
b30ab791 3993 if (!get_ldev(device)) {
b411b363
PR
3994 retcode = ERR_NO_DISK;
3995 goto out;
3996 }
3997
3998 /* this is "skip initial sync", assume to be clean */
a6b32bc3
AG
3999 if (device->state.conn == C_CONNECTED &&
4000 first_peer_device(device)->connection->agreed_pro_version >= 90 &&
b30ab791 4001 device->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
d0180171 4002 drbd_info(device, "Preparing to skip initial sync\n");
b411b363 4003 skip_initial_sync = 1;
b30ab791 4004 } else if (device->state.conn != C_STANDALONE) {
b411b363
PR
4005 retcode = ERR_CONNECTED;
4006 goto out_dec;
4007 }
4008
b30ab791
AG
4009 drbd_uuid_set(device, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
4010 drbd_uuid_new_current(device); /* New current, previous to UI_BITMAP */
b411b363
PR
4011
4012 if (args.clear_bm) {
b30ab791 4013 err = drbd_bitmap_io(device, &drbd_bmio_clear_n_write,
20ceb2b2 4014 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
b411b363 4015 if (err) {
d0180171 4016 drbd_err(device, "Writing bitmap failed with %d\n", err);
b411b363
PR
4017 retcode = ERR_IO_MD_DISK;
4018 }
4019 if (skip_initial_sync) {
69a22773 4020 drbd_send_uuids_skip_initial_sync(first_peer_device(device));
b30ab791
AG
4021 _drbd_uuid_set(device, UI_BITMAP, 0);
4022 drbd_print_uuids(device, "cleared bitmap UUID");
0500813f 4023 spin_lock_irq(&device->resource->req_lock);
b30ab791 4024 _drbd_set_state(_NS2(device, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
b411b363 4025 CS_VERBOSE, NULL);
0500813f 4026 spin_unlock_irq(&device->resource->req_lock);
b411b363
PR
4027 }
4028 }
4029
b30ab791 4030 drbd_md_sync(device);
b411b363 4031out_dec:
b30ab791 4032 put_ldev(device);
b411b363 4033out:
b30ab791 4034 mutex_unlock(device->state_mutex);
9e276872 4035 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 4036out_nolock:
a910b123 4037 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
4038 return 0;
4039}
4040
3b98c0c2 4041static enum drbd_ret_code
a910b123 4042drbd_check_resource_name(struct drbd_config_context *adm_ctx)
b411b363 4043{
a910b123 4044 const char *name = adm_ctx->resource_name;
3b98c0c2 4045 if (!name || !name[0]) {
a910b123 4046 drbd_msg_put_info(adm_ctx->reply_skb, "resource name missing");
3b98c0c2 4047 return ERR_MANDATORY_TAG;
b411b363 4048 }
3b98c0c2
LE
4049 /* if we want to use these in sysfs/configfs/debugfs some day,
4050 * we must not allow slashes */
4051 if (strchr(name, '/')) {
a910b123 4052 drbd_msg_put_info(adm_ctx->reply_skb, "invalid resource name");
3b98c0c2 4053 return ERR_INVALID_REQUEST;
b411b363 4054 }
3b98c0c2 4055 return NO_ERROR;
774b3055 4056}
b411b363 4057
a2972846
AG
4058static void resource_to_info(struct resource_info *info,
4059 struct drbd_resource *resource)
4060{
4061 info->res_role = conn_highest_role(first_connection(resource));
4062 info->res_susp = resource->susp;
4063 info->res_susp_nod = resource->susp_nod;
4064 info->res_susp_fen = resource->susp_fen;
4065}
4066
789c1b62 4067int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
b411b363 4068{
a2972846 4069 struct drbd_connection *connection;
a910b123 4070 struct drbd_config_context adm_ctx;
3b98c0c2 4071 enum drbd_ret_code retcode;
afbbfa88
AG
4072 struct res_opts res_opts;
4073 int err;
b411b363 4074
a910b123 4075 retcode = drbd_adm_prepare(&adm_ctx, skb, info, 0);
3b98c0c2
LE
4076 if (!adm_ctx.reply_skb)
4077 return retcode;
4078 if (retcode != NO_ERROR)
4079 goto out;
b411b363 4080
afbbfa88
AG
4081 set_res_opts_defaults(&res_opts);
4082 err = res_opts_from_attrs(&res_opts, info);
4083 if (err && err != -ENOMSG) {
4084 retcode = ERR_MANDATORY_TAG;
a910b123 4085 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
afbbfa88 4086 goto out;
b411b363
PR
4087 }
4088
a910b123 4089 retcode = drbd_check_resource_name(&adm_ctx);
3b98c0c2
LE
4090 if (retcode != NO_ERROR)
4091 goto out;
b411b363 4092
5c661042 4093 if (adm_ctx.resource) {
38f19616
LE
4094 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
4095 retcode = ERR_INVALID_REQUEST;
a910b123 4096 drbd_msg_put_info(adm_ctx.reply_skb, "resource exists");
38f19616
LE
4097 }
4098 /* else: still NO_ERROR */
3b98c0c2 4099 goto out;
b411b363 4100 }
b411b363 4101
9e276872 4102 /* not yet safe for genl_family.parallel_ops */
28bc3b8c 4103 mutex_lock(&resources_mutex);
a2972846 4104 connection = conn_create(adm_ctx.resource_name, &res_opts);
28bc3b8c 4105 mutex_unlock(&resources_mutex);
a2972846
AG
4106
4107 if (connection) {
4108 struct resource_info resource_info;
4109
4110 mutex_lock(&notification_mutex);
4111 resource_to_info(&resource_info, connection->resource);
4112 notify_resource_state(NULL, 0, connection->resource,
4113 &resource_info, NOTIFY_CREATE);
4114 mutex_unlock(&notification_mutex);
4115 } else
4116 retcode = ERR_NOMEM;
4117
3b98c0c2 4118out:
a910b123 4119 drbd_adm_finish(&adm_ctx, info, retcode);
3b98c0c2 4120 return 0;
b411b363
PR
4121}
4122
a2972846
AG
4123static void device_to_info(struct device_info *info,
4124 struct drbd_device *device)
4125{
4126 info->dev_disk_state = device->state.disk;
4127}
4128
4129
05a10ec7 4130int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info)
b411b363 4131{
a910b123 4132 struct drbd_config_context adm_ctx;
3b98c0c2
LE
4133 struct drbd_genlmsghdr *dh = info->userhdr;
4134 enum drbd_ret_code retcode;
b411b363 4135
a910b123 4136 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
3b98c0c2
LE
4137 if (!adm_ctx.reply_skb)
4138 return retcode;
4139 if (retcode != NO_ERROR)
4140 goto out;
b411b363 4141
f2257a56 4142 if (dh->minor > MINORMASK) {
a910b123 4143 drbd_msg_put_info(adm_ctx.reply_skb, "requested minor out of range");
3b98c0c2
LE
4144 retcode = ERR_INVALID_REQUEST;
4145 goto out;
b411b363 4146 }
0c8e36d9 4147 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
a910b123 4148 drbd_msg_put_info(adm_ctx.reply_skb, "requested volume id out of range");
3b98c0c2
LE
4149 retcode = ERR_INVALID_REQUEST;
4150 goto out;
b411b363 4151 }
b411b363 4152
38f19616 4153 /* drbd_adm_prepare made sure already
a6b32bc3 4154 * that first_peer_device(device)->connection and device->vnr match the request. */
b30ab791 4155 if (adm_ctx.device) {
38f19616 4156 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
179e20b8 4157 retcode = ERR_MINOR_OR_VOLUME_EXISTS;
38f19616
LE
4158 /* else: still NO_ERROR */
4159 goto out;
b411b363 4160 }
38f19616 4161
9e276872 4162 mutex_lock(&adm_ctx.resource->adm_mutex);
a910b123 4163 retcode = drbd_create_device(&adm_ctx, dh->minor);
a2972846
AG
4164 if (retcode == NO_ERROR) {
4165 struct drbd_device *device;
4166 struct drbd_peer_device *peer_device;
4167 struct device_info info;
4168 unsigned int peer_devices = 0;
4169 enum drbd_notification_type flags;
4170
4171 device = minor_to_device(dh->minor);
4172 for_each_peer_device(peer_device, device) {
4173 if (!has_net_conf(peer_device->connection))
4174 continue;
4175 peer_devices++;
4176 }
4177
4178 device_to_info(&info, device);
4179 mutex_lock(&notification_mutex);
4180 flags = (peer_devices--) ? NOTIFY_CONTINUES : 0;
4181 notify_device_state(NULL, 0, device, &info, NOTIFY_CREATE | flags);
4182 for_each_peer_device(peer_device, device) {
4183 struct peer_device_info peer_device_info;
4184
4185 if (!has_net_conf(peer_device->connection))
4186 continue;
4187 peer_device_to_info(&peer_device_info, peer_device);
4188 flags = (peer_devices--) ? NOTIFY_CONTINUES : 0;
4189 notify_peer_device_state(NULL, 0, peer_device, &peer_device_info,
4190 NOTIFY_CREATE | flags);
4191 }
4192 mutex_unlock(&notification_mutex);
4193 }
9e276872 4194 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 4195out:
a910b123 4196 drbd_adm_finish(&adm_ctx, info, retcode);
3b98c0c2 4197 return 0;
b411b363
PR
4198}
4199
05a10ec7 4200static enum drbd_ret_code adm_del_minor(struct drbd_device *device)
b411b363 4201{
a2972846
AG
4202 struct drbd_peer_device *peer_device;
4203
b30ab791
AG
4204 if (device->state.disk == D_DISKLESS &&
4205 /* no need to be device->state.conn == C_STANDALONE &&
85f75dd7
LE
4206 * we may want to delete a minor from a live replication group.
4207 */
b30ab791 4208 device->state.role == R_SECONDARY) {
a2972846
AG
4209 struct drbd_connection *connection =
4210 first_connection(device->resource);
4211
b30ab791 4212 _drbd_request_state(device, NS(conn, C_WF_REPORT_PARAMS),
369bea63 4213 CS_VERBOSE + CS_WAIT_COMPLETE);
a2972846
AG
4214
4215 /* If the state engine hasn't stopped the sender thread yet, we
4216 * need to flush the sender work queue before generating the
4217 * DESTROY events here. */
4218 if (get_t_state(&connection->worker) == RUNNING)
4219 drbd_flush_workqueue(&connection->sender_work);
4220
4221 mutex_lock(&notification_mutex);
4222 for_each_peer_device(peer_device, device) {
4223 if (!has_net_conf(peer_device->connection))
4224 continue;
4225 notify_peer_device_state(NULL, 0, peer_device, NULL,
4226 NOTIFY_DESTROY | NOTIFY_CONTINUES);
4227 }
4228 notify_device_state(NULL, 0, device, NULL, NOTIFY_DESTROY);
4229 mutex_unlock(&notification_mutex);
4230
f82795d6 4231 drbd_delete_device(device);
85f75dd7
LE
4232 return NO_ERROR;
4233 } else
4234 return ERR_MINOR_CONFIGURED;
b411b363
PR
4235}
4236
05a10ec7 4237int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info)
b411b363 4238{
a910b123 4239 struct drbd_config_context adm_ctx;
3b98c0c2 4240 enum drbd_ret_code retcode;
b411b363 4241
a910b123 4242 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
4243 if (!adm_ctx.reply_skb)
4244 return retcode;
4245 if (retcode != NO_ERROR)
4246 goto out;
b411b363 4247
9e276872 4248 mutex_lock(&adm_ctx.resource->adm_mutex);
05a10ec7 4249 retcode = adm_del_minor(adm_ctx.device);
9e276872 4250 mutex_unlock(&adm_ctx.resource->adm_mutex);
85f75dd7 4251out:
a910b123 4252 drbd_adm_finish(&adm_ctx, info, retcode);
85f75dd7 4253 return 0;
b411b363
PR
4254}
4255
179e20b8
AG
4256static int adm_del_resource(struct drbd_resource *resource)
4257{
4258 struct drbd_connection *connection;
4259
4260 for_each_connection(connection, resource) {
4261 if (connection->cstate > C_STANDALONE)
4262 return ERR_NET_CONFIGURED;
4263 }
4264 if (!idr_is_empty(&resource->devices))
4265 return ERR_RES_IN_USE;
4266
a2972846
AG
4267 /* The state engine has stopped the sender thread, so we don't
4268 * need to flush the sender work queue before generating the
4269 * DESTROY event here. */
4270 mutex_lock(&notification_mutex);
4271 notify_resource_state(NULL, 0, resource, NULL, NOTIFY_DESTROY);
4272 mutex_unlock(&notification_mutex);
4273
28bc3b8c 4274 mutex_lock(&resources_mutex);
179e20b8 4275 list_del_rcu(&resource->resources);
28bc3b8c 4276 mutex_unlock(&resources_mutex);
179e20b8
AG
4277 /* Make sure all threads have actually stopped: state handling only
4278 * does drbd_thread_stop_nowait(). */
4279 list_for_each_entry(connection, &resource->connections, connections)
4280 drbd_thread_stop(&connection->worker);
4281 synchronize_rcu();
4282 drbd_free_resource(resource);
4283 return NO_ERROR;
4284}
4285
85f75dd7 4286int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
b411b363 4287{
a910b123 4288 struct drbd_config_context adm_ctx;
b6f85ef9
AG
4289 struct drbd_resource *resource;
4290 struct drbd_connection *connection;
4291 struct drbd_device *device;
f3dfa40a 4292 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
85f75dd7 4293 unsigned i;
b411b363 4294
a910b123 4295 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
85f75dd7
LE
4296 if (!adm_ctx.reply_skb)
4297 return retcode;
4298 if (retcode != NO_ERROR)
9e276872 4299 goto finish;
b411b363 4300
b6f85ef9 4301 resource = adm_ctx.resource;
9e276872 4302 mutex_lock(&resource->adm_mutex);
85f75dd7 4303 /* demote */
b6f85ef9
AG
4304 for_each_connection(connection, resource) {
4305 struct drbd_peer_device *peer_device;
4306
4307 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
4308 retcode = drbd_set_role(peer_device->device, R_SECONDARY, 0);
4309 if (retcode < SS_SUCCESS) {
a910b123 4310 drbd_msg_put_info(adm_ctx.reply_skb, "failed to demote");
b6f85ef9
AG
4311 goto out;
4312 }
4313 }
4314
4315 retcode = conn_try_disconnect(connection, 0);
85f75dd7 4316 if (retcode < SS_SUCCESS) {
a910b123 4317 drbd_msg_put_info(adm_ctx.reply_skb, "failed to disconnect");
c141ebda 4318 goto out;
85f75dd7 4319 }
b411b363 4320 }
b411b363 4321
85f75dd7 4322 /* detach */
b6f85ef9
AG
4323 idr_for_each_entry(&resource->devices, device, i) {
4324 retcode = adm_detach(device, 0);
27012382 4325 if (retcode < SS_SUCCESS || retcode > NO_ERROR) {
a910b123 4326 drbd_msg_put_info(adm_ctx.reply_skb, "failed to detach");
c141ebda 4327 goto out;
85f75dd7
LE
4328 }
4329 }
b411b363 4330
85f75dd7 4331 /* delete volumes */
b6f85ef9
AG
4332 idr_for_each_entry(&resource->devices, device, i) {
4333 retcode = adm_del_minor(device);
85f75dd7
LE
4334 if (retcode != NO_ERROR) {
4335 /* "can not happen" */
a910b123 4336 drbd_msg_put_info(adm_ctx.reply_skb, "failed to delete volume");
ef356262 4337 goto out;
85f75dd7
LE
4338 }
4339 }
b411b363 4340
179e20b8 4341 retcode = adm_del_resource(resource);
3b98c0c2 4342out:
9e276872
LE
4343 mutex_unlock(&resource->adm_mutex);
4344finish:
a910b123 4345 drbd_adm_finish(&adm_ctx, info, retcode);
3b98c0c2 4346 return 0;
b411b363
PR
4347}
4348
789c1b62 4349int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
b411b363 4350{
a910b123 4351 struct drbd_config_context adm_ctx;
77c556f6 4352 struct drbd_resource *resource;
3b98c0c2 4353 enum drbd_ret_code retcode;
b411b363 4354
a910b123 4355 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
3b98c0c2
LE
4356 if (!adm_ctx.reply_skb)
4357 return retcode;
4358 if (retcode != NO_ERROR)
9e276872 4359 goto finish;
77c556f6 4360 resource = adm_ctx.resource;
b411b363 4361
179e20b8
AG
4362 mutex_lock(&resource->adm_mutex);
4363 retcode = adm_del_resource(resource);
9e276872
LE
4364 mutex_unlock(&resource->adm_mutex);
4365finish:
a910b123 4366 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
4367 return 0;
4368}
4369
b30ab791 4370void drbd_bcast_event(struct drbd_device *device, const struct sib_info *sib)
b411b363 4371{
3b98c0c2
LE
4372 struct sk_buff *msg;
4373 struct drbd_genlmsghdr *d_out;
4374 unsigned seq;
4375 int err = -ENOMEM;
4376
4377 seq = atomic_inc_return(&drbd_genl_seq);
4378 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4379 if (!msg)
4380 goto failed;
4381
4382 err = -EMSGSIZE;
4383 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
4384 if (!d_out) /* cannot happen, but anyways. */
4385 goto nla_put_failure;
b30ab791 4386 d_out->minor = device_to_minor(device);
6f9b5f84 4387 d_out->ret_code = NO_ERROR;
3b98c0c2 4388
b30ab791 4389 if (nla_put_status_info(msg, device, sib))
3b98c0c2
LE
4390 goto nla_put_failure;
4391 genlmsg_end(msg, d_out);
d38f8612 4392 err = drbd_genl_multicast_events(msg, GFP_NOWAIT);
3b98c0c2
LE
4393 /* msg has been consumed or freed in netlink_broadcast() */
4394 if (err && err != -ESRCH)
4395 goto failed;
b411b363 4396
3b98c0c2 4397 return;
b411b363 4398
3b98c0c2
LE
4399nla_put_failure:
4400 nlmsg_free(msg);
4401failed:
d0180171 4402 drbd_err(device, "Error %d while broadcasting event. "
3b98c0c2
LE
4403 "Event seq:%u sib_reason:%u\n",
4404 err, seq, sib->sib_reason);
b411b363 4405}
a2972846 4406
a2972846
AG
4407static int nla_put_notification_header(struct sk_buff *msg,
4408 enum drbd_notification_type type)
4409{
4410 struct drbd_notification_header nh = {
4411 .nh_type = type,
4412 };
4413
4414 return drbd_notification_header_to_skb(msg, &nh, true);
4415}
4416
4417void notify_resource_state(struct sk_buff *skb,
4418 unsigned int seq,
4419 struct drbd_resource *resource,
4420 struct resource_info *resource_info,
4421 enum drbd_notification_type type)
4422{
4423 struct resource_statistics resource_statistics;
4424 struct drbd_genlmsghdr *dh;
4425 bool multicast = false;
4426 int err;
4427
4428 if (!skb) {
4429 seq = atomic_inc_return(&notify_genl_seq);
4430 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4431 err = -ENOMEM;
4432 if (!skb)
4433 goto failed;
4434 multicast = true;
4435 }
4436
4437 err = -EMSGSIZE;
4438 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_RESOURCE_STATE);
4439 if (!dh)
4440 goto nla_put_failure;
4441 dh->minor = -1U;
4442 dh->ret_code = NO_ERROR;
4443 if (nla_put_drbd_cfg_context(skb, resource, NULL, NULL) ||
4444 nla_put_notification_header(skb, type) ||
4445 ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY &&
4446 resource_info_to_skb(skb, resource_info, true)))
4447 goto nla_put_failure;
4448 resource_statistics.res_stat_write_ordering = resource->write_ordering;
4449 err = resource_statistics_to_skb(skb, &resource_statistics, !capable(CAP_SYS_ADMIN));
4450 if (err)
4451 goto nla_put_failure;
4452 genlmsg_end(skb, dh);
4453 if (multicast) {
d38f8612 4454 err = drbd_genl_multicast_events(skb, GFP_NOWAIT);
a2972846
AG
4455 /* skb has been consumed or freed in netlink_broadcast() */
4456 if (err && err != -ESRCH)
4457 goto failed;
4458 }
4459 return;
4460
4461nla_put_failure:
4462 nlmsg_free(skb);
4463failed:
4464 drbd_err(resource, "Error %d while broadcasting event. Event seq:%u\n",
4465 err, seq);
4466}
4467
4468void notify_device_state(struct sk_buff *skb,
4469 unsigned int seq,
4470 struct drbd_device *device,
4471 struct device_info *device_info,
4472 enum drbd_notification_type type)
4473{
4474 struct device_statistics device_statistics;
4475 struct drbd_genlmsghdr *dh;
4476 bool multicast = false;
4477 int err;
4478
4479 if (!skb) {
4480 seq = atomic_inc_return(&notify_genl_seq);
4481 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4482 err = -ENOMEM;
4483 if (!skb)
4484 goto failed;
4485 multicast = true;
4486 }
4487
4488 err = -EMSGSIZE;
4489 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_DEVICE_STATE);
4490 if (!dh)
4491 goto nla_put_failure;
4492 dh->minor = device->minor;
4493 dh->ret_code = NO_ERROR;
4494 if (nla_put_drbd_cfg_context(skb, device->resource, NULL, device) ||
4495 nla_put_notification_header(skb, type) ||
4496 ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY &&
4497 device_info_to_skb(skb, device_info, true)))
4498 goto nla_put_failure;
4499 device_to_statistics(&device_statistics, device);
4500 device_statistics_to_skb(skb, &device_statistics, !capable(CAP_SYS_ADMIN));
4501 genlmsg_end(skb, dh);
4502 if (multicast) {
d38f8612 4503 err = drbd_genl_multicast_events(skb, GFP_NOWAIT);
a2972846
AG
4504 /* skb has been consumed or freed in netlink_broadcast() */
4505 if (err && err != -ESRCH)
4506 goto failed;
4507 }
4508 return;
4509
4510nla_put_failure:
4511 nlmsg_free(skb);
4512failed:
4513 drbd_err(device, "Error %d while broadcasting event. Event seq:%u\n",
4514 err, seq);
4515}
4516
4517void notify_connection_state(struct sk_buff *skb,
4518 unsigned int seq,
4519 struct drbd_connection *connection,
4520 struct connection_info *connection_info,
4521 enum drbd_notification_type type)
4522{
4523 struct connection_statistics connection_statistics;
4524 struct drbd_genlmsghdr *dh;
4525 bool multicast = false;
4526 int err;
4527
4528 if (!skb) {
4529 seq = atomic_inc_return(&notify_genl_seq);
4530 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4531 err = -ENOMEM;
4532 if (!skb)
4533 goto failed;
4534 multicast = true;
4535 }
4536
4537 err = -EMSGSIZE;
4538 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_CONNECTION_STATE);
4539 if (!dh)
4540 goto nla_put_failure;
4541 dh->minor = -1U;
4542 dh->ret_code = NO_ERROR;
4543 if (nla_put_drbd_cfg_context(skb, connection->resource, connection, NULL) ||
4544 nla_put_notification_header(skb, type) ||
4545 ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY &&
4546 connection_info_to_skb(skb, connection_info, true)))
4547 goto nla_put_failure;
4548 connection_statistics.conn_congested = test_bit(NET_CONGESTED, &connection->flags);
4549 connection_statistics_to_skb(skb, &connection_statistics, !capable(CAP_SYS_ADMIN));
4550 genlmsg_end(skb, dh);
4551 if (multicast) {
d38f8612 4552 err = drbd_genl_multicast_events(skb, GFP_NOWAIT);
a2972846
AG
4553 /* skb has been consumed or freed in netlink_broadcast() */
4554 if (err && err != -ESRCH)
4555 goto failed;
4556 }
4557 return;
4558
4559nla_put_failure:
4560 nlmsg_free(skb);
4561failed:
4562 drbd_err(connection, "Error %d while broadcasting event. Event seq:%u\n",
4563 err, seq);
4564}
4565
4566void notify_peer_device_state(struct sk_buff *skb,
4567 unsigned int seq,
4568 struct drbd_peer_device *peer_device,
4569 struct peer_device_info *peer_device_info,
4570 enum drbd_notification_type type)
4571{
4572 struct peer_device_statistics peer_device_statistics;
4573 struct drbd_resource *resource = peer_device->device->resource;
4574 struct drbd_genlmsghdr *dh;
4575 bool multicast = false;
4576 int err;
4577
4578 if (!skb) {
4579 seq = atomic_inc_return(&notify_genl_seq);
4580 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4581 err = -ENOMEM;
4582 if (!skb)
4583 goto failed;
4584 multicast = true;
4585 }
4586
4587 err = -EMSGSIZE;
4588 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_PEER_DEVICE_STATE);
4589 if (!dh)
4590 goto nla_put_failure;
4591 dh->minor = -1U;
4592 dh->ret_code = NO_ERROR;
4593 if (nla_put_drbd_cfg_context(skb, resource, peer_device->connection, peer_device->device) ||
4594 nla_put_notification_header(skb, type) ||
4595 ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY &&
4596 peer_device_info_to_skb(skb, peer_device_info, true)))
4597 goto nla_put_failure;
4598 peer_device_to_statistics(&peer_device_statistics, peer_device);
4599 peer_device_statistics_to_skb(skb, &peer_device_statistics, !capable(CAP_SYS_ADMIN));
4600 genlmsg_end(skb, dh);
4601 if (multicast) {
d38f8612 4602 err = drbd_genl_multicast_events(skb, GFP_NOWAIT);
a2972846
AG
4603 /* skb has been consumed or freed in netlink_broadcast() */
4604 if (err && err != -ESRCH)
4605 goto failed;
4606 }
4607 return;
4608
4609nla_put_failure:
4610 nlmsg_free(skb);
4611failed:
4612 drbd_err(peer_device, "Error %d while broadcasting event. Event seq:%u\n",
4613 err, seq);
4614}
4615
4616void notify_helper(enum drbd_notification_type type,
4617 struct drbd_device *device, struct drbd_connection *connection,
4618 const char *name, int status)
4619{
4620 struct drbd_resource *resource = device ? device->resource : connection->resource;
4621 struct drbd_helper_info helper_info;
4622 unsigned int seq = atomic_inc_return(&notify_genl_seq);
4623 struct sk_buff *skb = NULL;
4624 struct drbd_genlmsghdr *dh;
4625 int err;
4626
4627 strlcpy(helper_info.helper_name, name, sizeof(helper_info.helper_name));
4628 helper_info.helper_name_len = min(strlen(name), sizeof(helper_info.helper_name));
4629 helper_info.helper_status = status;
4630
4631 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4632 err = -ENOMEM;
4633 if (!skb)
4634 goto fail;
4635
4636 err = -EMSGSIZE;
4637 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_HELPER);
4638 if (!dh)
4639 goto fail;
4640 dh->minor = device ? device->minor : -1;
4641 dh->ret_code = NO_ERROR;
4642 mutex_lock(&notification_mutex);
4643 if (nla_put_drbd_cfg_context(skb, resource, connection, device) ||
4644 nla_put_notification_header(skb, type) ||
4645 drbd_helper_info_to_skb(skb, &helper_info, true))
4646 goto unlock_fail;
4647 genlmsg_end(skb, dh);
d38f8612 4648 err = drbd_genl_multicast_events(skb, GFP_NOWAIT);
a2972846
AG
4649 skb = NULL;
4650 /* skb has been consumed or freed in netlink_broadcast() */
4651 if (err && err != -ESRCH)
4652 goto unlock_fail;
4653 mutex_unlock(&notification_mutex);
4654 return;
4655
4656unlock_fail:
4657 mutex_unlock(&notification_mutex);
4658fail:
4659 nlmsg_free(skb);
4660 drbd_err(resource, "Error %d while broadcasting event. Event seq:%u\n",
4661 err, seq);
4662}
4663
4664static void notify_initial_state_done(struct sk_buff *skb, unsigned int seq)
4665{
4666 struct drbd_genlmsghdr *dh;
4667 int err;
4668
4669 err = -EMSGSIZE;
4670 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_INITIAL_STATE_DONE);
4671 if (!dh)
4672 goto nla_put_failure;
4673 dh->minor = -1U;
4674 dh->ret_code = NO_ERROR;
4675 if (nla_put_notification_header(skb, NOTIFY_EXISTS))
4676 goto nla_put_failure;
4677 genlmsg_end(skb, dh);
4678 return;
4679
4680nla_put_failure:
4681 nlmsg_free(skb);
4682 pr_err("Error %d sending event. Event seq:%u\n", err, seq);
4683}
4684
4685static void free_state_changes(struct list_head *list)
4686{
4687 while (!list_empty(list)) {
4688 struct drbd_state_change *state_change =
4689 list_first_entry(list, struct drbd_state_change, list);
4690 list_del(&state_change->list);
4691 forget_state_change(state_change);
4692 }
4693}
4694
4695static unsigned int notifications_for_state_change(struct drbd_state_change *state_change)
4696{
4697 return 1 +
4698 state_change->n_connections +
4699 state_change->n_devices +
4700 state_change->n_devices * state_change->n_connections;
4701}
4702
4703static int get_initial_state(struct sk_buff *skb, struct netlink_callback *cb)
4704{
4705 struct drbd_state_change *state_change = (struct drbd_state_change *)cb->args[0];
4706 unsigned int seq = cb->args[2];
4707 unsigned int n;
4708 enum drbd_notification_type flags = 0;
4709
4710 /* There is no need for taking notification_mutex here: it doesn't
4711 matter if the initial state events mix with later state chage
4712 events; we can always tell the events apart by the NOTIFY_EXISTS
4713 flag. */
4714
4715 cb->args[5]--;
4716 if (cb->args[5] == 1) {
4717 notify_initial_state_done(skb, seq);
4718 goto out;
4719 }
4720 n = cb->args[4]++;
4721 if (cb->args[4] < cb->args[3])
4722 flags |= NOTIFY_CONTINUES;
4723 if (n < 1) {
4724 notify_resource_state_change(skb, seq, state_change->resource,
4725 NOTIFY_EXISTS | flags);
4726 goto next;
4727 }
4728 n--;
4729 if (n < state_change->n_connections) {
4730 notify_connection_state_change(skb, seq, &state_change->connections[n],
4731 NOTIFY_EXISTS | flags);
4732 goto next;
4733 }
4734 n -= state_change->n_connections;
4735 if (n < state_change->n_devices) {
4736 notify_device_state_change(skb, seq, &state_change->devices[n],
4737 NOTIFY_EXISTS | flags);
4738 goto next;
4739 }
4740 n -= state_change->n_devices;
4741 if (n < state_change->n_devices * state_change->n_connections) {
4742 notify_peer_device_state_change(skb, seq, &state_change->peer_devices[n],
4743 NOTIFY_EXISTS | flags);
4744 goto next;
4745 }
4746
4747next:
4748 if (cb->args[4] == cb->args[3]) {
4749 struct drbd_state_change *next_state_change =
4750 list_entry(state_change->list.next,
4751 struct drbd_state_change, list);
4752 cb->args[0] = (long)next_state_change;
4753 cb->args[3] = notifications_for_state_change(next_state_change);
4754 cb->args[4] = 0;
4755 }
4756out:
4757 return skb->len;
4758}
4759
4760int drbd_adm_get_initial_state(struct sk_buff *skb, struct netlink_callback *cb)
4761{
4762 struct drbd_resource *resource;
4763 LIST_HEAD(head);
4764
4765 if (cb->args[5] >= 1) {
4766 if (cb->args[5] > 1)
4767 return get_initial_state(skb, cb);
4768 if (cb->args[0]) {
4769 struct drbd_state_change *state_change =
4770 (struct drbd_state_change *)cb->args[0];
4771
4772 /* connect list to head */
4773 list_add(&head, &state_change->list);
4774 free_state_changes(&head);
4775 }
4776 return 0;
4777 }
4778
4779 cb->args[5] = 2; /* number of iterations */
4780 mutex_lock(&resources_mutex);
4781 for_each_resource(resource, &drbd_resources) {
4782 struct drbd_state_change *state_change;
4783
4784 state_change = remember_old_state(resource, GFP_KERNEL);
4785 if (!state_change) {
4786 if (!list_empty(&head))
4787 free_state_changes(&head);
4788 mutex_unlock(&resources_mutex);
4789 return -ENOMEM;
4790 }
4791 copy_old_to_new_state_change(state_change);
4792 list_add_tail(&state_change->list, &head);
4793 cb->args[5] += notifications_for_state_change(state_change);
4794 }
4795 mutex_unlock(&resources_mutex);
4796
4797 if (!list_empty(&head)) {
4798 struct drbd_state_change *state_change =
4799 list_entry(head.next, struct drbd_state_change, list);
4800 cb->args[0] = (long)state_change;
4801 cb->args[3] = notifications_for_state_change(state_change);
4802 list_del(&head); /* detach list from head */
4803 }
4804
4805 cb->args[2] = cb->nlh->nlmsg_seq;
4806 return get_initial_state(skb, cb);
4807}