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