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