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