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