]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/block/drbd/drbd_nl.c
drbd: Backport the "status" command
[mirror_ubuntu-artful-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. */
b30ab791 868void drbd_suspend_io(struct drbd_device *device)
b411b363 869{
b30ab791
AG
870 set_bit(SUSPEND_IO, &device->flags);
871 if (drbd_suspended(device))
265be2d0 872 return;
b30ab791 873 wait_event(device->misc_wait, !atomic_read(&device->ap_bio_cnt));
b411b363
PR
874}
875
b30ab791 876void drbd_resume_io(struct drbd_device *device)
b411b363 877{
b30ab791
AG
878 clear_bit(SUSPEND_IO, &device->flags);
879 wake_up(&device->misc_wait);
b411b363
PR
880}
881
882/**
883 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
b30ab791 884 * @device: DRBD device.
b411b363
PR
885 *
886 * Returns 0 on success, negative return values indicate errors.
887 * You should call drbd_md_sync() after calling this function.
888 */
d752b269 889enum determine_dev_size
b30ab791 890drbd_determine_dev_size(struct drbd_device *device, enum dds_flags flags, struct resize_parms *rs) __must_hold(local)
b411b363
PR
891{
892 sector_t prev_first_sect, prev_size; /* previous meta location */
cccac985 893 sector_t la_size_sect, u_size;
b30ab791 894 struct drbd_md *md = &device->ldev->md;
d752b269
PR
895 u32 prev_al_stripe_size_4k;
896 u32 prev_al_stripes;
b411b363
PR
897 sector_t size;
898 char ppb[10];
d752b269 899 void *buffer;
b411b363
PR
900
901 int md_moved, la_size_changed;
e96c9633 902 enum determine_dev_size rv = DS_UNCHANGED;
b411b363
PR
903
904 /* race:
905 * application request passes inc_ap_bio,
906 * but then cannot get an AL-reference.
907 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
908 *
909 * to avoid that:
910 * Suspend IO right here.
911 * still lock the act_log to not trigger ASSERTs there.
912 */
b30ab791 913 drbd_suspend_io(device);
e37d2438 914 buffer = drbd_md_get_buffer(device, __func__); /* Lock meta-data IO */
d752b269 915 if (!buffer) {
b30ab791 916 drbd_resume_io(device);
d752b269
PR
917 return DS_ERROR;
918 }
b411b363
PR
919
920 /* no wait necessary anymore, actually we could assert that */
b30ab791 921 wait_event(device->al_wait, lc_try_lock(device->act_log));
b411b363 922
b30ab791
AG
923 prev_first_sect = drbd_md_first_sector(device->ldev);
924 prev_size = device->ldev->md.md_size_sect;
925 la_size_sect = device->ldev->md.la_size_sect;
b411b363 926
d752b269
PR
927 if (rs) {
928 /* rs is non NULL if we should change the AL layout only */
929
930 prev_al_stripes = md->al_stripes;
931 prev_al_stripe_size_4k = md->al_stripe_size_4k;
932
933 md->al_stripes = rs->al_stripes;
934 md->al_stripe_size_4k = rs->al_stripe_size / 4;
935 md->al_size_4k = (u64)rs->al_stripes * rs->al_stripe_size / 4;
936 }
937
b30ab791 938 drbd_md_set_sector_offsets(device, device->ldev);
b411b363 939
daeda1cc 940 rcu_read_lock();
b30ab791 941 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
daeda1cc 942 rcu_read_unlock();
b30ab791 943 size = drbd_new_dev_size(device, device->ldev, u_size, flags & DDSF_FORCED);
b411b363 944
d752b269
PR
945 if (size < la_size_sect) {
946 if (rs && u_size == 0) {
947 /* Remove "rs &&" later. This check should always be active, but
948 right now the receiver expects the permissive behavior */
d0180171 949 drbd_warn(device, "Implicit shrink not allowed. "
d752b269
PR
950 "Use --size=%llus for explicit shrink.\n",
951 (unsigned long long)size);
952 rv = DS_ERROR_SHRINK;
953 }
954 if (u_size > size)
955 rv = DS_ERROR_SPACE_MD;
956 if (rv != DS_UNCHANGED)
957 goto err_out;
958 }
959
b30ab791
AG
960 if (drbd_get_capacity(device->this_bdev) != size ||
961 drbd_bm_capacity(device) != size) {
b411b363 962 int err;
b30ab791 963 err = drbd_bm_resize(device, size, !(flags & DDSF_NO_RESYNC));
b411b363
PR
964 if (unlikely(err)) {
965 /* currently there is only one error: ENOMEM! */
b30ab791 966 size = drbd_bm_capacity(device)>>1;
b411b363 967 if (size == 0) {
d0180171 968 drbd_err(device, "OUT OF MEMORY! "
b411b363
PR
969 "Could not allocate bitmap!\n");
970 } else {
d0180171 971 drbd_err(device, "BM resizing failed. "
b411b363
PR
972 "Leaving size unchanged at size = %lu KB\n",
973 (unsigned long)size);
974 }
e96c9633 975 rv = DS_ERROR;
b411b363
PR
976 }
977 /* racy, see comments above. */
b30ab791
AG
978 drbd_set_my_capacity(device, size);
979 device->ldev->md.la_size_sect = size;
d0180171 980 drbd_info(device, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
b411b363
PR
981 (unsigned long long)size>>1);
982 }
d752b269
PR
983 if (rv <= DS_ERROR)
984 goto err_out;
b411b363 985
b30ab791 986 la_size_changed = (la_size_sect != device->ldev->md.la_size_sect);
b411b363 987
b30ab791
AG
988 md_moved = prev_first_sect != drbd_md_first_sector(device->ldev)
989 || prev_size != device->ldev->md.md_size_sect;
b411b363 990
d752b269
PR
991 if (la_size_changed || md_moved || rs) {
992 u32 prev_flags;
24dccabb 993
fcb09674
LE
994 /* We do some synchronous IO below, which may take some time.
995 * Clear the timer, to avoid scary "timer expired!" messages,
996 * "Superblock" is written out at least twice below, anyways. */
997 del_timer(&device->md_sync_timer);
b30ab791 998 drbd_al_shrink(device); /* All extents inactive. */
d752b269
PR
999
1000 prev_flags = md->flags;
1001 md->flags &= ~MDF_PRIMARY_IND;
b30ab791 1002 drbd_md_write(device, buffer);
d752b269 1003
d0180171 1004 drbd_info(device, "Writing the whole bitmap, %s\n",
b411b363
PR
1005 la_size_changed && md_moved ? "size changed and md moved" :
1006 la_size_changed ? "size changed" : "md moved");
20ceb2b2 1007 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
b30ab791 1008 drbd_bitmap_io(device, md_moved ? &drbd_bm_write_all : &drbd_bm_write,
d752b269 1009 "size changed", BM_LOCKED_MASK);
b30ab791 1010 drbd_initialize_al(device, buffer);
d752b269
PR
1011
1012 md->flags = prev_flags;
b30ab791 1013 drbd_md_write(device, buffer);
d752b269
PR
1014
1015 if (rs)
d0180171
AG
1016 drbd_info(device, "Changed AL layout to al-stripes = %d, al-stripe-size-kB = %d\n",
1017 md->al_stripes, md->al_stripe_size_4k * 4);
b411b363
PR
1018 }
1019
cccac985 1020 if (size > la_size_sect)
57737adc 1021 rv = la_size_sect ? DS_GREW : DS_GREW_FROM_ZERO;
cccac985 1022 if (size < la_size_sect)
e96c9633 1023 rv = DS_SHRUNK;
d752b269
PR
1024
1025 if (0) {
1026 err_out:
1027 if (rs) {
1028 md->al_stripes = prev_al_stripes;
1029 md->al_stripe_size_4k = prev_al_stripe_size_4k;
1030 md->al_size_4k = (u64)prev_al_stripes * prev_al_stripe_size_4k;
1031
b30ab791 1032 drbd_md_set_sector_offsets(device, device->ldev);
d752b269
PR
1033 }
1034 }
b30ab791
AG
1035 lc_unlock(device->act_log);
1036 wake_up(&device->al_wait);
1037 drbd_md_put_buffer(device);
1038 drbd_resume_io(device);
b411b363
PR
1039
1040 return rv;
1041}
1042
1043sector_t
b30ab791 1044drbd_new_dev_size(struct drbd_device *device, struct drbd_backing_dev *bdev,
ef5e44a6 1045 sector_t u_size, int assume_peer_has_space)
b411b363 1046{
b30ab791 1047 sector_t p_size = device->p_size; /* partner's disk size. */
cccac985 1048 sector_t la_size_sect = bdev->md.la_size_sect; /* last agreed size. */
b411b363 1049 sector_t m_size; /* my size */
b411b363
PR
1050 sector_t size = 0;
1051
1052 m_size = drbd_get_max_capacity(bdev);
1053
b30ab791 1054 if (device->state.conn < C_CONNECTED && assume_peer_has_space) {
d0180171 1055 drbd_warn(device, "Resize while not connected was forced by the user!\n");
a393db6f
PR
1056 p_size = m_size;
1057 }
1058
b411b363
PR
1059 if (p_size && m_size) {
1060 size = min_t(sector_t, p_size, m_size);
1061 } else {
cccac985
LE
1062 if (la_size_sect) {
1063 size = la_size_sect;
b411b363
PR
1064 if (m_size && m_size < size)
1065 size = m_size;
1066 if (p_size && p_size < size)
1067 size = p_size;
1068 } else {
1069 if (m_size)
1070 size = m_size;
1071 if (p_size)
1072 size = p_size;
1073 }
1074 }
1075
1076 if (size == 0)
d0180171 1077 drbd_err(device, "Both nodes diskless!\n");
b411b363
PR
1078
1079 if (u_size) {
1080 if (u_size > size)
d0180171 1081 drbd_err(device, "Requested disk size is too big (%lu > %lu)\n",
b411b363
PR
1082 (unsigned long)u_size>>1, (unsigned long)size>>1);
1083 else
1084 size = u_size;
1085 }
1086
1087 return size;
1088}
1089
1090/**
1091 * drbd_check_al_size() - Ensures that the AL is of the right size
b30ab791 1092 * @device: DRBD device.
b411b363
PR
1093 *
1094 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
1095 * failed, and 0 on success. You should call drbd_md_sync() after you called
1096 * this function.
1097 */
b30ab791 1098static int drbd_check_al_size(struct drbd_device *device, struct disk_conf *dc)
b411b363
PR
1099{
1100 struct lru_cache *n, *t;
1101 struct lc_element *e;
1102 unsigned int in_use;
1103 int i;
1104
b30ab791
AG
1105 if (device->act_log &&
1106 device->act_log->nr_elements == dc->al_extents)
b411b363
PR
1107 return 0;
1108
1109 in_use = 0;
b30ab791 1110 t = device->act_log;
7ad651b5 1111 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
f399002e 1112 dc->al_extents, sizeof(struct lc_element), 0);
b411b363
PR
1113
1114 if (n == NULL) {
d0180171 1115 drbd_err(device, "Cannot allocate act_log lru!\n");
b411b363
PR
1116 return -ENOMEM;
1117 }
b30ab791 1118 spin_lock_irq(&device->al_lock);
b411b363
PR
1119 if (t) {
1120 for (i = 0; i < t->nr_elements; i++) {
1121 e = lc_element_by_index(t, i);
1122 if (e->refcnt)
d0180171 1123 drbd_err(device, "refcnt(%d)==%d\n",
b411b363
PR
1124 e->lc_number, e->refcnt);
1125 in_use += e->refcnt;
1126 }
1127 }
1128 if (!in_use)
b30ab791
AG
1129 device->act_log = n;
1130 spin_unlock_irq(&device->al_lock);
b411b363 1131 if (in_use) {
d0180171 1132 drbd_err(device, "Activity log still in use!\n");
b411b363
PR
1133 lc_destroy(n);
1134 return -EBUSY;
1135 } else {
1136 if (t)
1137 lc_destroy(t);
1138 }
b30ab791 1139 drbd_md_mark_dirty(device); /* we changed device->act_log->nr_elemens */
b411b363
PR
1140 return 0;
1141}
1142
8fe39aac
PR
1143static void drbd_setup_queue_param(struct drbd_device *device, struct drbd_backing_dev *bdev,
1144 unsigned int max_bio_size)
b411b363 1145{
b30ab791 1146 struct request_queue * const q = device->rq_queue;
db141b2f
LE
1147 unsigned int max_hw_sectors = max_bio_size >> 9;
1148 unsigned int max_segments = 0;
c1b3156f 1149 struct request_queue *b = NULL;
99432fcc 1150
8fe39aac
PR
1151 if (bdev) {
1152 b = bdev->backing_bdev->bd_disk->queue;
99432fcc
PR
1153
1154 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
daeda1cc 1155 rcu_read_lock();
b30ab791 1156 max_segments = rcu_dereference(device->ldev->disk_conf)->max_bio_bvecs;
daeda1cc 1157 rcu_read_unlock();
c1b3156f
PR
1158
1159 blk_set_stacking_limits(&q->limits);
1160 blk_queue_max_write_same_sectors(q, 0);
99432fcc 1161 }
b411b363 1162
b411b363 1163 blk_queue_logical_block_size(q, 512);
1816a2b4
LE
1164 blk_queue_max_hw_sectors(q, max_hw_sectors);
1165 /* This is the workaround for "bio would need to, but cannot, be split" */
1166 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
1167 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
b411b363 1168
c1b3156f 1169 if (b) {
20c68fde
LE
1170 struct drbd_connection *connection = first_peer_device(device)->connection;
1171
1172 if (blk_queue_discard(b) &&
1173 (connection->cstate < C_CONNECTED || connection->agreed_features & FF_TRIM)) {
20c68fde
LE
1174 /* For now, don't allow more than one activity log extent worth of data
1175 * to be discarded in one go. We may need to rework drbd_al_begin_io()
1176 * to allow for even larger discard ranges */
2bb4cd5c 1177 blk_queue_max_discard_sectors(q, DRBD_MAX_DISCARD_SECTORS);
20c68fde
LE
1178
1179 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
1180 /* REALLY? Is stacking secdiscard "legal"? */
1181 if (blk_queue_secdiscard(b))
1182 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q);
1183 } else {
2bb4cd5c 1184 blk_queue_max_discard_sectors(q, 0);
20c68fde
LE
1185 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
1186 queue_flag_clear_unlocked(QUEUE_FLAG_SECDISCARD, q);
1187 }
99432fcc
PR
1188
1189 blk_queue_stack_limits(q, b);
1190
1191 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
d0180171 1192 drbd_info(device, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
99432fcc
PR
1193 q->backing_dev_info.ra_pages,
1194 b->backing_dev_info.ra_pages);
1195 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1196 }
99432fcc
PR
1197 }
1198}
1199
8fe39aac 1200void drbd_reconsider_max_bio_size(struct drbd_device *device, struct drbd_backing_dev *bdev)
99432fcc 1201{
db141b2f 1202 unsigned int now, new, local, peer;
99432fcc 1203
b30ab791
AG
1204 now = queue_max_hw_sectors(device->rq_queue) << 9;
1205 local = device->local_max_bio_size; /* Eventually last known value, from volatile memory */
1206 peer = device->peer_max_bio_size; /* Eventually last known value, from meta data */
b411b363 1207
8fe39aac
PR
1208 if (bdev) {
1209 local = queue_max_hw_sectors(bdev->backing_bdev->bd_disk->queue) << 9;
b30ab791 1210 device->local_max_bio_size = local;
b411b363 1211 }
db141b2f 1212 local = min(local, DRBD_MAX_BIO_SIZE);
99432fcc
PR
1213
1214 /* We may ignore peer limits if the peer is modern enough.
1215 Because new from 8.3.8 onwards the peer can use multiple
1216 BIOs for a single peer_request */
b30ab791 1217 if (device->state.conn >= C_WF_REPORT_PARAMS) {
a6b32bc3 1218 if (first_peer_device(device)->connection->agreed_pro_version < 94)
b30ab791 1219 peer = min(device->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
6809384c 1220 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
a6b32bc3 1221 else if (first_peer_device(device)->connection->agreed_pro_version == 94)
99432fcc 1222 peer = DRBD_MAX_SIZE_H80_PACKET;
a6b32bc3 1223 else if (first_peer_device(device)->connection->agreed_pro_version < 100)
2ffca4f3
PR
1224 peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
1225 else
99432fcc 1226 peer = DRBD_MAX_BIO_SIZE;
99432fcc 1227
fa090e70
LE
1228 /* We may later detach and re-attach on a disconnected Primary.
1229 * Avoid this setting to jump back in that case.
1230 * We want to store what we know the peer DRBD can handle,
1231 * not what the peer IO backend can handle. */
1232 if (peer > device->peer_max_bio_size)
1233 device->peer_max_bio_size = peer;
1234 }
db141b2f 1235 new = min(local, peer);
99432fcc 1236
b30ab791 1237 if (device->state.role == R_PRIMARY && new < now)
d0180171 1238 drbd_err(device, "ASSERT FAILED new < now; (%u < %u)\n", new, now);
99432fcc
PR
1239
1240 if (new != now)
d0180171 1241 drbd_info(device, "max BIO size = %u\n", new);
99432fcc 1242
8fe39aac 1243 drbd_setup_queue_param(device, bdev, new);
b411b363
PR
1244}
1245
a18e9d1e 1246/* Starts the worker thread */
bde89a9e 1247static void conn_reconfig_start(struct drbd_connection *connection)
b411b363 1248{
bde89a9e 1249 drbd_thread_start(&connection->worker);
b5043c5e 1250 drbd_flush_workqueue(&connection->sender_work);
b411b363
PR
1251}
1252
a18e9d1e 1253/* if still unconfigured, stops worker again. */
bde89a9e 1254static void conn_reconfig_done(struct drbd_connection *connection)
b411b363 1255{
992d6e91 1256 bool stop_threads;
0500813f 1257 spin_lock_irq(&connection->resource->req_lock);
bde89a9e
AG
1258 stop_threads = conn_all_vols_unconf(connection) &&
1259 connection->cstate == C_STANDALONE;
0500813f 1260 spin_unlock_irq(&connection->resource->req_lock);
992d6e91
LE
1261 if (stop_threads) {
1262 /* asender is implicitly stopped by receiver
81fa2e67 1263 * in conn_disconnect() */
bde89a9e
AG
1264 drbd_thread_stop(&connection->receiver);
1265 drbd_thread_stop(&connection->worker);
992d6e91 1266 }
b411b363
PR
1267}
1268
0778286a 1269/* Make sure IO is suspended before calling this function(). */
b30ab791 1270static void drbd_suspend_al(struct drbd_device *device)
0778286a
PR
1271{
1272 int s = 0;
1273
b30ab791 1274 if (!lc_try_lock(device->act_log)) {
d0180171 1275 drbd_warn(device, "Failed to lock al in drbd_suspend_al()\n");
0778286a
PR
1276 return;
1277 }
1278
b30ab791 1279 drbd_al_shrink(device);
0500813f 1280 spin_lock_irq(&device->resource->req_lock);
b30ab791
AG
1281 if (device->state.conn < C_CONNECTED)
1282 s = !test_and_set_bit(AL_SUSPENDED, &device->flags);
0500813f 1283 spin_unlock_irq(&device->resource->req_lock);
b30ab791 1284 lc_unlock(device->act_log);
0778286a
PR
1285
1286 if (s)
d0180171 1287 drbd_info(device, "Suspended AL updates\n");
0778286a
PR
1288}
1289
5979e361
LE
1290
1291static bool should_set_defaults(struct genl_info *info)
1292{
1293 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1294 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1295}
1296
5bbcf5e6 1297static unsigned int drbd_al_extents_max(struct drbd_backing_dev *bdev)
d589a21e 1298{
5bbcf5e6
LE
1299 /* This is limited by 16 bit "slot" numbers,
1300 * and by available on-disk context storage.
1301 *
1302 * Also (u16)~0 is special (denotes a "free" extent).
1303 *
1304 * One transaction occupies one 4kB on-disk block,
1305 * we have n such blocks in the on disk ring buffer,
1306 * the "current" transaction may fail (n-1),
1307 * and there is 919 slot numbers context information per transaction.
1308 *
1309 * 72 transaction blocks amounts to more than 2**16 context slots,
1310 * so cap there first.
1311 */
1312 const unsigned int max_al_nr = DRBD_AL_EXTENTS_MAX;
1313 const unsigned int sufficient_on_disk =
1314 (max_al_nr + AL_CONTEXT_PER_TRANSACTION -1)
1315 /AL_CONTEXT_PER_TRANSACTION;
d589a21e 1316
5bbcf5e6
LE
1317 unsigned int al_size_4k = bdev->md.al_size_4k;
1318
1319 if (al_size_4k > sufficient_on_disk)
1320 return max_al_nr;
1321
1322 return (al_size_4k - 1) * AL_CONTEXT_PER_TRANSACTION;
d589a21e
PR
1323}
1324
70df7092
LE
1325static bool write_ordering_changed(struct disk_conf *a, struct disk_conf *b)
1326{
1327 return a->disk_barrier != b->disk_barrier ||
1328 a->disk_flushes != b->disk_flushes ||
1329 a->disk_drain != b->disk_drain;
1330}
1331
f399002e
LE
1332int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1333{
a910b123 1334 struct drbd_config_context adm_ctx;
f399002e 1335 enum drbd_ret_code retcode;
b30ab791 1336 struct drbd_device *device;
daeda1cc 1337 struct disk_conf *new_disk_conf, *old_disk_conf;
813472ce 1338 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
f399002e 1339 int err, fifo_size;
f399002e 1340
a910b123 1341 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
f399002e
LE
1342 if (!adm_ctx.reply_skb)
1343 return retcode;
1344 if (retcode != NO_ERROR)
9e276872 1345 goto finish;
f399002e 1346
b30ab791 1347 device = adm_ctx.device;
9e276872 1348 mutex_lock(&adm_ctx.resource->adm_mutex);
f399002e
LE
1349
1350 /* we also need a disk
1351 * to change the options on */
b30ab791 1352 if (!get_ldev(device)) {
f399002e
LE
1353 retcode = ERR_NO_DISK;
1354 goto out;
1355 }
1356
daeda1cc 1357 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
5ecc72c3 1358 if (!new_disk_conf) {
f399002e
LE
1359 retcode = ERR_NOMEM;
1360 goto fail;
1361 }
1362
0500813f 1363 mutex_lock(&device->resource->conf_update);
b30ab791 1364 old_disk_conf = device->ldev->disk_conf;
daeda1cc 1365 *new_disk_conf = *old_disk_conf;
5979e361 1366 if (should_set_defaults(info))
b966b5dd 1367 set_disk_conf_defaults(new_disk_conf);
5979e361 1368
5ecc72c3 1369 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
c75b9b10 1370 if (err && err != -ENOMSG) {
f399002e 1371 retcode = ERR_MANDATORY_TAG;
a910b123 1372 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
8e229434 1373 goto fail_unlock;
f399002e
LE
1374 }
1375
5ecc72c3
LE
1376 if (!expect(new_disk_conf->resync_rate >= 1))
1377 new_disk_conf->resync_rate = 1;
f399002e 1378
5bbcf5e6
LE
1379 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1380 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
b30ab791
AG
1381 if (new_disk_conf->al_extents > drbd_al_extents_max(device->ldev))
1382 new_disk_conf->al_extents = drbd_al_extents_max(device->ldev);
5bbcf5e6
LE
1383
1384 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1385 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
f399002e 1386
5ecc72c3 1387 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
b30ab791 1388 if (fifo_size != device->rs_plan_s->size) {
813472ce
PR
1389 new_plan = fifo_alloc(fifo_size);
1390 if (!new_plan) {
d0180171 1391 drbd_err(device, "kmalloc of fifo_buffer failed");
f399002e 1392 retcode = ERR_NOMEM;
daeda1cc 1393 goto fail_unlock;
f399002e
LE
1394 }
1395 }
1396
b30ab791
AG
1397 drbd_suspend_io(device);
1398 wait_event(device->al_wait, lc_try_lock(device->act_log));
1399 drbd_al_shrink(device);
1400 err = drbd_check_al_size(device, new_disk_conf);
1401 lc_unlock(device->act_log);
1402 wake_up(&device->al_wait);
1403 drbd_resume_io(device);
f399002e
LE
1404
1405 if (err) {
1406 retcode = ERR_NOMEM;
daeda1cc 1407 goto fail_unlock;
f399002e
LE
1408 }
1409
28bc3b8c 1410 lock_all_resources();
b30ab791 1411 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
dc97b708 1412 if (retcode == NO_ERROR) {
b30ab791
AG
1413 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
1414 drbd_resync_after_changed(device);
dc97b708 1415 }
28bc3b8c 1416 unlock_all_resources();
f399002e 1417
daeda1cc
PR
1418 if (retcode != NO_ERROR)
1419 goto fail_unlock;
f399002e 1420
813472ce 1421 if (new_plan) {
b30ab791
AG
1422 old_plan = device->rs_plan_s;
1423 rcu_assign_pointer(device->rs_plan_s, new_plan);
9958c857 1424 }
9958c857 1425
0500813f 1426 mutex_unlock(&device->resource->conf_update);
27eb13e9 1427
9a51ab1c 1428 if (new_disk_conf->al_updates)
b30ab791 1429 device->ldev->md.flags &= ~MDF_AL_DISABLED;
9a51ab1c 1430 else
b30ab791 1431 device->ldev->md.flags |= MDF_AL_DISABLED;
9a51ab1c 1432
691631c0 1433 if (new_disk_conf->md_flushes)
b30ab791 1434 clear_bit(MD_NO_FUA, &device->flags);
691631c0 1435 else
b30ab791 1436 set_bit(MD_NO_FUA, &device->flags);
691631c0 1437
70df7092 1438 if (write_ordering_changed(old_disk_conf, new_disk_conf))
f6ba8636 1439 drbd_bump_write_ordering(device->resource, NULL, WO_BDEV_FLUSH);
27eb13e9 1440
b30ab791 1441 drbd_md_sync(device);
f399002e 1442
69a22773
AG
1443 if (device->state.conn >= C_CONNECTED) {
1444 struct drbd_peer_device *peer_device;
1445
1446 for_each_peer_device(peer_device, device)
1447 drbd_send_sync_param(peer_device);
1448 }
f399002e 1449
daeda1cc
PR
1450 synchronize_rcu();
1451 kfree(old_disk_conf);
813472ce 1452 kfree(old_plan);
b30ab791 1453 mod_timer(&device->request_timer, jiffies + HZ);
daeda1cc
PR
1454 goto success;
1455
1456fail_unlock:
0500813f 1457 mutex_unlock(&device->resource->conf_update);
f399002e 1458 fail:
5ecc72c3 1459 kfree(new_disk_conf);
813472ce 1460 kfree(new_plan);
daeda1cc 1461success:
b30ab791 1462 put_ldev(device);
f399002e 1463 out:
9e276872
LE
1464 mutex_unlock(&adm_ctx.resource->adm_mutex);
1465 finish:
a910b123 1466 drbd_adm_finish(&adm_ctx, info, retcode);
f399002e
LE
1467 return 0;
1468}
1469
3b98c0c2 1470int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
b411b363 1471{
a910b123 1472 struct drbd_config_context adm_ctx;
b30ab791 1473 struct drbd_device *device;
44a4d551
LE
1474 struct drbd_peer_device *peer_device;
1475 struct drbd_connection *connection;
3b98c0c2 1476 int err;
116676ca 1477 enum drbd_ret_code retcode;
b411b363
PR
1478 enum determine_dev_size dd;
1479 sector_t max_possible_sectors;
1480 sector_t min_md_device_sectors;
1481 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
daeda1cc 1482 struct disk_conf *new_disk_conf = NULL;
e525fd89 1483 struct block_device *bdev;
b411b363 1484 struct lru_cache *resync_lru = NULL;
9958c857 1485 struct fifo_buffer *new_plan = NULL;
b411b363 1486 union drbd_state ns, os;
f2024e7c 1487 enum drbd_state_rv rv;
44ed167d 1488 struct net_conf *nc;
b411b363 1489
a910b123 1490 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
1491 if (!adm_ctx.reply_skb)
1492 return retcode;
1493 if (retcode != NO_ERROR)
40cbf085 1494 goto finish;
b411b363 1495
b30ab791 1496 device = adm_ctx.device;
9e276872 1497 mutex_lock(&adm_ctx.resource->adm_mutex);
44a4d551 1498 peer_device = first_peer_device(device);
3b8a44f8 1499 connection = peer_device->connection;
44a4d551 1500 conn_reconfig_start(connection);
b411b363
PR
1501
1502 /* if you want to reconfigure, please tear down first */
b30ab791 1503 if (device->state.disk > D_DISKLESS) {
b411b363
PR
1504 retcode = ERR_DISK_CONFIGURED;
1505 goto fail;
1506 }
82f59cc6
LE
1507 /* It may just now have detached because of IO error. Make sure
1508 * drbd_ldev_destroy is done already, we may end up here very fast,
1509 * e.g. if someone calls attach from the on-io-error handler,
1510 * to realize a "hot spare" feature (not that I'd recommend that) */
e334f550 1511 wait_event(device->misc_wait, !test_bit(GOING_DISKLESS, &device->flags));
b411b363 1512
383606e0 1513 /* make sure there is no leftover from previous force-detach attempts */
b30ab791
AG
1514 clear_bit(FORCE_DETACH, &device->flags);
1515 clear_bit(WAS_IO_ERROR, &device->flags);
1516 clear_bit(WAS_READ_ERROR, &device->flags);
383606e0 1517
0029d624 1518 /* and no leftover from previously aborted resync or verify, either */
b30ab791
AG
1519 device->rs_total = 0;
1520 device->rs_failed = 0;
1521 atomic_set(&device->rs_pending_cnt, 0);
0029d624 1522
3b98c0c2 1523 /* allocation not in the IO path, drbdsetup context */
b411b363
PR
1524 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1525 if (!nbc) {
1526 retcode = ERR_NOMEM;
1527 goto fail;
1528 }
9f2247bb
PR
1529 spin_lock_init(&nbc->md.uuid_lock);
1530
daeda1cc
PR
1531 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
1532 if (!new_disk_conf) {
1533 retcode = ERR_NOMEM;
b411b363
PR
1534 goto fail;
1535 }
daeda1cc 1536 nbc->disk_conf = new_disk_conf;
b411b363 1537
daeda1cc
PR
1538 set_disk_conf_defaults(new_disk_conf);
1539 err = disk_conf_from_attrs(new_disk_conf, info);
3b98c0c2 1540 if (err) {
b411b363 1541 retcode = ERR_MANDATORY_TAG;
a910b123 1542 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
b411b363
PR
1543 goto fail;
1544 }
1545
5bbcf5e6
LE
1546 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1547 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
d589a21e 1548
9958c857
PR
1549 new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
1550 if (!new_plan) {
1551 retcode = ERR_NOMEM;
1552 goto fail;
1553 }
1554
daeda1cc 1555 if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
b411b363
PR
1556 retcode = ERR_MD_IDX_INVALID;
1557 goto fail;
1558 }
1559
44ed167d 1560 rcu_read_lock();
44a4d551 1561 nc = rcu_dereference(connection->net_conf);
44ed167d 1562 if (nc) {
daeda1cc 1563 if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
44ed167d 1564 rcu_read_unlock();
47ff2d0a 1565 retcode = ERR_STONITH_AND_PROT_A;
28bc3b8c 1566 goto fail;
47ff2d0a
PR
1567 }
1568 }
44ed167d 1569 rcu_read_unlock();
47ff2d0a 1570
daeda1cc 1571 bdev = blkdev_get_by_path(new_disk_conf->backing_dev,
b30ab791 1572 FMODE_READ | FMODE_WRITE | FMODE_EXCL, device);
e525fd89 1573 if (IS_ERR(bdev)) {
d0180171 1574 drbd_err(device, "open(\"%s\") failed with %ld\n", new_disk_conf->backing_dev,
e525fd89 1575 PTR_ERR(bdev));
b411b363 1576 retcode = ERR_OPEN_DISK;
28bc3b8c 1577 goto fail;
b411b363 1578 }
e525fd89
TH
1579 nbc->backing_bdev = bdev;
1580
1581 /*
1582 * meta_dev_idx >= 0: external fixed size, possibly multiple
1583 * drbd sharing one meta device. TODO in that case, paranoia
1584 * check that [md_bdev, meta_dev_idx] is not yet used by some
1585 * other drbd minor! (if you use drbd.conf + drbdadm, that
1586 * should check it for you already; but if you don't, or
1587 * someone fooled it, we need to double check here)
1588 */
daeda1cc 1589 bdev = blkdev_get_by_path(new_disk_conf->meta_dev,
d4d77629 1590 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
daeda1cc 1591 (new_disk_conf->meta_dev_idx < 0) ?
b30ab791 1592 (void *)device : (void *)drbd_m_holder);
e525fd89 1593 if (IS_ERR(bdev)) {
d0180171 1594 drbd_err(device, "open(\"%s\") failed with %ld\n", new_disk_conf->meta_dev,
e525fd89 1595 PTR_ERR(bdev));
b411b363 1596 retcode = ERR_OPEN_MD_DISK;
28bc3b8c 1597 goto fail;
b411b363 1598 }
e525fd89 1599 nbc->md_bdev = bdev;
b411b363 1600
e525fd89 1601 if ((nbc->backing_bdev == nbc->md_bdev) !=
daeda1cc
PR
1602 (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1603 new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
e525fd89 1604 retcode = ERR_MD_IDX_INVALID;
28bc3b8c 1605 goto fail;
b411b363
PR
1606 }
1607
1608 resync_lru = lc_create("resync", drbd_bm_ext_cache,
46a15bc3 1609 1, 61, sizeof(struct bm_extent),
b411b363
PR
1610 offsetof(struct bm_extent, lce));
1611 if (!resync_lru) {
1612 retcode = ERR_NOMEM;
28bc3b8c 1613 goto fail;
b411b363
PR
1614 }
1615
c04ccaa6
LE
1616 /* Read our meta data super block early.
1617 * This also sets other on-disk offsets. */
b30ab791 1618 retcode = drbd_md_read(device, nbc);
c04ccaa6 1619 if (retcode != NO_ERROR)
28bc3b8c 1620 goto fail;
b411b363 1621
5bbcf5e6
LE
1622 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1623 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
1624 if (new_disk_conf->al_extents > drbd_al_extents_max(nbc))
1625 new_disk_conf->al_extents = drbd_al_extents_max(nbc);
1626
daeda1cc 1627 if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
d0180171 1628 drbd_err(device, "max capacity %llu smaller than disk size %llu\n",
b411b363 1629 (unsigned long long) drbd_get_max_capacity(nbc),
daeda1cc 1630 (unsigned long long) new_disk_conf->disk_size);
7948bcdc 1631 retcode = ERR_DISK_TOO_SMALL;
28bc3b8c 1632 goto fail;
b411b363
PR
1633 }
1634
daeda1cc 1635 if (new_disk_conf->meta_dev_idx < 0) {
b411b363
PR
1636 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1637 /* at least one MB, otherwise it does not make sense */
1638 min_md_device_sectors = (2<<10);
1639 } else {
1640 max_possible_sectors = DRBD_MAX_SECTORS;
ae8bf312 1641 min_md_device_sectors = MD_128MB_SECT * (new_disk_conf->meta_dev_idx + 1);
b411b363
PR
1642 }
1643
b411b363 1644 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
7948bcdc 1645 retcode = ERR_MD_DISK_TOO_SMALL;
d0180171 1646 drbd_warn(device, "refusing attach: md-device too small, "
b411b363
PR
1647 "at least %llu sectors needed for this meta-disk type\n",
1648 (unsigned long long) min_md_device_sectors);
28bc3b8c 1649 goto fail;
b411b363
PR
1650 }
1651
1652 /* Make sure the new disk is big enough
1653 * (we may currently be R_PRIMARY with no local disk...) */
1654 if (drbd_get_max_capacity(nbc) <
b30ab791 1655 drbd_get_capacity(device->this_bdev)) {
7948bcdc 1656 retcode = ERR_DISK_TOO_SMALL;
28bc3b8c 1657 goto fail;
b411b363
PR
1658 }
1659
1660 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1661
1352994b 1662 if (nbc->known_size > max_possible_sectors) {
d0180171 1663 drbd_warn(device, "==> truncating very big lower level device "
1352994b
LE
1664 "to currently maximum possible %llu sectors <==\n",
1665 (unsigned long long) max_possible_sectors);
daeda1cc 1666 if (new_disk_conf->meta_dev_idx >= 0)
d0180171 1667 drbd_warn(device, "==>> using internal or flexible "
1352994b
LE
1668 "meta data may help <<==\n");
1669 }
1670
b30ab791 1671 drbd_suspend_io(device);
b411b363 1672 /* also wait for the last barrier ack. */
b6dd1a89
LE
1673 /* FIXME see also https://daiquiri.linbit/cgi-bin/bugzilla/show_bug.cgi?id=171
1674 * We need a way to either ignore barrier acks for barriers sent before a device
1675 * was attached, or a way to wait for all pending barrier acks to come in.
1676 * As barriers are counted per resource,
1677 * we'd need to suspend io on all devices of a resource.
1678 */
b30ab791 1679 wait_event(device->misc_wait, !atomic_read(&device->ap_pending_cnt) || drbd_suspended(device));
b411b363 1680 /* and for any other previously queued work */
44a4d551 1681 drbd_flush_workqueue(&connection->sender_work);
b411b363 1682
b30ab791 1683 rv = _drbd_request_state(device, NS(disk, D_ATTACHING), CS_VERBOSE);
f2024e7c 1684 retcode = rv; /* FIXME: Type mismatch. */
b30ab791 1685 drbd_resume_io(device);
f2024e7c 1686 if (rv < SS_SUCCESS)
28bc3b8c 1687 goto fail;
b411b363 1688
b30ab791 1689 if (!get_ldev_if_state(device, D_ATTACHING))
b411b363
PR
1690 goto force_diskless;
1691
b30ab791
AG
1692 if (!device->bitmap) {
1693 if (drbd_bm_init(device)) {
b411b363
PR
1694 retcode = ERR_NOMEM;
1695 goto force_diskless_dec;
1696 }
1697 }
1698
b30ab791 1699 if (device->state.conn < C_CONNECTED &&
babea49e 1700 device->state.role == R_PRIMARY && device->ed_uuid &&
b30ab791 1701 (device->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
d0180171 1702 drbd_err(device, "Can only attach to data with current UUID=%016llX\n",
b30ab791 1703 (unsigned long long)device->ed_uuid);
b411b363
PR
1704 retcode = ERR_DATA_NOT_CURRENT;
1705 goto force_diskless_dec;
1706 }
1707
1708 /* Since we are diskless, fix the activity log first... */
b30ab791 1709 if (drbd_check_al_size(device, new_disk_conf)) {
b411b363
PR
1710 retcode = ERR_NOMEM;
1711 goto force_diskless_dec;
1712 }
1713
1714 /* Prevent shrinking of consistent devices ! */
1715 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
b30ab791 1716 drbd_new_dev_size(device, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
d0180171 1717 drbd_warn(device, "refusing to truncate a consistent device\n");
7948bcdc 1718 retcode = ERR_DISK_TOO_SMALL;
b411b363
PR
1719 goto force_diskless_dec;
1720 }
1721
28bc3b8c
AG
1722 lock_all_resources();
1723 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
1724 if (retcode != NO_ERROR) {
1725 unlock_all_resources();
1726 goto force_diskless_dec;
1727 }
1728
b411b363
PR
1729 /* Reset the "barriers don't work" bits here, then force meta data to
1730 * be written, to ensure we determine if barriers are supported. */
e544046a 1731 if (new_disk_conf->md_flushes)
b30ab791 1732 clear_bit(MD_NO_FUA, &device->flags);
b411b363 1733 else
b30ab791 1734 set_bit(MD_NO_FUA, &device->flags);
b411b363
PR
1735
1736 /* Point of no return reached.
1737 * Devices and memory are no longer released by error cleanup below.
b30ab791 1738 * now device takes over responsibility, and the state engine should
b411b363 1739 * clean it up somewhere. */
0b0ba1ef 1740 D_ASSERT(device, device->ldev == NULL);
b30ab791
AG
1741 device->ldev = nbc;
1742 device->resync = resync_lru;
1743 device->rs_plan_s = new_plan;
b411b363
PR
1744 nbc = NULL;
1745 resync_lru = NULL;
daeda1cc 1746 new_disk_conf = NULL;
9958c857 1747 new_plan = NULL;
b411b363 1748
1ec317d3 1749 drbd_resync_after_changed(device);
f6ba8636 1750 drbd_bump_write_ordering(device->resource, device->ldev, WO_BDEV_FLUSH);
28bc3b8c 1751 unlock_all_resources();
b411b363 1752
b30ab791
AG
1753 if (drbd_md_test_flag(device->ldev, MDF_CRASHED_PRIMARY))
1754 set_bit(CRASHED_PRIMARY, &device->flags);
b411b363 1755 else
b30ab791 1756 clear_bit(CRASHED_PRIMARY, &device->flags);
b411b363 1757
b30ab791 1758 if (drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
6bbf53ca 1759 !(device->state.role == R_PRIMARY && device->resource->susp_nod))
b30ab791 1760 set_bit(CRASHED_PRIMARY, &device->flags);
b411b363 1761
b30ab791
AG
1762 device->send_cnt = 0;
1763 device->recv_cnt = 0;
1764 device->read_cnt = 0;
1765 device->writ_cnt = 0;
b411b363 1766
8fe39aac 1767 drbd_reconsider_max_bio_size(device, device->ldev);
b411b363
PR
1768
1769 /* If I am currently not R_PRIMARY,
1770 * but meta data primary indicator is set,
1771 * I just now recover from a hard crash,
1772 * and have been R_PRIMARY before that crash.
1773 *
1774 * Now, if I had no connection before that crash
1775 * (have been degraded R_PRIMARY), chances are that
1776 * I won't find my peer now either.
1777 *
1778 * In that case, and _only_ in that case,
1779 * we use the degr-wfc-timeout instead of the default,
1780 * so we can automatically recover from a crash of a
1781 * degraded but active "cluster" after a certain timeout.
1782 */
b30ab791
AG
1783 clear_bit(USE_DEGR_WFC_T, &device->flags);
1784 if (device->state.role != R_PRIMARY &&
1785 drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
1786 !drbd_md_test_flag(device->ldev, MDF_CONNECTED_IND))
1787 set_bit(USE_DEGR_WFC_T, &device->flags);
b411b363 1788
b30ab791 1789 dd = drbd_determine_dev_size(device, 0, NULL);
d752b269 1790 if (dd <= DS_ERROR) {
b411b363
PR
1791 retcode = ERR_NOMEM_BITMAP;
1792 goto force_diskless_dec;
e96c9633 1793 } else if (dd == DS_GREW)
b30ab791 1794 set_bit(RESYNC_AFTER_NEG, &device->flags);
b411b363 1795
b30ab791
AG
1796 if (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC) ||
1797 (test_bit(CRASHED_PRIMARY, &device->flags) &&
1798 drbd_md_test_flag(device->ldev, MDF_AL_DISABLED))) {
d0180171 1799 drbd_info(device, "Assuming that all blocks are out of sync "
b411b363 1800 "(aka FullSync)\n");
b30ab791 1801 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
20ceb2b2 1802 "set_n_write from attaching", BM_LOCKED_MASK)) {
b411b363
PR
1803 retcode = ERR_IO_MD_DISK;
1804 goto force_diskless_dec;
1805 }
1806 } else {
b30ab791 1807 if (drbd_bitmap_io(device, &drbd_bm_read,
22ab6a30 1808 "read from attaching", BM_LOCKED_MASK)) {
19f843aa
LE
1809 retcode = ERR_IO_MD_DISK;
1810 goto force_diskless_dec;
1811 }
b411b363
PR
1812 }
1813
b30ab791
AG
1814 if (_drbd_bm_total_weight(device) == drbd_bm_bits(device))
1815 drbd_suspend_al(device); /* IO is still suspended here... */
0778286a 1816
0500813f 1817 spin_lock_irq(&device->resource->req_lock);
b30ab791 1818 os = drbd_read_state(device);
78bae59b 1819 ns = os;
b411b363
PR
1820 /* If MDF_CONSISTENT is not set go into inconsistent state,
1821 otherwise investigate MDF_WasUpToDate...
1822 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1823 otherwise into D_CONSISTENT state.
1824 */
b30ab791
AG
1825 if (drbd_md_test_flag(device->ldev, MDF_CONSISTENT)) {
1826 if (drbd_md_test_flag(device->ldev, MDF_WAS_UP_TO_DATE))
b411b363
PR
1827 ns.disk = D_CONSISTENT;
1828 else
1829 ns.disk = D_OUTDATED;
1830 } else {
1831 ns.disk = D_INCONSISTENT;
1832 }
1833
b30ab791 1834 if (drbd_md_test_flag(device->ldev, MDF_PEER_OUT_DATED))
b411b363
PR
1835 ns.pdsk = D_OUTDATED;
1836
daeda1cc
PR
1837 rcu_read_lock();
1838 if (ns.disk == D_CONSISTENT &&
b30ab791 1839 (ns.pdsk == D_OUTDATED || rcu_dereference(device->ldev->disk_conf)->fencing == FP_DONT_CARE))
b411b363
PR
1840 ns.disk = D_UP_TO_DATE;
1841
1842 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1843 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1844 this point, because drbd_request_state() modifies these
1845 flags. */
1846
b30ab791
AG
1847 if (rcu_dereference(device->ldev->disk_conf)->al_updates)
1848 device->ldev->md.flags &= ~MDF_AL_DISABLED;
9a51ab1c 1849 else
b30ab791 1850 device->ldev->md.flags |= MDF_AL_DISABLED;
9a51ab1c
PR
1851
1852 rcu_read_unlock();
1853
b411b363
PR
1854 /* In case we are C_CONNECTED postpone any decision on the new disk
1855 state after the negotiation phase. */
b30ab791
AG
1856 if (device->state.conn == C_CONNECTED) {
1857 device->new_state_tmp.i = ns.i;
b411b363
PR
1858 ns.i = os.i;
1859 ns.disk = D_NEGOTIATING;
dc66c74d
PR
1860
1861 /* We expect to receive up-to-date UUIDs soon.
1862 To avoid a race in receive_state, free p_uuid while
1863 holding req_lock. I.e. atomic with the state change */
b30ab791
AG
1864 kfree(device->p_uuid);
1865 device->p_uuid = NULL;
b411b363
PR
1866 }
1867
b30ab791 1868 rv = _drbd_set_state(device, ns, CS_VERBOSE, NULL);
0500813f 1869 spin_unlock_irq(&device->resource->req_lock);
b411b363
PR
1870
1871 if (rv < SS_SUCCESS)
1872 goto force_diskless_dec;
1873
b30ab791 1874 mod_timer(&device->request_timer, jiffies + HZ);
cdfda633 1875
b30ab791
AG
1876 if (device->state.role == R_PRIMARY)
1877 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
b411b363 1878 else
b30ab791 1879 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
b411b363 1880
b30ab791
AG
1881 drbd_md_mark_dirty(device);
1882 drbd_md_sync(device);
b411b363 1883
b30ab791
AG
1884 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
1885 put_ldev(device);
44a4d551 1886 conn_reconfig_done(connection);
9e276872 1887 mutex_unlock(&adm_ctx.resource->adm_mutex);
a910b123 1888 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
1889 return 0;
1890
1891 force_diskless_dec:
b30ab791 1892 put_ldev(device);
b411b363 1893 force_diskless:
b30ab791
AG
1894 drbd_force_state(device, NS(disk, D_DISKLESS));
1895 drbd_md_sync(device);
b411b363 1896 fail:
44a4d551 1897 conn_reconfig_done(connection);
b411b363 1898 if (nbc) {
e525fd89
TH
1899 if (nbc->backing_bdev)
1900 blkdev_put(nbc->backing_bdev,
1901 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1902 if (nbc->md_bdev)
1903 blkdev_put(nbc->md_bdev,
1904 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
b411b363
PR
1905 kfree(nbc);
1906 }
daeda1cc 1907 kfree(new_disk_conf);
b411b363 1908 lc_destroy(resync_lru);
9958c857 1909 kfree(new_plan);
9e276872 1910 mutex_unlock(&adm_ctx.resource->adm_mutex);
40cbf085 1911 finish:
a910b123 1912 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
1913 return 0;
1914}
1915
b30ab791 1916static int adm_detach(struct drbd_device *device, int force)
b411b363 1917{
19f83c76 1918 enum drbd_state_rv retcode;
9a0d9d03 1919 int ret;
02ee8f95 1920
cdfda633 1921 if (force) {
b30ab791
AG
1922 set_bit(FORCE_DETACH, &device->flags);
1923 drbd_force_state(device, NS(disk, D_FAILED));
cdfda633 1924 retcode = SS_SUCCESS;
02ee8f95
PR
1925 goto out;
1926 }
1927
b30ab791 1928 drbd_suspend_io(device); /* so no-one is stuck in drbd_al_begin_io */
e37d2438 1929 drbd_md_get_buffer(device, __func__); /* make sure there is no in-flight meta-data IO */
b30ab791
AG
1930 retcode = drbd_request_state(device, NS(disk, D_FAILED));
1931 drbd_md_put_buffer(device);
9a0d9d03 1932 /* D_FAILED will transition to DISKLESS. */
b30ab791
AG
1933 ret = wait_event_interruptible(device->misc_wait,
1934 device->state.disk != D_FAILED);
1935 drbd_resume_io(device);
9b2f61ae 1936 if ((int)retcode == (int)SS_IS_DISKLESS)
9a0d9d03
LE
1937 retcode = SS_NOTHING_TO_DO;
1938 if (ret)
1939 retcode = ERR_INTR;
02ee8f95 1940out:
85f75dd7 1941 return retcode;
b411b363
PR
1942}
1943
82f59cc6
LE
1944/* Detaching the disk is a process in multiple stages. First we need to lock
1945 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1946 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1947 * internal references as well.
1948 * Only then we have finally detached. */
3b98c0c2 1949int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
b411b363 1950{
a910b123 1951 struct drbd_config_context adm_ctx;
116676ca 1952 enum drbd_ret_code retcode;
cdfda633
PR
1953 struct detach_parms parms = { };
1954 int err;
b411b363 1955
a910b123 1956 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
1957 if (!adm_ctx.reply_skb)
1958 return retcode;
1959 if (retcode != NO_ERROR)
1960 goto out;
b411b363 1961
cdfda633
PR
1962 if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
1963 err = detach_parms_from_attrs(&parms, info);
1964 if (err) {
1965 retcode = ERR_MANDATORY_TAG;
a910b123 1966 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
cdfda633
PR
1967 goto out;
1968 }
b411b363
PR
1969 }
1970
9e276872 1971 mutex_lock(&adm_ctx.resource->adm_mutex);
b30ab791 1972 retcode = adm_detach(adm_ctx.device, parms.force_detach);
9e276872 1973 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 1974out:
a910b123 1975 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
1976 return 0;
1977}
b411b363 1978
bde89a9e 1979static bool conn_resync_running(struct drbd_connection *connection)
f399002e 1980{
c06ece6b 1981 struct drbd_peer_device *peer_device;
695d08fa 1982 bool rv = false;
f399002e
LE
1983 int vnr;
1984
695d08fa 1985 rcu_read_lock();
c06ece6b
AG
1986 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1987 struct drbd_device *device = peer_device->device;
b30ab791
AG
1988 if (device->state.conn == C_SYNC_SOURCE ||
1989 device->state.conn == C_SYNC_TARGET ||
1990 device->state.conn == C_PAUSED_SYNC_S ||
1991 device->state.conn == C_PAUSED_SYNC_T) {
695d08fa
PR
1992 rv = true;
1993 break;
1994 }
b411b363 1995 }
695d08fa 1996 rcu_read_unlock();
b411b363 1997
695d08fa 1998 return rv;
f399002e 1999}
47ff2d0a 2000
bde89a9e 2001static bool conn_ov_running(struct drbd_connection *connection)
f399002e 2002{
c06ece6b 2003 struct drbd_peer_device *peer_device;
695d08fa 2004 bool rv = false;
f399002e
LE
2005 int vnr;
2006
695d08fa 2007 rcu_read_lock();
c06ece6b
AG
2008 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
2009 struct drbd_device *device = peer_device->device;
b30ab791
AG
2010 if (device->state.conn == C_VERIFY_S ||
2011 device->state.conn == C_VERIFY_T) {
695d08fa
PR
2012 rv = true;
2013 break;
47ff2d0a
PR
2014 }
2015 }
695d08fa 2016 rcu_read_unlock();
b411b363 2017
695d08fa 2018 return rv;
f399002e 2019}
422028b1 2020
cd64397c 2021static enum drbd_ret_code
270eb5c9 2022_check_net_options(struct drbd_connection *connection, struct net_conf *old_net_conf, struct net_conf *new_net_conf)
cd64397c 2023{
c06ece6b 2024 struct drbd_peer_device *peer_device;
cd64397c 2025 int i;
b411b363 2026
270eb5c9
AG
2027 if (old_net_conf && connection->cstate == C_WF_REPORT_PARAMS && connection->agreed_pro_version < 100) {
2028 if (new_net_conf->wire_protocol != old_net_conf->wire_protocol)
dcb20d1a 2029 return ERR_NEED_APV_100;
b411b363 2030
270eb5c9 2031 if (new_net_conf->two_primaries != old_net_conf->two_primaries)
dcb20d1a
PR
2032 return ERR_NEED_APV_100;
2033
270eb5c9 2034 if (strcmp(new_net_conf->integrity_alg, old_net_conf->integrity_alg))
dcb20d1a 2035 return ERR_NEED_APV_100;
b411b363
PR
2036 }
2037
270eb5c9 2038 if (!new_net_conf->two_primaries &&
bde89a9e
AG
2039 conn_highest_role(connection) == R_PRIMARY &&
2040 conn_highest_peer(connection) == R_PRIMARY)
dcb20d1a 2041 return ERR_NEED_ALLOW_TWO_PRI;
b411b363 2042
270eb5c9
AG
2043 if (new_net_conf->two_primaries &&
2044 (new_net_conf->wire_protocol != DRBD_PROT_C))
cd64397c
PR
2045 return ERR_NOT_PROTO_C;
2046
c06ece6b
AG
2047 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2048 struct drbd_device *device = peer_device->device;
b30ab791
AG
2049 if (get_ldev(device)) {
2050 enum drbd_fencing_p fp = rcu_dereference(device->ldev->disk_conf)->fencing;
2051 put_ldev(device);
270eb5c9 2052 if (new_net_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
cd64397c 2053 return ERR_STONITH_AND_PROT_A;
b411b363 2054 }
270eb5c9 2055 if (device->state.role == R_PRIMARY && new_net_conf->discard_my_data)
eb12010e 2056 return ERR_DISCARD_IMPOSSIBLE;
b411b363
PR
2057 }
2058
270eb5c9 2059 if (new_net_conf->on_congestion != OC_BLOCK && new_net_conf->wire_protocol != DRBD_PROT_A)
cd64397c 2060 return ERR_CONG_NOT_PROTO_A;
b411b363 2061
cd64397c
PR
2062 return NO_ERROR;
2063}
b411b363 2064
44ed167d 2065static enum drbd_ret_code
270eb5c9 2066check_net_options(struct drbd_connection *connection, struct net_conf *new_net_conf)
44ed167d
PR
2067{
2068 static enum drbd_ret_code rv;
c06ece6b 2069 struct drbd_peer_device *peer_device;
44ed167d 2070 int i;
b411b363 2071
44ed167d 2072 rcu_read_lock();
270eb5c9 2073 rv = _check_net_options(connection, rcu_dereference(connection->net_conf), new_net_conf);
44ed167d 2074 rcu_read_unlock();
b411b363 2075
179e20b8 2076 /* connection->peer_devices protected by genl_lock() here */
c06ece6b
AG
2077 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2078 struct drbd_device *device = peer_device->device;
b30ab791
AG
2079 if (!device->bitmap) {
2080 if (drbd_bm_init(device))
44ed167d 2081 return ERR_NOMEM;
b411b363
PR
2082 }
2083 }
2084
44ed167d
PR
2085 return rv;
2086}
b411b363 2087
0fd0ea06
PR
2088struct crypto {
2089 struct crypto_hash *verify_tfm;
2090 struct crypto_hash *csums_tfm;
2091 struct crypto_hash *cram_hmac_tfm;
8d412fc6 2092 struct crypto_hash *integrity_tfm;
0fd0ea06 2093};
b411b363 2094
0fd0ea06 2095static int
4b6ad6d4 2096alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
0fd0ea06
PR
2097{
2098 if (!tfm_name[0])
2099 return NO_ERROR;
b411b363 2100
0fd0ea06
PR
2101 *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
2102 if (IS_ERR(*tfm)) {
2103 *tfm = NULL;
2104 return err_alg;
b411b363 2105 }
b411b363 2106
0fd0ea06
PR
2107 return NO_ERROR;
2108}
b411b363 2109
0fd0ea06 2110static enum drbd_ret_code
270eb5c9 2111alloc_crypto(struct crypto *crypto, struct net_conf *new_net_conf)
0fd0ea06
PR
2112{
2113 char hmac_name[CRYPTO_MAX_ALG_NAME];
2114 enum drbd_ret_code rv;
0fd0ea06 2115
270eb5c9 2116 rv = alloc_hash(&crypto->csums_tfm, new_net_conf->csums_alg,
4b6ad6d4 2117 ERR_CSUMS_ALG);
0fd0ea06
PR
2118 if (rv != NO_ERROR)
2119 return rv;
270eb5c9 2120 rv = alloc_hash(&crypto->verify_tfm, new_net_conf->verify_alg,
4b6ad6d4 2121 ERR_VERIFY_ALG);
0fd0ea06
PR
2122 if (rv != NO_ERROR)
2123 return rv;
270eb5c9 2124 rv = alloc_hash(&crypto->integrity_tfm, new_net_conf->integrity_alg,
4b6ad6d4 2125 ERR_INTEGRITY_ALG);
0fd0ea06
PR
2126 if (rv != NO_ERROR)
2127 return rv;
270eb5c9 2128 if (new_net_conf->cram_hmac_alg[0] != 0) {
0fd0ea06 2129 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
270eb5c9 2130 new_net_conf->cram_hmac_alg);
b411b363 2131
4b6ad6d4
AG
2132 rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
2133 ERR_AUTH_ALG);
b411b363
PR
2134 }
2135
0fd0ea06
PR
2136 return rv;
2137}
b411b363 2138
0fd0ea06
PR
2139static void free_crypto(struct crypto *crypto)
2140{
0fd0ea06 2141 crypto_free_hash(crypto->cram_hmac_tfm);
8d412fc6 2142 crypto_free_hash(crypto->integrity_tfm);
0fd0ea06
PR
2143 crypto_free_hash(crypto->csums_tfm);
2144 crypto_free_hash(crypto->verify_tfm);
2145}
b411b363 2146
f399002e
LE
2147int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
2148{
a910b123 2149 struct drbd_config_context adm_ctx;
f399002e 2150 enum drbd_ret_code retcode;
bde89a9e 2151 struct drbd_connection *connection;
270eb5c9 2152 struct net_conf *old_net_conf, *new_net_conf = NULL;
f399002e
LE
2153 int err;
2154 int ovr; /* online verify running */
2155 int rsr; /* re-sync running */
0fd0ea06 2156 struct crypto crypto = { };
b411b363 2157
a910b123 2158 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_CONNECTION);
f399002e
LE
2159 if (!adm_ctx.reply_skb)
2160 return retcode;
2161 if (retcode != NO_ERROR)
9e276872 2162 goto finish;
b411b363 2163
bde89a9e 2164 connection = adm_ctx.connection;
9e276872 2165 mutex_lock(&adm_ctx.resource->adm_mutex);
b411b363 2166
270eb5c9
AG
2167 new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
2168 if (!new_net_conf) {
f399002e
LE
2169 retcode = ERR_NOMEM;
2170 goto out;
2171 }
b411b363 2172
bde89a9e 2173 conn_reconfig_start(connection);
b411b363 2174
bde89a9e 2175 mutex_lock(&connection->data.mutex);
0500813f 2176 mutex_lock(&connection->resource->conf_update);
270eb5c9 2177 old_net_conf = connection->net_conf;
2561b9c1 2178
270eb5c9 2179 if (!old_net_conf) {
a910b123 2180 drbd_msg_put_info(adm_ctx.reply_skb, "net conf missing, try connect");
f399002e 2181 retcode = ERR_INVALID_REQUEST;
2561b9c1
PR
2182 goto fail;
2183 }
2184
270eb5c9 2185 *new_net_conf = *old_net_conf;
5979e361 2186 if (should_set_defaults(info))
270eb5c9 2187 set_net_conf_defaults(new_net_conf);
f399002e 2188
270eb5c9 2189 err = net_conf_from_attrs_for_change(new_net_conf, info);
c75b9b10 2190 if (err && err != -ENOMSG) {
f399002e 2191 retcode = ERR_MANDATORY_TAG;
a910b123 2192 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
f399002e 2193 goto fail;
2561b9c1 2194 }
b411b363 2195
270eb5c9 2196 retcode = check_net_options(connection, new_net_conf);
cd64397c
PR
2197 if (retcode != NO_ERROR)
2198 goto fail;
b411b363 2199
f399002e 2200 /* re-sync running */
bde89a9e 2201 rsr = conn_resync_running(connection);
270eb5c9 2202 if (rsr && strcmp(new_net_conf->csums_alg, old_net_conf->csums_alg)) {
f399002e 2203 retcode = ERR_CSUMS_RESYNC_RUNNING;
91fd4dad 2204 goto fail;
b411b363
PR
2205 }
2206
f399002e 2207 /* online verify running */
bde89a9e 2208 ovr = conn_ov_running(connection);
270eb5c9 2209 if (ovr && strcmp(new_net_conf->verify_alg, old_net_conf->verify_alg)) {
0fd0ea06 2210 retcode = ERR_VERIFY_RUNNING;
b411b363 2211 goto fail;
f399002e 2212 }
b411b363 2213
270eb5c9 2214 retcode = alloc_crypto(&crypto, new_net_conf);
0fd0ea06 2215 if (retcode != NO_ERROR)
b411b363 2216 goto fail;
f399002e 2217
270eb5c9 2218 rcu_assign_pointer(connection->net_conf, new_net_conf);
f399002e
LE
2219
2220 if (!rsr) {
bde89a9e
AG
2221 crypto_free_hash(connection->csums_tfm);
2222 connection->csums_tfm = crypto.csums_tfm;
0fd0ea06 2223 crypto.csums_tfm = NULL;
f399002e
LE
2224 }
2225 if (!ovr) {
bde89a9e
AG
2226 crypto_free_hash(connection->verify_tfm);
2227 connection->verify_tfm = crypto.verify_tfm;
0fd0ea06 2228 crypto.verify_tfm = NULL;
b411b363
PR
2229 }
2230
bde89a9e
AG
2231 crypto_free_hash(connection->integrity_tfm);
2232 connection->integrity_tfm = crypto.integrity_tfm;
2233 if (connection->cstate >= C_WF_REPORT_PARAMS && connection->agreed_pro_version >= 100)
2234 /* Do this without trying to take connection->data.mutex again. */
2235 __drbd_send_protocol(connection, P_PROTOCOL_UPDATE);
0fd0ea06 2236
bde89a9e
AG
2237 crypto_free_hash(connection->cram_hmac_tfm);
2238 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
0fd0ea06 2239
0500813f 2240 mutex_unlock(&connection->resource->conf_update);
bde89a9e 2241 mutex_unlock(&connection->data.mutex);
91fd4dad 2242 synchronize_rcu();
270eb5c9 2243 kfree(old_net_conf);
91fd4dad 2244
69a22773
AG
2245 if (connection->cstate >= C_WF_REPORT_PARAMS) {
2246 struct drbd_peer_device *peer_device;
2247 int vnr;
2248
2249 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
2250 drbd_send_sync_param(peer_device);
2251 }
f399002e 2252
91fd4dad
PR
2253 goto done;
2254
b411b363 2255 fail:
0500813f 2256 mutex_unlock(&connection->resource->conf_update);
bde89a9e 2257 mutex_unlock(&connection->data.mutex);
0fd0ea06 2258 free_crypto(&crypto);
270eb5c9 2259 kfree(new_net_conf);
91fd4dad 2260 done:
bde89a9e 2261 conn_reconfig_done(connection);
f399002e 2262 out:
9e276872
LE
2263 mutex_unlock(&adm_ctx.resource->adm_mutex);
2264 finish:
a910b123 2265 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2266 return 0;
2267}
2268
a2972846
AG
2269static void connection_to_info(struct connection_info *info,
2270 struct drbd_connection *connection)
2271{
2272 info->conn_connection_state = connection->cstate;
2273 info->conn_role = conn_highest_peer(connection);
2274}
2275
2276static void peer_device_to_info(struct peer_device_info *info,
2277 struct drbd_peer_device *peer_device)
2278{
2279 struct drbd_device *device = peer_device->device;
2280
2281 info->peer_repl_state =
2282 max_t(enum drbd_conns, C_WF_REPORT_PARAMS, device->state.conn);
2283 info->peer_disk_state = device->state.pdsk;
2284 info->peer_resync_susp_user = device->state.user_isp;
2285 info->peer_resync_susp_peer = device->state.peer_isp;
2286 info->peer_resync_susp_dependency = device->state.aftr_isp;
2287}
2288
3b98c0c2 2289int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
b411b363 2290{
a2972846
AG
2291 struct connection_info connection_info;
2292 enum drbd_notification_type flags;
2293 unsigned int peer_devices = 0;
a910b123 2294 struct drbd_config_context adm_ctx;
c06ece6b 2295 struct drbd_peer_device *peer_device;
270eb5c9 2296 struct net_conf *old_net_conf, *new_net_conf = NULL;
0fd0ea06 2297 struct crypto crypto = { };
77c556f6 2298 struct drbd_resource *resource;
bde89a9e 2299 struct drbd_connection *connection;
3b98c0c2
LE
2300 enum drbd_ret_code retcode;
2301 int i;
2302 int err;
b411b363 2303
a910b123 2304 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
b411b363 2305
3b98c0c2
LE
2306 if (!adm_ctx.reply_skb)
2307 return retcode;
2308 if (retcode != NO_ERROR)
2309 goto out;
089c075d 2310 if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
a910b123 2311 drbd_msg_put_info(adm_ctx.reply_skb, "connection endpoint(s) missing");
089c075d
AG
2312 retcode = ERR_INVALID_REQUEST;
2313 goto out;
2314 }
b411b363 2315
089c075d
AG
2316 /* No need for _rcu here. All reconfiguration is
2317 * strictly serialized on genl_lock(). We are protected against
2318 * concurrent reconfiguration/addition/deletion */
77c556f6
AG
2319 for_each_resource(resource, &drbd_resources) {
2320 for_each_connection(connection, resource) {
2321 if (nla_len(adm_ctx.my_addr) == connection->my_addr_len &&
2322 !memcmp(nla_data(adm_ctx.my_addr), &connection->my_addr,
2323 connection->my_addr_len)) {
2324 retcode = ERR_LOCAL_ADDR;
2325 goto out;
2326 }
b411b363 2327
77c556f6
AG
2328 if (nla_len(adm_ctx.peer_addr) == connection->peer_addr_len &&
2329 !memcmp(nla_data(adm_ctx.peer_addr), &connection->peer_addr,
2330 connection->peer_addr_len)) {
2331 retcode = ERR_PEER_ADDR;
2332 goto out;
2333 }
089c075d 2334 }
b411b363
PR
2335 }
2336
9e276872 2337 mutex_lock(&adm_ctx.resource->adm_mutex);
3ab706fe 2338 connection = first_connection(adm_ctx.resource);
bde89a9e 2339 conn_reconfig_start(connection);
b411b363 2340
bde89a9e 2341 if (connection->cstate > C_STANDALONE) {
b411b363 2342 retcode = ERR_NET_CONFIGURED;
b411b363
PR
2343 goto fail;
2344 }
2345
a209b4ae 2346 /* allocation not in the IO path, drbdsetup / netlink process context */
270eb5c9
AG
2347 new_net_conf = kzalloc(sizeof(*new_net_conf), GFP_KERNEL);
2348 if (!new_net_conf) {
b411b363 2349 retcode = ERR_NOMEM;
b411b363
PR
2350 goto fail;
2351 }
2352
270eb5c9 2353 set_net_conf_defaults(new_net_conf);
b411b363 2354
270eb5c9 2355 err = net_conf_from_attrs(new_net_conf, info);
25e40932 2356 if (err && err != -ENOMSG) {
b411b363 2357 retcode = ERR_MANDATORY_TAG;
a910b123 2358 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
b411b363
PR
2359 goto fail;
2360 }
2361
270eb5c9 2362 retcode = check_net_options(connection, new_net_conf);
cd64397c 2363 if (retcode != NO_ERROR)
422028b1 2364 goto fail;
b411b363 2365
270eb5c9 2366 retcode = alloc_crypto(&crypto, new_net_conf);
0fd0ea06
PR
2367 if (retcode != NO_ERROR)
2368 goto fail;
b411b363 2369
270eb5c9 2370 ((char *)new_net_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
7b4e4d31 2371
b5043c5e 2372 drbd_flush_workqueue(&connection->sender_work);
b411b363 2373
0500813f 2374 mutex_lock(&adm_ctx.resource->conf_update);
270eb5c9
AG
2375 old_net_conf = connection->net_conf;
2376 if (old_net_conf) {
b411b363 2377 retcode = ERR_NET_CONFIGURED;
0500813f 2378 mutex_unlock(&adm_ctx.resource->conf_update);
b411b363
PR
2379 goto fail;
2380 }
270eb5c9 2381 rcu_assign_pointer(connection->net_conf, new_net_conf);
b411b363 2382
bde89a9e
AG
2383 conn_free_crypto(connection);
2384 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
2385 connection->integrity_tfm = crypto.integrity_tfm;
2386 connection->csums_tfm = crypto.csums_tfm;
2387 connection->verify_tfm = crypto.verify_tfm;
b411b363 2388
bde89a9e
AG
2389 connection->my_addr_len = nla_len(adm_ctx.my_addr);
2390 memcpy(&connection->my_addr, nla_data(adm_ctx.my_addr), connection->my_addr_len);
2391 connection->peer_addr_len = nla_len(adm_ctx.peer_addr);
2392 memcpy(&connection->peer_addr, nla_data(adm_ctx.peer_addr), connection->peer_addr_len);
b411b363 2393
a2972846
AG
2394 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2395 peer_devices++;
2396 }
2397
2398 connection_to_info(&connection_info, connection);
2399 flags = (peer_devices--) ? NOTIFY_CONTINUES : 0;
2400 mutex_lock(&notification_mutex);
2401 notify_connection_state(NULL, 0, connection, &connection_info, NOTIFY_CREATE | flags);
2402 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2403 struct peer_device_info peer_device_info;
2404
2405 peer_device_to_info(&peer_device_info, peer_device);
2406 flags = (peer_devices--) ? NOTIFY_CONTINUES : 0;
2407 notify_peer_device_state(NULL, 0, peer_device, &peer_device_info, NOTIFY_CREATE | flags);
2408 }
2409 mutex_unlock(&notification_mutex);
0500813f 2410 mutex_unlock(&adm_ctx.resource->conf_update);
b411b363 2411
695d08fa 2412 rcu_read_lock();
c06ece6b
AG
2413 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2414 struct drbd_device *device = peer_device->device;
b30ab791
AG
2415 device->send_cnt = 0;
2416 device->recv_cnt = 0;
b411b363 2417 }
695d08fa 2418 rcu_read_unlock();
b411b363 2419
bde89a9e 2420 retcode = conn_request_state(connection, NS(conn, C_UNCONNECTED), CS_VERBOSE);
b411b363 2421
bde89a9e 2422 conn_reconfig_done(connection);
9e276872 2423 mutex_unlock(&adm_ctx.resource->adm_mutex);
a910b123 2424 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363 2425 return 0;
b411b363 2426
b411b363 2427fail:
0fd0ea06 2428 free_crypto(&crypto);
270eb5c9 2429 kfree(new_net_conf);
b411b363 2430
bde89a9e 2431 conn_reconfig_done(connection);
9e276872 2432 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 2433out:
a910b123 2434 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2435 return 0;
2436}
2437
bde89a9e 2438static enum drbd_state_rv conn_try_disconnect(struct drbd_connection *connection, bool force)
85f75dd7
LE
2439{
2440 enum drbd_state_rv rv;
85f75dd7 2441
bde89a9e 2442 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
f3dfa40a 2443 force ? CS_HARD : 0);
85f75dd7
LE
2444
2445 switch (rv) {
2446 case SS_NOTHING_TO_DO:
f3dfa40a 2447 break;
85f75dd7
LE
2448 case SS_ALREADY_STANDALONE:
2449 return SS_SUCCESS;
2450 case SS_PRIMARY_NOP:
2451 /* Our state checking code wants to see the peer outdated. */
bde89a9e 2452 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING, pdsk, D_OUTDATED), 0);
2bd5ed5d
PR
2453
2454 if (rv == SS_OUTDATE_WO_CONN) /* lost connection before graceful disconnect succeeded */
bde89a9e 2455 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_VERBOSE);
2bd5ed5d 2456
85f75dd7
LE
2457 break;
2458 case SS_CW_FAILED_BY_PEER:
2459 /* The peer probably wants to see us outdated. */
bde89a9e 2460 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING,
85f75dd7
LE
2461 disk, D_OUTDATED), 0);
2462 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
bde89a9e 2463 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
f3dfa40a 2464 CS_HARD);
b411b363 2465 }
85f75dd7
LE
2466 break;
2467 default:;
2468 /* no special handling necessary */
2469 }
2470
f3dfa40a
LE
2471 if (rv >= SS_SUCCESS) {
2472 enum drbd_state_rv rv2;
2473 /* No one else can reconfigure the network while I am here.
2474 * The state handling only uses drbd_thread_stop_nowait(),
2475 * we want to really wait here until the receiver is no more.
2476 */
9693da23 2477 drbd_thread_stop(&connection->receiver);
f3dfa40a
LE
2478
2479 /* Race breaker. This additional state change request may be
2480 * necessary, if this was a forced disconnect during a receiver
2481 * restart. We may have "killed" the receiver thread just
8fe60551 2482 * after drbd_receiver() returned. Typically, we should be
f3dfa40a
LE
2483 * C_STANDALONE already, now, and this becomes a no-op.
2484 */
bde89a9e 2485 rv2 = conn_request_state(connection, NS(conn, C_STANDALONE),
f3dfa40a
LE
2486 CS_VERBOSE | CS_HARD);
2487 if (rv2 < SS_SUCCESS)
1ec861eb 2488 drbd_err(connection,
f3dfa40a
LE
2489 "unexpected rv2=%d in conn_try_disconnect()\n",
2490 rv2);
a2972846
AG
2491 /* Unlike in DRBD 9, the state engine has generated
2492 * NOTIFY_DESTROY events before clearing connection->net_conf. */
b411b363 2493 }
85f75dd7
LE
2494 return rv;
2495}
b411b363 2496
3b98c0c2 2497int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
b411b363 2498{
a910b123 2499 struct drbd_config_context adm_ctx;
3b98c0c2 2500 struct disconnect_parms parms;
bde89a9e 2501 struct drbd_connection *connection;
85f75dd7 2502 enum drbd_state_rv rv;
3b98c0c2
LE
2503 enum drbd_ret_code retcode;
2504 int err;
2561b9c1 2505
a910b123 2506 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_CONNECTION);
3b98c0c2
LE
2507 if (!adm_ctx.reply_skb)
2508 return retcode;
2509 if (retcode != NO_ERROR)
2561b9c1 2510 goto fail;
b411b363 2511
bde89a9e 2512 connection = adm_ctx.connection;
3b98c0c2
LE
2513 memset(&parms, 0, sizeof(parms));
2514 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
f399002e 2515 err = disconnect_parms_from_attrs(&parms, info);
3b98c0c2
LE
2516 if (err) {
2517 retcode = ERR_MANDATORY_TAG;
a910b123 2518 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
b411b363
PR
2519 goto fail;
2520 }
2521 }
2522
9e276872 2523 mutex_lock(&adm_ctx.resource->adm_mutex);
bde89a9e 2524 rv = conn_try_disconnect(connection, parms.force_disconnect);
85f75dd7 2525 if (rv < SS_SUCCESS)
f3dfa40a
LE
2526 retcode = rv; /* FIXME: Type mismatch. */
2527 else
2528 retcode = NO_ERROR;
9e276872 2529 mutex_unlock(&adm_ctx.resource->adm_mutex);
b411b363 2530 fail:
a910b123 2531 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2532 return 0;
2533}
2534
b30ab791 2535void resync_after_online_grow(struct drbd_device *device)
b411b363
PR
2536{
2537 int iass; /* I am sync source */
2538
d0180171 2539 drbd_info(device, "Resync of new storage after online grow\n");
b30ab791
AG
2540 if (device->state.role != device->state.peer)
2541 iass = (device->state.role == R_PRIMARY);
b411b363 2542 else
a6b32bc3 2543 iass = test_bit(RESOLVE_CONFLICTS, &first_peer_device(device)->connection->flags);
b411b363
PR
2544
2545 if (iass)
b30ab791 2546 drbd_start_resync(device, C_SYNC_SOURCE);
b411b363 2547 else
b30ab791 2548 _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
b411b363
PR
2549}
2550
3b98c0c2 2551int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
b411b363 2552{
a910b123 2553 struct drbd_config_context adm_ctx;
daeda1cc 2554 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
3b98c0c2 2555 struct resize_parms rs;
b30ab791 2556 struct drbd_device *device;
3b98c0c2 2557 enum drbd_ret_code retcode;
b411b363 2558 enum determine_dev_size dd;
d752b269 2559 bool change_al_layout = false;
6495d2c6 2560 enum dds_flags ddsf;
daeda1cc 2561 sector_t u_size;
3b98c0c2 2562 int err;
b411b363 2563
a910b123 2564 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
2565 if (!adm_ctx.reply_skb)
2566 return retcode;
2567 if (retcode != NO_ERROR)
9e276872 2568 goto finish;
3b98c0c2 2569
9e276872 2570 mutex_lock(&adm_ctx.resource->adm_mutex);
b30ab791
AG
2571 device = adm_ctx.device;
2572 if (!get_ldev(device)) {
d752b269
PR
2573 retcode = ERR_NO_DISK;
2574 goto fail;
2575 }
2576
3b98c0c2 2577 memset(&rs, 0, sizeof(struct resize_parms));
b30ab791
AG
2578 rs.al_stripes = device->ldev->md.al_stripes;
2579 rs.al_stripe_size = device->ldev->md.al_stripe_size_4k * 4;
3b98c0c2 2580 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
f399002e 2581 err = resize_parms_from_attrs(&rs, info);
b411b363 2582 if (err) {
3b98c0c2 2583 retcode = ERR_MANDATORY_TAG;
a910b123 2584 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
d752b269 2585 goto fail_ldev;
b411b363
PR
2586 }
2587 }
2588
b30ab791 2589 if (device->state.conn > C_CONNECTED) {
b411b363 2590 retcode = ERR_RESIZE_RESYNC;
d752b269 2591 goto fail_ldev;
b411b363 2592 }
b411b363 2593
b30ab791
AG
2594 if (device->state.role == R_SECONDARY &&
2595 device->state.peer == R_SECONDARY) {
b411b363 2596 retcode = ERR_NO_PRIMARY;
d752b269 2597 goto fail_ldev;
b411b363 2598 }
b411b363 2599
a6b32bc3 2600 if (rs.no_resync && first_peer_device(device)->connection->agreed_pro_version < 93) {
6495d2c6 2601 retcode = ERR_NEED_APV_93;
9bcd2521 2602 goto fail_ldev;
6495d2c6
PR
2603 }
2604
daeda1cc 2605 rcu_read_lock();
b30ab791 2606 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
daeda1cc
PR
2607 rcu_read_unlock();
2608 if (u_size != (sector_t)rs.resize_size) {
2609 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
2610 if (!new_disk_conf) {
778f271d 2611 retcode = ERR_NOMEM;
9bcd2521 2612 goto fail_ldev;
778f271d
PR
2613 }
2614 }
2615
b30ab791
AG
2616 if (device->ldev->md.al_stripes != rs.al_stripes ||
2617 device->ldev->md.al_stripe_size_4k != rs.al_stripe_size / 4) {
d752b269
PR
2618 u32 al_size_k = rs.al_stripes * rs.al_stripe_size;
2619
2620 if (al_size_k > (16 * 1024 * 1024)) {
2621 retcode = ERR_MD_LAYOUT_TOO_BIG;
2622 goto fail_ldev;
2623 }
2624
2625 if (al_size_k < MD_32kB_SECT/2) {
2626 retcode = ERR_MD_LAYOUT_TOO_SMALL;
2627 goto fail_ldev;
2628 }
2629
cdc6af8d 2630 if (device->state.conn != C_CONNECTED && !rs.resize_force) {
d752b269
PR
2631 retcode = ERR_MD_LAYOUT_CONNECTED;
2632 goto fail_ldev;
2633 }
2634
2635 change_al_layout = true;
2636 }
2637
b30ab791
AG
2638 if (device->ldev->known_size != drbd_get_capacity(device->ldev->backing_bdev))
2639 device->ldev->known_size = drbd_get_capacity(device->ldev->backing_bdev);
b411b363 2640
daeda1cc 2641 if (new_disk_conf) {
0500813f 2642 mutex_lock(&device->resource->conf_update);
b30ab791 2643 old_disk_conf = device->ldev->disk_conf;
daeda1cc
PR
2644 *new_disk_conf = *old_disk_conf;
2645 new_disk_conf->disk_size = (sector_t)rs.resize_size;
b30ab791 2646 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
0500813f 2647 mutex_unlock(&device->resource->conf_update);
daeda1cc
PR
2648 synchronize_rcu();
2649 kfree(old_disk_conf);
b411b363
PR
2650 }
2651
6495d2c6 2652 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
b30ab791
AG
2653 dd = drbd_determine_dev_size(device, ddsf, change_al_layout ? &rs : NULL);
2654 drbd_md_sync(device);
2655 put_ldev(device);
e96c9633 2656 if (dd == DS_ERROR) {
b411b363
PR
2657 retcode = ERR_NOMEM_BITMAP;
2658 goto fail;
d752b269
PR
2659 } else if (dd == DS_ERROR_SPACE_MD) {
2660 retcode = ERR_MD_LAYOUT_NO_FIT;
2661 goto fail;
2662 } else if (dd == DS_ERROR_SHRINK) {
2663 retcode = ERR_IMPLICIT_SHRINK;
2664 goto fail;
b411b363 2665 }
778f271d 2666
b30ab791 2667 if (device->state.conn == C_CONNECTED) {
e96c9633 2668 if (dd == DS_GREW)
b30ab791 2669 set_bit(RESIZE_PENDING, &device->flags);
b411b363 2670
69a22773
AG
2671 drbd_send_uuids(first_peer_device(device));
2672 drbd_send_sizes(first_peer_device(device), 1, ddsf);
778f271d
PR
2673 }
2674
b411b363 2675 fail:
9e276872
LE
2676 mutex_unlock(&adm_ctx.resource->adm_mutex);
2677 finish:
a910b123 2678 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363 2679 return 0;
b411b363 2680
9bcd2521 2681 fail_ldev:
b30ab791 2682 put_ldev(device);
9bcd2521 2683 goto fail;
b411b363 2684}
b411b363 2685
f399002e 2686int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
b411b363 2687{
a910b123 2688 struct drbd_config_context adm_ctx;
3b98c0c2 2689 enum drbd_ret_code retcode;
b57a1e27 2690 struct res_opts res_opts;
f399002e 2691 int err;
b411b363 2692
a910b123 2693 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
3b98c0c2
LE
2694 if (!adm_ctx.reply_skb)
2695 return retcode;
2696 if (retcode != NO_ERROR)
2697 goto fail;
b411b363 2698
eb6bea67 2699 res_opts = adm_ctx.resource->res_opts;
5979e361 2700 if (should_set_defaults(info))
b966b5dd 2701 set_res_opts_defaults(&res_opts);
b411b363 2702
b57a1e27 2703 err = res_opts_from_attrs(&res_opts, info);
c75b9b10 2704 if (err && err != -ENOMSG) {
b411b363 2705 retcode = ERR_MANDATORY_TAG;
a910b123 2706 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
b411b363
PR
2707 goto fail;
2708 }
2709
9e276872 2710 mutex_lock(&adm_ctx.resource->adm_mutex);
eb6bea67 2711 err = set_resource_options(adm_ctx.resource, &res_opts);
afbbfa88
AG
2712 if (err) {
2713 retcode = ERR_INVALID_REQUEST;
2714 if (err == -ENOMEM)
2715 retcode = ERR_NOMEM;
b411b363 2716 }
9e276872 2717 mutex_unlock(&adm_ctx.resource->adm_mutex);
b411b363 2718
b411b363 2719fail:
a910b123 2720 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2721 return 0;
2722}
2723
3b98c0c2 2724int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
b411b363 2725{
a910b123 2726 struct drbd_config_context adm_ctx;
b30ab791 2727 struct drbd_device *device;
3b98c0c2
LE
2728 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2729
a910b123 2730 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
2731 if (!adm_ctx.reply_skb)
2732 return retcode;
2733 if (retcode != NO_ERROR)
2734 goto out;
2735
b30ab791 2736 device = adm_ctx.device;
8fe39aac
PR
2737 if (!get_ldev(device)) {
2738 retcode = ERR_NO_DISK;
2739 goto out;
2740 }
2741
2742 mutex_lock(&adm_ctx.resource->adm_mutex);
b411b363 2743
194bfb32 2744 /* If there is still bitmap IO pending, probably because of a previous
7ee1fb93
LE
2745 * resync just being finished, wait for it before requesting a new resync.
2746 * Also wait for it's after_state_ch(). */
b30ab791
AG
2747 drbd_suspend_io(device);
2748 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
b5043c5e 2749 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
194bfb32 2750
0b2dafcd
PR
2751 /* If we happen to be C_STANDALONE R_SECONDARY, just change to
2752 * D_INCONSISTENT, and set all bits in the bitmap. Otherwise,
2753 * try to start a resync handshake as sync target for full sync.
9376d9f8 2754 */
b30ab791
AG
2755 if (device->state.conn == C_STANDALONE && device->state.role == R_SECONDARY) {
2756 retcode = drbd_request_state(device, NS(disk, D_INCONSISTENT));
0b2dafcd 2757 if (retcode >= SS_SUCCESS) {
b30ab791 2758 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
0b2dafcd
PR
2759 "set_n_write from invalidate", BM_LOCKED_MASK))
2760 retcode = ERR_IO_MD_DISK;
2761 }
2762 } else
b30ab791
AG
2763 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_T));
2764 drbd_resume_io(device);
9e276872 2765 mutex_unlock(&adm_ctx.resource->adm_mutex);
8fe39aac 2766 put_ldev(device);
3b98c0c2 2767out:
a910b123 2768 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2769 return 0;
2770}
2771
3b98c0c2
LE
2772static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2773 union drbd_state mask, union drbd_state val)
b411b363 2774{
a910b123 2775 struct drbd_config_context adm_ctx;
3b98c0c2 2776 enum drbd_ret_code retcode;
194bfb32 2777
a910b123 2778 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
2779 if (!adm_ctx.reply_skb)
2780 return retcode;
2781 if (retcode != NO_ERROR)
2782 goto out;
b411b363 2783
9e276872 2784 mutex_lock(&adm_ctx.resource->adm_mutex);
b30ab791 2785 retcode = drbd_request_state(adm_ctx.device, mask, val);
9e276872 2786 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 2787out:
a910b123 2788 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2789 return 0;
2790}
2791
8fe39aac 2792static int drbd_bmio_set_susp_al(struct drbd_device *device) __must_hold(local)
0778286a
PR
2793{
2794 int rv;
2795
b30ab791
AG
2796 rv = drbd_bmio_set_n_write(device);
2797 drbd_suspend_al(device);
0778286a
PR
2798 return rv;
2799}
2800
3b98c0c2 2801int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
b411b363 2802{
a910b123 2803 struct drbd_config_context adm_ctx;
25b0d6c8 2804 int retcode; /* drbd_ret_code, drbd_state_rv */
b30ab791 2805 struct drbd_device *device;
25b0d6c8 2806
a910b123 2807 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
25b0d6c8
PR
2808 if (!adm_ctx.reply_skb)
2809 return retcode;
2810 if (retcode != NO_ERROR)
2811 goto out;
2812
b30ab791 2813 device = adm_ctx.device;
8fe39aac
PR
2814 if (!get_ldev(device)) {
2815 retcode = ERR_NO_DISK;
2816 goto out;
2817 }
2818
2819 mutex_lock(&adm_ctx.resource->adm_mutex);
b411b363 2820
194bfb32 2821 /* If there is still bitmap IO pending, probably because of a previous
7ee1fb93
LE
2822 * resync just being finished, wait for it before requesting a new resync.
2823 * Also wait for it's after_state_ch(). */
b30ab791
AG
2824 drbd_suspend_io(device);
2825 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
b5043c5e 2826 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
194bfb32 2827
0b2dafcd
PR
2828 /* If we happen to be C_STANDALONE R_PRIMARY, just set all bits
2829 * in the bitmap. Otherwise, try to start a resync handshake
2830 * as sync source for full sync.
2831 */
b30ab791 2832 if (device->state.conn == C_STANDALONE && device->state.role == R_PRIMARY) {
0b2dafcd
PR
2833 /* The peer will get a resync upon connect anyways. Just make that
2834 into a full resync. */
b30ab791 2835 retcode = drbd_request_state(device, NS(pdsk, D_INCONSISTENT));
0b2dafcd 2836 if (retcode >= SS_SUCCESS) {
b30ab791 2837 if (drbd_bitmap_io(device, &drbd_bmio_set_susp_al,
0b2dafcd
PR
2838 "set_n_write from invalidate_peer",
2839 BM_LOCKED_SET_ALLOWED))
2840 retcode = ERR_IO_MD_DISK;
2841 }
2842 } else
b30ab791
AG
2843 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_S));
2844 drbd_resume_io(device);
9e276872 2845 mutex_unlock(&adm_ctx.resource->adm_mutex);
8fe39aac 2846 put_ldev(device);
25b0d6c8 2847out:
a910b123 2848 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2849 return 0;
2850}
2851
3b98c0c2 2852int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
b411b363 2853{
a910b123 2854 struct drbd_config_context adm_ctx;
3b98c0c2 2855 enum drbd_ret_code retcode;
b411b363 2856
a910b123 2857 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
2858 if (!adm_ctx.reply_skb)
2859 return retcode;
2860 if (retcode != NO_ERROR)
2861 goto out;
b411b363 2862
9e276872 2863 mutex_lock(&adm_ctx.resource->adm_mutex);
b30ab791 2864 if (drbd_request_state(adm_ctx.device, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
3b98c0c2 2865 retcode = ERR_PAUSE_IS_SET;
9e276872 2866 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 2867out:
a910b123 2868 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2869 return 0;
2870}
2871
3b98c0c2 2872int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
b411b363 2873{
a910b123 2874 struct drbd_config_context adm_ctx;
da9fbc27 2875 union drbd_dev_state s;
3b98c0c2
LE
2876 enum drbd_ret_code retcode;
2877
a910b123 2878 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
2879 if (!adm_ctx.reply_skb)
2880 return retcode;
2881 if (retcode != NO_ERROR)
2882 goto out;
b411b363 2883
9e276872 2884 mutex_lock(&adm_ctx.resource->adm_mutex);
b30ab791
AG
2885 if (drbd_request_state(adm_ctx.device, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2886 s = adm_ctx.device->state;
cd88d030
PR
2887 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2888 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2889 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2890 } else {
2891 retcode = ERR_PAUSE_IS_CLEAR;
2892 }
2893 }
9e276872 2894 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 2895out:
a910b123 2896 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2897 return 0;
2898}
2899
3b98c0c2 2900int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
b411b363 2901{
3b98c0c2 2902 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
b411b363
PR
2903}
2904
3b98c0c2 2905int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
b411b363 2906{
a910b123 2907 struct drbd_config_context adm_ctx;
b30ab791 2908 struct drbd_device *device;
3b98c0c2
LE
2909 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2910
a910b123 2911 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
2912 if (!adm_ctx.reply_skb)
2913 return retcode;
2914 if (retcode != NO_ERROR)
2915 goto out;
2916
9e276872 2917 mutex_lock(&adm_ctx.resource->adm_mutex);
b30ab791
AG
2918 device = adm_ctx.device;
2919 if (test_bit(NEW_CUR_UUID, &device->flags)) {
2920 drbd_uuid_new_current(device);
2921 clear_bit(NEW_CUR_UUID, &device->flags);
43a5182c 2922 }
b30ab791
AG
2923 drbd_suspend_io(device);
2924 retcode = drbd_request_state(device, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
3b98c0c2 2925 if (retcode == SS_SUCCESS) {
b30ab791 2926 if (device->state.conn < C_CONNECTED)
a6b32bc3 2927 tl_clear(first_peer_device(device)->connection);
b30ab791 2928 if (device->state.disk == D_DISKLESS || device->state.disk == D_FAILED)
a6b32bc3 2929 tl_restart(first_peer_device(device)->connection, FAIL_FROZEN_DISK_IO);
265be2d0 2930 }
b30ab791 2931 drbd_resume_io(device);
9e276872 2932 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 2933out:
a910b123 2934 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2935 return 0;
2936}
2937
3b98c0c2 2938int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
b411b363 2939{
3b98c0c2 2940 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
b411b363
PR
2941}
2942
251b8f8e
AG
2943static int nla_put_drbd_cfg_context(struct sk_buff *skb,
2944 struct drbd_resource *resource,
2945 struct drbd_connection *connection,
2946 struct drbd_device *device)
b411b363 2947{
543cc10b
LE
2948 struct nlattr *nla;
2949 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
2950 if (!nla)
2951 goto nla_put_failure;
251b8f8e
AG
2952 if (device &&
2953 nla_put_u32(skb, T_ctx_volume, device->vnr))
26ec9287 2954 goto nla_put_failure;
f597f6b8 2955 if (nla_put_string(skb, T_ctx_resource_name, resource->name))
26ec9287 2956 goto nla_put_failure;
251b8f8e
AG
2957 if (connection) {
2958 if (connection->my_addr_len &&
2959 nla_put(skb, T_ctx_my_addr, connection->my_addr_len, &connection->my_addr))
2960 goto nla_put_failure;
2961 if (connection->peer_addr_len &&
2962 nla_put(skb, T_ctx_peer_addr, connection->peer_addr_len, &connection->peer_addr))
2963 goto nla_put_failure;
2964 }
543cc10b
LE
2965 nla_nest_end(skb, nla);
2966 return 0;
b411b363 2967
543cc10b
LE
2968nla_put_failure:
2969 if (nla)
2970 nla_nest_cancel(skb, nla);
2971 return -EMSGSIZE;
2972}
b411b363 2973
a55bbd37
AG
2974/*
2975 * The generic netlink dump callbacks are called outside the genl_lock(), so
2976 * they cannot use the simple attribute parsing code which uses global
2977 * attribute tables.
2978 */
2979static struct nlattr *find_cfg_context_attr(const struct nlmsghdr *nlh, int attr)
2980{
2981 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
2982 const int maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
2983 struct nlattr *nla;
2984
2985 nla = nla_find(nlmsg_attrdata(nlh, hdrlen), nlmsg_attrlen(nlh, hdrlen),
2986 DRBD_NLA_CFG_CONTEXT);
2987 if (!nla)
2988 return NULL;
2989 return drbd_nla_find_nested(maxtype, nla, __nla_type(attr));
2990}
2991
2992static void resource_to_info(struct resource_info *, struct drbd_resource *);
2993
2994int drbd_adm_dump_resources(struct sk_buff *skb, struct netlink_callback *cb)
2995{
2996 struct drbd_genlmsghdr *dh;
2997 struct drbd_resource *resource;
2998 struct resource_info resource_info;
2999 struct resource_statistics resource_statistics;
3000 int err;
3001
3002 rcu_read_lock();
3003 if (cb->args[0]) {
3004 for_each_resource_rcu(resource, &drbd_resources)
3005 if (resource == (struct drbd_resource *)cb->args[0])
3006 goto found_resource;
3007 err = 0; /* resource was probably deleted */
3008 goto out;
3009 }
3010 resource = list_entry(&drbd_resources,
3011 struct drbd_resource, resources);
3012
3013found_resource:
3014 list_for_each_entry_continue_rcu(resource, &drbd_resources, resources) {
3015 goto put_result;
3016 }
3017 err = 0;
3018 goto out;
3019
3020put_result:
3021 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3022 cb->nlh->nlmsg_seq, &drbd_genl_family,
3023 NLM_F_MULTI, DRBD_ADM_GET_RESOURCES);
3024 err = -ENOMEM;
3025 if (!dh)
3026 goto out;
3027 dh->minor = -1U;
3028 dh->ret_code = NO_ERROR;
3029 err = nla_put_drbd_cfg_context(skb, resource, NULL, NULL);
3030 if (err)
3031 goto out;
3032 err = res_opts_to_skb(skb, &resource->res_opts, !capable(CAP_SYS_ADMIN));
3033 if (err)
3034 goto out;
3035 resource_to_info(&resource_info, resource);
3036 err = resource_info_to_skb(skb, &resource_info, !capable(CAP_SYS_ADMIN));
3037 if (err)
3038 goto out;
3039 resource_statistics.res_stat_write_ordering = resource->write_ordering;
3040 err = resource_statistics_to_skb(skb, &resource_statistics, !capable(CAP_SYS_ADMIN));
3041 if (err)
3042 goto out;
3043 cb->args[0] = (long)resource;
3044 genlmsg_end(skb, dh);
3045 err = 0;
3046
3047out:
3048 rcu_read_unlock();
3049 if (err)
3050 return err;
3051 return skb->len;
3052}
3053
3054static void device_to_statistics(struct device_statistics *s,
3055 struct drbd_device *device)
3056{
3057 memset(s, 0, sizeof(*s));
3058 s->dev_upper_blocked = !may_inc_ap_bio(device);
3059 if (get_ldev(device)) {
3060 struct drbd_md *md = &device->ldev->md;
3061 u64 *history_uuids = (u64 *)s->history_uuids;
3062 struct request_queue *q;
3063 int n;
3064
3065 spin_lock_irq(&md->uuid_lock);
3066 s->dev_current_uuid = md->uuid[UI_CURRENT];
3067 BUILD_BUG_ON(sizeof(s->history_uuids) < UI_HISTORY_END - UI_HISTORY_START + 1);
3068 for (n = 0; n < UI_HISTORY_END - UI_HISTORY_START + 1; n++)
3069 history_uuids[n] = md->uuid[UI_HISTORY_START + n];
3070 for (; n < HISTORY_UUIDS; n++)
3071 history_uuids[n] = 0;
3072 s->history_uuids_len = HISTORY_UUIDS;
3073 spin_unlock_irq(&md->uuid_lock);
3074
3075 s->dev_disk_flags = md->flags;
3076 q = bdev_get_queue(device->ldev->backing_bdev);
3077 s->dev_lower_blocked =
3078 bdi_congested(&q->backing_dev_info,
3079 (1 << WB_async_congested) |
3080 (1 << WB_sync_congested));
3081 put_ldev(device);
3082 }
3083 s->dev_size = drbd_get_capacity(device->this_bdev);
3084 s->dev_read = device->read_cnt;
3085 s->dev_write = device->writ_cnt;
3086 s->dev_al_writes = device->al_writ_cnt;
3087 s->dev_bm_writes = device->bm_writ_cnt;
3088 s->dev_upper_pending = atomic_read(&device->ap_bio_cnt);
3089 s->dev_lower_pending = atomic_read(&device->local_cnt);
3090 s->dev_al_suspended = test_bit(AL_SUSPENDED, &device->flags);
3091 s->dev_exposed_data_uuid = device->ed_uuid;
3092}
3093
3094static int put_resource_in_arg0(struct netlink_callback *cb, int holder_nr)
3095{
3096 if (cb->args[0]) {
3097 struct drbd_resource *resource =
3098 (struct drbd_resource *)cb->args[0];
3099 kref_put(&resource->kref, drbd_destroy_resource);
3100 }
3101
3102 return 0;
3103}
3104
3105int drbd_adm_dump_devices_done(struct netlink_callback *cb) {
3106 return put_resource_in_arg0(cb, 7);
3107}
3108
3109static void device_to_info(struct device_info *, struct drbd_device *);
3110
3111int drbd_adm_dump_devices(struct sk_buff *skb, struct netlink_callback *cb)
3112{
3113 struct nlattr *resource_filter;
3114 struct drbd_resource *resource;
3115 struct drbd_device *uninitialized_var(device);
3116 int minor, err, retcode;
3117 struct drbd_genlmsghdr *dh;
3118 struct device_info device_info;
3119 struct device_statistics device_statistics;
3120 struct idr *idr_to_search;
3121
3122 resource = (struct drbd_resource *)cb->args[0];
3123 if (!cb->args[0] && !cb->args[1]) {
3124 resource_filter = find_cfg_context_attr(cb->nlh, T_ctx_resource_name);
3125 if (resource_filter) {
3126 retcode = ERR_RES_NOT_KNOWN;
3127 resource = drbd_find_resource(nla_data(resource_filter));
3128 if (!resource)
3129 goto put_result;
3130 cb->args[0] = (long)resource;
3131 }
3132 }
3133
3134 rcu_read_lock();
3135 minor = cb->args[1];
3136 idr_to_search = resource ? &resource->devices : &drbd_devices;
3137 device = idr_get_next(idr_to_search, &minor);
3138 if (!device) {
3139 err = 0;
3140 goto out;
3141 }
3142 idr_for_each_entry_continue(idr_to_search, device, minor) {
3143 retcode = NO_ERROR;
3144 goto put_result; /* only one iteration */
3145 }
3146 err = 0;
3147 goto out; /* no more devices */
3148
3149put_result:
3150 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3151 cb->nlh->nlmsg_seq, &drbd_genl_family,
3152 NLM_F_MULTI, DRBD_ADM_GET_DEVICES);
3153 err = -ENOMEM;
3154 if (!dh)
3155 goto out;
3156 dh->ret_code = retcode;
3157 dh->minor = -1U;
3158 if (retcode == NO_ERROR) {
3159 dh->minor = device->minor;
3160 err = nla_put_drbd_cfg_context(skb, device->resource, NULL, device);
3161 if (err)
3162 goto out;
3163 if (get_ldev(device)) {
3164 struct disk_conf *disk_conf =
3165 rcu_dereference(device->ldev->disk_conf);
3166
3167 err = disk_conf_to_skb(skb, disk_conf, !capable(CAP_SYS_ADMIN));
3168 put_ldev(device);
3169 if (err)
3170 goto out;
3171 }
3172 device_to_info(&device_info, device);
3173 err = device_info_to_skb(skb, &device_info, !capable(CAP_SYS_ADMIN));
3174 if (err)
3175 goto out;
3176
3177 device_to_statistics(&device_statistics, device);
3178 err = device_statistics_to_skb(skb, &device_statistics, !capable(CAP_SYS_ADMIN));
3179 if (err)
3180 goto out;
3181 cb->args[1] = minor + 1;
3182 }
3183 genlmsg_end(skb, dh);
3184 err = 0;
3185
3186out:
3187 rcu_read_unlock();
3188 if (err)
3189 return err;
3190 return skb->len;
3191}
3192
3193int drbd_adm_dump_connections_done(struct netlink_callback *cb)
3194{
3195 return put_resource_in_arg0(cb, 6);
3196}
3197
3198enum { SINGLE_RESOURCE, ITERATE_RESOURCES };
3199
3200int drbd_adm_dump_connections(struct sk_buff *skb, struct netlink_callback *cb)
3201{
3202 struct nlattr *resource_filter;
3203 struct drbd_resource *resource = NULL, *next_resource;
3204 struct drbd_connection *uninitialized_var(connection);
3205 int err = 0, retcode;
3206 struct drbd_genlmsghdr *dh;
3207 struct connection_info connection_info;
3208 struct connection_statistics connection_statistics;
3209
3210 rcu_read_lock();
3211 resource = (struct drbd_resource *)cb->args[0];
3212 if (!cb->args[0]) {
3213 resource_filter = find_cfg_context_attr(cb->nlh, T_ctx_resource_name);
3214 if (resource_filter) {
3215 retcode = ERR_RES_NOT_KNOWN;
3216 resource = drbd_find_resource(nla_data(resource_filter));
3217 if (!resource)
3218 goto put_result;
3219 cb->args[0] = (long)resource;
3220 cb->args[1] = SINGLE_RESOURCE;
3221 }
3222 }
3223 if (!resource) {
3224 if (list_empty(&drbd_resources))
3225 goto out;
3226 resource = list_first_entry(&drbd_resources, struct drbd_resource, resources);
3227 kref_get(&resource->kref);
3228 cb->args[0] = (long)resource;
3229 cb->args[1] = ITERATE_RESOURCES;
3230 }
3231
3232 next_resource:
3233 rcu_read_unlock();
3234 mutex_lock(&resource->conf_update);
3235 rcu_read_lock();
3236 if (cb->args[2]) {
3237 for_each_connection_rcu(connection, resource)
3238 if (connection == (struct drbd_connection *)cb->args[2])
3239 goto found_connection;
3240 /* connection was probably deleted */
3241 goto no_more_connections;
3242 }
3243 connection = list_entry(&resource->connections, struct drbd_connection, connections);
3244
3245found_connection:
3246 list_for_each_entry_continue_rcu(connection, &resource->connections, connections) {
3247 if (!has_net_conf(connection))
3248 continue;
3249 retcode = NO_ERROR;
3250 goto put_result; /* only one iteration */
3251 }
3252
3253no_more_connections:
3254 if (cb->args[1] == ITERATE_RESOURCES) {
3255 for_each_resource_rcu(next_resource, &drbd_resources) {
3256 if (next_resource == resource)
3257 goto found_resource;
3258 }
3259 /* resource was probably deleted */
3260 }
3261 goto out;
3262
3263found_resource:
3264 list_for_each_entry_continue_rcu(next_resource, &drbd_resources, resources) {
3265 mutex_unlock(&resource->conf_update);
3266 kref_put(&resource->kref, drbd_destroy_resource);
3267 resource = next_resource;
3268 kref_get(&resource->kref);
3269 cb->args[0] = (long)resource;
3270 cb->args[2] = 0;
3271 goto next_resource;
3272 }
3273 goto out; /* no more resources */
3274
3275put_result:
3276 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3277 cb->nlh->nlmsg_seq, &drbd_genl_family,
3278 NLM_F_MULTI, DRBD_ADM_GET_CONNECTIONS);
3279 err = -ENOMEM;
3280 if (!dh)
3281 goto out;
3282 dh->ret_code = retcode;
3283 dh->minor = -1U;
3284 if (retcode == NO_ERROR) {
3285 struct net_conf *net_conf;
3286
3287 err = nla_put_drbd_cfg_context(skb, resource, connection, NULL);
3288 if (err)
3289 goto out;
3290 net_conf = rcu_dereference(connection->net_conf);
3291 if (net_conf) {
3292 err = net_conf_to_skb(skb, net_conf, !capable(CAP_SYS_ADMIN));
3293 if (err)
3294 goto out;
3295 }
3296 connection_to_info(&connection_info, connection);
3297 err = connection_info_to_skb(skb, &connection_info, !capable(CAP_SYS_ADMIN));
3298 if (err)
3299 goto out;
3300 connection_statistics.conn_congested = test_bit(NET_CONGESTED, &connection->flags);
3301 err = connection_statistics_to_skb(skb, &connection_statistics, !capable(CAP_SYS_ADMIN));
3302 if (err)
3303 goto out;
3304 cb->args[2] = (long)connection;
3305 }
3306 genlmsg_end(skb, dh);
3307 err = 0;
3308
3309out:
3310 rcu_read_unlock();
3311 if (resource)
3312 mutex_unlock(&resource->conf_update);
3313 if (err)
3314 return err;
3315 return skb->len;
3316}
3317
3318enum mdf_peer_flag {
3319 MDF_PEER_CONNECTED = 1 << 0,
3320 MDF_PEER_OUTDATED = 1 << 1,
3321 MDF_PEER_FENCING = 1 << 2,
3322 MDF_PEER_FULL_SYNC = 1 << 3,
3323};
3324
3325static void peer_device_to_statistics(struct peer_device_statistics *s,
3326 struct drbd_peer_device *peer_device)
3327{
3328 struct drbd_device *device = peer_device->device;
3329
3330 memset(s, 0, sizeof(*s));
3331 s->peer_dev_received = device->recv_cnt;
3332 s->peer_dev_sent = device->send_cnt;
3333 s->peer_dev_pending = atomic_read(&device->ap_pending_cnt) +
3334 atomic_read(&device->rs_pending_cnt);
3335 s->peer_dev_unacked = atomic_read(&device->unacked_cnt);
3336 s->peer_dev_out_of_sync = drbd_bm_total_weight(device) << (BM_BLOCK_SHIFT - 9);
3337 s->peer_dev_resync_failed = device->rs_failed << (BM_BLOCK_SHIFT - 9);
3338 if (get_ldev(device)) {
3339 struct drbd_md *md = &device->ldev->md;
3340
3341 spin_lock_irq(&md->uuid_lock);
3342 s->peer_dev_bitmap_uuid = md->uuid[UI_BITMAP];
3343 spin_unlock_irq(&md->uuid_lock);
3344 s->peer_dev_flags =
3345 (drbd_md_test_flag(device->ldev, MDF_CONNECTED_IND) ?
3346 MDF_PEER_CONNECTED : 0) +
3347 (drbd_md_test_flag(device->ldev, MDF_CONSISTENT) &&
3348 !drbd_md_test_flag(device->ldev, MDF_WAS_UP_TO_DATE) ?
3349 MDF_PEER_OUTDATED : 0) +
3350 /* FIXME: MDF_PEER_FENCING? */
3351 (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC) ?
3352 MDF_PEER_FULL_SYNC : 0);
3353 put_ldev(device);
3354 }
3355}
3356
3357int drbd_adm_dump_peer_devices_done(struct netlink_callback *cb)
3358{
3359 return put_resource_in_arg0(cb, 9);
3360}
3361
3362int drbd_adm_dump_peer_devices(struct sk_buff *skb, struct netlink_callback *cb)
3363{
3364 struct nlattr *resource_filter;
3365 struct drbd_resource *resource;
3366 struct drbd_device *uninitialized_var(device);
3367 struct drbd_peer_device *peer_device = NULL;
3368 int minor, err, retcode;
3369 struct drbd_genlmsghdr *dh;
3370 struct idr *idr_to_search;
3371
3372 resource = (struct drbd_resource *)cb->args[0];
3373 if (!cb->args[0] && !cb->args[1]) {
3374 resource_filter = find_cfg_context_attr(cb->nlh, T_ctx_resource_name);
3375 if (resource_filter) {
3376 retcode = ERR_RES_NOT_KNOWN;
3377 resource = drbd_find_resource(nla_data(resource_filter));
3378 if (!resource)
3379 goto put_result;
3380 }
3381 cb->args[0] = (long)resource;
3382 }
3383
3384 rcu_read_lock();
3385 minor = cb->args[1];
3386 idr_to_search = resource ? &resource->devices : &drbd_devices;
3387 device = idr_find(idr_to_search, minor);
3388 if (!device) {
3389next_device:
3390 minor++;
3391 cb->args[2] = 0;
3392 device = idr_get_next(idr_to_search, &minor);
3393 if (!device) {
3394 err = 0;
3395 goto out;
3396 }
3397 }
3398 if (cb->args[2]) {
3399 for_each_peer_device(peer_device, device)
3400 if (peer_device == (struct drbd_peer_device *)cb->args[2])
3401 goto found_peer_device;
3402 /* peer device was probably deleted */
3403 goto next_device;
3404 }
3405 /* Make peer_device point to the list head (not the first entry). */
3406 peer_device = list_entry(&device->peer_devices, struct drbd_peer_device, peer_devices);
3407
3408found_peer_device:
3409 list_for_each_entry_continue_rcu(peer_device, &device->peer_devices, peer_devices) {
3410 if (!has_net_conf(peer_device->connection))
3411 continue;
3412 retcode = NO_ERROR;
3413 goto put_result; /* only one iteration */
3414 }
3415 goto next_device;
3416
3417put_result:
3418 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3419 cb->nlh->nlmsg_seq, &drbd_genl_family,
3420 NLM_F_MULTI, DRBD_ADM_GET_PEER_DEVICES);
3421 err = -ENOMEM;
3422 if (!dh)
3423 goto out;
3424 dh->ret_code = retcode;
3425 dh->minor = -1U;
3426 if (retcode == NO_ERROR) {
3427 struct peer_device_info peer_device_info;
3428 struct peer_device_statistics peer_device_statistics;
3429
3430 dh->minor = minor;
3431 err = nla_put_drbd_cfg_context(skb, device->resource, peer_device->connection, device);
3432 if (err)
3433 goto out;
3434 peer_device_to_info(&peer_device_info, peer_device);
3435 err = peer_device_info_to_skb(skb, &peer_device_info, !capable(CAP_SYS_ADMIN));
3436 if (err)
3437 goto out;
3438 peer_device_to_statistics(&peer_device_statistics, peer_device);
3439 err = peer_device_statistics_to_skb(skb, &peer_device_statistics, !capable(CAP_SYS_ADMIN));
3440 if (err)
3441 goto out;
3442 cb->args[1] = minor;
3443 cb->args[2] = (long)peer_device;
3444 }
3445 genlmsg_end(skb, dh);
3446 err = 0;
3447
3448out:
3449 rcu_read_unlock();
3450 if (err)
3451 return err;
3452 return skb->len;
3453}
251b8f8e
AG
3454/*
3455 * Return the connection of @resource if @resource has exactly one connection.
3456 */
3457static struct drbd_connection *the_only_connection(struct drbd_resource *resource)
3458{
3459 struct list_head *connections = &resource->connections;
3460
3461 if (list_empty(connections) || connections->next->next != connections)
3462 return NULL;
3463 return list_first_entry(&resource->connections, struct drbd_connection, connections);
3464}
3465
8ce953aa 3466static int nla_put_status_info(struct sk_buff *skb, struct drbd_device *device,
3b98c0c2 3467 const struct sib_info *sib)
b411b363 3468{
251b8f8e 3469 struct drbd_resource *resource = device->resource;
3b98c0c2
LE
3470 struct state_info *si = NULL; /* for sizeof(si->member); */
3471 struct nlattr *nla;
3472 int got_ldev;
3b98c0c2
LE
3473 int err = 0;
3474 int exclude_sensitive;
3475
3476 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
3477 * to. So we better exclude_sensitive information.
3478 *
3479 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
3480 * in the context of the requesting user process. Exclude sensitive
3481 * information, unless current has superuser.
3482 *
3483 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
3484 * relies on the current implementation of netlink_dump(), which
3485 * executes the dump callback successively from netlink_recvmsg(),
3486 * always in the context of the receiving process */
3487 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
3488
b30ab791 3489 got_ldev = get_ldev(device);
3b98c0c2
LE
3490
3491 /* We need to add connection name and volume number information still.
3492 * Minor number is in drbd_genlmsghdr. */
251b8f8e 3493 if (nla_put_drbd_cfg_context(skb, resource, the_only_connection(resource), device))
3b98c0c2 3494 goto nla_put_failure;
3b98c0c2 3495
eb6bea67 3496 if (res_opts_to_skb(skb, &device->resource->res_opts, exclude_sensitive))
f399002e
LE
3497 goto nla_put_failure;
3498
daeda1cc 3499 rcu_read_lock();
f9eb7bf4
AG
3500 if (got_ldev) {
3501 struct disk_conf *disk_conf;
44ed167d 3502
b30ab791 3503 disk_conf = rcu_dereference(device->ldev->disk_conf);
f9eb7bf4
AG
3504 err = disk_conf_to_skb(skb, disk_conf, exclude_sensitive);
3505 }
3506 if (!err) {
3507 struct net_conf *nc;
3508
a6b32bc3 3509 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
f9eb7bf4
AG
3510 if (nc)
3511 err = net_conf_to_skb(skb, nc, exclude_sensitive);
3512 }
44ed167d
PR
3513 rcu_read_unlock();
3514 if (err)
3515 goto nla_put_failure;
3b98c0c2 3516
3b98c0c2
LE
3517 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
3518 if (!nla)
3519 goto nla_put_failure;
26ec9287 3520 if (nla_put_u32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY) ||
b30ab791
AG
3521 nla_put_u32(skb, T_current_state, device->state.i) ||
3522 nla_put_u64(skb, T_ed_uuid, device->ed_uuid) ||
3523 nla_put_u64(skb, T_capacity, drbd_get_capacity(device->this_bdev)) ||
3524 nla_put_u64(skb, T_send_cnt, device->send_cnt) ||
3525 nla_put_u64(skb, T_recv_cnt, device->recv_cnt) ||
3526 nla_put_u64(skb, T_read_cnt, device->read_cnt) ||
3527 nla_put_u64(skb, T_writ_cnt, device->writ_cnt) ||
3528 nla_put_u64(skb, T_al_writ_cnt, device->al_writ_cnt) ||
3529 nla_put_u64(skb, T_bm_writ_cnt, device->bm_writ_cnt) ||
3530 nla_put_u32(skb, T_ap_bio_cnt, atomic_read(&device->ap_bio_cnt)) ||
3531 nla_put_u32(skb, T_ap_pending_cnt, atomic_read(&device->ap_pending_cnt)) ||
3532 nla_put_u32(skb, T_rs_pending_cnt, atomic_read(&device->rs_pending_cnt)))
26ec9287 3533 goto nla_put_failure;
3b98c0c2
LE
3534
3535 if (got_ldev) {
39a1aa7f 3536 int err;
b411b363 3537
b30ab791
AG
3538 spin_lock_irq(&device->ldev->md.uuid_lock);
3539 err = nla_put(skb, T_uuids, sizeof(si->uuids), device->ldev->md.uuid);
3540 spin_unlock_irq(&device->ldev->md.uuid_lock);
39a1aa7f
PR
3541
3542 if (err)
3543 goto nla_put_failure;
3544
b30ab791
AG
3545 if (nla_put_u32(skb, T_disk_flags, device->ldev->md.flags) ||
3546 nla_put_u64(skb, T_bits_total, drbd_bm_bits(device)) ||
3547 nla_put_u64(skb, T_bits_oos, drbd_bm_total_weight(device)))
26ec9287 3548 goto nla_put_failure;
b30ab791
AG
3549 if (C_SYNC_SOURCE <= device->state.conn &&
3550 C_PAUSED_SYNC_T >= device->state.conn) {
3551 if (nla_put_u64(skb, T_bits_rs_total, device->rs_total) ||
3552 nla_put_u64(skb, T_bits_rs_failed, device->rs_failed))
26ec9287 3553 goto nla_put_failure;
3b98c0c2 3554 }
b411b363 3555 }
b411b363 3556
3b98c0c2
LE
3557 if (sib) {
3558 switch(sib->sib_reason) {
3559 case SIB_SYNC_PROGRESS:
3560 case SIB_GET_STATUS_REPLY:
3561 break;
3562 case SIB_STATE_CHANGE:
26ec9287
AG
3563 if (nla_put_u32(skb, T_prev_state, sib->os.i) ||
3564 nla_put_u32(skb, T_new_state, sib->ns.i))
3565 goto nla_put_failure;
3b98c0c2
LE
3566 break;
3567 case SIB_HELPER_POST:
26ec9287
AG
3568 if (nla_put_u32(skb, T_helper_exit_code,
3569 sib->helper_exit_code))
3570 goto nla_put_failure;
3b98c0c2
LE
3571 /* fall through */
3572 case SIB_HELPER_PRE:
26ec9287
AG
3573 if (nla_put_string(skb, T_helper, sib->helper_name))
3574 goto nla_put_failure;
3b98c0c2
LE
3575 break;
3576 }
b411b363 3577 }
3b98c0c2 3578 nla_nest_end(skb, nla);
b411b363 3579
3b98c0c2
LE
3580 if (0)
3581nla_put_failure:
3582 err = -EMSGSIZE;
3583 if (got_ldev)
b30ab791 3584 put_ldev(device);
3b98c0c2 3585 return err;
b411b363
PR
3586}
3587
3b98c0c2 3588int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
b411b363 3589{
a910b123 3590 struct drbd_config_context adm_ctx;
3b98c0c2
LE
3591 enum drbd_ret_code retcode;
3592 int err;
b411b363 3593
a910b123 3594 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
3595 if (!adm_ctx.reply_skb)
3596 return retcode;
3597 if (retcode != NO_ERROR)
3598 goto out;
b411b363 3599
b30ab791 3600 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.device, NULL);
3b98c0c2
LE
3601 if (err) {
3602 nlmsg_free(adm_ctx.reply_skb);
3603 return err;
b411b363 3604 }
3b98c0c2 3605out:
a910b123 3606 drbd_adm_finish(&adm_ctx, info, retcode);
3b98c0c2 3607 return 0;
b411b363
PR
3608}
3609
4b7a530f 3610static int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
b411b363 3611{
b30ab791 3612 struct drbd_device *device;
3b98c0c2 3613 struct drbd_genlmsghdr *dh;
77c556f6
AG
3614 struct drbd_resource *pos = (struct drbd_resource *)cb->args[0];
3615 struct drbd_resource *resource = NULL;
77c556f6 3616 struct drbd_resource *tmp;
543cc10b
LE
3617 unsigned volume = cb->args[1];
3618
3619 /* Open coded, deferred, iteration:
77c556f6 3620 * for_each_resource_safe(resource, tmp, &drbd_resources) {
251b8f8e
AG
3621 * connection = "first connection of resource or undefined";
3622 * idr_for_each_entry(&resource->devices, device, i) {
543cc10b
LE
3623 * ...
3624 * }
3625 * }
77c556f6 3626 * where resource is cb->args[0];
543cc10b
LE
3627 * and i is cb->args[1];
3628 *
71932efc
LE
3629 * cb->args[2] indicates if we shall loop over all resources,
3630 * or just dump all volumes of a single resource.
3631 *
3b98c0c2
LE
3632 * This may miss entries inserted after this dump started,
3633 * or entries deleted before they are reached.
543cc10b 3634 *
b30ab791 3635 * We need to make sure the device won't disappear while
543cc10b
LE
3636 * we are looking at it, and revalidate our iterators
3637 * on each iteration.
3638 */
b411b363 3639
05a10ec7 3640 /* synchronize with conn_create()/drbd_destroy_connection() */
c141ebda 3641 rcu_read_lock();
543cc10b 3642 /* revalidate iterator position */
77c556f6 3643 for_each_resource_rcu(tmp, &drbd_resources) {
543cc10b
LE
3644 if (pos == NULL) {
3645 /* first iteration */
3646 pos = tmp;
77c556f6 3647 resource = pos;
543cc10b
LE
3648 break;
3649 }
3650 if (tmp == pos) {
77c556f6 3651 resource = pos;
543cc10b
LE
3652 break;
3653 }
b411b363 3654 }
77c556f6
AG
3655 if (resource) {
3656next_resource:
251b8f8e
AG
3657 device = idr_get_next(&resource->devices, &volume);
3658 if (!device) {
77c556f6
AG
3659 /* No more volumes to dump on this resource.
3660 * Advance resource iterator. */
3661 pos = list_entry_rcu(resource->resources.next,
3662 struct drbd_resource, resources);
3663 /* Did we dump any volume of this resource yet? */
543cc10b 3664 if (volume != 0) {
71932efc
LE
3665 /* If we reached the end of the list,
3666 * or only a single resource dump was requested,
3667 * we are done. */
77c556f6 3668 if (&pos->resources == &drbd_resources || cb->args[2])
71932efc 3669 goto out;
543cc10b 3670 volume = 0;
77c556f6
AG
3671 resource = pos;
3672 goto next_resource;
543cc10b
LE
3673 }
3674 }
3675
98683650 3676 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3b98c0c2
LE
3677 cb->nlh->nlmsg_seq, &drbd_genl_family,
3678 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
3679 if (!dh)
543cc10b
LE
3680 goto out;
3681
251b8f8e 3682 if (!device) {
bde89a9e 3683 /* This is a connection without a single volume.
367d675d
LE
3684 * Suprisingly enough, it may have a network
3685 * configuration. */
251b8f8e
AG
3686 struct drbd_connection *connection;
3687
543cc10b
LE
3688 dh->minor = -1U;
3689 dh->ret_code = NO_ERROR;
251b8f8e
AG
3690 connection = the_only_connection(resource);
3691 if (nla_put_drbd_cfg_context(skb, resource, connection, NULL))
367d675d 3692 goto cancel;
251b8f8e
AG
3693 if (connection) {
3694 struct net_conf *nc;
3695
3696 nc = rcu_dereference(connection->net_conf);
3697 if (nc && net_conf_to_skb(skb, nc, 1) != 0)
3698 goto cancel;
3699 }
367d675d 3700 goto done;
543cc10b 3701 }
b411b363 3702
0b0ba1ef 3703 D_ASSERT(device, device->vnr == volume);
251b8f8e 3704 D_ASSERT(device, device->resource == resource);
3b98c0c2 3705
b30ab791 3706 dh->minor = device_to_minor(device);
3b98c0c2
LE
3707 dh->ret_code = NO_ERROR;
3708
b30ab791 3709 if (nla_put_status_info(skb, device, NULL)) {
367d675d 3710cancel:
3b98c0c2 3711 genlmsg_cancel(skb, dh);
543cc10b 3712 goto out;
3b98c0c2 3713 }
367d675d 3714done:
3b98c0c2 3715 genlmsg_end(skb, dh);
bde89a9e 3716 }
b411b363 3717
543cc10b 3718out:
c141ebda 3719 rcu_read_unlock();
543cc10b 3720 /* where to start the next iteration */
bde89a9e 3721 cb->args[0] = (long)pos;
77c556f6 3722 cb->args[1] = (pos == resource) ? volume + 1 : 0;
b411b363 3723
77c556f6 3724 /* No more resources/volumes/minors found results in an empty skb.
543cc10b 3725 * Which will terminate the dump. */
3b98c0c2 3726 return skb->len;
b411b363
PR
3727}
3728
71932efc
LE
3729/*
3730 * Request status of all resources, or of all volumes within a single resource.
3731 *
3732 * This is a dump, as the answer may not fit in a single reply skb otherwise.
3733 * Which means we cannot use the family->attrbuf or other such members, because
3734 * dump is NOT protected by the genl_lock(). During dump, we only have access
3735 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
3736 *
3737 * Once things are setup properly, we call into get_one_status().
b411b363 3738 */
71932efc 3739int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
b411b363 3740{
71932efc
LE
3741 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
3742 struct nlattr *nla;
7c3063cc 3743 const char *resource_name;
4bc76048 3744 struct drbd_resource *resource;
7c3063cc 3745 int maxtype;
71932efc
LE
3746
3747 /* Is this a followup call? */
3748 if (cb->args[0]) {
3749 /* ... of a single resource dump,
3750 * and the resource iterator has been advanced already? */
3751 if (cb->args[2] && cb->args[2] != cb->args[0])
3752 return 0; /* DONE. */
3753 goto dump;
3754 }
3755
3756 /* First call (from netlink_dump_start). We need to figure out
3757 * which resource(s) the user wants us to dump. */
3758 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
3759 nlmsg_attrlen(cb->nlh, hdrlen),
3760 DRBD_NLA_CFG_CONTEXT);
3761
3762 /* No explicit context given. Dump all. */
3763 if (!nla)
3764 goto dump;
7c3063cc
AG
3765 maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
3766 nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
3767 if (IS_ERR(nla))
3768 return PTR_ERR(nla);
71932efc
LE
3769 /* context given, but no name present? */
3770 if (!nla)
3771 return -EINVAL;
7c3063cc 3772 resource_name = nla_data(nla);
4bc76048
AG
3773 if (!*resource_name)
3774 return -ENODEV;
3775 resource = drbd_find_resource(resource_name);
3776 if (!resource)
71932efc
LE
3777 return -ENODEV;
3778
4bc76048 3779 kref_put(&resource->kref, drbd_destroy_resource); /* get_one_status() revalidates the resource */
0ace9dfa 3780
71932efc 3781 /* prime iterators, and set "filter" mode mark:
bde89a9e 3782 * only dump this connection. */
4bc76048 3783 cb->args[0] = (long)resource;
71932efc 3784 /* cb->args[1] = 0; passed in this way. */
4bc76048 3785 cb->args[2] = (long)resource;
71932efc
LE
3786
3787dump:
3788 return get_one_status(skb, cb);
3789}
b411b363 3790
3b98c0c2 3791int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
b411b363 3792{
a910b123 3793 struct drbd_config_context adm_ctx;
3b98c0c2
LE
3794 enum drbd_ret_code retcode;
3795 struct timeout_parms tp;
3796 int err;
b411b363 3797
a910b123 3798 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
3799 if (!adm_ctx.reply_skb)
3800 return retcode;
3801 if (retcode != NO_ERROR)
3802 goto out;
b411b363 3803
3b98c0c2 3804 tp.timeout_type =
b30ab791
AG
3805 adm_ctx.device->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
3806 test_bit(USE_DEGR_WFC_T, &adm_ctx.device->flags) ? UT_DEGRADED :
3b98c0c2 3807 UT_DEFAULT;
b411b363 3808
3b98c0c2
LE
3809 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
3810 if (err) {
3811 nlmsg_free(adm_ctx.reply_skb);
3812 return err;
3813 }
3814out:
a910b123 3815 drbd_adm_finish(&adm_ctx, info, retcode);
3b98c0c2 3816 return 0;
b411b363
PR
3817}
3818
3b98c0c2 3819int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
b411b363 3820{
a910b123 3821 struct drbd_config_context adm_ctx;
b30ab791 3822 struct drbd_device *device;
3b98c0c2 3823 enum drbd_ret_code retcode;
58ffa580 3824 struct start_ov_parms parms;
b411b363 3825
a910b123 3826 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
3827 if (!adm_ctx.reply_skb)
3828 return retcode;
3829 if (retcode != NO_ERROR)
3830 goto out;
873b0d5f 3831
b30ab791 3832 device = adm_ctx.device;
58ffa580
LE
3833
3834 /* resume from last known position, if possible */
b30ab791 3835 parms.ov_start_sector = device->ov_start_sector;
58ffa580 3836 parms.ov_stop_sector = ULLONG_MAX;
3b98c0c2 3837 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
f399002e 3838 int err = start_ov_parms_from_attrs(&parms, info);
3b98c0c2
LE
3839 if (err) {
3840 retcode = ERR_MANDATORY_TAG;
a910b123 3841 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
3b98c0c2
LE
3842 goto out;
3843 }
b411b363 3844 }
9e276872
LE
3845 mutex_lock(&adm_ctx.resource->adm_mutex);
3846
58ffa580 3847 /* w_make_ov_request expects position to be aligned */
b30ab791
AG
3848 device->ov_start_sector = parms.ov_start_sector & ~(BM_SECT_PER_BIT-1);
3849 device->ov_stop_sector = parms.ov_stop_sector;
873b0d5f
LE
3850
3851 /* If there is still bitmap IO pending, e.g. previous resync or verify
3852 * just being finished, wait for it before requesting a new resync. */
b30ab791
AG
3853 drbd_suspend_io(device);
3854 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
3855 retcode = drbd_request_state(device, NS(conn, C_VERIFY_S));
3856 drbd_resume_io(device);
9e276872
LE
3857
3858 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 3859out:
a910b123 3860 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
3861 return 0;
3862}
3863
3864
3b98c0c2 3865int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
b411b363 3866{
a910b123 3867 struct drbd_config_context adm_ctx;
b30ab791 3868 struct drbd_device *device;
3b98c0c2 3869 enum drbd_ret_code retcode;
b411b363
PR
3870 int skip_initial_sync = 0;
3871 int err;
3b98c0c2 3872 struct new_c_uuid_parms args;
b411b363 3873
a910b123 3874 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
3875 if (!adm_ctx.reply_skb)
3876 return retcode;
3877 if (retcode != NO_ERROR)
3878 goto out_nolock;
b411b363 3879
b30ab791 3880 device = adm_ctx.device;
3b98c0c2
LE
3881 memset(&args, 0, sizeof(args));
3882 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
f399002e 3883 err = new_c_uuid_parms_from_attrs(&args, info);
3b98c0c2
LE
3884 if (err) {
3885 retcode = ERR_MANDATORY_TAG;
a910b123 3886 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
3b98c0c2
LE
3887 goto out_nolock;
3888 }
b411b363
PR
3889 }
3890
9e276872 3891 mutex_lock(&adm_ctx.resource->adm_mutex);
b30ab791 3892 mutex_lock(device->state_mutex); /* Protects us against serialized state changes. */
b411b363 3893
b30ab791 3894 if (!get_ldev(device)) {
b411b363
PR
3895 retcode = ERR_NO_DISK;
3896 goto out;
3897 }
3898
3899 /* this is "skip initial sync", assume to be clean */
a6b32bc3
AG
3900 if (device->state.conn == C_CONNECTED &&
3901 first_peer_device(device)->connection->agreed_pro_version >= 90 &&
b30ab791 3902 device->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
d0180171 3903 drbd_info(device, "Preparing to skip initial sync\n");
b411b363 3904 skip_initial_sync = 1;
b30ab791 3905 } else if (device->state.conn != C_STANDALONE) {
b411b363
PR
3906 retcode = ERR_CONNECTED;
3907 goto out_dec;
3908 }
3909
b30ab791
AG
3910 drbd_uuid_set(device, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
3911 drbd_uuid_new_current(device); /* New current, previous to UI_BITMAP */
b411b363
PR
3912
3913 if (args.clear_bm) {
b30ab791 3914 err = drbd_bitmap_io(device, &drbd_bmio_clear_n_write,
20ceb2b2 3915 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
b411b363 3916 if (err) {
d0180171 3917 drbd_err(device, "Writing bitmap failed with %d\n", err);
b411b363
PR
3918 retcode = ERR_IO_MD_DISK;
3919 }
3920 if (skip_initial_sync) {
69a22773 3921 drbd_send_uuids_skip_initial_sync(first_peer_device(device));
b30ab791
AG
3922 _drbd_uuid_set(device, UI_BITMAP, 0);
3923 drbd_print_uuids(device, "cleared bitmap UUID");
0500813f 3924 spin_lock_irq(&device->resource->req_lock);
b30ab791 3925 _drbd_set_state(_NS2(device, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
b411b363 3926 CS_VERBOSE, NULL);
0500813f 3927 spin_unlock_irq(&device->resource->req_lock);
b411b363
PR
3928 }
3929 }
3930
b30ab791 3931 drbd_md_sync(device);
b411b363 3932out_dec:
b30ab791 3933 put_ldev(device);
b411b363 3934out:
b30ab791 3935 mutex_unlock(device->state_mutex);
9e276872 3936 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 3937out_nolock:
a910b123 3938 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
3939 return 0;
3940}
3941
3b98c0c2 3942static enum drbd_ret_code
a910b123 3943drbd_check_resource_name(struct drbd_config_context *adm_ctx)
b411b363 3944{
a910b123 3945 const char *name = adm_ctx->resource_name;
3b98c0c2 3946 if (!name || !name[0]) {
a910b123 3947 drbd_msg_put_info(adm_ctx->reply_skb, "resource name missing");
3b98c0c2 3948 return ERR_MANDATORY_TAG;
b411b363 3949 }
3b98c0c2
LE
3950 /* if we want to use these in sysfs/configfs/debugfs some day,
3951 * we must not allow slashes */
3952 if (strchr(name, '/')) {
a910b123 3953 drbd_msg_put_info(adm_ctx->reply_skb, "invalid resource name");
3b98c0c2 3954 return ERR_INVALID_REQUEST;
b411b363 3955 }
3b98c0c2 3956 return NO_ERROR;
774b3055 3957}
b411b363 3958
a2972846
AG
3959static void resource_to_info(struct resource_info *info,
3960 struct drbd_resource *resource)
3961{
3962 info->res_role = conn_highest_role(first_connection(resource));
3963 info->res_susp = resource->susp;
3964 info->res_susp_nod = resource->susp_nod;
3965 info->res_susp_fen = resource->susp_fen;
3966}
3967
789c1b62 3968int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
b411b363 3969{
a2972846 3970 struct drbd_connection *connection;
a910b123 3971 struct drbd_config_context adm_ctx;
3b98c0c2 3972 enum drbd_ret_code retcode;
afbbfa88
AG
3973 struct res_opts res_opts;
3974 int err;
b411b363 3975
a910b123 3976 retcode = drbd_adm_prepare(&adm_ctx, skb, info, 0);
3b98c0c2
LE
3977 if (!adm_ctx.reply_skb)
3978 return retcode;
3979 if (retcode != NO_ERROR)
3980 goto out;
b411b363 3981
afbbfa88
AG
3982 set_res_opts_defaults(&res_opts);
3983 err = res_opts_from_attrs(&res_opts, info);
3984 if (err && err != -ENOMSG) {
3985 retcode = ERR_MANDATORY_TAG;
a910b123 3986 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
afbbfa88 3987 goto out;
b411b363
PR
3988 }
3989
a910b123 3990 retcode = drbd_check_resource_name(&adm_ctx);
3b98c0c2
LE
3991 if (retcode != NO_ERROR)
3992 goto out;
b411b363 3993
5c661042 3994 if (adm_ctx.resource) {
38f19616
LE
3995 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
3996 retcode = ERR_INVALID_REQUEST;
a910b123 3997 drbd_msg_put_info(adm_ctx.reply_skb, "resource exists");
38f19616
LE
3998 }
3999 /* else: still NO_ERROR */
3b98c0c2 4000 goto out;
b411b363 4001 }
b411b363 4002
9e276872 4003 /* not yet safe for genl_family.parallel_ops */
28bc3b8c 4004 mutex_lock(&resources_mutex);
a2972846 4005 connection = conn_create(adm_ctx.resource_name, &res_opts);
28bc3b8c 4006 mutex_unlock(&resources_mutex);
a2972846
AG
4007
4008 if (connection) {
4009 struct resource_info resource_info;
4010
4011 mutex_lock(&notification_mutex);
4012 resource_to_info(&resource_info, connection->resource);
4013 notify_resource_state(NULL, 0, connection->resource,
4014 &resource_info, NOTIFY_CREATE);
4015 mutex_unlock(&notification_mutex);
4016 } else
4017 retcode = ERR_NOMEM;
4018
3b98c0c2 4019out:
a910b123 4020 drbd_adm_finish(&adm_ctx, info, retcode);
3b98c0c2 4021 return 0;
b411b363
PR
4022}
4023
a2972846
AG
4024static void device_to_info(struct device_info *info,
4025 struct drbd_device *device)
4026{
4027 info->dev_disk_state = device->state.disk;
4028}
4029
4030
05a10ec7 4031int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info)
b411b363 4032{
a910b123 4033 struct drbd_config_context adm_ctx;
3b98c0c2
LE
4034 struct drbd_genlmsghdr *dh = info->userhdr;
4035 enum drbd_ret_code retcode;
b411b363 4036
a910b123 4037 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
3b98c0c2
LE
4038 if (!adm_ctx.reply_skb)
4039 return retcode;
4040 if (retcode != NO_ERROR)
4041 goto out;
b411b363 4042
f2257a56 4043 if (dh->minor > MINORMASK) {
a910b123 4044 drbd_msg_put_info(adm_ctx.reply_skb, "requested minor out of range");
3b98c0c2
LE
4045 retcode = ERR_INVALID_REQUEST;
4046 goto out;
b411b363 4047 }
0c8e36d9 4048 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
a910b123 4049 drbd_msg_put_info(adm_ctx.reply_skb, "requested volume id out of range");
3b98c0c2
LE
4050 retcode = ERR_INVALID_REQUEST;
4051 goto out;
b411b363 4052 }
b411b363 4053
38f19616 4054 /* drbd_adm_prepare made sure already
a6b32bc3 4055 * that first_peer_device(device)->connection and device->vnr match the request. */
b30ab791 4056 if (adm_ctx.device) {
38f19616 4057 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
179e20b8 4058 retcode = ERR_MINOR_OR_VOLUME_EXISTS;
38f19616
LE
4059 /* else: still NO_ERROR */
4060 goto out;
b411b363 4061 }
38f19616 4062
9e276872 4063 mutex_lock(&adm_ctx.resource->adm_mutex);
a910b123 4064 retcode = drbd_create_device(&adm_ctx, dh->minor);
a2972846
AG
4065 if (retcode == NO_ERROR) {
4066 struct drbd_device *device;
4067 struct drbd_peer_device *peer_device;
4068 struct device_info info;
4069 unsigned int peer_devices = 0;
4070 enum drbd_notification_type flags;
4071
4072 device = minor_to_device(dh->minor);
4073 for_each_peer_device(peer_device, device) {
4074 if (!has_net_conf(peer_device->connection))
4075 continue;
4076 peer_devices++;
4077 }
4078
4079 device_to_info(&info, device);
4080 mutex_lock(&notification_mutex);
4081 flags = (peer_devices--) ? NOTIFY_CONTINUES : 0;
4082 notify_device_state(NULL, 0, device, &info, NOTIFY_CREATE | flags);
4083 for_each_peer_device(peer_device, device) {
4084 struct peer_device_info peer_device_info;
4085
4086 if (!has_net_conf(peer_device->connection))
4087 continue;
4088 peer_device_to_info(&peer_device_info, peer_device);
4089 flags = (peer_devices--) ? NOTIFY_CONTINUES : 0;
4090 notify_peer_device_state(NULL, 0, peer_device, &peer_device_info,
4091 NOTIFY_CREATE | flags);
4092 }
4093 mutex_unlock(&notification_mutex);
4094 }
9e276872 4095 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 4096out:
a910b123 4097 drbd_adm_finish(&adm_ctx, info, retcode);
3b98c0c2 4098 return 0;
b411b363
PR
4099}
4100
05a10ec7 4101static enum drbd_ret_code adm_del_minor(struct drbd_device *device)
b411b363 4102{
a2972846
AG
4103 struct drbd_peer_device *peer_device;
4104
b30ab791
AG
4105 if (device->state.disk == D_DISKLESS &&
4106 /* no need to be device->state.conn == C_STANDALONE &&
85f75dd7
LE
4107 * we may want to delete a minor from a live replication group.
4108 */
b30ab791 4109 device->state.role == R_SECONDARY) {
a2972846
AG
4110 struct drbd_connection *connection =
4111 first_connection(device->resource);
4112
b30ab791 4113 _drbd_request_state(device, NS(conn, C_WF_REPORT_PARAMS),
369bea63 4114 CS_VERBOSE + CS_WAIT_COMPLETE);
a2972846
AG
4115
4116 /* If the state engine hasn't stopped the sender thread yet, we
4117 * need to flush the sender work queue before generating the
4118 * DESTROY events here. */
4119 if (get_t_state(&connection->worker) == RUNNING)
4120 drbd_flush_workqueue(&connection->sender_work);
4121
4122 mutex_lock(&notification_mutex);
4123 for_each_peer_device(peer_device, device) {
4124 if (!has_net_conf(peer_device->connection))
4125 continue;
4126 notify_peer_device_state(NULL, 0, peer_device, NULL,
4127 NOTIFY_DESTROY | NOTIFY_CONTINUES);
4128 }
4129 notify_device_state(NULL, 0, device, NULL, NOTIFY_DESTROY);
4130 mutex_unlock(&notification_mutex);
4131
f82795d6 4132 drbd_delete_device(device);
85f75dd7
LE
4133 return NO_ERROR;
4134 } else
4135 return ERR_MINOR_CONFIGURED;
b411b363
PR
4136}
4137
05a10ec7 4138int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info)
b411b363 4139{
a910b123 4140 struct drbd_config_context adm_ctx;
3b98c0c2 4141 enum drbd_ret_code retcode;
b411b363 4142
a910b123 4143 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
4144 if (!adm_ctx.reply_skb)
4145 return retcode;
4146 if (retcode != NO_ERROR)
4147 goto out;
b411b363 4148
9e276872 4149 mutex_lock(&adm_ctx.resource->adm_mutex);
05a10ec7 4150 retcode = adm_del_minor(adm_ctx.device);
9e276872 4151 mutex_unlock(&adm_ctx.resource->adm_mutex);
85f75dd7 4152out:
a910b123 4153 drbd_adm_finish(&adm_ctx, info, retcode);
85f75dd7 4154 return 0;
b411b363
PR
4155}
4156
179e20b8
AG
4157static int adm_del_resource(struct drbd_resource *resource)
4158{
4159 struct drbd_connection *connection;
4160
4161 for_each_connection(connection, resource) {
4162 if (connection->cstate > C_STANDALONE)
4163 return ERR_NET_CONFIGURED;
4164 }
4165 if (!idr_is_empty(&resource->devices))
4166 return ERR_RES_IN_USE;
4167
a2972846
AG
4168 /* The state engine has stopped the sender thread, so we don't
4169 * need to flush the sender work queue before generating the
4170 * DESTROY event here. */
4171 mutex_lock(&notification_mutex);
4172 notify_resource_state(NULL, 0, resource, NULL, NOTIFY_DESTROY);
4173 mutex_unlock(&notification_mutex);
4174
28bc3b8c 4175 mutex_lock(&resources_mutex);
179e20b8 4176 list_del_rcu(&resource->resources);
28bc3b8c 4177 mutex_unlock(&resources_mutex);
179e20b8
AG
4178 /* Make sure all threads have actually stopped: state handling only
4179 * does drbd_thread_stop_nowait(). */
4180 list_for_each_entry(connection, &resource->connections, connections)
4181 drbd_thread_stop(&connection->worker);
4182 synchronize_rcu();
4183 drbd_free_resource(resource);
4184 return NO_ERROR;
4185}
4186
85f75dd7 4187int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
b411b363 4188{
a910b123 4189 struct drbd_config_context adm_ctx;
b6f85ef9
AG
4190 struct drbd_resource *resource;
4191 struct drbd_connection *connection;
4192 struct drbd_device *device;
f3dfa40a 4193 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
85f75dd7 4194 unsigned i;
b411b363 4195
a910b123 4196 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
85f75dd7
LE
4197 if (!adm_ctx.reply_skb)
4198 return retcode;
4199 if (retcode != NO_ERROR)
9e276872 4200 goto finish;
b411b363 4201
b6f85ef9 4202 resource = adm_ctx.resource;
9e276872 4203 mutex_lock(&resource->adm_mutex);
85f75dd7 4204 /* demote */
b6f85ef9
AG
4205 for_each_connection(connection, resource) {
4206 struct drbd_peer_device *peer_device;
4207
4208 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
4209 retcode = drbd_set_role(peer_device->device, R_SECONDARY, 0);
4210 if (retcode < SS_SUCCESS) {
a910b123 4211 drbd_msg_put_info(adm_ctx.reply_skb, "failed to demote");
b6f85ef9
AG
4212 goto out;
4213 }
4214 }
4215
4216 retcode = conn_try_disconnect(connection, 0);
85f75dd7 4217 if (retcode < SS_SUCCESS) {
a910b123 4218 drbd_msg_put_info(adm_ctx.reply_skb, "failed to disconnect");
c141ebda 4219 goto out;
85f75dd7 4220 }
b411b363 4221 }
b411b363 4222
85f75dd7 4223 /* detach */
b6f85ef9
AG
4224 idr_for_each_entry(&resource->devices, device, i) {
4225 retcode = adm_detach(device, 0);
27012382 4226 if (retcode < SS_SUCCESS || retcode > NO_ERROR) {
a910b123 4227 drbd_msg_put_info(adm_ctx.reply_skb, "failed to detach");
c141ebda 4228 goto out;
85f75dd7
LE
4229 }
4230 }
b411b363 4231
85f75dd7 4232 /* delete volumes */
b6f85ef9
AG
4233 idr_for_each_entry(&resource->devices, device, i) {
4234 retcode = adm_del_minor(device);
85f75dd7
LE
4235 if (retcode != NO_ERROR) {
4236 /* "can not happen" */
a910b123 4237 drbd_msg_put_info(adm_ctx.reply_skb, "failed to delete volume");
ef356262 4238 goto out;
85f75dd7
LE
4239 }
4240 }
b411b363 4241
179e20b8 4242 retcode = adm_del_resource(resource);
3b98c0c2 4243out:
9e276872
LE
4244 mutex_unlock(&resource->adm_mutex);
4245finish:
a910b123 4246 drbd_adm_finish(&adm_ctx, info, retcode);
3b98c0c2 4247 return 0;
b411b363
PR
4248}
4249
789c1b62 4250int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
b411b363 4251{
a910b123 4252 struct drbd_config_context adm_ctx;
77c556f6 4253 struct drbd_resource *resource;
3b98c0c2 4254 enum drbd_ret_code retcode;
b411b363 4255
a910b123 4256 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
3b98c0c2
LE
4257 if (!adm_ctx.reply_skb)
4258 return retcode;
4259 if (retcode != NO_ERROR)
9e276872 4260 goto finish;
77c556f6 4261 resource = adm_ctx.resource;
b411b363 4262
179e20b8
AG
4263 mutex_lock(&resource->adm_mutex);
4264 retcode = adm_del_resource(resource);
9e276872
LE
4265 mutex_unlock(&resource->adm_mutex);
4266finish:
a910b123 4267 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
4268 return 0;
4269}
4270
b30ab791 4271void drbd_bcast_event(struct drbd_device *device, const struct sib_info *sib)
b411b363 4272{
3b98c0c2
LE
4273 struct sk_buff *msg;
4274 struct drbd_genlmsghdr *d_out;
4275 unsigned seq;
4276 int err = -ENOMEM;
4277
4278 seq = atomic_inc_return(&drbd_genl_seq);
4279 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4280 if (!msg)
4281 goto failed;
4282
4283 err = -EMSGSIZE;
4284 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
4285 if (!d_out) /* cannot happen, but anyways. */
4286 goto nla_put_failure;
b30ab791 4287 d_out->minor = device_to_minor(device);
6f9b5f84 4288 d_out->ret_code = NO_ERROR;
3b98c0c2 4289
b30ab791 4290 if (nla_put_status_info(msg, device, sib))
3b98c0c2
LE
4291 goto nla_put_failure;
4292 genlmsg_end(msg, d_out);
4293 err = drbd_genl_multicast_events(msg, 0);
4294 /* msg has been consumed or freed in netlink_broadcast() */
4295 if (err && err != -ESRCH)
4296 goto failed;
b411b363 4297
3b98c0c2 4298 return;
b411b363 4299
3b98c0c2
LE
4300nla_put_failure:
4301 nlmsg_free(msg);
4302failed:
d0180171 4303 drbd_err(device, "Error %d while broadcasting event. "
3b98c0c2
LE
4304 "Event seq:%u sib_reason:%u\n",
4305 err, seq, sib->sib_reason);
b411b363 4306}
a2972846 4307
a2972846
AG
4308static int nla_put_notification_header(struct sk_buff *msg,
4309 enum drbd_notification_type type)
4310{
4311 struct drbd_notification_header nh = {
4312 .nh_type = type,
4313 };
4314
4315 return drbd_notification_header_to_skb(msg, &nh, true);
4316}
4317
4318void notify_resource_state(struct sk_buff *skb,
4319 unsigned int seq,
4320 struct drbd_resource *resource,
4321 struct resource_info *resource_info,
4322 enum drbd_notification_type type)
4323{
4324 struct resource_statistics resource_statistics;
4325 struct drbd_genlmsghdr *dh;
4326 bool multicast = false;
4327 int err;
4328
4329 if (!skb) {
4330 seq = atomic_inc_return(&notify_genl_seq);
4331 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4332 err = -ENOMEM;
4333 if (!skb)
4334 goto failed;
4335 multicast = true;
4336 }
4337
4338 err = -EMSGSIZE;
4339 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_RESOURCE_STATE);
4340 if (!dh)
4341 goto nla_put_failure;
4342 dh->minor = -1U;
4343 dh->ret_code = NO_ERROR;
4344 if (nla_put_drbd_cfg_context(skb, resource, NULL, NULL) ||
4345 nla_put_notification_header(skb, type) ||
4346 ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY &&
4347 resource_info_to_skb(skb, resource_info, true)))
4348 goto nla_put_failure;
4349 resource_statistics.res_stat_write_ordering = resource->write_ordering;
4350 err = resource_statistics_to_skb(skb, &resource_statistics, !capable(CAP_SYS_ADMIN));
4351 if (err)
4352 goto nla_put_failure;
4353 genlmsg_end(skb, dh);
4354 if (multicast) {
4355 err = drbd_genl_multicast_events(skb, 0);
4356 /* skb has been consumed or freed in netlink_broadcast() */
4357 if (err && err != -ESRCH)
4358 goto failed;
4359 }
4360 return;
4361
4362nla_put_failure:
4363 nlmsg_free(skb);
4364failed:
4365 drbd_err(resource, "Error %d while broadcasting event. Event seq:%u\n",
4366 err, seq);
4367}
4368
4369void notify_device_state(struct sk_buff *skb,
4370 unsigned int seq,
4371 struct drbd_device *device,
4372 struct device_info *device_info,
4373 enum drbd_notification_type type)
4374{
4375 struct device_statistics device_statistics;
4376 struct drbd_genlmsghdr *dh;
4377 bool multicast = false;
4378 int err;
4379
4380 if (!skb) {
4381 seq = atomic_inc_return(&notify_genl_seq);
4382 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4383 err = -ENOMEM;
4384 if (!skb)
4385 goto failed;
4386 multicast = true;
4387 }
4388
4389 err = -EMSGSIZE;
4390 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_DEVICE_STATE);
4391 if (!dh)
4392 goto nla_put_failure;
4393 dh->minor = device->minor;
4394 dh->ret_code = NO_ERROR;
4395 if (nla_put_drbd_cfg_context(skb, device->resource, NULL, device) ||
4396 nla_put_notification_header(skb, type) ||
4397 ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY &&
4398 device_info_to_skb(skb, device_info, true)))
4399 goto nla_put_failure;
4400 device_to_statistics(&device_statistics, device);
4401 device_statistics_to_skb(skb, &device_statistics, !capable(CAP_SYS_ADMIN));
4402 genlmsg_end(skb, dh);
4403 if (multicast) {
4404 err = drbd_genl_multicast_events(skb, 0);
4405 /* skb has been consumed or freed in netlink_broadcast() */
4406 if (err && err != -ESRCH)
4407 goto failed;
4408 }
4409 return;
4410
4411nla_put_failure:
4412 nlmsg_free(skb);
4413failed:
4414 drbd_err(device, "Error %d while broadcasting event. Event seq:%u\n",
4415 err, seq);
4416}
4417
4418void notify_connection_state(struct sk_buff *skb,
4419 unsigned int seq,
4420 struct drbd_connection *connection,
4421 struct connection_info *connection_info,
4422 enum drbd_notification_type type)
4423{
4424 struct connection_statistics connection_statistics;
4425 struct drbd_genlmsghdr *dh;
4426 bool multicast = false;
4427 int err;
4428
4429 if (!skb) {
4430 seq = atomic_inc_return(&notify_genl_seq);
4431 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4432 err = -ENOMEM;
4433 if (!skb)
4434 goto failed;
4435 multicast = true;
4436 }
4437
4438 err = -EMSGSIZE;
4439 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_CONNECTION_STATE);
4440 if (!dh)
4441 goto nla_put_failure;
4442 dh->minor = -1U;
4443 dh->ret_code = NO_ERROR;
4444 if (nla_put_drbd_cfg_context(skb, connection->resource, connection, NULL) ||
4445 nla_put_notification_header(skb, type) ||
4446 ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY &&
4447 connection_info_to_skb(skb, connection_info, true)))
4448 goto nla_put_failure;
4449 connection_statistics.conn_congested = test_bit(NET_CONGESTED, &connection->flags);
4450 connection_statistics_to_skb(skb, &connection_statistics, !capable(CAP_SYS_ADMIN));
4451 genlmsg_end(skb, dh);
4452 if (multicast) {
4453 err = drbd_genl_multicast_events(skb, 0);
4454 /* skb has been consumed or freed in netlink_broadcast() */
4455 if (err && err != -ESRCH)
4456 goto failed;
4457 }
4458 return;
4459
4460nla_put_failure:
4461 nlmsg_free(skb);
4462failed:
4463 drbd_err(connection, "Error %d while broadcasting event. Event seq:%u\n",
4464 err, seq);
4465}
4466
4467void notify_peer_device_state(struct sk_buff *skb,
4468 unsigned int seq,
4469 struct drbd_peer_device *peer_device,
4470 struct peer_device_info *peer_device_info,
4471 enum drbd_notification_type type)
4472{
4473 struct peer_device_statistics peer_device_statistics;
4474 struct drbd_resource *resource = peer_device->device->resource;
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_PEER_DEVICE_STATE);
4490 if (!dh)
4491 goto nla_put_failure;
4492 dh->minor = -1U;
4493 dh->ret_code = NO_ERROR;
4494 if (nla_put_drbd_cfg_context(skb, resource, peer_device->connection, peer_device->device) ||
4495 nla_put_notification_header(skb, type) ||
4496 ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY &&
4497 peer_device_info_to_skb(skb, peer_device_info, true)))
4498 goto nla_put_failure;
4499 peer_device_to_statistics(&peer_device_statistics, peer_device);
4500 peer_device_statistics_to_skb(skb, &peer_device_statistics, !capable(CAP_SYS_ADMIN));
4501 genlmsg_end(skb, dh);
4502 if (multicast) {
4503 err = drbd_genl_multicast_events(skb, 0);
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(peer_device, "Error %d while broadcasting event. Event seq:%u\n",
4514 err, seq);
4515}
4516
4517void notify_helper(enum drbd_notification_type type,
4518 struct drbd_device *device, struct drbd_connection *connection,
4519 const char *name, int status)
4520{
4521 struct drbd_resource *resource = device ? device->resource : connection->resource;
4522 struct drbd_helper_info helper_info;
4523 unsigned int seq = atomic_inc_return(&notify_genl_seq);
4524 struct sk_buff *skb = NULL;
4525 struct drbd_genlmsghdr *dh;
4526 int err;
4527
4528 strlcpy(helper_info.helper_name, name, sizeof(helper_info.helper_name));
4529 helper_info.helper_name_len = min(strlen(name), sizeof(helper_info.helper_name));
4530 helper_info.helper_status = status;
4531
4532 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4533 err = -ENOMEM;
4534 if (!skb)
4535 goto fail;
4536
4537 err = -EMSGSIZE;
4538 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_HELPER);
4539 if (!dh)
4540 goto fail;
4541 dh->minor = device ? device->minor : -1;
4542 dh->ret_code = NO_ERROR;
4543 mutex_lock(&notification_mutex);
4544 if (nla_put_drbd_cfg_context(skb, resource, connection, device) ||
4545 nla_put_notification_header(skb, type) ||
4546 drbd_helper_info_to_skb(skb, &helper_info, true))
4547 goto unlock_fail;
4548 genlmsg_end(skb, dh);
4549 err = drbd_genl_multicast_events(skb, 0);
4550 skb = NULL;
4551 /* skb has been consumed or freed in netlink_broadcast() */
4552 if (err && err != -ESRCH)
4553 goto unlock_fail;
4554 mutex_unlock(&notification_mutex);
4555 return;
4556
4557unlock_fail:
4558 mutex_unlock(&notification_mutex);
4559fail:
4560 nlmsg_free(skb);
4561 drbd_err(resource, "Error %d while broadcasting event. Event seq:%u\n",
4562 err, seq);
4563}
4564
4565static void notify_initial_state_done(struct sk_buff *skb, unsigned int seq)
4566{
4567 struct drbd_genlmsghdr *dh;
4568 int err;
4569
4570 err = -EMSGSIZE;
4571 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_INITIAL_STATE_DONE);
4572 if (!dh)
4573 goto nla_put_failure;
4574 dh->minor = -1U;
4575 dh->ret_code = NO_ERROR;
4576 if (nla_put_notification_header(skb, NOTIFY_EXISTS))
4577 goto nla_put_failure;
4578 genlmsg_end(skb, dh);
4579 return;
4580
4581nla_put_failure:
4582 nlmsg_free(skb);
4583 pr_err("Error %d sending event. Event seq:%u\n", err, seq);
4584}
4585
4586static void free_state_changes(struct list_head *list)
4587{
4588 while (!list_empty(list)) {
4589 struct drbd_state_change *state_change =
4590 list_first_entry(list, struct drbd_state_change, list);
4591 list_del(&state_change->list);
4592 forget_state_change(state_change);
4593 }
4594}
4595
4596static unsigned int notifications_for_state_change(struct drbd_state_change *state_change)
4597{
4598 return 1 +
4599 state_change->n_connections +
4600 state_change->n_devices +
4601 state_change->n_devices * state_change->n_connections;
4602}
4603
4604static int get_initial_state(struct sk_buff *skb, struct netlink_callback *cb)
4605{
4606 struct drbd_state_change *state_change = (struct drbd_state_change *)cb->args[0];
4607 unsigned int seq = cb->args[2];
4608 unsigned int n;
4609 enum drbd_notification_type flags = 0;
4610
4611 /* There is no need for taking notification_mutex here: it doesn't
4612 matter if the initial state events mix with later state chage
4613 events; we can always tell the events apart by the NOTIFY_EXISTS
4614 flag. */
4615
4616 cb->args[5]--;
4617 if (cb->args[5] == 1) {
4618 notify_initial_state_done(skb, seq);
4619 goto out;
4620 }
4621 n = cb->args[4]++;
4622 if (cb->args[4] < cb->args[3])
4623 flags |= NOTIFY_CONTINUES;
4624 if (n < 1) {
4625 notify_resource_state_change(skb, seq, state_change->resource,
4626 NOTIFY_EXISTS | flags);
4627 goto next;
4628 }
4629 n--;
4630 if (n < state_change->n_connections) {
4631 notify_connection_state_change(skb, seq, &state_change->connections[n],
4632 NOTIFY_EXISTS | flags);
4633 goto next;
4634 }
4635 n -= state_change->n_connections;
4636 if (n < state_change->n_devices) {
4637 notify_device_state_change(skb, seq, &state_change->devices[n],
4638 NOTIFY_EXISTS | flags);
4639 goto next;
4640 }
4641 n -= state_change->n_devices;
4642 if (n < state_change->n_devices * state_change->n_connections) {
4643 notify_peer_device_state_change(skb, seq, &state_change->peer_devices[n],
4644 NOTIFY_EXISTS | flags);
4645 goto next;
4646 }
4647
4648next:
4649 if (cb->args[4] == cb->args[3]) {
4650 struct drbd_state_change *next_state_change =
4651 list_entry(state_change->list.next,
4652 struct drbd_state_change, list);
4653 cb->args[0] = (long)next_state_change;
4654 cb->args[3] = notifications_for_state_change(next_state_change);
4655 cb->args[4] = 0;
4656 }
4657out:
4658 return skb->len;
4659}
4660
4661int drbd_adm_get_initial_state(struct sk_buff *skb, struct netlink_callback *cb)
4662{
4663 struct drbd_resource *resource;
4664 LIST_HEAD(head);
4665
4666 if (cb->args[5] >= 1) {
4667 if (cb->args[5] > 1)
4668 return get_initial_state(skb, cb);
4669 if (cb->args[0]) {
4670 struct drbd_state_change *state_change =
4671 (struct drbd_state_change *)cb->args[0];
4672
4673 /* connect list to head */
4674 list_add(&head, &state_change->list);
4675 free_state_changes(&head);
4676 }
4677 return 0;
4678 }
4679
4680 cb->args[5] = 2; /* number of iterations */
4681 mutex_lock(&resources_mutex);
4682 for_each_resource(resource, &drbd_resources) {
4683 struct drbd_state_change *state_change;
4684
4685 state_change = remember_old_state(resource, GFP_KERNEL);
4686 if (!state_change) {
4687 if (!list_empty(&head))
4688 free_state_changes(&head);
4689 mutex_unlock(&resources_mutex);
4690 return -ENOMEM;
4691 }
4692 copy_old_to_new_state_change(state_change);
4693 list_add_tail(&state_change->list, &head);
4694 cb->args[5] += notifications_for_state_change(state_change);
4695 }
4696 mutex_unlock(&resources_mutex);
4697
4698 if (!list_empty(&head)) {
4699 struct drbd_state_change *state_change =
4700 list_entry(head.next, struct drbd_state_change, list);
4701 cb->args[0] = (long)state_change;
4702 cb->args[3] = notifications_for_state_change(state_change);
4703 list_del(&head); /* detach list from head */
4704 }
4705
4706 cb->args[2] = cb->nlh->nlmsg_seq;
4707 return get_initial_state(skb, cb);
4708}