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