blob: 40de384aade6963e821333341bb9f293bf0a41c8 [file] [log] [blame]
Philipp Reisnerb411b362009-09-25 16:07:19 -07001/*
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
Philipp Reisnerb411b362009-09-25 16:07:19 -070026#include <linux/module.h>
27#include <linux/drbd.h>
28#include <linux/in.h>
29#include <linux/fs.h>
30#include <linux/file.h>
31#include <linux/slab.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070032#include <linux/blkpg.h>
33#include <linux/cpumask.h>
34#include "drbd_int.h"
Philipp Reisner265be2d2010-05-31 10:14:17 +020035#include "drbd_req.h"
Philipp Reisnerb411b362009-09-25 16:07:19 -070036#include "drbd_wrappers.h"
37#include <asm/unaligned.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070038#include <linux/drbd_limits.h>
Philipp Reisner87f7be42010-06-11 13:56:33 +020039#include <linux/kthread.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070040
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010041#include <net/genetlink.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070042
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010043/* .doit */
44// int drbd_adm_create_resource(struct sk_buff *skb, struct genl_info *info);
45// int drbd_adm_delete_resource(struct sk_buff *skb, struct genl_info *info);
46
47int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info);
48int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info);
49
50int drbd_adm_create_connection(struct sk_buff *skb, struct genl_info *info);
51int drbd_adm_delete_connection(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +010052int drbd_adm_down(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010053
54int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info);
55int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info);
Lars Ellenbergf3990022011-03-23 14:31:09 +010056int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010057int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info);
58int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info);
Lars Ellenbergf3990022011-03-23 14:31:09 +010059int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010060int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info);
61int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info);
62int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info);
63int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info);
64int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info);
65int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info);
66int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info);
67int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info);
68int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info);
69int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info);
70int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info);
Lars Ellenbergf3990022011-03-23 14:31:09 +010071int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010072int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info);
73int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info);
74/* .dumpit */
75int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb);
76
77#include <linux/drbd_genl_api.h>
78#include <linux/genl_magic_func.h>
79
80/* used blkdev_get_by_path, to claim our meta data device(s) */
Philipp Reisnerb411b362009-09-25 16:07:19 -070081static char *drbd_m_holder = "Hands off! this is DRBD's meta data device.";
82
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010083/* Configuration is strictly serialized, because generic netlink message
84 * processing is strictly serialized by the genl_lock().
85 * Which means we can use one static global drbd_config_context struct.
86 */
87static struct drbd_config_context {
88 /* assigned from drbd_genlmsghdr */
89 unsigned int minor;
90 /* assigned from request attributes, if present */
91 unsigned int volume;
92#define VOLUME_UNSPECIFIED (-1U)
93 /* pointer into the request skb,
94 * limited lifetime! */
95 char *conn_name;
Philipp Reisnerb411b362009-09-25 16:07:19 -070096
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010097 /* reply buffer */
98 struct sk_buff *reply_skb;
99 /* pointer into reply buffer */
100 struct drbd_genlmsghdr *reply_dh;
101 /* resolved from attributes, if possible */
102 struct drbd_conf *mdev;
103 struct drbd_tconn *tconn;
104} adm_ctx;
105
106static void drbd_adm_send_reply(struct sk_buff *skb, struct genl_info *info)
107{
108 genlmsg_end(skb, genlmsg_data(nlmsg_data(nlmsg_hdr(skb))));
109 if (genlmsg_reply(skb, info))
110 printk(KERN_ERR "drbd: error sending genl reply\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700111}
112
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100113/* Used on a fresh "drbd_adm_prepare"d reply_skb, this cannot fail: The only
114 * reason it could fail was no space in skb, and there are 4k available. */
Lars Ellenberg8432b312011-03-08 16:11:16 +0100115int drbd_msg_put_info(const char *info)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100116{
117 struct sk_buff *skb = adm_ctx.reply_skb;
118 struct nlattr *nla;
119 int err = -EMSGSIZE;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700120
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100121 if (!info || !info[0])
122 return 0;
123
124 nla = nla_nest_start(skb, DRBD_NLA_CFG_REPLY);
125 if (!nla)
126 return err;
127
128 err = nla_put_string(skb, T_info_text, info);
129 if (err) {
130 nla_nest_cancel(skb, nla);
131 return err;
132 } else
133 nla_nest_end(skb, nla);
134 return 0;
135}
136
137/* This would be a good candidate for a "pre_doit" hook,
138 * and per-family private info->pointers.
139 * But we need to stay compatible with older kernels.
140 * If it returns successfully, adm_ctx members are valid.
141 */
142#define DRBD_ADM_NEED_MINOR 1
143#define DRBD_ADM_NEED_CONN 2
144static int drbd_adm_prepare(struct sk_buff *skb, struct genl_info *info,
145 unsigned flags)
146{
147 struct drbd_genlmsghdr *d_in = info->userhdr;
148 const u8 cmd = info->genlhdr->cmd;
149 int err;
150
151 memset(&adm_ctx, 0, sizeof(adm_ctx));
152
153 /* genl_rcv_msg only checks for CAP_NET_ADMIN on "GENL_ADMIN_PERM" :( */
154 if (cmd != DRBD_ADM_GET_STATUS
155 && security_netlink_recv(skb, CAP_SYS_ADMIN))
156 return -EPERM;
157
158 adm_ctx.reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
159 if (!adm_ctx.reply_skb)
160 goto fail;
161
162 adm_ctx.reply_dh = genlmsg_put_reply(adm_ctx.reply_skb,
163 info, &drbd_genl_family, 0, cmd);
164 /* put of a few bytes into a fresh skb of >= 4k will always succeed.
165 * but anyways */
166 if (!adm_ctx.reply_dh)
167 goto fail;
168
169 adm_ctx.reply_dh->minor = d_in->minor;
170 adm_ctx.reply_dh->ret_code = NO_ERROR;
171
172 if (info->attrs[DRBD_NLA_CFG_CONTEXT]) {
173 struct nlattr *nla;
174 /* parse and validate only */
Lars Ellenbergf3990022011-03-23 14:31:09 +0100175 err = drbd_cfg_context_from_attrs(NULL, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100176 if (err)
177 goto fail;
178
179 /* It was present, and valid,
180 * copy it over to the reply skb. */
181 err = nla_put_nohdr(adm_ctx.reply_skb,
182 info->attrs[DRBD_NLA_CFG_CONTEXT]->nla_len,
183 info->attrs[DRBD_NLA_CFG_CONTEXT]);
184 if (err)
185 goto fail;
186
187 /* and assign stuff to the global adm_ctx */
188 nla = nested_attr_tb[__nla_type(T_ctx_volume)];
189 adm_ctx.volume = nla ? nla_get_u32(nla) : VOLUME_UNSPECIFIED;
190 nla = nested_attr_tb[__nla_type(T_ctx_conn_name)];
191 if (nla)
192 adm_ctx.conn_name = nla_data(nla);
193 } else
194 adm_ctx.volume = VOLUME_UNSPECIFIED;
195
196 adm_ctx.minor = d_in->minor;
197 adm_ctx.mdev = minor_to_mdev(d_in->minor);
198 adm_ctx.tconn = conn_by_name(adm_ctx.conn_name);
199
200 if (!adm_ctx.mdev && (flags & DRBD_ADM_NEED_MINOR)) {
201 drbd_msg_put_info("unknown minor");
202 return ERR_MINOR_INVALID;
203 }
204 if (!adm_ctx.tconn && (flags & DRBD_ADM_NEED_CONN)) {
205 drbd_msg_put_info("unknown connection");
206 return ERR_INVALID_REQUEST;
207 }
208
209 /* some more paranoia, if the request was over-determined */
Lars Ellenberg527f4b22011-03-14 13:58:03 +0100210 if (adm_ctx.mdev && adm_ctx.tconn &&
211 adm_ctx.mdev->tconn != adm_ctx.tconn) {
212 pr_warning("request: minor=%u, conn=%s; but that minor belongs to connection %s\n",
213 adm_ctx.minor, adm_ctx.conn_name, adm_ctx.mdev->tconn->name);
214 drbd_msg_put_info("minor exists in different connection");
215 return ERR_INVALID_REQUEST;
216 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100217 if (adm_ctx.mdev &&
218 adm_ctx.volume != VOLUME_UNSPECIFIED &&
219 adm_ctx.volume != adm_ctx.mdev->vnr) {
220 pr_warning("request: minor=%u, volume=%u; but that minor is volume %u in %s\n",
221 adm_ctx.minor, adm_ctx.volume,
222 adm_ctx.mdev->vnr, adm_ctx.mdev->tconn->name);
Lars Ellenberg527f4b22011-03-14 13:58:03 +0100223 drbd_msg_put_info("minor exists as different volume");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100224 return ERR_INVALID_REQUEST;
225 }
Lars Ellenbergcffec5b2011-03-10 23:33:15 +0100226 if (adm_ctx.mdev && !adm_ctx.tconn)
227 adm_ctx.tconn = adm_ctx.mdev->tconn;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100228 return NO_ERROR;
229
230fail:
231 nlmsg_free(adm_ctx.reply_skb);
232 adm_ctx.reply_skb = NULL;
233 return -ENOMEM;
234}
235
236static int drbd_adm_finish(struct genl_info *info, int retcode)
237{
238 struct nlattr *nla;
239 const char *conn_name = NULL;
240
241 if (!adm_ctx.reply_skb)
242 return -ENOMEM;
243
244 adm_ctx.reply_dh->ret_code = retcode;
245
246 nla = info->attrs[DRBD_NLA_CFG_CONTEXT];
247 if (nla) {
248 nla = nla_find_nested(nla, __nla_type(T_ctx_conn_name));
249 if (nla)
250 conn_name = nla_data(nla);
251 }
252
253 drbd_adm_send_reply(adm_ctx.reply_skb, info);
254 return 0;
255}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700256
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100257static void setup_khelper_env(struct drbd_tconn *tconn, char **envp)
258{
259 char *afs;
260
261 if (get_net_conf(tconn)) {
262 switch (((struct sockaddr *)tconn->net_conf->peer_addr)->sa_family) {
263 case AF_INET6:
264 afs = "ipv6";
265 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
266 &((struct sockaddr_in6 *)tconn->net_conf->peer_addr)->sin6_addr);
267 break;
268 case AF_INET:
269 afs = "ipv4";
270 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
271 &((struct sockaddr_in *)tconn->net_conf->peer_addr)->sin_addr);
272 break;
273 default:
274 afs = "ssocks";
275 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
276 &((struct sockaddr_in *)tconn->net_conf->peer_addr)->sin_addr);
277 }
278 snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
279 put_net_conf(tconn);
280 }
281}
282
Philipp Reisnerb411b362009-09-25 16:07:19 -0700283int drbd_khelper(struct drbd_conf *mdev, char *cmd)
284{
285 char *envp[] = { "HOME=/",
286 "TERM=linux",
287 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100288 (char[20]) { }, /* address family */
289 (char[60]) { }, /* address */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700290 NULL };
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100291 char mb[12];
Philipp Reisnerb411b362009-09-25 16:07:19 -0700292 char *argv[] = {usermode_helper, cmd, mb, NULL };
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100293 struct sib_info sib;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700294 int ret;
295
296 snprintf(mb, 12, "minor-%d", mdev_to_minor(mdev));
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100297 setup_khelper_env(mdev->tconn, envp);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700298
Lars Ellenberg1090c052010-07-19 17:41:04 +0200299 /* The helper may take some time.
300 * write out any unsynced meta data changes now */
301 drbd_md_sync(mdev);
302
Philipp Reisnerb411b362009-09-25 16:07:19 -0700303 dev_info(DEV, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100304 sib.sib_reason = SIB_HELPER_PRE;
305 sib.helper_name = cmd;
306 drbd_bcast_event(mdev, &sib);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700307 ret = call_usermodehelper(usermode_helper, argv, envp, 1);
308 if (ret)
309 dev_warn(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
310 usermode_helper, cmd, mb,
311 (ret >> 8) & 0xff, ret);
312 else
313 dev_info(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
314 usermode_helper, cmd, mb,
315 (ret >> 8) & 0xff, ret);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100316 sib.sib_reason = SIB_HELPER_POST;
317 sib.helper_exit_code = ret;
318 drbd_bcast_event(mdev, &sib);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700319
320 if (ret < 0) /* Ignore any ERRNOs we got. */
321 ret = 0;
322
323 return ret;
324}
325
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100326static void conn_md_sync(struct drbd_tconn *tconn)
327{
328 struct drbd_conf *mdev;
Philipp Reisnere90285e2011-03-22 12:51:21 +0100329 int vnr;
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100330
Philipp Reisnerd3fcb492011-04-13 14:46:05 -0700331 down_read(&drbd_cfg_rwsem);
Philipp Reisnere90285e2011-03-22 12:51:21 +0100332 idr_for_each_entry(&tconn->volumes, mdev, vnr)
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100333 drbd_md_sync(mdev);
Philipp Reisnerd3fcb492011-04-13 14:46:05 -0700334 up_read(&drbd_cfg_rwsem);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100335}
336
337int conn_khelper(struct drbd_tconn *tconn, char *cmd)
338{
339 char *envp[] = { "HOME=/",
340 "TERM=linux",
341 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
342 (char[20]) { }, /* address family */
343 (char[60]) { }, /* address */
344 NULL };
345 char *argv[] = {usermode_helper, cmd, tconn->name, NULL };
346 int ret;
347
348 setup_khelper_env(tconn, envp);
349 conn_md_sync(tconn);
350
351 conn_info(tconn, "helper command: %s %s %s\n", usermode_helper, cmd, tconn->name);
352 /* TODO: conn_bcast_event() ?? */
353
354 ret = call_usermodehelper(usermode_helper, argv, envp, 1);
355 if (ret)
356 conn_warn(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
357 usermode_helper, cmd, tconn->name,
358 (ret >> 8) & 0xff, ret);
359 else
360 conn_info(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
361 usermode_helper, cmd, tconn->name,
362 (ret >> 8) & 0xff, ret);
363 /* TODO: conn_bcast_event() ?? */
364
365 if (ret < 0) /* Ignore any ERRNOs we got. */
366 ret = 0;
367
368 return ret;
369}
370
Philipp Reisnercb703452011-03-24 11:03:07 +0100371static enum drbd_fencing_p highest_fencing_policy(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700372{
Philipp Reisnercb703452011-03-24 11:03:07 +0100373 enum drbd_fencing_p fp = FP_NOT_AVAIL;
374 struct drbd_conf *mdev;
375 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700376
Philipp Reisner695d08f2011-04-11 22:53:32 -0700377 rcu_read_lock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100378 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
379 if (get_ldev_if_state(mdev, D_CONSISTENT)) {
380 fp = max_t(enum drbd_fencing_p, fp, mdev->ldev->dc.fencing);
381 put_ldev(mdev);
382 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700383 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700384 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700385
Philipp Reisnercb703452011-03-24 11:03:07 +0100386 return fp;
387}
388
389bool conn_try_outdate_peer(struct drbd_tconn *tconn)
390{
391 union drbd_state mask = { };
392 union drbd_state val = { };
393 enum drbd_fencing_p fp;
394 char *ex_to_string;
395 int r;
396
397 if (tconn->cstate >= C_WF_REPORT_PARAMS) {
398 conn_err(tconn, "Expected cstate < C_WF_REPORT_PARAMS\n");
399 return false;
400 }
401
402 fp = highest_fencing_policy(tconn);
403 switch (fp) {
404 case FP_NOT_AVAIL:
405 conn_warn(tconn, "Not fencing peer, I'm not even Consistent myself.\n");
406 goto out;
407 case FP_DONT_CARE:
408 return true;
409 default: ;
410 }
411
412 r = conn_khelper(tconn, "fence-peer");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700413
414 switch ((r>>8) & 0xff) {
415 case 3: /* peer is inconsistent */
416 ex_to_string = "peer is inconsistent or worse";
Philipp Reisnercb703452011-03-24 11:03:07 +0100417 mask.pdsk = D_MASK;
418 val.pdsk = D_INCONSISTENT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700419 break;
420 case 4: /* peer got outdated, or was already outdated */
421 ex_to_string = "peer was fenced";
Philipp Reisnercb703452011-03-24 11:03:07 +0100422 mask.pdsk = D_MASK;
423 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700424 break;
425 case 5: /* peer was down */
Philipp Reisnercb703452011-03-24 11:03:07 +0100426 if (conn_highest_disk(tconn) == D_UP_TO_DATE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700427 /* we will(have) create(d) a new UUID anyways... */
428 ex_to_string = "peer is unreachable, assumed to be dead";
Philipp Reisnercb703452011-03-24 11:03:07 +0100429 mask.pdsk = D_MASK;
430 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700431 } else {
432 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
Philipp Reisnerb411b362009-09-25 16:07:19 -0700433 }
434 break;
435 case 6: /* Peer is primary, voluntarily outdate myself.
436 * This is useful when an unconnected R_SECONDARY is asked to
437 * become R_PRIMARY, but finds the other peer being active. */
438 ex_to_string = "peer is active";
Philipp Reisnercb703452011-03-24 11:03:07 +0100439 conn_warn(tconn, "Peer is primary, outdating myself.\n");
440 mask.disk = D_MASK;
441 val.disk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700442 break;
443 case 7:
444 if (fp != FP_STONITH)
Philipp Reisnercb703452011-03-24 11:03:07 +0100445 conn_err(tconn, "fence-peer() = 7 && fencing != Stonith !!!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700446 ex_to_string = "peer was stonithed";
Philipp Reisnercb703452011-03-24 11:03:07 +0100447 mask.pdsk = D_MASK;
448 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700449 break;
450 default:
451 /* The script is broken ... */
Philipp Reisnercb703452011-03-24 11:03:07 +0100452 conn_err(tconn, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
453 return false; /* Eventually leave IO frozen */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700454 }
455
Philipp Reisnercb703452011-03-24 11:03:07 +0100456 conn_info(tconn, "fence-peer helper returned %d (%s)\n",
457 (r>>8) & 0xff, ex_to_string);
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200458
Philipp Reisnercb703452011-03-24 11:03:07 +0100459 out:
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200460
Philipp Reisnercb703452011-03-24 11:03:07 +0100461 /* Not using
462 conn_request_state(tconn, mask, val, CS_VERBOSE);
463 here, because we might were able to re-establish the connection in the
464 meantime. */
465 spin_lock_irq(&tconn->req_lock);
466 if (tconn->cstate < C_WF_REPORT_PARAMS)
467 _conn_request_state(tconn, mask, val, CS_VERBOSE);
468 spin_unlock_irq(&tconn->req_lock);
469
470 return conn_highest_pdsk(tconn) <= D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700471}
472
Philipp Reisner87f7be42010-06-11 13:56:33 +0200473static int _try_outdate_peer_async(void *data)
474{
Philipp Reisnercb703452011-03-24 11:03:07 +0100475 struct drbd_tconn *tconn = (struct drbd_tconn *)data;
Philipp Reisner87f7be42010-06-11 13:56:33 +0200476
Philipp Reisnercb703452011-03-24 11:03:07 +0100477 conn_try_outdate_peer(tconn);
Philipp Reisner87f7be42010-06-11 13:56:33 +0200478
479 return 0;
480}
481
Philipp Reisnercb703452011-03-24 11:03:07 +0100482void conn_try_outdate_peer_async(struct drbd_tconn *tconn)
Philipp Reisner87f7be42010-06-11 13:56:33 +0200483{
484 struct task_struct *opa;
485
Philipp Reisnercb703452011-03-24 11:03:07 +0100486 opa = kthread_run(_try_outdate_peer_async, tconn, "drbd_async_h");
Philipp Reisner87f7be42010-06-11 13:56:33 +0200487 if (IS_ERR(opa))
Philipp Reisnercb703452011-03-24 11:03:07 +0100488 conn_err(tconn, "out of mem, failed to invoke fence-peer helper\n");
Philipp Reisner87f7be42010-06-11 13:56:33 +0200489}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700490
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100491enum drbd_state_rv
492drbd_set_role(struct drbd_conf *mdev, enum drbd_role new_role, int force)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700493{
494 const int max_tries = 4;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100495 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700496 int try = 0;
497 int forced = 0;
498 union drbd_state mask, val;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700499
500 if (new_role == R_PRIMARY)
Philipp Reisner0625ac12011-02-07 14:49:19 +0100501 request_ping(mdev->tconn); /* Detect a dead peer ASAP */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700502
Philipp Reisner8410da8f02011-02-11 20:11:10 +0100503 mutex_lock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700504
505 mask.i = 0; mask.role = R_MASK;
506 val.i = 0; val.role = new_role;
507
508 while (try++ < max_tries) {
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100509 rv = _drbd_request_state(mdev, mask, val, CS_WAIT_COMPLETE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700510
511 /* in case we first succeeded to outdate,
512 * but now suddenly could establish a connection */
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100513 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700514 val.pdsk = 0;
515 mask.pdsk = 0;
516 continue;
517 }
518
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100519 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
Philipp Reisnerd10a33c2010-03-04 15:11:39 +0100520 (mdev->state.disk < D_UP_TO_DATE &&
521 mdev->state.disk >= D_INCONSISTENT)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700522 mask.disk = D_MASK;
523 val.disk = D_UP_TO_DATE;
524 forced = 1;
525 continue;
526 }
527
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100528 if (rv == SS_NO_UP_TO_DATE_DISK &&
Philipp Reisnerb411b362009-09-25 16:07:19 -0700529 mdev->state.disk == D_CONSISTENT && mask.pdsk == 0) {
530 D_ASSERT(mdev->state.pdsk == D_UNKNOWN);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700531
Philipp Reisnercb703452011-03-24 11:03:07 +0100532 if (conn_try_outdate_peer(mdev->tconn)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700533 val.disk = D_UP_TO_DATE;
534 mask.disk = D_MASK;
535 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700536 continue;
537 }
538
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100539 if (rv == SS_NOTHING_TO_DO)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100540 goto out;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100541 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
Philipp Reisnercb703452011-03-24 11:03:07 +0100542 if (!conn_try_outdate_peer(mdev->tconn) && force) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700543 dev_warn(DEV, "Forced into split brain situation!\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100544 mask.pdsk = D_MASK;
545 val.pdsk = D_OUTDATED;
546
Philipp Reisnerb411b362009-09-25 16:07:19 -0700547 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700548 continue;
549 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100550 if (rv == SS_TWO_PRIMARIES) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700551 /* Maybe the peer is detected as dead very soon...
552 retry at most once more in this case. */
Philipp Reisner89e58e72011-01-19 13:12:45 +0100553 schedule_timeout_interruptible((mdev->tconn->net_conf->ping_timeo+1)*HZ/10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700554 if (try < max_tries)
555 try = max_tries - 1;
556 continue;
557 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100558 if (rv < SS_SUCCESS) {
559 rv = _drbd_request_state(mdev, mask, val,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700560 CS_VERBOSE + CS_WAIT_COMPLETE);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100561 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100562 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700563 }
564 break;
565 }
566
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100567 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100568 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700569
570 if (forced)
571 dev_warn(DEV, "Forced to consider local data as UpToDate!\n");
572
573 /* Wait until nothing is on the fly :) */
574 wait_event(mdev->misc_wait, atomic_read(&mdev->ap_pending_cnt) == 0);
575
576 if (new_role == R_SECONDARY) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100577 set_disk_ro(mdev->vdisk, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700578 if (get_ldev(mdev)) {
579 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
580 put_ldev(mdev);
581 }
582 } else {
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +0100583 if (get_net_conf(mdev->tconn)) {
Philipp Reisner89e58e72011-01-19 13:12:45 +0100584 mdev->tconn->net_conf->want_lose = 0;
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +0100585 put_net_conf(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700586 }
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100587 set_disk_ro(mdev->vdisk, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700588 if (get_ldev(mdev)) {
589 if (((mdev->state.conn < C_CONNECTED ||
590 mdev->state.pdsk <= D_FAILED)
591 && mdev->ldev->md.uuid[UI_BITMAP] == 0) || forced)
592 drbd_uuid_new_current(mdev);
593
594 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
595 put_ldev(mdev);
596 }
597 }
598
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100599 /* writeout of activity log covered areas of the bitmap
600 * to stable storage done in after state change already */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700601
602 if (mdev->state.conn >= C_WF_REPORT_PARAMS) {
603 /* if this was forced, we should consider sync */
604 if (forced)
605 drbd_send_uuids(mdev);
606 drbd_send_state(mdev);
607 }
608
609 drbd_md_sync(mdev);
610
611 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100612out:
Philipp Reisner8410da8f02011-02-11 20:11:10 +0100613 mutex_unlock(mdev->state_mutex);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100614 return rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700615}
616
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100617static const char *from_attrs_err_to_txt(int err)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700618{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100619 return err == -ENOMSG ? "required attribute missing" :
620 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
Lars Ellenbergf3990022011-03-23 14:31:09 +0100621 err == -EEXIST ? "can not change invariant setting" :
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100622 "invalid attribute value";
Philipp Reisnerb411b362009-09-25 16:07:19 -0700623}
624
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100625int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700626{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100627 struct set_role_parms parms;
628 int err;
629 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700630
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100631 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
632 if (!adm_ctx.reply_skb)
633 return retcode;
634 if (retcode != NO_ERROR)
635 goto out;
636
637 memset(&parms, 0, sizeof(parms));
638 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +0100639 err = set_role_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100640 if (err) {
641 retcode = ERR_MANDATORY_TAG;
642 drbd_msg_put_info(from_attrs_err_to_txt(err));
643 goto out;
644 }
645 }
646
647 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
648 retcode = drbd_set_role(adm_ctx.mdev, R_PRIMARY, parms.assume_uptodate);
649 else
650 retcode = drbd_set_role(adm_ctx.mdev, R_SECONDARY, 0);
651out:
652 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700653 return 0;
654}
655
656/* initializes the md.*_offset members, so we are able to find
657 * the on disk meta data */
658static void drbd_md_set_sector_offsets(struct drbd_conf *mdev,
659 struct drbd_backing_dev *bdev)
660{
661 sector_t md_size_sect = 0;
662 switch (bdev->dc.meta_dev_idx) {
663 default:
664 /* v07 style fixed size indexed meta data */
665 bdev->md.md_size_sect = MD_RESERVED_SECT;
666 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
667 bdev->md.al_offset = MD_AL_OFFSET;
668 bdev->md.bm_offset = MD_BM_OFFSET;
669 break;
670 case DRBD_MD_INDEX_FLEX_EXT:
671 /* just occupy the full device; unit: sectors */
672 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
673 bdev->md.md_offset = 0;
674 bdev->md.al_offset = MD_AL_OFFSET;
675 bdev->md.bm_offset = MD_BM_OFFSET;
676 break;
677 case DRBD_MD_INDEX_INTERNAL:
678 case DRBD_MD_INDEX_FLEX_INT:
679 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
680 /* al size is still fixed */
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100681 bdev->md.al_offset = -MD_AL_SECTORS;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700682 /* we need (slightly less than) ~ this much bitmap sectors: */
683 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
684 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
685 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
686 md_size_sect = ALIGN(md_size_sect, 8);
687
688 /* plus the "drbd meta data super block",
689 * and the activity log; */
690 md_size_sect += MD_BM_OFFSET;
691
692 bdev->md.md_size_sect = md_size_sect;
693 /* bitmap offset is adjusted by 'super' block size */
694 bdev->md.bm_offset = -md_size_sect + MD_AL_OFFSET;
695 break;
696 }
697}
698
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100699/* input size is expected to be in KB */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700700char *ppsize(char *buf, unsigned long long size)
701{
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100702 /* Needs 9 bytes at max including trailing NUL:
703 * -1ULL ==> "16384 EB" */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700704 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
705 int base = 0;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100706 while (size >= 10000 && base < sizeof(units)-1) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700707 /* shift + round */
708 size = (size >> 10) + !!(size & (1<<9));
709 base++;
710 }
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100711 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700712
713 return buf;
714}
715
716/* there is still a theoretical deadlock when called from receiver
717 * on an D_INCONSISTENT R_PRIMARY:
718 * remote READ does inc_ap_bio, receiver would need to receive answer
719 * packet from remote to dec_ap_bio again.
720 * receiver receive_sizes(), comes here,
721 * waits for ap_bio_cnt == 0. -> deadlock.
722 * but this cannot happen, actually, because:
723 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
724 * (not connected, or bad/no disk on peer):
725 * see drbd_fail_request_early, ap_bio_cnt is zero.
726 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
727 * peer may not initiate a resize.
728 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100729/* Note these are not to be confused with
730 * drbd_adm_suspend_io/drbd_adm_resume_io,
731 * which are (sub) state changes triggered by admin (drbdsetup),
732 * and can be long lived.
733 * This changes an mdev->flag, is triggered by drbd internals,
734 * and should be short-lived. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700735void drbd_suspend_io(struct drbd_conf *mdev)
736{
737 set_bit(SUSPEND_IO, &mdev->flags);
Philipp Reisner2aebfab2011-03-28 16:48:11 +0200738 if (drbd_suspended(mdev))
Philipp Reisner265be2d2010-05-31 10:14:17 +0200739 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700740 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
741}
742
743void drbd_resume_io(struct drbd_conf *mdev)
744{
745 clear_bit(SUSPEND_IO, &mdev->flags);
746 wake_up(&mdev->misc_wait);
747}
748
749/**
750 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
751 * @mdev: DRBD device.
752 *
753 * Returns 0 on success, negative return values indicate errors.
754 * You should call drbd_md_sync() after calling this function.
755 */
Bart Van Assche24c48302011-05-21 18:32:29 +0200756enum determine_dev_size drbd_determine_dev_size(struct drbd_conf *mdev, enum dds_flags flags) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700757{
758 sector_t prev_first_sect, prev_size; /* previous meta location */
759 sector_t la_size;
760 sector_t size;
761 char ppb[10];
762
763 int md_moved, la_size_changed;
764 enum determine_dev_size rv = unchanged;
765
766 /* race:
767 * application request passes inc_ap_bio,
768 * but then cannot get an AL-reference.
769 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
770 *
771 * to avoid that:
772 * Suspend IO right here.
773 * still lock the act_log to not trigger ASSERTs there.
774 */
775 drbd_suspend_io(mdev);
776
777 /* no wait necessary anymore, actually we could assert that */
778 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
779
780 prev_first_sect = drbd_md_first_sector(mdev->ldev);
781 prev_size = mdev->ldev->md.md_size_sect;
782 la_size = mdev->ldev->md.la_size_sect;
783
784 /* TODO: should only be some assert here, not (re)init... */
785 drbd_md_set_sector_offsets(mdev, mdev->ldev);
786
Philipp Reisnerd8450302010-03-24 15:51:26 +0100787 size = drbd_new_dev_size(mdev, mdev->ldev, flags & DDSF_FORCED);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700788
789 if (drbd_get_capacity(mdev->this_bdev) != size ||
790 drbd_bm_capacity(mdev) != size) {
791 int err;
Philipp Reisner02d9a942010-03-24 16:23:03 +0100792 err = drbd_bm_resize(mdev, size, !(flags & DDSF_NO_RESYNC));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700793 if (unlikely(err)) {
794 /* currently there is only one error: ENOMEM! */
795 size = drbd_bm_capacity(mdev)>>1;
796 if (size == 0) {
797 dev_err(DEV, "OUT OF MEMORY! "
798 "Could not allocate bitmap!\n");
799 } else {
800 dev_err(DEV, "BM resizing failed. "
801 "Leaving size unchanged at size = %lu KB\n",
802 (unsigned long)size);
803 }
804 rv = dev_size_error;
805 }
806 /* racy, see comments above. */
807 drbd_set_my_capacity(mdev, size);
808 mdev->ldev->md.la_size_sect = size;
809 dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
810 (unsigned long long)size>>1);
811 }
812 if (rv == dev_size_error)
813 goto out;
814
815 la_size_changed = (la_size != mdev->ldev->md.la_size_sect);
816
817 md_moved = prev_first_sect != drbd_md_first_sector(mdev->ldev)
818 || prev_size != mdev->ldev->md.md_size_sect;
819
820 if (la_size_changed || md_moved) {
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100821 int err;
822
Philipp Reisnerb411b362009-09-25 16:07:19 -0700823 drbd_al_shrink(mdev); /* All extents inactive. */
824 dev_info(DEV, "Writing the whole bitmap, %s\n",
825 la_size_changed && md_moved ? "size changed and md moved" :
826 la_size_changed ? "size changed" : "md moved");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100827 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
828 err = drbd_bitmap_io(mdev, &drbd_bm_write,
829 "size changed", BM_LOCKED_MASK);
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100830 if (err) {
831 rv = dev_size_error;
832 goto out;
833 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700834 drbd_md_mark_dirty(mdev);
835 }
836
837 if (size > la_size)
838 rv = grew;
839 if (size < la_size)
840 rv = shrunk;
841out:
842 lc_unlock(mdev->act_log);
843 wake_up(&mdev->al_wait);
844 drbd_resume_io(mdev);
845
846 return rv;
847}
848
849sector_t
Philipp Reisnera393db62009-12-22 13:35:52 +0100850drbd_new_dev_size(struct drbd_conf *mdev, struct drbd_backing_dev *bdev, int assume_peer_has_space)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700851{
852 sector_t p_size = mdev->p_size; /* partner's disk size. */
853 sector_t la_size = bdev->md.la_size_sect; /* last agreed size. */
854 sector_t m_size; /* my size */
855 sector_t u_size = bdev->dc.disk_size; /* size requested by user. */
856 sector_t size = 0;
857
858 m_size = drbd_get_max_capacity(bdev);
859
Philipp Reisnera393db62009-12-22 13:35:52 +0100860 if (mdev->state.conn < C_CONNECTED && assume_peer_has_space) {
861 dev_warn(DEV, "Resize while not connected was forced by the user!\n");
862 p_size = m_size;
863 }
864
Philipp Reisnerb411b362009-09-25 16:07:19 -0700865 if (p_size && m_size) {
866 size = min_t(sector_t, p_size, m_size);
867 } else {
868 if (la_size) {
869 size = la_size;
870 if (m_size && m_size < size)
871 size = m_size;
872 if (p_size && p_size < size)
873 size = p_size;
874 } else {
875 if (m_size)
876 size = m_size;
877 if (p_size)
878 size = p_size;
879 }
880 }
881
882 if (size == 0)
883 dev_err(DEV, "Both nodes diskless!\n");
884
885 if (u_size) {
886 if (u_size > size)
887 dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n",
888 (unsigned long)u_size>>1, (unsigned long)size>>1);
889 else
890 size = u_size;
891 }
892
893 return size;
894}
895
896/**
897 * drbd_check_al_size() - Ensures that the AL is of the right size
898 * @mdev: DRBD device.
899 *
900 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
901 * failed, and 0 on success. You should call drbd_md_sync() after you called
902 * this function.
903 */
Lars Ellenbergf3990022011-03-23 14:31:09 +0100904static int drbd_check_al_size(struct drbd_conf *mdev, struct disk_conf *dc)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700905{
906 struct lru_cache *n, *t;
907 struct lc_element *e;
908 unsigned int in_use;
909 int i;
910
Lars Ellenbergf3990022011-03-23 14:31:09 +0100911 if (!expect(dc->al_extents >= DRBD_AL_EXTENTS_MIN))
912 dc->al_extents = DRBD_AL_EXTENTS_MIN;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700913
914 if (mdev->act_log &&
Lars Ellenbergf3990022011-03-23 14:31:09 +0100915 mdev->act_log->nr_elements == dc->al_extents)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700916 return 0;
917
918 in_use = 0;
919 t = mdev->act_log;
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100920 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
Lars Ellenbergf3990022011-03-23 14:31:09 +0100921 dc->al_extents, sizeof(struct lc_element), 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700922
923 if (n == NULL) {
924 dev_err(DEV, "Cannot allocate act_log lru!\n");
925 return -ENOMEM;
926 }
927 spin_lock_irq(&mdev->al_lock);
928 if (t) {
929 for (i = 0; i < t->nr_elements; i++) {
930 e = lc_element_by_index(t, i);
931 if (e->refcnt)
932 dev_err(DEV, "refcnt(%d)==%d\n",
933 e->lc_number, e->refcnt);
934 in_use += e->refcnt;
935 }
936 }
937 if (!in_use)
938 mdev->act_log = n;
939 spin_unlock_irq(&mdev->al_lock);
940 if (in_use) {
941 dev_err(DEV, "Activity log still in use!\n");
942 lc_destroy(n);
943 return -EBUSY;
944 } else {
945 if (t)
946 lc_destroy(t);
947 }
948 drbd_md_mark_dirty(mdev); /* we changed mdev->act_log->nr_elemens */
949 return 0;
950}
951
Philipp Reisner99432fc2011-05-20 16:39:13 +0200952static void drbd_setup_queue_param(struct drbd_conf *mdev, unsigned int max_bio_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700953{
954 struct request_queue * const q = mdev->rq_queue;
Philipp Reisner99432fc2011-05-20 16:39:13 +0200955 int max_hw_sectors = max_bio_size >> 9;
956 int max_segments = 0;
957
958 if (get_ldev_if_state(mdev, D_ATTACHING)) {
959 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
960
961 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
962 max_segments = mdev->ldev->dc.max_bio_bvecs;
963 put_ldev(mdev);
964 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700965
Philipp Reisnerb411b362009-09-25 16:07:19 -0700966 blk_queue_logical_block_size(q, 512);
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100967 blk_queue_max_hw_sectors(q, max_hw_sectors);
968 /* This is the workaround for "bio would need to, but cannot, be split" */
969 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
970 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700971
Philipp Reisner99432fc2011-05-20 16:39:13 +0200972 if (get_ldev_if_state(mdev, D_ATTACHING)) {
973 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700974
Philipp Reisner99432fc2011-05-20 16:39:13 +0200975 blk_queue_stack_limits(q, b);
976
977 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
978 dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
979 q->backing_dev_info.ra_pages,
980 b->backing_dev_info.ra_pages);
981 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
982 }
983 put_ldev(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700984 }
985}
986
Philipp Reisner99432fc2011-05-20 16:39:13 +0200987void drbd_reconsider_max_bio_size(struct drbd_conf *mdev)
988{
989 int now, new, local, peer;
990
991 now = queue_max_hw_sectors(mdev->rq_queue) << 9;
992 local = mdev->local_max_bio_size; /* Eventually last known value, from volatile memory */
993 peer = mdev->peer_max_bio_size; /* Eventually last known value, from meta data */
994
995 if (get_ldev_if_state(mdev, D_ATTACHING)) {
996 local = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
997 mdev->local_max_bio_size = local;
998 put_ldev(mdev);
999 }
1000
1001 /* We may ignore peer limits if the peer is modern enough.
1002 Because new from 8.3.8 onwards the peer can use multiple
1003 BIOs for a single peer_request */
1004 if (mdev->state.conn >= C_CONNECTED) {
Philipp Reisner31890f42011-01-19 14:12:51 +01001005 if (mdev->tconn->agreed_pro_version < 94)
Philipp Reisner99432fc2011-05-20 16:39:13 +02001006 peer = mdev->peer_max_bio_size;
Philipp Reisner31890f42011-01-19 14:12:51 +01001007 else if (mdev->tconn->agreed_pro_version == 94)
Philipp Reisner99432fc2011-05-20 16:39:13 +02001008 peer = DRBD_MAX_SIZE_H80_PACKET;
1009 else /* drbd 8.3.8 onwards */
1010 peer = DRBD_MAX_BIO_SIZE;
1011 }
1012
1013 new = min_t(int, local, peer);
1014
1015 if (mdev->state.role == R_PRIMARY && new < now)
1016 dev_err(DEV, "ASSERT FAILED new < now; (%d < %d)\n", new, now);
1017
1018 if (new != now)
1019 dev_info(DEV, "max BIO size = %u\n", new);
1020
1021 drbd_setup_queue_param(mdev, new);
1022}
1023
Philipp Reisnerb411b362009-09-25 16:07:19 -07001024/* serialize deconfig (worker exiting, doing cleanup)
1025 * and reconfig (drbdsetup disk, drbdsetup net)
1026 *
Lars Ellenbergc518d042010-09-01 09:50:23 +02001027 * Wait for a potentially exiting worker, then restart it,
1028 * or start a new one. Flush any pending work, there may still be an
1029 * after_state_change queued.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001030 */
Philipp Reisner0e29d162011-02-18 14:23:11 +01001031static void conn_reconfig_start(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001032{
Philipp Reisner0e29d162011-02-18 14:23:11 +01001033 wait_event(tconn->ping_wait, !test_and_set_bit(CONFIG_PENDING, &tconn->flags));
1034 wait_event(tconn->ping_wait, !test_bit(OBJECT_DYING, &tconn->flags));
1035 drbd_thread_start(&tconn->worker);
1036 conn_flush_workqueue(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001037}
1038
1039/* if still unconfigured, stops worker again.
1040 * if configured now, clears CONFIG_PENDING.
1041 * wakes potential waiters */
Philipp Reisner0e29d162011-02-18 14:23:11 +01001042static void conn_reconfig_done(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001043{
Philipp Reisner0e29d162011-02-18 14:23:11 +01001044 spin_lock_irq(&tconn->req_lock);
1045 if (conn_all_vols_unconf(tconn)) {
1046 set_bit(OBJECT_DYING, &tconn->flags);
1047 drbd_thread_stop_nowait(&tconn->worker);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001048 } else
Philipp Reisner0e29d162011-02-18 14:23:11 +01001049 clear_bit(CONFIG_PENDING, &tconn->flags);
1050 spin_unlock_irq(&tconn->req_lock);
1051 wake_up(&tconn->ping_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001052}
1053
Philipp Reisner07782862010-08-31 12:00:50 +02001054/* Make sure IO is suspended before calling this function(). */
1055static void drbd_suspend_al(struct drbd_conf *mdev)
1056{
1057 int s = 0;
1058
Lars Ellenberg61610422011-02-21 13:20:54 +01001059 if (!lc_try_lock(mdev->act_log)) {
Philipp Reisner07782862010-08-31 12:00:50 +02001060 dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n");
1061 return;
1062 }
1063
Lars Ellenberg61610422011-02-21 13:20:54 +01001064 drbd_al_shrink(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001065 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner07782862010-08-31 12:00:50 +02001066 if (mdev->state.conn < C_CONNECTED)
1067 s = !test_and_set_bit(AL_SUSPENDED, &mdev->flags);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001068 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg61610422011-02-21 13:20:54 +01001069 lc_unlock(mdev->act_log);
Philipp Reisner07782862010-08-31 12:00:50 +02001070
1071 if (s)
1072 dev_info(DEV, "Suspended AL updates\n");
1073}
1074
Lars Ellenbergf3990022011-03-23 14:31:09 +01001075int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1076{
1077 enum drbd_ret_code retcode;
1078 struct drbd_conf *mdev;
1079 struct disk_conf *ndc; /* new disk conf */
1080 int err, fifo_size;
1081 int *rs_plan_s = NULL;
1082
1083 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1084 if (!adm_ctx.reply_skb)
1085 return retcode;
1086 if (retcode != NO_ERROR)
1087 goto out;
1088
1089 mdev = adm_ctx.mdev;
1090
1091 /* we also need a disk
1092 * to change the options on */
1093 if (!get_ldev(mdev)) {
1094 retcode = ERR_NO_DISK;
1095 goto out;
1096 }
1097
1098/* FIXME freeze IO, cluster wide.
1099 *
1100 * We should make sure no-one uses
1101 * some half-updated struct when we
1102 * assign it later. */
1103
1104 ndc = kmalloc(sizeof(*ndc), GFP_KERNEL);
1105 if (!ndc) {
1106 retcode = ERR_NOMEM;
1107 goto fail;
1108 }
1109
1110 memcpy(ndc, &mdev->ldev->dc, sizeof(*ndc));
1111 err = disk_conf_from_attrs_for_change(ndc, info);
1112 if (err) {
1113 retcode = ERR_MANDATORY_TAG;
1114 drbd_msg_put_info(from_attrs_err_to_txt(err));
1115 }
1116
1117 if (!expect(ndc->resync_rate >= 1))
1118 ndc->resync_rate = 1;
1119
1120 /* clip to allowed range */
1121 if (!expect(ndc->al_extents >= DRBD_AL_EXTENTS_MIN))
1122 ndc->al_extents = DRBD_AL_EXTENTS_MIN;
1123 if (!expect(ndc->al_extents <= DRBD_AL_EXTENTS_MAX))
1124 ndc->al_extents = DRBD_AL_EXTENTS_MAX;
1125
1126 /* most sanity checks done, try to assign the new sync-after
1127 * dependency. need to hold the global lock in there,
1128 * to avoid a race in the dependency loop check. */
1129 retcode = drbd_alter_sa(mdev, ndc->resync_after);
1130 if (retcode != NO_ERROR)
1131 goto fail;
1132
1133 fifo_size = (ndc->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
1134 if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) {
1135 rs_plan_s = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL);
1136 if (!rs_plan_s) {
1137 dev_err(DEV, "kmalloc of fifo_buffer failed");
1138 retcode = ERR_NOMEM;
1139 goto fail;
1140 }
1141 }
1142
1143 if (fifo_size != mdev->rs_plan_s.size) {
1144 kfree(mdev->rs_plan_s.values);
1145 mdev->rs_plan_s.values = rs_plan_s;
1146 mdev->rs_plan_s.size = fifo_size;
1147 mdev->rs_planed = 0;
1148 rs_plan_s = NULL;
1149 }
1150
1151 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
1152 drbd_al_shrink(mdev);
1153 err = drbd_check_al_size(mdev, ndc);
1154 lc_unlock(mdev->act_log);
1155 wake_up(&mdev->al_wait);
1156
1157 if (err) {
1158 retcode = ERR_NOMEM;
1159 goto fail;
1160 }
1161
1162 /* FIXME
1163 * To avoid someone looking at a half-updated struct, we probably
1164 * should have a rw-semaphor on net_conf and disk_conf.
1165 */
1166 mdev->ldev->dc = *ndc;
1167
1168 drbd_md_sync(mdev);
1169
1170
1171 if (mdev->state.conn >= C_CONNECTED)
1172 drbd_send_sync_param(mdev);
1173
1174 fail:
1175 put_ldev(mdev);
1176 kfree(ndc);
1177 kfree(rs_plan_s);
1178 out:
1179 drbd_adm_finish(info, retcode);
1180 return 0;
1181}
1182
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001183int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001184{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001185 struct drbd_conf *mdev;
1186 int err;
Andreas Gruenbacher116676c2010-12-08 13:33:11 +01001187 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001188 enum determine_dev_size dd;
1189 sector_t max_possible_sectors;
1190 sector_t min_md_device_sectors;
1191 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
Tejun Heoe525fd82010-11-13 11:55:17 +01001192 struct block_device *bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001193 struct lru_cache *resync_lru = NULL;
1194 union drbd_state ns, os;
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001195 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001196 int cp_discovered = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001197
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001198 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1199 if (!adm_ctx.reply_skb)
1200 return retcode;
1201 if (retcode != NO_ERROR)
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001202 goto finish;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001203
1204 mdev = adm_ctx.mdev;
Philipp Reisner0e29d162011-02-18 14:23:11 +01001205 conn_reconfig_start(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001206
1207 /* if you want to reconfigure, please tear down first */
1208 if (mdev->state.disk > D_DISKLESS) {
1209 retcode = ERR_DISK_CONFIGURED;
1210 goto fail;
1211 }
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001212 /* It may just now have detached because of IO error. Make sure
1213 * drbd_ldev_destroy is done already, we may end up here very fast,
1214 * e.g. if someone calls attach from the on-io-error handler,
1215 * to realize a "hot spare" feature (not that I'd recommend that) */
1216 wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001217
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001218 /* allocation not in the IO path, drbdsetup context */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001219 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1220 if (!nbc) {
1221 retcode = ERR_NOMEM;
1222 goto fail;
1223 }
1224
Lars Ellenbergf3990022011-03-23 14:31:09 +01001225 nbc->dc = (struct disk_conf) {
1226 {}, 0, /* backing_dev */
1227 {}, 0, /* meta_dev */
1228 0, /* meta_dev_idx */
1229 DRBD_DISK_SIZE_SECT_DEF, /* disk_size */
1230 DRBD_MAX_BIO_BVECS_DEF, /* max_bio_bvecs */
1231 DRBD_ON_IO_ERROR_DEF, /* on_io_error */
1232 DRBD_FENCING_DEF, /* fencing */
1233 DRBD_RATE_DEF, /* resync_rate */
1234 DRBD_AFTER_DEF, /* resync_after */
1235 DRBD_AL_EXTENTS_DEF, /* al_extents */
1236 DRBD_C_PLAN_AHEAD_DEF, /* c_plan_ahead */
1237 DRBD_C_DELAY_TARGET_DEF, /* c_delay_target */
1238 DRBD_C_FILL_TARGET_DEF, /* c_fill_target */
1239 DRBD_C_MAX_RATE_DEF, /* c_max_rate */
1240 DRBD_C_MIN_RATE_DEF, /* c_min_rate */
1241 0, /* no_disk_barrier */
1242 0, /* no_disk_flush */
1243 0, /* no_disk_drain */
1244 0, /* no_md_flush */
1245 };
Philipp Reisnerb411b362009-09-25 16:07:19 -07001246
Lars Ellenbergf3990022011-03-23 14:31:09 +01001247 err = disk_conf_from_attrs(&nbc->dc, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001248 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001249 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001250 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001251 goto fail;
1252 }
1253
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001254 if ((int)nbc->dc.meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001255 retcode = ERR_MD_IDX_INVALID;
1256 goto fail;
1257 }
1258
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01001259 if (get_net_conf(mdev->tconn)) {
Philipp Reisner89e58e72011-01-19 13:12:45 +01001260 int prot = mdev->tconn->net_conf->wire_protocol;
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01001261 put_net_conf(mdev->tconn);
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001262 if (nbc->dc.fencing == FP_STONITH && prot == DRBD_PROT_A) {
1263 retcode = ERR_STONITH_AND_PROT_A;
1264 goto fail;
1265 }
1266 }
1267
Tejun Heod4d77622010-11-13 11:55:18 +01001268 bdev = blkdev_get_by_path(nbc->dc.backing_dev,
1269 FMODE_READ | FMODE_WRITE | FMODE_EXCL, mdev);
Tejun Heoe525fd82010-11-13 11:55:17 +01001270 if (IS_ERR(bdev)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001271 dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.backing_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001272 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001273 retcode = ERR_OPEN_DISK;
1274 goto fail;
1275 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001276 nbc->backing_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001277
Tejun Heoe525fd82010-11-13 11:55:17 +01001278 /*
1279 * meta_dev_idx >= 0: external fixed size, possibly multiple
1280 * drbd sharing one meta device. TODO in that case, paranoia
1281 * check that [md_bdev, meta_dev_idx] is not yet used by some
1282 * other drbd minor! (if you use drbd.conf + drbdadm, that
1283 * should check it for you already; but if you don't, or
1284 * someone fooled it, we need to double check here)
1285 */
Tejun Heod4d77622010-11-13 11:55:18 +01001286 bdev = blkdev_get_by_path(nbc->dc.meta_dev,
1287 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001288 ((int)nbc->dc.meta_dev_idx < 0) ?
Tejun Heod4d77622010-11-13 11:55:18 +01001289 (void *)mdev : (void *)drbd_m_holder);
Tejun Heoe525fd82010-11-13 11:55:17 +01001290 if (IS_ERR(bdev)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001291 dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.meta_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001292 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001293 retcode = ERR_OPEN_MD_DISK;
1294 goto fail;
1295 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001296 nbc->md_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001297
Tejun Heoe525fd82010-11-13 11:55:17 +01001298 if ((nbc->backing_bdev == nbc->md_bdev) !=
1299 (nbc->dc.meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1300 nbc->dc.meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
1301 retcode = ERR_MD_IDX_INVALID;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001302 goto fail;
1303 }
1304
1305 resync_lru = lc_create("resync", drbd_bm_ext_cache,
Lars Ellenberg46a15bc2011-02-21 13:21:01 +01001306 1, 61, sizeof(struct bm_extent),
Philipp Reisnerb411b362009-09-25 16:07:19 -07001307 offsetof(struct bm_extent, lce));
1308 if (!resync_lru) {
1309 retcode = ERR_NOMEM;
Tejun Heoe525fd82010-11-13 11:55:17 +01001310 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001311 }
1312
1313 /* RT - for drbd_get_max_capacity() DRBD_MD_INDEX_FLEX_INT */
1314 drbd_md_set_sector_offsets(mdev, nbc);
1315
1316 if (drbd_get_max_capacity(nbc) < nbc->dc.disk_size) {
1317 dev_err(DEV, "max capacity %llu smaller than disk size %llu\n",
1318 (unsigned long long) drbd_get_max_capacity(nbc),
1319 (unsigned long long) nbc->dc.disk_size);
1320 retcode = ERR_DISK_TO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001321 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001322 }
1323
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001324 if ((int)nbc->dc.meta_dev_idx < 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001325 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1326 /* at least one MB, otherwise it does not make sense */
1327 min_md_device_sectors = (2<<10);
1328 } else {
1329 max_possible_sectors = DRBD_MAX_SECTORS;
1330 min_md_device_sectors = MD_RESERVED_SECT * (nbc->dc.meta_dev_idx + 1);
1331 }
1332
Philipp Reisnerb411b362009-09-25 16:07:19 -07001333 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
1334 retcode = ERR_MD_DISK_TO_SMALL;
1335 dev_warn(DEV, "refusing attach: md-device too small, "
1336 "at least %llu sectors needed for this meta-disk type\n",
1337 (unsigned long long) min_md_device_sectors);
Tejun Heoe525fd82010-11-13 11:55:17 +01001338 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001339 }
1340
1341 /* Make sure the new disk is big enough
1342 * (we may currently be R_PRIMARY with no local disk...) */
1343 if (drbd_get_max_capacity(nbc) <
1344 drbd_get_capacity(mdev->this_bdev)) {
1345 retcode = ERR_DISK_TO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001346 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001347 }
1348
1349 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1350
Lars Ellenberg13529942009-10-12 19:07:49 +02001351 if (nbc->known_size > max_possible_sectors) {
1352 dev_warn(DEV, "==> truncating very big lower level device "
1353 "to currently maximum possible %llu sectors <==\n",
1354 (unsigned long long) max_possible_sectors);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001355 if ((int)nbc->dc.meta_dev_idx >= 0)
Lars Ellenberg13529942009-10-12 19:07:49 +02001356 dev_warn(DEV, "==>> using internal or flexible "
1357 "meta data may help <<==\n");
1358 }
1359
Philipp Reisnerb411b362009-09-25 16:07:19 -07001360 drbd_suspend_io(mdev);
1361 /* also wait for the last barrier ack. */
Philipp Reisner2aebfab2011-03-28 16:48:11 +02001362 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_pending_cnt) || drbd_suspended(mdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001363 /* and for any other previously queued work */
Philipp Reisnera21e9292011-02-08 15:08:49 +01001364 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001365
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001366 rv = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE);
1367 retcode = rv; /* FIXME: Type mismatch. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001368 drbd_resume_io(mdev);
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001369 if (rv < SS_SUCCESS)
Tejun Heoe525fd82010-11-13 11:55:17 +01001370 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001371
1372 if (!get_ldev_if_state(mdev, D_ATTACHING))
1373 goto force_diskless;
1374
1375 drbd_md_set_sector_offsets(mdev, nbc);
1376
1377 if (!mdev->bitmap) {
1378 if (drbd_bm_init(mdev)) {
1379 retcode = ERR_NOMEM;
1380 goto force_diskless_dec;
1381 }
1382 }
1383
1384 retcode = drbd_md_read(mdev, nbc);
1385 if (retcode != NO_ERROR)
1386 goto force_diskless_dec;
1387
1388 if (mdev->state.conn < C_CONNECTED &&
1389 mdev->state.role == R_PRIMARY &&
1390 (mdev->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
1391 dev_err(DEV, "Can only attach to data with current UUID=%016llX\n",
1392 (unsigned long long)mdev->ed_uuid);
1393 retcode = ERR_DATA_NOT_CURRENT;
1394 goto force_diskless_dec;
1395 }
1396
1397 /* Since we are diskless, fix the activity log first... */
Lars Ellenbergf3990022011-03-23 14:31:09 +01001398 if (drbd_check_al_size(mdev, &nbc->dc)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001399 retcode = ERR_NOMEM;
1400 goto force_diskless_dec;
1401 }
1402
1403 /* Prevent shrinking of consistent devices ! */
1404 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
Philipp Reisnera393db62009-12-22 13:35:52 +01001405 drbd_new_dev_size(mdev, nbc, 0) < nbc->md.la_size_sect) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001406 dev_warn(DEV, "refusing to truncate a consistent device\n");
1407 retcode = ERR_DISK_TO_SMALL;
1408 goto force_diskless_dec;
1409 }
1410
1411 if (!drbd_al_read_log(mdev, nbc)) {
1412 retcode = ERR_IO_MD_DISK;
1413 goto force_diskless_dec;
1414 }
1415
Philipp Reisnerb411b362009-09-25 16:07:19 -07001416 /* Reset the "barriers don't work" bits here, then force meta data to
1417 * be written, to ensure we determine if barriers are supported. */
1418 if (nbc->dc.no_md_flush)
Philipp Reisnera8a4e512010-08-25 10:21:04 +02001419 set_bit(MD_NO_FUA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001420 else
Philipp Reisnera8a4e512010-08-25 10:21:04 +02001421 clear_bit(MD_NO_FUA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001422
1423 /* Point of no return reached.
1424 * Devices and memory are no longer released by error cleanup below.
1425 * now mdev takes over responsibility, and the state engine should
1426 * clean it up somewhere. */
1427 D_ASSERT(mdev->ldev == NULL);
1428 mdev->ldev = nbc;
1429 mdev->resync = resync_lru;
1430 nbc = NULL;
1431 resync_lru = NULL;
1432
Philipp Reisner2451fc32010-08-24 13:43:11 +02001433 mdev->write_ordering = WO_bdev_flush;
1434 drbd_bump_write_ordering(mdev, WO_bdev_flush);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001435
1436 if (drbd_md_test_flag(mdev->ldev, MDF_CRASHED_PRIMARY))
1437 set_bit(CRASHED_PRIMARY, &mdev->flags);
1438 else
1439 clear_bit(CRASHED_PRIMARY, &mdev->flags);
1440
Philipp Reisner894c6a92010-06-18 16:03:20 +02001441 if (drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
Philipp Reisnerda9fbc22011-03-29 10:52:01 +02001442 !(mdev->state.role == R_PRIMARY && mdev->tconn->susp_nod)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001443 set_bit(CRASHED_PRIMARY, &mdev->flags);
1444 cp_discovered = 1;
1445 }
1446
1447 mdev->send_cnt = 0;
1448 mdev->recv_cnt = 0;
1449 mdev->read_cnt = 0;
1450 mdev->writ_cnt = 0;
1451
Philipp Reisner99432fc2011-05-20 16:39:13 +02001452 drbd_reconsider_max_bio_size(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001453
1454 /* If I am currently not R_PRIMARY,
1455 * but meta data primary indicator is set,
1456 * I just now recover from a hard crash,
1457 * and have been R_PRIMARY before that crash.
1458 *
1459 * Now, if I had no connection before that crash
1460 * (have been degraded R_PRIMARY), chances are that
1461 * I won't find my peer now either.
1462 *
1463 * In that case, and _only_ in that case,
1464 * we use the degr-wfc-timeout instead of the default,
1465 * so we can automatically recover from a crash of a
1466 * degraded but active "cluster" after a certain timeout.
1467 */
1468 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
1469 if (mdev->state.role != R_PRIMARY &&
1470 drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
1471 !drbd_md_test_flag(mdev->ldev, MDF_CONNECTED_IND))
1472 set_bit(USE_DEGR_WFC_T, &mdev->flags);
1473
Bart Van Assche24c48302011-05-21 18:32:29 +02001474 dd = drbd_determine_dev_size(mdev, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001475 if (dd == dev_size_error) {
1476 retcode = ERR_NOMEM_BITMAP;
1477 goto force_diskless_dec;
1478 } else if (dd == grew)
1479 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
1480
1481 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
1482 dev_info(DEV, "Assuming that all blocks are out of sync "
1483 "(aka FullSync)\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001484 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write,
1485 "set_n_write from attaching", BM_LOCKED_MASK)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001486 retcode = ERR_IO_MD_DISK;
1487 goto force_diskless_dec;
1488 }
1489 } else {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001490 if (drbd_bitmap_io(mdev, &drbd_bm_read,
Andreas Gruenbacher22ab6a32010-12-13 01:44:11 +01001491 "read from attaching", BM_LOCKED_MASK)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001492 retcode = ERR_IO_MD_DISK;
1493 goto force_diskless_dec;
1494 }
1495 }
1496
1497 if (cp_discovered) {
1498 drbd_al_apply_to_bm(mdev);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001499 if (drbd_bitmap_io(mdev, &drbd_bm_write,
1500 "crashed primary apply AL", BM_LOCKED_MASK)) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001501 retcode = ERR_IO_MD_DISK;
1502 goto force_diskless_dec;
1503 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001504 }
1505
Philipp Reisner07782862010-08-31 12:00:50 +02001506 if (_drbd_bm_total_weight(mdev) == drbd_bm_bits(mdev))
1507 drbd_suspend_al(mdev); /* IO is still suspended here... */
1508
Philipp Reisner87eeee42011-01-19 14:16:30 +01001509 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner78bae592011-03-28 15:40:12 +02001510 os = drbd_read_state(mdev);
1511 ns = os;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001512 /* If MDF_CONSISTENT is not set go into inconsistent state,
1513 otherwise investigate MDF_WasUpToDate...
1514 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1515 otherwise into D_CONSISTENT state.
1516 */
1517 if (drbd_md_test_flag(mdev->ldev, MDF_CONSISTENT)) {
1518 if (drbd_md_test_flag(mdev->ldev, MDF_WAS_UP_TO_DATE))
1519 ns.disk = D_CONSISTENT;
1520 else
1521 ns.disk = D_OUTDATED;
1522 } else {
1523 ns.disk = D_INCONSISTENT;
1524 }
1525
1526 if (drbd_md_test_flag(mdev->ldev, MDF_PEER_OUT_DATED))
1527 ns.pdsk = D_OUTDATED;
1528
1529 if ( ns.disk == D_CONSISTENT &&
1530 (ns.pdsk == D_OUTDATED || mdev->ldev->dc.fencing == FP_DONT_CARE))
1531 ns.disk = D_UP_TO_DATE;
1532
1533 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1534 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1535 this point, because drbd_request_state() modifies these
1536 flags. */
1537
1538 /* In case we are C_CONNECTED postpone any decision on the new disk
1539 state after the negotiation phase. */
1540 if (mdev->state.conn == C_CONNECTED) {
1541 mdev->new_state_tmp.i = ns.i;
1542 ns.i = os.i;
1543 ns.disk = D_NEGOTIATING;
Philipp Reisnerdc66c742010-06-02 14:31:29 +02001544
1545 /* We expect to receive up-to-date UUIDs soon.
1546 To avoid a race in receive_state, free p_uuid while
1547 holding req_lock. I.e. atomic with the state change */
1548 kfree(mdev->p_uuid);
1549 mdev->p_uuid = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001550 }
1551
1552 rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001553 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001554
1555 if (rv < SS_SUCCESS)
1556 goto force_diskless_dec;
1557
1558 if (mdev->state.role == R_PRIMARY)
1559 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
1560 else
1561 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
1562
1563 drbd_md_mark_dirty(mdev);
1564 drbd_md_sync(mdev);
1565
1566 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1567 put_ldev(mdev);
Philipp Reisner0e29d162011-02-18 14:23:11 +01001568 conn_reconfig_done(mdev->tconn);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001569 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001570 return 0;
1571
1572 force_diskless_dec:
1573 put_ldev(mdev);
1574 force_diskless:
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001575 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001576 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001577 fail:
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001578 conn_reconfig_done(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001579 if (nbc) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001580 if (nbc->backing_bdev)
1581 blkdev_put(nbc->backing_bdev,
1582 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1583 if (nbc->md_bdev)
1584 blkdev_put(nbc->md_bdev,
1585 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001586 kfree(nbc);
1587 }
1588 lc_destroy(resync_lru);
1589
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001590 finish:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001591 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001592 return 0;
1593}
1594
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001595static int adm_detach(struct drbd_conf *mdev)
1596{
Philipp Reisner19f83c72011-03-29 14:21:03 +02001597 enum drbd_state_rv retcode;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001598 drbd_suspend_io(mdev); /* so no-one is stuck in drbd_al_begin_io */
1599 retcode = drbd_request_state(mdev, NS(disk, D_DISKLESS));
1600 wait_event(mdev->misc_wait,
1601 mdev->state.disk != D_DISKLESS ||
1602 !atomic_read(&mdev->local_cnt));
1603 drbd_resume_io(mdev);
1604 return retcode;
1605}
1606
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001607/* Detaching the disk is a process in multiple stages. First we need to lock
1608 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1609 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1610 * internal references as well.
1611 * Only then we have finally detached. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001612int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001613{
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001614 enum drbd_ret_code retcode;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001615
1616 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1617 if (!adm_ctx.reply_skb)
1618 return retcode;
1619 if (retcode != NO_ERROR)
1620 goto out;
1621
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001622 retcode = adm_detach(adm_ctx.mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001623out:
1624 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001625 return 0;
1626}
1627
Lars Ellenbergf3990022011-03-23 14:31:09 +01001628static bool conn_resync_running(struct drbd_tconn *tconn)
1629{
1630 struct drbd_conf *mdev;
Philipp Reisner695d08f2011-04-11 22:53:32 -07001631 bool rv = false;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001632 int vnr;
1633
Philipp Reisner695d08f2011-04-11 22:53:32 -07001634 rcu_read_lock();
Lars Ellenbergf3990022011-03-23 14:31:09 +01001635 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1636 if (mdev->state.conn == C_SYNC_SOURCE ||
1637 mdev->state.conn == C_SYNC_TARGET ||
1638 mdev->state.conn == C_PAUSED_SYNC_S ||
Philipp Reisner695d08f2011-04-11 22:53:32 -07001639 mdev->state.conn == C_PAUSED_SYNC_T) {
1640 rv = true;
1641 break;
1642 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001643 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001644 rcu_read_unlock();
1645
1646 return rv;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001647}
1648
1649static bool conn_ov_running(struct drbd_tconn *tconn)
1650{
1651 struct drbd_conf *mdev;
Philipp Reisner695d08f2011-04-11 22:53:32 -07001652 bool rv = false;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001653 int vnr;
1654
Philipp Reisner695d08f2011-04-11 22:53:32 -07001655 rcu_read_lock();
Lars Ellenbergf3990022011-03-23 14:31:09 +01001656 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1657 if (mdev->state.conn == C_VERIFY_S ||
Philipp Reisner695d08f2011-04-11 22:53:32 -07001658 mdev->state.conn == C_VERIFY_T) {
1659 rv = true;
1660 break;
1661 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001662 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001663 rcu_read_unlock();
1664
1665 return rv;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001666}
1667
Philipp Reisnercd643972011-04-13 18:00:59 -07001668static enum drbd_ret_code
1669check_net_options(struct drbd_tconn *tconn, struct net_conf *new_conf)
1670{
1671 struct drbd_conf *mdev;
1672 int i;
1673
1674 if (new_conf->two_primaries &&
1675 (new_conf->wire_protocol != DRBD_PROT_C))
1676 return ERR_NOT_PROTO_C;
1677
1678 rcu_read_lock();
1679 idr_for_each_entry(&tconn->volumes, mdev, i) {
1680 if (get_ldev(mdev)) {
1681 enum drbd_fencing_p fp = mdev->ldev->dc.fencing;
1682 put_ldev(mdev);
1683 if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH) {
1684 rcu_read_unlock();
1685 return ERR_STONITH_AND_PROT_A;
1686 }
1687 }
1688 if (mdev->state.role == R_PRIMARY && new_conf->want_lose) {
1689 rcu_read_unlock();
1690 return ERR_DISCARD;
1691 }
1692 if (!mdev->bitmap) {
1693 if(drbd_bm_init(mdev)) {
1694 rcu_read_unlock();
1695 return ERR_NOMEM;
1696 }
1697 }
1698 }
1699 rcu_read_unlock();
1700
1701 if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A)
1702 return ERR_CONG_NOT_PROTO_A;
1703
1704 return NO_ERROR;
1705}
1706
Lars Ellenbergf3990022011-03-23 14:31:09 +01001707int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
1708{
1709 enum drbd_ret_code retcode;
1710 struct drbd_tconn *tconn;
1711 struct net_conf *new_conf = NULL;
1712 int err;
1713 int ovr; /* online verify running */
1714 int rsr; /* re-sync running */
1715 struct crypto_hash *verify_tfm = NULL;
1716 struct crypto_hash *csums_tfm = NULL;
1717
1718
1719 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
1720 if (!adm_ctx.reply_skb)
1721 return retcode;
1722 if (retcode != NO_ERROR)
1723 goto out;
1724
1725 tconn = adm_ctx.tconn;
1726
1727 new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
1728 if (!new_conf) {
1729 retcode = ERR_NOMEM;
1730 goto out;
1731 }
1732
1733 /* we also need a net config
1734 * to change the options on */
1735 if (!get_net_conf(tconn)) {
1736 drbd_msg_put_info("net conf missing, try connect");
1737 retcode = ERR_INVALID_REQUEST;
1738 goto out;
1739 }
1740
1741 conn_reconfig_start(tconn);
1742
1743 memcpy(new_conf, tconn->net_conf, sizeof(*new_conf));
1744 err = net_conf_from_attrs_for_change(new_conf, info);
1745 if (err) {
1746 retcode = ERR_MANDATORY_TAG;
1747 drbd_msg_put_info(from_attrs_err_to_txt(err));
1748 goto fail;
1749 }
1750
Philipp Reisnercd643972011-04-13 18:00:59 -07001751 retcode = check_net_options(tconn, new_conf);
1752 if (retcode != NO_ERROR)
1753 goto fail;
1754
Lars Ellenbergf3990022011-03-23 14:31:09 +01001755 /* re-sync running */
1756 rsr = conn_resync_running(tconn);
1757 if (rsr && strcmp(new_conf->csums_alg, tconn->net_conf->csums_alg)) {
1758 retcode = ERR_CSUMS_RESYNC_RUNNING;
1759 goto fail;
1760 }
1761
1762 if (!rsr && new_conf->csums_alg[0]) {
1763 csums_tfm = crypto_alloc_hash(new_conf->csums_alg, 0, CRYPTO_ALG_ASYNC);
1764 if (IS_ERR(csums_tfm)) {
1765 csums_tfm = NULL;
1766 retcode = ERR_CSUMS_ALG;
1767 goto fail;
1768 }
1769
1770 if (!drbd_crypto_is_hash(crypto_hash_tfm(csums_tfm))) {
1771 retcode = ERR_CSUMS_ALG_ND;
1772 goto fail;
1773 }
1774 }
1775
1776 /* online verify running */
1777 ovr = conn_ov_running(tconn);
1778 if (ovr) {
1779 if (strcmp(new_conf->verify_alg, tconn->net_conf->verify_alg)) {
1780 retcode = ERR_VERIFY_RUNNING;
1781 goto fail;
1782 }
1783 }
1784
1785 if (!ovr && new_conf->verify_alg[0]) {
1786 verify_tfm = crypto_alloc_hash(new_conf->verify_alg, 0, CRYPTO_ALG_ASYNC);
1787 if (IS_ERR(verify_tfm)) {
1788 verify_tfm = NULL;
1789 retcode = ERR_VERIFY_ALG;
1790 goto fail;
1791 }
1792
1793 if (!drbd_crypto_is_hash(crypto_hash_tfm(verify_tfm))) {
1794 retcode = ERR_VERIFY_ALG_ND;
1795 goto fail;
1796 }
1797 }
1798
1799
1800 /* For now, use struct assignment, not pointer assignment.
1801 * We don't have any means to determine who might still
1802 * keep a local alias into the struct,
1803 * so we cannot just free it and hope for the best :(
1804 * FIXME
1805 * To avoid someone looking at a half-updated struct, we probably
1806 * should have a rw-semaphor on net_conf and disk_conf.
1807 */
1808 *tconn->net_conf = *new_conf;
1809
1810 if (!rsr) {
1811 crypto_free_hash(tconn->csums_tfm);
1812 tconn->csums_tfm = csums_tfm;
1813 csums_tfm = NULL;
1814 }
1815 if (!ovr) {
1816 crypto_free_hash(tconn->verify_tfm);
1817 tconn->verify_tfm = verify_tfm;
1818 verify_tfm = NULL;
1819 }
1820
1821 if (tconn->cstate >= C_WF_REPORT_PARAMS)
1822 drbd_send_sync_param(minor_to_mdev(conn_lowest_minor(tconn)));
1823
1824 fail:
1825 crypto_free_hash(csums_tfm);
1826 crypto_free_hash(verify_tfm);
1827 kfree(new_conf);
1828 put_net_conf(tconn);
1829 conn_reconfig_done(tconn);
1830 out:
1831 drbd_adm_finish(info, retcode);
1832 return 0;
1833}
1834
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001835int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001836{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001837 char hmac_name[CRYPTO_MAX_ALG_NAME];
1838 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001839 struct net_conf *new_conf = NULL;
1840 struct crypto_hash *tfm = NULL;
1841 struct crypto_hash *integrity_w_tfm = NULL;
1842 struct crypto_hash *integrity_r_tfm = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001843 void *int_dig_in = NULL;
1844 void *int_dig_vv = NULL;
Philipp Reisner80883192011-02-18 14:56:45 +01001845 struct drbd_tconn *oconn;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001846 struct drbd_tconn *tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001847 struct sockaddr *new_my_addr, *new_peer_addr, *taken_addr;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001848 enum drbd_ret_code retcode;
1849 int i;
1850 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001851
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001852 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
1853 if (!adm_ctx.reply_skb)
1854 return retcode;
1855 if (retcode != NO_ERROR)
1856 goto out;
1857
1858 tconn = adm_ctx.tconn;
Philipp Reisner80883192011-02-18 14:56:45 +01001859 conn_reconfig_start(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001860
Philipp Reisner80883192011-02-18 14:56:45 +01001861 if (tconn->cstate > C_STANDALONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001862 retcode = ERR_NET_CONFIGURED;
1863 goto fail;
1864 }
1865
1866 /* allocation not in the IO path, cqueue thread context */
Lars Ellenbergf3990022011-03-23 14:31:09 +01001867 new_conf = kmalloc(sizeof(struct net_conf), GFP_KERNEL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001868 if (!new_conf) {
1869 retcode = ERR_NOMEM;
1870 goto fail;
1871 }
1872
Lars Ellenbergf3990022011-03-23 14:31:09 +01001873 *new_conf = (struct net_conf) {
1874 {}, 0, /* my_addr */
1875 {}, 0, /* peer_addr */
1876 {}, 0, /* shared_secret */
1877 {}, 0, /* cram_hmac_alg */
1878 {}, 0, /* integrity_alg */
1879 {}, 0, /* verify_alg */
1880 {}, 0, /* csums_alg */
1881 DRBD_PROTOCOL_DEF, /* wire_protocol */
1882 DRBD_CONNECT_INT_DEF, /* try_connect_int */
1883 DRBD_TIMEOUT_DEF, /* timeout */
1884 DRBD_PING_INT_DEF, /* ping_int */
1885 DRBD_PING_TIMEO_DEF, /* ping_timeo */
1886 DRBD_SNDBUF_SIZE_DEF, /* sndbuf_size */
1887 DRBD_RCVBUF_SIZE_DEF, /* rcvbuf_size */
1888 DRBD_KO_COUNT_DEF, /* ko_count */
1889 DRBD_MAX_BUFFERS_DEF, /* max_buffers */
1890 DRBD_MAX_EPOCH_SIZE_DEF, /* max_epoch_size */
1891 DRBD_UNPLUG_WATERMARK_DEF, /* unplug_watermark */
1892 DRBD_AFTER_SB_0P_DEF, /* after_sb_0p */
1893 DRBD_AFTER_SB_1P_DEF, /* after_sb_1p */
1894 DRBD_AFTER_SB_2P_DEF, /* after_sb_2p */
1895 DRBD_RR_CONFLICT_DEF, /* rr_conflict */
1896 DRBD_ON_CONGESTION_DEF, /* on_congestion */
1897 DRBD_CONG_FILL_DEF, /* cong_fill */
1898 DRBD_CONG_EXTENTS_DEF, /* cong_extents */
1899 0, /* two_primaries */
1900 0, /* want_lose */
1901 0, /* no_cork */
1902 0, /* always_asbp */
1903 0, /* dry_run */
1904 0, /* use_rle */
1905 };
Philipp Reisnerb411b362009-09-25 16:07:19 -07001906
Lars Ellenbergf3990022011-03-23 14:31:09 +01001907 err = net_conf_from_attrs(new_conf, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001908 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001909 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001910 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001911 goto fail;
1912 }
1913
Philipp Reisnercd643972011-04-13 18:00:59 -07001914 retcode = check_net_options(tconn, new_conf);
1915 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001916 goto fail;
Philipp Reisner422028b2010-10-27 11:12:07 +02001917
Philipp Reisnerb411b362009-09-25 16:07:19 -07001918 retcode = NO_ERROR;
1919
1920 new_my_addr = (struct sockaddr *)&new_conf->my_addr;
1921 new_peer_addr = (struct sockaddr *)&new_conf->peer_addr;
Lars Ellenberg543cc102011-03-10 22:18:18 +01001922
Philipp Reisneref356262011-04-13 14:21:29 -07001923 /* No need to take drbd_cfg_rwsem here. All reconfiguration is
Lars Ellenberg543cc102011-03-10 22:18:18 +01001924 * strictly serialized on genl_lock(). We are protected against
1925 * concurrent reconfiguration/addition/deletion */
Philipp Reisner80883192011-02-18 14:56:45 +01001926 list_for_each_entry(oconn, &drbd_tconns, all_tconn) {
1927 if (oconn == tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001928 continue;
Philipp Reisner80883192011-02-18 14:56:45 +01001929 if (get_net_conf(oconn)) {
1930 taken_addr = (struct sockaddr *)&oconn->net_conf->my_addr;
1931 if (new_conf->my_addr_len == oconn->net_conf->my_addr_len &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07001932 !memcmp(new_my_addr, taken_addr, new_conf->my_addr_len))
1933 retcode = ERR_LOCAL_ADDR;
1934
Philipp Reisner80883192011-02-18 14:56:45 +01001935 taken_addr = (struct sockaddr *)&oconn->net_conf->peer_addr;
1936 if (new_conf->peer_addr_len == oconn->net_conf->peer_addr_len &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07001937 !memcmp(new_peer_addr, taken_addr, new_conf->peer_addr_len))
1938 retcode = ERR_PEER_ADDR;
1939
Philipp Reisner80883192011-02-18 14:56:45 +01001940 put_net_conf(oconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001941 if (retcode != NO_ERROR)
1942 goto fail;
1943 }
1944 }
1945
1946 if (new_conf->cram_hmac_alg[0] != 0) {
1947 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
1948 new_conf->cram_hmac_alg);
1949 tfm = crypto_alloc_hash(hmac_name, 0, CRYPTO_ALG_ASYNC);
1950 if (IS_ERR(tfm)) {
1951 tfm = NULL;
1952 retcode = ERR_AUTH_ALG;
1953 goto fail;
1954 }
1955
Philipp Reisner07982192009-12-28 16:58:38 +01001956 if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001957 retcode = ERR_AUTH_ALG_ND;
1958 goto fail;
1959 }
1960 }
1961
1962 if (new_conf->integrity_alg[0]) {
1963 integrity_w_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC);
1964 if (IS_ERR(integrity_w_tfm)) {
1965 integrity_w_tfm = NULL;
1966 retcode=ERR_INTEGRITY_ALG;
1967 goto fail;
1968 }
1969
1970 if (!drbd_crypto_is_hash(crypto_hash_tfm(integrity_w_tfm))) {
1971 retcode=ERR_INTEGRITY_ALG_ND;
1972 goto fail;
1973 }
1974
1975 integrity_r_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC);
1976 if (IS_ERR(integrity_r_tfm)) {
1977 integrity_r_tfm = NULL;
1978 retcode=ERR_INTEGRITY_ALG;
1979 goto fail;
1980 }
1981 }
1982
Philipp Reisnerb411b362009-09-25 16:07:19 -07001983 ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
1984
Philipp Reisner80883192011-02-18 14:56:45 +01001985 /* allocation not in the IO path, cqueue thread context */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001986 if (integrity_w_tfm) {
1987 i = crypto_hash_digestsize(integrity_w_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001988 int_dig_in = kmalloc(i, GFP_KERNEL);
1989 if (!int_dig_in) {
1990 retcode = ERR_NOMEM;
1991 goto fail;
1992 }
1993 int_dig_vv = kmalloc(i, GFP_KERNEL);
1994 if (!int_dig_vv) {
1995 retcode = ERR_NOMEM;
1996 goto fail;
1997 }
1998 }
1999
Philipp Reisner80883192011-02-18 14:56:45 +01002000 conn_flush_workqueue(tconn);
2001 spin_lock_irq(&tconn->req_lock);
2002 if (tconn->net_conf != NULL) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002003 retcode = ERR_NET_CONFIGURED;
Philipp Reisner80883192011-02-18 14:56:45 +01002004 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002005 goto fail;
2006 }
Philipp Reisner80883192011-02-18 14:56:45 +01002007 tconn->net_conf = new_conf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002008
Philipp Reisner80883192011-02-18 14:56:45 +01002009 crypto_free_hash(tconn->cram_hmac_tfm);
2010 tconn->cram_hmac_tfm = tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002011
Philipp Reisner80883192011-02-18 14:56:45 +01002012 crypto_free_hash(tconn->integrity_w_tfm);
2013 tconn->integrity_w_tfm = integrity_w_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002014
Philipp Reisner80883192011-02-18 14:56:45 +01002015 crypto_free_hash(tconn->integrity_r_tfm);
2016 tconn->integrity_r_tfm = integrity_r_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002017
Philipp Reisner80883192011-02-18 14:56:45 +01002018 kfree(tconn->int_dig_in);
2019 kfree(tconn->int_dig_vv);
Philipp Reisner80883192011-02-18 14:56:45 +01002020 tconn->int_dig_in=int_dig_in;
2021 tconn->int_dig_vv=int_dig_vv;
2022 retcode = _conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
2023 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002024
Philipp Reisner695d08f2011-04-11 22:53:32 -07002025 rcu_read_lock();
Philipp Reisner80883192011-02-18 14:56:45 +01002026 idr_for_each_entry(&tconn->volumes, mdev, i) {
2027 mdev->send_cnt = 0;
2028 mdev->recv_cnt = 0;
2029 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
2030 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07002031 rcu_read_unlock();
Philipp Reisner80883192011-02-18 14:56:45 +01002032 conn_reconfig_done(tconn);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002033 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002034 return 0;
2035
2036fail:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002037 kfree(int_dig_in);
2038 kfree(int_dig_vv);
2039 crypto_free_hash(tfm);
2040 crypto_free_hash(integrity_w_tfm);
2041 crypto_free_hash(integrity_r_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002042 kfree(new_conf);
2043
Philipp Reisner80883192011-02-18 14:56:45 +01002044 conn_reconfig_done(tconn);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002045out:
2046 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002047 return 0;
2048}
2049
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002050static enum drbd_state_rv conn_try_disconnect(struct drbd_tconn *tconn, bool force)
2051{
2052 enum drbd_state_rv rv;
2053 if (force) {
2054 spin_lock_irq(&tconn->req_lock);
2055 if (tconn->cstate >= C_WF_CONNECTION)
2056 _conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
2057 spin_unlock_irq(&tconn->req_lock);
2058 return SS_SUCCESS;
2059 }
2060
2061 rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING), 0);
2062
2063 switch (rv) {
2064 case SS_NOTHING_TO_DO:
2065 case SS_ALREADY_STANDALONE:
2066 return SS_SUCCESS;
2067 case SS_PRIMARY_NOP:
2068 /* Our state checking code wants to see the peer outdated. */
2069 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
2070 pdsk, D_OUTDATED), CS_VERBOSE);
2071 break;
2072 case SS_CW_FAILED_BY_PEER:
2073 /* The peer probably wants to see us outdated. */
2074 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
2075 disk, D_OUTDATED), 0);
2076 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
2077 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
2078 rv = SS_SUCCESS;
2079 }
2080 break;
2081 default:;
2082 /* no special handling necessary */
2083 }
2084
2085 return rv;
2086}
2087
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002088int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002089{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002090 struct disconnect_parms parms;
2091 struct drbd_tconn *tconn;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002092 enum drbd_state_rv rv;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002093 enum drbd_ret_code retcode;
2094 int err;
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002095
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002096 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
2097 if (!adm_ctx.reply_skb)
2098 return retcode;
2099 if (retcode != NO_ERROR)
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002100 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002101
2102 tconn = adm_ctx.tconn;
2103 memset(&parms, 0, sizeof(parms));
2104 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002105 err = disconnect_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002106 if (err) {
2107 retcode = ERR_MANDATORY_TAG;
2108 drbd_msg_put_info(from_attrs_err_to_txt(err));
2109 goto fail;
2110 }
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002111 }
2112
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002113 rv = conn_try_disconnect(tconn, parms.force_disconnect);
2114 if (rv < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002115 goto fail;
2116
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01002117 if (wait_event_interruptible(tconn->ping_wait,
2118 tconn->cstate != C_DISCONNECTING)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002119 /* Do not test for mdev->state.conn == C_STANDALONE, since
2120 someone else might connect us in the mean time! */
2121 retcode = ERR_INTR;
2122 goto fail;
2123 }
2124
Philipp Reisnerb411b362009-09-25 16:07:19 -07002125 retcode = NO_ERROR;
2126 fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002127 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002128 return 0;
2129}
2130
2131void resync_after_online_grow(struct drbd_conf *mdev)
2132{
2133 int iass; /* I am sync source */
2134
2135 dev_info(DEV, "Resync of new storage after online grow\n");
2136 if (mdev->state.role != mdev->state.peer)
2137 iass = (mdev->state.role == R_PRIMARY);
2138 else
Philipp Reisner25703f82011-02-07 14:35:25 +01002139 iass = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002140
2141 if (iass)
2142 drbd_start_resync(mdev, C_SYNC_SOURCE);
2143 else
2144 _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
2145}
2146
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002147int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002148{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002149 struct resize_parms rs;
2150 struct drbd_conf *mdev;
2151 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002152 enum determine_dev_size dd;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002153 enum dds_flags ddsf;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002154 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002155
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002156 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2157 if (!adm_ctx.reply_skb)
2158 return retcode;
2159 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002160 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002161
2162 memset(&rs, 0, sizeof(struct resize_parms));
2163 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002164 err = resize_parms_from_attrs(&rs, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002165 if (err) {
2166 retcode = ERR_MANDATORY_TAG;
2167 drbd_msg_put_info(from_attrs_err_to_txt(err));
2168 goto fail;
2169 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002170 }
2171
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002172 mdev = adm_ctx.mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002173 if (mdev->state.conn > C_CONNECTED) {
2174 retcode = ERR_RESIZE_RESYNC;
2175 goto fail;
2176 }
2177
2178 if (mdev->state.role == R_SECONDARY &&
2179 mdev->state.peer == R_SECONDARY) {
2180 retcode = ERR_NO_PRIMARY;
2181 goto fail;
2182 }
2183
2184 if (!get_ldev(mdev)) {
2185 retcode = ERR_NO_DISK;
2186 goto fail;
2187 }
2188
Philipp Reisner31890f42011-01-19 14:12:51 +01002189 if (rs.no_resync && mdev->tconn->agreed_pro_version < 93) {
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002190 retcode = ERR_NEED_APV_93;
2191 goto fail;
2192 }
2193
Philipp Reisner087c2492010-03-26 13:49:56 +01002194 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002195 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002196
2197 mdev->ldev->dc.disk_size = (sector_t)rs.resize_size;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002198 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
Bart Van Assche24c48302011-05-21 18:32:29 +02002199 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002200 drbd_md_sync(mdev);
2201 put_ldev(mdev);
2202 if (dd == dev_size_error) {
2203 retcode = ERR_NOMEM_BITMAP;
2204 goto fail;
2205 }
2206
Philipp Reisner087c2492010-03-26 13:49:56 +01002207 if (mdev->state.conn == C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002208 if (dd == grew)
2209 set_bit(RESIZE_PENDING, &mdev->flags);
2210
2211 drbd_send_uuids(mdev);
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002212 drbd_send_sizes(mdev, 1, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002213 }
2214
2215 fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002216 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002217 return 0;
2218}
2219
Lars Ellenbergf3990022011-03-23 14:31:09 +01002220int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002221{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002222 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002223 cpumask_var_t new_cpu_mask;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002224 struct drbd_tconn *tconn;
Philipp Reisner778f2712010-07-06 11:14:00 +02002225 int *rs_plan_s = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002226 struct res_opts sc;
2227 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002228
Lars Ellenbergf3990022011-03-23 14:31:09 +01002229 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002230 if (!adm_ctx.reply_skb)
2231 return retcode;
2232 if (retcode != NO_ERROR)
2233 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002234 tconn = adm_ctx.tconn;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002235
Philipp Reisnerb411b362009-09-25 16:07:19 -07002236 if (!zalloc_cpumask_var(&new_cpu_mask, GFP_KERNEL)) {
2237 retcode = ERR_NOMEM;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002238 drbd_msg_put_info("unable to allocate cpumask");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002239 goto fail;
2240 }
2241
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002242 if (((struct drbd_genlmsghdr*)info->userhdr)->flags
2243 & DRBD_GENL_F_SET_DEFAULTS) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002244 memset(&sc, 0, sizeof(struct res_opts));
Philipp Reisner265be2d2010-05-31 10:14:17 +02002245 sc.on_no_data = DRBD_ON_NO_DATA_DEF;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002246 } else
Lars Ellenbergf3990022011-03-23 14:31:09 +01002247 sc = tconn->res_opts;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002248
Lars Ellenbergf3990022011-03-23 14:31:09 +01002249 err = res_opts_from_attrs(&sc, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002250 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002251 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002252 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002253 goto fail;
2254 }
2255
Philipp Reisnerb411b362009-09-25 16:07:19 -07002256 /* silently ignore cpu mask on UP kernel */
2257 if (nr_cpu_ids > 1 && sc.cpu_mask[0] != 0) {
2258 err = __bitmap_parse(sc.cpu_mask, 32, 0,
2259 cpumask_bits(new_cpu_mask), nr_cpu_ids);
2260 if (err) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002261 conn_warn(tconn, "__bitmap_parse() failed with %d\n", err);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002262 retcode = ERR_CPU_MASK_PARSE;
2263 goto fail;
2264 }
2265 }
2266
Lars Ellenberg7ad651b2011-02-21 13:21:03 +01002267
Lars Ellenbergf3990022011-03-23 14:31:09 +01002268 tconn->res_opts = sc;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002269
Lars Ellenbergf3990022011-03-23 14:31:09 +01002270 if (!cpumask_equal(tconn->cpu_mask, new_cpu_mask)) {
2271 cpumask_copy(tconn->cpu_mask, new_cpu_mask);
2272 drbd_calc_cpu_mask(tconn);
2273 tconn->receiver.reset_cpu_mask = 1;
2274 tconn->asender.reset_cpu_mask = 1;
2275 tconn->worker.reset_cpu_mask = 1;
Philipp Reisner778f2712010-07-06 11:14:00 +02002276 }
2277
Philipp Reisnerb411b362009-09-25 16:07:19 -07002278fail:
Philipp Reisner778f2712010-07-06 11:14:00 +02002279 kfree(rs_plan_s);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002280 free_cpumask_var(new_cpu_mask);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002281
2282 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002283 return 0;
2284}
2285
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002286int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002287{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002288 struct drbd_conf *mdev;
2289 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2290
2291 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2292 if (!adm_ctx.reply_skb)
2293 return retcode;
2294 if (retcode != NO_ERROR)
2295 goto out;
2296
2297 mdev = adm_ctx.mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002298
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002299 /* If there is still bitmap IO pending, probably because of a previous
2300 * resync just being finished, wait for it before requesting a new resync. */
2301 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
2302
Philipp Reisnerb411b362009-09-25 16:07:19 -07002303 retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T), CS_ORDERED);
2304
2305 if (retcode < SS_SUCCESS && retcode != SS_NEED_CONNECTION)
2306 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
2307
2308 while (retcode == SS_NEED_CONNECTION) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01002309 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002310 if (mdev->state.conn < C_CONNECTED)
2311 retcode = _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_VERBOSE, NULL);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002312 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002313
2314 if (retcode != SS_NEED_CONNECTION)
2315 break;
2316
2317 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
2318 }
2319
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002320out:
2321 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002322 return 0;
2323}
2324
Philipp Reisner07782862010-08-31 12:00:50 +02002325static int drbd_bmio_set_susp_al(struct drbd_conf *mdev)
2326{
2327 int rv;
2328
2329 rv = drbd_bmio_set_n_write(mdev);
2330 drbd_suspend_al(mdev);
2331 return rv;
2332}
2333
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002334static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2335 union drbd_state mask, union drbd_state val)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002336{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002337 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002338
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002339 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2340 if (!adm_ctx.reply_skb)
2341 return retcode;
2342 if (retcode != NO_ERROR)
2343 goto out;
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002344
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002345 retcode = drbd_request_state(adm_ctx.mdev, mask, val);
2346out:
2347 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002348 return 0;
2349}
2350
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002351int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002352{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002353 return drbd_adm_simple_request_state(skb, info, NS(conn, C_STARTING_SYNC_S));
2354}
Philipp Reisnerb411b362009-09-25 16:07:19 -07002355
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002356int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
2357{
2358 enum drbd_ret_code retcode;
2359
2360 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2361 if (!adm_ctx.reply_skb)
2362 return retcode;
2363 if (retcode != NO_ERROR)
2364 goto out;
2365
2366 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002367 retcode = ERR_PAUSE_IS_SET;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002368out:
2369 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002370 return 0;
2371}
2372
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002373int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002374{
Philipp Reisnerda9fbc22011-03-29 10:52:01 +02002375 union drbd_dev_state s;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002376 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002377
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002378 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2379 if (!adm_ctx.reply_skb)
2380 return retcode;
2381 if (retcode != NO_ERROR)
2382 goto out;
2383
2384 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2385 s = adm_ctx.mdev->state;
Philipp Reisnercd88d032011-01-20 11:46:41 +01002386 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2387 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2388 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2389 } else {
2390 retcode = ERR_PAUSE_IS_CLEAR;
2391 }
2392 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002393
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002394out:
2395 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002396 return 0;
2397}
2398
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002399int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002400{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002401 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002402}
2403
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002404int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002405{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002406 struct drbd_conf *mdev;
2407 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2408
2409 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2410 if (!adm_ctx.reply_skb)
2411 return retcode;
2412 if (retcode != NO_ERROR)
2413 goto out;
2414
2415 mdev = adm_ctx.mdev;
Philipp Reisner43a51822010-06-11 11:26:34 +02002416 if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
2417 drbd_uuid_new_current(mdev);
2418 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner43a51822010-06-11 11:26:34 +02002419 }
Philipp Reisner265be2d2010-05-31 10:14:17 +02002420 drbd_suspend_io(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002421 retcode = drbd_request_state(mdev, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
2422 if (retcode == SS_SUCCESS) {
Philipp Reisner265be2d2010-05-31 10:14:17 +02002423 if (mdev->state.conn < C_CONNECTED)
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01002424 tl_clear(mdev->tconn);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002425 if (mdev->state.disk == D_DISKLESS || mdev->state.disk == D_FAILED)
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01002426 tl_restart(mdev->tconn, FAIL_FROZEN_DISK_IO);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002427 }
2428 drbd_resume_io(mdev);
2429
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002430out:
2431 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002432 return 0;
2433}
2434
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002435int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002436{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002437 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002438}
2439
Lars Ellenberg543cc102011-03-10 22:18:18 +01002440int nla_put_drbd_cfg_context(struct sk_buff *skb, const char *conn_name, unsigned vnr)
2441{
2442 struct nlattr *nla;
2443 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
2444 if (!nla)
2445 goto nla_put_failure;
2446 if (vnr != VOLUME_UNSPECIFIED)
2447 NLA_PUT_U32(skb, T_ctx_volume, vnr);
2448 NLA_PUT_STRING(skb, T_ctx_conn_name, conn_name);
2449 nla_nest_end(skb, nla);
2450 return 0;
2451
2452nla_put_failure:
2453 if (nla)
2454 nla_nest_cancel(skb, nla);
2455 return -EMSGSIZE;
2456}
2457
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002458int nla_put_status_info(struct sk_buff *skb, struct drbd_conf *mdev,
2459 const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002460{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002461 struct state_info *si = NULL; /* for sizeof(si->member); */
2462 struct nlattr *nla;
2463 int got_ldev;
2464 int got_net;
2465 int err = 0;
2466 int exclude_sensitive;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002467
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002468 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2469 * to. So we better exclude_sensitive information.
2470 *
2471 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2472 * in the context of the requesting user process. Exclude sensitive
2473 * information, unless current has superuser.
2474 *
2475 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2476 * relies on the current implementation of netlink_dump(), which
2477 * executes the dump callback successively from netlink_recvmsg(),
2478 * always in the context of the receiving process */
2479 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002480
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002481 got_ldev = get_ldev(mdev);
2482 got_net = get_net_conf(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002483
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002484 /* We need to add connection name and volume number information still.
2485 * Minor number is in drbd_genlmsghdr. */
Lars Ellenberg543cc102011-03-10 22:18:18 +01002486 if (nla_put_drbd_cfg_context(skb, mdev->tconn->name, mdev->vnr))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002487 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002488
Lars Ellenbergf3990022011-03-23 14:31:09 +01002489 if (res_opts_to_skb(skb, &mdev->tconn->res_opts, exclude_sensitive))
2490 goto nla_put_failure;
2491
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002492 if (got_ldev)
2493 if (disk_conf_to_skb(skb, &mdev->ldev->dc, exclude_sensitive))
2494 goto nla_put_failure;
2495 if (got_net)
2496 if (net_conf_to_skb(skb, mdev->tconn->net_conf, exclude_sensitive))
2497 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002498
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002499 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
2500 if (!nla)
2501 goto nla_put_failure;
2502 NLA_PUT_U32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY);
2503 NLA_PUT_U32(skb, T_current_state, mdev->state.i);
2504 NLA_PUT_U64(skb, T_ed_uuid, mdev->ed_uuid);
2505 NLA_PUT_U64(skb, T_capacity, drbd_get_capacity(mdev->this_bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002506
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002507 if (got_ldev) {
2508 NLA_PUT_U32(skb, T_disk_flags, mdev->ldev->md.flags);
2509 NLA_PUT(skb, T_uuids, sizeof(si->uuids), mdev->ldev->md.uuid);
2510 NLA_PUT_U64(skb, T_bits_total, drbd_bm_bits(mdev));
2511 NLA_PUT_U64(skb, T_bits_oos, drbd_bm_total_weight(mdev));
2512 if (C_SYNC_SOURCE <= mdev->state.conn &&
2513 C_PAUSED_SYNC_T >= mdev->state.conn) {
2514 NLA_PUT_U64(skb, T_bits_rs_total, mdev->rs_total);
2515 NLA_PUT_U64(skb, T_bits_rs_failed, mdev->rs_failed);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002516 }
2517 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002518
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002519 if (sib) {
2520 switch(sib->sib_reason) {
2521 case SIB_SYNC_PROGRESS:
2522 case SIB_GET_STATUS_REPLY:
2523 break;
2524 case SIB_STATE_CHANGE:
2525 NLA_PUT_U32(skb, T_prev_state, sib->os.i);
2526 NLA_PUT_U32(skb, T_new_state, sib->ns.i);
2527 break;
2528 case SIB_HELPER_POST:
2529 NLA_PUT_U32(skb,
2530 T_helper_exit_code, sib->helper_exit_code);
2531 /* fall through */
2532 case SIB_HELPER_PRE:
2533 NLA_PUT_STRING(skb, T_helper, sib->helper_name);
2534 break;
2535 }
2536 }
2537 nla_nest_end(skb, nla);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002538
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002539 if (0)
2540nla_put_failure:
2541 err = -EMSGSIZE;
2542 if (got_ldev)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002543 put_ldev(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002544 if (got_net)
2545 put_net_conf(mdev->tconn);
2546 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002547}
2548
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002549int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002550{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002551 enum drbd_ret_code retcode;
2552 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002553
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002554 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2555 if (!adm_ctx.reply_skb)
2556 return retcode;
2557 if (retcode != NO_ERROR)
2558 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002559
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002560 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.mdev, NULL);
2561 if (err) {
2562 nlmsg_free(adm_ctx.reply_skb);
2563 return err;
2564 }
2565out:
2566 drbd_adm_finish(info, retcode);
2567 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002568}
2569
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002570int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002571{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002572 struct drbd_conf *mdev;
2573 struct drbd_genlmsghdr *dh;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002574 struct drbd_tconn *pos = (struct drbd_tconn*)cb->args[0];
2575 struct drbd_tconn *tconn = NULL;
2576 struct drbd_tconn *tmp;
2577 unsigned volume = cb->args[1];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002578
Lars Ellenberg543cc102011-03-10 22:18:18 +01002579 /* Open coded, deferred, iteration:
2580 * list_for_each_entry_safe(tconn, tmp, &drbd_tconns, all_tconn) {
2581 * idr_for_each_entry(&tconn->volumes, mdev, i) {
2582 * ...
2583 * }
2584 * }
2585 * where tconn is cb->args[0];
2586 * and i is cb->args[1];
2587 *
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002588 * This may miss entries inserted after this dump started,
2589 * or entries deleted before they are reached.
Lars Ellenberg543cc102011-03-10 22:18:18 +01002590 *
2591 * We need to make sure the mdev won't disappear while
2592 * we are looking at it, and revalidate our iterators
2593 * on each iteration.
2594 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002595
Lars Ellenberg543cc102011-03-10 22:18:18 +01002596 /* synchronize with drbd_new_tconn/drbd_free_tconn */
Philipp Reisneref356262011-04-13 14:21:29 -07002597 down_read(&drbd_cfg_rwsem);
Lars Ellenberg543cc102011-03-10 22:18:18 +01002598next_tconn:
2599 /* revalidate iterator position */
2600 list_for_each_entry(tmp, &drbd_tconns, all_tconn) {
2601 if (pos == NULL) {
2602 /* first iteration */
2603 pos = tmp;
2604 tconn = pos;
2605 break;
2606 }
2607 if (tmp == pos) {
2608 tconn = pos;
2609 break;
2610 }
2611 }
2612 if (tconn) {
2613 mdev = idr_get_next(&tconn->volumes, &volume);
2614 if (!mdev) {
2615 /* No more volumes to dump on this tconn.
2616 * Advance tconn iterator. */
2617 pos = list_entry(tconn->all_tconn.next,
2618 struct drbd_tconn, all_tconn);
2619 /* But, did we dump any volume on this tconn yet? */
2620 if (volume != 0) {
2621 tconn = NULL;
2622 volume = 0;
2623 goto next_tconn;
2624 }
2625 }
2626
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002627 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).pid,
2628 cb->nlh->nlmsg_seq, &drbd_genl_family,
2629 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
2630 if (!dh)
Lars Ellenberg543cc102011-03-10 22:18:18 +01002631 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002632
Lars Ellenberg543cc102011-03-10 22:18:18 +01002633 if (!mdev) {
2634 /* this is a tconn without a single volume */
2635 dh->minor = -1U;
2636 dh->ret_code = NO_ERROR;
2637 if (nla_put_drbd_cfg_context(skb, tconn->name, VOLUME_UNSPECIFIED))
2638 genlmsg_cancel(skb, dh);
2639 else
2640 genlmsg_end(skb, dh);
2641 goto out;
2642 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002643
Lars Ellenberg543cc102011-03-10 22:18:18 +01002644 D_ASSERT(mdev->vnr == volume);
2645 D_ASSERT(mdev->tconn == tconn);
2646
2647 dh->minor = mdev_to_minor(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002648 dh->ret_code = NO_ERROR;
2649
2650 if (nla_put_status_info(skb, mdev, NULL)) {
2651 genlmsg_cancel(skb, dh);
Lars Ellenberg543cc102011-03-10 22:18:18 +01002652 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002653 }
2654 genlmsg_end(skb, dh);
2655 }
2656
Lars Ellenberg543cc102011-03-10 22:18:18 +01002657out:
Philipp Reisneref356262011-04-13 14:21:29 -07002658 up_read(&drbd_cfg_rwsem);
Lars Ellenberg543cc102011-03-10 22:18:18 +01002659 /* where to start the next iteration */
2660 cb->args[0] = (long)pos;
2661 cb->args[1] = (pos == tconn) ? volume + 1 : 0;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002662
Lars Ellenberg543cc102011-03-10 22:18:18 +01002663 /* No more tconns/volumes/minors found results in an empty skb.
2664 * Which will terminate the dump. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002665 return skb->len;
2666}
2667
2668int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
2669{
2670 enum drbd_ret_code retcode;
2671 struct timeout_parms tp;
2672 int err;
2673
2674 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2675 if (!adm_ctx.reply_skb)
2676 return retcode;
2677 if (retcode != NO_ERROR)
2678 goto out;
2679
2680 tp.timeout_type =
2681 adm_ctx.mdev->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
2682 test_bit(USE_DEGR_WFC_T, &adm_ctx.mdev->flags) ? UT_DEGRADED :
2683 UT_DEFAULT;
2684
2685 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
2686 if (err) {
2687 nlmsg_free(adm_ctx.reply_skb);
2688 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002689 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002690out:
2691 drbd_adm_finish(info, retcode);
2692 return 0;
2693}
Lars Ellenberg873b0d52011-01-21 22:53:48 +01002694
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002695int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
2696{
2697 struct drbd_conf *mdev;
2698 enum drbd_ret_code retcode;
2699
2700 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2701 if (!adm_ctx.reply_skb)
2702 return retcode;
2703 if (retcode != NO_ERROR)
2704 goto out;
2705
2706 mdev = adm_ctx.mdev;
2707 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
2708 /* resume from last known position, if possible */
2709 struct start_ov_parms parms =
2710 { .ov_start_sector = mdev->ov_start_sector };
Lars Ellenbergf3990022011-03-23 14:31:09 +01002711 int err = start_ov_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002712 if (err) {
2713 retcode = ERR_MANDATORY_TAG;
2714 drbd_msg_put_info(from_attrs_err_to_txt(err));
2715 goto out;
2716 }
2717 /* w_make_ov_request expects position to be aligned */
2718 mdev->ov_start_sector = parms.ov_start_sector & ~BM_SECT_PER_BIT;
2719 }
Lars Ellenberg873b0d52011-01-21 22:53:48 +01002720 /* If there is still bitmap IO pending, e.g. previous resync or verify
2721 * just being finished, wait for it before requesting a new resync. */
2722 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002723 retcode = drbd_request_state(mdev,NS(conn,C_VERIFY_S));
2724out:
2725 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002726 return 0;
2727}
2728
2729
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002730int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002731{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002732 struct drbd_conf *mdev;
2733 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002734 int skip_initial_sync = 0;
2735 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002736 struct new_c_uuid_parms args;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002737
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002738 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2739 if (!adm_ctx.reply_skb)
2740 return retcode;
2741 if (retcode != NO_ERROR)
2742 goto out_nolock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002743
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002744 mdev = adm_ctx.mdev;
2745 memset(&args, 0, sizeof(args));
2746 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002747 err = new_c_uuid_parms_from_attrs(&args, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002748 if (err) {
2749 retcode = ERR_MANDATORY_TAG;
2750 drbd_msg_put_info(from_attrs_err_to_txt(err));
2751 goto out_nolock;
2752 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002753 }
2754
Philipp Reisner8410da8f02011-02-11 20:11:10 +01002755 mutex_lock(mdev->state_mutex); /* Protects us against serialized state changes. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002756
2757 if (!get_ldev(mdev)) {
2758 retcode = ERR_NO_DISK;
2759 goto out;
2760 }
2761
2762 /* this is "skip initial sync", assume to be clean */
Philipp Reisner31890f42011-01-19 14:12:51 +01002763 if (mdev->state.conn == C_CONNECTED && mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07002764 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
2765 dev_info(DEV, "Preparing to skip initial sync\n");
2766 skip_initial_sync = 1;
2767 } else if (mdev->state.conn != C_STANDALONE) {
2768 retcode = ERR_CONNECTED;
2769 goto out_dec;
2770 }
2771
2772 drbd_uuid_set(mdev, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
2773 drbd_uuid_new_current(mdev); /* New current, previous to UI_BITMAP */
2774
2775 if (args.clear_bm) {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002776 err = drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
2777 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002778 if (err) {
2779 dev_err(DEV, "Writing bitmap failed with %d\n",err);
2780 retcode = ERR_IO_MD_DISK;
2781 }
2782 if (skip_initial_sync) {
2783 drbd_send_uuids_skip_initial_sync(mdev);
2784 _drbd_uuid_set(mdev, UI_BITMAP, 0);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002785 drbd_print_uuids(mdev, "cleared bitmap UUID");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002786 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002787 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
2788 CS_VERBOSE, NULL);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002789 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002790 }
2791 }
2792
2793 drbd_md_sync(mdev);
2794out_dec:
2795 put_ldev(mdev);
2796out:
Philipp Reisner8410da8f02011-02-11 20:11:10 +01002797 mutex_unlock(mdev->state_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002798out_nolock:
2799 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002800 return 0;
2801}
2802
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002803static enum drbd_ret_code
2804drbd_check_conn_name(const char *name)
Philipp Reisner774b3052011-02-22 02:07:03 -05002805{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002806 if (!name || !name[0]) {
2807 drbd_msg_put_info("connection name missing");
2808 return ERR_MANDATORY_TAG;
2809 }
2810 /* if we want to use these in sysfs/configfs/debugfs some day,
2811 * we must not allow slashes */
2812 if (strchr(name, '/')) {
2813 drbd_msg_put_info("invalid connection name");
2814 return ERR_INVALID_REQUEST;
2815 }
2816 return NO_ERROR;
2817}
Philipp Reisner774b3052011-02-22 02:07:03 -05002818
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002819int drbd_adm_create_connection(struct sk_buff *skb, struct genl_info *info)
2820{
2821 enum drbd_ret_code retcode;
2822
2823 retcode = drbd_adm_prepare(skb, info, 0);
2824 if (!adm_ctx.reply_skb)
2825 return retcode;
2826 if (retcode != NO_ERROR)
2827 goto out;
2828
2829 retcode = drbd_check_conn_name(adm_ctx.conn_name);
2830 if (retcode != NO_ERROR)
2831 goto out;
2832
2833 if (adm_ctx.tconn) {
Lars Ellenberg38f19612011-03-14 13:22:35 +01002834 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
2835 retcode = ERR_INVALID_REQUEST;
2836 drbd_msg_put_info("connection exists");
2837 }
2838 /* else: still NO_ERROR */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002839 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05002840 }
2841
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002842 if (!drbd_new_tconn(adm_ctx.conn_name))
2843 retcode = ERR_NOMEM;
2844out:
2845 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05002846 return 0;
2847}
2848
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002849int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05002850{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002851 struct drbd_genlmsghdr *dh = info->userhdr;
2852 enum drbd_ret_code retcode;
Philipp Reisner774b3052011-02-22 02:07:03 -05002853
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002854 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
2855 if (!adm_ctx.reply_skb)
2856 return retcode;
2857 if (retcode != NO_ERROR)
2858 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05002859
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002860 /* FIXME drop minor_count parameter, limit to MINORMASK */
2861 if (dh->minor >= minor_count) {
2862 drbd_msg_put_info("requested minor out of range");
2863 retcode = ERR_INVALID_REQUEST;
2864 goto out;
2865 }
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +02002866 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002867 drbd_msg_put_info("requested volume id out of range");
2868 retcode = ERR_INVALID_REQUEST;
2869 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05002870 }
2871
Lars Ellenberg38f19612011-03-14 13:22:35 +01002872 /* drbd_adm_prepare made sure already
2873 * that mdev->tconn and mdev->vnr match the request. */
2874 if (adm_ctx.mdev) {
2875 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
2876 retcode = ERR_MINOR_EXISTS;
2877 /* else: still NO_ERROR */
2878 goto out;
2879 }
2880
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07002881 down_write(&drbd_cfg_rwsem);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002882 retcode = conn_new_minor(adm_ctx.tconn, dh->minor, adm_ctx.volume);
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07002883 up_write(&drbd_cfg_rwsem);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002884out:
2885 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05002886 return 0;
2887}
2888
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002889static enum drbd_ret_code adm_delete_minor(struct drbd_conf *mdev)
2890{
2891 if (mdev->state.disk == D_DISKLESS &&
2892 /* no need to be mdev->state.conn == C_STANDALONE &&
2893 * we may want to delete a minor from a live replication group.
2894 */
2895 mdev->state.role == R_SECONDARY) {
Philipp Reisnerff370e52011-04-11 21:10:11 -07002896 drbd_delete_device(mdev);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002897 return NO_ERROR;
2898 } else
2899 return ERR_MINOR_CONFIGURED;
2900}
2901
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002902int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05002903{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002904 enum drbd_ret_code retcode;
2905
2906 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2907 if (!adm_ctx.reply_skb)
2908 return retcode;
2909 if (retcode != NO_ERROR)
2910 goto out;
2911
Philipp Reisneref356262011-04-13 14:21:29 -07002912 down_write(&drbd_cfg_rwsem);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002913 retcode = adm_delete_minor(adm_ctx.mdev);
Philipp Reisneref356262011-04-13 14:21:29 -07002914 up_write(&drbd_cfg_rwsem);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002915 /* if this was the last volume of this connection,
2916 * this will terminate all threads */
2917 if (retcode == NO_ERROR)
Lars Ellenbergcffec5b2011-03-10 23:33:15 +01002918 conn_reconfig_done(adm_ctx.tconn);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002919out:
2920 drbd_adm_finish(info, retcode);
2921 return 0;
2922}
2923
2924int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
2925{
2926 enum drbd_ret_code retcode;
2927 enum drbd_state_rv rv;
2928 struct drbd_conf *mdev;
2929 unsigned i;
2930
2931 retcode = drbd_adm_prepare(skb, info, 0);
2932 if (!adm_ctx.reply_skb)
2933 return retcode;
2934 if (retcode != NO_ERROR)
2935 goto out;
2936
2937 if (!adm_ctx.tconn) {
2938 retcode = ERR_CONN_NOT_KNOWN;
2939 goto out;
2940 }
2941
Philipp Reisneref356262011-04-13 14:21:29 -07002942 down_read(&drbd_cfg_rwsem);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002943 /* demote */
2944 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
2945 retcode = drbd_set_role(mdev, R_SECONDARY, 0);
2946 if (retcode < SS_SUCCESS) {
2947 drbd_msg_put_info("failed to demote");
2948 goto out_unlock;
2949 }
2950 }
2951
2952 /* disconnect */
2953 rv = conn_try_disconnect(adm_ctx.tconn, 0);
2954 if (rv < SS_SUCCESS) {
2955 retcode = rv; /* enum type mismatch! */
2956 drbd_msg_put_info("failed to disconnect");
2957 goto out_unlock;
2958 }
2959
2960 /* detach */
2961 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
2962 rv = adm_detach(mdev);
2963 if (rv < SS_SUCCESS) {
2964 retcode = rv; /* enum type mismatch! */
2965 drbd_msg_put_info("failed to detach");
2966 goto out_unlock;
2967 }
2968 }
Philipp Reisneref356262011-04-13 14:21:29 -07002969 up_read(&drbd_cfg_rwsem);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002970
2971 /* delete volumes */
Philipp Reisneref356262011-04-13 14:21:29 -07002972 down_write(&drbd_cfg_rwsem);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002973 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
2974 retcode = adm_delete_minor(mdev);
2975 if (retcode != NO_ERROR) {
2976 /* "can not happen" */
2977 drbd_msg_put_info("failed to delete volume");
Philipp Reisneref356262011-04-13 14:21:29 -07002978 up_write(&drbd_cfg_rwsem);
2979 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002980 }
2981 }
2982
2983 /* stop all threads */
2984 conn_reconfig_done(adm_ctx.tconn);
2985
2986 /* delete connection */
2987 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
2988 drbd_free_tconn(adm_ctx.tconn);
2989 retcode = NO_ERROR;
2990 } else {
2991 /* "can not happen" */
2992 retcode = ERR_CONN_IN_USE;
2993 drbd_msg_put_info("failed to delete connection");
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002994 }
Philipp Reisneref356262011-04-13 14:21:29 -07002995
2996 up_write(&drbd_cfg_rwsem);
2997 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002998out_unlock:
Philipp Reisneref356262011-04-13 14:21:29 -07002999 up_read(&drbd_cfg_rwsem);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003000out:
3001 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003002 return 0;
3003}
3004
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003005int drbd_adm_delete_connection(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05003006{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003007 enum drbd_ret_code retcode;
3008
3009 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
3010 if (!adm_ctx.reply_skb)
3011 return retcode;
3012 if (retcode != NO_ERROR)
3013 goto out;
3014
Philipp Reisneref356262011-04-13 14:21:29 -07003015 down_write(&drbd_cfg_rwsem);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003016 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
3017 drbd_free_tconn(adm_ctx.tconn);
3018 retcode = NO_ERROR;
Philipp Reisner774b3052011-02-22 02:07:03 -05003019 } else {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003020 retcode = ERR_CONN_IN_USE;
Philipp Reisner774b3052011-02-22 02:07:03 -05003021 }
Philipp Reisneref356262011-04-13 14:21:29 -07003022 up_write(&drbd_cfg_rwsem);
Philipp Reisner774b3052011-02-22 02:07:03 -05003023
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003024out:
3025 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003026 return 0;
3027}
3028
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003029void drbd_bcast_event(struct drbd_conf *mdev, const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003030{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003031 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
3032 struct sk_buff *msg;
3033 struct drbd_genlmsghdr *d_out;
3034 unsigned seq;
3035 int err = -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003036
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003037 seq = atomic_inc_return(&drbd_genl_seq);
3038 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
3039 if (!msg)
3040 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003041
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003042 err = -EMSGSIZE;
3043 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
3044 if (!d_out) /* cannot happen, but anyways. */
3045 goto nla_put_failure;
3046 d_out->minor = mdev_to_minor(mdev);
3047 d_out->ret_code = 0;
Philipp Reisner9f5180e2009-10-06 09:30:14 +02003048
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003049 if (nla_put_status_info(msg, mdev, sib))
3050 goto nla_put_failure;
3051 genlmsg_end(msg, d_out);
3052 err = drbd_genl_multicast_events(msg, 0);
3053 /* msg has been consumed or freed in netlink_broadcast() */
3054 if (err && err != -ESRCH)
3055 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003056
Philipp Reisnerb411b362009-09-25 16:07:19 -07003057 return;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003058
3059nla_put_failure:
3060 nlmsg_free(msg);
3061failed:
3062 dev_err(DEV, "Error %d while broadcasting event. "
3063 "Event seq:%u sib_reason:%u\n",
3064 err, seq, sib->sib_reason);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003065}