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