blob: 1a8fb7a0db59f5493adb88ccc8010e57f3be3f19 [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);
Philipp Reisner0ace9df2011-04-24 10:53:19 +0200198 adm_ctx.tconn = conn_get_by_name(adm_ctx.conn_name);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100199
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 }
Philipp Reisner0ace9df2011-04-24 10:53:19 +0200226
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100227 return NO_ERROR;
228
229fail:
230 nlmsg_free(adm_ctx.reply_skb);
231 adm_ctx.reply_skb = NULL;
232 return -ENOMEM;
233}
234
235static int drbd_adm_finish(struct genl_info *info, int retcode)
236{
237 struct nlattr *nla;
238 const char *conn_name = NULL;
239
Philipp Reisner0ace9df2011-04-24 10:53:19 +0200240 if (adm_ctx.tconn) {
241 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
242 adm_ctx.tconn = NULL;
243 }
244
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100245 if (!adm_ctx.reply_skb)
246 return -ENOMEM;
247
248 adm_ctx.reply_dh->ret_code = retcode;
249
250 nla = info->attrs[DRBD_NLA_CFG_CONTEXT];
251 if (nla) {
252 nla = nla_find_nested(nla, __nla_type(T_ctx_conn_name));
253 if (nla)
254 conn_name = nla_data(nla);
255 }
256
257 drbd_adm_send_reply(adm_ctx.reply_skb, info);
258 return 0;
259}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700260
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100261static void setup_khelper_env(struct drbd_tconn *tconn, char **envp)
262{
263 char *afs;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200264 struct net_conf *nc;
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100265
Philipp Reisner44ed1672011-04-19 17:10:19 +0200266 rcu_read_lock();
267 nc = rcu_dereference(tconn->net_conf);
268 if (nc) {
269 switch (((struct sockaddr *)nc->peer_addr)->sa_family) {
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100270 case AF_INET6:
271 afs = "ipv6";
272 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
Philipp Reisner44ed1672011-04-19 17:10:19 +0200273 &((struct sockaddr_in6 *)nc->peer_addr)->sin6_addr);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100274 break;
275 case AF_INET:
276 afs = "ipv4";
277 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
Philipp Reisner44ed1672011-04-19 17:10:19 +0200278 &((struct sockaddr_in *)nc->peer_addr)->sin_addr);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100279 break;
280 default:
281 afs = "ssocks";
282 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
Philipp Reisner44ed1672011-04-19 17:10:19 +0200283 &((struct sockaddr_in *)nc->peer_addr)->sin_addr);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100284 }
285 snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100286 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200287 rcu_read_unlock();
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100288}
289
Philipp Reisnerb411b362009-09-25 16:07:19 -0700290int drbd_khelper(struct drbd_conf *mdev, char *cmd)
291{
292 char *envp[] = { "HOME=/",
293 "TERM=linux",
294 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100295 (char[20]) { }, /* address family */
296 (char[60]) { }, /* address */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700297 NULL };
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100298 char mb[12];
Philipp Reisnerb411b362009-09-25 16:07:19 -0700299 char *argv[] = {usermode_helper, cmd, mb, NULL };
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100300 struct sib_info sib;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700301 int ret;
302
303 snprintf(mb, 12, "minor-%d", mdev_to_minor(mdev));
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100304 setup_khelper_env(mdev->tconn, envp);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700305
Lars Ellenberg1090c052010-07-19 17:41:04 +0200306 /* The helper may take some time.
307 * write out any unsynced meta data changes now */
308 drbd_md_sync(mdev);
309
Philipp Reisnerb411b362009-09-25 16:07:19 -0700310 dev_info(DEV, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100311 sib.sib_reason = SIB_HELPER_PRE;
312 sib.helper_name = cmd;
313 drbd_bcast_event(mdev, &sib);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700314 ret = call_usermodehelper(usermode_helper, argv, envp, 1);
315 if (ret)
316 dev_warn(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
317 usermode_helper, cmd, mb,
318 (ret >> 8) & 0xff, ret);
319 else
320 dev_info(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
321 usermode_helper, cmd, mb,
322 (ret >> 8) & 0xff, ret);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100323 sib.sib_reason = SIB_HELPER_POST;
324 sib.helper_exit_code = ret;
325 drbd_bcast_event(mdev, &sib);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700326
327 if (ret < 0) /* Ignore any ERRNOs we got. */
328 ret = 0;
329
330 return ret;
331}
332
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100333static void conn_md_sync(struct drbd_tconn *tconn)
334{
335 struct drbd_conf *mdev;
Philipp Reisnere90285e2011-03-22 12:51:21 +0100336 int vnr;
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100337
Philipp Reisnerd3fcb492011-04-13 14:46:05 -0700338 down_read(&drbd_cfg_rwsem);
Philipp Reisnere90285e2011-03-22 12:51:21 +0100339 idr_for_each_entry(&tconn->volumes, mdev, vnr)
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100340 drbd_md_sync(mdev);
Philipp Reisnerd3fcb492011-04-13 14:46:05 -0700341 up_read(&drbd_cfg_rwsem);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100342}
343
344int conn_khelper(struct drbd_tconn *tconn, char *cmd)
345{
346 char *envp[] = { "HOME=/",
347 "TERM=linux",
348 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
349 (char[20]) { }, /* address family */
350 (char[60]) { }, /* address */
351 NULL };
352 char *argv[] = {usermode_helper, cmd, tconn->name, NULL };
353 int ret;
354
355 setup_khelper_env(tconn, envp);
356 conn_md_sync(tconn);
357
358 conn_info(tconn, "helper command: %s %s %s\n", usermode_helper, cmd, tconn->name);
359 /* TODO: conn_bcast_event() ?? */
360
361 ret = call_usermodehelper(usermode_helper, argv, envp, 1);
362 if (ret)
363 conn_warn(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
364 usermode_helper, cmd, tconn->name,
365 (ret >> 8) & 0xff, ret);
366 else
367 conn_info(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
368 usermode_helper, cmd, tconn->name,
369 (ret >> 8) & 0xff, ret);
370 /* TODO: conn_bcast_event() ?? */
371
372 if (ret < 0) /* Ignore any ERRNOs we got. */
373 ret = 0;
374
375 return ret;
376}
377
Philipp Reisnercb703452011-03-24 11:03:07 +0100378static enum drbd_fencing_p highest_fencing_policy(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700379{
Philipp Reisnercb703452011-03-24 11:03:07 +0100380 enum drbd_fencing_p fp = FP_NOT_AVAIL;
381 struct drbd_conf *mdev;
382 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700383
Philipp Reisner695d08f2011-04-11 22:53:32 -0700384 rcu_read_lock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100385 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
386 if (get_ldev_if_state(mdev, D_CONSISTENT)) {
387 fp = max_t(enum drbd_fencing_p, fp, mdev->ldev->dc.fencing);
388 put_ldev(mdev);
389 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700390 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700391 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700392
Philipp Reisnercb703452011-03-24 11:03:07 +0100393 return fp;
394}
395
396bool conn_try_outdate_peer(struct drbd_tconn *tconn)
397{
398 union drbd_state mask = { };
399 union drbd_state val = { };
400 enum drbd_fencing_p fp;
401 char *ex_to_string;
402 int r;
403
404 if (tconn->cstate >= C_WF_REPORT_PARAMS) {
405 conn_err(tconn, "Expected cstate < C_WF_REPORT_PARAMS\n");
406 return false;
407 }
408
409 fp = highest_fencing_policy(tconn);
410 switch (fp) {
411 case FP_NOT_AVAIL:
412 conn_warn(tconn, "Not fencing peer, I'm not even Consistent myself.\n");
413 goto out;
414 case FP_DONT_CARE:
415 return true;
416 default: ;
417 }
418
419 r = conn_khelper(tconn, "fence-peer");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700420
421 switch ((r>>8) & 0xff) {
422 case 3: /* peer is inconsistent */
423 ex_to_string = "peer is inconsistent or worse";
Philipp Reisnercb703452011-03-24 11:03:07 +0100424 mask.pdsk = D_MASK;
425 val.pdsk = D_INCONSISTENT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700426 break;
427 case 4: /* peer got outdated, or was already outdated */
428 ex_to_string = "peer was fenced";
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 break;
432 case 5: /* peer was down */
Philipp Reisnercb703452011-03-24 11:03:07 +0100433 if (conn_highest_disk(tconn) == D_UP_TO_DATE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700434 /* we will(have) create(d) a new UUID anyways... */
435 ex_to_string = "peer is unreachable, assumed to be dead";
Philipp Reisnercb703452011-03-24 11:03:07 +0100436 mask.pdsk = D_MASK;
437 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700438 } else {
439 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
Philipp Reisnerb411b362009-09-25 16:07:19 -0700440 }
441 break;
442 case 6: /* Peer is primary, voluntarily outdate myself.
443 * This is useful when an unconnected R_SECONDARY is asked to
444 * become R_PRIMARY, but finds the other peer being active. */
445 ex_to_string = "peer is active";
Philipp Reisnercb703452011-03-24 11:03:07 +0100446 conn_warn(tconn, "Peer is primary, outdating myself.\n");
447 mask.disk = D_MASK;
448 val.disk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700449 break;
450 case 7:
451 if (fp != FP_STONITH)
Philipp Reisnercb703452011-03-24 11:03:07 +0100452 conn_err(tconn, "fence-peer() = 7 && fencing != Stonith !!!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700453 ex_to_string = "peer was stonithed";
Philipp Reisnercb703452011-03-24 11:03:07 +0100454 mask.pdsk = D_MASK;
455 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700456 break;
457 default:
458 /* The script is broken ... */
Philipp Reisnercb703452011-03-24 11:03:07 +0100459 conn_err(tconn, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
460 return false; /* Eventually leave IO frozen */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700461 }
462
Philipp Reisnercb703452011-03-24 11:03:07 +0100463 conn_info(tconn, "fence-peer helper returned %d (%s)\n",
464 (r>>8) & 0xff, ex_to_string);
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200465
Philipp Reisnercb703452011-03-24 11:03:07 +0100466 out:
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200467
Philipp Reisnercb703452011-03-24 11:03:07 +0100468 /* Not using
469 conn_request_state(tconn, mask, val, CS_VERBOSE);
470 here, because we might were able to re-establish the connection in the
471 meantime. */
472 spin_lock_irq(&tconn->req_lock);
473 if (tconn->cstate < C_WF_REPORT_PARAMS)
474 _conn_request_state(tconn, mask, val, CS_VERBOSE);
475 spin_unlock_irq(&tconn->req_lock);
476
477 return conn_highest_pdsk(tconn) <= D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700478}
479
Philipp Reisner87f7be42010-06-11 13:56:33 +0200480static int _try_outdate_peer_async(void *data)
481{
Philipp Reisnercb703452011-03-24 11:03:07 +0100482 struct drbd_tconn *tconn = (struct drbd_tconn *)data;
Philipp Reisner87f7be42010-06-11 13:56:33 +0200483
Philipp Reisnercb703452011-03-24 11:03:07 +0100484 conn_try_outdate_peer(tconn);
Philipp Reisner87f7be42010-06-11 13:56:33 +0200485
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200486 kref_put(&tconn->kref, &conn_destroy);
Philipp Reisner87f7be42010-06-11 13:56:33 +0200487 return 0;
488}
489
Philipp Reisnercb703452011-03-24 11:03:07 +0100490void conn_try_outdate_peer_async(struct drbd_tconn *tconn)
Philipp Reisner87f7be42010-06-11 13:56:33 +0200491{
492 struct task_struct *opa;
493
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200494 kref_get(&tconn->kref);
Philipp Reisnercb703452011-03-24 11:03:07 +0100495 opa = kthread_run(_try_outdate_peer_async, tconn, "drbd_async_h");
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200496 if (IS_ERR(opa)) {
Philipp Reisnercb703452011-03-24 11:03:07 +0100497 conn_err(tconn, "out of mem, failed to invoke fence-peer helper\n");
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200498 kref_put(&tconn->kref, &conn_destroy);
499 }
Philipp Reisner87f7be42010-06-11 13:56:33 +0200500}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700501
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100502enum drbd_state_rv
503drbd_set_role(struct drbd_conf *mdev, enum drbd_role new_role, int force)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700504{
505 const int max_tries = 4;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100506 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200507 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700508 int try = 0;
509 int forced = 0;
510 union drbd_state mask, val;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700511
512 if (new_role == R_PRIMARY)
Philipp Reisner0625ac12011-02-07 14:49:19 +0100513 request_ping(mdev->tconn); /* Detect a dead peer ASAP */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700514
Philipp Reisner8410da82011-02-11 20:11:10 +0100515 mutex_lock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700516
517 mask.i = 0; mask.role = R_MASK;
518 val.i = 0; val.role = new_role;
519
520 while (try++ < max_tries) {
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100521 rv = _drbd_request_state(mdev, mask, val, CS_WAIT_COMPLETE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700522
523 /* in case we first succeeded to outdate,
524 * but now suddenly could establish a connection */
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100525 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700526 val.pdsk = 0;
527 mask.pdsk = 0;
528 continue;
529 }
530
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100531 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
Philipp Reisnerd10a33c2010-03-04 15:11:39 +0100532 (mdev->state.disk < D_UP_TO_DATE &&
533 mdev->state.disk >= D_INCONSISTENT)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700534 mask.disk = D_MASK;
535 val.disk = D_UP_TO_DATE;
536 forced = 1;
537 continue;
538 }
539
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100540 if (rv == SS_NO_UP_TO_DATE_DISK &&
Philipp Reisnerb411b362009-09-25 16:07:19 -0700541 mdev->state.disk == D_CONSISTENT && mask.pdsk == 0) {
542 D_ASSERT(mdev->state.pdsk == D_UNKNOWN);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700543
Philipp Reisnercb703452011-03-24 11:03:07 +0100544 if (conn_try_outdate_peer(mdev->tconn)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700545 val.disk = D_UP_TO_DATE;
546 mask.disk = D_MASK;
547 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700548 continue;
549 }
550
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100551 if (rv == SS_NOTHING_TO_DO)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100552 goto out;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100553 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
Philipp Reisnercb703452011-03-24 11:03:07 +0100554 if (!conn_try_outdate_peer(mdev->tconn) && force) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700555 dev_warn(DEV, "Forced into split brain situation!\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100556 mask.pdsk = D_MASK;
557 val.pdsk = D_OUTDATED;
558
Philipp Reisnerb411b362009-09-25 16:07:19 -0700559 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700560 continue;
561 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100562 if (rv == SS_TWO_PRIMARIES) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700563 /* Maybe the peer is detected as dead very soon...
564 retry at most once more in this case. */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200565 int timeo;
566 rcu_read_lock();
567 nc = rcu_dereference(mdev->tconn->net_conf);
568 timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
569 rcu_read_unlock();
570 schedule_timeout_interruptible(timeo);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700571 if (try < max_tries)
572 try = max_tries - 1;
573 continue;
574 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100575 if (rv < SS_SUCCESS) {
576 rv = _drbd_request_state(mdev, mask, val,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700577 CS_VERBOSE + CS_WAIT_COMPLETE);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100578 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100579 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700580 }
581 break;
582 }
583
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100584 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100585 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700586
587 if (forced)
588 dev_warn(DEV, "Forced to consider local data as UpToDate!\n");
589
590 /* Wait until nothing is on the fly :) */
591 wait_event(mdev->misc_wait, atomic_read(&mdev->ap_pending_cnt) == 0);
592
593 if (new_role == R_SECONDARY) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100594 set_disk_ro(mdev->vdisk, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700595 if (get_ldev(mdev)) {
596 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
597 put_ldev(mdev);
598 }
599 } else {
Philipp Reisner91fd4da2011-04-20 17:47:29 +0200600 mutex_lock(&mdev->tconn->net_conf_update);
601 nc = mdev->tconn->net_conf;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200602 if (nc)
Philipp Reisner91fd4da2011-04-20 17:47:29 +0200603 nc->want_lose = 0; /* without copy; single bit op is atomic */
604 mutex_unlock(&mdev->tconn->net_conf_update);
605
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100606 set_disk_ro(mdev->vdisk, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700607 if (get_ldev(mdev)) {
608 if (((mdev->state.conn < C_CONNECTED ||
609 mdev->state.pdsk <= D_FAILED)
610 && mdev->ldev->md.uuid[UI_BITMAP] == 0) || forced)
611 drbd_uuid_new_current(mdev);
612
613 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
614 put_ldev(mdev);
615 }
616 }
617
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100618 /* writeout of activity log covered areas of the bitmap
619 * to stable storage done in after state change already */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700620
621 if (mdev->state.conn >= C_WF_REPORT_PARAMS) {
622 /* if this was forced, we should consider sync */
623 if (forced)
624 drbd_send_uuids(mdev);
625 drbd_send_state(mdev);
626 }
627
628 drbd_md_sync(mdev);
629
630 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100631out:
Philipp Reisner8410da82011-02-11 20:11:10 +0100632 mutex_unlock(mdev->state_mutex);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100633 return rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700634}
635
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100636static const char *from_attrs_err_to_txt(int err)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700637{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100638 return err == -ENOMSG ? "required attribute missing" :
639 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
Lars Ellenbergf3990022011-03-23 14:31:09 +0100640 err == -EEXIST ? "can not change invariant setting" :
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100641 "invalid attribute value";
Philipp Reisnerb411b362009-09-25 16:07:19 -0700642}
643
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100644int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700645{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100646 struct set_role_parms parms;
647 int err;
648 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700649
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100650 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
651 if (!adm_ctx.reply_skb)
652 return retcode;
653 if (retcode != NO_ERROR)
654 goto out;
655
656 memset(&parms, 0, sizeof(parms));
657 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +0100658 err = set_role_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100659 if (err) {
660 retcode = ERR_MANDATORY_TAG;
661 drbd_msg_put_info(from_attrs_err_to_txt(err));
662 goto out;
663 }
664 }
665
666 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
667 retcode = drbd_set_role(adm_ctx.mdev, R_PRIMARY, parms.assume_uptodate);
668 else
669 retcode = drbd_set_role(adm_ctx.mdev, R_SECONDARY, 0);
670out:
671 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700672 return 0;
673}
674
675/* initializes the md.*_offset members, so we are able to find
676 * the on disk meta data */
677static void drbd_md_set_sector_offsets(struct drbd_conf *mdev,
678 struct drbd_backing_dev *bdev)
679{
680 sector_t md_size_sect = 0;
681 switch (bdev->dc.meta_dev_idx) {
682 default:
683 /* v07 style fixed size indexed meta data */
684 bdev->md.md_size_sect = MD_RESERVED_SECT;
685 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
686 bdev->md.al_offset = MD_AL_OFFSET;
687 bdev->md.bm_offset = MD_BM_OFFSET;
688 break;
689 case DRBD_MD_INDEX_FLEX_EXT:
690 /* just occupy the full device; unit: sectors */
691 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
692 bdev->md.md_offset = 0;
693 bdev->md.al_offset = MD_AL_OFFSET;
694 bdev->md.bm_offset = MD_BM_OFFSET;
695 break;
696 case DRBD_MD_INDEX_INTERNAL:
697 case DRBD_MD_INDEX_FLEX_INT:
698 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
699 /* al size is still fixed */
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100700 bdev->md.al_offset = -MD_AL_SECTORS;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700701 /* we need (slightly less than) ~ this much bitmap sectors: */
702 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
703 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
704 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
705 md_size_sect = ALIGN(md_size_sect, 8);
706
707 /* plus the "drbd meta data super block",
708 * and the activity log; */
709 md_size_sect += MD_BM_OFFSET;
710
711 bdev->md.md_size_sect = md_size_sect;
712 /* bitmap offset is adjusted by 'super' block size */
713 bdev->md.bm_offset = -md_size_sect + MD_AL_OFFSET;
714 break;
715 }
716}
717
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100718/* input size is expected to be in KB */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700719char *ppsize(char *buf, unsigned long long size)
720{
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100721 /* Needs 9 bytes at max including trailing NUL:
722 * -1ULL ==> "16384 EB" */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700723 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
724 int base = 0;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100725 while (size >= 10000 && base < sizeof(units)-1) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700726 /* shift + round */
727 size = (size >> 10) + !!(size & (1<<9));
728 base++;
729 }
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100730 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700731
732 return buf;
733}
734
735/* there is still a theoretical deadlock when called from receiver
736 * on an D_INCONSISTENT R_PRIMARY:
737 * remote READ does inc_ap_bio, receiver would need to receive answer
738 * packet from remote to dec_ap_bio again.
739 * receiver receive_sizes(), comes here,
740 * waits for ap_bio_cnt == 0. -> deadlock.
741 * but this cannot happen, actually, because:
742 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
743 * (not connected, or bad/no disk on peer):
744 * see drbd_fail_request_early, ap_bio_cnt is zero.
745 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
746 * peer may not initiate a resize.
747 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100748/* Note these are not to be confused with
749 * drbd_adm_suspend_io/drbd_adm_resume_io,
750 * which are (sub) state changes triggered by admin (drbdsetup),
751 * and can be long lived.
752 * This changes an mdev->flag, is triggered by drbd internals,
753 * and should be short-lived. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700754void drbd_suspend_io(struct drbd_conf *mdev)
755{
756 set_bit(SUSPEND_IO, &mdev->flags);
Philipp Reisner2aebfab2011-03-28 16:48:11 +0200757 if (drbd_suspended(mdev))
Philipp Reisner265be2d2010-05-31 10:14:17 +0200758 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700759 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
760}
761
762void drbd_resume_io(struct drbd_conf *mdev)
763{
764 clear_bit(SUSPEND_IO, &mdev->flags);
765 wake_up(&mdev->misc_wait);
766}
767
768/**
769 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
770 * @mdev: DRBD device.
771 *
772 * Returns 0 on success, negative return values indicate errors.
773 * You should call drbd_md_sync() after calling this function.
774 */
Bart Van Assche24c48302011-05-21 18:32:29 +0200775enum 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 -0700776{
777 sector_t prev_first_sect, prev_size; /* previous meta location */
778 sector_t la_size;
779 sector_t size;
780 char ppb[10];
781
782 int md_moved, la_size_changed;
783 enum determine_dev_size rv = unchanged;
784
785 /* race:
786 * application request passes inc_ap_bio,
787 * but then cannot get an AL-reference.
788 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
789 *
790 * to avoid that:
791 * Suspend IO right here.
792 * still lock the act_log to not trigger ASSERTs there.
793 */
794 drbd_suspend_io(mdev);
795
796 /* no wait necessary anymore, actually we could assert that */
797 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
798
799 prev_first_sect = drbd_md_first_sector(mdev->ldev);
800 prev_size = mdev->ldev->md.md_size_sect;
801 la_size = mdev->ldev->md.la_size_sect;
802
803 /* TODO: should only be some assert here, not (re)init... */
804 drbd_md_set_sector_offsets(mdev, mdev->ldev);
805
Philipp Reisnerd8450302010-03-24 15:51:26 +0100806 size = drbd_new_dev_size(mdev, mdev->ldev, flags & DDSF_FORCED);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700807
808 if (drbd_get_capacity(mdev->this_bdev) != size ||
809 drbd_bm_capacity(mdev) != size) {
810 int err;
Philipp Reisner02d9a942010-03-24 16:23:03 +0100811 err = drbd_bm_resize(mdev, size, !(flags & DDSF_NO_RESYNC));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700812 if (unlikely(err)) {
813 /* currently there is only one error: ENOMEM! */
814 size = drbd_bm_capacity(mdev)>>1;
815 if (size == 0) {
816 dev_err(DEV, "OUT OF MEMORY! "
817 "Could not allocate bitmap!\n");
818 } else {
819 dev_err(DEV, "BM resizing failed. "
820 "Leaving size unchanged at size = %lu KB\n",
821 (unsigned long)size);
822 }
823 rv = dev_size_error;
824 }
825 /* racy, see comments above. */
826 drbd_set_my_capacity(mdev, size);
827 mdev->ldev->md.la_size_sect = size;
828 dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
829 (unsigned long long)size>>1);
830 }
831 if (rv == dev_size_error)
832 goto out;
833
834 la_size_changed = (la_size != mdev->ldev->md.la_size_sect);
835
836 md_moved = prev_first_sect != drbd_md_first_sector(mdev->ldev)
837 || prev_size != mdev->ldev->md.md_size_sect;
838
839 if (la_size_changed || md_moved) {
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100840 int err;
841
Philipp Reisnerb411b362009-09-25 16:07:19 -0700842 drbd_al_shrink(mdev); /* All extents inactive. */
843 dev_info(DEV, "Writing the whole bitmap, %s\n",
844 la_size_changed && md_moved ? "size changed and md moved" :
845 la_size_changed ? "size changed" : "md moved");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100846 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
847 err = drbd_bitmap_io(mdev, &drbd_bm_write,
848 "size changed", BM_LOCKED_MASK);
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100849 if (err) {
850 rv = dev_size_error;
851 goto out;
852 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700853 drbd_md_mark_dirty(mdev);
854 }
855
856 if (size > la_size)
857 rv = grew;
858 if (size < la_size)
859 rv = shrunk;
860out:
861 lc_unlock(mdev->act_log);
862 wake_up(&mdev->al_wait);
863 drbd_resume_io(mdev);
864
865 return rv;
866}
867
868sector_t
Philipp Reisnera393db62009-12-22 13:35:52 +0100869drbd_new_dev_size(struct drbd_conf *mdev, struct drbd_backing_dev *bdev, int assume_peer_has_space)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700870{
871 sector_t p_size = mdev->p_size; /* partner's disk size. */
872 sector_t la_size = bdev->md.la_size_sect; /* last agreed size. */
873 sector_t m_size; /* my size */
874 sector_t u_size = bdev->dc.disk_size; /* size requested by user. */
875 sector_t size = 0;
876
877 m_size = drbd_get_max_capacity(bdev);
878
Philipp Reisnera393db62009-12-22 13:35:52 +0100879 if (mdev->state.conn < C_CONNECTED && assume_peer_has_space) {
880 dev_warn(DEV, "Resize while not connected was forced by the user!\n");
881 p_size = m_size;
882 }
883
Philipp Reisnerb411b362009-09-25 16:07:19 -0700884 if (p_size && m_size) {
885 size = min_t(sector_t, p_size, m_size);
886 } else {
887 if (la_size) {
888 size = la_size;
889 if (m_size && m_size < size)
890 size = m_size;
891 if (p_size && p_size < size)
892 size = p_size;
893 } else {
894 if (m_size)
895 size = m_size;
896 if (p_size)
897 size = p_size;
898 }
899 }
900
901 if (size == 0)
902 dev_err(DEV, "Both nodes diskless!\n");
903
904 if (u_size) {
905 if (u_size > size)
906 dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n",
907 (unsigned long)u_size>>1, (unsigned long)size>>1);
908 else
909 size = u_size;
910 }
911
912 return size;
913}
914
915/**
916 * drbd_check_al_size() - Ensures that the AL is of the right size
917 * @mdev: DRBD device.
918 *
919 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
920 * failed, and 0 on success. You should call drbd_md_sync() after you called
921 * this function.
922 */
Lars Ellenbergf3990022011-03-23 14:31:09 +0100923static int drbd_check_al_size(struct drbd_conf *mdev, struct disk_conf *dc)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700924{
925 struct lru_cache *n, *t;
926 struct lc_element *e;
927 unsigned int in_use;
928 int i;
929
Lars Ellenbergf3990022011-03-23 14:31:09 +0100930 if (!expect(dc->al_extents >= DRBD_AL_EXTENTS_MIN))
931 dc->al_extents = DRBD_AL_EXTENTS_MIN;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700932
933 if (mdev->act_log &&
Lars Ellenbergf3990022011-03-23 14:31:09 +0100934 mdev->act_log->nr_elements == dc->al_extents)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700935 return 0;
936
937 in_use = 0;
938 t = mdev->act_log;
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100939 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
Lars Ellenbergf3990022011-03-23 14:31:09 +0100940 dc->al_extents, sizeof(struct lc_element), 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700941
942 if (n == NULL) {
943 dev_err(DEV, "Cannot allocate act_log lru!\n");
944 return -ENOMEM;
945 }
946 spin_lock_irq(&mdev->al_lock);
947 if (t) {
948 for (i = 0; i < t->nr_elements; i++) {
949 e = lc_element_by_index(t, i);
950 if (e->refcnt)
951 dev_err(DEV, "refcnt(%d)==%d\n",
952 e->lc_number, e->refcnt);
953 in_use += e->refcnt;
954 }
955 }
956 if (!in_use)
957 mdev->act_log = n;
958 spin_unlock_irq(&mdev->al_lock);
959 if (in_use) {
960 dev_err(DEV, "Activity log still in use!\n");
961 lc_destroy(n);
962 return -EBUSY;
963 } else {
964 if (t)
965 lc_destroy(t);
966 }
967 drbd_md_mark_dirty(mdev); /* we changed mdev->act_log->nr_elemens */
968 return 0;
969}
970
Philipp Reisner99432fc2011-05-20 16:39:13 +0200971static void drbd_setup_queue_param(struct drbd_conf *mdev, unsigned int max_bio_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700972{
973 struct request_queue * const q = mdev->rq_queue;
Philipp Reisner99432fc2011-05-20 16:39:13 +0200974 int max_hw_sectors = max_bio_size >> 9;
975 int max_segments = 0;
976
977 if (get_ldev_if_state(mdev, D_ATTACHING)) {
978 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
979
980 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
981 max_segments = mdev->ldev->dc.max_bio_bvecs;
982 put_ldev(mdev);
983 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700984
Philipp Reisnerb411b362009-09-25 16:07:19 -0700985 blk_queue_logical_block_size(q, 512);
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100986 blk_queue_max_hw_sectors(q, max_hw_sectors);
987 /* This is the workaround for "bio would need to, but cannot, be split" */
988 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
989 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700990
Philipp Reisner99432fc2011-05-20 16:39:13 +0200991 if (get_ldev_if_state(mdev, D_ATTACHING)) {
992 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700993
Philipp Reisner99432fc2011-05-20 16:39:13 +0200994 blk_queue_stack_limits(q, b);
995
996 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
997 dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
998 q->backing_dev_info.ra_pages,
999 b->backing_dev_info.ra_pages);
1000 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1001 }
1002 put_ldev(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001003 }
1004}
1005
Philipp Reisner99432fc2011-05-20 16:39:13 +02001006void drbd_reconsider_max_bio_size(struct drbd_conf *mdev)
1007{
1008 int now, new, local, peer;
1009
1010 now = queue_max_hw_sectors(mdev->rq_queue) << 9;
1011 local = mdev->local_max_bio_size; /* Eventually last known value, from volatile memory */
1012 peer = mdev->peer_max_bio_size; /* Eventually last known value, from meta data */
1013
1014 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1015 local = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
1016 mdev->local_max_bio_size = local;
1017 put_ldev(mdev);
1018 }
1019
1020 /* We may ignore peer limits if the peer is modern enough.
1021 Because new from 8.3.8 onwards the peer can use multiple
1022 BIOs for a single peer_request */
1023 if (mdev->state.conn >= C_CONNECTED) {
Philipp Reisner31890f42011-01-19 14:12:51 +01001024 if (mdev->tconn->agreed_pro_version < 94)
Philipp Reisner99432fc2011-05-20 16:39:13 +02001025 peer = mdev->peer_max_bio_size;
Philipp Reisner31890f42011-01-19 14:12:51 +01001026 else if (mdev->tconn->agreed_pro_version == 94)
Philipp Reisner99432fc2011-05-20 16:39:13 +02001027 peer = DRBD_MAX_SIZE_H80_PACKET;
1028 else /* drbd 8.3.8 onwards */
1029 peer = DRBD_MAX_BIO_SIZE;
1030 }
1031
1032 new = min_t(int, local, peer);
1033
1034 if (mdev->state.role == R_PRIMARY && new < now)
1035 dev_err(DEV, "ASSERT FAILED new < now; (%d < %d)\n", new, now);
1036
1037 if (new != now)
1038 dev_info(DEV, "max BIO size = %u\n", new);
1039
1040 drbd_setup_queue_param(mdev, new);
1041}
1042
Philipp Reisnera18e9d12011-04-24 11:09:55 +02001043/* Starts the worker thread */
Philipp Reisner0e29d162011-02-18 14:23:11 +01001044static void conn_reconfig_start(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001045{
Philipp Reisner0e29d162011-02-18 14:23:11 +01001046 drbd_thread_start(&tconn->worker);
1047 conn_flush_workqueue(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001048}
1049
Philipp Reisnera18e9d12011-04-24 11:09:55 +02001050/* if still unconfigured, stops worker again. */
Philipp Reisner0e29d162011-02-18 14:23:11 +01001051static void conn_reconfig_done(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001052{
Philipp Reisner0e29d162011-02-18 14:23:11 +01001053 spin_lock_irq(&tconn->req_lock);
Philipp Reisnera18e9d12011-04-24 11:09:55 +02001054 if (conn_all_vols_unconf(tconn))
Philipp Reisner0e29d162011-02-18 14:23:11 +01001055 drbd_thread_stop_nowait(&tconn->worker);
Philipp Reisner0e29d162011-02-18 14:23:11 +01001056 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001057}
1058
Philipp Reisner07782862010-08-31 12:00:50 +02001059/* Make sure IO is suspended before calling this function(). */
1060static void drbd_suspend_al(struct drbd_conf *mdev)
1061{
1062 int s = 0;
1063
Lars Ellenberg61610422011-02-21 13:20:54 +01001064 if (!lc_try_lock(mdev->act_log)) {
Philipp Reisner07782862010-08-31 12:00:50 +02001065 dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n");
1066 return;
1067 }
1068
Lars Ellenberg61610422011-02-21 13:20:54 +01001069 drbd_al_shrink(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001070 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner07782862010-08-31 12:00:50 +02001071 if (mdev->state.conn < C_CONNECTED)
1072 s = !test_and_set_bit(AL_SUSPENDED, &mdev->flags);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001073 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg61610422011-02-21 13:20:54 +01001074 lc_unlock(mdev->act_log);
Philipp Reisner07782862010-08-31 12:00:50 +02001075
1076 if (s)
1077 dev_info(DEV, "Suspended AL updates\n");
1078}
1079
Lars Ellenbergf3990022011-03-23 14:31:09 +01001080int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1081{
1082 enum drbd_ret_code retcode;
1083 struct drbd_conf *mdev;
1084 struct disk_conf *ndc; /* new disk conf */
1085 int err, fifo_size;
1086 int *rs_plan_s = NULL;
1087
1088 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1089 if (!adm_ctx.reply_skb)
1090 return retcode;
1091 if (retcode != NO_ERROR)
1092 goto out;
1093
1094 mdev = adm_ctx.mdev;
1095
1096 /* we also need a disk
1097 * to change the options on */
1098 if (!get_ldev(mdev)) {
1099 retcode = ERR_NO_DISK;
1100 goto out;
1101 }
1102
1103/* FIXME freeze IO, cluster wide.
1104 *
1105 * We should make sure no-one uses
1106 * some half-updated struct when we
1107 * assign it later. */
1108
1109 ndc = kmalloc(sizeof(*ndc), GFP_KERNEL);
1110 if (!ndc) {
1111 retcode = ERR_NOMEM;
1112 goto fail;
1113 }
1114
1115 memcpy(ndc, &mdev->ldev->dc, sizeof(*ndc));
1116 err = disk_conf_from_attrs_for_change(ndc, info);
1117 if (err) {
1118 retcode = ERR_MANDATORY_TAG;
1119 drbd_msg_put_info(from_attrs_err_to_txt(err));
1120 }
1121
1122 if (!expect(ndc->resync_rate >= 1))
1123 ndc->resync_rate = 1;
1124
1125 /* clip to allowed range */
1126 if (!expect(ndc->al_extents >= DRBD_AL_EXTENTS_MIN))
1127 ndc->al_extents = DRBD_AL_EXTENTS_MIN;
1128 if (!expect(ndc->al_extents <= DRBD_AL_EXTENTS_MAX))
1129 ndc->al_extents = DRBD_AL_EXTENTS_MAX;
1130
1131 /* most sanity checks done, try to assign the new sync-after
1132 * dependency. need to hold the global lock in there,
1133 * to avoid a race in the dependency loop check. */
1134 retcode = drbd_alter_sa(mdev, ndc->resync_after);
1135 if (retcode != NO_ERROR)
1136 goto fail;
1137
1138 fifo_size = (ndc->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
1139 if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) {
1140 rs_plan_s = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL);
1141 if (!rs_plan_s) {
1142 dev_err(DEV, "kmalloc of fifo_buffer failed");
1143 retcode = ERR_NOMEM;
1144 goto fail;
1145 }
1146 }
1147
1148 if (fifo_size != mdev->rs_plan_s.size) {
1149 kfree(mdev->rs_plan_s.values);
1150 mdev->rs_plan_s.values = rs_plan_s;
1151 mdev->rs_plan_s.size = fifo_size;
1152 mdev->rs_planed = 0;
1153 rs_plan_s = NULL;
1154 }
1155
1156 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
1157 drbd_al_shrink(mdev);
1158 err = drbd_check_al_size(mdev, ndc);
1159 lc_unlock(mdev->act_log);
1160 wake_up(&mdev->al_wait);
1161
1162 if (err) {
1163 retcode = ERR_NOMEM;
1164 goto fail;
1165 }
1166
1167 /* FIXME
1168 * To avoid someone looking at a half-updated struct, we probably
1169 * should have a rw-semaphor on net_conf and disk_conf.
1170 */
1171 mdev->ldev->dc = *ndc;
1172
1173 drbd_md_sync(mdev);
1174
1175
1176 if (mdev->state.conn >= C_CONNECTED)
1177 drbd_send_sync_param(mdev);
1178
1179 fail:
1180 put_ldev(mdev);
1181 kfree(ndc);
1182 kfree(rs_plan_s);
1183 out:
1184 drbd_adm_finish(info, retcode);
1185 return 0;
1186}
1187
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001188int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001189{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001190 struct drbd_conf *mdev;
1191 int err;
Andreas Gruenbacher116676c2010-12-08 13:33:11 +01001192 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001193 enum determine_dev_size dd;
1194 sector_t max_possible_sectors;
1195 sector_t min_md_device_sectors;
1196 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
Tejun Heoe525fd82010-11-13 11:55:17 +01001197 struct block_device *bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001198 struct lru_cache *resync_lru = NULL;
1199 union drbd_state ns, os;
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001200 enum drbd_state_rv rv;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001201 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001202 int cp_discovered = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001203
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001204 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1205 if (!adm_ctx.reply_skb)
1206 return retcode;
1207 if (retcode != NO_ERROR)
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001208 goto finish;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001209
1210 mdev = adm_ctx.mdev;
Philipp Reisner0e29d162011-02-18 14:23:11 +01001211 conn_reconfig_start(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001212
1213 /* if you want to reconfigure, please tear down first */
1214 if (mdev->state.disk > D_DISKLESS) {
1215 retcode = ERR_DISK_CONFIGURED;
1216 goto fail;
1217 }
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001218 /* It may just now have detached because of IO error. Make sure
1219 * drbd_ldev_destroy is done already, we may end up here very fast,
1220 * e.g. if someone calls attach from the on-io-error handler,
1221 * to realize a "hot spare" feature (not that I'd recommend that) */
1222 wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001223
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001224 /* allocation not in the IO path, drbdsetup context */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001225 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1226 if (!nbc) {
1227 retcode = ERR_NOMEM;
1228 goto fail;
1229 }
1230
Lars Ellenbergf3990022011-03-23 14:31:09 +01001231 nbc->dc = (struct disk_conf) {
1232 {}, 0, /* backing_dev */
1233 {}, 0, /* meta_dev */
1234 0, /* meta_dev_idx */
1235 DRBD_DISK_SIZE_SECT_DEF, /* disk_size */
1236 DRBD_MAX_BIO_BVECS_DEF, /* max_bio_bvecs */
1237 DRBD_ON_IO_ERROR_DEF, /* on_io_error */
1238 DRBD_FENCING_DEF, /* fencing */
1239 DRBD_RATE_DEF, /* resync_rate */
1240 DRBD_AFTER_DEF, /* resync_after */
1241 DRBD_AL_EXTENTS_DEF, /* al_extents */
1242 DRBD_C_PLAN_AHEAD_DEF, /* c_plan_ahead */
1243 DRBD_C_DELAY_TARGET_DEF, /* c_delay_target */
1244 DRBD_C_FILL_TARGET_DEF, /* c_fill_target */
1245 DRBD_C_MAX_RATE_DEF, /* c_max_rate */
1246 DRBD_C_MIN_RATE_DEF, /* c_min_rate */
1247 0, /* no_disk_barrier */
1248 0, /* no_disk_flush */
1249 0, /* no_disk_drain */
1250 0, /* no_md_flush */
1251 };
Philipp Reisnerb411b362009-09-25 16:07:19 -07001252
Lars Ellenbergf3990022011-03-23 14:31:09 +01001253 err = disk_conf_from_attrs(&nbc->dc, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001254 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001255 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001256 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001257 goto fail;
1258 }
1259
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001260 if ((int)nbc->dc.meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001261 retcode = ERR_MD_IDX_INVALID;
1262 goto fail;
1263 }
1264
Philipp Reisner44ed1672011-04-19 17:10:19 +02001265 rcu_read_lock();
1266 nc = rcu_dereference(mdev->tconn->net_conf);
1267 if (nc) {
1268 if (nbc->dc.fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
1269 rcu_read_unlock();
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001270 retcode = ERR_STONITH_AND_PROT_A;
1271 goto fail;
1272 }
1273 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02001274 rcu_read_unlock();
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001275
Tejun Heod4d77622010-11-13 11:55:18 +01001276 bdev = blkdev_get_by_path(nbc->dc.backing_dev,
1277 FMODE_READ | FMODE_WRITE | FMODE_EXCL, mdev);
Tejun Heoe525fd82010-11-13 11:55:17 +01001278 if (IS_ERR(bdev)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001279 dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.backing_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001280 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001281 retcode = ERR_OPEN_DISK;
1282 goto fail;
1283 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001284 nbc->backing_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001285
Tejun Heoe525fd82010-11-13 11:55:17 +01001286 /*
1287 * meta_dev_idx >= 0: external fixed size, possibly multiple
1288 * drbd sharing one meta device. TODO in that case, paranoia
1289 * check that [md_bdev, meta_dev_idx] is not yet used by some
1290 * other drbd minor! (if you use drbd.conf + drbdadm, that
1291 * should check it for you already; but if you don't, or
1292 * someone fooled it, we need to double check here)
1293 */
Tejun Heod4d77622010-11-13 11:55:18 +01001294 bdev = blkdev_get_by_path(nbc->dc.meta_dev,
1295 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001296 ((int)nbc->dc.meta_dev_idx < 0) ?
Tejun Heod4d77622010-11-13 11:55:18 +01001297 (void *)mdev : (void *)drbd_m_holder);
Tejun Heoe525fd82010-11-13 11:55:17 +01001298 if (IS_ERR(bdev)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001299 dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.meta_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001300 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001301 retcode = ERR_OPEN_MD_DISK;
1302 goto fail;
1303 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001304 nbc->md_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001305
Tejun Heoe525fd82010-11-13 11:55:17 +01001306 if ((nbc->backing_bdev == nbc->md_bdev) !=
1307 (nbc->dc.meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1308 nbc->dc.meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
1309 retcode = ERR_MD_IDX_INVALID;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001310 goto fail;
1311 }
1312
1313 resync_lru = lc_create("resync", drbd_bm_ext_cache,
Lars Ellenberg46a15bc2011-02-21 13:21:01 +01001314 1, 61, sizeof(struct bm_extent),
Philipp Reisnerb411b362009-09-25 16:07:19 -07001315 offsetof(struct bm_extent, lce));
1316 if (!resync_lru) {
1317 retcode = ERR_NOMEM;
Tejun Heoe525fd82010-11-13 11:55:17 +01001318 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001319 }
1320
1321 /* RT - for drbd_get_max_capacity() DRBD_MD_INDEX_FLEX_INT */
1322 drbd_md_set_sector_offsets(mdev, nbc);
1323
1324 if (drbd_get_max_capacity(nbc) < nbc->dc.disk_size) {
1325 dev_err(DEV, "max capacity %llu smaller than disk size %llu\n",
1326 (unsigned long long) drbd_get_max_capacity(nbc),
1327 (unsigned long long) nbc->dc.disk_size);
1328 retcode = ERR_DISK_TO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001329 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001330 }
1331
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001332 if ((int)nbc->dc.meta_dev_idx < 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001333 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1334 /* at least one MB, otherwise it does not make sense */
1335 min_md_device_sectors = (2<<10);
1336 } else {
1337 max_possible_sectors = DRBD_MAX_SECTORS;
1338 min_md_device_sectors = MD_RESERVED_SECT * (nbc->dc.meta_dev_idx + 1);
1339 }
1340
Philipp Reisnerb411b362009-09-25 16:07:19 -07001341 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
1342 retcode = ERR_MD_DISK_TO_SMALL;
1343 dev_warn(DEV, "refusing attach: md-device too small, "
1344 "at least %llu sectors needed for this meta-disk type\n",
1345 (unsigned long long) min_md_device_sectors);
Tejun Heoe525fd82010-11-13 11:55:17 +01001346 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001347 }
1348
1349 /* Make sure the new disk is big enough
1350 * (we may currently be R_PRIMARY with no local disk...) */
1351 if (drbd_get_max_capacity(nbc) <
1352 drbd_get_capacity(mdev->this_bdev)) {
1353 retcode = ERR_DISK_TO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001354 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001355 }
1356
1357 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1358
Lars Ellenberg13529942009-10-12 19:07:49 +02001359 if (nbc->known_size > max_possible_sectors) {
1360 dev_warn(DEV, "==> truncating very big lower level device "
1361 "to currently maximum possible %llu sectors <==\n",
1362 (unsigned long long) max_possible_sectors);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001363 if ((int)nbc->dc.meta_dev_idx >= 0)
Lars Ellenberg13529942009-10-12 19:07:49 +02001364 dev_warn(DEV, "==>> using internal or flexible "
1365 "meta data may help <<==\n");
1366 }
1367
Philipp Reisnerb411b362009-09-25 16:07:19 -07001368 drbd_suspend_io(mdev);
1369 /* also wait for the last barrier ack. */
Philipp Reisner2aebfab2011-03-28 16:48:11 +02001370 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_pending_cnt) || drbd_suspended(mdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001371 /* and for any other previously queued work */
Philipp Reisnera21e9292011-02-08 15:08:49 +01001372 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001373
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001374 rv = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE);
1375 retcode = rv; /* FIXME: Type mismatch. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001376 drbd_resume_io(mdev);
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001377 if (rv < SS_SUCCESS)
Tejun Heoe525fd82010-11-13 11:55:17 +01001378 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001379
1380 if (!get_ldev_if_state(mdev, D_ATTACHING))
1381 goto force_diskless;
1382
1383 drbd_md_set_sector_offsets(mdev, nbc);
1384
1385 if (!mdev->bitmap) {
1386 if (drbd_bm_init(mdev)) {
1387 retcode = ERR_NOMEM;
1388 goto force_diskless_dec;
1389 }
1390 }
1391
1392 retcode = drbd_md_read(mdev, nbc);
1393 if (retcode != NO_ERROR)
1394 goto force_diskless_dec;
1395
1396 if (mdev->state.conn < C_CONNECTED &&
1397 mdev->state.role == R_PRIMARY &&
1398 (mdev->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
1399 dev_err(DEV, "Can only attach to data with current UUID=%016llX\n",
1400 (unsigned long long)mdev->ed_uuid);
1401 retcode = ERR_DATA_NOT_CURRENT;
1402 goto force_diskless_dec;
1403 }
1404
1405 /* Since we are diskless, fix the activity log first... */
Lars Ellenbergf3990022011-03-23 14:31:09 +01001406 if (drbd_check_al_size(mdev, &nbc->dc)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001407 retcode = ERR_NOMEM;
1408 goto force_diskless_dec;
1409 }
1410
1411 /* Prevent shrinking of consistent devices ! */
1412 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
Philipp Reisnera393db62009-12-22 13:35:52 +01001413 drbd_new_dev_size(mdev, nbc, 0) < nbc->md.la_size_sect) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001414 dev_warn(DEV, "refusing to truncate a consistent device\n");
1415 retcode = ERR_DISK_TO_SMALL;
1416 goto force_diskless_dec;
1417 }
1418
1419 if (!drbd_al_read_log(mdev, nbc)) {
1420 retcode = ERR_IO_MD_DISK;
1421 goto force_diskless_dec;
1422 }
1423
Philipp Reisnerb411b362009-09-25 16:07:19 -07001424 /* Reset the "barriers don't work" bits here, then force meta data to
1425 * be written, to ensure we determine if barriers are supported. */
1426 if (nbc->dc.no_md_flush)
Philipp Reisnera8a4e512010-08-25 10:21:04 +02001427 set_bit(MD_NO_FUA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001428 else
Philipp Reisnera8a4e512010-08-25 10:21:04 +02001429 clear_bit(MD_NO_FUA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001430
1431 /* Point of no return reached.
1432 * Devices and memory are no longer released by error cleanup below.
1433 * now mdev takes over responsibility, and the state engine should
1434 * clean it up somewhere. */
1435 D_ASSERT(mdev->ldev == NULL);
1436 mdev->ldev = nbc;
1437 mdev->resync = resync_lru;
1438 nbc = NULL;
1439 resync_lru = NULL;
1440
Philipp Reisner2451fc32010-08-24 13:43:11 +02001441 mdev->write_ordering = WO_bdev_flush;
1442 drbd_bump_write_ordering(mdev, WO_bdev_flush);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001443
1444 if (drbd_md_test_flag(mdev->ldev, MDF_CRASHED_PRIMARY))
1445 set_bit(CRASHED_PRIMARY, &mdev->flags);
1446 else
1447 clear_bit(CRASHED_PRIMARY, &mdev->flags);
1448
Philipp Reisner894c6a92010-06-18 16:03:20 +02001449 if (drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
Philipp Reisnerda9fbc22011-03-29 10:52:01 +02001450 !(mdev->state.role == R_PRIMARY && mdev->tconn->susp_nod)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001451 set_bit(CRASHED_PRIMARY, &mdev->flags);
1452 cp_discovered = 1;
1453 }
1454
1455 mdev->send_cnt = 0;
1456 mdev->recv_cnt = 0;
1457 mdev->read_cnt = 0;
1458 mdev->writ_cnt = 0;
1459
Philipp Reisner99432fc2011-05-20 16:39:13 +02001460 drbd_reconsider_max_bio_size(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001461
1462 /* If I am currently not R_PRIMARY,
1463 * but meta data primary indicator is set,
1464 * I just now recover from a hard crash,
1465 * and have been R_PRIMARY before that crash.
1466 *
1467 * Now, if I had no connection before that crash
1468 * (have been degraded R_PRIMARY), chances are that
1469 * I won't find my peer now either.
1470 *
1471 * In that case, and _only_ in that case,
1472 * we use the degr-wfc-timeout instead of the default,
1473 * so we can automatically recover from a crash of a
1474 * degraded but active "cluster" after a certain timeout.
1475 */
1476 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
1477 if (mdev->state.role != R_PRIMARY &&
1478 drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
1479 !drbd_md_test_flag(mdev->ldev, MDF_CONNECTED_IND))
1480 set_bit(USE_DEGR_WFC_T, &mdev->flags);
1481
Bart Van Assche24c48302011-05-21 18:32:29 +02001482 dd = drbd_determine_dev_size(mdev, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001483 if (dd == dev_size_error) {
1484 retcode = ERR_NOMEM_BITMAP;
1485 goto force_diskless_dec;
1486 } else if (dd == grew)
1487 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
1488
1489 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
1490 dev_info(DEV, "Assuming that all blocks are out of sync "
1491 "(aka FullSync)\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001492 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write,
1493 "set_n_write from attaching", BM_LOCKED_MASK)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001494 retcode = ERR_IO_MD_DISK;
1495 goto force_diskless_dec;
1496 }
1497 } else {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001498 if (drbd_bitmap_io(mdev, &drbd_bm_read,
Andreas Gruenbacher22ab6a32010-12-13 01:44:11 +01001499 "read from attaching", BM_LOCKED_MASK)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001500 retcode = ERR_IO_MD_DISK;
1501 goto force_diskless_dec;
1502 }
1503 }
1504
1505 if (cp_discovered) {
1506 drbd_al_apply_to_bm(mdev);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001507 if (drbd_bitmap_io(mdev, &drbd_bm_write,
1508 "crashed primary apply AL", BM_LOCKED_MASK)) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001509 retcode = ERR_IO_MD_DISK;
1510 goto force_diskless_dec;
1511 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001512 }
1513
Philipp Reisner07782862010-08-31 12:00:50 +02001514 if (_drbd_bm_total_weight(mdev) == drbd_bm_bits(mdev))
1515 drbd_suspend_al(mdev); /* IO is still suspended here... */
1516
Philipp Reisner87eeee42011-01-19 14:16:30 +01001517 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner78bae592011-03-28 15:40:12 +02001518 os = drbd_read_state(mdev);
1519 ns = os;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001520 /* If MDF_CONSISTENT is not set go into inconsistent state,
1521 otherwise investigate MDF_WasUpToDate...
1522 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1523 otherwise into D_CONSISTENT state.
1524 */
1525 if (drbd_md_test_flag(mdev->ldev, MDF_CONSISTENT)) {
1526 if (drbd_md_test_flag(mdev->ldev, MDF_WAS_UP_TO_DATE))
1527 ns.disk = D_CONSISTENT;
1528 else
1529 ns.disk = D_OUTDATED;
1530 } else {
1531 ns.disk = D_INCONSISTENT;
1532 }
1533
1534 if (drbd_md_test_flag(mdev->ldev, MDF_PEER_OUT_DATED))
1535 ns.pdsk = D_OUTDATED;
1536
1537 if ( ns.disk == D_CONSISTENT &&
1538 (ns.pdsk == D_OUTDATED || mdev->ldev->dc.fencing == FP_DONT_CARE))
1539 ns.disk = D_UP_TO_DATE;
1540
1541 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1542 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1543 this point, because drbd_request_state() modifies these
1544 flags. */
1545
1546 /* In case we are C_CONNECTED postpone any decision on the new disk
1547 state after the negotiation phase. */
1548 if (mdev->state.conn == C_CONNECTED) {
1549 mdev->new_state_tmp.i = ns.i;
1550 ns.i = os.i;
1551 ns.disk = D_NEGOTIATING;
Philipp Reisnerdc66c742010-06-02 14:31:29 +02001552
1553 /* We expect to receive up-to-date UUIDs soon.
1554 To avoid a race in receive_state, free p_uuid while
1555 holding req_lock. I.e. atomic with the state change */
1556 kfree(mdev->p_uuid);
1557 mdev->p_uuid = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001558 }
1559
1560 rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001561 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001562
1563 if (rv < SS_SUCCESS)
1564 goto force_diskless_dec;
1565
1566 if (mdev->state.role == R_PRIMARY)
1567 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
1568 else
1569 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
1570
1571 drbd_md_mark_dirty(mdev);
1572 drbd_md_sync(mdev);
1573
1574 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1575 put_ldev(mdev);
Philipp Reisner0e29d162011-02-18 14:23:11 +01001576 conn_reconfig_done(mdev->tconn);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001577 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001578 return 0;
1579
1580 force_diskless_dec:
1581 put_ldev(mdev);
1582 force_diskless:
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001583 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001584 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001585 fail:
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001586 conn_reconfig_done(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001587 if (nbc) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001588 if (nbc->backing_bdev)
1589 blkdev_put(nbc->backing_bdev,
1590 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1591 if (nbc->md_bdev)
1592 blkdev_put(nbc->md_bdev,
1593 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001594 kfree(nbc);
1595 }
1596 lc_destroy(resync_lru);
1597
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001598 finish:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001599 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001600 return 0;
1601}
1602
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001603static int adm_detach(struct drbd_conf *mdev)
1604{
Philipp Reisner19f83c72011-03-29 14:21:03 +02001605 enum drbd_state_rv retcode;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001606 drbd_suspend_io(mdev); /* so no-one is stuck in drbd_al_begin_io */
1607 retcode = drbd_request_state(mdev, NS(disk, D_DISKLESS));
1608 wait_event(mdev->misc_wait,
1609 mdev->state.disk != D_DISKLESS ||
1610 !atomic_read(&mdev->local_cnt));
1611 drbd_resume_io(mdev);
1612 return retcode;
1613}
1614
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001615/* Detaching the disk is a process in multiple stages. First we need to lock
1616 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1617 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1618 * internal references as well.
1619 * Only then we have finally detached. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001620int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001621{
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001622 enum drbd_ret_code retcode;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001623
1624 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1625 if (!adm_ctx.reply_skb)
1626 return retcode;
1627 if (retcode != NO_ERROR)
1628 goto out;
1629
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001630 retcode = adm_detach(adm_ctx.mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001631out:
1632 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001633 return 0;
1634}
1635
Lars Ellenbergf3990022011-03-23 14:31:09 +01001636static bool conn_resync_running(struct drbd_tconn *tconn)
1637{
1638 struct drbd_conf *mdev;
Philipp Reisner695d08f2011-04-11 22:53:32 -07001639 bool rv = false;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001640 int vnr;
1641
Philipp Reisner695d08f2011-04-11 22:53:32 -07001642 rcu_read_lock();
Lars Ellenbergf3990022011-03-23 14:31:09 +01001643 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1644 if (mdev->state.conn == C_SYNC_SOURCE ||
1645 mdev->state.conn == C_SYNC_TARGET ||
1646 mdev->state.conn == C_PAUSED_SYNC_S ||
Philipp Reisner695d08f2011-04-11 22:53:32 -07001647 mdev->state.conn == C_PAUSED_SYNC_T) {
1648 rv = true;
1649 break;
1650 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001651 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001652 rcu_read_unlock();
1653
1654 return rv;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001655}
1656
1657static bool conn_ov_running(struct drbd_tconn *tconn)
1658{
1659 struct drbd_conf *mdev;
Philipp Reisner695d08f2011-04-11 22:53:32 -07001660 bool rv = false;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001661 int vnr;
1662
Philipp Reisner695d08f2011-04-11 22:53:32 -07001663 rcu_read_lock();
Lars Ellenbergf3990022011-03-23 14:31:09 +01001664 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1665 if (mdev->state.conn == C_VERIFY_S ||
Philipp Reisner695d08f2011-04-11 22:53:32 -07001666 mdev->state.conn == C_VERIFY_T) {
1667 rv = true;
1668 break;
1669 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001670 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001671 rcu_read_unlock();
1672
1673 return rv;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001674}
1675
Philipp Reisnercd643972011-04-13 18:00:59 -07001676static enum drbd_ret_code
Philipp Reisner44ed1672011-04-19 17:10:19 +02001677_check_net_options(struct drbd_tconn *tconn, struct net_conf *old_conf, struct net_conf *new_conf)
Philipp Reisnercd643972011-04-13 18:00:59 -07001678{
1679 struct drbd_conf *mdev;
1680 int i;
1681
Philipp Reisner44ed1672011-04-19 17:10:19 +02001682 if (old_conf && tconn->agreed_pro_version < 100 &&
Philipp Reisnerb032b6f2011-04-13 18:16:10 -07001683 tconn->cstate == C_WF_REPORT_PARAMS &&
Philipp Reisner44ed1672011-04-19 17:10:19 +02001684 new_conf->wire_protocol != old_conf->wire_protocol)
Philipp Reisnerb032b6f2011-04-13 18:16:10 -07001685 return ERR_NEED_APV_100;
1686
Philipp Reisnercd643972011-04-13 18:00:59 -07001687 if (new_conf->two_primaries &&
1688 (new_conf->wire_protocol != DRBD_PROT_C))
1689 return ERR_NOT_PROTO_C;
1690
Philipp Reisnercd643972011-04-13 18:00:59 -07001691 idr_for_each_entry(&tconn->volumes, mdev, i) {
1692 if (get_ldev(mdev)) {
1693 enum drbd_fencing_p fp = mdev->ldev->dc.fencing;
1694 put_ldev(mdev);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001695 if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
Philipp Reisnercd643972011-04-13 18:00:59 -07001696 return ERR_STONITH_AND_PROT_A;
Philipp Reisnercd643972011-04-13 18:00:59 -07001697 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02001698 if (mdev->state.role == R_PRIMARY && new_conf->want_lose)
Philipp Reisnercd643972011-04-13 18:00:59 -07001699 return ERR_DISCARD;
Philipp Reisnercd643972011-04-13 18:00:59 -07001700 }
Philipp Reisnercd643972011-04-13 18:00:59 -07001701
1702 if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A)
1703 return ERR_CONG_NOT_PROTO_A;
1704
1705 return NO_ERROR;
1706}
1707
Philipp Reisner44ed1672011-04-19 17:10:19 +02001708static enum drbd_ret_code
1709check_net_options(struct drbd_tconn *tconn, struct net_conf *new_conf)
1710{
1711 static enum drbd_ret_code rv;
1712 struct drbd_conf *mdev;
1713 int i;
1714
1715 rcu_read_lock();
1716 rv = _check_net_options(tconn, rcu_dereference(tconn->net_conf), new_conf);
1717 rcu_read_unlock();
1718
1719 /* tconn->volumes protected by genl_lock() here */
1720 idr_for_each_entry(&tconn->volumes, mdev, i) {
1721 if (!mdev->bitmap) {
1722 if(drbd_bm_init(mdev))
1723 return ERR_NOMEM;
1724 }
1725 }
1726
1727 return rv;
1728}
1729
Lars Ellenbergf3990022011-03-23 14:31:09 +01001730int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
1731{
1732 enum drbd_ret_code retcode;
1733 struct drbd_tconn *tconn;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001734 struct net_conf *old_conf, *new_conf = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001735 int err;
1736 int ovr; /* online verify running */
1737 int rsr; /* re-sync running */
1738 struct crypto_hash *verify_tfm = NULL;
1739 struct crypto_hash *csums_tfm = NULL;
1740
1741
1742 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
1743 if (!adm_ctx.reply_skb)
1744 return retcode;
1745 if (retcode != NO_ERROR)
1746 goto out;
1747
1748 tconn = adm_ctx.tconn;
1749
1750 new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
1751 if (!new_conf) {
1752 retcode = ERR_NOMEM;
1753 goto out;
1754 }
1755
Lars Ellenbergf3990022011-03-23 14:31:09 +01001756 conn_reconfig_start(tconn);
1757
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001758 mutex_lock(&tconn->net_conf_update);
1759 old_conf = tconn->net_conf;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001760
1761 if (!old_conf) {
1762 drbd_msg_put_info("net conf missing, try connect");
1763 retcode = ERR_INVALID_REQUEST;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001764 goto fail;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001765 }
1766
1767 *new_conf = *old_conf;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001768
Lars Ellenbergf3990022011-03-23 14:31:09 +01001769 err = net_conf_from_attrs_for_change(new_conf, info);
1770 if (err) {
1771 retcode = ERR_MANDATORY_TAG;
1772 drbd_msg_put_info(from_attrs_err_to_txt(err));
1773 goto fail;
1774 }
1775
Philipp Reisnercd643972011-04-13 18:00:59 -07001776 retcode = check_net_options(tconn, new_conf);
1777 if (retcode != NO_ERROR)
1778 goto fail;
1779
Lars Ellenbergf3990022011-03-23 14:31:09 +01001780 /* re-sync running */
1781 rsr = conn_resync_running(tconn);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001782 if (rsr && old_conf && strcmp(new_conf->csums_alg, old_conf->csums_alg)) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001783 retcode = ERR_CSUMS_RESYNC_RUNNING;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001784 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001785 }
1786
1787 if (!rsr && new_conf->csums_alg[0]) {
1788 csums_tfm = crypto_alloc_hash(new_conf->csums_alg, 0, CRYPTO_ALG_ASYNC);
1789 if (IS_ERR(csums_tfm)) {
1790 csums_tfm = NULL;
1791 retcode = ERR_CSUMS_ALG;
1792 goto fail;
1793 }
1794
1795 if (!drbd_crypto_is_hash(crypto_hash_tfm(csums_tfm))) {
1796 retcode = ERR_CSUMS_ALG_ND;
1797 goto fail;
1798 }
1799 }
1800
1801 /* online verify running */
1802 ovr = conn_ov_running(tconn);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001803 if (ovr) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02001804 if (strcmp(new_conf->verify_alg, old_conf->verify_alg)) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001805 retcode = ERR_VERIFY_RUNNING;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001806 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001807 }
1808 }
1809
1810 if (!ovr && new_conf->verify_alg[0]) {
1811 verify_tfm = crypto_alloc_hash(new_conf->verify_alg, 0, CRYPTO_ALG_ASYNC);
1812 if (IS_ERR(verify_tfm)) {
1813 verify_tfm = NULL;
1814 retcode = ERR_VERIFY_ALG;
1815 goto fail;
1816 }
1817
1818 if (!drbd_crypto_is_hash(crypto_hash_tfm(verify_tfm))) {
1819 retcode = ERR_VERIFY_ALG_ND;
1820 goto fail;
1821 }
1822 }
1823
Philipp Reisner44ed1672011-04-19 17:10:19 +02001824 rcu_assign_pointer(tconn->net_conf, new_conf);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001825
1826 if (!rsr) {
1827 crypto_free_hash(tconn->csums_tfm);
1828 tconn->csums_tfm = csums_tfm;
1829 csums_tfm = NULL;
1830 }
1831 if (!ovr) {
1832 crypto_free_hash(tconn->verify_tfm);
1833 tconn->verify_tfm = verify_tfm;
1834 verify_tfm = NULL;
1835 }
1836
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001837 mutex_unlock(&tconn->net_conf_update);
1838 synchronize_rcu();
1839 kfree(old_conf);
1840
Lars Ellenbergf3990022011-03-23 14:31:09 +01001841 if (tconn->cstate >= C_WF_REPORT_PARAMS)
1842 drbd_send_sync_param(minor_to_mdev(conn_lowest_minor(tconn)));
1843
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001844 goto done;
1845
Lars Ellenbergf3990022011-03-23 14:31:09 +01001846 fail:
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001847 mutex_unlock(&tconn->net_conf_update);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001848 crypto_free_hash(csums_tfm);
1849 crypto_free_hash(verify_tfm);
1850 kfree(new_conf);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02001851 done:
Lars Ellenbergf3990022011-03-23 14:31:09 +01001852 conn_reconfig_done(tconn);
1853 out:
1854 drbd_adm_finish(info, retcode);
1855 return 0;
1856}
1857
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001858int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001859{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001860 char hmac_name[CRYPTO_MAX_ALG_NAME];
1861 struct drbd_conf *mdev;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001862 struct net_conf *old_conf, *new_conf = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001863 struct crypto_hash *tfm = NULL;
1864 struct crypto_hash *integrity_w_tfm = NULL;
1865 struct crypto_hash *integrity_r_tfm = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001866 void *int_dig_in = NULL;
1867 void *int_dig_vv = NULL;
Philipp Reisner80883192011-02-18 14:56:45 +01001868 struct drbd_tconn *oconn;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001869 struct drbd_tconn *tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001870 struct sockaddr *new_my_addr, *new_peer_addr, *taken_addr;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001871 enum drbd_ret_code retcode;
1872 int i;
1873 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001874
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001875 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
1876 if (!adm_ctx.reply_skb)
1877 return retcode;
1878 if (retcode != NO_ERROR)
1879 goto out;
1880
1881 tconn = adm_ctx.tconn;
Philipp Reisner80883192011-02-18 14:56:45 +01001882 conn_reconfig_start(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001883
Philipp Reisner80883192011-02-18 14:56:45 +01001884 if (tconn->cstate > C_STANDALONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001885 retcode = ERR_NET_CONFIGURED;
1886 goto fail;
1887 }
1888
1889 /* allocation not in the IO path, cqueue thread context */
Lars Ellenbergf3990022011-03-23 14:31:09 +01001890 new_conf = kmalloc(sizeof(struct net_conf), GFP_KERNEL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001891 if (!new_conf) {
1892 retcode = ERR_NOMEM;
1893 goto fail;
1894 }
1895
Lars Ellenbergf3990022011-03-23 14:31:09 +01001896 *new_conf = (struct net_conf) {
1897 {}, 0, /* my_addr */
1898 {}, 0, /* peer_addr */
1899 {}, 0, /* shared_secret */
1900 {}, 0, /* cram_hmac_alg */
1901 {}, 0, /* integrity_alg */
1902 {}, 0, /* verify_alg */
1903 {}, 0, /* csums_alg */
1904 DRBD_PROTOCOL_DEF, /* wire_protocol */
1905 DRBD_CONNECT_INT_DEF, /* try_connect_int */
1906 DRBD_TIMEOUT_DEF, /* timeout */
1907 DRBD_PING_INT_DEF, /* ping_int */
1908 DRBD_PING_TIMEO_DEF, /* ping_timeo */
1909 DRBD_SNDBUF_SIZE_DEF, /* sndbuf_size */
1910 DRBD_RCVBUF_SIZE_DEF, /* rcvbuf_size */
1911 DRBD_KO_COUNT_DEF, /* ko_count */
1912 DRBD_MAX_BUFFERS_DEF, /* max_buffers */
1913 DRBD_MAX_EPOCH_SIZE_DEF, /* max_epoch_size */
1914 DRBD_UNPLUG_WATERMARK_DEF, /* unplug_watermark */
1915 DRBD_AFTER_SB_0P_DEF, /* after_sb_0p */
1916 DRBD_AFTER_SB_1P_DEF, /* after_sb_1p */
1917 DRBD_AFTER_SB_2P_DEF, /* after_sb_2p */
1918 DRBD_RR_CONFLICT_DEF, /* rr_conflict */
1919 DRBD_ON_CONGESTION_DEF, /* on_congestion */
1920 DRBD_CONG_FILL_DEF, /* cong_fill */
1921 DRBD_CONG_EXTENTS_DEF, /* cong_extents */
1922 0, /* two_primaries */
1923 0, /* want_lose */
1924 0, /* no_cork */
1925 0, /* always_asbp */
1926 0, /* dry_run */
1927 0, /* use_rle */
1928 };
Philipp Reisnerb411b362009-09-25 16:07:19 -07001929
Lars Ellenbergf3990022011-03-23 14:31:09 +01001930 err = net_conf_from_attrs(new_conf, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001931 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001932 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001933 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001934 goto fail;
1935 }
1936
Philipp Reisnercd643972011-04-13 18:00:59 -07001937 retcode = check_net_options(tconn, new_conf);
1938 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001939 goto fail;
Philipp Reisner422028b2010-10-27 11:12:07 +02001940
Philipp Reisnerb411b362009-09-25 16:07:19 -07001941 retcode = NO_ERROR;
1942
1943 new_my_addr = (struct sockaddr *)&new_conf->my_addr;
1944 new_peer_addr = (struct sockaddr *)&new_conf->peer_addr;
Lars Ellenberg543cc102011-03-10 22:18:18 +01001945
Philipp Reisneref356262011-04-13 14:21:29 -07001946 /* No need to take drbd_cfg_rwsem here. All reconfiguration is
Lars Ellenberg543cc102011-03-10 22:18:18 +01001947 * strictly serialized on genl_lock(). We are protected against
1948 * concurrent reconfiguration/addition/deletion */
Philipp Reisner80883192011-02-18 14:56:45 +01001949 list_for_each_entry(oconn, &drbd_tconns, all_tconn) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02001950 struct net_conf *nc;
Philipp Reisner80883192011-02-18 14:56:45 +01001951 if (oconn == tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001952 continue;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001953
1954 rcu_read_lock();
1955 nc = rcu_dereference(oconn->net_conf);
1956 if (nc) {
1957 taken_addr = (struct sockaddr *)&nc->my_addr;
1958 if (new_conf->my_addr_len == nc->my_addr_len &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07001959 !memcmp(new_my_addr, taken_addr, new_conf->my_addr_len))
1960 retcode = ERR_LOCAL_ADDR;
1961
Philipp Reisner44ed1672011-04-19 17:10:19 +02001962 taken_addr = (struct sockaddr *)&nc->peer_addr;
1963 if (new_conf->peer_addr_len == nc->peer_addr_len &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07001964 !memcmp(new_peer_addr, taken_addr, new_conf->peer_addr_len))
1965 retcode = ERR_PEER_ADDR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001966 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02001967 rcu_read_unlock();
1968 if (retcode != NO_ERROR)
1969 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001970 }
1971
1972 if (new_conf->cram_hmac_alg[0] != 0) {
1973 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
1974 new_conf->cram_hmac_alg);
1975 tfm = crypto_alloc_hash(hmac_name, 0, CRYPTO_ALG_ASYNC);
1976 if (IS_ERR(tfm)) {
1977 tfm = NULL;
1978 retcode = ERR_AUTH_ALG;
1979 goto fail;
1980 }
1981
Philipp Reisner07982192009-12-28 16:58:38 +01001982 if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001983 retcode = ERR_AUTH_ALG_ND;
1984 goto fail;
1985 }
1986 }
1987
1988 if (new_conf->integrity_alg[0]) {
1989 integrity_w_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC);
1990 if (IS_ERR(integrity_w_tfm)) {
1991 integrity_w_tfm = NULL;
1992 retcode=ERR_INTEGRITY_ALG;
1993 goto fail;
1994 }
1995
1996 if (!drbd_crypto_is_hash(crypto_hash_tfm(integrity_w_tfm))) {
1997 retcode=ERR_INTEGRITY_ALG_ND;
1998 goto fail;
1999 }
2000
2001 integrity_r_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC);
2002 if (IS_ERR(integrity_r_tfm)) {
2003 integrity_r_tfm = NULL;
2004 retcode=ERR_INTEGRITY_ALG;
2005 goto fail;
2006 }
2007 }
2008
Philipp Reisnerb411b362009-09-25 16:07:19 -07002009 ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
2010
Philipp Reisner80883192011-02-18 14:56:45 +01002011 /* allocation not in the IO path, cqueue thread context */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002012 if (integrity_w_tfm) {
2013 i = crypto_hash_digestsize(integrity_w_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002014 int_dig_in = kmalloc(i, GFP_KERNEL);
2015 if (!int_dig_in) {
2016 retcode = ERR_NOMEM;
2017 goto fail;
2018 }
2019 int_dig_vv = kmalloc(i, GFP_KERNEL);
2020 if (!int_dig_vv) {
2021 retcode = ERR_NOMEM;
2022 goto fail;
2023 }
2024 }
2025
Philipp Reisner80883192011-02-18 14:56:45 +01002026 conn_flush_workqueue(tconn);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002027
2028 mutex_lock(&tconn->net_conf_update);
2029 old_conf = tconn->net_conf;
2030 if (old_conf) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002031 retcode = ERR_NET_CONFIGURED;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002032 mutex_unlock(&tconn->net_conf_update);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002033 goto fail;
2034 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002035 rcu_assign_pointer(tconn->net_conf, new_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002036
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002037 conn_free_crypto(tconn);
Philipp Reisner80883192011-02-18 14:56:45 +01002038 tconn->cram_hmac_tfm = tfm;
Philipp Reisner80883192011-02-18 14:56:45 +01002039 tconn->integrity_w_tfm = integrity_w_tfm;
Philipp Reisner80883192011-02-18 14:56:45 +01002040 tconn->integrity_r_tfm = integrity_r_tfm;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002041 tconn->int_dig_in = int_dig_in;
2042 tconn->int_dig_vv = int_dig_vv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002043
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002044 mutex_unlock(&tconn->net_conf_update);
2045
Philipp Reisner695d08f2011-04-11 22:53:32 -07002046 rcu_read_lock();
Philipp Reisner80883192011-02-18 14:56:45 +01002047 idr_for_each_entry(&tconn->volumes, mdev, i) {
2048 mdev->send_cnt = 0;
2049 mdev->recv_cnt = 0;
Philipp Reisner80883192011-02-18 14:56:45 +01002050 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07002051 rcu_read_unlock();
Lars Ellenberg5ee743e2011-04-26 16:22:25 +02002052
2053 retcode = conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
2054
Philipp Reisner80883192011-02-18 14:56:45 +01002055 conn_reconfig_done(tconn);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002056 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002057 return 0;
2058
2059fail:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002060 kfree(int_dig_in);
2061 kfree(int_dig_vv);
2062 crypto_free_hash(tfm);
2063 crypto_free_hash(integrity_w_tfm);
2064 crypto_free_hash(integrity_r_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002065 kfree(new_conf);
2066
Philipp Reisner80883192011-02-18 14:56:45 +01002067 conn_reconfig_done(tconn);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002068out:
2069 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002070 return 0;
2071}
2072
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002073static enum drbd_state_rv conn_try_disconnect(struct drbd_tconn *tconn, bool force)
2074{
2075 enum drbd_state_rv rv;
2076 if (force) {
2077 spin_lock_irq(&tconn->req_lock);
2078 if (tconn->cstate >= C_WF_CONNECTION)
2079 _conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
2080 spin_unlock_irq(&tconn->req_lock);
2081 return SS_SUCCESS;
2082 }
2083
2084 rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING), 0);
2085
2086 switch (rv) {
2087 case SS_NOTHING_TO_DO:
2088 case SS_ALREADY_STANDALONE:
2089 return SS_SUCCESS;
2090 case SS_PRIMARY_NOP:
2091 /* Our state checking code wants to see the peer outdated. */
2092 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
2093 pdsk, D_OUTDATED), CS_VERBOSE);
2094 break;
2095 case SS_CW_FAILED_BY_PEER:
2096 /* The peer probably wants to see us outdated. */
2097 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
2098 disk, D_OUTDATED), 0);
2099 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
2100 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
2101 rv = SS_SUCCESS;
2102 }
2103 break;
2104 default:;
2105 /* no special handling necessary */
2106 }
2107
2108 return rv;
2109}
2110
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002111int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002112{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002113 struct disconnect_parms parms;
2114 struct drbd_tconn *tconn;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002115 enum drbd_state_rv rv;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002116 enum drbd_ret_code retcode;
2117 int err;
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002118
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002119 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
2120 if (!adm_ctx.reply_skb)
2121 return retcode;
2122 if (retcode != NO_ERROR)
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002123 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002124
2125 tconn = adm_ctx.tconn;
2126 memset(&parms, 0, sizeof(parms));
2127 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002128 err = disconnect_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002129 if (err) {
2130 retcode = ERR_MANDATORY_TAG;
2131 drbd_msg_put_info(from_attrs_err_to_txt(err));
2132 goto fail;
2133 }
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002134 }
2135
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002136 rv = conn_try_disconnect(tconn, parms.force_disconnect);
2137 if (rv < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002138 goto fail;
2139
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01002140 if (wait_event_interruptible(tconn->ping_wait,
2141 tconn->cstate != C_DISCONNECTING)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002142 /* Do not test for mdev->state.conn == C_STANDALONE, since
2143 someone else might connect us in the mean time! */
2144 retcode = ERR_INTR;
2145 goto fail;
2146 }
2147
Philipp Reisnerb411b362009-09-25 16:07:19 -07002148 retcode = NO_ERROR;
2149 fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002150 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002151 return 0;
2152}
2153
2154void resync_after_online_grow(struct drbd_conf *mdev)
2155{
2156 int iass; /* I am sync source */
2157
2158 dev_info(DEV, "Resync of new storage after online grow\n");
2159 if (mdev->state.role != mdev->state.peer)
2160 iass = (mdev->state.role == R_PRIMARY);
2161 else
Philipp Reisner25703f82011-02-07 14:35:25 +01002162 iass = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002163
2164 if (iass)
2165 drbd_start_resync(mdev, C_SYNC_SOURCE);
2166 else
2167 _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
2168}
2169
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002170int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002171{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002172 struct resize_parms rs;
2173 struct drbd_conf *mdev;
2174 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002175 enum determine_dev_size dd;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002176 enum dds_flags ddsf;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002177 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002178
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002179 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2180 if (!adm_ctx.reply_skb)
2181 return retcode;
2182 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002183 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002184
2185 memset(&rs, 0, sizeof(struct resize_parms));
2186 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002187 err = resize_parms_from_attrs(&rs, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002188 if (err) {
2189 retcode = ERR_MANDATORY_TAG;
2190 drbd_msg_put_info(from_attrs_err_to_txt(err));
2191 goto fail;
2192 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002193 }
2194
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002195 mdev = adm_ctx.mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002196 if (mdev->state.conn > C_CONNECTED) {
2197 retcode = ERR_RESIZE_RESYNC;
2198 goto fail;
2199 }
2200
2201 if (mdev->state.role == R_SECONDARY &&
2202 mdev->state.peer == R_SECONDARY) {
2203 retcode = ERR_NO_PRIMARY;
2204 goto fail;
2205 }
2206
2207 if (!get_ldev(mdev)) {
2208 retcode = ERR_NO_DISK;
2209 goto fail;
2210 }
2211
Philipp Reisner31890f42011-01-19 14:12:51 +01002212 if (rs.no_resync && mdev->tconn->agreed_pro_version < 93) {
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002213 retcode = ERR_NEED_APV_93;
2214 goto fail;
2215 }
2216
Philipp Reisner087c2492010-03-26 13:49:56 +01002217 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002218 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002219
2220 mdev->ldev->dc.disk_size = (sector_t)rs.resize_size;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002221 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
Bart Van Assche24c48302011-05-21 18:32:29 +02002222 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002223 drbd_md_sync(mdev);
2224 put_ldev(mdev);
2225 if (dd == dev_size_error) {
2226 retcode = ERR_NOMEM_BITMAP;
2227 goto fail;
2228 }
2229
Philipp Reisner087c2492010-03-26 13:49:56 +01002230 if (mdev->state.conn == C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002231 if (dd == grew)
2232 set_bit(RESIZE_PENDING, &mdev->flags);
2233
2234 drbd_send_uuids(mdev);
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002235 drbd_send_sizes(mdev, 1, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002236 }
2237
2238 fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002239 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002240 return 0;
2241}
2242
Lars Ellenbergf3990022011-03-23 14:31:09 +01002243int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002244{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002245 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002246 cpumask_var_t new_cpu_mask;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002247 struct drbd_tconn *tconn;
Philipp Reisner778f2712010-07-06 11:14:00 +02002248 int *rs_plan_s = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002249 struct res_opts sc;
2250 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002251
Lars Ellenbergf3990022011-03-23 14:31:09 +01002252 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002253 if (!adm_ctx.reply_skb)
2254 return retcode;
2255 if (retcode != NO_ERROR)
2256 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002257 tconn = adm_ctx.tconn;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002258
Philipp Reisnerb411b362009-09-25 16:07:19 -07002259 if (!zalloc_cpumask_var(&new_cpu_mask, GFP_KERNEL)) {
2260 retcode = ERR_NOMEM;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002261 drbd_msg_put_info("unable to allocate cpumask");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002262 goto fail;
2263 }
2264
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002265 if (((struct drbd_genlmsghdr*)info->userhdr)->flags
2266 & DRBD_GENL_F_SET_DEFAULTS) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002267 memset(&sc, 0, sizeof(struct res_opts));
Philipp Reisner265be2d2010-05-31 10:14:17 +02002268 sc.on_no_data = DRBD_ON_NO_DATA_DEF;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002269 } else
Lars Ellenbergf3990022011-03-23 14:31:09 +01002270 sc = tconn->res_opts;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002271
Lars Ellenbergf3990022011-03-23 14:31:09 +01002272 err = res_opts_from_attrs(&sc, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002273 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002274 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002275 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002276 goto fail;
2277 }
2278
Philipp Reisnerb411b362009-09-25 16:07:19 -07002279 /* silently ignore cpu mask on UP kernel */
2280 if (nr_cpu_ids > 1 && sc.cpu_mask[0] != 0) {
2281 err = __bitmap_parse(sc.cpu_mask, 32, 0,
2282 cpumask_bits(new_cpu_mask), nr_cpu_ids);
2283 if (err) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002284 conn_warn(tconn, "__bitmap_parse() failed with %d\n", err);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002285 retcode = ERR_CPU_MASK_PARSE;
2286 goto fail;
2287 }
2288 }
2289
Lars Ellenberg7ad651b2011-02-21 13:21:03 +01002290
Lars Ellenbergf3990022011-03-23 14:31:09 +01002291 tconn->res_opts = sc;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002292
Lars Ellenbergf3990022011-03-23 14:31:09 +01002293 if (!cpumask_equal(tconn->cpu_mask, new_cpu_mask)) {
2294 cpumask_copy(tconn->cpu_mask, new_cpu_mask);
2295 drbd_calc_cpu_mask(tconn);
2296 tconn->receiver.reset_cpu_mask = 1;
2297 tconn->asender.reset_cpu_mask = 1;
2298 tconn->worker.reset_cpu_mask = 1;
Philipp Reisner778f2712010-07-06 11:14:00 +02002299 }
2300
Philipp Reisnerb411b362009-09-25 16:07:19 -07002301fail:
Philipp Reisner778f2712010-07-06 11:14:00 +02002302 kfree(rs_plan_s);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002303 free_cpumask_var(new_cpu_mask);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002304
2305 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002306 return 0;
2307}
2308
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002309int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002310{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002311 struct drbd_conf *mdev;
2312 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2313
2314 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2315 if (!adm_ctx.reply_skb)
2316 return retcode;
2317 if (retcode != NO_ERROR)
2318 goto out;
2319
2320 mdev = adm_ctx.mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002321
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002322 /* If there is still bitmap IO pending, probably because of a previous
2323 * resync just being finished, wait for it before requesting a new resync. */
2324 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
2325
Philipp Reisnerb411b362009-09-25 16:07:19 -07002326 retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T), CS_ORDERED);
2327
2328 if (retcode < SS_SUCCESS && retcode != SS_NEED_CONNECTION)
2329 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
2330
2331 while (retcode == SS_NEED_CONNECTION) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01002332 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002333 if (mdev->state.conn < C_CONNECTED)
2334 retcode = _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_VERBOSE, NULL);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002335 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002336
2337 if (retcode != SS_NEED_CONNECTION)
2338 break;
2339
2340 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
2341 }
2342
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002343out:
2344 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002345 return 0;
2346}
2347
Philipp Reisner07782862010-08-31 12:00:50 +02002348static int drbd_bmio_set_susp_al(struct drbd_conf *mdev)
2349{
2350 int rv;
2351
2352 rv = drbd_bmio_set_n_write(mdev);
2353 drbd_suspend_al(mdev);
2354 return rv;
2355}
2356
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002357static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2358 union drbd_state mask, union drbd_state val)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002359{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002360 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002361
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002362 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2363 if (!adm_ctx.reply_skb)
2364 return retcode;
2365 if (retcode != NO_ERROR)
2366 goto out;
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002367
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002368 retcode = drbd_request_state(adm_ctx.mdev, mask, val);
2369out:
2370 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002371 return 0;
2372}
2373
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002374int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002375{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002376 return drbd_adm_simple_request_state(skb, info, NS(conn, C_STARTING_SYNC_S));
2377}
Philipp Reisnerb411b362009-09-25 16:07:19 -07002378
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002379int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
2380{
2381 enum drbd_ret_code retcode;
2382
2383 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2384 if (!adm_ctx.reply_skb)
2385 return retcode;
2386 if (retcode != NO_ERROR)
2387 goto out;
2388
2389 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002390 retcode = ERR_PAUSE_IS_SET;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002391out:
2392 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002393 return 0;
2394}
2395
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002396int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002397{
Philipp Reisnerda9fbc22011-03-29 10:52:01 +02002398 union drbd_dev_state s;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002399 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002400
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002401 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2402 if (!adm_ctx.reply_skb)
2403 return retcode;
2404 if (retcode != NO_ERROR)
2405 goto out;
2406
2407 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2408 s = adm_ctx.mdev->state;
Philipp Reisnercd88d032011-01-20 11:46:41 +01002409 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2410 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2411 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2412 } else {
2413 retcode = ERR_PAUSE_IS_CLEAR;
2414 }
2415 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002416
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002417out:
2418 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002419 return 0;
2420}
2421
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002422int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002423{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002424 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002425}
2426
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002427int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002428{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002429 struct drbd_conf *mdev;
2430 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2431
2432 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2433 if (!adm_ctx.reply_skb)
2434 return retcode;
2435 if (retcode != NO_ERROR)
2436 goto out;
2437
2438 mdev = adm_ctx.mdev;
Philipp Reisner43a51822010-06-11 11:26:34 +02002439 if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
2440 drbd_uuid_new_current(mdev);
2441 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner43a51822010-06-11 11:26:34 +02002442 }
Philipp Reisner265be2d2010-05-31 10:14:17 +02002443 drbd_suspend_io(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002444 retcode = drbd_request_state(mdev, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
2445 if (retcode == SS_SUCCESS) {
Philipp Reisner265be2d2010-05-31 10:14:17 +02002446 if (mdev->state.conn < C_CONNECTED)
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01002447 tl_clear(mdev->tconn);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002448 if (mdev->state.disk == D_DISKLESS || mdev->state.disk == D_FAILED)
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01002449 tl_restart(mdev->tconn, FAIL_FROZEN_DISK_IO);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002450 }
2451 drbd_resume_io(mdev);
2452
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002453out:
2454 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002455 return 0;
2456}
2457
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002458int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002459{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002460 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002461}
2462
Lars Ellenberg543cc102011-03-10 22:18:18 +01002463int nla_put_drbd_cfg_context(struct sk_buff *skb, const char *conn_name, unsigned vnr)
2464{
2465 struct nlattr *nla;
2466 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
2467 if (!nla)
2468 goto nla_put_failure;
2469 if (vnr != VOLUME_UNSPECIFIED)
2470 NLA_PUT_U32(skb, T_ctx_volume, vnr);
2471 NLA_PUT_STRING(skb, T_ctx_conn_name, conn_name);
2472 nla_nest_end(skb, nla);
2473 return 0;
2474
2475nla_put_failure:
2476 if (nla)
2477 nla_nest_cancel(skb, nla);
2478 return -EMSGSIZE;
2479}
2480
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002481int nla_put_status_info(struct sk_buff *skb, struct drbd_conf *mdev,
2482 const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002483{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002484 struct state_info *si = NULL; /* for sizeof(si->member); */
Philipp Reisner44ed1672011-04-19 17:10:19 +02002485 struct net_conf *nc;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002486 struct nlattr *nla;
2487 int got_ldev;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002488 int err = 0;
2489 int exclude_sensitive;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002490
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002491 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2492 * to. So we better exclude_sensitive information.
2493 *
2494 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2495 * in the context of the requesting user process. Exclude sensitive
2496 * information, unless current has superuser.
2497 *
2498 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2499 * relies on the current implementation of netlink_dump(), which
2500 * executes the dump callback successively from netlink_recvmsg(),
2501 * always in the context of the receiving process */
2502 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002503
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002504 got_ldev = get_ldev(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002505
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002506 /* We need to add connection name and volume number information still.
2507 * Minor number is in drbd_genlmsghdr. */
Lars Ellenberg543cc102011-03-10 22:18:18 +01002508 if (nla_put_drbd_cfg_context(skb, mdev->tconn->name, mdev->vnr))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002509 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002510
Lars Ellenbergf3990022011-03-23 14:31:09 +01002511 if (res_opts_to_skb(skb, &mdev->tconn->res_opts, exclude_sensitive))
2512 goto nla_put_failure;
2513
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002514 if (got_ldev)
2515 if (disk_conf_to_skb(skb, &mdev->ldev->dc, exclude_sensitive))
2516 goto nla_put_failure;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002517
2518 rcu_read_lock();
2519 nc = rcu_dereference(mdev->tconn->net_conf);
2520 if (nc)
2521 err = net_conf_to_skb(skb, nc, exclude_sensitive);
2522 rcu_read_unlock();
2523 if (err)
2524 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002525
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002526 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
2527 if (!nla)
2528 goto nla_put_failure;
2529 NLA_PUT_U32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY);
2530 NLA_PUT_U32(skb, T_current_state, mdev->state.i);
2531 NLA_PUT_U64(skb, T_ed_uuid, mdev->ed_uuid);
2532 NLA_PUT_U64(skb, T_capacity, drbd_get_capacity(mdev->this_bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002533
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002534 if (got_ldev) {
2535 NLA_PUT_U32(skb, T_disk_flags, mdev->ldev->md.flags);
2536 NLA_PUT(skb, T_uuids, sizeof(si->uuids), mdev->ldev->md.uuid);
2537 NLA_PUT_U64(skb, T_bits_total, drbd_bm_bits(mdev));
2538 NLA_PUT_U64(skb, T_bits_oos, drbd_bm_total_weight(mdev));
2539 if (C_SYNC_SOURCE <= mdev->state.conn &&
2540 C_PAUSED_SYNC_T >= mdev->state.conn) {
2541 NLA_PUT_U64(skb, T_bits_rs_total, mdev->rs_total);
2542 NLA_PUT_U64(skb, T_bits_rs_failed, mdev->rs_failed);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002543 }
2544 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002545
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002546 if (sib) {
2547 switch(sib->sib_reason) {
2548 case SIB_SYNC_PROGRESS:
2549 case SIB_GET_STATUS_REPLY:
2550 break;
2551 case SIB_STATE_CHANGE:
2552 NLA_PUT_U32(skb, T_prev_state, sib->os.i);
2553 NLA_PUT_U32(skb, T_new_state, sib->ns.i);
2554 break;
2555 case SIB_HELPER_POST:
2556 NLA_PUT_U32(skb,
2557 T_helper_exit_code, sib->helper_exit_code);
2558 /* fall through */
2559 case SIB_HELPER_PRE:
2560 NLA_PUT_STRING(skb, T_helper, sib->helper_name);
2561 break;
2562 }
2563 }
2564 nla_nest_end(skb, nla);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002565
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002566 if (0)
2567nla_put_failure:
2568 err = -EMSGSIZE;
2569 if (got_ldev)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002570 put_ldev(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002571 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002572}
2573
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002574int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002575{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002576 enum drbd_ret_code retcode;
2577 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002578
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002579 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2580 if (!adm_ctx.reply_skb)
2581 return retcode;
2582 if (retcode != NO_ERROR)
2583 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002584
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002585 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.mdev, NULL);
2586 if (err) {
2587 nlmsg_free(adm_ctx.reply_skb);
2588 return err;
2589 }
2590out:
2591 drbd_adm_finish(info, retcode);
2592 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002593}
2594
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002595int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002596{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002597 struct drbd_conf *mdev;
2598 struct drbd_genlmsghdr *dh;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002599 struct drbd_tconn *pos = (struct drbd_tconn*)cb->args[0];
2600 struct drbd_tconn *tconn = NULL;
2601 struct drbd_tconn *tmp;
2602 unsigned volume = cb->args[1];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002603
Lars Ellenberg543cc102011-03-10 22:18:18 +01002604 /* Open coded, deferred, iteration:
2605 * list_for_each_entry_safe(tconn, tmp, &drbd_tconns, all_tconn) {
2606 * idr_for_each_entry(&tconn->volumes, mdev, i) {
2607 * ...
2608 * }
2609 * }
2610 * where tconn is cb->args[0];
2611 * and i is cb->args[1];
2612 *
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002613 * cb->args[2] indicates if we shall loop over all resources,
2614 * or just dump all volumes of a single resource.
2615 *
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002616 * This may miss entries inserted after this dump started,
2617 * or entries deleted before they are reached.
Lars Ellenberg543cc102011-03-10 22:18:18 +01002618 *
2619 * We need to make sure the mdev won't disappear while
2620 * we are looking at it, and revalidate our iterators
2621 * on each iteration.
2622 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002623
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02002624 /* synchronize with conn_create()/conn_destroy() */
Philipp Reisneref356262011-04-13 14:21:29 -07002625 down_read(&drbd_cfg_rwsem);
Lars Ellenberg543cc102011-03-10 22:18:18 +01002626 /* revalidate iterator position */
2627 list_for_each_entry(tmp, &drbd_tconns, all_tconn) {
2628 if (pos == NULL) {
2629 /* first iteration */
2630 pos = tmp;
2631 tconn = pos;
2632 break;
2633 }
2634 if (tmp == pos) {
2635 tconn = pos;
2636 break;
2637 }
2638 }
2639 if (tconn) {
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002640next_tconn:
Lars Ellenberg543cc102011-03-10 22:18:18 +01002641 mdev = idr_get_next(&tconn->volumes, &volume);
2642 if (!mdev) {
2643 /* No more volumes to dump on this tconn.
2644 * Advance tconn iterator. */
2645 pos = list_entry(tconn->all_tconn.next,
2646 struct drbd_tconn, all_tconn);
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002647 /* Did we dump any volume on this tconn yet? */
Lars Ellenberg543cc102011-03-10 22:18:18 +01002648 if (volume != 0) {
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002649 /* If we reached the end of the list,
2650 * or only a single resource dump was requested,
2651 * we are done. */
2652 if (&pos->all_tconn == &drbd_tconns || cb->args[2])
2653 goto out;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002654 volume = 0;
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002655 tconn = pos;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002656 goto next_tconn;
2657 }
2658 }
2659
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002660 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).pid,
2661 cb->nlh->nlmsg_seq, &drbd_genl_family,
2662 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
2663 if (!dh)
Lars Ellenberg543cc102011-03-10 22:18:18 +01002664 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002665
Lars Ellenberg543cc102011-03-10 22:18:18 +01002666 if (!mdev) {
2667 /* this is a tconn without a single volume */
2668 dh->minor = -1U;
2669 dh->ret_code = NO_ERROR;
2670 if (nla_put_drbd_cfg_context(skb, tconn->name, VOLUME_UNSPECIFIED))
2671 genlmsg_cancel(skb, dh);
2672 else
2673 genlmsg_end(skb, dh);
2674 goto out;
2675 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002676
Lars Ellenberg543cc102011-03-10 22:18:18 +01002677 D_ASSERT(mdev->vnr == volume);
2678 D_ASSERT(mdev->tconn == tconn);
2679
2680 dh->minor = mdev_to_minor(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002681 dh->ret_code = NO_ERROR;
2682
2683 if (nla_put_status_info(skb, mdev, NULL)) {
2684 genlmsg_cancel(skb, dh);
Lars Ellenberg543cc102011-03-10 22:18:18 +01002685 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002686 }
2687 genlmsg_end(skb, dh);
2688 }
2689
Lars Ellenberg543cc102011-03-10 22:18:18 +01002690out:
Philipp Reisneref356262011-04-13 14:21:29 -07002691 up_read(&drbd_cfg_rwsem);
Lars Ellenberg543cc102011-03-10 22:18:18 +01002692 /* where to start the next iteration */
2693 cb->args[0] = (long)pos;
2694 cb->args[1] = (pos == tconn) ? volume + 1 : 0;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002695
Lars Ellenberg543cc102011-03-10 22:18:18 +01002696 /* No more tconns/volumes/minors found results in an empty skb.
2697 * Which will terminate the dump. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002698 return skb->len;
2699}
2700
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002701/*
2702 * Request status of all resources, or of all volumes within a single resource.
2703 *
2704 * This is a dump, as the answer may not fit in a single reply skb otherwise.
2705 * Which means we cannot use the family->attrbuf or other such members, because
2706 * dump is NOT protected by the genl_lock(). During dump, we only have access
2707 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
2708 *
2709 * Once things are setup properly, we call into get_one_status().
2710 */
2711int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
2712{
2713 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
2714 struct nlattr *nla;
2715 const char *conn_name;
2716 struct drbd_tconn *tconn;
2717
2718 /* Is this a followup call? */
2719 if (cb->args[0]) {
2720 /* ... of a single resource dump,
2721 * and the resource iterator has been advanced already? */
2722 if (cb->args[2] && cb->args[2] != cb->args[0])
2723 return 0; /* DONE. */
2724 goto dump;
2725 }
2726
2727 /* First call (from netlink_dump_start). We need to figure out
2728 * which resource(s) the user wants us to dump. */
2729 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
2730 nlmsg_attrlen(cb->nlh, hdrlen),
2731 DRBD_NLA_CFG_CONTEXT);
2732
2733 /* No explicit context given. Dump all. */
2734 if (!nla)
2735 goto dump;
2736 nla = nla_find_nested(nla, __nla_type(T_ctx_conn_name));
2737 /* context given, but no name present? */
2738 if (!nla)
2739 return -EINVAL;
2740 conn_name = nla_data(nla);
Philipp Reisner0ace9df2011-04-24 10:53:19 +02002741 tconn = conn_get_by_name(conn_name);
2742
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002743 if (!tconn)
2744 return -ENODEV;
2745
Philipp Reisner0ace9df2011-04-24 10:53:19 +02002746 kref_put(&tconn->kref, &conn_destroy); /* get_one_status() (re)validates tconn by itself */
2747
Lars Ellenberg71932ef2011-04-18 09:43:25 +02002748 /* prime iterators, and set "filter" mode mark:
2749 * only dump this tconn. */
2750 cb->args[0] = (long)tconn;
2751 /* cb->args[1] = 0; passed in this way. */
2752 cb->args[2] = (long)tconn;
2753
2754dump:
2755 return get_one_status(skb, cb);
2756}
2757
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002758int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
2759{
2760 enum drbd_ret_code retcode;
2761 struct timeout_parms tp;
2762 int err;
2763
2764 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2765 if (!adm_ctx.reply_skb)
2766 return retcode;
2767 if (retcode != NO_ERROR)
2768 goto out;
2769
2770 tp.timeout_type =
2771 adm_ctx.mdev->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
2772 test_bit(USE_DEGR_WFC_T, &adm_ctx.mdev->flags) ? UT_DEGRADED :
2773 UT_DEFAULT;
2774
2775 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
2776 if (err) {
2777 nlmsg_free(adm_ctx.reply_skb);
2778 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002779 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002780out:
2781 drbd_adm_finish(info, retcode);
2782 return 0;
2783}
Lars Ellenberg873b0d52011-01-21 22:53:48 +01002784
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002785int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
2786{
2787 struct drbd_conf *mdev;
2788 enum drbd_ret_code retcode;
2789
2790 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2791 if (!adm_ctx.reply_skb)
2792 return retcode;
2793 if (retcode != NO_ERROR)
2794 goto out;
2795
2796 mdev = adm_ctx.mdev;
2797 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
2798 /* resume from last known position, if possible */
2799 struct start_ov_parms parms =
2800 { .ov_start_sector = mdev->ov_start_sector };
Lars Ellenbergf3990022011-03-23 14:31:09 +01002801 int err = start_ov_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002802 if (err) {
2803 retcode = ERR_MANDATORY_TAG;
2804 drbd_msg_put_info(from_attrs_err_to_txt(err));
2805 goto out;
2806 }
2807 /* w_make_ov_request expects position to be aligned */
2808 mdev->ov_start_sector = parms.ov_start_sector & ~BM_SECT_PER_BIT;
2809 }
Lars Ellenberg873b0d52011-01-21 22:53:48 +01002810 /* If there is still bitmap IO pending, e.g. previous resync or verify
2811 * just being finished, wait for it before requesting a new resync. */
2812 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002813 retcode = drbd_request_state(mdev,NS(conn,C_VERIFY_S));
2814out:
2815 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002816 return 0;
2817}
2818
2819
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002820int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002821{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002822 struct drbd_conf *mdev;
2823 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002824 int skip_initial_sync = 0;
2825 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002826 struct new_c_uuid_parms args;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002827
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002828 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2829 if (!adm_ctx.reply_skb)
2830 return retcode;
2831 if (retcode != NO_ERROR)
2832 goto out_nolock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002833
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002834 mdev = adm_ctx.mdev;
2835 memset(&args, 0, sizeof(args));
2836 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002837 err = new_c_uuid_parms_from_attrs(&args, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002838 if (err) {
2839 retcode = ERR_MANDATORY_TAG;
2840 drbd_msg_put_info(from_attrs_err_to_txt(err));
2841 goto out_nolock;
2842 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002843 }
2844
Philipp Reisner8410da82011-02-11 20:11:10 +01002845 mutex_lock(mdev->state_mutex); /* Protects us against serialized state changes. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002846
2847 if (!get_ldev(mdev)) {
2848 retcode = ERR_NO_DISK;
2849 goto out;
2850 }
2851
2852 /* this is "skip initial sync", assume to be clean */
Philipp Reisner31890f42011-01-19 14:12:51 +01002853 if (mdev->state.conn == C_CONNECTED && mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07002854 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
2855 dev_info(DEV, "Preparing to skip initial sync\n");
2856 skip_initial_sync = 1;
2857 } else if (mdev->state.conn != C_STANDALONE) {
2858 retcode = ERR_CONNECTED;
2859 goto out_dec;
2860 }
2861
2862 drbd_uuid_set(mdev, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
2863 drbd_uuid_new_current(mdev); /* New current, previous to UI_BITMAP */
2864
2865 if (args.clear_bm) {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002866 err = drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
2867 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002868 if (err) {
2869 dev_err(DEV, "Writing bitmap failed with %d\n",err);
2870 retcode = ERR_IO_MD_DISK;
2871 }
2872 if (skip_initial_sync) {
2873 drbd_send_uuids_skip_initial_sync(mdev);
2874 _drbd_uuid_set(mdev, UI_BITMAP, 0);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002875 drbd_print_uuids(mdev, "cleared bitmap UUID");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002876 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002877 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
2878 CS_VERBOSE, NULL);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002879 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002880 }
2881 }
2882
2883 drbd_md_sync(mdev);
2884out_dec:
2885 put_ldev(mdev);
2886out:
Philipp Reisner8410da82011-02-11 20:11:10 +01002887 mutex_unlock(mdev->state_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002888out_nolock:
2889 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002890 return 0;
2891}
2892
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002893static enum drbd_ret_code
2894drbd_check_conn_name(const char *name)
Philipp Reisner774b3052011-02-22 02:07:03 -05002895{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002896 if (!name || !name[0]) {
2897 drbd_msg_put_info("connection name missing");
2898 return ERR_MANDATORY_TAG;
2899 }
2900 /* if we want to use these in sysfs/configfs/debugfs some day,
2901 * we must not allow slashes */
2902 if (strchr(name, '/')) {
2903 drbd_msg_put_info("invalid connection name");
2904 return ERR_INVALID_REQUEST;
2905 }
2906 return NO_ERROR;
2907}
Philipp Reisner774b3052011-02-22 02:07:03 -05002908
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002909int drbd_adm_create_connection(struct sk_buff *skb, struct genl_info *info)
2910{
2911 enum drbd_ret_code retcode;
2912
2913 retcode = drbd_adm_prepare(skb, info, 0);
2914 if (!adm_ctx.reply_skb)
2915 return retcode;
2916 if (retcode != NO_ERROR)
2917 goto out;
2918
2919 retcode = drbd_check_conn_name(adm_ctx.conn_name);
2920 if (retcode != NO_ERROR)
2921 goto out;
2922
2923 if (adm_ctx.tconn) {
Lars Ellenberg38f19612011-03-14 13:22:35 +01002924 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
2925 retcode = ERR_INVALID_REQUEST;
2926 drbd_msg_put_info("connection exists");
2927 }
2928 /* else: still NO_ERROR */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002929 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05002930 }
2931
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02002932 if (!conn_create(adm_ctx.conn_name))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002933 retcode = ERR_NOMEM;
2934out:
2935 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05002936 return 0;
2937}
2938
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002939int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05002940{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002941 struct drbd_genlmsghdr *dh = info->userhdr;
2942 enum drbd_ret_code retcode;
Philipp Reisner774b3052011-02-22 02:07:03 -05002943
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002944 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
2945 if (!adm_ctx.reply_skb)
2946 return retcode;
2947 if (retcode != NO_ERROR)
2948 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05002949
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002950 /* FIXME drop minor_count parameter, limit to MINORMASK */
2951 if (dh->minor >= minor_count) {
2952 drbd_msg_put_info("requested minor out of range");
2953 retcode = ERR_INVALID_REQUEST;
2954 goto out;
2955 }
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +02002956 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002957 drbd_msg_put_info("requested volume id out of range");
2958 retcode = ERR_INVALID_REQUEST;
2959 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05002960 }
2961
Lars Ellenberg38f19612011-03-14 13:22:35 +01002962 /* drbd_adm_prepare made sure already
2963 * that mdev->tconn and mdev->vnr match the request. */
2964 if (adm_ctx.mdev) {
2965 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
2966 retcode = ERR_MINOR_EXISTS;
2967 /* else: still NO_ERROR */
2968 goto out;
2969 }
2970
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07002971 down_write(&drbd_cfg_rwsem);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002972 retcode = conn_new_minor(adm_ctx.tconn, dh->minor, adm_ctx.volume);
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07002973 up_write(&drbd_cfg_rwsem);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002974out:
2975 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05002976 return 0;
2977}
2978
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002979static enum drbd_ret_code adm_delete_minor(struct drbd_conf *mdev)
2980{
2981 if (mdev->state.disk == D_DISKLESS &&
2982 /* no need to be mdev->state.conn == C_STANDALONE &&
2983 * we may want to delete a minor from a live replication group.
2984 */
2985 mdev->state.role == R_SECONDARY) {
Philipp Reisnerff370e52011-04-11 21:10:11 -07002986 drbd_delete_device(mdev);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002987 return NO_ERROR;
2988 } else
2989 return ERR_MINOR_CONFIGURED;
2990}
2991
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002992int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05002993{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002994 enum drbd_ret_code retcode;
2995
2996 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2997 if (!adm_ctx.reply_skb)
2998 return retcode;
2999 if (retcode != NO_ERROR)
3000 goto out;
3001
Philipp Reisneref356262011-04-13 14:21:29 -07003002 down_write(&drbd_cfg_rwsem);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003003 retcode = adm_delete_minor(adm_ctx.mdev);
Philipp Reisneref356262011-04-13 14:21:29 -07003004 up_write(&drbd_cfg_rwsem);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003005out:
3006 drbd_adm_finish(info, retcode);
3007 return 0;
3008}
3009
3010int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
3011{
3012 enum drbd_ret_code retcode;
3013 enum drbd_state_rv rv;
3014 struct drbd_conf *mdev;
3015 unsigned i;
3016
3017 retcode = drbd_adm_prepare(skb, info, 0);
3018 if (!adm_ctx.reply_skb)
3019 return retcode;
3020 if (retcode != NO_ERROR)
3021 goto out;
3022
3023 if (!adm_ctx.tconn) {
3024 retcode = ERR_CONN_NOT_KNOWN;
3025 goto out;
3026 }
3027
Philipp Reisneref356262011-04-13 14:21:29 -07003028 down_read(&drbd_cfg_rwsem);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003029 /* demote */
3030 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3031 retcode = drbd_set_role(mdev, R_SECONDARY, 0);
3032 if (retcode < SS_SUCCESS) {
3033 drbd_msg_put_info("failed to demote");
3034 goto out_unlock;
3035 }
3036 }
3037
3038 /* disconnect */
3039 rv = conn_try_disconnect(adm_ctx.tconn, 0);
3040 if (rv < SS_SUCCESS) {
3041 retcode = rv; /* enum type mismatch! */
3042 drbd_msg_put_info("failed to disconnect");
3043 goto out_unlock;
3044 }
3045
3046 /* detach */
3047 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3048 rv = adm_detach(mdev);
3049 if (rv < SS_SUCCESS) {
3050 retcode = rv; /* enum type mismatch! */
3051 drbd_msg_put_info("failed to detach");
3052 goto out_unlock;
3053 }
3054 }
Philipp Reisneref356262011-04-13 14:21:29 -07003055 up_read(&drbd_cfg_rwsem);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003056
3057 /* delete volumes */
Philipp Reisneref356262011-04-13 14:21:29 -07003058 down_write(&drbd_cfg_rwsem);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003059 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3060 retcode = adm_delete_minor(mdev);
3061 if (retcode != NO_ERROR) {
3062 /* "can not happen" */
3063 drbd_msg_put_info("failed to delete volume");
Philipp Reisneref356262011-04-13 14:21:29 -07003064 up_write(&drbd_cfg_rwsem);
3065 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003066 }
3067 }
3068
3069 /* stop all threads */
3070 conn_reconfig_done(adm_ctx.tconn);
3071
3072 /* delete connection */
3073 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02003074 list_del(&adm_ctx.tconn->all_tconn);
3075 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
3076
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003077 retcode = NO_ERROR;
3078 } else {
3079 /* "can not happen" */
3080 retcode = ERR_CONN_IN_USE;
3081 drbd_msg_put_info("failed to delete connection");
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003082 }
Philipp Reisneref356262011-04-13 14:21:29 -07003083
3084 up_write(&drbd_cfg_rwsem);
3085 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003086out_unlock:
Philipp Reisneref356262011-04-13 14:21:29 -07003087 up_read(&drbd_cfg_rwsem);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003088out:
3089 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003090 return 0;
3091}
3092
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003093int drbd_adm_delete_connection(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05003094{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003095 enum drbd_ret_code retcode;
3096
3097 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
3098 if (!adm_ctx.reply_skb)
3099 return retcode;
3100 if (retcode != NO_ERROR)
3101 goto out;
3102
Philipp Reisneref356262011-04-13 14:21:29 -07003103 down_write(&drbd_cfg_rwsem);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003104 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02003105 list_del(&adm_ctx.tconn->all_tconn);
3106 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
3107
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003108 retcode = NO_ERROR;
Philipp Reisner774b3052011-02-22 02:07:03 -05003109 } else {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003110 retcode = ERR_CONN_IN_USE;
Philipp Reisner774b3052011-02-22 02:07:03 -05003111 }
Philipp Reisneref356262011-04-13 14:21:29 -07003112 up_write(&drbd_cfg_rwsem);
Philipp Reisner774b3052011-02-22 02:07:03 -05003113
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003114out:
3115 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003116 return 0;
3117}
3118
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003119void drbd_bcast_event(struct drbd_conf *mdev, const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003120{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003121 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
3122 struct sk_buff *msg;
3123 struct drbd_genlmsghdr *d_out;
3124 unsigned seq;
3125 int err = -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003126
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003127 seq = atomic_inc_return(&drbd_genl_seq);
3128 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
3129 if (!msg)
3130 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003131
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003132 err = -EMSGSIZE;
3133 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
3134 if (!d_out) /* cannot happen, but anyways. */
3135 goto nla_put_failure;
3136 d_out->minor = mdev_to_minor(mdev);
3137 d_out->ret_code = 0;
Philipp Reisner9f5180e2009-10-06 09:30:14 +02003138
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003139 if (nla_put_status_info(msg, mdev, sib))
3140 goto nla_put_failure;
3141 genlmsg_end(msg, d_out);
3142 err = drbd_genl_multicast_events(msg, 0);
3143 /* msg has been consumed or freed in netlink_broadcast() */
3144 if (err && err != -ESRCH)
3145 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003146
Philipp Reisnerb411b362009-09-25 16:07:19 -07003147 return;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003148
3149nla_put_failure:
3150 nlmsg_free(msg);
3151failed:
3152 dev_err(DEV, "Error %d while broadcasting event. "
3153 "Event seq:%u sib_reason:%u\n",
3154 err, seq, sib->sib_reason);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003155}