]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/block/drbd/drbd_nl.c
drbd: Improve some function and variable naming
[mirror_ubuntu-zesty-kernel.git] / drivers / block / drbd / drbd_nl.c
CommitLineData
b411b363
PR
1/*
2 drbd_nl.c
3
4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5
6 Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
7 Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8 Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
9
10 drbd is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 drbd is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with drbd; see the file COPYING. If not, write to
22 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 */
25
b411b363
PR
26#include <linux/module.h>
27#include <linux/drbd.h>
28#include <linux/in.h>
29#include <linux/fs.h>
30#include <linux/file.h>
31#include <linux/slab.h>
b411b363
PR
32#include <linux/blkpg.h>
33#include <linux/cpumask.h>
34#include "drbd_int.h"
a3603a6e 35#include "drbd_protocol.h"
265be2d0 36#include "drbd_req.h"
b411b363
PR
37#include "drbd_wrappers.h"
38#include <asm/unaligned.h>
b411b363 39#include <linux/drbd_limits.h>
87f7be4c 40#include <linux/kthread.h>
b411b363 41
3b98c0c2
LE
42#include <net/genetlink.h>
43
44/* .doit */
45// int drbd_adm_create_resource(struct sk_buff *skb, struct genl_info *info);
46// int drbd_adm_delete_resource(struct sk_buff *skb, struct genl_info *info);
47
05a10ec7
AG
48int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info);
49int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info);
3b98c0c2 50
789c1b62
AG
51int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info);
52int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info);
85f75dd7 53int drbd_adm_down(struct sk_buff *skb, struct genl_info *info);
3b98c0c2
LE
54
55int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info);
56int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info);
f399002e 57int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info);
3b98c0c2
LE
58int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info);
59int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info);
f399002e 60int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info);
3b98c0c2
LE
61int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info);
62int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info);
63int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info);
64int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info);
65int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info);
66int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info);
67int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info);
68int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info);
69int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info);
70int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info);
71int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info);
f399002e 72int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info);
3b98c0c2
LE
73int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info);
74int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info);
75/* .dumpit */
76int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb);
77
78#include <linux/drbd_genl_api.h>
01b39b50 79#include "drbd_nla.h"
3b98c0c2
LE
80#include <linux/genl_magic_func.h>
81
82/* used blkdev_get_by_path, to claim our meta data device(s) */
b411b363
PR
83static char *drbd_m_holder = "Hands off! this is DRBD's meta data device.";
84
3b98c0c2
LE
85/* Configuration is strictly serialized, because generic netlink message
86 * processing is strictly serialized by the genl_lock().
87 * Which means we can use one static global drbd_config_context struct.
88 */
89static struct drbd_config_context {
90 /* assigned from drbd_genlmsghdr */
91 unsigned int minor;
92 /* assigned from request attributes, if present */
93 unsigned int volume;
94#define VOLUME_UNSPECIFIED (-1U)
95 /* pointer into the request skb,
96 * limited lifetime! */
7c3063cc 97 char *resource_name;
089c075d
AG
98 struct nlattr *my_addr;
99 struct nlattr *peer_addr;
3b98c0c2
LE
100
101 /* reply buffer */
102 struct sk_buff *reply_skb;
103 /* pointer into reply buffer */
104 struct drbd_genlmsghdr *reply_dh;
105 /* resolved from attributes, if possible */
b30ab791 106 struct drbd_device *device;
bde89a9e 107 struct drbd_connection *connection;
3b98c0c2
LE
108} adm_ctx;
109
110static void drbd_adm_send_reply(struct sk_buff *skb, struct genl_info *info)
111{
112 genlmsg_end(skb, genlmsg_data(nlmsg_data(nlmsg_hdr(skb))));
113 if (genlmsg_reply(skb, info))
114 printk(KERN_ERR "drbd: error sending genl reply\n");
b411b363 115}
3b98c0c2
LE
116
117/* Used on a fresh "drbd_adm_prepare"d reply_skb, this cannot fail: The only
118 * reason it could fail was no space in skb, and there are 4k available. */
8432b314 119int drbd_msg_put_info(const char *info)
3b98c0c2
LE
120{
121 struct sk_buff *skb = adm_ctx.reply_skb;
122 struct nlattr *nla;
123 int err = -EMSGSIZE;
124
125 if (!info || !info[0])
126 return 0;
127
128 nla = nla_nest_start(skb, DRBD_NLA_CFG_REPLY);
129 if (!nla)
130 return err;
131
132 err = nla_put_string(skb, T_info_text, info);
133 if (err) {
134 nla_nest_cancel(skb, nla);
135 return err;
136 } else
137 nla_nest_end(skb, nla);
138 return 0;
b411b363
PR
139}
140
3b98c0c2
LE
141/* This would be a good candidate for a "pre_doit" hook,
142 * and per-family private info->pointers.
143 * But we need to stay compatible with older kernels.
144 * If it returns successfully, adm_ctx members are valid.
145 */
146#define DRBD_ADM_NEED_MINOR 1
44e52cfa 147#define DRBD_ADM_NEED_RESOURCE 2
089c075d 148#define DRBD_ADM_NEED_CONNECTION 4
3b98c0c2
LE
149static int drbd_adm_prepare(struct sk_buff *skb, struct genl_info *info,
150 unsigned flags)
151{
152 struct drbd_genlmsghdr *d_in = info->userhdr;
153 const u8 cmd = info->genlhdr->cmd;
154 int err;
155
156 memset(&adm_ctx, 0, sizeof(adm_ctx));
157
158 /* genl_rcv_msg only checks for CAP_NET_ADMIN on "GENL_ADMIN_PERM" :( */
98683650 159 if (cmd != DRBD_ADM_GET_STATUS && !capable(CAP_NET_ADMIN))
3b98c0c2
LE
160 return -EPERM;
161
162 adm_ctx.reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1e2a2551
AG
163 if (!adm_ctx.reply_skb) {
164 err = -ENOMEM;
3b98c0c2 165 goto fail;
1e2a2551 166 }
3b98c0c2
LE
167
168 adm_ctx.reply_dh = genlmsg_put_reply(adm_ctx.reply_skb,
169 info, &drbd_genl_family, 0, cmd);
170 /* put of a few bytes into a fresh skb of >= 4k will always succeed.
171 * but anyways */
1e2a2551
AG
172 if (!adm_ctx.reply_dh) {
173 err = -ENOMEM;
3b98c0c2 174 goto fail;
1e2a2551 175 }
3b98c0c2
LE
176
177 adm_ctx.reply_dh->minor = d_in->minor;
178 adm_ctx.reply_dh->ret_code = NO_ERROR;
179
089c075d 180 adm_ctx.volume = VOLUME_UNSPECIFIED;
3b98c0c2
LE
181 if (info->attrs[DRBD_NLA_CFG_CONTEXT]) {
182 struct nlattr *nla;
183 /* parse and validate only */
f399002e 184 err = drbd_cfg_context_from_attrs(NULL, info);
3b98c0c2
LE
185 if (err)
186 goto fail;
187
188 /* It was present, and valid,
189 * copy it over to the reply skb. */
190 err = nla_put_nohdr(adm_ctx.reply_skb,
191 info->attrs[DRBD_NLA_CFG_CONTEXT]->nla_len,
192 info->attrs[DRBD_NLA_CFG_CONTEXT]);
193 if (err)
194 goto fail;
195
196 /* and assign stuff to the global adm_ctx */
197 nla = nested_attr_tb[__nla_type(T_ctx_volume)];
089c075d
AG
198 if (nla)
199 adm_ctx.volume = nla_get_u32(nla);
7c3063cc 200 nla = nested_attr_tb[__nla_type(T_ctx_resource_name)];
3b98c0c2 201 if (nla)
7c3063cc 202 adm_ctx.resource_name = nla_data(nla);
089c075d
AG
203 adm_ctx.my_addr = nested_attr_tb[__nla_type(T_ctx_my_addr)];
204 adm_ctx.peer_addr = nested_attr_tb[__nla_type(T_ctx_peer_addr)];
205 if ((adm_ctx.my_addr &&
bde89a9e 206 nla_len(adm_ctx.my_addr) > sizeof(adm_ctx.connection->my_addr)) ||
089c075d 207 (adm_ctx.peer_addr &&
bde89a9e 208 nla_len(adm_ctx.peer_addr) > sizeof(adm_ctx.connection->peer_addr))) {
089c075d
AG
209 err = -EINVAL;
210 goto fail;
211 }
212 }
3b98c0c2
LE
213
214 adm_ctx.minor = d_in->minor;
b30ab791 215 adm_ctx.device = minor_to_device(d_in->minor);
bde89a9e 216 adm_ctx.connection = conn_get_by_name(adm_ctx.resource_name);
3b98c0c2 217
b30ab791 218 if (!adm_ctx.device && (flags & DRBD_ADM_NEED_MINOR)) {
3b98c0c2
LE
219 drbd_msg_put_info("unknown minor");
220 return ERR_MINOR_INVALID;
221 }
bde89a9e 222 if (!adm_ctx.connection && (flags & DRBD_ADM_NEED_RESOURCE)) {
44e52cfa 223 drbd_msg_put_info("unknown resource");
3b98c0c2
LE
224 return ERR_INVALID_REQUEST;
225 }
226
089c075d 227 if (flags & DRBD_ADM_NEED_CONNECTION) {
bde89a9e 228 if (adm_ctx.connection && !(flags & DRBD_ADM_NEED_RESOURCE)) {
089c075d
AG
229 drbd_msg_put_info("no resource name expected");
230 return ERR_INVALID_REQUEST;
231 }
b30ab791 232 if (adm_ctx.device) {
089c075d
AG
233 drbd_msg_put_info("no minor number expected");
234 return ERR_INVALID_REQUEST;
235 }
236 if (adm_ctx.my_addr && adm_ctx.peer_addr)
bde89a9e 237 adm_ctx.connection = conn_get_by_addrs(nla_data(adm_ctx.my_addr),
089c075d
AG
238 nla_len(adm_ctx.my_addr),
239 nla_data(adm_ctx.peer_addr),
240 nla_len(adm_ctx.peer_addr));
bde89a9e 241 if (!adm_ctx.connection) {
089c075d
AG
242 drbd_msg_put_info("unknown connection");
243 return ERR_INVALID_REQUEST;
244 }
245 }
246
3b98c0c2 247 /* some more paranoia, if the request was over-determined */
bde89a9e 248 if (adm_ctx.device && adm_ctx.connection &&
a6b32bc3 249 first_peer_device(adm_ctx.device)->connection != adm_ctx.connection) {
44e52cfa 250 pr_warning("request: minor=%u, resource=%s; but that minor belongs to connection %s\n",
7c3063cc 251 adm_ctx.minor, adm_ctx.resource_name,
a6b32bc3 252 first_peer_device(adm_ctx.device)->connection->name);
44e52cfa 253 drbd_msg_put_info("minor exists in different resource");
527f4b24
LE
254 return ERR_INVALID_REQUEST;
255 }
b30ab791 256 if (adm_ctx.device &&
3b98c0c2 257 adm_ctx.volume != VOLUME_UNSPECIFIED &&
b30ab791 258 adm_ctx.volume != adm_ctx.device->vnr) {
3b98c0c2
LE
259 pr_warning("request: minor=%u, volume=%u; but that minor is volume %u in %s\n",
260 adm_ctx.minor, adm_ctx.volume,
a6b32bc3 261 adm_ctx.device->vnr, first_peer_device(adm_ctx.device)->connection->name);
527f4b24 262 drbd_msg_put_info("minor exists as different volume");
3b98c0c2
LE
263 return ERR_INVALID_REQUEST;
264 }
0ace9dfa 265
3b98c0c2
LE
266 return NO_ERROR;
267
268fail:
269 nlmsg_free(adm_ctx.reply_skb);
270 adm_ctx.reply_skb = NULL;
1e2a2551 271 return err;
3b98c0c2
LE
272}
273
274static int drbd_adm_finish(struct genl_info *info, int retcode)
275{
bde89a9e 276 if (adm_ctx.connection) {
05a10ec7 277 kref_put(&adm_ctx.connection->kref, drbd_destroy_connection);
bde89a9e 278 adm_ctx.connection = NULL;
0ace9dfa
PR
279 }
280
3b98c0c2
LE
281 if (!adm_ctx.reply_skb)
282 return -ENOMEM;
283
284 adm_ctx.reply_dh->ret_code = retcode;
3b98c0c2
LE
285 drbd_adm_send_reply(adm_ctx.reply_skb, info);
286 return 0;
287}
b411b363 288
bde89a9e 289static void setup_khelper_env(struct drbd_connection *connection, char **envp)
b411b363 290{
6b75dced 291 char *afs;
b411b363 292
089c075d 293 /* FIXME: A future version will not allow this case. */
bde89a9e 294 if (connection->my_addr_len == 0 || connection->peer_addr_len == 0)
089c075d
AG
295 return;
296
bde89a9e 297 switch (((struct sockaddr *)&connection->peer_addr)->sa_family) {
089c075d
AG
298 case AF_INET6:
299 afs = "ipv6";
300 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
bde89a9e 301 &((struct sockaddr_in6 *)&connection->peer_addr)->sin6_addr);
b411b363 302 break;
089c075d
AG
303 case AF_INET:
304 afs = "ipv4";
305 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
bde89a9e 306 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
b411b363 307 break;
089c075d
AG
308 default:
309 afs = "ssocks";
310 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
bde89a9e 311 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
b411b363 312 }
089c075d 313 snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
6b75dced 314}
b411b363 315
b30ab791 316int drbd_khelper(struct drbd_device *device, char *cmd)
b411b363
PR
317{
318 char *envp[] = { "HOME=/",
319 "TERM=linux",
320 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
6b75dced
PR
321 (char[20]) { }, /* address family */
322 (char[60]) { }, /* address */
b411b363 323 NULL };
6b75dced 324 char mb[12];
b411b363 325 char *argv[] = {usermode_helper, cmd, mb, NULL };
a6b32bc3 326 struct drbd_connection *connection = first_peer_device(device)->connection;
6b75dced 327 struct sib_info sib;
b411b363
PR
328 int ret;
329
bde89a9e
AG
330 if (current == connection->worker.task)
331 set_bit(CALLBACK_PENDING, &connection->flags);
c2ba686f 332
b30ab791 333 snprintf(mb, 12, "minor-%d", device_to_minor(device));
bde89a9e 334 setup_khelper_env(connection, envp);
b411b363 335
1090c056
LE
336 /* The helper may take some time.
337 * write out any unsynced meta data changes now */
b30ab791 338 drbd_md_sync(device);
1090c056 339
b411b363 340 dev_info(DEV, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
3b98c0c2
LE
341 sib.sib_reason = SIB_HELPER_PRE;
342 sib.helper_name = cmd;
b30ab791 343 drbd_bcast_event(device, &sib);
70834d30 344 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
b411b363
PR
345 if (ret)
346 dev_warn(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
347 usermode_helper, cmd, mb,
348 (ret >> 8) & 0xff, ret);
349 else
350 dev_info(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
351 usermode_helper, cmd, mb,
352 (ret >> 8) & 0xff, ret);
3b98c0c2
LE
353 sib.sib_reason = SIB_HELPER_POST;
354 sib.helper_exit_code = ret;
b30ab791 355 drbd_bcast_event(device, &sib);
b411b363 356
bde89a9e
AG
357 if (current == connection->worker.task)
358 clear_bit(CALLBACK_PENDING, &connection->flags);
b411b363
PR
359
360 if (ret < 0) /* Ignore any ERRNOs we got. */
361 ret = 0;
362
363 return ret;
364}
365
bde89a9e 366static int conn_khelper(struct drbd_connection *connection, char *cmd)
6b75dced
PR
367{
368 char *envp[] = { "HOME=/",
369 "TERM=linux",
370 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
371 (char[20]) { }, /* address family */
372 (char[60]) { }, /* address */
373 NULL };
bde89a9e 374 char *argv[] = {usermode_helper, cmd, connection->name, NULL };
6b75dced
PR
375 int ret;
376
bde89a9e
AG
377 setup_khelper_env(connection, envp);
378 conn_md_sync(connection);
6b75dced 379
bde89a9e 380 conn_info(connection, "helper command: %s %s %s\n", usermode_helper, cmd, connection->name);
6b75dced
PR
381 /* TODO: conn_bcast_event() ?? */
382
98683650 383 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
6b75dced 384 if (ret)
bde89a9e
AG
385 conn_warn(connection, "helper command: %s %s %s exit code %u (0x%x)\n",
386 usermode_helper, cmd, connection->name,
6b75dced
PR
387 (ret >> 8) & 0xff, ret);
388 else
bde89a9e
AG
389 conn_info(connection, "helper command: %s %s %s exit code %u (0x%x)\n",
390 usermode_helper, cmd, connection->name,
6b75dced
PR
391 (ret >> 8) & 0xff, ret);
392 /* TODO: conn_bcast_event() ?? */
c2ba686f 393
b411b363
PR
394 if (ret < 0) /* Ignore any ERRNOs we got. */
395 ret = 0;
396
397 return ret;
398}
399
bde89a9e 400static enum drbd_fencing_p highest_fencing_policy(struct drbd_connection *connection)
b411b363 401{
cb703454 402 enum drbd_fencing_p fp = FP_NOT_AVAIL;
b30ab791 403 struct drbd_device *device;
cb703454
PR
404 int vnr;
405
695d08fa 406 rcu_read_lock();
bde89a9e 407 idr_for_each_entry(&connection->volumes, device, vnr) {
b30ab791 408 if (get_ldev_if_state(device, D_CONSISTENT)) {
daeda1cc 409 fp = max_t(enum drbd_fencing_p, fp,
b30ab791
AG
410 rcu_dereference(device->ldev->disk_conf)->fencing);
411 put_ldev(device);
cb703454
PR
412 }
413 }
695d08fa 414 rcu_read_unlock();
cb703454
PR
415
416 return fp;
417}
418
bde89a9e 419bool conn_try_outdate_peer(struct drbd_connection *connection)
b411b363 420{
28e448bb 421 unsigned int connect_cnt;
cb703454
PR
422 union drbd_state mask = { };
423 union drbd_state val = { };
424 enum drbd_fencing_p fp;
b411b363
PR
425 char *ex_to_string;
426 int r;
b411b363 427
bde89a9e
AG
428 if (connection->cstate >= C_WF_REPORT_PARAMS) {
429 conn_err(connection, "Expected cstate < C_WF_REPORT_PARAMS\n");
cb703454
PR
430 return false;
431 }
b411b363 432
bde89a9e
AG
433 spin_lock_irq(&connection->req_lock);
434 connect_cnt = connection->connect_cnt;
435 spin_unlock_irq(&connection->req_lock);
28e448bb 436
bde89a9e 437 fp = highest_fencing_policy(connection);
cb703454
PR
438 switch (fp) {
439 case FP_NOT_AVAIL:
bde89a9e 440 conn_warn(connection, "Not fencing peer, I'm not even Consistent myself.\n");
fb22c402 441 goto out;
cb703454
PR
442 case FP_DONT_CARE:
443 return true;
444 default: ;
b411b363
PR
445 }
446
bde89a9e 447 r = conn_khelper(connection, "fence-peer");
b411b363
PR
448
449 switch ((r>>8) & 0xff) {
450 case 3: /* peer is inconsistent */
451 ex_to_string = "peer is inconsistent or worse";
cb703454
PR
452 mask.pdsk = D_MASK;
453 val.pdsk = D_INCONSISTENT;
b411b363
PR
454 break;
455 case 4: /* peer got outdated, or was already outdated */
456 ex_to_string = "peer was fenced";
cb703454
PR
457 mask.pdsk = D_MASK;
458 val.pdsk = D_OUTDATED;
b411b363
PR
459 break;
460 case 5: /* peer was down */
bde89a9e 461 if (conn_highest_disk(connection) == D_UP_TO_DATE) {
b411b363
PR
462 /* we will(have) create(d) a new UUID anyways... */
463 ex_to_string = "peer is unreachable, assumed to be dead";
cb703454
PR
464 mask.pdsk = D_MASK;
465 val.pdsk = D_OUTDATED;
b411b363
PR
466 } else {
467 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
b411b363
PR
468 }
469 break;
470 case 6: /* Peer is primary, voluntarily outdate myself.
471 * This is useful when an unconnected R_SECONDARY is asked to
472 * become R_PRIMARY, but finds the other peer being active. */
473 ex_to_string = "peer is active";
bde89a9e 474 conn_warn(connection, "Peer is primary, outdating myself.\n");
cb703454
PR
475 mask.disk = D_MASK;
476 val.disk = D_OUTDATED;
b411b363
PR
477 break;
478 case 7:
479 if (fp != FP_STONITH)
bde89a9e 480 conn_err(connection, "fence-peer() = 7 && fencing != Stonith !!!\n");
b411b363 481 ex_to_string = "peer was stonithed";
cb703454
PR
482 mask.pdsk = D_MASK;
483 val.pdsk = D_OUTDATED;
b411b363
PR
484 break;
485 default:
486 /* The script is broken ... */
bde89a9e 487 conn_err(connection, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
cb703454 488 return false; /* Eventually leave IO frozen */
b411b363
PR
489 }
490
bde89a9e 491 conn_info(connection, "fence-peer helper returned %d (%s)\n",
cb703454 492 (r>>8) & 0xff, ex_to_string);
fb22c402 493
cb703454 494 out:
fb22c402 495
cb703454 496 /* Not using
bde89a9e 497 conn_request_state(connection, mask, val, CS_VERBOSE);
cb703454
PR
498 here, because we might were able to re-establish the connection in the
499 meantime. */
bde89a9e
AG
500 spin_lock_irq(&connection->req_lock);
501 if (connection->cstate < C_WF_REPORT_PARAMS && !test_bit(STATE_SENT, &connection->flags)) {
502 if (connection->connect_cnt != connect_cnt)
28e448bb
PR
503 /* In case the connection was established and droped
504 while the fence-peer handler was running, ignore it */
bde89a9e 505 conn_info(connection, "Ignoring fence-peer exit code\n");
28e448bb 506 else
bde89a9e 507 _conn_request_state(connection, mask, val, CS_VERBOSE);
28e448bb 508 }
bde89a9e 509 spin_unlock_irq(&connection->req_lock);
cb703454 510
bde89a9e 511 return conn_highest_pdsk(connection) <= D_OUTDATED;
b411b363
PR
512}
513
87f7be4c
PR
514static int _try_outdate_peer_async(void *data)
515{
bde89a9e 516 struct drbd_connection *connection = (struct drbd_connection *)data;
87f7be4c 517
bde89a9e 518 conn_try_outdate_peer(connection);
87f7be4c 519
05a10ec7 520 kref_put(&connection->kref, drbd_destroy_connection);
87f7be4c
PR
521 return 0;
522}
523
bde89a9e 524void conn_try_outdate_peer_async(struct drbd_connection *connection)
87f7be4c
PR
525{
526 struct task_struct *opa;
527
bde89a9e
AG
528 kref_get(&connection->kref);
529 opa = kthread_run(_try_outdate_peer_async, connection, "drbd_async_h");
9dc9fbb3 530 if (IS_ERR(opa)) {
bde89a9e 531 conn_err(connection, "out of mem, failed to invoke fence-peer helper\n");
05a10ec7 532 kref_put(&connection->kref, drbd_destroy_connection);
9dc9fbb3 533 }
87f7be4c 534}
b411b363 535
bf885f8a 536enum drbd_state_rv
b30ab791 537drbd_set_role(struct drbd_device *device, enum drbd_role new_role, int force)
b411b363
PR
538{
539 const int max_tries = 4;
bf885f8a 540 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
44ed167d 541 struct net_conf *nc;
b411b363
PR
542 int try = 0;
543 int forced = 0;
544 union drbd_state mask, val;
b411b363
PR
545
546 if (new_role == R_PRIMARY)
a6b32bc3 547 request_ping(first_peer_device(device)->connection); /* Detect a dead peer ASAP */
b411b363 548
b30ab791 549 mutex_lock(device->state_mutex);
b411b363
PR
550
551 mask.i = 0; mask.role = R_MASK;
552 val.i = 0; val.role = new_role;
553
554 while (try++ < max_tries) {
b30ab791 555 rv = _drbd_request_state(device, mask, val, CS_WAIT_COMPLETE);
b411b363
PR
556
557 /* in case we first succeeded to outdate,
558 * but now suddenly could establish a connection */
bf885f8a 559 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
b411b363
PR
560 val.pdsk = 0;
561 mask.pdsk = 0;
562 continue;
563 }
564
bf885f8a 565 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
b30ab791
AG
566 (device->state.disk < D_UP_TO_DATE &&
567 device->state.disk >= D_INCONSISTENT)) {
b411b363
PR
568 mask.disk = D_MASK;
569 val.disk = D_UP_TO_DATE;
570 forced = 1;
571 continue;
572 }
573
bf885f8a 574 if (rv == SS_NO_UP_TO_DATE_DISK &&
b30ab791
AG
575 device->state.disk == D_CONSISTENT && mask.pdsk == 0) {
576 D_ASSERT(device->state.pdsk == D_UNKNOWN);
b411b363 577
a6b32bc3 578 if (conn_try_outdate_peer(first_peer_device(device)->connection)) {
b411b363
PR
579 val.disk = D_UP_TO_DATE;
580 mask.disk = D_MASK;
581 }
b411b363
PR
582 continue;
583 }
584
bf885f8a 585 if (rv == SS_NOTHING_TO_DO)
3b98c0c2 586 goto out;
bf885f8a 587 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
a6b32bc3 588 if (!conn_try_outdate_peer(first_peer_device(device)->connection) && force) {
b411b363 589 dev_warn(DEV, "Forced into split brain situation!\n");
cb703454
PR
590 mask.pdsk = D_MASK;
591 val.pdsk = D_OUTDATED;
b411b363 592
cb703454 593 }
b411b363
PR
594 continue;
595 }
bf885f8a 596 if (rv == SS_TWO_PRIMARIES) {
b411b363
PR
597 /* Maybe the peer is detected as dead very soon...
598 retry at most once more in this case. */
44ed167d
PR
599 int timeo;
600 rcu_read_lock();
a6b32bc3 601 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
44ed167d
PR
602 timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
603 rcu_read_unlock();
604 schedule_timeout_interruptible(timeo);
b411b363
PR
605 if (try < max_tries)
606 try = max_tries - 1;
607 continue;
608 }
bf885f8a 609 if (rv < SS_SUCCESS) {
b30ab791 610 rv = _drbd_request_state(device, mask, val,
b411b363 611 CS_VERBOSE + CS_WAIT_COMPLETE);
bf885f8a 612 if (rv < SS_SUCCESS)
3b98c0c2 613 goto out;
b411b363
PR
614 }
615 break;
616 }
617
bf885f8a 618 if (rv < SS_SUCCESS)
3b98c0c2 619 goto out;
b411b363
PR
620
621 if (forced)
622 dev_warn(DEV, "Forced to consider local data as UpToDate!\n");
623
624 /* Wait until nothing is on the fly :) */
b30ab791 625 wait_event(device->misc_wait, atomic_read(&device->ap_pending_cnt) == 0);
b411b363 626
b6dd1a89
LE
627 /* FIXME also wait for all pending P_BARRIER_ACK? */
628
b411b363 629 if (new_role == R_SECONDARY) {
b30ab791
AG
630 set_disk_ro(device->vdisk, true);
631 if (get_ldev(device)) {
632 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
633 put_ldev(device);
b411b363
PR
634 }
635 } else {
a6b32bc3
AG
636 mutex_lock(&first_peer_device(device)->connection->conf_update);
637 nc = first_peer_device(device)->connection->net_conf;
44ed167d 638 if (nc)
6139f60d 639 nc->discard_my_data = 0; /* without copy; single bit op is atomic */
a6b32bc3 640 mutex_unlock(&first_peer_device(device)->connection->conf_update);
91fd4dad 641
b30ab791
AG
642 set_disk_ro(device->vdisk, false);
643 if (get_ldev(device)) {
644 if (((device->state.conn < C_CONNECTED ||
645 device->state.pdsk <= D_FAILED)
646 && device->ldev->md.uuid[UI_BITMAP] == 0) || forced)
647 drbd_uuid_new_current(device);
b411b363 648
b30ab791
AG
649 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
650 put_ldev(device);
b411b363
PR
651 }
652 }
653
19f843aa
LE
654 /* writeout of activity log covered areas of the bitmap
655 * to stable storage done in after state change already */
b411b363 656
b30ab791 657 if (device->state.conn >= C_WF_REPORT_PARAMS) {
b411b363
PR
658 /* if this was forced, we should consider sync */
659 if (forced)
b30ab791
AG
660 drbd_send_uuids(device);
661 drbd_send_current_state(device);
b411b363
PR
662 }
663
b30ab791 664 drbd_md_sync(device);
b411b363 665
b30ab791 666 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
3b98c0c2 667out:
b30ab791 668 mutex_unlock(device->state_mutex);
bf885f8a 669 return rv;
b411b363
PR
670}
671
3b98c0c2 672static const char *from_attrs_err_to_txt(int err)
ef50a3e3 673{
3b98c0c2
LE
674 return err == -ENOMSG ? "required attribute missing" :
675 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
f399002e 676 err == -EEXIST ? "can not change invariant setting" :
3b98c0c2 677 "invalid attribute value";
ef50a3e3 678}
b411b363 679
3b98c0c2 680int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
b411b363 681{
3b98c0c2
LE
682 struct set_role_parms parms;
683 int err;
684 enum drbd_ret_code retcode;
b411b363 685
3b98c0c2
LE
686 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
687 if (!adm_ctx.reply_skb)
688 return retcode;
689 if (retcode != NO_ERROR)
690 goto out;
b411b363 691
3b98c0c2
LE
692 memset(&parms, 0, sizeof(parms));
693 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
f399002e 694 err = set_role_parms_from_attrs(&parms, info);
3b98c0c2
LE
695 if (err) {
696 retcode = ERR_MANDATORY_TAG;
697 drbd_msg_put_info(from_attrs_err_to_txt(err));
698 goto out;
699 }
700 }
b411b363 701
3b98c0c2 702 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
b30ab791 703 retcode = drbd_set_role(adm_ctx.device, R_PRIMARY, parms.assume_uptodate);
3b98c0c2 704 else
b30ab791 705 retcode = drbd_set_role(adm_ctx.device, R_SECONDARY, 0);
3b98c0c2
LE
706out:
707 drbd_adm_finish(info, retcode);
b411b363
PR
708 return 0;
709}
710
ae8bf312
LE
711/* Initializes the md.*_offset members, so we are able to find
712 * the on disk meta data.
713 *
714 * We currently have two possible layouts:
715 * external:
716 * |----------- md_size_sect ------------------|
717 * [ 4k superblock ][ activity log ][ Bitmap ]
718 * | al_offset == 8 |
719 * | bm_offset = al_offset + X |
720 * ==> bitmap sectors = md_size_sect - bm_offset
721 *
722 * internal:
723 * |----------- md_size_sect ------------------|
724 * [data.....][ Bitmap ][ activity log ][ 4k superblock ]
725 * | al_offset < 0 |
726 * | bm_offset = al_offset - Y |
727 * ==> bitmap sectors = Y = al_offset - bm_offset
728 *
729 * Activity log size used to be fixed 32kB,
730 * but is about to become configurable.
731 */
b30ab791 732static void drbd_md_set_sector_offsets(struct drbd_device *device,
b411b363
PR
733 struct drbd_backing_dev *bdev)
734{
735 sector_t md_size_sect = 0;
c04ccaa6 736 unsigned int al_size_sect = bdev->md.al_size_4k * 8;
daeda1cc 737
3a4d4eb3
LE
738 bdev->md.md_offset = drbd_md_ss(bdev);
739
68e41a43 740 switch (bdev->md.meta_dev_idx) {
b411b363
PR
741 default:
742 /* v07 style fixed size indexed meta data */
ae8bf312 743 bdev->md.md_size_sect = MD_128MB_SECT;
ae8bf312
LE
744 bdev->md.al_offset = MD_4kB_SECT;
745 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
b411b363
PR
746 break;
747 case DRBD_MD_INDEX_FLEX_EXT:
748 /* just occupy the full device; unit: sectors */
749 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
ae8bf312
LE
750 bdev->md.al_offset = MD_4kB_SECT;
751 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
b411b363
PR
752 break;
753 case DRBD_MD_INDEX_INTERNAL:
754 case DRBD_MD_INDEX_FLEX_INT:
b411b363 755 /* al size is still fixed */
ae8bf312 756 bdev->md.al_offset = -al_size_sect;
b411b363
PR
757 /* we need (slightly less than) ~ this much bitmap sectors: */
758 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
759 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
760 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
761 md_size_sect = ALIGN(md_size_sect, 8);
762
763 /* plus the "drbd meta data super block",
764 * and the activity log; */
ae8bf312 765 md_size_sect += MD_4kB_SECT + al_size_sect;
b411b363
PR
766
767 bdev->md.md_size_sect = md_size_sect;
768 /* bitmap offset is adjusted by 'super' block size */
ae8bf312 769 bdev->md.bm_offset = -md_size_sect + MD_4kB_SECT;
b411b363
PR
770 break;
771 }
772}
773
4b0715f0 774/* input size is expected to be in KB */
b411b363
PR
775char *ppsize(char *buf, unsigned long long size)
776{
4b0715f0
LE
777 /* Needs 9 bytes at max including trailing NUL:
778 * -1ULL ==> "16384 EB" */
b411b363
PR
779 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
780 int base = 0;
4b0715f0 781 while (size >= 10000 && base < sizeof(units)-1) {
b411b363
PR
782 /* shift + round */
783 size = (size >> 10) + !!(size & (1<<9));
784 base++;
785 }
4b0715f0 786 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
b411b363
PR
787
788 return buf;
789}
790
791/* there is still a theoretical deadlock when called from receiver
792 * on an D_INCONSISTENT R_PRIMARY:
793 * remote READ does inc_ap_bio, receiver would need to receive answer
794 * packet from remote to dec_ap_bio again.
795 * receiver receive_sizes(), comes here,
796 * waits for ap_bio_cnt == 0. -> deadlock.
797 * but this cannot happen, actually, because:
798 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
799 * (not connected, or bad/no disk on peer):
800 * see drbd_fail_request_early, ap_bio_cnt is zero.
801 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
802 * peer may not initiate a resize.
803 */
3b98c0c2
LE
804/* Note these are not to be confused with
805 * drbd_adm_suspend_io/drbd_adm_resume_io,
806 * which are (sub) state changes triggered by admin (drbdsetup),
807 * and can be long lived.
b30ab791 808 * This changes an device->flag, is triggered by drbd internals,
3b98c0c2 809 * and should be short-lived. */
b30ab791 810void drbd_suspend_io(struct drbd_device *device)
b411b363 811{
b30ab791
AG
812 set_bit(SUSPEND_IO, &device->flags);
813 if (drbd_suspended(device))
265be2d0 814 return;
b30ab791 815 wait_event(device->misc_wait, !atomic_read(&device->ap_bio_cnt));
b411b363
PR
816}
817
b30ab791 818void drbd_resume_io(struct drbd_device *device)
b411b363 819{
b30ab791
AG
820 clear_bit(SUSPEND_IO, &device->flags);
821 wake_up(&device->misc_wait);
b411b363
PR
822}
823
824/**
825 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
b30ab791 826 * @device: DRBD device.
b411b363
PR
827 *
828 * Returns 0 on success, negative return values indicate errors.
829 * You should call drbd_md_sync() after calling this function.
830 */
d752b269 831enum determine_dev_size
b30ab791 832drbd_determine_dev_size(struct drbd_device *device, enum dds_flags flags, struct resize_parms *rs) __must_hold(local)
b411b363
PR
833{
834 sector_t prev_first_sect, prev_size; /* previous meta location */
cccac985 835 sector_t la_size_sect, u_size;
b30ab791 836 struct drbd_md *md = &device->ldev->md;
d752b269
PR
837 u32 prev_al_stripe_size_4k;
838 u32 prev_al_stripes;
b411b363
PR
839 sector_t size;
840 char ppb[10];
d752b269 841 void *buffer;
b411b363
PR
842
843 int md_moved, la_size_changed;
e96c9633 844 enum determine_dev_size rv = DS_UNCHANGED;
b411b363
PR
845
846 /* race:
847 * application request passes inc_ap_bio,
848 * but then cannot get an AL-reference.
849 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
850 *
851 * to avoid that:
852 * Suspend IO right here.
853 * still lock the act_log to not trigger ASSERTs there.
854 */
b30ab791
AG
855 drbd_suspend_io(device);
856 buffer = drbd_md_get_buffer(device); /* Lock meta-data IO */
d752b269 857 if (!buffer) {
b30ab791 858 drbd_resume_io(device);
d752b269
PR
859 return DS_ERROR;
860 }
b411b363
PR
861
862 /* no wait necessary anymore, actually we could assert that */
b30ab791 863 wait_event(device->al_wait, lc_try_lock(device->act_log));
b411b363 864
b30ab791
AG
865 prev_first_sect = drbd_md_first_sector(device->ldev);
866 prev_size = device->ldev->md.md_size_sect;
867 la_size_sect = device->ldev->md.la_size_sect;
b411b363 868
d752b269
PR
869 if (rs) {
870 /* rs is non NULL if we should change the AL layout only */
871
872 prev_al_stripes = md->al_stripes;
873 prev_al_stripe_size_4k = md->al_stripe_size_4k;
874
875 md->al_stripes = rs->al_stripes;
876 md->al_stripe_size_4k = rs->al_stripe_size / 4;
877 md->al_size_4k = (u64)rs->al_stripes * rs->al_stripe_size / 4;
878 }
879
b30ab791 880 drbd_md_set_sector_offsets(device, device->ldev);
b411b363 881
daeda1cc 882 rcu_read_lock();
b30ab791 883 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
daeda1cc 884 rcu_read_unlock();
b30ab791 885 size = drbd_new_dev_size(device, device->ldev, u_size, flags & DDSF_FORCED);
b411b363 886
d752b269
PR
887 if (size < la_size_sect) {
888 if (rs && u_size == 0) {
889 /* Remove "rs &&" later. This check should always be active, but
890 right now the receiver expects the permissive behavior */
891 dev_warn(DEV, "Implicit shrink not allowed. "
892 "Use --size=%llus for explicit shrink.\n",
893 (unsigned long long)size);
894 rv = DS_ERROR_SHRINK;
895 }
896 if (u_size > size)
897 rv = DS_ERROR_SPACE_MD;
898 if (rv != DS_UNCHANGED)
899 goto err_out;
900 }
901
b30ab791
AG
902 if (drbd_get_capacity(device->this_bdev) != size ||
903 drbd_bm_capacity(device) != size) {
b411b363 904 int err;
b30ab791 905 err = drbd_bm_resize(device, size, !(flags & DDSF_NO_RESYNC));
b411b363
PR
906 if (unlikely(err)) {
907 /* currently there is only one error: ENOMEM! */
b30ab791 908 size = drbd_bm_capacity(device)>>1;
b411b363
PR
909 if (size == 0) {
910 dev_err(DEV, "OUT OF MEMORY! "
911 "Could not allocate bitmap!\n");
912 } else {
913 dev_err(DEV, "BM resizing failed. "
914 "Leaving size unchanged at size = %lu KB\n",
915 (unsigned long)size);
916 }
e96c9633 917 rv = DS_ERROR;
b411b363
PR
918 }
919 /* racy, see comments above. */
b30ab791
AG
920 drbd_set_my_capacity(device, size);
921 device->ldev->md.la_size_sect = size;
b411b363
PR
922 dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
923 (unsigned long long)size>>1);
924 }
d752b269
PR
925 if (rv <= DS_ERROR)
926 goto err_out;
b411b363 927
b30ab791 928 la_size_changed = (la_size_sect != device->ldev->md.la_size_sect);
b411b363 929
b30ab791
AG
930 md_moved = prev_first_sect != drbd_md_first_sector(device->ldev)
931 || prev_size != device->ldev->md.md_size_sect;
b411b363 932
d752b269
PR
933 if (la_size_changed || md_moved || rs) {
934 u32 prev_flags;
24dccabb 935
b30ab791 936 drbd_al_shrink(device); /* All extents inactive. */
d752b269
PR
937
938 prev_flags = md->flags;
939 md->flags &= ~MDF_PRIMARY_IND;
b30ab791 940 drbd_md_write(device, buffer);
d752b269 941
b411b363
PR
942 dev_info(DEV, "Writing the whole bitmap, %s\n",
943 la_size_changed && md_moved ? "size changed and md moved" :
944 la_size_changed ? "size changed" : "md moved");
20ceb2b2 945 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
b30ab791 946 drbd_bitmap_io(device, md_moved ? &drbd_bm_write_all : &drbd_bm_write,
d752b269 947 "size changed", BM_LOCKED_MASK);
b30ab791 948 drbd_initialize_al(device, buffer);
d752b269
PR
949
950 md->flags = prev_flags;
b30ab791 951 drbd_md_write(device, buffer);
d752b269
PR
952
953 if (rs)
954 dev_info(DEV, "Changed AL layout to al-stripes = %d, al-stripe-size-kB = %d\n",
955 md->al_stripes, md->al_stripe_size_4k * 4);
b411b363
PR
956 }
957
cccac985 958 if (size > la_size_sect)
57737adc 959 rv = la_size_sect ? DS_GREW : DS_GREW_FROM_ZERO;
cccac985 960 if (size < la_size_sect)
e96c9633 961 rv = DS_SHRUNK;
d752b269
PR
962
963 if (0) {
964 err_out:
965 if (rs) {
966 md->al_stripes = prev_al_stripes;
967 md->al_stripe_size_4k = prev_al_stripe_size_4k;
968 md->al_size_4k = (u64)prev_al_stripes * prev_al_stripe_size_4k;
969
b30ab791 970 drbd_md_set_sector_offsets(device, device->ldev);
d752b269
PR
971 }
972 }
b30ab791
AG
973 lc_unlock(device->act_log);
974 wake_up(&device->al_wait);
975 drbd_md_put_buffer(device);
976 drbd_resume_io(device);
b411b363
PR
977
978 return rv;
979}
980
981sector_t
b30ab791 982drbd_new_dev_size(struct drbd_device *device, struct drbd_backing_dev *bdev,
ef5e44a6 983 sector_t u_size, int assume_peer_has_space)
b411b363 984{
b30ab791 985 sector_t p_size = device->p_size; /* partner's disk size. */
cccac985 986 sector_t la_size_sect = bdev->md.la_size_sect; /* last agreed size. */
b411b363 987 sector_t m_size; /* my size */
b411b363
PR
988 sector_t size = 0;
989
990 m_size = drbd_get_max_capacity(bdev);
991
b30ab791 992 if (device->state.conn < C_CONNECTED && assume_peer_has_space) {
a393db6f
PR
993 dev_warn(DEV, "Resize while not connected was forced by the user!\n");
994 p_size = m_size;
995 }
996
b411b363
PR
997 if (p_size && m_size) {
998 size = min_t(sector_t, p_size, m_size);
999 } else {
cccac985
LE
1000 if (la_size_sect) {
1001 size = la_size_sect;
b411b363
PR
1002 if (m_size && m_size < size)
1003 size = m_size;
1004 if (p_size && p_size < size)
1005 size = p_size;
1006 } else {
1007 if (m_size)
1008 size = m_size;
1009 if (p_size)
1010 size = p_size;
1011 }
1012 }
1013
1014 if (size == 0)
1015 dev_err(DEV, "Both nodes diskless!\n");
1016
1017 if (u_size) {
1018 if (u_size > size)
1019 dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n",
1020 (unsigned long)u_size>>1, (unsigned long)size>>1);
1021 else
1022 size = u_size;
1023 }
1024
1025 return size;
1026}
1027
1028/**
1029 * drbd_check_al_size() - Ensures that the AL is of the right size
b30ab791 1030 * @device: DRBD device.
b411b363
PR
1031 *
1032 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
1033 * failed, and 0 on success. You should call drbd_md_sync() after you called
1034 * this function.
1035 */
b30ab791 1036static int drbd_check_al_size(struct drbd_device *device, struct disk_conf *dc)
b411b363
PR
1037{
1038 struct lru_cache *n, *t;
1039 struct lc_element *e;
1040 unsigned int in_use;
1041 int i;
1042
b30ab791
AG
1043 if (device->act_log &&
1044 device->act_log->nr_elements == dc->al_extents)
b411b363
PR
1045 return 0;
1046
1047 in_use = 0;
b30ab791 1048 t = device->act_log;
7ad651b5 1049 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
f399002e 1050 dc->al_extents, sizeof(struct lc_element), 0);
b411b363
PR
1051
1052 if (n == NULL) {
1053 dev_err(DEV, "Cannot allocate act_log lru!\n");
1054 return -ENOMEM;
1055 }
b30ab791 1056 spin_lock_irq(&device->al_lock);
b411b363
PR
1057 if (t) {
1058 for (i = 0; i < t->nr_elements; i++) {
1059 e = lc_element_by_index(t, i);
1060 if (e->refcnt)
1061 dev_err(DEV, "refcnt(%d)==%d\n",
1062 e->lc_number, e->refcnt);
1063 in_use += e->refcnt;
1064 }
1065 }
1066 if (!in_use)
b30ab791
AG
1067 device->act_log = n;
1068 spin_unlock_irq(&device->al_lock);
b411b363
PR
1069 if (in_use) {
1070 dev_err(DEV, "Activity log still in use!\n");
1071 lc_destroy(n);
1072 return -EBUSY;
1073 } else {
1074 if (t)
1075 lc_destroy(t);
1076 }
b30ab791 1077 drbd_md_mark_dirty(device); /* we changed device->act_log->nr_elemens */
b411b363
PR
1078 return 0;
1079}
1080
b30ab791 1081static void drbd_setup_queue_param(struct drbd_device *device, unsigned int max_bio_size)
b411b363 1082{
b30ab791 1083 struct request_queue * const q = device->rq_queue;
db141b2f
LE
1084 unsigned int max_hw_sectors = max_bio_size >> 9;
1085 unsigned int max_segments = 0;
99432fcc 1086
b30ab791
AG
1087 if (get_ldev_if_state(device, D_ATTACHING)) {
1088 struct request_queue * const b = device->ldev->backing_bdev->bd_disk->queue;
99432fcc
PR
1089
1090 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
daeda1cc 1091 rcu_read_lock();
b30ab791 1092 max_segments = rcu_dereference(device->ldev->disk_conf)->max_bio_bvecs;
daeda1cc 1093 rcu_read_unlock();
b30ab791 1094 put_ldev(device);
99432fcc 1095 }
b411b363 1096
b411b363 1097 blk_queue_logical_block_size(q, 512);
1816a2b4
LE
1098 blk_queue_max_hw_sectors(q, max_hw_sectors);
1099 /* This is the workaround for "bio would need to, but cannot, be split" */
1100 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
1101 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
b411b363 1102
b30ab791
AG
1103 if (get_ldev_if_state(device, D_ATTACHING)) {
1104 struct request_queue * const b = device->ldev->backing_bdev->bd_disk->queue;
99432fcc
PR
1105
1106 blk_queue_stack_limits(q, b);
1107
1108 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
1109 dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
1110 q->backing_dev_info.ra_pages,
1111 b->backing_dev_info.ra_pages);
1112 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1113 }
b30ab791 1114 put_ldev(device);
99432fcc
PR
1115 }
1116}
1117
b30ab791 1118void drbd_reconsider_max_bio_size(struct drbd_device *device)
99432fcc 1119{
db141b2f 1120 unsigned int now, new, local, peer;
99432fcc 1121
b30ab791
AG
1122 now = queue_max_hw_sectors(device->rq_queue) << 9;
1123 local = device->local_max_bio_size; /* Eventually last known value, from volatile memory */
1124 peer = device->peer_max_bio_size; /* Eventually last known value, from meta data */
b411b363 1125
b30ab791
AG
1126 if (get_ldev_if_state(device, D_ATTACHING)) {
1127 local = queue_max_hw_sectors(device->ldev->backing_bdev->bd_disk->queue) << 9;
1128 device->local_max_bio_size = local;
1129 put_ldev(device);
b411b363 1130 }
db141b2f 1131 local = min(local, DRBD_MAX_BIO_SIZE);
99432fcc
PR
1132
1133 /* We may ignore peer limits if the peer is modern enough.
1134 Because new from 8.3.8 onwards the peer can use multiple
1135 BIOs for a single peer_request */
b30ab791 1136 if (device->state.conn >= C_WF_REPORT_PARAMS) {
a6b32bc3 1137 if (first_peer_device(device)->connection->agreed_pro_version < 94)
b30ab791 1138 peer = min(device->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
6809384c 1139 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
a6b32bc3 1140 else if (first_peer_device(device)->connection->agreed_pro_version == 94)
99432fcc 1141 peer = DRBD_MAX_SIZE_H80_PACKET;
a6b32bc3 1142 else if (first_peer_device(device)->connection->agreed_pro_version < 100)
2ffca4f3
PR
1143 peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
1144 else
99432fcc
PR
1145 peer = DRBD_MAX_BIO_SIZE;
1146 }
1147
db141b2f 1148 new = min(local, peer);
99432fcc 1149
b30ab791 1150 if (device->state.role == R_PRIMARY && new < now)
db141b2f 1151 dev_err(DEV, "ASSERT FAILED new < now; (%u < %u)\n", new, now);
99432fcc
PR
1152
1153 if (new != now)
1154 dev_info(DEV, "max BIO size = %u\n", new);
1155
b30ab791 1156 drbd_setup_queue_param(device, new);
b411b363
PR
1157}
1158
a18e9d1e 1159/* Starts the worker thread */
bde89a9e 1160static void conn_reconfig_start(struct drbd_connection *connection)
b411b363 1161{
bde89a9e
AG
1162 drbd_thread_start(&connection->worker);
1163 conn_flush_workqueue(connection);
b411b363
PR
1164}
1165
a18e9d1e 1166/* if still unconfigured, stops worker again. */
bde89a9e 1167static void conn_reconfig_done(struct drbd_connection *connection)
b411b363 1168{
992d6e91 1169 bool stop_threads;
bde89a9e
AG
1170 spin_lock_irq(&connection->req_lock);
1171 stop_threads = conn_all_vols_unconf(connection) &&
1172 connection->cstate == C_STANDALONE;
1173 spin_unlock_irq(&connection->req_lock);
992d6e91
LE
1174 if (stop_threads) {
1175 /* asender is implicitly stopped by receiver
81fa2e67 1176 * in conn_disconnect() */
bde89a9e
AG
1177 drbd_thread_stop(&connection->receiver);
1178 drbd_thread_stop(&connection->worker);
992d6e91 1179 }
b411b363
PR
1180}
1181
0778286a 1182/* Make sure IO is suspended before calling this function(). */
b30ab791 1183static void drbd_suspend_al(struct drbd_device *device)
0778286a
PR
1184{
1185 int s = 0;
1186
b30ab791 1187 if (!lc_try_lock(device->act_log)) {
0778286a
PR
1188 dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n");
1189 return;
1190 }
1191
b30ab791 1192 drbd_al_shrink(device);
a6b32bc3 1193 spin_lock_irq(&first_peer_device(device)->connection->req_lock);
b30ab791
AG
1194 if (device->state.conn < C_CONNECTED)
1195 s = !test_and_set_bit(AL_SUSPENDED, &device->flags);
a6b32bc3 1196 spin_unlock_irq(&first_peer_device(device)->connection->req_lock);
b30ab791 1197 lc_unlock(device->act_log);
0778286a
PR
1198
1199 if (s)
1200 dev_info(DEV, "Suspended AL updates\n");
1201}
1202
5979e361
LE
1203
1204static bool should_set_defaults(struct genl_info *info)
1205{
1206 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1207 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1208}
1209
5bbcf5e6 1210static unsigned int drbd_al_extents_max(struct drbd_backing_dev *bdev)
d589a21e 1211{
5bbcf5e6
LE
1212 /* This is limited by 16 bit "slot" numbers,
1213 * and by available on-disk context storage.
1214 *
1215 * Also (u16)~0 is special (denotes a "free" extent).
1216 *
1217 * One transaction occupies one 4kB on-disk block,
1218 * we have n such blocks in the on disk ring buffer,
1219 * the "current" transaction may fail (n-1),
1220 * and there is 919 slot numbers context information per transaction.
1221 *
1222 * 72 transaction blocks amounts to more than 2**16 context slots,
1223 * so cap there first.
1224 */
1225 const unsigned int max_al_nr = DRBD_AL_EXTENTS_MAX;
1226 const unsigned int sufficient_on_disk =
1227 (max_al_nr + AL_CONTEXT_PER_TRANSACTION -1)
1228 /AL_CONTEXT_PER_TRANSACTION;
d589a21e 1229
5bbcf5e6
LE
1230 unsigned int al_size_4k = bdev->md.al_size_4k;
1231
1232 if (al_size_4k > sufficient_on_disk)
1233 return max_al_nr;
1234
1235 return (al_size_4k - 1) * AL_CONTEXT_PER_TRANSACTION;
d589a21e
PR
1236}
1237
f399002e
LE
1238int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1239{
1240 enum drbd_ret_code retcode;
b30ab791 1241 struct drbd_device *device;
daeda1cc 1242 struct disk_conf *new_disk_conf, *old_disk_conf;
813472ce 1243 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
f399002e 1244 int err, fifo_size;
f399002e
LE
1245
1246 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1247 if (!adm_ctx.reply_skb)
1248 return retcode;
1249 if (retcode != NO_ERROR)
1250 goto out;
1251
b30ab791 1252 device = adm_ctx.device;
f399002e
LE
1253
1254 /* we also need a disk
1255 * to change the options on */
b30ab791 1256 if (!get_ldev(device)) {
f399002e
LE
1257 retcode = ERR_NO_DISK;
1258 goto out;
1259 }
1260
daeda1cc 1261 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
5ecc72c3 1262 if (!new_disk_conf) {
f399002e
LE
1263 retcode = ERR_NOMEM;
1264 goto fail;
1265 }
1266
a6b32bc3 1267 mutex_lock(&first_peer_device(device)->connection->conf_update);
b30ab791 1268 old_disk_conf = device->ldev->disk_conf;
daeda1cc 1269 *new_disk_conf = *old_disk_conf;
5979e361 1270 if (should_set_defaults(info))
b966b5dd 1271 set_disk_conf_defaults(new_disk_conf);
5979e361 1272
5ecc72c3 1273 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
c75b9b10 1274 if (err && err != -ENOMSG) {
f399002e
LE
1275 retcode = ERR_MANDATORY_TAG;
1276 drbd_msg_put_info(from_attrs_err_to_txt(err));
8e229434 1277 goto fail_unlock;
f399002e
LE
1278 }
1279
5ecc72c3
LE
1280 if (!expect(new_disk_conf->resync_rate >= 1))
1281 new_disk_conf->resync_rate = 1;
f399002e 1282
5bbcf5e6
LE
1283 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1284 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
b30ab791
AG
1285 if (new_disk_conf->al_extents > drbd_al_extents_max(device->ldev))
1286 new_disk_conf->al_extents = drbd_al_extents_max(device->ldev);
5bbcf5e6
LE
1287
1288 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1289 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
f399002e 1290
5ecc72c3 1291 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
b30ab791 1292 if (fifo_size != device->rs_plan_s->size) {
813472ce
PR
1293 new_plan = fifo_alloc(fifo_size);
1294 if (!new_plan) {
f399002e
LE
1295 dev_err(DEV, "kmalloc of fifo_buffer failed");
1296 retcode = ERR_NOMEM;
daeda1cc 1297 goto fail_unlock;
f399002e
LE
1298 }
1299 }
1300
b30ab791
AG
1301 drbd_suspend_io(device);
1302 wait_event(device->al_wait, lc_try_lock(device->act_log));
1303 drbd_al_shrink(device);
1304 err = drbd_check_al_size(device, new_disk_conf);
1305 lc_unlock(device->act_log);
1306 wake_up(&device->al_wait);
1307 drbd_resume_io(device);
f399002e
LE
1308
1309 if (err) {
1310 retcode = ERR_NOMEM;
daeda1cc 1311 goto fail_unlock;
f399002e
LE
1312 }
1313
dc97b708 1314 write_lock_irq(&global_state_lock);
b30ab791 1315 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
dc97b708 1316 if (retcode == NO_ERROR) {
b30ab791
AG
1317 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
1318 drbd_resync_after_changed(device);
dc97b708
PR
1319 }
1320 write_unlock_irq(&global_state_lock);
f399002e 1321
daeda1cc
PR
1322 if (retcode != NO_ERROR)
1323 goto fail_unlock;
f399002e 1324
813472ce 1325 if (new_plan) {
b30ab791
AG
1326 old_plan = device->rs_plan_s;
1327 rcu_assign_pointer(device->rs_plan_s, new_plan);
9958c857 1328 }
9958c857 1329
a6b32bc3 1330 mutex_unlock(&first_peer_device(device)->connection->conf_update);
27eb13e9 1331
9a51ab1c 1332 if (new_disk_conf->al_updates)
b30ab791 1333 device->ldev->md.flags &= ~MDF_AL_DISABLED;
9a51ab1c 1334 else
b30ab791 1335 device->ldev->md.flags |= MDF_AL_DISABLED;
9a51ab1c 1336
691631c0 1337 if (new_disk_conf->md_flushes)
b30ab791 1338 clear_bit(MD_NO_FUA, &device->flags);
691631c0 1339 else
b30ab791 1340 set_bit(MD_NO_FUA, &device->flags);
691631c0 1341
a6b32bc3 1342 drbd_bump_write_ordering(first_peer_device(device)->connection, WO_bdev_flush);
27eb13e9 1343
b30ab791 1344 drbd_md_sync(device);
f399002e 1345
b30ab791
AG
1346 if (device->state.conn >= C_CONNECTED)
1347 drbd_send_sync_param(device);
f399002e 1348
daeda1cc
PR
1349 synchronize_rcu();
1350 kfree(old_disk_conf);
813472ce 1351 kfree(old_plan);
b30ab791 1352 mod_timer(&device->request_timer, jiffies + HZ);
daeda1cc
PR
1353 goto success;
1354
1355fail_unlock:
a6b32bc3 1356 mutex_unlock(&first_peer_device(device)->connection->conf_update);
f399002e 1357 fail:
5ecc72c3 1358 kfree(new_disk_conf);
813472ce 1359 kfree(new_plan);
daeda1cc 1360success:
b30ab791 1361 put_ldev(device);
f399002e
LE
1362 out:
1363 drbd_adm_finish(info, retcode);
1364 return 0;
1365}
1366
3b98c0c2 1367int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
b411b363 1368{
b30ab791 1369 struct drbd_device *device;
3b98c0c2 1370 int err;
116676ca 1371 enum drbd_ret_code retcode;
b411b363
PR
1372 enum determine_dev_size dd;
1373 sector_t max_possible_sectors;
1374 sector_t min_md_device_sectors;
1375 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
daeda1cc 1376 struct disk_conf *new_disk_conf = NULL;
e525fd89 1377 struct block_device *bdev;
b411b363 1378 struct lru_cache *resync_lru = NULL;
9958c857 1379 struct fifo_buffer *new_plan = NULL;
b411b363 1380 union drbd_state ns, os;
f2024e7c 1381 enum drbd_state_rv rv;
44ed167d 1382 struct net_conf *nc;
b411b363 1383
3b98c0c2
LE
1384 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1385 if (!adm_ctx.reply_skb)
1386 return retcode;
1387 if (retcode != NO_ERROR)
40cbf085 1388 goto finish;
b411b363 1389
b30ab791 1390 device = adm_ctx.device;
a6b32bc3 1391 conn_reconfig_start(first_peer_device(device)->connection);
b411b363
PR
1392
1393 /* if you want to reconfigure, please tear down first */
b30ab791 1394 if (device->state.disk > D_DISKLESS) {
b411b363
PR
1395 retcode = ERR_DISK_CONFIGURED;
1396 goto fail;
1397 }
82f59cc6
LE
1398 /* It may just now have detached because of IO error. Make sure
1399 * drbd_ldev_destroy is done already, we may end up here very fast,
1400 * e.g. if someone calls attach from the on-io-error handler,
1401 * to realize a "hot spare" feature (not that I'd recommend that) */
b30ab791 1402 wait_event(device->misc_wait, !atomic_read(&device->local_cnt));
b411b363 1403
383606e0 1404 /* make sure there is no leftover from previous force-detach attempts */
b30ab791
AG
1405 clear_bit(FORCE_DETACH, &device->flags);
1406 clear_bit(WAS_IO_ERROR, &device->flags);
1407 clear_bit(WAS_READ_ERROR, &device->flags);
383606e0 1408
0029d624 1409 /* and no leftover from previously aborted resync or verify, either */
b30ab791
AG
1410 device->rs_total = 0;
1411 device->rs_failed = 0;
1412 atomic_set(&device->rs_pending_cnt, 0);
0029d624 1413
3b98c0c2 1414 /* allocation not in the IO path, drbdsetup context */
b411b363
PR
1415 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1416 if (!nbc) {
1417 retcode = ERR_NOMEM;
1418 goto fail;
1419 }
9f2247bb
PR
1420 spin_lock_init(&nbc->md.uuid_lock);
1421
daeda1cc
PR
1422 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
1423 if (!new_disk_conf) {
1424 retcode = ERR_NOMEM;
b411b363
PR
1425 goto fail;
1426 }
daeda1cc 1427 nbc->disk_conf = new_disk_conf;
b411b363 1428
daeda1cc
PR
1429 set_disk_conf_defaults(new_disk_conf);
1430 err = disk_conf_from_attrs(new_disk_conf, info);
3b98c0c2 1431 if (err) {
b411b363 1432 retcode = ERR_MANDATORY_TAG;
3b98c0c2 1433 drbd_msg_put_info(from_attrs_err_to_txt(err));
b411b363
PR
1434 goto fail;
1435 }
1436
5bbcf5e6
LE
1437 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1438 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
d589a21e 1439
9958c857
PR
1440 new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
1441 if (!new_plan) {
1442 retcode = ERR_NOMEM;
1443 goto fail;
1444 }
1445
daeda1cc 1446 if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
b411b363
PR
1447 retcode = ERR_MD_IDX_INVALID;
1448 goto fail;
1449 }
1450
a3f8f7dc 1451 write_lock_irq(&global_state_lock);
b30ab791 1452 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
a3f8f7dc
LE
1453 write_unlock_irq(&global_state_lock);
1454 if (retcode != NO_ERROR)
1455 goto fail;
1456
44ed167d 1457 rcu_read_lock();
a6b32bc3 1458 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
44ed167d 1459 if (nc) {
daeda1cc 1460 if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
44ed167d 1461 rcu_read_unlock();
47ff2d0a
PR
1462 retcode = ERR_STONITH_AND_PROT_A;
1463 goto fail;
1464 }
1465 }
44ed167d 1466 rcu_read_unlock();
47ff2d0a 1467
daeda1cc 1468 bdev = blkdev_get_by_path(new_disk_conf->backing_dev,
b30ab791 1469 FMODE_READ | FMODE_WRITE | FMODE_EXCL, device);
e525fd89 1470 if (IS_ERR(bdev)) {
daeda1cc 1471 dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->backing_dev,
e525fd89 1472 PTR_ERR(bdev));
b411b363
PR
1473 retcode = ERR_OPEN_DISK;
1474 goto fail;
1475 }
e525fd89
TH
1476 nbc->backing_bdev = bdev;
1477
1478 /*
1479 * meta_dev_idx >= 0: external fixed size, possibly multiple
1480 * drbd sharing one meta device. TODO in that case, paranoia
1481 * check that [md_bdev, meta_dev_idx] is not yet used by some
1482 * other drbd minor! (if you use drbd.conf + drbdadm, that
1483 * should check it for you already; but if you don't, or
1484 * someone fooled it, we need to double check here)
1485 */
daeda1cc 1486 bdev = blkdev_get_by_path(new_disk_conf->meta_dev,
d4d77629 1487 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
daeda1cc 1488 (new_disk_conf->meta_dev_idx < 0) ?
b30ab791 1489 (void *)device : (void *)drbd_m_holder);
e525fd89 1490 if (IS_ERR(bdev)) {
daeda1cc 1491 dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->meta_dev,
e525fd89 1492 PTR_ERR(bdev));
b411b363
PR
1493 retcode = ERR_OPEN_MD_DISK;
1494 goto fail;
1495 }
e525fd89 1496 nbc->md_bdev = bdev;
b411b363 1497
e525fd89 1498 if ((nbc->backing_bdev == nbc->md_bdev) !=
daeda1cc
PR
1499 (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1500 new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
e525fd89 1501 retcode = ERR_MD_IDX_INVALID;
b411b363
PR
1502 goto fail;
1503 }
1504
1505 resync_lru = lc_create("resync", drbd_bm_ext_cache,
46a15bc3 1506 1, 61, sizeof(struct bm_extent),
b411b363
PR
1507 offsetof(struct bm_extent, lce));
1508 if (!resync_lru) {
1509 retcode = ERR_NOMEM;
e525fd89 1510 goto fail;
b411b363
PR
1511 }
1512
c04ccaa6
LE
1513 /* Read our meta data super block early.
1514 * This also sets other on-disk offsets. */
b30ab791 1515 retcode = drbd_md_read(device, nbc);
c04ccaa6
LE
1516 if (retcode != NO_ERROR)
1517 goto fail;
b411b363 1518
5bbcf5e6
LE
1519 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1520 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
1521 if (new_disk_conf->al_extents > drbd_al_extents_max(nbc))
1522 new_disk_conf->al_extents = drbd_al_extents_max(nbc);
1523
daeda1cc 1524 if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
b411b363
PR
1525 dev_err(DEV, "max capacity %llu smaller than disk size %llu\n",
1526 (unsigned long long) drbd_get_max_capacity(nbc),
daeda1cc 1527 (unsigned long long) new_disk_conf->disk_size);
7948bcdc 1528 retcode = ERR_DISK_TOO_SMALL;
e525fd89 1529 goto fail;
b411b363
PR
1530 }
1531
daeda1cc 1532 if (new_disk_conf->meta_dev_idx < 0) {
b411b363
PR
1533 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1534 /* at least one MB, otherwise it does not make sense */
1535 min_md_device_sectors = (2<<10);
1536 } else {
1537 max_possible_sectors = DRBD_MAX_SECTORS;
ae8bf312 1538 min_md_device_sectors = MD_128MB_SECT * (new_disk_conf->meta_dev_idx + 1);
b411b363
PR
1539 }
1540
b411b363 1541 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
7948bcdc 1542 retcode = ERR_MD_DISK_TOO_SMALL;
b411b363
PR
1543 dev_warn(DEV, "refusing attach: md-device too small, "
1544 "at least %llu sectors needed for this meta-disk type\n",
1545 (unsigned long long) min_md_device_sectors);
e525fd89 1546 goto fail;
b411b363
PR
1547 }
1548
1549 /* Make sure the new disk is big enough
1550 * (we may currently be R_PRIMARY with no local disk...) */
1551 if (drbd_get_max_capacity(nbc) <
b30ab791 1552 drbd_get_capacity(device->this_bdev)) {
7948bcdc 1553 retcode = ERR_DISK_TOO_SMALL;
e525fd89 1554 goto fail;
b411b363
PR
1555 }
1556
1557 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1558
1352994b
LE
1559 if (nbc->known_size > max_possible_sectors) {
1560 dev_warn(DEV, "==> truncating very big lower level device "
1561 "to currently maximum possible %llu sectors <==\n",
1562 (unsigned long long) max_possible_sectors);
daeda1cc 1563 if (new_disk_conf->meta_dev_idx >= 0)
1352994b
LE
1564 dev_warn(DEV, "==>> using internal or flexible "
1565 "meta data may help <<==\n");
1566 }
1567
b30ab791 1568 drbd_suspend_io(device);
b411b363 1569 /* also wait for the last barrier ack. */
b6dd1a89
LE
1570 /* FIXME see also https://daiquiri.linbit/cgi-bin/bugzilla/show_bug.cgi?id=171
1571 * We need a way to either ignore barrier acks for barriers sent before a device
1572 * was attached, or a way to wait for all pending barrier acks to come in.
1573 * As barriers are counted per resource,
1574 * we'd need to suspend io on all devices of a resource.
1575 */
b30ab791 1576 wait_event(device->misc_wait, !atomic_read(&device->ap_pending_cnt) || drbd_suspended(device));
b411b363 1577 /* and for any other previously queued work */
b30ab791 1578 drbd_flush_workqueue(device);
b411b363 1579
b30ab791 1580 rv = _drbd_request_state(device, NS(disk, D_ATTACHING), CS_VERBOSE);
f2024e7c 1581 retcode = rv; /* FIXME: Type mismatch. */
b30ab791 1582 drbd_resume_io(device);
f2024e7c 1583 if (rv < SS_SUCCESS)
e525fd89 1584 goto fail;
b411b363 1585
b30ab791 1586 if (!get_ldev_if_state(device, D_ATTACHING))
b411b363
PR
1587 goto force_diskless;
1588
b30ab791
AG
1589 if (!device->bitmap) {
1590 if (drbd_bm_init(device)) {
b411b363
PR
1591 retcode = ERR_NOMEM;
1592 goto force_diskless_dec;
1593 }
1594 }
1595
b30ab791
AG
1596 if (device->state.conn < C_CONNECTED &&
1597 device->state.role == R_PRIMARY &&
1598 (device->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
b411b363 1599 dev_err(DEV, "Can only attach to data with current UUID=%016llX\n",
b30ab791 1600 (unsigned long long)device->ed_uuid);
b411b363
PR
1601 retcode = ERR_DATA_NOT_CURRENT;
1602 goto force_diskless_dec;
1603 }
1604
1605 /* Since we are diskless, fix the activity log first... */
b30ab791 1606 if (drbd_check_al_size(device, new_disk_conf)) {
b411b363
PR
1607 retcode = ERR_NOMEM;
1608 goto force_diskless_dec;
1609 }
1610
1611 /* Prevent shrinking of consistent devices ! */
1612 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
b30ab791 1613 drbd_new_dev_size(device, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
b411b363 1614 dev_warn(DEV, "refusing to truncate a consistent device\n");
7948bcdc 1615 retcode = ERR_DISK_TOO_SMALL;
b411b363
PR
1616 goto force_diskless_dec;
1617 }
1618
b411b363
PR
1619 /* Reset the "barriers don't work" bits here, then force meta data to
1620 * be written, to ensure we determine if barriers are supported. */
e544046a 1621 if (new_disk_conf->md_flushes)
b30ab791 1622 clear_bit(MD_NO_FUA, &device->flags);
b411b363 1623 else
b30ab791 1624 set_bit(MD_NO_FUA, &device->flags);
b411b363
PR
1625
1626 /* Point of no return reached.
1627 * Devices and memory are no longer released by error cleanup below.
b30ab791 1628 * now device takes over responsibility, and the state engine should
b411b363 1629 * clean it up somewhere. */
b30ab791
AG
1630 D_ASSERT(device->ldev == NULL);
1631 device->ldev = nbc;
1632 device->resync = resync_lru;
1633 device->rs_plan_s = new_plan;
b411b363
PR
1634 nbc = NULL;
1635 resync_lru = NULL;
daeda1cc 1636 new_disk_conf = NULL;
9958c857 1637 new_plan = NULL;
b411b363 1638
a6b32bc3 1639 drbd_bump_write_ordering(first_peer_device(device)->connection, WO_bdev_flush);
b411b363 1640
b30ab791
AG
1641 if (drbd_md_test_flag(device->ldev, MDF_CRASHED_PRIMARY))
1642 set_bit(CRASHED_PRIMARY, &device->flags);
b411b363 1643 else
b30ab791 1644 clear_bit(CRASHED_PRIMARY, &device->flags);
b411b363 1645
b30ab791 1646 if (drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
a6b32bc3
AG
1647 !(device->state.role == R_PRIMARY &&
1648 first_peer_device(device)->connection->susp_nod))
b30ab791 1649 set_bit(CRASHED_PRIMARY, &device->flags);
b411b363 1650
b30ab791
AG
1651 device->send_cnt = 0;
1652 device->recv_cnt = 0;
1653 device->read_cnt = 0;
1654 device->writ_cnt = 0;
b411b363 1655
b30ab791 1656 drbd_reconsider_max_bio_size(device);
b411b363
PR
1657
1658 /* If I am currently not R_PRIMARY,
1659 * but meta data primary indicator is set,
1660 * I just now recover from a hard crash,
1661 * and have been R_PRIMARY before that crash.
1662 *
1663 * Now, if I had no connection before that crash
1664 * (have been degraded R_PRIMARY), chances are that
1665 * I won't find my peer now either.
1666 *
1667 * In that case, and _only_ in that case,
1668 * we use the degr-wfc-timeout instead of the default,
1669 * so we can automatically recover from a crash of a
1670 * degraded but active "cluster" after a certain timeout.
1671 */
b30ab791
AG
1672 clear_bit(USE_DEGR_WFC_T, &device->flags);
1673 if (device->state.role != R_PRIMARY &&
1674 drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
1675 !drbd_md_test_flag(device->ldev, MDF_CONNECTED_IND))
1676 set_bit(USE_DEGR_WFC_T, &device->flags);
b411b363 1677
b30ab791 1678 dd = drbd_determine_dev_size(device, 0, NULL);
d752b269 1679 if (dd <= DS_ERROR) {
b411b363
PR
1680 retcode = ERR_NOMEM_BITMAP;
1681 goto force_diskless_dec;
e96c9633 1682 } else if (dd == DS_GREW)
b30ab791 1683 set_bit(RESYNC_AFTER_NEG, &device->flags);
b411b363 1684
b30ab791
AG
1685 if (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC) ||
1686 (test_bit(CRASHED_PRIMARY, &device->flags) &&
1687 drbd_md_test_flag(device->ldev, MDF_AL_DISABLED))) {
b411b363
PR
1688 dev_info(DEV, "Assuming that all blocks are out of sync "
1689 "(aka FullSync)\n");
b30ab791 1690 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
20ceb2b2 1691 "set_n_write from attaching", BM_LOCKED_MASK)) {
b411b363
PR
1692 retcode = ERR_IO_MD_DISK;
1693 goto force_diskless_dec;
1694 }
1695 } else {
b30ab791 1696 if (drbd_bitmap_io(device, &drbd_bm_read,
22ab6a30 1697 "read from attaching", BM_LOCKED_MASK)) {
19f843aa
LE
1698 retcode = ERR_IO_MD_DISK;
1699 goto force_diskless_dec;
1700 }
b411b363
PR
1701 }
1702
b30ab791
AG
1703 if (_drbd_bm_total_weight(device) == drbd_bm_bits(device))
1704 drbd_suspend_al(device); /* IO is still suspended here... */
0778286a 1705
a6b32bc3 1706 spin_lock_irq(&first_peer_device(device)->connection->req_lock);
b30ab791 1707 os = drbd_read_state(device);
78bae59b 1708 ns = os;
b411b363
PR
1709 /* If MDF_CONSISTENT is not set go into inconsistent state,
1710 otherwise investigate MDF_WasUpToDate...
1711 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1712 otherwise into D_CONSISTENT state.
1713 */
b30ab791
AG
1714 if (drbd_md_test_flag(device->ldev, MDF_CONSISTENT)) {
1715 if (drbd_md_test_flag(device->ldev, MDF_WAS_UP_TO_DATE))
b411b363
PR
1716 ns.disk = D_CONSISTENT;
1717 else
1718 ns.disk = D_OUTDATED;
1719 } else {
1720 ns.disk = D_INCONSISTENT;
1721 }
1722
b30ab791 1723 if (drbd_md_test_flag(device->ldev, MDF_PEER_OUT_DATED))
b411b363
PR
1724 ns.pdsk = D_OUTDATED;
1725
daeda1cc
PR
1726 rcu_read_lock();
1727 if (ns.disk == D_CONSISTENT &&
b30ab791 1728 (ns.pdsk == D_OUTDATED || rcu_dereference(device->ldev->disk_conf)->fencing == FP_DONT_CARE))
b411b363
PR
1729 ns.disk = D_UP_TO_DATE;
1730
1731 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1732 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1733 this point, because drbd_request_state() modifies these
1734 flags. */
1735
b30ab791
AG
1736 if (rcu_dereference(device->ldev->disk_conf)->al_updates)
1737 device->ldev->md.flags &= ~MDF_AL_DISABLED;
9a51ab1c 1738 else
b30ab791 1739 device->ldev->md.flags |= MDF_AL_DISABLED;
9a51ab1c
PR
1740
1741 rcu_read_unlock();
1742
b411b363
PR
1743 /* In case we are C_CONNECTED postpone any decision on the new disk
1744 state after the negotiation phase. */
b30ab791
AG
1745 if (device->state.conn == C_CONNECTED) {
1746 device->new_state_tmp.i = ns.i;
b411b363
PR
1747 ns.i = os.i;
1748 ns.disk = D_NEGOTIATING;
dc66c74d
PR
1749
1750 /* We expect to receive up-to-date UUIDs soon.
1751 To avoid a race in receive_state, free p_uuid while
1752 holding req_lock. I.e. atomic with the state change */
b30ab791
AG
1753 kfree(device->p_uuid);
1754 device->p_uuid = NULL;
b411b363
PR
1755 }
1756
b30ab791 1757 rv = _drbd_set_state(device, ns, CS_VERBOSE, NULL);
a6b32bc3 1758 spin_unlock_irq(&first_peer_device(device)->connection->req_lock);
b411b363
PR
1759
1760 if (rv < SS_SUCCESS)
1761 goto force_diskless_dec;
1762
b30ab791 1763 mod_timer(&device->request_timer, jiffies + HZ);
cdfda633 1764
b30ab791
AG
1765 if (device->state.role == R_PRIMARY)
1766 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
b411b363 1767 else
b30ab791 1768 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
b411b363 1769
b30ab791
AG
1770 drbd_md_mark_dirty(device);
1771 drbd_md_sync(device);
b411b363 1772
b30ab791
AG
1773 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
1774 put_ldev(device);
a6b32bc3 1775 conn_reconfig_done(first_peer_device(device)->connection);
3b98c0c2 1776 drbd_adm_finish(info, retcode);
b411b363
PR
1777 return 0;
1778
1779 force_diskless_dec:
b30ab791 1780 put_ldev(device);
b411b363 1781 force_diskless:
b30ab791
AG
1782 drbd_force_state(device, NS(disk, D_DISKLESS));
1783 drbd_md_sync(device);
b411b363 1784 fail:
a6b32bc3 1785 conn_reconfig_done(first_peer_device(device)->connection);
b411b363 1786 if (nbc) {
e525fd89
TH
1787 if (nbc->backing_bdev)
1788 blkdev_put(nbc->backing_bdev,
1789 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1790 if (nbc->md_bdev)
1791 blkdev_put(nbc->md_bdev,
1792 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
b411b363
PR
1793 kfree(nbc);
1794 }
daeda1cc 1795 kfree(new_disk_conf);
b411b363 1796 lc_destroy(resync_lru);
9958c857 1797 kfree(new_plan);
b411b363 1798
40cbf085 1799 finish:
3b98c0c2 1800 drbd_adm_finish(info, retcode);
b411b363
PR
1801 return 0;
1802}
1803
b30ab791 1804static int adm_detach(struct drbd_device *device, int force)
b411b363 1805{
19f83c76 1806 enum drbd_state_rv retcode;
9a0d9d03 1807 int ret;
02ee8f95 1808
cdfda633 1809 if (force) {
b30ab791
AG
1810 set_bit(FORCE_DETACH, &device->flags);
1811 drbd_force_state(device, NS(disk, D_FAILED));
cdfda633 1812 retcode = SS_SUCCESS;
02ee8f95
PR
1813 goto out;
1814 }
1815
b30ab791
AG
1816 drbd_suspend_io(device); /* so no-one is stuck in drbd_al_begin_io */
1817 drbd_md_get_buffer(device); /* make sure there is no in-flight meta-data IO */
1818 retcode = drbd_request_state(device, NS(disk, D_FAILED));
1819 drbd_md_put_buffer(device);
9a0d9d03 1820 /* D_FAILED will transition to DISKLESS. */
b30ab791
AG
1821 ret = wait_event_interruptible(device->misc_wait,
1822 device->state.disk != D_FAILED);
1823 drbd_resume_io(device);
9b2f61ae 1824 if ((int)retcode == (int)SS_IS_DISKLESS)
9a0d9d03
LE
1825 retcode = SS_NOTHING_TO_DO;
1826 if (ret)
1827 retcode = ERR_INTR;
02ee8f95 1828out:
85f75dd7 1829 return retcode;
b411b363
PR
1830}
1831
82f59cc6
LE
1832/* Detaching the disk is a process in multiple stages. First we need to lock
1833 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1834 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1835 * internal references as well.
1836 * Only then we have finally detached. */
3b98c0c2 1837int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
b411b363 1838{
116676ca 1839 enum drbd_ret_code retcode;
cdfda633
PR
1840 struct detach_parms parms = { };
1841 int err;
b411b363 1842
3b98c0c2
LE
1843 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1844 if (!adm_ctx.reply_skb)
1845 return retcode;
1846 if (retcode != NO_ERROR)
1847 goto out;
b411b363 1848
cdfda633
PR
1849 if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
1850 err = detach_parms_from_attrs(&parms, info);
1851 if (err) {
1852 retcode = ERR_MANDATORY_TAG;
1853 drbd_msg_put_info(from_attrs_err_to_txt(err));
1854 goto out;
1855 }
b411b363
PR
1856 }
1857
b30ab791 1858 retcode = adm_detach(adm_ctx.device, parms.force_detach);
3b98c0c2
LE
1859out:
1860 drbd_adm_finish(info, retcode);
b411b363
PR
1861 return 0;
1862}
b411b363 1863
bde89a9e 1864static bool conn_resync_running(struct drbd_connection *connection)
f399002e 1865{
b30ab791 1866 struct drbd_device *device;
695d08fa 1867 bool rv = false;
f399002e
LE
1868 int vnr;
1869
695d08fa 1870 rcu_read_lock();
bde89a9e 1871 idr_for_each_entry(&connection->volumes, device, vnr) {
b30ab791
AG
1872 if (device->state.conn == C_SYNC_SOURCE ||
1873 device->state.conn == C_SYNC_TARGET ||
1874 device->state.conn == C_PAUSED_SYNC_S ||
1875 device->state.conn == C_PAUSED_SYNC_T) {
695d08fa
PR
1876 rv = true;
1877 break;
1878 }
b411b363 1879 }
695d08fa 1880 rcu_read_unlock();
b411b363 1881
695d08fa 1882 return rv;
f399002e 1883}
47ff2d0a 1884
bde89a9e 1885static bool conn_ov_running(struct drbd_connection *connection)
f399002e 1886{
b30ab791 1887 struct drbd_device *device;
695d08fa 1888 bool rv = false;
f399002e
LE
1889 int vnr;
1890
695d08fa 1891 rcu_read_lock();
bde89a9e 1892 idr_for_each_entry(&connection->volumes, device, vnr) {
b30ab791
AG
1893 if (device->state.conn == C_VERIFY_S ||
1894 device->state.conn == C_VERIFY_T) {
695d08fa
PR
1895 rv = true;
1896 break;
47ff2d0a
PR
1897 }
1898 }
695d08fa 1899 rcu_read_unlock();
b411b363 1900
695d08fa 1901 return rv;
f399002e 1902}
422028b1 1903
cd64397c 1904static enum drbd_ret_code
bde89a9e 1905_check_net_options(struct drbd_connection *connection, struct net_conf *old_conf, struct net_conf *new_conf)
cd64397c 1906{
b30ab791 1907 struct drbd_device *device;
cd64397c 1908 int i;
b411b363 1909
bde89a9e 1910 if (old_conf && connection->cstate == C_WF_REPORT_PARAMS && connection->agreed_pro_version < 100) {
dcb20d1a
PR
1911 if (new_conf->wire_protocol != old_conf->wire_protocol)
1912 return ERR_NEED_APV_100;
b411b363 1913
dcb20d1a
PR
1914 if (new_conf->two_primaries != old_conf->two_primaries)
1915 return ERR_NEED_APV_100;
1916
dcb20d1a
PR
1917 if (strcmp(new_conf->integrity_alg, old_conf->integrity_alg))
1918 return ERR_NEED_APV_100;
b411b363
PR
1919 }
1920
dcb20d1a 1921 if (!new_conf->two_primaries &&
bde89a9e
AG
1922 conn_highest_role(connection) == R_PRIMARY &&
1923 conn_highest_peer(connection) == R_PRIMARY)
dcb20d1a 1924 return ERR_NEED_ALLOW_TWO_PRI;
b411b363 1925
cd64397c
PR
1926 if (new_conf->two_primaries &&
1927 (new_conf->wire_protocol != DRBD_PROT_C))
1928 return ERR_NOT_PROTO_C;
1929
bde89a9e 1930 idr_for_each_entry(&connection->volumes, device, i) {
b30ab791
AG
1931 if (get_ldev(device)) {
1932 enum drbd_fencing_p fp = rcu_dereference(device->ldev->disk_conf)->fencing;
1933 put_ldev(device);
44ed167d 1934 if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
cd64397c 1935 return ERR_STONITH_AND_PROT_A;
b411b363 1936 }
b30ab791 1937 if (device->state.role == R_PRIMARY && new_conf->discard_my_data)
eb12010e 1938 return ERR_DISCARD_IMPOSSIBLE;
b411b363
PR
1939 }
1940
cd64397c
PR
1941 if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A)
1942 return ERR_CONG_NOT_PROTO_A;
b411b363 1943
cd64397c
PR
1944 return NO_ERROR;
1945}
b411b363 1946
44ed167d 1947static enum drbd_ret_code
bde89a9e 1948check_net_options(struct drbd_connection *connection, struct net_conf *new_conf)
44ed167d
PR
1949{
1950 static enum drbd_ret_code rv;
b30ab791 1951 struct drbd_device *device;
44ed167d 1952 int i;
b411b363 1953
44ed167d 1954 rcu_read_lock();
bde89a9e 1955 rv = _check_net_options(connection, rcu_dereference(connection->net_conf), new_conf);
44ed167d 1956 rcu_read_unlock();
b411b363 1957
bde89a9e
AG
1958 /* connection->volumes protected by genl_lock() here */
1959 idr_for_each_entry(&connection->volumes, device, i) {
b30ab791
AG
1960 if (!device->bitmap) {
1961 if (drbd_bm_init(device))
44ed167d 1962 return ERR_NOMEM;
b411b363
PR
1963 }
1964 }
1965
44ed167d
PR
1966 return rv;
1967}
b411b363 1968
0fd0ea06
PR
1969struct crypto {
1970 struct crypto_hash *verify_tfm;
1971 struct crypto_hash *csums_tfm;
1972 struct crypto_hash *cram_hmac_tfm;
8d412fc6 1973 struct crypto_hash *integrity_tfm;
0fd0ea06 1974};
b411b363 1975
0fd0ea06 1976static int
4b6ad6d4 1977alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
0fd0ea06
PR
1978{
1979 if (!tfm_name[0])
1980 return NO_ERROR;
b411b363 1981
0fd0ea06
PR
1982 *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
1983 if (IS_ERR(*tfm)) {
1984 *tfm = NULL;
1985 return err_alg;
b411b363 1986 }
b411b363 1987
0fd0ea06
PR
1988 return NO_ERROR;
1989}
b411b363 1990
0fd0ea06
PR
1991static enum drbd_ret_code
1992alloc_crypto(struct crypto *crypto, struct net_conf *new_conf)
1993{
1994 char hmac_name[CRYPTO_MAX_ALG_NAME];
1995 enum drbd_ret_code rv;
0fd0ea06 1996
4b6ad6d4
AG
1997 rv = alloc_hash(&crypto->csums_tfm, new_conf->csums_alg,
1998 ERR_CSUMS_ALG);
0fd0ea06
PR
1999 if (rv != NO_ERROR)
2000 return rv;
4b6ad6d4
AG
2001 rv = alloc_hash(&crypto->verify_tfm, new_conf->verify_alg,
2002 ERR_VERIFY_ALG);
0fd0ea06
PR
2003 if (rv != NO_ERROR)
2004 return rv;
4b6ad6d4
AG
2005 rv = alloc_hash(&crypto->integrity_tfm, new_conf->integrity_alg,
2006 ERR_INTEGRITY_ALG);
0fd0ea06
PR
2007 if (rv != NO_ERROR)
2008 return rv;
0fd0ea06
PR
2009 if (new_conf->cram_hmac_alg[0] != 0) {
2010 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
2011 new_conf->cram_hmac_alg);
b411b363 2012
4b6ad6d4
AG
2013 rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
2014 ERR_AUTH_ALG);
b411b363
PR
2015 }
2016
0fd0ea06
PR
2017 return rv;
2018}
b411b363 2019
0fd0ea06
PR
2020static void free_crypto(struct crypto *crypto)
2021{
0fd0ea06 2022 crypto_free_hash(crypto->cram_hmac_tfm);
8d412fc6 2023 crypto_free_hash(crypto->integrity_tfm);
0fd0ea06
PR
2024 crypto_free_hash(crypto->csums_tfm);
2025 crypto_free_hash(crypto->verify_tfm);
2026}
b411b363 2027
f399002e
LE
2028int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
2029{
2030 enum drbd_ret_code retcode;
bde89a9e 2031 struct drbd_connection *connection;
44ed167d 2032 struct net_conf *old_conf, *new_conf = NULL;
f399002e
LE
2033 int err;
2034 int ovr; /* online verify running */
2035 int rsr; /* re-sync running */
0fd0ea06 2036 struct crypto crypto = { };
b411b363 2037
089c075d 2038 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
f399002e
LE
2039 if (!adm_ctx.reply_skb)
2040 return retcode;
2041 if (retcode != NO_ERROR)
2042 goto out;
b411b363 2043
bde89a9e 2044 connection = adm_ctx.connection;
b411b363 2045
f399002e
LE
2046 new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
2047 if (!new_conf) {
2048 retcode = ERR_NOMEM;
2049 goto out;
2050 }
b411b363 2051
bde89a9e 2052 conn_reconfig_start(connection);
b411b363 2053
bde89a9e
AG
2054 mutex_lock(&connection->data.mutex);
2055 mutex_lock(&connection->conf_update);
2056 old_conf = connection->net_conf;
2561b9c1 2057
44ed167d 2058 if (!old_conf) {
f399002e
LE
2059 drbd_msg_put_info("net conf missing, try connect");
2060 retcode = ERR_INVALID_REQUEST;
2561b9c1
PR
2061 goto fail;
2062 }
2063
44ed167d 2064 *new_conf = *old_conf;
5979e361 2065 if (should_set_defaults(info))
b966b5dd 2066 set_net_conf_defaults(new_conf);
f399002e 2067
f399002e 2068 err = net_conf_from_attrs_for_change(new_conf, info);
c75b9b10 2069 if (err && err != -ENOMSG) {
f399002e
LE
2070 retcode = ERR_MANDATORY_TAG;
2071 drbd_msg_put_info(from_attrs_err_to_txt(err));
2072 goto fail;
2561b9c1 2073 }
b411b363 2074
bde89a9e 2075 retcode = check_net_options(connection, new_conf);
cd64397c
PR
2076 if (retcode != NO_ERROR)
2077 goto fail;
b411b363 2078
f399002e 2079 /* re-sync running */
bde89a9e 2080 rsr = conn_resync_running(connection);
0fd0ea06 2081 if (rsr && strcmp(new_conf->csums_alg, old_conf->csums_alg)) {
f399002e 2082 retcode = ERR_CSUMS_RESYNC_RUNNING;
91fd4dad 2083 goto fail;
b411b363
PR
2084 }
2085
f399002e 2086 /* online verify running */
bde89a9e 2087 ovr = conn_ov_running(connection);
0fd0ea06
PR
2088 if (ovr && strcmp(new_conf->verify_alg, old_conf->verify_alg)) {
2089 retcode = ERR_VERIFY_RUNNING;
b411b363 2090 goto fail;
f399002e 2091 }
b411b363 2092
0fd0ea06
PR
2093 retcode = alloc_crypto(&crypto, new_conf);
2094 if (retcode != NO_ERROR)
b411b363 2095 goto fail;
f399002e 2096
bde89a9e 2097 rcu_assign_pointer(connection->net_conf, new_conf);
f399002e
LE
2098
2099 if (!rsr) {
bde89a9e
AG
2100 crypto_free_hash(connection->csums_tfm);
2101 connection->csums_tfm = crypto.csums_tfm;
0fd0ea06 2102 crypto.csums_tfm = NULL;
f399002e
LE
2103 }
2104 if (!ovr) {
bde89a9e
AG
2105 crypto_free_hash(connection->verify_tfm);
2106 connection->verify_tfm = crypto.verify_tfm;
0fd0ea06 2107 crypto.verify_tfm = NULL;
b411b363
PR
2108 }
2109
bde89a9e
AG
2110 crypto_free_hash(connection->integrity_tfm);
2111 connection->integrity_tfm = crypto.integrity_tfm;
2112 if (connection->cstate >= C_WF_REPORT_PARAMS && connection->agreed_pro_version >= 100)
2113 /* Do this without trying to take connection->data.mutex again. */
2114 __drbd_send_protocol(connection, P_PROTOCOL_UPDATE);
0fd0ea06 2115
bde89a9e
AG
2116 crypto_free_hash(connection->cram_hmac_tfm);
2117 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
0fd0ea06 2118
bde89a9e
AG
2119 mutex_unlock(&connection->conf_update);
2120 mutex_unlock(&connection->data.mutex);
91fd4dad
PR
2121 synchronize_rcu();
2122 kfree(old_conf);
2123
bde89a9e
AG
2124 if (connection->cstate >= C_WF_REPORT_PARAMS)
2125 drbd_send_sync_param(minor_to_device(conn_lowest_minor(connection)));
f399002e 2126
91fd4dad
PR
2127 goto done;
2128
b411b363 2129 fail:
bde89a9e
AG
2130 mutex_unlock(&connection->conf_update);
2131 mutex_unlock(&connection->data.mutex);
0fd0ea06 2132 free_crypto(&crypto);
f399002e 2133 kfree(new_conf);
91fd4dad 2134 done:
bde89a9e 2135 conn_reconfig_done(connection);
f399002e
LE
2136 out:
2137 drbd_adm_finish(info, retcode);
b411b363
PR
2138 return 0;
2139}
2140
3b98c0c2 2141int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
b411b363 2142{
b30ab791 2143 struct drbd_device *device;
44ed167d 2144 struct net_conf *old_conf, *new_conf = NULL;
0fd0ea06 2145 struct crypto crypto = { };
bde89a9e 2146 struct drbd_connection *connection;
3b98c0c2
LE
2147 enum drbd_ret_code retcode;
2148 int i;
2149 int err;
b411b363 2150
44e52cfa 2151 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
b411b363 2152
3b98c0c2
LE
2153 if (!adm_ctx.reply_skb)
2154 return retcode;
2155 if (retcode != NO_ERROR)
2156 goto out;
089c075d
AG
2157 if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
2158 drbd_msg_put_info("connection endpoint(s) missing");
2159 retcode = ERR_INVALID_REQUEST;
2160 goto out;
2161 }
b411b363 2162
089c075d
AG
2163 /* No need for _rcu here. All reconfiguration is
2164 * strictly serialized on genl_lock(). We are protected against
2165 * concurrent reconfiguration/addition/deletion */
bde89a9e
AG
2166 list_for_each_entry(connection, &drbd_connections, connections) {
2167 if (nla_len(adm_ctx.my_addr) == connection->my_addr_len &&
2168 !memcmp(nla_data(adm_ctx.my_addr), &connection->my_addr, connection->my_addr_len)) {
089c075d
AG
2169 retcode = ERR_LOCAL_ADDR;
2170 goto out;
2171 }
b411b363 2172
bde89a9e
AG
2173 if (nla_len(adm_ctx.peer_addr) == connection->peer_addr_len &&
2174 !memcmp(nla_data(adm_ctx.peer_addr), &connection->peer_addr, connection->peer_addr_len)) {
089c075d
AG
2175 retcode = ERR_PEER_ADDR;
2176 goto out;
2177 }
b411b363
PR
2178 }
2179
bde89a9e
AG
2180 connection = adm_ctx.connection;
2181 conn_reconfig_start(connection);
b411b363 2182
bde89a9e 2183 if (connection->cstate > C_STANDALONE) {
b411b363 2184 retcode = ERR_NET_CONFIGURED;
b411b363
PR
2185 goto fail;
2186 }
2187
a209b4ae 2188 /* allocation not in the IO path, drbdsetup / netlink process context */
5979e361 2189 new_conf = kzalloc(sizeof(*new_conf), GFP_KERNEL);
b411b363
PR
2190 if (!new_conf) {
2191 retcode = ERR_NOMEM;
b411b363
PR
2192 goto fail;
2193 }
2194
b966b5dd 2195 set_net_conf_defaults(new_conf);
b411b363 2196
f399002e 2197 err = net_conf_from_attrs(new_conf, info);
25e40932 2198 if (err && err != -ENOMSG) {
b411b363 2199 retcode = ERR_MANDATORY_TAG;
3b98c0c2 2200 drbd_msg_put_info(from_attrs_err_to_txt(err));
b411b363
PR
2201 goto fail;
2202 }
2203
bde89a9e 2204 retcode = check_net_options(connection, new_conf);
cd64397c 2205 if (retcode != NO_ERROR)
422028b1 2206 goto fail;
b411b363 2207
0fd0ea06
PR
2208 retcode = alloc_crypto(&crypto, new_conf);
2209 if (retcode != NO_ERROR)
2210 goto fail;
b411b363 2211
b411b363 2212 ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
7b4e4d31 2213
bde89a9e 2214 conn_flush_workqueue(connection);
b411b363 2215
bde89a9e
AG
2216 mutex_lock(&connection->conf_update);
2217 old_conf = connection->net_conf;
91fd4dad 2218 if (old_conf) {
b411b363 2219 retcode = ERR_NET_CONFIGURED;
bde89a9e 2220 mutex_unlock(&connection->conf_update);
b411b363
PR
2221 goto fail;
2222 }
bde89a9e 2223 rcu_assign_pointer(connection->net_conf, new_conf);
b411b363 2224
bde89a9e
AG
2225 conn_free_crypto(connection);
2226 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
2227 connection->integrity_tfm = crypto.integrity_tfm;
2228 connection->csums_tfm = crypto.csums_tfm;
2229 connection->verify_tfm = crypto.verify_tfm;
b411b363 2230
bde89a9e
AG
2231 connection->my_addr_len = nla_len(adm_ctx.my_addr);
2232 memcpy(&connection->my_addr, nla_data(adm_ctx.my_addr), connection->my_addr_len);
2233 connection->peer_addr_len = nla_len(adm_ctx.peer_addr);
2234 memcpy(&connection->peer_addr, nla_data(adm_ctx.peer_addr), connection->peer_addr_len);
b411b363 2235
bde89a9e 2236 mutex_unlock(&connection->conf_update);
b411b363 2237
695d08fa 2238 rcu_read_lock();
bde89a9e 2239 idr_for_each_entry(&connection->volumes, device, i) {
b30ab791
AG
2240 device->send_cnt = 0;
2241 device->recv_cnt = 0;
b411b363 2242 }
695d08fa 2243 rcu_read_unlock();
b411b363 2244
bde89a9e 2245 retcode = conn_request_state(connection, NS(conn, C_UNCONNECTED), CS_VERBOSE);
b411b363 2246
bde89a9e 2247 conn_reconfig_done(connection);
3b98c0c2 2248 drbd_adm_finish(info, retcode);
b411b363 2249 return 0;
b411b363 2250
b411b363 2251fail:
0fd0ea06 2252 free_crypto(&crypto);
b411b363 2253 kfree(new_conf);
b411b363 2254
bde89a9e 2255 conn_reconfig_done(connection);
3b98c0c2
LE
2256out:
2257 drbd_adm_finish(info, retcode);
b411b363
PR
2258 return 0;
2259}
2260
bde89a9e 2261static enum drbd_state_rv conn_try_disconnect(struct drbd_connection *connection, bool force)
85f75dd7
LE
2262{
2263 enum drbd_state_rv rv;
85f75dd7 2264
bde89a9e 2265 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
f3dfa40a 2266 force ? CS_HARD : 0);
85f75dd7
LE
2267
2268 switch (rv) {
2269 case SS_NOTHING_TO_DO:
f3dfa40a 2270 break;
85f75dd7
LE
2271 case SS_ALREADY_STANDALONE:
2272 return SS_SUCCESS;
2273 case SS_PRIMARY_NOP:
2274 /* Our state checking code wants to see the peer outdated. */
bde89a9e 2275 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING, pdsk, D_OUTDATED), 0);
2bd5ed5d
PR
2276
2277 if (rv == SS_OUTDATE_WO_CONN) /* lost connection before graceful disconnect succeeded */
bde89a9e 2278 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_VERBOSE);
2bd5ed5d 2279
85f75dd7
LE
2280 break;
2281 case SS_CW_FAILED_BY_PEER:
2282 /* The peer probably wants to see us outdated. */
bde89a9e 2283 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING,
85f75dd7
LE
2284 disk, D_OUTDATED), 0);
2285 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
bde89a9e 2286 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
f3dfa40a 2287 CS_HARD);
b411b363 2288 }
85f75dd7
LE
2289 break;
2290 default:;
2291 /* no special handling necessary */
2292 }
2293
f3dfa40a
LE
2294 if (rv >= SS_SUCCESS) {
2295 enum drbd_state_rv rv2;
2296 /* No one else can reconfigure the network while I am here.
2297 * The state handling only uses drbd_thread_stop_nowait(),
2298 * we want to really wait here until the receiver is no more.
2299 */
bde89a9e 2300 drbd_thread_stop(&adm_ctx.connection->receiver);
f3dfa40a
LE
2301
2302 /* Race breaker. This additional state change request may be
2303 * necessary, if this was a forced disconnect during a receiver
2304 * restart. We may have "killed" the receiver thread just
2305 * after drbdd_init() returned. Typically, we should be
2306 * C_STANDALONE already, now, and this becomes a no-op.
2307 */
bde89a9e 2308 rv2 = conn_request_state(connection, NS(conn, C_STANDALONE),
f3dfa40a
LE
2309 CS_VERBOSE | CS_HARD);
2310 if (rv2 < SS_SUCCESS)
bde89a9e 2311 conn_err(connection,
f3dfa40a
LE
2312 "unexpected rv2=%d in conn_try_disconnect()\n",
2313 rv2);
b411b363 2314 }
85f75dd7
LE
2315 return rv;
2316}
b411b363 2317
3b98c0c2 2318int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
b411b363 2319{
3b98c0c2 2320 struct disconnect_parms parms;
bde89a9e 2321 struct drbd_connection *connection;
85f75dd7 2322 enum drbd_state_rv rv;
3b98c0c2
LE
2323 enum drbd_ret_code retcode;
2324 int err;
2561b9c1 2325
089c075d 2326 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
3b98c0c2
LE
2327 if (!adm_ctx.reply_skb)
2328 return retcode;
2329 if (retcode != NO_ERROR)
2561b9c1 2330 goto fail;
b411b363 2331
bde89a9e 2332 connection = adm_ctx.connection;
3b98c0c2
LE
2333 memset(&parms, 0, sizeof(parms));
2334 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
f399002e 2335 err = disconnect_parms_from_attrs(&parms, info);
3b98c0c2
LE
2336 if (err) {
2337 retcode = ERR_MANDATORY_TAG;
2338 drbd_msg_put_info(from_attrs_err_to_txt(err));
b411b363
PR
2339 goto fail;
2340 }
2341 }
2342
bde89a9e 2343 rv = conn_try_disconnect(connection, parms.force_disconnect);
85f75dd7 2344 if (rv < SS_SUCCESS)
f3dfa40a
LE
2345 retcode = rv; /* FIXME: Type mismatch. */
2346 else
2347 retcode = NO_ERROR;
b411b363 2348 fail:
3b98c0c2 2349 drbd_adm_finish(info, retcode);
b411b363
PR
2350 return 0;
2351}
2352
b30ab791 2353void resync_after_online_grow(struct drbd_device *device)
b411b363
PR
2354{
2355 int iass; /* I am sync source */
2356
2357 dev_info(DEV, "Resync of new storage after online grow\n");
b30ab791
AG
2358 if (device->state.role != device->state.peer)
2359 iass = (device->state.role == R_PRIMARY);
b411b363 2360 else
a6b32bc3 2361 iass = test_bit(RESOLVE_CONFLICTS, &first_peer_device(device)->connection->flags);
b411b363
PR
2362
2363 if (iass)
b30ab791 2364 drbd_start_resync(device, C_SYNC_SOURCE);
b411b363 2365 else
b30ab791 2366 _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
b411b363
PR
2367}
2368
3b98c0c2 2369int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
b411b363 2370{
daeda1cc 2371 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
3b98c0c2 2372 struct resize_parms rs;
b30ab791 2373 struct drbd_device *device;
3b98c0c2 2374 enum drbd_ret_code retcode;
b411b363 2375 enum determine_dev_size dd;
d752b269 2376 bool change_al_layout = false;
6495d2c6 2377 enum dds_flags ddsf;
daeda1cc 2378 sector_t u_size;
3b98c0c2 2379 int err;
b411b363 2380
3b98c0c2
LE
2381 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2382 if (!adm_ctx.reply_skb)
2383 return retcode;
2384 if (retcode != NO_ERROR)
b411b363 2385 goto fail;
3b98c0c2 2386
b30ab791
AG
2387 device = adm_ctx.device;
2388 if (!get_ldev(device)) {
d752b269
PR
2389 retcode = ERR_NO_DISK;
2390 goto fail;
2391 }
2392
3b98c0c2 2393 memset(&rs, 0, sizeof(struct resize_parms));
b30ab791
AG
2394 rs.al_stripes = device->ldev->md.al_stripes;
2395 rs.al_stripe_size = device->ldev->md.al_stripe_size_4k * 4;
3b98c0c2 2396 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
f399002e 2397 err = resize_parms_from_attrs(&rs, info);
b411b363 2398 if (err) {
3b98c0c2
LE
2399 retcode = ERR_MANDATORY_TAG;
2400 drbd_msg_put_info(from_attrs_err_to_txt(err));
d752b269 2401 goto fail_ldev;
b411b363
PR
2402 }
2403 }
2404
b30ab791 2405 if (device->state.conn > C_CONNECTED) {
b411b363 2406 retcode = ERR_RESIZE_RESYNC;
d752b269 2407 goto fail_ldev;
b411b363 2408 }
b411b363 2409
b30ab791
AG
2410 if (device->state.role == R_SECONDARY &&
2411 device->state.peer == R_SECONDARY) {
b411b363 2412 retcode = ERR_NO_PRIMARY;
d752b269 2413 goto fail_ldev;
b411b363 2414 }
b411b363 2415
a6b32bc3 2416 if (rs.no_resync && first_peer_device(device)->connection->agreed_pro_version < 93) {
6495d2c6 2417 retcode = ERR_NEED_APV_93;
9bcd2521 2418 goto fail_ldev;
6495d2c6
PR
2419 }
2420
daeda1cc 2421 rcu_read_lock();
b30ab791 2422 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
daeda1cc
PR
2423 rcu_read_unlock();
2424 if (u_size != (sector_t)rs.resize_size) {
2425 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
2426 if (!new_disk_conf) {
778f271d 2427 retcode = ERR_NOMEM;
9bcd2521 2428 goto fail_ldev;
778f271d
PR
2429 }
2430 }
2431
b30ab791
AG
2432 if (device->ldev->md.al_stripes != rs.al_stripes ||
2433 device->ldev->md.al_stripe_size_4k != rs.al_stripe_size / 4) {
d752b269
PR
2434 u32 al_size_k = rs.al_stripes * rs.al_stripe_size;
2435
2436 if (al_size_k > (16 * 1024 * 1024)) {
2437 retcode = ERR_MD_LAYOUT_TOO_BIG;
2438 goto fail_ldev;
2439 }
2440
2441 if (al_size_k < MD_32kB_SECT/2) {
2442 retcode = ERR_MD_LAYOUT_TOO_SMALL;
2443 goto fail_ldev;
2444 }
2445
b30ab791 2446 if (device->state.conn != C_CONNECTED) {
d752b269
PR
2447 retcode = ERR_MD_LAYOUT_CONNECTED;
2448 goto fail_ldev;
2449 }
2450
2451 change_al_layout = true;
2452 }
2453
b30ab791
AG
2454 if (device->ldev->known_size != drbd_get_capacity(device->ldev->backing_bdev))
2455 device->ldev->known_size = drbd_get_capacity(device->ldev->backing_bdev);
b411b363 2456
daeda1cc 2457 if (new_disk_conf) {
a6b32bc3 2458 mutex_lock(&first_peer_device(device)->connection->conf_update);
b30ab791 2459 old_disk_conf = device->ldev->disk_conf;
daeda1cc
PR
2460 *new_disk_conf = *old_disk_conf;
2461 new_disk_conf->disk_size = (sector_t)rs.resize_size;
b30ab791 2462 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
a6b32bc3 2463 mutex_unlock(&first_peer_device(device)->connection->conf_update);
daeda1cc
PR
2464 synchronize_rcu();
2465 kfree(old_disk_conf);
b411b363
PR
2466 }
2467
6495d2c6 2468 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
b30ab791
AG
2469 dd = drbd_determine_dev_size(device, ddsf, change_al_layout ? &rs : NULL);
2470 drbd_md_sync(device);
2471 put_ldev(device);
e96c9633 2472 if (dd == DS_ERROR) {
b411b363
PR
2473 retcode = ERR_NOMEM_BITMAP;
2474 goto fail;
d752b269
PR
2475 } else if (dd == DS_ERROR_SPACE_MD) {
2476 retcode = ERR_MD_LAYOUT_NO_FIT;
2477 goto fail;
2478 } else if (dd == DS_ERROR_SHRINK) {
2479 retcode = ERR_IMPLICIT_SHRINK;
2480 goto fail;
b411b363 2481 }
778f271d 2482
b30ab791 2483 if (device->state.conn == C_CONNECTED) {
e96c9633 2484 if (dd == DS_GREW)
b30ab791 2485 set_bit(RESIZE_PENDING, &device->flags);
b411b363 2486
b30ab791
AG
2487 drbd_send_uuids(device);
2488 drbd_send_sizes(device, 1, ddsf);
778f271d
PR
2489 }
2490
b411b363 2491 fail:
3b98c0c2 2492 drbd_adm_finish(info, retcode);
b411b363 2493 return 0;
b411b363 2494
9bcd2521 2495 fail_ldev:
b30ab791 2496 put_ldev(device);
9bcd2521 2497 goto fail;
b411b363 2498}
b411b363 2499
f399002e 2500int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
b411b363 2501{
3b98c0c2 2502 enum drbd_ret_code retcode;
bde89a9e 2503 struct drbd_connection *connection;
b57a1e27 2504 struct res_opts res_opts;
f399002e 2505 int err;
b411b363 2506
44e52cfa 2507 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
3b98c0c2
LE
2508 if (!adm_ctx.reply_skb)
2509 return retcode;
2510 if (retcode != NO_ERROR)
2511 goto fail;
bde89a9e 2512 connection = adm_ctx.connection;
b411b363 2513
bde89a9e 2514 res_opts = connection->res_opts;
5979e361 2515 if (should_set_defaults(info))
b966b5dd 2516 set_res_opts_defaults(&res_opts);
b411b363 2517
b57a1e27 2518 err = res_opts_from_attrs(&res_opts, info);
c75b9b10 2519 if (err && err != -ENOMSG) {
b411b363 2520 retcode = ERR_MANDATORY_TAG;
3b98c0c2 2521 drbd_msg_put_info(from_attrs_err_to_txt(err));
b411b363
PR
2522 goto fail;
2523 }
2524
bde89a9e 2525 err = set_resource_options(connection, &res_opts);
afbbfa88
AG
2526 if (err) {
2527 retcode = ERR_INVALID_REQUEST;
2528 if (err == -ENOMEM)
2529 retcode = ERR_NOMEM;
b411b363
PR
2530 }
2531
b411b363 2532fail:
3b98c0c2 2533 drbd_adm_finish(info, retcode);
b411b363
PR
2534 return 0;
2535}
2536
3b98c0c2 2537int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
b411b363 2538{
b30ab791 2539 struct drbd_device *device;
3b98c0c2
LE
2540 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2541
2542 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2543 if (!adm_ctx.reply_skb)
2544 return retcode;
2545 if (retcode != NO_ERROR)
2546 goto out;
2547
b30ab791 2548 device = adm_ctx.device;
b411b363 2549
194bfb32 2550 /* If there is still bitmap IO pending, probably because of a previous
7ee1fb93
LE
2551 * resync just being finished, wait for it before requesting a new resync.
2552 * Also wait for it's after_state_ch(). */
b30ab791
AG
2553 drbd_suspend_io(device);
2554 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
2555 drbd_flush_workqueue(device);
194bfb32 2556
0b2dafcd
PR
2557 /* If we happen to be C_STANDALONE R_SECONDARY, just change to
2558 * D_INCONSISTENT, and set all bits in the bitmap. Otherwise,
2559 * try to start a resync handshake as sync target for full sync.
9376d9f8 2560 */
b30ab791
AG
2561 if (device->state.conn == C_STANDALONE && device->state.role == R_SECONDARY) {
2562 retcode = drbd_request_state(device, NS(disk, D_INCONSISTENT));
0b2dafcd 2563 if (retcode >= SS_SUCCESS) {
b30ab791 2564 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
0b2dafcd
PR
2565 "set_n_write from invalidate", BM_LOCKED_MASK))
2566 retcode = ERR_IO_MD_DISK;
2567 }
2568 } else
b30ab791
AG
2569 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_T));
2570 drbd_resume_io(device);
b411b363 2571
3b98c0c2
LE
2572out:
2573 drbd_adm_finish(info, retcode);
b411b363
PR
2574 return 0;
2575}
2576
3b98c0c2
LE
2577static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2578 union drbd_state mask, union drbd_state val)
b411b363 2579{
3b98c0c2 2580 enum drbd_ret_code retcode;
194bfb32 2581
3b98c0c2
LE
2582 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2583 if (!adm_ctx.reply_skb)
2584 return retcode;
2585 if (retcode != NO_ERROR)
2586 goto out;
b411b363 2587
b30ab791 2588 retcode = drbd_request_state(adm_ctx.device, mask, val);
3b98c0c2
LE
2589out:
2590 drbd_adm_finish(info, retcode);
b411b363
PR
2591 return 0;
2592}
2593
b30ab791 2594static int drbd_bmio_set_susp_al(struct drbd_device *device)
0778286a
PR
2595{
2596 int rv;
2597
b30ab791
AG
2598 rv = drbd_bmio_set_n_write(device);
2599 drbd_suspend_al(device);
0778286a
PR
2600 return rv;
2601}
2602
3b98c0c2 2603int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
b411b363 2604{
25b0d6c8 2605 int retcode; /* drbd_ret_code, drbd_state_rv */
b30ab791 2606 struct drbd_device *device;
25b0d6c8
PR
2607
2608 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2609 if (!adm_ctx.reply_skb)
2610 return retcode;
2611 if (retcode != NO_ERROR)
2612 goto out;
2613
b30ab791 2614 device = adm_ctx.device;
b411b363 2615
194bfb32 2616 /* If there is still bitmap IO pending, probably because of a previous
7ee1fb93
LE
2617 * resync just being finished, wait for it before requesting a new resync.
2618 * Also wait for it's after_state_ch(). */
b30ab791
AG
2619 drbd_suspend_io(device);
2620 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
2621 drbd_flush_workqueue(device);
194bfb32 2622
0b2dafcd
PR
2623 /* If we happen to be C_STANDALONE R_PRIMARY, just set all bits
2624 * in the bitmap. Otherwise, try to start a resync handshake
2625 * as sync source for full sync.
2626 */
b30ab791 2627 if (device->state.conn == C_STANDALONE && device->state.role == R_PRIMARY) {
0b2dafcd
PR
2628 /* The peer will get a resync upon connect anyways. Just make that
2629 into a full resync. */
b30ab791 2630 retcode = drbd_request_state(device, NS(pdsk, D_INCONSISTENT));
0b2dafcd 2631 if (retcode >= SS_SUCCESS) {
b30ab791 2632 if (drbd_bitmap_io(device, &drbd_bmio_set_susp_al,
0b2dafcd
PR
2633 "set_n_write from invalidate_peer",
2634 BM_LOCKED_SET_ALLOWED))
2635 retcode = ERR_IO_MD_DISK;
2636 }
2637 } else
b30ab791
AG
2638 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_S));
2639 drbd_resume_io(device);
b411b363 2640
25b0d6c8
PR
2641out:
2642 drbd_adm_finish(info, retcode);
b411b363
PR
2643 return 0;
2644}
2645
3b98c0c2 2646int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
b411b363 2647{
3b98c0c2 2648 enum drbd_ret_code retcode;
b411b363 2649
3b98c0c2
LE
2650 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2651 if (!adm_ctx.reply_skb)
2652 return retcode;
2653 if (retcode != NO_ERROR)
2654 goto out;
b411b363 2655
b30ab791 2656 if (drbd_request_state(adm_ctx.device, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
3b98c0c2
LE
2657 retcode = ERR_PAUSE_IS_SET;
2658out:
2659 drbd_adm_finish(info, retcode);
b411b363
PR
2660 return 0;
2661}
2662
3b98c0c2 2663int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
b411b363 2664{
da9fbc27 2665 union drbd_dev_state s;
3b98c0c2
LE
2666 enum drbd_ret_code retcode;
2667
2668 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2669 if (!adm_ctx.reply_skb)
2670 return retcode;
2671 if (retcode != NO_ERROR)
2672 goto out;
b411b363 2673
b30ab791
AG
2674 if (drbd_request_state(adm_ctx.device, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2675 s = adm_ctx.device->state;
cd88d030
PR
2676 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2677 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2678 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2679 } else {
2680 retcode = ERR_PAUSE_IS_CLEAR;
2681 }
2682 }
b411b363 2683
3b98c0c2
LE
2684out:
2685 drbd_adm_finish(info, retcode);
b411b363
PR
2686 return 0;
2687}
2688
3b98c0c2 2689int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
b411b363 2690{
3b98c0c2 2691 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
b411b363
PR
2692}
2693
3b98c0c2 2694int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
b411b363 2695{
b30ab791 2696 struct drbd_device *device;
3b98c0c2
LE
2697 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2698
2699 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2700 if (!adm_ctx.reply_skb)
2701 return retcode;
2702 if (retcode != NO_ERROR)
2703 goto out;
2704
b30ab791
AG
2705 device = adm_ctx.device;
2706 if (test_bit(NEW_CUR_UUID, &device->flags)) {
2707 drbd_uuid_new_current(device);
2708 clear_bit(NEW_CUR_UUID, &device->flags);
43a5182c 2709 }
b30ab791
AG
2710 drbd_suspend_io(device);
2711 retcode = drbd_request_state(device, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
3b98c0c2 2712 if (retcode == SS_SUCCESS) {
b30ab791 2713 if (device->state.conn < C_CONNECTED)
a6b32bc3 2714 tl_clear(first_peer_device(device)->connection);
b30ab791 2715 if (device->state.disk == D_DISKLESS || device->state.disk == D_FAILED)
a6b32bc3 2716 tl_restart(first_peer_device(device)->connection, FAIL_FROZEN_DISK_IO);
265be2d0 2717 }
b30ab791 2718 drbd_resume_io(device);
265be2d0 2719
3b98c0c2
LE
2720out:
2721 drbd_adm_finish(info, retcode);
b411b363
PR
2722 return 0;
2723}
2724
3b98c0c2 2725int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
b411b363 2726{
3b98c0c2 2727 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
b411b363
PR
2728}
2729
bde89a9e 2730static int nla_put_drbd_cfg_context(struct sk_buff *skb, struct drbd_connection *connection, unsigned vnr)
b411b363 2731{
543cc10b
LE
2732 struct nlattr *nla;
2733 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
2734 if (!nla)
2735 goto nla_put_failure;
26ec9287
AG
2736 if (vnr != VOLUME_UNSPECIFIED &&
2737 nla_put_u32(skb, T_ctx_volume, vnr))
2738 goto nla_put_failure;
bde89a9e 2739 if (nla_put_string(skb, T_ctx_resource_name, connection->name))
26ec9287 2740 goto nla_put_failure;
bde89a9e
AG
2741 if (connection->my_addr_len &&
2742 nla_put(skb, T_ctx_my_addr, connection->my_addr_len, &connection->my_addr))
26ec9287 2743 goto nla_put_failure;
bde89a9e
AG
2744 if (connection->peer_addr_len &&
2745 nla_put(skb, T_ctx_peer_addr, connection->peer_addr_len, &connection->peer_addr))
26ec9287 2746 goto nla_put_failure;
543cc10b
LE
2747 nla_nest_end(skb, nla);
2748 return 0;
b411b363 2749
543cc10b
LE
2750nla_put_failure:
2751 if (nla)
2752 nla_nest_cancel(skb, nla);
2753 return -EMSGSIZE;
2754}
b411b363 2755
b30ab791 2756static int nla_put_status_info(struct sk_buff *skb, struct drbd_device *device,
3b98c0c2 2757 const struct sib_info *sib)
b411b363 2758{
3b98c0c2
LE
2759 struct state_info *si = NULL; /* for sizeof(si->member); */
2760 struct nlattr *nla;
2761 int got_ldev;
3b98c0c2
LE
2762 int err = 0;
2763 int exclude_sensitive;
2764
2765 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2766 * to. So we better exclude_sensitive information.
2767 *
2768 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2769 * in the context of the requesting user process. Exclude sensitive
2770 * information, unless current has superuser.
2771 *
2772 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2773 * relies on the current implementation of netlink_dump(), which
2774 * executes the dump callback successively from netlink_recvmsg(),
2775 * always in the context of the receiving process */
2776 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
2777
b30ab791 2778 got_ldev = get_ldev(device);
3b98c0c2
LE
2779
2780 /* We need to add connection name and volume number information still.
2781 * Minor number is in drbd_genlmsghdr. */
a6b32bc3 2782 if (nla_put_drbd_cfg_context(skb, first_peer_device(device)->connection, device->vnr))
3b98c0c2 2783 goto nla_put_failure;
3b98c0c2 2784
a6b32bc3 2785 if (res_opts_to_skb(skb, &first_peer_device(device)->connection->res_opts, exclude_sensitive))
f399002e
LE
2786 goto nla_put_failure;
2787
daeda1cc 2788 rcu_read_lock();
f9eb7bf4
AG
2789 if (got_ldev) {
2790 struct disk_conf *disk_conf;
44ed167d 2791
b30ab791 2792 disk_conf = rcu_dereference(device->ldev->disk_conf);
f9eb7bf4
AG
2793 err = disk_conf_to_skb(skb, disk_conf, exclude_sensitive);
2794 }
2795 if (!err) {
2796 struct net_conf *nc;
2797
a6b32bc3 2798 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
f9eb7bf4
AG
2799 if (nc)
2800 err = net_conf_to_skb(skb, nc, exclude_sensitive);
2801 }
44ed167d
PR
2802 rcu_read_unlock();
2803 if (err)
2804 goto nla_put_failure;
3b98c0c2 2805
3b98c0c2
LE
2806 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
2807 if (!nla)
2808 goto nla_put_failure;
26ec9287 2809 if (nla_put_u32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY) ||
b30ab791
AG
2810 nla_put_u32(skb, T_current_state, device->state.i) ||
2811 nla_put_u64(skb, T_ed_uuid, device->ed_uuid) ||
2812 nla_put_u64(skb, T_capacity, drbd_get_capacity(device->this_bdev)) ||
2813 nla_put_u64(skb, T_send_cnt, device->send_cnt) ||
2814 nla_put_u64(skb, T_recv_cnt, device->recv_cnt) ||
2815 nla_put_u64(skb, T_read_cnt, device->read_cnt) ||
2816 nla_put_u64(skb, T_writ_cnt, device->writ_cnt) ||
2817 nla_put_u64(skb, T_al_writ_cnt, device->al_writ_cnt) ||
2818 nla_put_u64(skb, T_bm_writ_cnt, device->bm_writ_cnt) ||
2819 nla_put_u32(skb, T_ap_bio_cnt, atomic_read(&device->ap_bio_cnt)) ||
2820 nla_put_u32(skb, T_ap_pending_cnt, atomic_read(&device->ap_pending_cnt)) ||
2821 nla_put_u32(skb, T_rs_pending_cnt, atomic_read(&device->rs_pending_cnt)))
26ec9287 2822 goto nla_put_failure;
3b98c0c2
LE
2823
2824 if (got_ldev) {
39a1aa7f 2825 int err;
b411b363 2826
b30ab791
AG
2827 spin_lock_irq(&device->ldev->md.uuid_lock);
2828 err = nla_put(skb, T_uuids, sizeof(si->uuids), device->ldev->md.uuid);
2829 spin_unlock_irq(&device->ldev->md.uuid_lock);
39a1aa7f
PR
2830
2831 if (err)
2832 goto nla_put_failure;
2833
b30ab791
AG
2834 if (nla_put_u32(skb, T_disk_flags, device->ldev->md.flags) ||
2835 nla_put_u64(skb, T_bits_total, drbd_bm_bits(device)) ||
2836 nla_put_u64(skb, T_bits_oos, drbd_bm_total_weight(device)))
26ec9287 2837 goto nla_put_failure;
b30ab791
AG
2838 if (C_SYNC_SOURCE <= device->state.conn &&
2839 C_PAUSED_SYNC_T >= device->state.conn) {
2840 if (nla_put_u64(skb, T_bits_rs_total, device->rs_total) ||
2841 nla_put_u64(skb, T_bits_rs_failed, device->rs_failed))
26ec9287 2842 goto nla_put_failure;
3b98c0c2 2843 }
b411b363 2844 }
b411b363 2845
3b98c0c2
LE
2846 if (sib) {
2847 switch(sib->sib_reason) {
2848 case SIB_SYNC_PROGRESS:
2849 case SIB_GET_STATUS_REPLY:
2850 break;
2851 case SIB_STATE_CHANGE:
26ec9287
AG
2852 if (nla_put_u32(skb, T_prev_state, sib->os.i) ||
2853 nla_put_u32(skb, T_new_state, sib->ns.i))
2854 goto nla_put_failure;
3b98c0c2
LE
2855 break;
2856 case SIB_HELPER_POST:
26ec9287
AG
2857 if (nla_put_u32(skb, T_helper_exit_code,
2858 sib->helper_exit_code))
2859 goto nla_put_failure;
3b98c0c2
LE
2860 /* fall through */
2861 case SIB_HELPER_PRE:
26ec9287
AG
2862 if (nla_put_string(skb, T_helper, sib->helper_name))
2863 goto nla_put_failure;
3b98c0c2
LE
2864 break;
2865 }
b411b363 2866 }
3b98c0c2 2867 nla_nest_end(skb, nla);
b411b363 2868
3b98c0c2
LE
2869 if (0)
2870nla_put_failure:
2871 err = -EMSGSIZE;
2872 if (got_ldev)
b30ab791 2873 put_ldev(device);
3b98c0c2 2874 return err;
b411b363
PR
2875}
2876
3b98c0c2 2877int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
b411b363 2878{
3b98c0c2
LE
2879 enum drbd_ret_code retcode;
2880 int err;
b411b363 2881
3b98c0c2
LE
2882 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2883 if (!adm_ctx.reply_skb)
2884 return retcode;
2885 if (retcode != NO_ERROR)
2886 goto out;
b411b363 2887
b30ab791 2888 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.device, NULL);
3b98c0c2
LE
2889 if (err) {
2890 nlmsg_free(adm_ctx.reply_skb);
2891 return err;
b411b363 2892 }
3b98c0c2
LE
2893out:
2894 drbd_adm_finish(info, retcode);
2895 return 0;
b411b363
PR
2896}
2897
4b7a530f 2898static int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
b411b363 2899{
b30ab791 2900 struct drbd_device *device;
3b98c0c2 2901 struct drbd_genlmsghdr *dh;
bde89a9e
AG
2902 struct drbd_connection *pos = (struct drbd_connection *)cb->args[0];
2903 struct drbd_connection *connection = NULL;
2904 struct drbd_connection *tmp;
543cc10b
LE
2905 unsigned volume = cb->args[1];
2906
2907 /* Open coded, deferred, iteration:
bde89a9e
AG
2908 * list_for_each_entry_safe(connection, tmp, &drbd_connections, connections) {
2909 * idr_for_each_entry(&connection->volumes, device, i) {
543cc10b
LE
2910 * ...
2911 * }
2912 * }
bde89a9e 2913 * where connection is cb->args[0];
543cc10b
LE
2914 * and i is cb->args[1];
2915 *
71932efc
LE
2916 * cb->args[2] indicates if we shall loop over all resources,
2917 * or just dump all volumes of a single resource.
2918 *
3b98c0c2
LE
2919 * This may miss entries inserted after this dump started,
2920 * or entries deleted before they are reached.
543cc10b 2921 *
b30ab791 2922 * We need to make sure the device won't disappear while
543cc10b
LE
2923 * we are looking at it, and revalidate our iterators
2924 * on each iteration.
2925 */
b411b363 2926
05a10ec7 2927 /* synchronize with conn_create()/drbd_destroy_connection() */
c141ebda 2928 rcu_read_lock();
543cc10b 2929 /* revalidate iterator position */
bde89a9e 2930 list_for_each_entry_rcu(tmp, &drbd_connections, connections) {
543cc10b
LE
2931 if (pos == NULL) {
2932 /* first iteration */
2933 pos = tmp;
bde89a9e 2934 connection = pos;
543cc10b
LE
2935 break;
2936 }
2937 if (tmp == pos) {
bde89a9e 2938 connection = pos;
543cc10b
LE
2939 break;
2940 }
b411b363 2941 }
bde89a9e
AG
2942 if (connection) {
2943next_connection:
2944 device = idr_get_next(&connection->volumes, &volume);
b30ab791 2945 if (!device) {
bde89a9e
AG
2946 /* No more volumes to dump on this connection.
2947 * Advance connection iterator. */
2948 pos = list_entry_rcu(connection->connections.next,
2949 struct drbd_connection, connections);
2950 /* Did we dump any volume on this connection yet? */
543cc10b 2951 if (volume != 0) {
71932efc
LE
2952 /* If we reached the end of the list,
2953 * or only a single resource dump was requested,
2954 * we are done. */
bde89a9e 2955 if (&pos->connections == &drbd_connections || cb->args[2])
71932efc 2956 goto out;
543cc10b 2957 volume = 0;
bde89a9e
AG
2958 connection = pos;
2959 goto next_connection;
543cc10b
LE
2960 }
2961 }
2962
98683650 2963 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3b98c0c2
LE
2964 cb->nlh->nlmsg_seq, &drbd_genl_family,
2965 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
2966 if (!dh)
543cc10b
LE
2967 goto out;
2968
b30ab791 2969 if (!device) {
bde89a9e 2970 /* This is a connection without a single volume.
367d675d
LE
2971 * Suprisingly enough, it may have a network
2972 * configuration. */
2973 struct net_conf *nc;
543cc10b
LE
2974 dh->minor = -1U;
2975 dh->ret_code = NO_ERROR;
bde89a9e 2976 if (nla_put_drbd_cfg_context(skb, connection, VOLUME_UNSPECIFIED))
367d675d 2977 goto cancel;
bde89a9e 2978 nc = rcu_dereference(connection->net_conf);
367d675d
LE
2979 if (nc && net_conf_to_skb(skb, nc, 1) != 0)
2980 goto cancel;
2981 goto done;
543cc10b 2982 }
b411b363 2983
b30ab791 2984 D_ASSERT(device->vnr == volume);
a6b32bc3 2985 D_ASSERT(first_peer_device(device)->connection == connection);
3b98c0c2 2986
b30ab791 2987 dh->minor = device_to_minor(device);
3b98c0c2
LE
2988 dh->ret_code = NO_ERROR;
2989
b30ab791 2990 if (nla_put_status_info(skb, device, NULL)) {
367d675d 2991cancel:
3b98c0c2 2992 genlmsg_cancel(skb, dh);
543cc10b 2993 goto out;
3b98c0c2 2994 }
367d675d 2995done:
3b98c0c2 2996 genlmsg_end(skb, dh);
bde89a9e 2997 }
b411b363 2998
543cc10b 2999out:
c141ebda 3000 rcu_read_unlock();
543cc10b 3001 /* where to start the next iteration */
bde89a9e
AG
3002 cb->args[0] = (long)pos;
3003 cb->args[1] = (pos == connection) ? volume + 1 : 0;
b411b363 3004
bde89a9e 3005 /* No more connections/volumes/minors found results in an empty skb.
543cc10b 3006 * Which will terminate the dump. */
3b98c0c2 3007 return skb->len;
b411b363
PR
3008}
3009
71932efc
LE
3010/*
3011 * Request status of all resources, or of all volumes within a single resource.
3012 *
3013 * This is a dump, as the answer may not fit in a single reply skb otherwise.
3014 * Which means we cannot use the family->attrbuf or other such members, because
3015 * dump is NOT protected by the genl_lock(). During dump, we only have access
3016 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
3017 *
3018 * Once things are setup properly, we call into get_one_status().
b411b363 3019 */
71932efc 3020int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
b411b363 3021{
71932efc
LE
3022 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
3023 struct nlattr *nla;
7c3063cc 3024 const char *resource_name;
bde89a9e 3025 struct drbd_connection *connection;
7c3063cc 3026 int maxtype;
71932efc
LE
3027
3028 /* Is this a followup call? */
3029 if (cb->args[0]) {
3030 /* ... of a single resource dump,
3031 * and the resource iterator has been advanced already? */
3032 if (cb->args[2] && cb->args[2] != cb->args[0])
3033 return 0; /* DONE. */
3034 goto dump;
3035 }
3036
3037 /* First call (from netlink_dump_start). We need to figure out
3038 * which resource(s) the user wants us to dump. */
3039 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
3040 nlmsg_attrlen(cb->nlh, hdrlen),
3041 DRBD_NLA_CFG_CONTEXT);
3042
3043 /* No explicit context given. Dump all. */
3044 if (!nla)
3045 goto dump;
7c3063cc
AG
3046 maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
3047 nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
3048 if (IS_ERR(nla))
3049 return PTR_ERR(nla);
71932efc
LE
3050 /* context given, but no name present? */
3051 if (!nla)
3052 return -EINVAL;
7c3063cc 3053 resource_name = nla_data(nla);
bde89a9e 3054 connection = conn_get_by_name(resource_name);
0ace9dfa 3055
bde89a9e 3056 if (!connection)
71932efc
LE
3057 return -ENODEV;
3058
05a10ec7 3059 kref_put(&connection->kref, drbd_destroy_connection); /* get_one_status() (re)validates connection by itself */
0ace9dfa 3060
71932efc 3061 /* prime iterators, and set "filter" mode mark:
bde89a9e
AG
3062 * only dump this connection. */
3063 cb->args[0] = (long)connection;
71932efc 3064 /* cb->args[1] = 0; passed in this way. */
bde89a9e 3065 cb->args[2] = (long)connection;
71932efc
LE
3066
3067dump:
3068 return get_one_status(skb, cb);
3069}
b411b363 3070
3b98c0c2 3071int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
b411b363 3072{
3b98c0c2
LE
3073 enum drbd_ret_code retcode;
3074 struct timeout_parms tp;
3075 int err;
b411b363 3076
3b98c0c2
LE
3077 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3078 if (!adm_ctx.reply_skb)
3079 return retcode;
3080 if (retcode != NO_ERROR)
3081 goto out;
b411b363 3082
3b98c0c2 3083 tp.timeout_type =
b30ab791
AG
3084 adm_ctx.device->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
3085 test_bit(USE_DEGR_WFC_T, &adm_ctx.device->flags) ? UT_DEGRADED :
3b98c0c2 3086 UT_DEFAULT;
b411b363 3087
3b98c0c2
LE
3088 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
3089 if (err) {
3090 nlmsg_free(adm_ctx.reply_skb);
3091 return err;
3092 }
3093out:
3094 drbd_adm_finish(info, retcode);
3095 return 0;
b411b363
PR
3096}
3097
3b98c0c2 3098int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
b411b363 3099{
b30ab791 3100 struct drbd_device *device;
3b98c0c2 3101 enum drbd_ret_code retcode;
58ffa580 3102 struct start_ov_parms parms;
b411b363 3103
3b98c0c2
LE
3104 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3105 if (!adm_ctx.reply_skb)
3106 return retcode;
3107 if (retcode != NO_ERROR)
3108 goto out;
873b0d5f 3109
b30ab791 3110 device = adm_ctx.device;
58ffa580
LE
3111
3112 /* resume from last known position, if possible */
b30ab791 3113 parms.ov_start_sector = device->ov_start_sector;
58ffa580 3114 parms.ov_stop_sector = ULLONG_MAX;
3b98c0c2 3115 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
f399002e 3116 int err = start_ov_parms_from_attrs(&parms, info);
3b98c0c2
LE
3117 if (err) {
3118 retcode = ERR_MANDATORY_TAG;
3119 drbd_msg_put_info(from_attrs_err_to_txt(err));
3120 goto out;
3121 }
b411b363 3122 }
58ffa580 3123 /* w_make_ov_request expects position to be aligned */
b30ab791
AG
3124 device->ov_start_sector = parms.ov_start_sector & ~(BM_SECT_PER_BIT-1);
3125 device->ov_stop_sector = parms.ov_stop_sector;
873b0d5f
LE
3126
3127 /* If there is still bitmap IO pending, e.g. previous resync or verify
3128 * just being finished, wait for it before requesting a new resync. */
b30ab791
AG
3129 drbd_suspend_io(device);
3130 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
3131 retcode = drbd_request_state(device, NS(conn, C_VERIFY_S));
3132 drbd_resume_io(device);
3b98c0c2
LE
3133out:
3134 drbd_adm_finish(info, retcode);
b411b363
PR
3135 return 0;
3136}
3137
3138
3b98c0c2 3139int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
b411b363 3140{
b30ab791 3141 struct drbd_device *device;
3b98c0c2 3142 enum drbd_ret_code retcode;
b411b363
PR
3143 int skip_initial_sync = 0;
3144 int err;
3b98c0c2 3145 struct new_c_uuid_parms args;
b411b363 3146
3b98c0c2
LE
3147 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3148 if (!adm_ctx.reply_skb)
3149 return retcode;
3150 if (retcode != NO_ERROR)
3151 goto out_nolock;
b411b363 3152
b30ab791 3153 device = adm_ctx.device;
3b98c0c2
LE
3154 memset(&args, 0, sizeof(args));
3155 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
f399002e 3156 err = new_c_uuid_parms_from_attrs(&args, info);
3b98c0c2
LE
3157 if (err) {
3158 retcode = ERR_MANDATORY_TAG;
3159 drbd_msg_put_info(from_attrs_err_to_txt(err));
3160 goto out_nolock;
3161 }
b411b363
PR
3162 }
3163
b30ab791 3164 mutex_lock(device->state_mutex); /* Protects us against serialized state changes. */
b411b363 3165
b30ab791 3166 if (!get_ldev(device)) {
b411b363
PR
3167 retcode = ERR_NO_DISK;
3168 goto out;
3169 }
3170
3171 /* this is "skip initial sync", assume to be clean */
a6b32bc3
AG
3172 if (device->state.conn == C_CONNECTED &&
3173 first_peer_device(device)->connection->agreed_pro_version >= 90 &&
b30ab791 3174 device->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
b411b363
PR
3175 dev_info(DEV, "Preparing to skip initial sync\n");
3176 skip_initial_sync = 1;
b30ab791 3177 } else if (device->state.conn != C_STANDALONE) {
b411b363
PR
3178 retcode = ERR_CONNECTED;
3179 goto out_dec;
3180 }
3181
b30ab791
AG
3182 drbd_uuid_set(device, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
3183 drbd_uuid_new_current(device); /* New current, previous to UI_BITMAP */
b411b363
PR
3184
3185 if (args.clear_bm) {
b30ab791 3186 err = drbd_bitmap_io(device, &drbd_bmio_clear_n_write,
20ceb2b2 3187 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
b411b363
PR
3188 if (err) {
3189 dev_err(DEV, "Writing bitmap failed with %d\n",err);
3190 retcode = ERR_IO_MD_DISK;
3191 }
3192 if (skip_initial_sync) {
b30ab791
AG
3193 drbd_send_uuids_skip_initial_sync(device);
3194 _drbd_uuid_set(device, UI_BITMAP, 0);
3195 drbd_print_uuids(device, "cleared bitmap UUID");
a6b32bc3 3196 spin_lock_irq(&first_peer_device(device)->connection->req_lock);
b30ab791 3197 _drbd_set_state(_NS2(device, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
b411b363 3198 CS_VERBOSE, NULL);
a6b32bc3 3199 spin_unlock_irq(&first_peer_device(device)->connection->req_lock);
b411b363
PR
3200 }
3201 }
3202
b30ab791 3203 drbd_md_sync(device);
b411b363 3204out_dec:
b30ab791 3205 put_ldev(device);
b411b363 3206out:
b30ab791 3207 mutex_unlock(device->state_mutex);
3b98c0c2
LE
3208out_nolock:
3209 drbd_adm_finish(info, retcode);
b411b363
PR
3210 return 0;
3211}
3212
3b98c0c2 3213static enum drbd_ret_code
7c3063cc 3214drbd_check_resource_name(const char *name)
b411b363 3215{
3b98c0c2 3216 if (!name || !name[0]) {
7c3063cc 3217 drbd_msg_put_info("resource name missing");
3b98c0c2 3218 return ERR_MANDATORY_TAG;
b411b363 3219 }
3b98c0c2
LE
3220 /* if we want to use these in sysfs/configfs/debugfs some day,
3221 * we must not allow slashes */
3222 if (strchr(name, '/')) {
7c3063cc 3223 drbd_msg_put_info("invalid resource name");
3b98c0c2 3224 return ERR_INVALID_REQUEST;
b411b363 3225 }
3b98c0c2 3226 return NO_ERROR;
774b3055 3227}
b411b363 3228
789c1b62 3229int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
b411b363 3230{
3b98c0c2 3231 enum drbd_ret_code retcode;
afbbfa88
AG
3232 struct res_opts res_opts;
3233 int err;
b411b363 3234
3b98c0c2
LE
3235 retcode = drbd_adm_prepare(skb, info, 0);
3236 if (!adm_ctx.reply_skb)
3237 return retcode;
3238 if (retcode != NO_ERROR)
3239 goto out;
b411b363 3240
afbbfa88
AG
3241 set_res_opts_defaults(&res_opts);
3242 err = res_opts_from_attrs(&res_opts, info);
3243 if (err && err != -ENOMSG) {
3244 retcode = ERR_MANDATORY_TAG;
3245 drbd_msg_put_info(from_attrs_err_to_txt(err));
3246 goto out;
b411b363
PR
3247 }
3248
7c3063cc 3249 retcode = drbd_check_resource_name(adm_ctx.resource_name);
3b98c0c2
LE
3250 if (retcode != NO_ERROR)
3251 goto out;
b411b363 3252
bde89a9e 3253 if (adm_ctx.connection) {
38f19616
LE
3254 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
3255 retcode = ERR_INVALID_REQUEST;
789c1b62 3256 drbd_msg_put_info("resource exists");
38f19616
LE
3257 }
3258 /* else: still NO_ERROR */
3b98c0c2 3259 goto out;
b411b363 3260 }
b411b363 3261
afbbfa88 3262 if (!conn_create(adm_ctx.resource_name, &res_opts))
b411b363 3263 retcode = ERR_NOMEM;
3b98c0c2
LE
3264out:
3265 drbd_adm_finish(info, retcode);
3266 return 0;
b411b363
PR
3267}
3268
05a10ec7 3269int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info)
b411b363 3270{
3b98c0c2
LE
3271 struct drbd_genlmsghdr *dh = info->userhdr;
3272 enum drbd_ret_code retcode;
b411b363 3273
44e52cfa 3274 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
3b98c0c2
LE
3275 if (!adm_ctx.reply_skb)
3276 return retcode;
3277 if (retcode != NO_ERROR)
3278 goto out;
b411b363 3279
f2257a56 3280 if (dh->minor > MINORMASK) {
3b98c0c2
LE
3281 drbd_msg_put_info("requested minor out of range");
3282 retcode = ERR_INVALID_REQUEST;
3283 goto out;
b411b363 3284 }
0c8e36d9 3285 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
3b98c0c2
LE
3286 drbd_msg_put_info("requested volume id out of range");
3287 retcode = ERR_INVALID_REQUEST;
3288 goto out;
b411b363 3289 }
b411b363 3290
38f19616 3291 /* drbd_adm_prepare made sure already
a6b32bc3 3292 * that first_peer_device(device)->connection and device->vnr match the request. */
b30ab791 3293 if (adm_ctx.device) {
38f19616
LE
3294 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
3295 retcode = ERR_MINOR_EXISTS;
3296 /* else: still NO_ERROR */
3297 goto out;
b411b363 3298 }
38f19616 3299
a6b32bc3 3300 retcode = drbd_create_minor(adm_ctx.connection, dh->minor, adm_ctx.volume);
3b98c0c2
LE
3301out:
3302 drbd_adm_finish(info, retcode);
3303 return 0;
b411b363
PR
3304}
3305
05a10ec7 3306static enum drbd_ret_code adm_del_minor(struct drbd_device *device)
b411b363 3307{
b30ab791
AG
3308 if (device->state.disk == D_DISKLESS &&
3309 /* no need to be device->state.conn == C_STANDALONE &&
85f75dd7
LE
3310 * we may want to delete a minor from a live replication group.
3311 */
b30ab791
AG
3312 device->state.role == R_SECONDARY) {
3313 _drbd_request_state(device, NS(conn, C_WF_REPORT_PARAMS),
369bea63 3314 CS_VERBOSE + CS_WAIT_COMPLETE);
a6b32bc3 3315 idr_remove(&first_peer_device(device)->connection->volumes, device->vnr);
05a10ec7 3316 idr_remove(&drbd_devices, device_to_minor(device));
b30ab791
AG
3317 destroy_workqueue(device->submit.wq);
3318 del_gendisk(device->vdisk);
81fa2e67 3319 synchronize_rcu();
05a10ec7 3320 kref_put(&device->kref, drbd_destroy_device);
85f75dd7
LE
3321 return NO_ERROR;
3322 } else
3323 return ERR_MINOR_CONFIGURED;
b411b363
PR
3324}
3325
05a10ec7 3326int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info)
b411b363 3327{
3b98c0c2 3328 enum drbd_ret_code retcode;
b411b363 3329
3b98c0c2
LE
3330 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3331 if (!adm_ctx.reply_skb)
3332 return retcode;
3333 if (retcode != NO_ERROR)
3334 goto out;
b411b363 3335
05a10ec7 3336 retcode = adm_del_minor(adm_ctx.device);
85f75dd7
LE
3337out:
3338 drbd_adm_finish(info, retcode);
3339 return 0;
b411b363
PR
3340}
3341
85f75dd7 3342int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
b411b363 3343{
f3dfa40a 3344 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
b30ab791 3345 struct drbd_device *device;
85f75dd7 3346 unsigned i;
b411b363 3347
85f75dd7
LE
3348 retcode = drbd_adm_prepare(skb, info, 0);
3349 if (!adm_ctx.reply_skb)
3350 return retcode;
3351 if (retcode != NO_ERROR)
3352 goto out;
b411b363 3353
bde89a9e 3354 if (!adm_ctx.connection) {
789c1b62 3355 retcode = ERR_RES_NOT_KNOWN;
85f75dd7 3356 goto out;
b411b363
PR
3357 }
3358
85f75dd7 3359 /* demote */
bde89a9e 3360 idr_for_each_entry(&adm_ctx.connection->volumes, device, i) {
b30ab791 3361 retcode = drbd_set_role(device, R_SECONDARY, 0);
85f75dd7
LE
3362 if (retcode < SS_SUCCESS) {
3363 drbd_msg_put_info("failed to demote");
c141ebda 3364 goto out;
85f75dd7 3365 }
b411b363 3366 }
b411b363 3367
bde89a9e 3368 retcode = conn_try_disconnect(adm_ctx.connection, 0);
f3dfa40a 3369 if (retcode < SS_SUCCESS) {
85f75dd7 3370 drbd_msg_put_info("failed to disconnect");
f3dfa40a 3371 goto out;
85f75dd7 3372 }
b411b363 3373
85f75dd7 3374 /* detach */
bde89a9e 3375 idr_for_each_entry(&adm_ctx.connection->volumes, device, i) {
b30ab791 3376 retcode = adm_detach(device, 0);
27012382 3377 if (retcode < SS_SUCCESS || retcode > NO_ERROR) {
85f75dd7 3378 drbd_msg_put_info("failed to detach");
c141ebda 3379 goto out;
85f75dd7
LE
3380 }
3381 }
b411b363 3382
bde89a9e 3383 /* If we reach this, all volumes (of this connection) are Secondary,
f3dfa40a 3384 * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
c141ebda 3385 * actually stopped, state handling only does drbd_thread_stop_nowait(). */
bde89a9e 3386 drbd_thread_stop(&adm_ctx.connection->worker);
b411b363 3387
f3dfa40a 3388 /* Now, nothing can fail anymore */
b411b363 3389
85f75dd7 3390 /* delete volumes */
bde89a9e 3391 idr_for_each_entry(&adm_ctx.connection->volumes, device, i) {
05a10ec7 3392 retcode = adm_del_minor(device);
85f75dd7
LE
3393 if (retcode != NO_ERROR) {
3394 /* "can not happen" */
3395 drbd_msg_put_info("failed to delete volume");
ef356262 3396 goto out;
85f75dd7
LE
3397 }
3398 }
b411b363 3399
85f75dd7 3400 /* delete connection */
bde89a9e
AG
3401 if (conn_lowest_minor(adm_ctx.connection) < 0) {
3402 list_del_rcu(&adm_ctx.connection->connections);
ec0bddbc 3403 synchronize_rcu();
05a10ec7 3404 kref_put(&adm_ctx.connection->kref, drbd_destroy_connection);
b411b363 3405
85f75dd7
LE
3406 retcode = NO_ERROR;
3407 } else {
3408 /* "can not happen" */
789c1b62 3409 retcode = ERR_RES_IN_USE;
85f75dd7 3410 drbd_msg_put_info("failed to delete connection");
85f75dd7 3411 }
ef356262 3412 goto out;
3b98c0c2
LE
3413out:
3414 drbd_adm_finish(info, retcode);
3415 return 0;
b411b363
PR
3416}
3417
789c1b62 3418int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
b411b363 3419{
3b98c0c2 3420 enum drbd_ret_code retcode;
b411b363 3421
44e52cfa 3422 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
3b98c0c2
LE
3423 if (!adm_ctx.reply_skb)
3424 return retcode;
3425 if (retcode != NO_ERROR)
3426 goto out;
b411b363 3427
bde89a9e
AG
3428 if (conn_lowest_minor(adm_ctx.connection) < 0) {
3429 list_del_rcu(&adm_ctx.connection->connections);
ec0bddbc 3430 synchronize_rcu();
05a10ec7 3431 kref_put(&adm_ctx.connection->kref, drbd_destroy_connection);
9dc9fbb3 3432
3b98c0c2
LE
3433 retcode = NO_ERROR;
3434 } else {
789c1b62 3435 retcode = ERR_RES_IN_USE;
b411b363
PR
3436 }
3437
992d6e91 3438 if (retcode == NO_ERROR)
bde89a9e 3439 drbd_thread_stop(&adm_ctx.connection->worker);
3b98c0c2
LE
3440out:
3441 drbd_adm_finish(info, retcode);
b411b363
PR
3442 return 0;
3443}
3444
b30ab791 3445void drbd_bcast_event(struct drbd_device *device, const struct sib_info *sib)
b411b363 3446{
3b98c0c2
LE
3447 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
3448 struct sk_buff *msg;
3449 struct drbd_genlmsghdr *d_out;
3450 unsigned seq;
3451 int err = -ENOMEM;
3452
ef86b779 3453 if (sib->sib_reason == SIB_SYNC_PROGRESS) {
b30ab791
AG
3454 if (time_after(jiffies, device->rs_last_bcast + HZ))
3455 device->rs_last_bcast = jiffies;
ef86b779
PR
3456 else
3457 return;
3458 }
b411b363 3459
3b98c0c2
LE
3460 seq = atomic_inc_return(&drbd_genl_seq);
3461 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
3462 if (!msg)
3463 goto failed;
3464
3465 err = -EMSGSIZE;
3466 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
3467 if (!d_out) /* cannot happen, but anyways. */
3468 goto nla_put_failure;
b30ab791 3469 d_out->minor = device_to_minor(device);
6f9b5f84 3470 d_out->ret_code = NO_ERROR;
3b98c0c2 3471
b30ab791 3472 if (nla_put_status_info(msg, device, sib))
3b98c0c2
LE
3473 goto nla_put_failure;
3474 genlmsg_end(msg, d_out);
3475 err = drbd_genl_multicast_events(msg, 0);
3476 /* msg has been consumed or freed in netlink_broadcast() */
3477 if (err && err != -ESRCH)
3478 goto failed;
b411b363 3479
3b98c0c2 3480 return;
b411b363 3481
3b98c0c2
LE
3482nla_put_failure:
3483 nlmsg_free(msg);
3484failed:
3485 dev_err(DEV, "Error %d while broadcasting event. "
3486 "Event seq:%u sib_reason:%u\n",
3487 err, seq, sib->sib_reason);
b411b363 3488}