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