blob: 0f52b88719c897847b23a200f37313ba5967431c [file] [log] [blame]
Philipp Reisnerb411b362009-09-25 16:07:19 -07001/*
2 drbd_nl.c
3
4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5
6 Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
7 Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8 Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
9
10 drbd is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 drbd is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with drbd; see the file COPYING. If not, write to
22 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 */
25
Philipp Reisnerb411b362009-09-25 16:07:19 -070026#include <linux/module.h>
27#include <linux/drbd.h>
28#include <linux/in.h>
29#include <linux/fs.h>
30#include <linux/file.h>
31#include <linux/slab.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070032#include <linux/blkpg.h>
33#include <linux/cpumask.h>
34#include "drbd_int.h"
Philipp Reisner265be2d2010-05-31 10:14:17 +020035#include "drbd_req.h"
Philipp Reisnerb411b362009-09-25 16:07:19 -070036#include "drbd_wrappers.h"
37#include <asm/unaligned.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070038#include <linux/drbd_limits.h>
Philipp Reisner87f7be42010-06-11 13:56:33 +020039#include <linux/kthread.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070040
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010041#include <net/genetlink.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070042
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010043/* .doit */
44// int drbd_adm_create_resource(struct sk_buff *skb, struct genl_info *info);
45// int drbd_adm_delete_resource(struct sk_buff *skb, struct genl_info *info);
46
47int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info);
48int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info);
49
50int drbd_adm_create_connection(struct sk_buff *skb, struct genl_info *info);
51int drbd_adm_delete_connection(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +010052int drbd_adm_down(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010053
54int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info);
55int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info);
Lars Ellenbergf3990022011-03-23 14:31:09 +010056int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010057int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info);
58int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info);
Lars Ellenbergf3990022011-03-23 14:31:09 +010059int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010060int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info);
61int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info);
62int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info);
63int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info);
64int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info);
65int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info);
66int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info);
67int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info);
68int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info);
69int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info);
70int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info);
Lars Ellenbergf3990022011-03-23 14:31:09 +010071int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010072int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info);
73int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info);
74/* .dumpit */
75int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb);
76
77#include <linux/drbd_genl_api.h>
78#include <linux/genl_magic_func.h>
79
80/* used blkdev_get_by_path, to claim our meta data device(s) */
Philipp Reisnerb411b362009-09-25 16:07:19 -070081static char *drbd_m_holder = "Hands off! this is DRBD's meta data device.";
82
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010083/* Configuration is strictly serialized, because generic netlink message
84 * processing is strictly serialized by the genl_lock().
85 * Which means we can use one static global drbd_config_context struct.
86 */
87static struct drbd_config_context {
88 /* assigned from drbd_genlmsghdr */
89 unsigned int minor;
90 /* assigned from request attributes, if present */
91 unsigned int volume;
92#define VOLUME_UNSPECIFIED (-1U)
93 /* pointer into the request skb,
94 * limited lifetime! */
95 char *conn_name;
Philipp Reisnerb411b362009-09-25 16:07:19 -070096
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010097 /* reply buffer */
98 struct sk_buff *reply_skb;
99 /* pointer into reply buffer */
100 struct drbd_genlmsghdr *reply_dh;
101 /* resolved from attributes, if possible */
102 struct drbd_conf *mdev;
103 struct drbd_tconn *tconn;
104} adm_ctx;
105
106static void drbd_adm_send_reply(struct sk_buff *skb, struct genl_info *info)
107{
108 genlmsg_end(skb, genlmsg_data(nlmsg_data(nlmsg_hdr(skb))));
109 if (genlmsg_reply(skb, info))
110 printk(KERN_ERR "drbd: error sending genl reply\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700111}
112
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100113/* Used on a fresh "drbd_adm_prepare"d reply_skb, this cannot fail: The only
114 * reason it could fail was no space in skb, and there are 4k available. */
Lars Ellenberg8432b312011-03-08 16:11:16 +0100115int drbd_msg_put_info(const char *info)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100116{
117 struct sk_buff *skb = adm_ctx.reply_skb;
118 struct nlattr *nla;
119 int err = -EMSGSIZE;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700120
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100121 if (!info || !info[0])
122 return 0;
123
124 nla = nla_nest_start(skb, DRBD_NLA_CFG_REPLY);
125 if (!nla)
126 return err;
127
128 err = nla_put_string(skb, T_info_text, info);
129 if (err) {
130 nla_nest_cancel(skb, nla);
131 return err;
132 } else
133 nla_nest_end(skb, nla);
134 return 0;
135}
136
137/* This would be a good candidate for a "pre_doit" hook,
138 * and per-family private info->pointers.
139 * But we need to stay compatible with older kernels.
140 * If it returns successfully, adm_ctx members are valid.
141 */
142#define DRBD_ADM_NEED_MINOR 1
143#define DRBD_ADM_NEED_CONN 2
144static int drbd_adm_prepare(struct sk_buff *skb, struct genl_info *info,
145 unsigned flags)
146{
147 struct drbd_genlmsghdr *d_in = info->userhdr;
148 const u8 cmd = info->genlhdr->cmd;
149 int err;
150
151 memset(&adm_ctx, 0, sizeof(adm_ctx));
152
153 /* genl_rcv_msg only checks for CAP_NET_ADMIN on "GENL_ADMIN_PERM" :( */
154 if (cmd != DRBD_ADM_GET_STATUS
155 && security_netlink_recv(skb, CAP_SYS_ADMIN))
156 return -EPERM;
157
158 adm_ctx.reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
159 if (!adm_ctx.reply_skb)
160 goto fail;
161
162 adm_ctx.reply_dh = genlmsg_put_reply(adm_ctx.reply_skb,
163 info, &drbd_genl_family, 0, cmd);
164 /* put of a few bytes into a fresh skb of >= 4k will always succeed.
165 * but anyways */
166 if (!adm_ctx.reply_dh)
167 goto fail;
168
169 adm_ctx.reply_dh->minor = d_in->minor;
170 adm_ctx.reply_dh->ret_code = NO_ERROR;
171
172 if (info->attrs[DRBD_NLA_CFG_CONTEXT]) {
173 struct nlattr *nla;
174 /* parse and validate only */
Lars Ellenbergf3990022011-03-23 14:31:09 +0100175 err = drbd_cfg_context_from_attrs(NULL, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100176 if (err)
177 goto fail;
178
179 /* It was present, and valid,
180 * copy it over to the reply skb. */
181 err = nla_put_nohdr(adm_ctx.reply_skb,
182 info->attrs[DRBD_NLA_CFG_CONTEXT]->nla_len,
183 info->attrs[DRBD_NLA_CFG_CONTEXT]);
184 if (err)
185 goto fail;
186
187 /* and assign stuff to the global adm_ctx */
188 nla = nested_attr_tb[__nla_type(T_ctx_volume)];
189 adm_ctx.volume = nla ? nla_get_u32(nla) : VOLUME_UNSPECIFIED;
190 nla = nested_attr_tb[__nla_type(T_ctx_conn_name)];
191 if (nla)
192 adm_ctx.conn_name = nla_data(nla);
193 } else
194 adm_ctx.volume = VOLUME_UNSPECIFIED;
195
196 adm_ctx.minor = d_in->minor;
197 adm_ctx.mdev = minor_to_mdev(d_in->minor);
198 adm_ctx.tconn = conn_by_name(adm_ctx.conn_name);
199
200 if (!adm_ctx.mdev && (flags & DRBD_ADM_NEED_MINOR)) {
201 drbd_msg_put_info("unknown minor");
202 return ERR_MINOR_INVALID;
203 }
204 if (!adm_ctx.tconn && (flags & DRBD_ADM_NEED_CONN)) {
205 drbd_msg_put_info("unknown connection");
206 return ERR_INVALID_REQUEST;
207 }
208
209 /* some more paranoia, if the request was over-determined */
Lars Ellenberg527f4b22011-03-14 13:58:03 +0100210 if (adm_ctx.mdev && adm_ctx.tconn &&
211 adm_ctx.mdev->tconn != adm_ctx.tconn) {
212 pr_warning("request: minor=%u, conn=%s; but that minor belongs to connection %s\n",
213 adm_ctx.minor, adm_ctx.conn_name, adm_ctx.mdev->tconn->name);
214 drbd_msg_put_info("minor exists in different connection");
215 return ERR_INVALID_REQUEST;
216 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100217 if (adm_ctx.mdev &&
218 adm_ctx.volume != VOLUME_UNSPECIFIED &&
219 adm_ctx.volume != adm_ctx.mdev->vnr) {
220 pr_warning("request: minor=%u, volume=%u; but that minor is volume %u in %s\n",
221 adm_ctx.minor, adm_ctx.volume,
222 adm_ctx.mdev->vnr, adm_ctx.mdev->tconn->name);
Lars Ellenberg527f4b22011-03-14 13:58:03 +0100223 drbd_msg_put_info("minor exists as different volume");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100224 return ERR_INVALID_REQUEST;
225 }
Lars Ellenbergcffec5b2011-03-10 23:33:15 +0100226 if (adm_ctx.mdev && !adm_ctx.tconn)
227 adm_ctx.tconn = adm_ctx.mdev->tconn;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100228 return NO_ERROR;
229
230fail:
231 nlmsg_free(adm_ctx.reply_skb);
232 adm_ctx.reply_skb = NULL;
233 return -ENOMEM;
234}
235
236static int drbd_adm_finish(struct genl_info *info, int retcode)
237{
238 struct nlattr *nla;
239 const char *conn_name = NULL;
240
241 if (!adm_ctx.reply_skb)
242 return -ENOMEM;
243
244 adm_ctx.reply_dh->ret_code = retcode;
245
246 nla = info->attrs[DRBD_NLA_CFG_CONTEXT];
247 if (nla) {
248 nla = nla_find_nested(nla, __nla_type(T_ctx_conn_name));
249 if (nla)
250 conn_name = nla_data(nla);
251 }
252
253 drbd_adm_send_reply(adm_ctx.reply_skb, info);
254 return 0;
255}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700256
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100257static void setup_khelper_env(struct drbd_tconn *tconn, char **envp)
258{
259 char *afs;
260
261 if (get_net_conf(tconn)) {
262 switch (((struct sockaddr *)tconn->net_conf->peer_addr)->sa_family) {
263 case AF_INET6:
264 afs = "ipv6";
265 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
266 &((struct sockaddr_in6 *)tconn->net_conf->peer_addr)->sin6_addr);
267 break;
268 case AF_INET:
269 afs = "ipv4";
270 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
271 &((struct sockaddr_in *)tconn->net_conf->peer_addr)->sin_addr);
272 break;
273 default:
274 afs = "ssocks";
275 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
276 &((struct sockaddr_in *)tconn->net_conf->peer_addr)->sin_addr);
277 }
278 snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
279 put_net_conf(tconn);
280 }
281}
282
Philipp Reisnerb411b362009-09-25 16:07:19 -0700283int drbd_khelper(struct drbd_conf *mdev, char *cmd)
284{
285 char *envp[] = { "HOME=/",
286 "TERM=linux",
287 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100288 (char[20]) { }, /* address family */
289 (char[60]) { }, /* address */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700290 NULL };
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100291 char mb[12];
Philipp Reisnerb411b362009-09-25 16:07:19 -0700292 char *argv[] = {usermode_helper, cmd, mb, NULL };
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100293 struct sib_info sib;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700294 int ret;
295
296 snprintf(mb, 12, "minor-%d", mdev_to_minor(mdev));
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100297 setup_khelper_env(mdev->tconn, envp);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700298
Lars Ellenberg1090c052010-07-19 17:41:04 +0200299 /* The helper may take some time.
300 * write out any unsynced meta data changes now */
301 drbd_md_sync(mdev);
302
Philipp Reisnerb411b362009-09-25 16:07:19 -0700303 dev_info(DEV, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100304 sib.sib_reason = SIB_HELPER_PRE;
305 sib.helper_name = cmd;
306 drbd_bcast_event(mdev, &sib);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700307 ret = call_usermodehelper(usermode_helper, argv, envp, 1);
308 if (ret)
309 dev_warn(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
310 usermode_helper, cmd, mb,
311 (ret >> 8) & 0xff, ret);
312 else
313 dev_info(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
314 usermode_helper, cmd, mb,
315 (ret >> 8) & 0xff, ret);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100316 sib.sib_reason = SIB_HELPER_POST;
317 sib.helper_exit_code = ret;
318 drbd_bcast_event(mdev, &sib);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700319
320 if (ret < 0) /* Ignore any ERRNOs we got. */
321 ret = 0;
322
323 return ret;
324}
325
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100326static void conn_md_sync(struct drbd_tconn *tconn)
327{
328 struct drbd_conf *mdev;
Philipp Reisnere90285e2011-03-22 12:51:21 +0100329 int vnr;
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100330
Philipp Reisnere90285e2011-03-22 12:51:21 +0100331 idr_for_each_entry(&tconn->volumes, mdev, vnr)
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100332 drbd_md_sync(mdev);
333}
334
335int conn_khelper(struct drbd_tconn *tconn, char *cmd)
336{
337 char *envp[] = { "HOME=/",
338 "TERM=linux",
339 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
340 (char[20]) { }, /* address family */
341 (char[60]) { }, /* address */
342 NULL };
343 char *argv[] = {usermode_helper, cmd, tconn->name, NULL };
344 int ret;
345
346 setup_khelper_env(tconn, envp);
347 conn_md_sync(tconn);
348
349 conn_info(tconn, "helper command: %s %s %s\n", usermode_helper, cmd, tconn->name);
350 /* TODO: conn_bcast_event() ?? */
351
352 ret = call_usermodehelper(usermode_helper, argv, envp, 1);
353 if (ret)
354 conn_warn(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
355 usermode_helper, cmd, tconn->name,
356 (ret >> 8) & 0xff, ret);
357 else
358 conn_info(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
359 usermode_helper, cmd, tconn->name,
360 (ret >> 8) & 0xff, ret);
361 /* TODO: conn_bcast_event() ?? */
362
363 if (ret < 0) /* Ignore any ERRNOs we got. */
364 ret = 0;
365
366 return ret;
367}
368
Philipp Reisnercb703452011-03-24 11:03:07 +0100369static enum drbd_fencing_p highest_fencing_policy(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700370{
Philipp Reisnercb703452011-03-24 11:03:07 +0100371 enum drbd_fencing_p fp = FP_NOT_AVAIL;
372 struct drbd_conf *mdev;
373 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700374
Philipp Reisnercb703452011-03-24 11:03:07 +0100375 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
376 if (get_ldev_if_state(mdev, D_CONSISTENT)) {
377 fp = max_t(enum drbd_fencing_p, fp, mdev->ldev->dc.fencing);
378 put_ldev(mdev);
379 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700380 }
381
Philipp Reisnercb703452011-03-24 11:03:07 +0100382 return fp;
383}
384
385bool conn_try_outdate_peer(struct drbd_tconn *tconn)
386{
387 union drbd_state mask = { };
388 union drbd_state val = { };
389 enum drbd_fencing_p fp;
390 char *ex_to_string;
391 int r;
392
393 if (tconn->cstate >= C_WF_REPORT_PARAMS) {
394 conn_err(tconn, "Expected cstate < C_WF_REPORT_PARAMS\n");
395 return false;
396 }
397
398 fp = highest_fencing_policy(tconn);
399 switch (fp) {
400 case FP_NOT_AVAIL:
401 conn_warn(tconn, "Not fencing peer, I'm not even Consistent myself.\n");
402 goto out;
403 case FP_DONT_CARE:
404 return true;
405 default: ;
406 }
407
408 r = conn_khelper(tconn, "fence-peer");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700409
410 switch ((r>>8) & 0xff) {
411 case 3: /* peer is inconsistent */
412 ex_to_string = "peer is inconsistent or worse";
Philipp Reisnercb703452011-03-24 11:03:07 +0100413 mask.pdsk = D_MASK;
414 val.pdsk = D_INCONSISTENT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700415 break;
416 case 4: /* peer got outdated, or was already outdated */
417 ex_to_string = "peer was fenced";
Philipp Reisnercb703452011-03-24 11:03:07 +0100418 mask.pdsk = D_MASK;
419 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700420 break;
421 case 5: /* peer was down */
Philipp Reisnercb703452011-03-24 11:03:07 +0100422 if (conn_highest_disk(tconn) == D_UP_TO_DATE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700423 /* we will(have) create(d) a new UUID anyways... */
424 ex_to_string = "peer is unreachable, assumed to be dead";
Philipp Reisnercb703452011-03-24 11:03:07 +0100425 mask.pdsk = D_MASK;
426 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700427 } else {
428 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
Philipp Reisnerb411b362009-09-25 16:07:19 -0700429 }
430 break;
431 case 6: /* Peer is primary, voluntarily outdate myself.
432 * This is useful when an unconnected R_SECONDARY is asked to
433 * become R_PRIMARY, but finds the other peer being active. */
434 ex_to_string = "peer is active";
Philipp Reisnercb703452011-03-24 11:03:07 +0100435 conn_warn(tconn, "Peer is primary, outdating myself.\n");
436 mask.disk = D_MASK;
437 val.disk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700438 break;
439 case 7:
440 if (fp != FP_STONITH)
Philipp Reisnercb703452011-03-24 11:03:07 +0100441 conn_err(tconn, "fence-peer() = 7 && fencing != Stonith !!!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700442 ex_to_string = "peer was stonithed";
Philipp Reisnercb703452011-03-24 11:03:07 +0100443 mask.pdsk = D_MASK;
444 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700445 break;
446 default:
447 /* The script is broken ... */
Philipp Reisnercb703452011-03-24 11:03:07 +0100448 conn_err(tconn, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
449 return false; /* Eventually leave IO frozen */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700450 }
451
Philipp Reisnercb703452011-03-24 11:03:07 +0100452 conn_info(tconn, "fence-peer helper returned %d (%s)\n",
453 (r>>8) & 0xff, ex_to_string);
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200454
Philipp Reisnercb703452011-03-24 11:03:07 +0100455 out:
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200456
Philipp Reisnercb703452011-03-24 11:03:07 +0100457 /* Not using
458 conn_request_state(tconn, mask, val, CS_VERBOSE);
459 here, because we might were able to re-establish the connection in the
460 meantime. */
461 spin_lock_irq(&tconn->req_lock);
462 if (tconn->cstate < C_WF_REPORT_PARAMS)
463 _conn_request_state(tconn, mask, val, CS_VERBOSE);
464 spin_unlock_irq(&tconn->req_lock);
465
466 return conn_highest_pdsk(tconn) <= D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700467}
468
Philipp Reisner87f7be42010-06-11 13:56:33 +0200469static int _try_outdate_peer_async(void *data)
470{
Philipp Reisnercb703452011-03-24 11:03:07 +0100471 struct drbd_tconn *tconn = (struct drbd_tconn *)data;
Philipp Reisner87f7be42010-06-11 13:56:33 +0200472
Philipp Reisnercb703452011-03-24 11:03:07 +0100473 conn_try_outdate_peer(tconn);
Philipp Reisner87f7be42010-06-11 13:56:33 +0200474
475 return 0;
476}
477
Philipp Reisnercb703452011-03-24 11:03:07 +0100478void conn_try_outdate_peer_async(struct drbd_tconn *tconn)
Philipp Reisner87f7be42010-06-11 13:56:33 +0200479{
480 struct task_struct *opa;
481
Philipp Reisnercb703452011-03-24 11:03:07 +0100482 opa = kthread_run(_try_outdate_peer_async, tconn, "drbd_async_h");
Philipp Reisner87f7be42010-06-11 13:56:33 +0200483 if (IS_ERR(opa))
Philipp Reisnercb703452011-03-24 11:03:07 +0100484 conn_err(tconn, "out of mem, failed to invoke fence-peer helper\n");
Philipp Reisner87f7be42010-06-11 13:56:33 +0200485}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700486
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100487enum drbd_state_rv
488drbd_set_role(struct drbd_conf *mdev, enum drbd_role new_role, int force)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700489{
490 const int max_tries = 4;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100491 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700492 int try = 0;
493 int forced = 0;
494 union drbd_state mask, val;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700495
496 if (new_role == R_PRIMARY)
Philipp Reisner0625ac12011-02-07 14:49:19 +0100497 request_ping(mdev->tconn); /* Detect a dead peer ASAP */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700498
Philipp Reisner8410da8f02011-02-11 20:11:10 +0100499 mutex_lock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700500
501 mask.i = 0; mask.role = R_MASK;
502 val.i = 0; val.role = new_role;
503
504 while (try++ < max_tries) {
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100505 rv = _drbd_request_state(mdev, mask, val, CS_WAIT_COMPLETE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700506
507 /* in case we first succeeded to outdate,
508 * but now suddenly could establish a connection */
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100509 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700510 val.pdsk = 0;
511 mask.pdsk = 0;
512 continue;
513 }
514
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100515 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
Philipp Reisnerd10a33c2010-03-04 15:11:39 +0100516 (mdev->state.disk < D_UP_TO_DATE &&
517 mdev->state.disk >= D_INCONSISTENT)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700518 mask.disk = D_MASK;
519 val.disk = D_UP_TO_DATE;
520 forced = 1;
521 continue;
522 }
523
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100524 if (rv == SS_NO_UP_TO_DATE_DISK &&
Philipp Reisnerb411b362009-09-25 16:07:19 -0700525 mdev->state.disk == D_CONSISTENT && mask.pdsk == 0) {
526 D_ASSERT(mdev->state.pdsk == D_UNKNOWN);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700527
Philipp Reisnercb703452011-03-24 11:03:07 +0100528 if (conn_try_outdate_peer(mdev->tconn)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700529 val.disk = D_UP_TO_DATE;
530 mask.disk = D_MASK;
531 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700532 continue;
533 }
534
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100535 if (rv == SS_NOTHING_TO_DO)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100536 goto out;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100537 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
Philipp Reisnercb703452011-03-24 11:03:07 +0100538 if (!conn_try_outdate_peer(mdev->tconn) && force) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700539 dev_warn(DEV, "Forced into split brain situation!\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100540 mask.pdsk = D_MASK;
541 val.pdsk = D_OUTDATED;
542
Philipp Reisnerb411b362009-09-25 16:07:19 -0700543 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700544 continue;
545 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100546 if (rv == SS_TWO_PRIMARIES) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700547 /* Maybe the peer is detected as dead very soon...
548 retry at most once more in this case. */
Philipp Reisner89e58e72011-01-19 13:12:45 +0100549 schedule_timeout_interruptible((mdev->tconn->net_conf->ping_timeo+1)*HZ/10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700550 if (try < max_tries)
551 try = max_tries - 1;
552 continue;
553 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100554 if (rv < SS_SUCCESS) {
555 rv = _drbd_request_state(mdev, mask, val,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700556 CS_VERBOSE + CS_WAIT_COMPLETE);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100557 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100558 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700559 }
560 break;
561 }
562
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100563 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100564 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700565
566 if (forced)
567 dev_warn(DEV, "Forced to consider local data as UpToDate!\n");
568
569 /* Wait until nothing is on the fly :) */
570 wait_event(mdev->misc_wait, atomic_read(&mdev->ap_pending_cnt) == 0);
571
572 if (new_role == R_SECONDARY) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100573 set_disk_ro(mdev->vdisk, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700574 if (get_ldev(mdev)) {
575 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
576 put_ldev(mdev);
577 }
578 } else {
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +0100579 if (get_net_conf(mdev->tconn)) {
Philipp Reisner89e58e72011-01-19 13:12:45 +0100580 mdev->tconn->net_conf->want_lose = 0;
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +0100581 put_net_conf(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700582 }
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100583 set_disk_ro(mdev->vdisk, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700584 if (get_ldev(mdev)) {
585 if (((mdev->state.conn < C_CONNECTED ||
586 mdev->state.pdsk <= D_FAILED)
587 && mdev->ldev->md.uuid[UI_BITMAP] == 0) || forced)
588 drbd_uuid_new_current(mdev);
589
590 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
591 put_ldev(mdev);
592 }
593 }
594
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100595 /* writeout of activity log covered areas of the bitmap
596 * to stable storage done in after state change already */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700597
598 if (mdev->state.conn >= C_WF_REPORT_PARAMS) {
599 /* if this was forced, we should consider sync */
600 if (forced)
601 drbd_send_uuids(mdev);
602 drbd_send_state(mdev);
603 }
604
605 drbd_md_sync(mdev);
606
607 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100608out:
Philipp Reisner8410da8f02011-02-11 20:11:10 +0100609 mutex_unlock(mdev->state_mutex);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100610 return rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700611}
612
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100613static const char *from_attrs_err_to_txt(int err)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700614{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100615 return err == -ENOMSG ? "required attribute missing" :
616 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
Lars Ellenbergf3990022011-03-23 14:31:09 +0100617 err == -EEXIST ? "can not change invariant setting" :
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100618 "invalid attribute value";
Philipp Reisnerb411b362009-09-25 16:07:19 -0700619}
620
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100621int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700622{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100623 struct set_role_parms parms;
624 int err;
625 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700626
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100627 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
628 if (!adm_ctx.reply_skb)
629 return retcode;
630 if (retcode != NO_ERROR)
631 goto out;
632
633 memset(&parms, 0, sizeof(parms));
634 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +0100635 err = set_role_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100636 if (err) {
637 retcode = ERR_MANDATORY_TAG;
638 drbd_msg_put_info(from_attrs_err_to_txt(err));
639 goto out;
640 }
641 }
642
643 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
644 retcode = drbd_set_role(adm_ctx.mdev, R_PRIMARY, parms.assume_uptodate);
645 else
646 retcode = drbd_set_role(adm_ctx.mdev, R_SECONDARY, 0);
647out:
648 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700649 return 0;
650}
651
652/* initializes the md.*_offset members, so we are able to find
653 * the on disk meta data */
654static void drbd_md_set_sector_offsets(struct drbd_conf *mdev,
655 struct drbd_backing_dev *bdev)
656{
657 sector_t md_size_sect = 0;
658 switch (bdev->dc.meta_dev_idx) {
659 default:
660 /* v07 style fixed size indexed meta data */
661 bdev->md.md_size_sect = MD_RESERVED_SECT;
662 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
663 bdev->md.al_offset = MD_AL_OFFSET;
664 bdev->md.bm_offset = MD_BM_OFFSET;
665 break;
666 case DRBD_MD_INDEX_FLEX_EXT:
667 /* just occupy the full device; unit: sectors */
668 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
669 bdev->md.md_offset = 0;
670 bdev->md.al_offset = MD_AL_OFFSET;
671 bdev->md.bm_offset = MD_BM_OFFSET;
672 break;
673 case DRBD_MD_INDEX_INTERNAL:
674 case DRBD_MD_INDEX_FLEX_INT:
675 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
676 /* al size is still fixed */
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100677 bdev->md.al_offset = -MD_AL_SECTORS;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700678 /* we need (slightly less than) ~ this much bitmap sectors: */
679 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
680 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
681 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
682 md_size_sect = ALIGN(md_size_sect, 8);
683
684 /* plus the "drbd meta data super block",
685 * and the activity log; */
686 md_size_sect += MD_BM_OFFSET;
687
688 bdev->md.md_size_sect = md_size_sect;
689 /* bitmap offset is adjusted by 'super' block size */
690 bdev->md.bm_offset = -md_size_sect + MD_AL_OFFSET;
691 break;
692 }
693}
694
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100695/* input size is expected to be in KB */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700696char *ppsize(char *buf, unsigned long long size)
697{
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100698 /* Needs 9 bytes at max including trailing NUL:
699 * -1ULL ==> "16384 EB" */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700700 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
701 int base = 0;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100702 while (size >= 10000 && base < sizeof(units)-1) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700703 /* shift + round */
704 size = (size >> 10) + !!(size & (1<<9));
705 base++;
706 }
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100707 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700708
709 return buf;
710}
711
712/* there is still a theoretical deadlock when called from receiver
713 * on an D_INCONSISTENT R_PRIMARY:
714 * remote READ does inc_ap_bio, receiver would need to receive answer
715 * packet from remote to dec_ap_bio again.
716 * receiver receive_sizes(), comes here,
717 * waits for ap_bio_cnt == 0. -> deadlock.
718 * but this cannot happen, actually, because:
719 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
720 * (not connected, or bad/no disk on peer):
721 * see drbd_fail_request_early, ap_bio_cnt is zero.
722 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
723 * peer may not initiate a resize.
724 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100725/* Note these are not to be confused with
726 * drbd_adm_suspend_io/drbd_adm_resume_io,
727 * which are (sub) state changes triggered by admin (drbdsetup),
728 * and can be long lived.
729 * This changes an mdev->flag, is triggered by drbd internals,
730 * and should be short-lived. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700731void drbd_suspend_io(struct drbd_conf *mdev)
732{
733 set_bit(SUSPEND_IO, &mdev->flags);
Philipp Reisner2aebfab2011-03-28 16:48:11 +0200734 if (drbd_suspended(mdev))
Philipp Reisner265be2d2010-05-31 10:14:17 +0200735 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700736 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
737}
738
739void drbd_resume_io(struct drbd_conf *mdev)
740{
741 clear_bit(SUSPEND_IO, &mdev->flags);
742 wake_up(&mdev->misc_wait);
743}
744
745/**
746 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
747 * @mdev: DRBD device.
748 *
749 * Returns 0 on success, negative return values indicate errors.
750 * You should call drbd_md_sync() after calling this function.
751 */
Bart Van Assche24c48302011-05-21 18:32:29 +0200752enum 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 -0700753{
754 sector_t prev_first_sect, prev_size; /* previous meta location */
755 sector_t la_size;
756 sector_t size;
757 char ppb[10];
758
759 int md_moved, la_size_changed;
760 enum determine_dev_size rv = unchanged;
761
762 /* race:
763 * application request passes inc_ap_bio,
764 * but then cannot get an AL-reference.
765 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
766 *
767 * to avoid that:
768 * Suspend IO right here.
769 * still lock the act_log to not trigger ASSERTs there.
770 */
771 drbd_suspend_io(mdev);
772
773 /* no wait necessary anymore, actually we could assert that */
774 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
775
776 prev_first_sect = drbd_md_first_sector(mdev->ldev);
777 prev_size = mdev->ldev->md.md_size_sect;
778 la_size = mdev->ldev->md.la_size_sect;
779
780 /* TODO: should only be some assert here, not (re)init... */
781 drbd_md_set_sector_offsets(mdev, mdev->ldev);
782
Philipp Reisnerd8450302010-03-24 15:51:26 +0100783 size = drbd_new_dev_size(mdev, mdev->ldev, flags & DDSF_FORCED);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700784
785 if (drbd_get_capacity(mdev->this_bdev) != size ||
786 drbd_bm_capacity(mdev) != size) {
787 int err;
Philipp Reisner02d9a942010-03-24 16:23:03 +0100788 err = drbd_bm_resize(mdev, size, !(flags & DDSF_NO_RESYNC));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700789 if (unlikely(err)) {
790 /* currently there is only one error: ENOMEM! */
791 size = drbd_bm_capacity(mdev)>>1;
792 if (size == 0) {
793 dev_err(DEV, "OUT OF MEMORY! "
794 "Could not allocate bitmap!\n");
795 } else {
796 dev_err(DEV, "BM resizing failed. "
797 "Leaving size unchanged at size = %lu KB\n",
798 (unsigned long)size);
799 }
800 rv = dev_size_error;
801 }
802 /* racy, see comments above. */
803 drbd_set_my_capacity(mdev, size);
804 mdev->ldev->md.la_size_sect = size;
805 dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
806 (unsigned long long)size>>1);
807 }
808 if (rv == dev_size_error)
809 goto out;
810
811 la_size_changed = (la_size != mdev->ldev->md.la_size_sect);
812
813 md_moved = prev_first_sect != drbd_md_first_sector(mdev->ldev)
814 || prev_size != mdev->ldev->md.md_size_sect;
815
816 if (la_size_changed || md_moved) {
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100817 int err;
818
Philipp Reisnerb411b362009-09-25 16:07:19 -0700819 drbd_al_shrink(mdev); /* All extents inactive. */
820 dev_info(DEV, "Writing the whole bitmap, %s\n",
821 la_size_changed && md_moved ? "size changed and md moved" :
822 la_size_changed ? "size changed" : "md moved");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100823 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
824 err = drbd_bitmap_io(mdev, &drbd_bm_write,
825 "size changed", BM_LOCKED_MASK);
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100826 if (err) {
827 rv = dev_size_error;
828 goto out;
829 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700830 drbd_md_mark_dirty(mdev);
831 }
832
833 if (size > la_size)
834 rv = grew;
835 if (size < la_size)
836 rv = shrunk;
837out:
838 lc_unlock(mdev->act_log);
839 wake_up(&mdev->al_wait);
840 drbd_resume_io(mdev);
841
842 return rv;
843}
844
845sector_t
Philipp Reisnera393db62009-12-22 13:35:52 +0100846drbd_new_dev_size(struct drbd_conf *mdev, struct drbd_backing_dev *bdev, int assume_peer_has_space)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700847{
848 sector_t p_size = mdev->p_size; /* partner's disk size. */
849 sector_t la_size = bdev->md.la_size_sect; /* last agreed size. */
850 sector_t m_size; /* my size */
851 sector_t u_size = bdev->dc.disk_size; /* size requested by user. */
852 sector_t size = 0;
853
854 m_size = drbd_get_max_capacity(bdev);
855
Philipp Reisnera393db62009-12-22 13:35:52 +0100856 if (mdev->state.conn < C_CONNECTED && assume_peer_has_space) {
857 dev_warn(DEV, "Resize while not connected was forced by the user!\n");
858 p_size = m_size;
859 }
860
Philipp Reisnerb411b362009-09-25 16:07:19 -0700861 if (p_size && m_size) {
862 size = min_t(sector_t, p_size, m_size);
863 } else {
864 if (la_size) {
865 size = la_size;
866 if (m_size && m_size < size)
867 size = m_size;
868 if (p_size && p_size < size)
869 size = p_size;
870 } else {
871 if (m_size)
872 size = m_size;
873 if (p_size)
874 size = p_size;
875 }
876 }
877
878 if (size == 0)
879 dev_err(DEV, "Both nodes diskless!\n");
880
881 if (u_size) {
882 if (u_size > size)
883 dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n",
884 (unsigned long)u_size>>1, (unsigned long)size>>1);
885 else
886 size = u_size;
887 }
888
889 return size;
890}
891
892/**
893 * drbd_check_al_size() - Ensures that the AL is of the right size
894 * @mdev: DRBD device.
895 *
896 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
897 * failed, and 0 on success. You should call drbd_md_sync() after you called
898 * this function.
899 */
Lars Ellenbergf3990022011-03-23 14:31:09 +0100900static int drbd_check_al_size(struct drbd_conf *mdev, struct disk_conf *dc)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700901{
902 struct lru_cache *n, *t;
903 struct lc_element *e;
904 unsigned int in_use;
905 int i;
906
Lars Ellenbergf3990022011-03-23 14:31:09 +0100907 if (!expect(dc->al_extents >= DRBD_AL_EXTENTS_MIN))
908 dc->al_extents = DRBD_AL_EXTENTS_MIN;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700909
910 if (mdev->act_log &&
Lars Ellenbergf3990022011-03-23 14:31:09 +0100911 mdev->act_log->nr_elements == dc->al_extents)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700912 return 0;
913
914 in_use = 0;
915 t = mdev->act_log;
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100916 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
Lars Ellenbergf3990022011-03-23 14:31:09 +0100917 dc->al_extents, sizeof(struct lc_element), 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700918
919 if (n == NULL) {
920 dev_err(DEV, "Cannot allocate act_log lru!\n");
921 return -ENOMEM;
922 }
923 spin_lock_irq(&mdev->al_lock);
924 if (t) {
925 for (i = 0; i < t->nr_elements; i++) {
926 e = lc_element_by_index(t, i);
927 if (e->refcnt)
928 dev_err(DEV, "refcnt(%d)==%d\n",
929 e->lc_number, e->refcnt);
930 in_use += e->refcnt;
931 }
932 }
933 if (!in_use)
934 mdev->act_log = n;
935 spin_unlock_irq(&mdev->al_lock);
936 if (in_use) {
937 dev_err(DEV, "Activity log still in use!\n");
938 lc_destroy(n);
939 return -EBUSY;
940 } else {
941 if (t)
942 lc_destroy(t);
943 }
944 drbd_md_mark_dirty(mdev); /* we changed mdev->act_log->nr_elemens */
945 return 0;
946}
947
Philipp Reisner99432fc2011-05-20 16:39:13 +0200948static void drbd_setup_queue_param(struct drbd_conf *mdev, unsigned int max_bio_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700949{
950 struct request_queue * const q = mdev->rq_queue;
Philipp Reisner99432fc2011-05-20 16:39:13 +0200951 int max_hw_sectors = max_bio_size >> 9;
952 int max_segments = 0;
953
954 if (get_ldev_if_state(mdev, D_ATTACHING)) {
955 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
956
957 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
958 max_segments = mdev->ldev->dc.max_bio_bvecs;
959 put_ldev(mdev);
960 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700961
Philipp Reisnerb411b362009-09-25 16:07:19 -0700962 blk_queue_logical_block_size(q, 512);
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100963 blk_queue_max_hw_sectors(q, max_hw_sectors);
964 /* This is the workaround for "bio would need to, but cannot, be split" */
965 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
966 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700967
Philipp Reisner99432fc2011-05-20 16:39:13 +0200968 if (get_ldev_if_state(mdev, D_ATTACHING)) {
969 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700970
Philipp Reisner99432fc2011-05-20 16:39:13 +0200971 blk_queue_stack_limits(q, b);
972
973 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
974 dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
975 q->backing_dev_info.ra_pages,
976 b->backing_dev_info.ra_pages);
977 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
978 }
979 put_ldev(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700980 }
981}
982
Philipp Reisner99432fc2011-05-20 16:39:13 +0200983void drbd_reconsider_max_bio_size(struct drbd_conf *mdev)
984{
985 int now, new, local, peer;
986
987 now = queue_max_hw_sectors(mdev->rq_queue) << 9;
988 local = mdev->local_max_bio_size; /* Eventually last known value, from volatile memory */
989 peer = mdev->peer_max_bio_size; /* Eventually last known value, from meta data */
990
991 if (get_ldev_if_state(mdev, D_ATTACHING)) {
992 local = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
993 mdev->local_max_bio_size = local;
994 put_ldev(mdev);
995 }
996
997 /* We may ignore peer limits if the peer is modern enough.
998 Because new from 8.3.8 onwards the peer can use multiple
999 BIOs for a single peer_request */
1000 if (mdev->state.conn >= C_CONNECTED) {
Philipp Reisner31890f42011-01-19 14:12:51 +01001001 if (mdev->tconn->agreed_pro_version < 94)
Philipp Reisner99432fc2011-05-20 16:39:13 +02001002 peer = mdev->peer_max_bio_size;
Philipp Reisner31890f42011-01-19 14:12:51 +01001003 else if (mdev->tconn->agreed_pro_version == 94)
Philipp Reisner99432fc2011-05-20 16:39:13 +02001004 peer = DRBD_MAX_SIZE_H80_PACKET;
1005 else /* drbd 8.3.8 onwards */
1006 peer = DRBD_MAX_BIO_SIZE;
1007 }
1008
1009 new = min_t(int, local, peer);
1010
1011 if (mdev->state.role == R_PRIMARY && new < now)
1012 dev_err(DEV, "ASSERT FAILED new < now; (%d < %d)\n", new, now);
1013
1014 if (new != now)
1015 dev_info(DEV, "max BIO size = %u\n", new);
1016
1017 drbd_setup_queue_param(mdev, new);
1018}
1019
Philipp Reisnerb411b362009-09-25 16:07:19 -07001020/* serialize deconfig (worker exiting, doing cleanup)
1021 * and reconfig (drbdsetup disk, drbdsetup net)
1022 *
Lars Ellenbergc518d042010-09-01 09:50:23 +02001023 * Wait for a potentially exiting worker, then restart it,
1024 * or start a new one. Flush any pending work, there may still be an
1025 * after_state_change queued.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001026 */
Philipp Reisner0e29d162011-02-18 14:23:11 +01001027static void conn_reconfig_start(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001028{
Philipp Reisner0e29d162011-02-18 14:23:11 +01001029 wait_event(tconn->ping_wait, !test_and_set_bit(CONFIG_PENDING, &tconn->flags));
1030 wait_event(tconn->ping_wait, !test_bit(OBJECT_DYING, &tconn->flags));
1031 drbd_thread_start(&tconn->worker);
1032 conn_flush_workqueue(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001033}
1034
1035/* if still unconfigured, stops worker again.
1036 * if configured now, clears CONFIG_PENDING.
1037 * wakes potential waiters */
Philipp Reisner0e29d162011-02-18 14:23:11 +01001038static void conn_reconfig_done(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001039{
Philipp Reisner0e29d162011-02-18 14:23:11 +01001040 spin_lock_irq(&tconn->req_lock);
1041 if (conn_all_vols_unconf(tconn)) {
1042 set_bit(OBJECT_DYING, &tconn->flags);
1043 drbd_thread_stop_nowait(&tconn->worker);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001044 } else
Philipp Reisner0e29d162011-02-18 14:23:11 +01001045 clear_bit(CONFIG_PENDING, &tconn->flags);
1046 spin_unlock_irq(&tconn->req_lock);
1047 wake_up(&tconn->ping_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001048}
1049
Philipp Reisner07782862010-08-31 12:00:50 +02001050/* Make sure IO is suspended before calling this function(). */
1051static void drbd_suspend_al(struct drbd_conf *mdev)
1052{
1053 int s = 0;
1054
Lars Ellenberg61610422011-02-21 13:20:54 +01001055 if (!lc_try_lock(mdev->act_log)) {
Philipp Reisner07782862010-08-31 12:00:50 +02001056 dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n");
1057 return;
1058 }
1059
Lars Ellenberg61610422011-02-21 13:20:54 +01001060 drbd_al_shrink(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001061 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner07782862010-08-31 12:00:50 +02001062 if (mdev->state.conn < C_CONNECTED)
1063 s = !test_and_set_bit(AL_SUSPENDED, &mdev->flags);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001064 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg61610422011-02-21 13:20:54 +01001065 lc_unlock(mdev->act_log);
Philipp Reisner07782862010-08-31 12:00:50 +02001066
1067 if (s)
1068 dev_info(DEV, "Suspended AL updates\n");
1069}
1070
Lars Ellenbergf3990022011-03-23 14:31:09 +01001071int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1072{
1073 enum drbd_ret_code retcode;
1074 struct drbd_conf *mdev;
1075 struct disk_conf *ndc; /* new disk conf */
1076 int err, fifo_size;
1077 int *rs_plan_s = NULL;
1078
1079 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1080 if (!adm_ctx.reply_skb)
1081 return retcode;
1082 if (retcode != NO_ERROR)
1083 goto out;
1084
1085 mdev = adm_ctx.mdev;
1086
1087 /* we also need a disk
1088 * to change the options on */
1089 if (!get_ldev(mdev)) {
1090 retcode = ERR_NO_DISK;
1091 goto out;
1092 }
1093
1094/* FIXME freeze IO, cluster wide.
1095 *
1096 * We should make sure no-one uses
1097 * some half-updated struct when we
1098 * assign it later. */
1099
1100 ndc = kmalloc(sizeof(*ndc), GFP_KERNEL);
1101 if (!ndc) {
1102 retcode = ERR_NOMEM;
1103 goto fail;
1104 }
1105
1106 memcpy(ndc, &mdev->ldev->dc, sizeof(*ndc));
1107 err = disk_conf_from_attrs_for_change(ndc, info);
1108 if (err) {
1109 retcode = ERR_MANDATORY_TAG;
1110 drbd_msg_put_info(from_attrs_err_to_txt(err));
1111 }
1112
1113 if (!expect(ndc->resync_rate >= 1))
1114 ndc->resync_rate = 1;
1115
1116 /* clip to allowed range */
1117 if (!expect(ndc->al_extents >= DRBD_AL_EXTENTS_MIN))
1118 ndc->al_extents = DRBD_AL_EXTENTS_MIN;
1119 if (!expect(ndc->al_extents <= DRBD_AL_EXTENTS_MAX))
1120 ndc->al_extents = DRBD_AL_EXTENTS_MAX;
1121
1122 /* most sanity checks done, try to assign the new sync-after
1123 * dependency. need to hold the global lock in there,
1124 * to avoid a race in the dependency loop check. */
1125 retcode = drbd_alter_sa(mdev, ndc->resync_after);
1126 if (retcode != NO_ERROR)
1127 goto fail;
1128
1129 fifo_size = (ndc->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
1130 if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) {
1131 rs_plan_s = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL);
1132 if (!rs_plan_s) {
1133 dev_err(DEV, "kmalloc of fifo_buffer failed");
1134 retcode = ERR_NOMEM;
1135 goto fail;
1136 }
1137 }
1138
1139 if (fifo_size != mdev->rs_plan_s.size) {
1140 kfree(mdev->rs_plan_s.values);
1141 mdev->rs_plan_s.values = rs_plan_s;
1142 mdev->rs_plan_s.size = fifo_size;
1143 mdev->rs_planed = 0;
1144 rs_plan_s = NULL;
1145 }
1146
1147 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
1148 drbd_al_shrink(mdev);
1149 err = drbd_check_al_size(mdev, ndc);
1150 lc_unlock(mdev->act_log);
1151 wake_up(&mdev->al_wait);
1152
1153 if (err) {
1154 retcode = ERR_NOMEM;
1155 goto fail;
1156 }
1157
1158 /* FIXME
1159 * To avoid someone looking at a half-updated struct, we probably
1160 * should have a rw-semaphor on net_conf and disk_conf.
1161 */
1162 mdev->ldev->dc = *ndc;
1163
1164 drbd_md_sync(mdev);
1165
1166
1167 if (mdev->state.conn >= C_CONNECTED)
1168 drbd_send_sync_param(mdev);
1169
1170 fail:
1171 put_ldev(mdev);
1172 kfree(ndc);
1173 kfree(rs_plan_s);
1174 out:
1175 drbd_adm_finish(info, retcode);
1176 return 0;
1177}
1178
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001179int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001180{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001181 struct drbd_conf *mdev;
1182 int err;
Andreas Gruenbacher116676c2010-12-08 13:33:11 +01001183 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001184 enum determine_dev_size dd;
1185 sector_t max_possible_sectors;
1186 sector_t min_md_device_sectors;
1187 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
Tejun Heoe525fd82010-11-13 11:55:17 +01001188 struct block_device *bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001189 struct lru_cache *resync_lru = NULL;
1190 union drbd_state ns, os;
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001191 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001192 int cp_discovered = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001193
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001194 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1195 if (!adm_ctx.reply_skb)
1196 return retcode;
1197 if (retcode != NO_ERROR)
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001198 goto finish;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001199
1200 mdev = adm_ctx.mdev;
Philipp Reisner0e29d162011-02-18 14:23:11 +01001201 conn_reconfig_start(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001202
1203 /* if you want to reconfigure, please tear down first */
1204 if (mdev->state.disk > D_DISKLESS) {
1205 retcode = ERR_DISK_CONFIGURED;
1206 goto fail;
1207 }
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001208 /* It may just now have detached because of IO error. Make sure
1209 * drbd_ldev_destroy is done already, we may end up here very fast,
1210 * e.g. if someone calls attach from the on-io-error handler,
1211 * to realize a "hot spare" feature (not that I'd recommend that) */
1212 wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001213
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001214 /* allocation not in the IO path, drbdsetup context */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001215 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1216 if (!nbc) {
1217 retcode = ERR_NOMEM;
1218 goto fail;
1219 }
1220
Lars Ellenbergf3990022011-03-23 14:31:09 +01001221 nbc->dc = (struct disk_conf) {
1222 {}, 0, /* backing_dev */
1223 {}, 0, /* meta_dev */
1224 0, /* meta_dev_idx */
1225 DRBD_DISK_SIZE_SECT_DEF, /* disk_size */
1226 DRBD_MAX_BIO_BVECS_DEF, /* max_bio_bvecs */
1227 DRBD_ON_IO_ERROR_DEF, /* on_io_error */
1228 DRBD_FENCING_DEF, /* fencing */
1229 DRBD_RATE_DEF, /* resync_rate */
1230 DRBD_AFTER_DEF, /* resync_after */
1231 DRBD_AL_EXTENTS_DEF, /* al_extents */
1232 DRBD_C_PLAN_AHEAD_DEF, /* c_plan_ahead */
1233 DRBD_C_DELAY_TARGET_DEF, /* c_delay_target */
1234 DRBD_C_FILL_TARGET_DEF, /* c_fill_target */
1235 DRBD_C_MAX_RATE_DEF, /* c_max_rate */
1236 DRBD_C_MIN_RATE_DEF, /* c_min_rate */
1237 0, /* no_disk_barrier */
1238 0, /* no_disk_flush */
1239 0, /* no_disk_drain */
1240 0, /* no_md_flush */
1241 };
Philipp Reisnerb411b362009-09-25 16:07:19 -07001242
Lars Ellenbergf3990022011-03-23 14:31:09 +01001243 err = disk_conf_from_attrs(&nbc->dc, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001244 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001245 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001246 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001247 goto fail;
1248 }
1249
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001250 if ((int)nbc->dc.meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001251 retcode = ERR_MD_IDX_INVALID;
1252 goto fail;
1253 }
1254
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01001255 if (get_net_conf(mdev->tconn)) {
Philipp Reisner89e58e72011-01-19 13:12:45 +01001256 int prot = mdev->tconn->net_conf->wire_protocol;
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01001257 put_net_conf(mdev->tconn);
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001258 if (nbc->dc.fencing == FP_STONITH && prot == DRBD_PROT_A) {
1259 retcode = ERR_STONITH_AND_PROT_A;
1260 goto fail;
1261 }
1262 }
1263
Tejun Heod4d77622010-11-13 11:55:18 +01001264 bdev = blkdev_get_by_path(nbc->dc.backing_dev,
1265 FMODE_READ | FMODE_WRITE | FMODE_EXCL, mdev);
Tejun Heoe525fd82010-11-13 11:55:17 +01001266 if (IS_ERR(bdev)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001267 dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.backing_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001268 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001269 retcode = ERR_OPEN_DISK;
1270 goto fail;
1271 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001272 nbc->backing_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001273
Tejun Heoe525fd82010-11-13 11:55:17 +01001274 /*
1275 * meta_dev_idx >= 0: external fixed size, possibly multiple
1276 * drbd sharing one meta device. TODO in that case, paranoia
1277 * check that [md_bdev, meta_dev_idx] is not yet used by some
1278 * other drbd minor! (if you use drbd.conf + drbdadm, that
1279 * should check it for you already; but if you don't, or
1280 * someone fooled it, we need to double check here)
1281 */
Tejun Heod4d77622010-11-13 11:55:18 +01001282 bdev = blkdev_get_by_path(nbc->dc.meta_dev,
1283 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001284 ((int)nbc->dc.meta_dev_idx < 0) ?
Tejun Heod4d77622010-11-13 11:55:18 +01001285 (void *)mdev : (void *)drbd_m_holder);
Tejun Heoe525fd82010-11-13 11:55:17 +01001286 if (IS_ERR(bdev)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001287 dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.meta_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001288 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001289 retcode = ERR_OPEN_MD_DISK;
1290 goto fail;
1291 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001292 nbc->md_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001293
Tejun Heoe525fd82010-11-13 11:55:17 +01001294 if ((nbc->backing_bdev == nbc->md_bdev) !=
1295 (nbc->dc.meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1296 nbc->dc.meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
1297 retcode = ERR_MD_IDX_INVALID;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001298 goto fail;
1299 }
1300
1301 resync_lru = lc_create("resync", drbd_bm_ext_cache,
Lars Ellenberg46a15bc2011-02-21 13:21:01 +01001302 1, 61, sizeof(struct bm_extent),
Philipp Reisnerb411b362009-09-25 16:07:19 -07001303 offsetof(struct bm_extent, lce));
1304 if (!resync_lru) {
1305 retcode = ERR_NOMEM;
Tejun Heoe525fd82010-11-13 11:55:17 +01001306 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001307 }
1308
1309 /* RT - for drbd_get_max_capacity() DRBD_MD_INDEX_FLEX_INT */
1310 drbd_md_set_sector_offsets(mdev, nbc);
1311
1312 if (drbd_get_max_capacity(nbc) < nbc->dc.disk_size) {
1313 dev_err(DEV, "max capacity %llu smaller than disk size %llu\n",
1314 (unsigned long long) drbd_get_max_capacity(nbc),
1315 (unsigned long long) nbc->dc.disk_size);
1316 retcode = ERR_DISK_TO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001317 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001318 }
1319
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001320 if ((int)nbc->dc.meta_dev_idx < 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001321 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1322 /* at least one MB, otherwise it does not make sense */
1323 min_md_device_sectors = (2<<10);
1324 } else {
1325 max_possible_sectors = DRBD_MAX_SECTORS;
1326 min_md_device_sectors = MD_RESERVED_SECT * (nbc->dc.meta_dev_idx + 1);
1327 }
1328
Philipp Reisnerb411b362009-09-25 16:07:19 -07001329 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
1330 retcode = ERR_MD_DISK_TO_SMALL;
1331 dev_warn(DEV, "refusing attach: md-device too small, "
1332 "at least %llu sectors needed for this meta-disk type\n",
1333 (unsigned long long) min_md_device_sectors);
Tejun Heoe525fd82010-11-13 11:55:17 +01001334 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001335 }
1336
1337 /* Make sure the new disk is big enough
1338 * (we may currently be R_PRIMARY with no local disk...) */
1339 if (drbd_get_max_capacity(nbc) <
1340 drbd_get_capacity(mdev->this_bdev)) {
1341 retcode = ERR_DISK_TO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001342 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001343 }
1344
1345 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1346
Lars Ellenberg13529942009-10-12 19:07:49 +02001347 if (nbc->known_size > max_possible_sectors) {
1348 dev_warn(DEV, "==> truncating very big lower level device "
1349 "to currently maximum possible %llu sectors <==\n",
1350 (unsigned long long) max_possible_sectors);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001351 if ((int)nbc->dc.meta_dev_idx >= 0)
Lars Ellenberg13529942009-10-12 19:07:49 +02001352 dev_warn(DEV, "==>> using internal or flexible "
1353 "meta data may help <<==\n");
1354 }
1355
Philipp Reisnerb411b362009-09-25 16:07:19 -07001356 drbd_suspend_io(mdev);
1357 /* also wait for the last barrier ack. */
Philipp Reisner2aebfab2011-03-28 16:48:11 +02001358 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_pending_cnt) || drbd_suspended(mdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001359 /* and for any other previously queued work */
Philipp Reisnera21e9292011-02-08 15:08:49 +01001360 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001361
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001362 rv = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE);
1363 retcode = rv; /* FIXME: Type mismatch. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001364 drbd_resume_io(mdev);
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001365 if (rv < SS_SUCCESS)
Tejun Heoe525fd82010-11-13 11:55:17 +01001366 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001367
1368 if (!get_ldev_if_state(mdev, D_ATTACHING))
1369 goto force_diskless;
1370
1371 drbd_md_set_sector_offsets(mdev, nbc);
1372
1373 if (!mdev->bitmap) {
1374 if (drbd_bm_init(mdev)) {
1375 retcode = ERR_NOMEM;
1376 goto force_diskless_dec;
1377 }
1378 }
1379
1380 retcode = drbd_md_read(mdev, nbc);
1381 if (retcode != NO_ERROR)
1382 goto force_diskless_dec;
1383
1384 if (mdev->state.conn < C_CONNECTED &&
1385 mdev->state.role == R_PRIMARY &&
1386 (mdev->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
1387 dev_err(DEV, "Can only attach to data with current UUID=%016llX\n",
1388 (unsigned long long)mdev->ed_uuid);
1389 retcode = ERR_DATA_NOT_CURRENT;
1390 goto force_diskless_dec;
1391 }
1392
1393 /* Since we are diskless, fix the activity log first... */
Lars Ellenbergf3990022011-03-23 14:31:09 +01001394 if (drbd_check_al_size(mdev, &nbc->dc)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001395 retcode = ERR_NOMEM;
1396 goto force_diskless_dec;
1397 }
1398
1399 /* Prevent shrinking of consistent devices ! */
1400 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
Philipp Reisnera393db62009-12-22 13:35:52 +01001401 drbd_new_dev_size(mdev, nbc, 0) < nbc->md.la_size_sect) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001402 dev_warn(DEV, "refusing to truncate a consistent device\n");
1403 retcode = ERR_DISK_TO_SMALL;
1404 goto force_diskless_dec;
1405 }
1406
1407 if (!drbd_al_read_log(mdev, nbc)) {
1408 retcode = ERR_IO_MD_DISK;
1409 goto force_diskless_dec;
1410 }
1411
Philipp Reisnerb411b362009-09-25 16:07:19 -07001412 /* Reset the "barriers don't work" bits here, then force meta data to
1413 * be written, to ensure we determine if barriers are supported. */
1414 if (nbc->dc.no_md_flush)
Philipp Reisnera8a4e512010-08-25 10:21:04 +02001415 set_bit(MD_NO_FUA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001416 else
Philipp Reisnera8a4e512010-08-25 10:21:04 +02001417 clear_bit(MD_NO_FUA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001418
1419 /* Point of no return reached.
1420 * Devices and memory are no longer released by error cleanup below.
1421 * now mdev takes over responsibility, and the state engine should
1422 * clean it up somewhere. */
1423 D_ASSERT(mdev->ldev == NULL);
1424 mdev->ldev = nbc;
1425 mdev->resync = resync_lru;
1426 nbc = NULL;
1427 resync_lru = NULL;
1428
Philipp Reisner2451fc32010-08-24 13:43:11 +02001429 mdev->write_ordering = WO_bdev_flush;
1430 drbd_bump_write_ordering(mdev, WO_bdev_flush);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001431
1432 if (drbd_md_test_flag(mdev->ldev, MDF_CRASHED_PRIMARY))
1433 set_bit(CRASHED_PRIMARY, &mdev->flags);
1434 else
1435 clear_bit(CRASHED_PRIMARY, &mdev->flags);
1436
Philipp Reisner894c6a92010-06-18 16:03:20 +02001437 if (drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
Philipp Reisnerda9fbc22011-03-29 10:52:01 +02001438 !(mdev->state.role == R_PRIMARY && mdev->tconn->susp_nod)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001439 set_bit(CRASHED_PRIMARY, &mdev->flags);
1440 cp_discovered = 1;
1441 }
1442
1443 mdev->send_cnt = 0;
1444 mdev->recv_cnt = 0;
1445 mdev->read_cnt = 0;
1446 mdev->writ_cnt = 0;
1447
Philipp Reisner99432fc2011-05-20 16:39:13 +02001448 drbd_reconsider_max_bio_size(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001449
1450 /* If I am currently not R_PRIMARY,
1451 * but meta data primary indicator is set,
1452 * I just now recover from a hard crash,
1453 * and have been R_PRIMARY before that crash.
1454 *
1455 * Now, if I had no connection before that crash
1456 * (have been degraded R_PRIMARY), chances are that
1457 * I won't find my peer now either.
1458 *
1459 * In that case, and _only_ in that case,
1460 * we use the degr-wfc-timeout instead of the default,
1461 * so we can automatically recover from a crash of a
1462 * degraded but active "cluster" after a certain timeout.
1463 */
1464 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
1465 if (mdev->state.role != R_PRIMARY &&
1466 drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
1467 !drbd_md_test_flag(mdev->ldev, MDF_CONNECTED_IND))
1468 set_bit(USE_DEGR_WFC_T, &mdev->flags);
1469
Bart Van Assche24c48302011-05-21 18:32:29 +02001470 dd = drbd_determine_dev_size(mdev, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001471 if (dd == dev_size_error) {
1472 retcode = ERR_NOMEM_BITMAP;
1473 goto force_diskless_dec;
1474 } else if (dd == grew)
1475 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
1476
1477 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
1478 dev_info(DEV, "Assuming that all blocks are out of sync "
1479 "(aka FullSync)\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001480 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write,
1481 "set_n_write from attaching", BM_LOCKED_MASK)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001482 retcode = ERR_IO_MD_DISK;
1483 goto force_diskless_dec;
1484 }
1485 } else {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001486 if (drbd_bitmap_io(mdev, &drbd_bm_read,
Andreas Gruenbacher22ab6a32010-12-13 01:44:11 +01001487 "read from attaching", BM_LOCKED_MASK)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001488 retcode = ERR_IO_MD_DISK;
1489 goto force_diskless_dec;
1490 }
1491 }
1492
1493 if (cp_discovered) {
1494 drbd_al_apply_to_bm(mdev);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001495 if (drbd_bitmap_io(mdev, &drbd_bm_write,
1496 "crashed primary apply AL", BM_LOCKED_MASK)) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001497 retcode = ERR_IO_MD_DISK;
1498 goto force_diskless_dec;
1499 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001500 }
1501
Philipp Reisner07782862010-08-31 12:00:50 +02001502 if (_drbd_bm_total_weight(mdev) == drbd_bm_bits(mdev))
1503 drbd_suspend_al(mdev); /* IO is still suspended here... */
1504
Philipp Reisner87eeee42011-01-19 14:16:30 +01001505 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner78bae592011-03-28 15:40:12 +02001506 os = drbd_read_state(mdev);
1507 ns = os;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001508 /* If MDF_CONSISTENT is not set go into inconsistent state,
1509 otherwise investigate MDF_WasUpToDate...
1510 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1511 otherwise into D_CONSISTENT state.
1512 */
1513 if (drbd_md_test_flag(mdev->ldev, MDF_CONSISTENT)) {
1514 if (drbd_md_test_flag(mdev->ldev, MDF_WAS_UP_TO_DATE))
1515 ns.disk = D_CONSISTENT;
1516 else
1517 ns.disk = D_OUTDATED;
1518 } else {
1519 ns.disk = D_INCONSISTENT;
1520 }
1521
1522 if (drbd_md_test_flag(mdev->ldev, MDF_PEER_OUT_DATED))
1523 ns.pdsk = D_OUTDATED;
1524
1525 if ( ns.disk == D_CONSISTENT &&
1526 (ns.pdsk == D_OUTDATED || mdev->ldev->dc.fencing == FP_DONT_CARE))
1527 ns.disk = D_UP_TO_DATE;
1528
1529 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1530 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1531 this point, because drbd_request_state() modifies these
1532 flags. */
1533
1534 /* In case we are C_CONNECTED postpone any decision on the new disk
1535 state after the negotiation phase. */
1536 if (mdev->state.conn == C_CONNECTED) {
1537 mdev->new_state_tmp.i = ns.i;
1538 ns.i = os.i;
1539 ns.disk = D_NEGOTIATING;
Philipp Reisnerdc66c742010-06-02 14:31:29 +02001540
1541 /* We expect to receive up-to-date UUIDs soon.
1542 To avoid a race in receive_state, free p_uuid while
1543 holding req_lock. I.e. atomic with the state change */
1544 kfree(mdev->p_uuid);
1545 mdev->p_uuid = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001546 }
1547
1548 rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001549 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001550
1551 if (rv < SS_SUCCESS)
1552 goto force_diskless_dec;
1553
1554 if (mdev->state.role == R_PRIMARY)
1555 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
1556 else
1557 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
1558
1559 drbd_md_mark_dirty(mdev);
1560 drbd_md_sync(mdev);
1561
1562 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1563 put_ldev(mdev);
Philipp Reisner0e29d162011-02-18 14:23:11 +01001564 conn_reconfig_done(mdev->tconn);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001565 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001566 return 0;
1567
1568 force_diskless_dec:
1569 put_ldev(mdev);
1570 force_diskless:
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001571 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001572 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001573 fail:
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001574 conn_reconfig_done(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001575 if (nbc) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001576 if (nbc->backing_bdev)
1577 blkdev_put(nbc->backing_bdev,
1578 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1579 if (nbc->md_bdev)
1580 blkdev_put(nbc->md_bdev,
1581 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001582 kfree(nbc);
1583 }
1584 lc_destroy(resync_lru);
1585
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001586 finish:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001587 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001588 return 0;
1589}
1590
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001591static int adm_detach(struct drbd_conf *mdev)
1592{
Philipp Reisner19f83c72011-03-29 14:21:03 +02001593 enum drbd_state_rv retcode;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001594 drbd_suspend_io(mdev); /* so no-one is stuck in drbd_al_begin_io */
1595 retcode = drbd_request_state(mdev, NS(disk, D_DISKLESS));
1596 wait_event(mdev->misc_wait,
1597 mdev->state.disk != D_DISKLESS ||
1598 !atomic_read(&mdev->local_cnt));
1599 drbd_resume_io(mdev);
1600 return retcode;
1601}
1602
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001603/* Detaching the disk is a process in multiple stages. First we need to lock
1604 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1605 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1606 * internal references as well.
1607 * Only then we have finally detached. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001608int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001609{
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001610 enum drbd_ret_code retcode;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001611
1612 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1613 if (!adm_ctx.reply_skb)
1614 return retcode;
1615 if (retcode != NO_ERROR)
1616 goto out;
1617
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001618 retcode = adm_detach(adm_ctx.mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001619out:
1620 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001621 return 0;
1622}
1623
Lars Ellenbergf3990022011-03-23 14:31:09 +01001624static bool conn_resync_running(struct drbd_tconn *tconn)
1625{
1626 struct drbd_conf *mdev;
1627 int vnr;
1628
1629 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1630 if (mdev->state.conn == C_SYNC_SOURCE ||
1631 mdev->state.conn == C_SYNC_TARGET ||
1632 mdev->state.conn == C_PAUSED_SYNC_S ||
1633 mdev->state.conn == C_PAUSED_SYNC_T)
1634 return true;
1635 }
1636 return false;
1637}
1638
1639static bool conn_ov_running(struct drbd_tconn *tconn)
1640{
1641 struct drbd_conf *mdev;
1642 int vnr;
1643
1644 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1645 if (mdev->state.conn == C_VERIFY_S ||
1646 mdev->state.conn == C_VERIFY_T)
1647 return true;
1648 }
1649 return false;
1650}
1651
1652int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
1653{
1654 enum drbd_ret_code retcode;
1655 struct drbd_tconn *tconn;
1656 struct net_conf *new_conf = NULL;
1657 int err;
1658 int ovr; /* online verify running */
1659 int rsr; /* re-sync running */
1660 struct crypto_hash *verify_tfm = NULL;
1661 struct crypto_hash *csums_tfm = NULL;
1662
1663
1664 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
1665 if (!adm_ctx.reply_skb)
1666 return retcode;
1667 if (retcode != NO_ERROR)
1668 goto out;
1669
1670 tconn = adm_ctx.tconn;
1671
1672 new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
1673 if (!new_conf) {
1674 retcode = ERR_NOMEM;
1675 goto out;
1676 }
1677
1678 /* we also need a net config
1679 * to change the options on */
1680 if (!get_net_conf(tconn)) {
1681 drbd_msg_put_info("net conf missing, try connect");
1682 retcode = ERR_INVALID_REQUEST;
1683 goto out;
1684 }
1685
1686 conn_reconfig_start(tconn);
1687
1688 memcpy(new_conf, tconn->net_conf, sizeof(*new_conf));
1689 err = net_conf_from_attrs_for_change(new_conf, info);
1690 if (err) {
1691 retcode = ERR_MANDATORY_TAG;
1692 drbd_msg_put_info(from_attrs_err_to_txt(err));
1693 goto fail;
1694 }
1695
1696 /* re-sync running */
1697 rsr = conn_resync_running(tconn);
1698 if (rsr && strcmp(new_conf->csums_alg, tconn->net_conf->csums_alg)) {
1699 retcode = ERR_CSUMS_RESYNC_RUNNING;
1700 goto fail;
1701 }
1702
1703 if (!rsr && new_conf->csums_alg[0]) {
1704 csums_tfm = crypto_alloc_hash(new_conf->csums_alg, 0, CRYPTO_ALG_ASYNC);
1705 if (IS_ERR(csums_tfm)) {
1706 csums_tfm = NULL;
1707 retcode = ERR_CSUMS_ALG;
1708 goto fail;
1709 }
1710
1711 if (!drbd_crypto_is_hash(crypto_hash_tfm(csums_tfm))) {
1712 retcode = ERR_CSUMS_ALG_ND;
1713 goto fail;
1714 }
1715 }
1716
1717 /* online verify running */
1718 ovr = conn_ov_running(tconn);
1719 if (ovr) {
1720 if (strcmp(new_conf->verify_alg, tconn->net_conf->verify_alg)) {
1721 retcode = ERR_VERIFY_RUNNING;
1722 goto fail;
1723 }
1724 }
1725
1726 if (!ovr && new_conf->verify_alg[0]) {
1727 verify_tfm = crypto_alloc_hash(new_conf->verify_alg, 0, CRYPTO_ALG_ASYNC);
1728 if (IS_ERR(verify_tfm)) {
1729 verify_tfm = NULL;
1730 retcode = ERR_VERIFY_ALG;
1731 goto fail;
1732 }
1733
1734 if (!drbd_crypto_is_hash(crypto_hash_tfm(verify_tfm))) {
1735 retcode = ERR_VERIFY_ALG_ND;
1736 goto fail;
1737 }
1738 }
1739
1740
1741 /* For now, use struct assignment, not pointer assignment.
1742 * We don't have any means to determine who might still
1743 * keep a local alias into the struct,
1744 * so we cannot just free it and hope for the best :(
1745 * FIXME
1746 * To avoid someone looking at a half-updated struct, we probably
1747 * should have a rw-semaphor on net_conf and disk_conf.
1748 */
1749 *tconn->net_conf = *new_conf;
1750
1751 if (!rsr) {
1752 crypto_free_hash(tconn->csums_tfm);
1753 tconn->csums_tfm = csums_tfm;
1754 csums_tfm = NULL;
1755 }
1756 if (!ovr) {
1757 crypto_free_hash(tconn->verify_tfm);
1758 tconn->verify_tfm = verify_tfm;
1759 verify_tfm = NULL;
1760 }
1761
1762 if (tconn->cstate >= C_WF_REPORT_PARAMS)
1763 drbd_send_sync_param(minor_to_mdev(conn_lowest_minor(tconn)));
1764
1765 fail:
1766 crypto_free_hash(csums_tfm);
1767 crypto_free_hash(verify_tfm);
1768 kfree(new_conf);
1769 put_net_conf(tconn);
1770 conn_reconfig_done(tconn);
1771 out:
1772 drbd_adm_finish(info, retcode);
1773 return 0;
1774}
1775
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001776int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001777{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001778 char hmac_name[CRYPTO_MAX_ALG_NAME];
1779 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001780 struct net_conf *new_conf = NULL;
1781 struct crypto_hash *tfm = NULL;
1782 struct crypto_hash *integrity_w_tfm = NULL;
1783 struct crypto_hash *integrity_r_tfm = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001784 void *int_dig_in = NULL;
1785 void *int_dig_vv = NULL;
Philipp Reisner80883192011-02-18 14:56:45 +01001786 struct drbd_tconn *oconn;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001787 struct drbd_tconn *tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001788 struct sockaddr *new_my_addr, *new_peer_addr, *taken_addr;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001789 enum drbd_ret_code retcode;
1790 int i;
1791 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001792
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001793 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
1794 if (!adm_ctx.reply_skb)
1795 return retcode;
1796 if (retcode != NO_ERROR)
1797 goto out;
1798
1799 tconn = adm_ctx.tconn;
Philipp Reisner80883192011-02-18 14:56:45 +01001800 conn_reconfig_start(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001801
Philipp Reisner80883192011-02-18 14:56:45 +01001802 if (tconn->cstate > C_STANDALONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001803 retcode = ERR_NET_CONFIGURED;
1804 goto fail;
1805 }
1806
1807 /* allocation not in the IO path, cqueue thread context */
Lars Ellenbergf3990022011-03-23 14:31:09 +01001808 new_conf = kmalloc(sizeof(struct net_conf), GFP_KERNEL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001809 if (!new_conf) {
1810 retcode = ERR_NOMEM;
1811 goto fail;
1812 }
1813
Lars Ellenbergf3990022011-03-23 14:31:09 +01001814 *new_conf = (struct net_conf) {
1815 {}, 0, /* my_addr */
1816 {}, 0, /* peer_addr */
1817 {}, 0, /* shared_secret */
1818 {}, 0, /* cram_hmac_alg */
1819 {}, 0, /* integrity_alg */
1820 {}, 0, /* verify_alg */
1821 {}, 0, /* csums_alg */
1822 DRBD_PROTOCOL_DEF, /* wire_protocol */
1823 DRBD_CONNECT_INT_DEF, /* try_connect_int */
1824 DRBD_TIMEOUT_DEF, /* timeout */
1825 DRBD_PING_INT_DEF, /* ping_int */
1826 DRBD_PING_TIMEO_DEF, /* ping_timeo */
1827 DRBD_SNDBUF_SIZE_DEF, /* sndbuf_size */
1828 DRBD_RCVBUF_SIZE_DEF, /* rcvbuf_size */
1829 DRBD_KO_COUNT_DEF, /* ko_count */
1830 DRBD_MAX_BUFFERS_DEF, /* max_buffers */
1831 DRBD_MAX_EPOCH_SIZE_DEF, /* max_epoch_size */
1832 DRBD_UNPLUG_WATERMARK_DEF, /* unplug_watermark */
1833 DRBD_AFTER_SB_0P_DEF, /* after_sb_0p */
1834 DRBD_AFTER_SB_1P_DEF, /* after_sb_1p */
1835 DRBD_AFTER_SB_2P_DEF, /* after_sb_2p */
1836 DRBD_RR_CONFLICT_DEF, /* rr_conflict */
1837 DRBD_ON_CONGESTION_DEF, /* on_congestion */
1838 DRBD_CONG_FILL_DEF, /* cong_fill */
1839 DRBD_CONG_EXTENTS_DEF, /* cong_extents */
1840 0, /* two_primaries */
1841 0, /* want_lose */
1842 0, /* no_cork */
1843 0, /* always_asbp */
1844 0, /* dry_run */
1845 0, /* use_rle */
1846 };
Philipp Reisnerb411b362009-09-25 16:07:19 -07001847
Lars Ellenbergf3990022011-03-23 14:31:09 +01001848 err = net_conf_from_attrs(new_conf, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001849 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001850 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001851 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001852 goto fail;
1853 }
1854
1855 if (new_conf->two_primaries
1856 && (new_conf->wire_protocol != DRBD_PROT_C)) {
1857 retcode = ERR_NOT_PROTO_C;
1858 goto fail;
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001859 }
1860
Philipp Reisner80883192011-02-18 14:56:45 +01001861 idr_for_each_entry(&tconn->volumes, mdev, i) {
1862 if (get_ldev(mdev)) {
1863 enum drbd_fencing_p fp = mdev->ldev->dc.fencing;
1864 put_ldev(mdev);
1865 if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH) {
1866 retcode = ERR_STONITH_AND_PROT_A;
1867 goto fail;
1868 }
1869 }
1870 if (mdev->state.role == R_PRIMARY && new_conf->want_lose) {
1871 retcode = ERR_DISCARD;
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001872 goto fail;
1873 }
Philipp Reisner80883192011-02-18 14:56:45 +01001874 if (!mdev->bitmap) {
1875 if(drbd_bm_init(mdev)) {
1876 retcode = ERR_NOMEM;
1877 goto fail;
1878 }
1879 }
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001880 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001881
Philipp Reisner422028b2010-10-27 11:12:07 +02001882 if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A) {
1883 retcode = ERR_CONG_NOT_PROTO_A;
1884 goto fail;
1885 }
1886
Philipp Reisnerb411b362009-09-25 16:07:19 -07001887 retcode = NO_ERROR;
1888
1889 new_my_addr = (struct sockaddr *)&new_conf->my_addr;
1890 new_peer_addr = (struct sockaddr *)&new_conf->peer_addr;
Lars Ellenberg543cc102011-03-10 22:18:18 +01001891
1892 /* No need to take drbd_cfg_mutex here. All reconfiguration is
1893 * strictly serialized on genl_lock(). We are protected against
1894 * concurrent reconfiguration/addition/deletion */
Philipp Reisner80883192011-02-18 14:56:45 +01001895 list_for_each_entry(oconn, &drbd_tconns, all_tconn) {
1896 if (oconn == tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001897 continue;
Philipp Reisner80883192011-02-18 14:56:45 +01001898 if (get_net_conf(oconn)) {
1899 taken_addr = (struct sockaddr *)&oconn->net_conf->my_addr;
1900 if (new_conf->my_addr_len == oconn->net_conf->my_addr_len &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07001901 !memcmp(new_my_addr, taken_addr, new_conf->my_addr_len))
1902 retcode = ERR_LOCAL_ADDR;
1903
Philipp Reisner80883192011-02-18 14:56:45 +01001904 taken_addr = (struct sockaddr *)&oconn->net_conf->peer_addr;
1905 if (new_conf->peer_addr_len == oconn->net_conf->peer_addr_len &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07001906 !memcmp(new_peer_addr, taken_addr, new_conf->peer_addr_len))
1907 retcode = ERR_PEER_ADDR;
1908
Philipp Reisner80883192011-02-18 14:56:45 +01001909 put_net_conf(oconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001910 if (retcode != NO_ERROR)
1911 goto fail;
1912 }
1913 }
1914
1915 if (new_conf->cram_hmac_alg[0] != 0) {
1916 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
1917 new_conf->cram_hmac_alg);
1918 tfm = crypto_alloc_hash(hmac_name, 0, CRYPTO_ALG_ASYNC);
1919 if (IS_ERR(tfm)) {
1920 tfm = NULL;
1921 retcode = ERR_AUTH_ALG;
1922 goto fail;
1923 }
1924
Philipp Reisner07982192009-12-28 16:58:38 +01001925 if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001926 retcode = ERR_AUTH_ALG_ND;
1927 goto fail;
1928 }
1929 }
1930
1931 if (new_conf->integrity_alg[0]) {
1932 integrity_w_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC);
1933 if (IS_ERR(integrity_w_tfm)) {
1934 integrity_w_tfm = NULL;
1935 retcode=ERR_INTEGRITY_ALG;
1936 goto fail;
1937 }
1938
1939 if (!drbd_crypto_is_hash(crypto_hash_tfm(integrity_w_tfm))) {
1940 retcode=ERR_INTEGRITY_ALG_ND;
1941 goto fail;
1942 }
1943
1944 integrity_r_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC);
1945 if (IS_ERR(integrity_r_tfm)) {
1946 integrity_r_tfm = NULL;
1947 retcode=ERR_INTEGRITY_ALG;
1948 goto fail;
1949 }
1950 }
1951
Philipp Reisnerb411b362009-09-25 16:07:19 -07001952 ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
1953
Philipp Reisner80883192011-02-18 14:56:45 +01001954 /* allocation not in the IO path, cqueue thread context */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001955 if (integrity_w_tfm) {
1956 i = crypto_hash_digestsize(integrity_w_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001957 int_dig_in = kmalloc(i, GFP_KERNEL);
1958 if (!int_dig_in) {
1959 retcode = ERR_NOMEM;
1960 goto fail;
1961 }
1962 int_dig_vv = kmalloc(i, GFP_KERNEL);
1963 if (!int_dig_vv) {
1964 retcode = ERR_NOMEM;
1965 goto fail;
1966 }
1967 }
1968
Philipp Reisner80883192011-02-18 14:56:45 +01001969 conn_flush_workqueue(tconn);
1970 spin_lock_irq(&tconn->req_lock);
1971 if (tconn->net_conf != NULL) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001972 retcode = ERR_NET_CONFIGURED;
Philipp Reisner80883192011-02-18 14:56:45 +01001973 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001974 goto fail;
1975 }
Philipp Reisner80883192011-02-18 14:56:45 +01001976 tconn->net_conf = new_conf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001977
Philipp Reisner80883192011-02-18 14:56:45 +01001978 crypto_free_hash(tconn->cram_hmac_tfm);
1979 tconn->cram_hmac_tfm = tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001980
Philipp Reisner80883192011-02-18 14:56:45 +01001981 crypto_free_hash(tconn->integrity_w_tfm);
1982 tconn->integrity_w_tfm = integrity_w_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001983
Philipp Reisner80883192011-02-18 14:56:45 +01001984 crypto_free_hash(tconn->integrity_r_tfm);
1985 tconn->integrity_r_tfm = integrity_r_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001986
Philipp Reisner80883192011-02-18 14:56:45 +01001987 kfree(tconn->int_dig_in);
1988 kfree(tconn->int_dig_vv);
Philipp Reisner80883192011-02-18 14:56:45 +01001989 tconn->int_dig_in=int_dig_in;
1990 tconn->int_dig_vv=int_dig_vv;
1991 retcode = _conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
1992 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001993
Philipp Reisner80883192011-02-18 14:56:45 +01001994 idr_for_each_entry(&tconn->volumes, mdev, i) {
1995 mdev->send_cnt = 0;
1996 mdev->recv_cnt = 0;
1997 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1998 }
Philipp Reisner80883192011-02-18 14:56:45 +01001999 conn_reconfig_done(tconn);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002000 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002001 return 0;
2002
2003fail:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002004 kfree(int_dig_in);
2005 kfree(int_dig_vv);
2006 crypto_free_hash(tfm);
2007 crypto_free_hash(integrity_w_tfm);
2008 crypto_free_hash(integrity_r_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002009 kfree(new_conf);
2010
Philipp Reisner80883192011-02-18 14:56:45 +01002011 conn_reconfig_done(tconn);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002012out:
2013 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002014 return 0;
2015}
2016
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002017static enum drbd_state_rv conn_try_disconnect(struct drbd_tconn *tconn, bool force)
2018{
2019 enum drbd_state_rv rv;
2020 if (force) {
2021 spin_lock_irq(&tconn->req_lock);
2022 if (tconn->cstate >= C_WF_CONNECTION)
2023 _conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
2024 spin_unlock_irq(&tconn->req_lock);
2025 return SS_SUCCESS;
2026 }
2027
2028 rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING), 0);
2029
2030 switch (rv) {
2031 case SS_NOTHING_TO_DO:
2032 case SS_ALREADY_STANDALONE:
2033 return SS_SUCCESS;
2034 case SS_PRIMARY_NOP:
2035 /* Our state checking code wants to see the peer outdated. */
2036 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
2037 pdsk, D_OUTDATED), CS_VERBOSE);
2038 break;
2039 case SS_CW_FAILED_BY_PEER:
2040 /* The peer probably wants to see us outdated. */
2041 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
2042 disk, D_OUTDATED), 0);
2043 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
2044 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
2045 rv = SS_SUCCESS;
2046 }
2047 break;
2048 default:;
2049 /* no special handling necessary */
2050 }
2051
2052 return rv;
2053}
2054
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002055int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002056{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002057 struct disconnect_parms parms;
2058 struct drbd_tconn *tconn;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002059 enum drbd_state_rv rv;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002060 enum drbd_ret_code retcode;
2061 int err;
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002062
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002063 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
2064 if (!adm_ctx.reply_skb)
2065 return retcode;
2066 if (retcode != NO_ERROR)
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002067 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002068
2069 tconn = adm_ctx.tconn;
2070 memset(&parms, 0, sizeof(parms));
2071 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002072 err = disconnect_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002073 if (err) {
2074 retcode = ERR_MANDATORY_TAG;
2075 drbd_msg_put_info(from_attrs_err_to_txt(err));
2076 goto fail;
2077 }
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002078 }
2079
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002080 rv = conn_try_disconnect(tconn, parms.force_disconnect);
2081 if (rv < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002082 goto fail;
2083
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01002084 if (wait_event_interruptible(tconn->ping_wait,
2085 tconn->cstate != C_DISCONNECTING)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002086 /* Do not test for mdev->state.conn == C_STANDALONE, since
2087 someone else might connect us in the mean time! */
2088 retcode = ERR_INTR;
2089 goto fail;
2090 }
2091
Philipp Reisnerb411b362009-09-25 16:07:19 -07002092 retcode = NO_ERROR;
2093 fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002094 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002095 return 0;
2096}
2097
2098void resync_after_online_grow(struct drbd_conf *mdev)
2099{
2100 int iass; /* I am sync source */
2101
2102 dev_info(DEV, "Resync of new storage after online grow\n");
2103 if (mdev->state.role != mdev->state.peer)
2104 iass = (mdev->state.role == R_PRIMARY);
2105 else
Philipp Reisner25703f82011-02-07 14:35:25 +01002106 iass = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002107
2108 if (iass)
2109 drbd_start_resync(mdev, C_SYNC_SOURCE);
2110 else
2111 _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
2112}
2113
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002114int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002115{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002116 struct resize_parms rs;
2117 struct drbd_conf *mdev;
2118 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002119 enum determine_dev_size dd;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002120 enum dds_flags ddsf;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002121 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002122
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002123 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2124 if (!adm_ctx.reply_skb)
2125 return retcode;
2126 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002127 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002128
2129 memset(&rs, 0, sizeof(struct resize_parms));
2130 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002131 err = resize_parms_from_attrs(&rs, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002132 if (err) {
2133 retcode = ERR_MANDATORY_TAG;
2134 drbd_msg_put_info(from_attrs_err_to_txt(err));
2135 goto fail;
2136 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002137 }
2138
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002139 mdev = adm_ctx.mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002140 if (mdev->state.conn > C_CONNECTED) {
2141 retcode = ERR_RESIZE_RESYNC;
2142 goto fail;
2143 }
2144
2145 if (mdev->state.role == R_SECONDARY &&
2146 mdev->state.peer == R_SECONDARY) {
2147 retcode = ERR_NO_PRIMARY;
2148 goto fail;
2149 }
2150
2151 if (!get_ldev(mdev)) {
2152 retcode = ERR_NO_DISK;
2153 goto fail;
2154 }
2155
Philipp Reisner31890f42011-01-19 14:12:51 +01002156 if (rs.no_resync && mdev->tconn->agreed_pro_version < 93) {
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002157 retcode = ERR_NEED_APV_93;
2158 goto fail;
2159 }
2160
Philipp Reisner087c2492010-03-26 13:49:56 +01002161 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002162 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002163
2164 mdev->ldev->dc.disk_size = (sector_t)rs.resize_size;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002165 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
Bart Van Assche24c48302011-05-21 18:32:29 +02002166 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002167 drbd_md_sync(mdev);
2168 put_ldev(mdev);
2169 if (dd == dev_size_error) {
2170 retcode = ERR_NOMEM_BITMAP;
2171 goto fail;
2172 }
2173
Philipp Reisner087c2492010-03-26 13:49:56 +01002174 if (mdev->state.conn == C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002175 if (dd == grew)
2176 set_bit(RESIZE_PENDING, &mdev->flags);
2177
2178 drbd_send_uuids(mdev);
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002179 drbd_send_sizes(mdev, 1, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002180 }
2181
2182 fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002183 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002184 return 0;
2185}
2186
Lars Ellenbergf3990022011-03-23 14:31:09 +01002187int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002188{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002189 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002190 cpumask_var_t new_cpu_mask;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002191 struct drbd_tconn *tconn;
Philipp Reisner778f2712010-07-06 11:14:00 +02002192 int *rs_plan_s = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002193 struct res_opts sc;
2194 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002195
Lars Ellenbergf3990022011-03-23 14:31:09 +01002196 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002197 if (!adm_ctx.reply_skb)
2198 return retcode;
2199 if (retcode != NO_ERROR)
2200 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002201 tconn = adm_ctx.tconn;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002202
Philipp Reisnerb411b362009-09-25 16:07:19 -07002203 if (!zalloc_cpumask_var(&new_cpu_mask, GFP_KERNEL)) {
2204 retcode = ERR_NOMEM;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002205 drbd_msg_put_info("unable to allocate cpumask");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002206 goto fail;
2207 }
2208
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002209 if (((struct drbd_genlmsghdr*)info->userhdr)->flags
2210 & DRBD_GENL_F_SET_DEFAULTS) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002211 memset(&sc, 0, sizeof(struct res_opts));
Philipp Reisner265be2d2010-05-31 10:14:17 +02002212 sc.on_no_data = DRBD_ON_NO_DATA_DEF;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002213 } else
Lars Ellenbergf3990022011-03-23 14:31:09 +01002214 sc = tconn->res_opts;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002215
Lars Ellenbergf3990022011-03-23 14:31:09 +01002216 err = res_opts_from_attrs(&sc, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002217 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002218 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002219 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002220 goto fail;
2221 }
2222
Philipp Reisnerb411b362009-09-25 16:07:19 -07002223 /* silently ignore cpu mask on UP kernel */
2224 if (nr_cpu_ids > 1 && sc.cpu_mask[0] != 0) {
2225 err = __bitmap_parse(sc.cpu_mask, 32, 0,
2226 cpumask_bits(new_cpu_mask), nr_cpu_ids);
2227 if (err) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002228 conn_warn(tconn, "__bitmap_parse() failed with %d\n", err);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002229 retcode = ERR_CPU_MASK_PARSE;
2230 goto fail;
2231 }
2232 }
2233
Lars Ellenberg7ad651b2011-02-21 13:21:03 +01002234
Lars Ellenbergf3990022011-03-23 14:31:09 +01002235 tconn->res_opts = sc;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002236
Lars Ellenbergf3990022011-03-23 14:31:09 +01002237 if (!cpumask_equal(tconn->cpu_mask, new_cpu_mask)) {
2238 cpumask_copy(tconn->cpu_mask, new_cpu_mask);
2239 drbd_calc_cpu_mask(tconn);
2240 tconn->receiver.reset_cpu_mask = 1;
2241 tconn->asender.reset_cpu_mask = 1;
2242 tconn->worker.reset_cpu_mask = 1;
Philipp Reisner778f2712010-07-06 11:14:00 +02002243 }
2244
Philipp Reisnerb411b362009-09-25 16:07:19 -07002245fail:
Philipp Reisner778f2712010-07-06 11:14:00 +02002246 kfree(rs_plan_s);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002247 free_cpumask_var(new_cpu_mask);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002248
2249 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002250 return 0;
2251}
2252
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002253int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002254{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002255 struct drbd_conf *mdev;
2256 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2257
2258 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2259 if (!adm_ctx.reply_skb)
2260 return retcode;
2261 if (retcode != NO_ERROR)
2262 goto out;
2263
2264 mdev = adm_ctx.mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002265
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002266 /* If there is still bitmap IO pending, probably because of a previous
2267 * resync just being finished, wait for it before requesting a new resync. */
2268 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
2269
Philipp Reisnerb411b362009-09-25 16:07:19 -07002270 retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T), CS_ORDERED);
2271
2272 if (retcode < SS_SUCCESS && retcode != SS_NEED_CONNECTION)
2273 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
2274
2275 while (retcode == SS_NEED_CONNECTION) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01002276 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002277 if (mdev->state.conn < C_CONNECTED)
2278 retcode = _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_VERBOSE, NULL);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002279 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002280
2281 if (retcode != SS_NEED_CONNECTION)
2282 break;
2283
2284 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
2285 }
2286
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002287out:
2288 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002289 return 0;
2290}
2291
Philipp Reisner07782862010-08-31 12:00:50 +02002292static int drbd_bmio_set_susp_al(struct drbd_conf *mdev)
2293{
2294 int rv;
2295
2296 rv = drbd_bmio_set_n_write(mdev);
2297 drbd_suspend_al(mdev);
2298 return rv;
2299}
2300
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002301static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2302 union drbd_state mask, union drbd_state val)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002303{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002304 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002305
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002306 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2307 if (!adm_ctx.reply_skb)
2308 return retcode;
2309 if (retcode != NO_ERROR)
2310 goto out;
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002311
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002312 retcode = drbd_request_state(adm_ctx.mdev, mask, val);
2313out:
2314 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002315 return 0;
2316}
2317
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002318int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002319{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002320 return drbd_adm_simple_request_state(skb, info, NS(conn, C_STARTING_SYNC_S));
2321}
Philipp Reisnerb411b362009-09-25 16:07:19 -07002322
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002323int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
2324{
2325 enum drbd_ret_code retcode;
2326
2327 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2328 if (!adm_ctx.reply_skb)
2329 return retcode;
2330 if (retcode != NO_ERROR)
2331 goto out;
2332
2333 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002334 retcode = ERR_PAUSE_IS_SET;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002335out:
2336 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002337 return 0;
2338}
2339
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002340int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002341{
Philipp Reisnerda9fbc22011-03-29 10:52:01 +02002342 union drbd_dev_state s;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002343 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002344
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002345 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2346 if (!adm_ctx.reply_skb)
2347 return retcode;
2348 if (retcode != NO_ERROR)
2349 goto out;
2350
2351 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2352 s = adm_ctx.mdev->state;
Philipp Reisnercd88d032011-01-20 11:46:41 +01002353 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2354 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2355 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2356 } else {
2357 retcode = ERR_PAUSE_IS_CLEAR;
2358 }
2359 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002360
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002361out:
2362 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002363 return 0;
2364}
2365
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002366int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002367{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002368 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002369}
2370
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002371int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002372{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002373 struct drbd_conf *mdev;
2374 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2375
2376 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2377 if (!adm_ctx.reply_skb)
2378 return retcode;
2379 if (retcode != NO_ERROR)
2380 goto out;
2381
2382 mdev = adm_ctx.mdev;
Philipp Reisner43a51822010-06-11 11:26:34 +02002383 if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
2384 drbd_uuid_new_current(mdev);
2385 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner43a51822010-06-11 11:26:34 +02002386 }
Philipp Reisner265be2d2010-05-31 10:14:17 +02002387 drbd_suspend_io(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002388 retcode = drbd_request_state(mdev, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
2389 if (retcode == SS_SUCCESS) {
Philipp Reisner265be2d2010-05-31 10:14:17 +02002390 if (mdev->state.conn < C_CONNECTED)
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01002391 tl_clear(mdev->tconn);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002392 if (mdev->state.disk == D_DISKLESS || mdev->state.disk == D_FAILED)
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01002393 tl_restart(mdev->tconn, FAIL_FROZEN_DISK_IO);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002394 }
2395 drbd_resume_io(mdev);
2396
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002397out:
2398 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002399 return 0;
2400}
2401
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002402int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002403{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002404 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002405}
2406
Lars Ellenberg543cc102011-03-10 22:18:18 +01002407int nla_put_drbd_cfg_context(struct sk_buff *skb, const char *conn_name, unsigned vnr)
2408{
2409 struct nlattr *nla;
2410 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
2411 if (!nla)
2412 goto nla_put_failure;
2413 if (vnr != VOLUME_UNSPECIFIED)
2414 NLA_PUT_U32(skb, T_ctx_volume, vnr);
2415 NLA_PUT_STRING(skb, T_ctx_conn_name, conn_name);
2416 nla_nest_end(skb, nla);
2417 return 0;
2418
2419nla_put_failure:
2420 if (nla)
2421 nla_nest_cancel(skb, nla);
2422 return -EMSGSIZE;
2423}
2424
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002425int nla_put_status_info(struct sk_buff *skb, struct drbd_conf *mdev,
2426 const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002427{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002428 struct state_info *si = NULL; /* for sizeof(si->member); */
2429 struct nlattr *nla;
2430 int got_ldev;
2431 int got_net;
2432 int err = 0;
2433 int exclude_sensitive;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002434
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002435 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2436 * to. So we better exclude_sensitive information.
2437 *
2438 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2439 * in the context of the requesting user process. Exclude sensitive
2440 * information, unless current has superuser.
2441 *
2442 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2443 * relies on the current implementation of netlink_dump(), which
2444 * executes the dump callback successively from netlink_recvmsg(),
2445 * always in the context of the receiving process */
2446 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002447
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002448 got_ldev = get_ldev(mdev);
2449 got_net = get_net_conf(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002450
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002451 /* We need to add connection name and volume number information still.
2452 * Minor number is in drbd_genlmsghdr. */
Lars Ellenberg543cc102011-03-10 22:18:18 +01002453 if (nla_put_drbd_cfg_context(skb, mdev->tconn->name, mdev->vnr))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002454 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002455
Lars Ellenbergf3990022011-03-23 14:31:09 +01002456 if (res_opts_to_skb(skb, &mdev->tconn->res_opts, exclude_sensitive))
2457 goto nla_put_failure;
2458
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002459 if (got_ldev)
2460 if (disk_conf_to_skb(skb, &mdev->ldev->dc, exclude_sensitive))
2461 goto nla_put_failure;
2462 if (got_net)
2463 if (net_conf_to_skb(skb, mdev->tconn->net_conf, exclude_sensitive))
2464 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002465
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002466 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
2467 if (!nla)
2468 goto nla_put_failure;
2469 NLA_PUT_U32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY);
2470 NLA_PUT_U32(skb, T_current_state, mdev->state.i);
2471 NLA_PUT_U64(skb, T_ed_uuid, mdev->ed_uuid);
2472 NLA_PUT_U64(skb, T_capacity, drbd_get_capacity(mdev->this_bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002473
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002474 if (got_ldev) {
2475 NLA_PUT_U32(skb, T_disk_flags, mdev->ldev->md.flags);
2476 NLA_PUT(skb, T_uuids, sizeof(si->uuids), mdev->ldev->md.uuid);
2477 NLA_PUT_U64(skb, T_bits_total, drbd_bm_bits(mdev));
2478 NLA_PUT_U64(skb, T_bits_oos, drbd_bm_total_weight(mdev));
2479 if (C_SYNC_SOURCE <= mdev->state.conn &&
2480 C_PAUSED_SYNC_T >= mdev->state.conn) {
2481 NLA_PUT_U64(skb, T_bits_rs_total, mdev->rs_total);
2482 NLA_PUT_U64(skb, T_bits_rs_failed, mdev->rs_failed);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002483 }
2484 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002485
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002486 if (sib) {
2487 switch(sib->sib_reason) {
2488 case SIB_SYNC_PROGRESS:
2489 case SIB_GET_STATUS_REPLY:
2490 break;
2491 case SIB_STATE_CHANGE:
2492 NLA_PUT_U32(skb, T_prev_state, sib->os.i);
2493 NLA_PUT_U32(skb, T_new_state, sib->ns.i);
2494 break;
2495 case SIB_HELPER_POST:
2496 NLA_PUT_U32(skb,
2497 T_helper_exit_code, sib->helper_exit_code);
2498 /* fall through */
2499 case SIB_HELPER_PRE:
2500 NLA_PUT_STRING(skb, T_helper, sib->helper_name);
2501 break;
2502 }
2503 }
2504 nla_nest_end(skb, nla);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002505
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002506 if (0)
2507nla_put_failure:
2508 err = -EMSGSIZE;
2509 if (got_ldev)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002510 put_ldev(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002511 if (got_net)
2512 put_net_conf(mdev->tconn);
2513 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002514}
2515
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002516int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002517{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002518 enum drbd_ret_code retcode;
2519 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002520
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002521 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2522 if (!adm_ctx.reply_skb)
2523 return retcode;
2524 if (retcode != NO_ERROR)
2525 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002526
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002527 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.mdev, NULL);
2528 if (err) {
2529 nlmsg_free(adm_ctx.reply_skb);
2530 return err;
2531 }
2532out:
2533 drbd_adm_finish(info, retcode);
2534 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002535}
2536
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002537int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002538{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002539 struct drbd_conf *mdev;
2540 struct drbd_genlmsghdr *dh;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002541 struct drbd_tconn *pos = (struct drbd_tconn*)cb->args[0];
2542 struct drbd_tconn *tconn = NULL;
2543 struct drbd_tconn *tmp;
2544 unsigned volume = cb->args[1];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002545
Lars Ellenberg543cc102011-03-10 22:18:18 +01002546 /* Open coded, deferred, iteration:
2547 * list_for_each_entry_safe(tconn, tmp, &drbd_tconns, all_tconn) {
2548 * idr_for_each_entry(&tconn->volumes, mdev, i) {
2549 * ...
2550 * }
2551 * }
2552 * where tconn is cb->args[0];
2553 * and i is cb->args[1];
2554 *
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002555 * This may miss entries inserted after this dump started,
2556 * or entries deleted before they are reached.
Lars Ellenberg543cc102011-03-10 22:18:18 +01002557 *
2558 * We need to make sure the mdev won't disappear while
2559 * we are looking at it, and revalidate our iterators
2560 * on each iteration.
2561 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002562
Lars Ellenberg543cc102011-03-10 22:18:18 +01002563 /* synchronize with drbd_new_tconn/drbd_free_tconn */
2564 mutex_lock(&drbd_cfg_mutex);
2565 /* synchronize with drbd_delete_device */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002566 rcu_read_lock();
Lars Ellenberg543cc102011-03-10 22:18:18 +01002567next_tconn:
2568 /* revalidate iterator position */
2569 list_for_each_entry(tmp, &drbd_tconns, all_tconn) {
2570 if (pos == NULL) {
2571 /* first iteration */
2572 pos = tmp;
2573 tconn = pos;
2574 break;
2575 }
2576 if (tmp == pos) {
2577 tconn = pos;
2578 break;
2579 }
2580 }
2581 if (tconn) {
2582 mdev = idr_get_next(&tconn->volumes, &volume);
2583 if (!mdev) {
2584 /* No more volumes to dump on this tconn.
2585 * Advance tconn iterator. */
2586 pos = list_entry(tconn->all_tconn.next,
2587 struct drbd_tconn, all_tconn);
2588 /* But, did we dump any volume on this tconn yet? */
2589 if (volume != 0) {
2590 tconn = NULL;
2591 volume = 0;
2592 goto next_tconn;
2593 }
2594 }
2595
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002596 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).pid,
2597 cb->nlh->nlmsg_seq, &drbd_genl_family,
2598 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
2599 if (!dh)
Lars Ellenberg543cc102011-03-10 22:18:18 +01002600 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002601
Lars Ellenberg543cc102011-03-10 22:18:18 +01002602 if (!mdev) {
2603 /* this is a tconn without a single volume */
2604 dh->minor = -1U;
2605 dh->ret_code = NO_ERROR;
2606 if (nla_put_drbd_cfg_context(skb, tconn->name, VOLUME_UNSPECIFIED))
2607 genlmsg_cancel(skb, dh);
2608 else
2609 genlmsg_end(skb, dh);
2610 goto out;
2611 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002612
Lars Ellenberg543cc102011-03-10 22:18:18 +01002613 D_ASSERT(mdev->vnr == volume);
2614 D_ASSERT(mdev->tconn == tconn);
2615
2616 dh->minor = mdev_to_minor(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002617 dh->ret_code = NO_ERROR;
2618
2619 if (nla_put_status_info(skb, mdev, NULL)) {
2620 genlmsg_cancel(skb, dh);
Lars Ellenberg543cc102011-03-10 22:18:18 +01002621 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002622 }
2623 genlmsg_end(skb, dh);
2624 }
2625
Lars Ellenberg543cc102011-03-10 22:18:18 +01002626out:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002627 rcu_read_unlock();
Lars Ellenberg543cc102011-03-10 22:18:18 +01002628 mutex_unlock(&drbd_cfg_mutex);
2629 /* where to start the next iteration */
2630 cb->args[0] = (long)pos;
2631 cb->args[1] = (pos == tconn) ? volume + 1 : 0;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002632
Lars Ellenberg543cc102011-03-10 22:18:18 +01002633 /* No more tconns/volumes/minors found results in an empty skb.
2634 * Which will terminate the dump. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002635 return skb->len;
2636}
2637
2638int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
2639{
2640 enum drbd_ret_code retcode;
2641 struct timeout_parms tp;
2642 int err;
2643
2644 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2645 if (!adm_ctx.reply_skb)
2646 return retcode;
2647 if (retcode != NO_ERROR)
2648 goto out;
2649
2650 tp.timeout_type =
2651 adm_ctx.mdev->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
2652 test_bit(USE_DEGR_WFC_T, &adm_ctx.mdev->flags) ? UT_DEGRADED :
2653 UT_DEFAULT;
2654
2655 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
2656 if (err) {
2657 nlmsg_free(adm_ctx.reply_skb);
2658 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002659 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002660out:
2661 drbd_adm_finish(info, retcode);
2662 return 0;
2663}
Lars Ellenberg873b0d52011-01-21 22:53:48 +01002664
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002665int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
2666{
2667 struct drbd_conf *mdev;
2668 enum drbd_ret_code retcode;
2669
2670 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2671 if (!adm_ctx.reply_skb)
2672 return retcode;
2673 if (retcode != NO_ERROR)
2674 goto out;
2675
2676 mdev = adm_ctx.mdev;
2677 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
2678 /* resume from last known position, if possible */
2679 struct start_ov_parms parms =
2680 { .ov_start_sector = mdev->ov_start_sector };
Lars Ellenbergf3990022011-03-23 14:31:09 +01002681 int err = start_ov_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002682 if (err) {
2683 retcode = ERR_MANDATORY_TAG;
2684 drbd_msg_put_info(from_attrs_err_to_txt(err));
2685 goto out;
2686 }
2687 /* w_make_ov_request expects position to be aligned */
2688 mdev->ov_start_sector = parms.ov_start_sector & ~BM_SECT_PER_BIT;
2689 }
Lars Ellenberg873b0d52011-01-21 22:53:48 +01002690 /* If there is still bitmap IO pending, e.g. previous resync or verify
2691 * just being finished, wait for it before requesting a new resync. */
2692 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002693 retcode = drbd_request_state(mdev,NS(conn,C_VERIFY_S));
2694out:
2695 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002696 return 0;
2697}
2698
2699
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002700int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002701{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002702 struct drbd_conf *mdev;
2703 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002704 int skip_initial_sync = 0;
2705 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002706 struct new_c_uuid_parms args;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002707
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002708 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2709 if (!adm_ctx.reply_skb)
2710 return retcode;
2711 if (retcode != NO_ERROR)
2712 goto out_nolock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002713
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002714 mdev = adm_ctx.mdev;
2715 memset(&args, 0, sizeof(args));
2716 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002717 err = new_c_uuid_parms_from_attrs(&args, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002718 if (err) {
2719 retcode = ERR_MANDATORY_TAG;
2720 drbd_msg_put_info(from_attrs_err_to_txt(err));
2721 goto out_nolock;
2722 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002723 }
2724
Philipp Reisner8410da8f02011-02-11 20:11:10 +01002725 mutex_lock(mdev->state_mutex); /* Protects us against serialized state changes. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002726
2727 if (!get_ldev(mdev)) {
2728 retcode = ERR_NO_DISK;
2729 goto out;
2730 }
2731
2732 /* this is "skip initial sync", assume to be clean */
Philipp Reisner31890f42011-01-19 14:12:51 +01002733 if (mdev->state.conn == C_CONNECTED && mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07002734 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
2735 dev_info(DEV, "Preparing to skip initial sync\n");
2736 skip_initial_sync = 1;
2737 } else if (mdev->state.conn != C_STANDALONE) {
2738 retcode = ERR_CONNECTED;
2739 goto out_dec;
2740 }
2741
2742 drbd_uuid_set(mdev, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
2743 drbd_uuid_new_current(mdev); /* New current, previous to UI_BITMAP */
2744
2745 if (args.clear_bm) {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002746 err = drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
2747 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002748 if (err) {
2749 dev_err(DEV, "Writing bitmap failed with %d\n",err);
2750 retcode = ERR_IO_MD_DISK;
2751 }
2752 if (skip_initial_sync) {
2753 drbd_send_uuids_skip_initial_sync(mdev);
2754 _drbd_uuid_set(mdev, UI_BITMAP, 0);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002755 drbd_print_uuids(mdev, "cleared bitmap UUID");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002756 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002757 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
2758 CS_VERBOSE, NULL);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002759 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002760 }
2761 }
2762
2763 drbd_md_sync(mdev);
2764out_dec:
2765 put_ldev(mdev);
2766out:
Philipp Reisner8410da8f02011-02-11 20:11:10 +01002767 mutex_unlock(mdev->state_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002768out_nolock:
2769 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002770 return 0;
2771}
2772
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002773static enum drbd_ret_code
2774drbd_check_conn_name(const char *name)
Philipp Reisner774b3052011-02-22 02:07:03 -05002775{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002776 if (!name || !name[0]) {
2777 drbd_msg_put_info("connection name missing");
2778 return ERR_MANDATORY_TAG;
2779 }
2780 /* if we want to use these in sysfs/configfs/debugfs some day,
2781 * we must not allow slashes */
2782 if (strchr(name, '/')) {
2783 drbd_msg_put_info("invalid connection name");
2784 return ERR_INVALID_REQUEST;
2785 }
2786 return NO_ERROR;
2787}
Philipp Reisner774b3052011-02-22 02:07:03 -05002788
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002789int drbd_adm_create_connection(struct sk_buff *skb, struct genl_info *info)
2790{
2791 enum drbd_ret_code retcode;
2792
2793 retcode = drbd_adm_prepare(skb, info, 0);
2794 if (!adm_ctx.reply_skb)
2795 return retcode;
2796 if (retcode != NO_ERROR)
2797 goto out;
2798
2799 retcode = drbd_check_conn_name(adm_ctx.conn_name);
2800 if (retcode != NO_ERROR)
2801 goto out;
2802
2803 if (adm_ctx.tconn) {
Lars Ellenberg38f19612011-03-14 13:22:35 +01002804 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
2805 retcode = ERR_INVALID_REQUEST;
2806 drbd_msg_put_info("connection exists");
2807 }
2808 /* else: still NO_ERROR */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002809 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05002810 }
2811
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002812 if (!drbd_new_tconn(adm_ctx.conn_name))
2813 retcode = ERR_NOMEM;
2814out:
2815 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05002816 return 0;
2817}
2818
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002819int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05002820{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002821 struct drbd_genlmsghdr *dh = info->userhdr;
2822 enum drbd_ret_code retcode;
Philipp Reisner774b3052011-02-22 02:07:03 -05002823
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002824 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
2825 if (!adm_ctx.reply_skb)
2826 return retcode;
2827 if (retcode != NO_ERROR)
2828 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05002829
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002830 /* FIXME drop minor_count parameter, limit to MINORMASK */
2831 if (dh->minor >= minor_count) {
2832 drbd_msg_put_info("requested minor out of range");
2833 retcode = ERR_INVALID_REQUEST;
2834 goto out;
2835 }
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +02002836 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002837 drbd_msg_put_info("requested volume id out of range");
2838 retcode = ERR_INVALID_REQUEST;
2839 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05002840 }
2841
Lars Ellenberg38f19612011-03-14 13:22:35 +01002842 /* drbd_adm_prepare made sure already
2843 * that mdev->tconn and mdev->vnr match the request. */
2844 if (adm_ctx.mdev) {
2845 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
2846 retcode = ERR_MINOR_EXISTS;
2847 /* else: still NO_ERROR */
2848 goto out;
2849 }
2850
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002851 retcode = conn_new_minor(adm_ctx.tconn, dh->minor, adm_ctx.volume);
2852out:
2853 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05002854 return 0;
2855}
2856
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002857static enum drbd_ret_code adm_delete_minor(struct drbd_conf *mdev)
2858{
2859 if (mdev->state.disk == D_DISKLESS &&
2860 /* no need to be mdev->state.conn == C_STANDALONE &&
2861 * we may want to delete a minor from a live replication group.
2862 */
2863 mdev->state.role == R_SECONDARY) {
2864 drbd_delete_device(mdev_to_minor(mdev));
2865 return NO_ERROR;
2866 } else
2867 return ERR_MINOR_CONFIGURED;
2868}
2869
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002870int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05002871{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002872 enum drbd_ret_code retcode;
2873
2874 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2875 if (!adm_ctx.reply_skb)
2876 return retcode;
2877 if (retcode != NO_ERROR)
2878 goto out;
2879
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002880 mutex_lock(&drbd_cfg_mutex);
2881 retcode = adm_delete_minor(adm_ctx.mdev);
2882 mutex_unlock(&drbd_cfg_mutex);
2883 /* if this was the last volume of this connection,
2884 * this will terminate all threads */
2885 if (retcode == NO_ERROR)
Lars Ellenbergcffec5b2011-03-10 23:33:15 +01002886 conn_reconfig_done(adm_ctx.tconn);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002887out:
2888 drbd_adm_finish(info, retcode);
2889 return 0;
2890}
2891
2892int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
2893{
2894 enum drbd_ret_code retcode;
2895 enum drbd_state_rv rv;
2896 struct drbd_conf *mdev;
2897 unsigned i;
2898
2899 retcode = drbd_adm_prepare(skb, info, 0);
2900 if (!adm_ctx.reply_skb)
2901 return retcode;
2902 if (retcode != NO_ERROR)
2903 goto out;
2904
2905 if (!adm_ctx.tconn) {
2906 retcode = ERR_CONN_NOT_KNOWN;
2907 goto out;
2908 }
2909
2910 mutex_lock(&drbd_cfg_mutex);
2911 /* demote */
2912 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
2913 retcode = drbd_set_role(mdev, R_SECONDARY, 0);
2914 if (retcode < SS_SUCCESS) {
2915 drbd_msg_put_info("failed to demote");
2916 goto out_unlock;
2917 }
2918 }
2919
2920 /* disconnect */
2921 rv = conn_try_disconnect(adm_ctx.tconn, 0);
2922 if (rv < SS_SUCCESS) {
2923 retcode = rv; /* enum type mismatch! */
2924 drbd_msg_put_info("failed to disconnect");
2925 goto out_unlock;
2926 }
2927
2928 /* detach */
2929 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
2930 rv = adm_detach(mdev);
2931 if (rv < SS_SUCCESS) {
2932 retcode = rv; /* enum type mismatch! */
2933 drbd_msg_put_info("failed to detach");
2934 goto out_unlock;
2935 }
2936 }
2937
2938 /* delete volumes */
2939 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
2940 retcode = adm_delete_minor(mdev);
2941 if (retcode != NO_ERROR) {
2942 /* "can not happen" */
2943 drbd_msg_put_info("failed to delete volume");
2944 goto out_unlock;
2945 }
2946 }
2947
2948 /* stop all threads */
2949 conn_reconfig_done(adm_ctx.tconn);
2950
2951 /* delete connection */
2952 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
2953 drbd_free_tconn(adm_ctx.tconn);
2954 retcode = NO_ERROR;
2955 } else {
2956 /* "can not happen" */
2957 retcode = ERR_CONN_IN_USE;
2958 drbd_msg_put_info("failed to delete connection");
2959 goto out_unlock;
2960 }
2961out_unlock:
2962 mutex_unlock(&drbd_cfg_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002963out:
2964 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05002965 return 0;
2966}
2967
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002968int drbd_adm_delete_connection(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05002969{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002970 enum drbd_ret_code retcode;
2971
2972 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
2973 if (!adm_ctx.reply_skb)
2974 return retcode;
2975 if (retcode != NO_ERROR)
2976 goto out;
2977
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002978 mutex_lock(&drbd_cfg_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002979 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
2980 drbd_free_tconn(adm_ctx.tconn);
2981 retcode = NO_ERROR;
Philipp Reisner774b3052011-02-22 02:07:03 -05002982 } else {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002983 retcode = ERR_CONN_IN_USE;
Philipp Reisner774b3052011-02-22 02:07:03 -05002984 }
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002985 mutex_unlock(&drbd_cfg_mutex);
Philipp Reisner774b3052011-02-22 02:07:03 -05002986
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002987out:
2988 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05002989 return 0;
2990}
2991
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002992void drbd_bcast_event(struct drbd_conf *mdev, const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002993{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002994 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
2995 struct sk_buff *msg;
2996 struct drbd_genlmsghdr *d_out;
2997 unsigned seq;
2998 int err = -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002999
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003000 seq = atomic_inc_return(&drbd_genl_seq);
3001 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
3002 if (!msg)
3003 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003004
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003005 err = -EMSGSIZE;
3006 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
3007 if (!d_out) /* cannot happen, but anyways. */
3008 goto nla_put_failure;
3009 d_out->minor = mdev_to_minor(mdev);
3010 d_out->ret_code = 0;
Philipp Reisner9f5180e2009-10-06 09:30:14 +02003011
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003012 if (nla_put_status_info(msg, mdev, sib))
3013 goto nla_put_failure;
3014 genlmsg_end(msg, d_out);
3015 err = drbd_genl_multicast_events(msg, 0);
3016 /* msg has been consumed or freed in netlink_broadcast() */
3017 if (err && err != -ESRCH)
3018 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003019
Philipp Reisnerb411b362009-09-25 16:07:19 -07003020 return;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003021
3022nla_put_failure:
3023 nlmsg_free(msg);
3024failed:
3025 dev_err(DEV, "Error %d while broadcasting event. "
3026 "Event seq:%u sib_reason:%u\n",
3027 err, seq, sib->sib_reason);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003028}