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