4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
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>.
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)
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.
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.
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/module.h>
29 #include <linux/drbd.h>
32 #include <linux/file.h>
33 #include <linux/slab.h>
34 #include <linux/blkpg.h>
35 #include <linux/cpumask.h>
37 #include "drbd_protocol.h"
39 #include "drbd_state_change.h"
40 #include <asm/unaligned.h>
41 #include <linux/drbd_limits.h>
42 #include <linux/kthread.h>
44 #include <net/genetlink.h>
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);
50 int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info);
51 int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info);
53 int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info);
54 int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info);
55 int drbd_adm_down(struct sk_buff *skb, struct genl_info *info);
57 int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info);
58 int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info);
59 int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info);
60 int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info);
61 int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info);
62 int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info);
63 int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info);
64 int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info);
65 int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info);
66 int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info);
67 int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info);
68 int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info);
69 int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info);
70 int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info);
71 int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info);
72 int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info);
73 int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info);
74 int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info);
75 int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info);
76 int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info);
78 int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb);
79 int drbd_adm_dump_resources(struct sk_buff *skb, struct netlink_callback *cb);
80 int drbd_adm_dump_devices(struct sk_buff *skb, struct netlink_callback *cb);
81 int drbd_adm_dump_devices_done(struct netlink_callback *cb);
82 int drbd_adm_dump_connections(struct sk_buff *skb, struct netlink_callback *cb);
83 int drbd_adm_dump_connections_done(struct netlink_callback *cb);
84 int drbd_adm_dump_peer_devices(struct sk_buff *skb, struct netlink_callback *cb);
85 int drbd_adm_dump_peer_devices_done(struct netlink_callback *cb);
86 int drbd_adm_get_initial_state(struct sk_buff *skb, struct netlink_callback *cb);
88 #include <linux/drbd_genl_api.h>
90 #include <linux/genl_magic_func.h>
92 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
93 static atomic_t notify_genl_seq = ATOMIC_INIT(2); /* two. */
95 DEFINE_MUTEX(notification_mutex);
97 /* used blkdev_get_by_path, to claim our meta data device(s) */
98 static char *drbd_m_holder = "Hands off! this is DRBD's meta data device.";
100 static void drbd_adm_send_reply(struct sk_buff *skb, struct genl_info *info)
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");
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. */
109 static int drbd_msg_put_info(struct sk_buff *skb, const char *info)
114 if (!info || !info[0])
117 nla = nla_nest_start(skb, DRBD_NLA_CFG_REPLY);
121 err = nla_put_string(skb, T_info_text, info);
123 nla_nest_cancel(skb, nla);
126 nla_nest_end(skb, nla);
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.
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.
139 #define DRBD_ADM_NEED_MINOR 1
140 #define DRBD_ADM_NEED_RESOURCE 2
141 #define DRBD_ADM_NEED_CONNECTION 4
142 static int drbd_adm_prepare(struct drbd_config_context *adm_ctx,
143 struct sk_buff *skb, struct genl_info *info, unsigned flags)
145 struct drbd_genlmsghdr *d_in = info->userhdr;
146 const u8 cmd = info->genlhdr->cmd;
149 memset(adm_ctx, 0, sizeof(*adm_ctx));
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))
155 adm_ctx->reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
156 if (!adm_ctx->reply_skb) {
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.
165 if (!adm_ctx->reply_dh) {
170 adm_ctx->reply_dh->minor = d_in->minor;
171 adm_ctx->reply_dh->ret_code = NO_ERROR;
173 adm_ctx->volume = VOLUME_UNSPECIFIED;
174 if (info->attrs[DRBD_NLA_CFG_CONTEXT]) {
176 /* parse and validate only */
177 err = drbd_cfg_context_from_attrs(NULL, info);
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]);
189 /* and assign stuff to the adm_ctx */
190 nla = nested_attr_tb[__nla_type(T_ctx_volume)];
192 adm_ctx->volume = nla_get_u32(nla);
193 nla = nested_attr_tb[__nla_type(T_ctx_resource_name)];
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))) {
207 adm_ctx->minor = d_in->minor;
208 adm_ctx->device = minor_to_device(d_in->minor);
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. */
214 kref_get(&adm_ctx->device->kref);
216 if (adm_ctx->resource_name) {
217 adm_ctx->resource = drbd_find_resource(adm_ctx->resource_name);
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;
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;
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;
236 if (adm_ctx->device) {
237 drbd_msg_put_info(adm_ctx->reply_skb, "no minor number expected");
238 return ERR_INVALID_REQUEST;
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;
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;
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;
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);
282 nlmsg_free(adm_ctx->reply_skb);
283 adm_ctx->reply_skb = NULL;
287 static int drbd_adm_finish(struct drbd_config_context *adm_ctx,
288 struct genl_info *info, int retcode)
290 if (adm_ctx->device) {
291 kref_put(&adm_ctx->device->kref, drbd_destroy_device);
292 adm_ctx->device = NULL;
294 if (adm_ctx->connection) {
295 kref_put(&adm_ctx->connection->kref, &drbd_destroy_connection);
296 adm_ctx->connection = NULL;
298 if (adm_ctx->resource) {
299 kref_put(&adm_ctx->resource->kref, drbd_destroy_resource);
300 adm_ctx->resource = NULL;
303 if (!adm_ctx->reply_skb)
306 adm_ctx->reply_dh->ret_code = retcode;
307 drbd_adm_send_reply(adm_ctx->reply_skb, info);
311 static void setup_khelper_env(struct drbd_connection *connection, char **envp)
315 /* FIXME: A future version will not allow this case. */
316 if (connection->my_addr_len == 0 || connection->peer_addr_len == 0)
319 switch (((struct sockaddr *)&connection->peer_addr)->sa_family) {
322 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
323 &((struct sockaddr_in6 *)&connection->peer_addr)->sin6_addr);
327 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
328 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
332 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
333 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
335 snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
338 int drbd_khelper(struct drbd_device *device, char *cmd)
340 char *envp[] = { "HOME=/",
342 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
343 (char[20]) { }, /* address family */
344 (char[60]) { }, /* address */
347 char *argv[] = {usermode_helper, cmd, mb, NULL };
348 struct drbd_connection *connection = first_peer_device(device)->connection;
352 if (current == connection->worker.task)
353 set_bit(CALLBACK_PENDING, &connection->flags);
355 snprintf(mb, 14, "minor-%d", device_to_minor(device));
356 setup_khelper_env(connection, envp);
358 /* The helper may take some time.
359 * write out any unsynced meta data changes now */
360 drbd_md_sync(device);
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);
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);
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);
381 if (current == connection->worker.task)
382 clear_bit(CALLBACK_PENDING, &connection->flags);
384 if (ret < 0) /* Ignore any ERRNOs we got. */
390 enum drbd_peer_state conn_khelper(struct drbd_connection *connection, char *cmd)
392 char *envp[] = { "HOME=/",
394 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
395 (char[20]) { }, /* address family */
396 (char[60]) { }, /* address */
398 char *resource_name = connection->resource->name;
399 char *argv[] = {usermode_helper, cmd, resource_name, NULL };
402 setup_khelper_env(connection, envp);
403 conn_md_sync(connection);
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);
409 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
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);
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);
421 if (ret < 0) /* Ignore any ERRNOs we got. */
427 static enum drbd_fencing_p highest_fencing_policy(struct drbd_connection *connection)
429 enum drbd_fencing_p fp = FP_NOT_AVAIL;
430 struct drbd_peer_device *peer_device;
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);
448 static bool resource_is_supended(struct drbd_resource *resource)
450 return resource->susp || resource->susp_fen || resource->susp_nod;
453 bool conn_try_outdate_peer(struct drbd_connection *connection)
455 struct drbd_resource * const resource = connection->resource;
456 unsigned int connect_cnt;
457 union drbd_state mask = { };
458 union drbd_state val = { };
459 enum drbd_fencing_p fp;
463 spin_lock_irq(&resource->req_lock);
464 if (connection->cstate >= C_WF_REPORT_PARAMS) {
465 drbd_err(connection, "Expected cstate < C_WF_REPORT_PARAMS\n");
466 spin_unlock_irq(&resource->req_lock);
470 connect_cnt = connection->connect_cnt;
471 spin_unlock_irq(&resource->req_lock);
473 fp = highest_fencing_policy(connection);
476 drbd_warn(connection, "Not fencing peer, I'm not even Consistent myself.\n");
477 spin_lock_irq(&resource->req_lock);
478 if (connection->cstate < C_WF_REPORT_PARAMS) {
479 _conn_request_state(connection,
480 (union drbd_state) { { .susp_fen = 1 } },
481 (union drbd_state) { { .susp_fen = 0 } },
482 CS_VERBOSE | CS_HARD | CS_DC_SUSP);
483 /* We are no longer suspended due to the fencing policy.
484 * We may still be suspended due to the on-no-data-accessible policy.
485 * If that was OND_IO_ERROR, fail pending requests. */
486 if (!resource_is_supended(resource))
487 _tl_restart(connection, CONNECTION_LOST_WHILE_PENDING);
489 /* Else: in case we raced with a connection handshake,
490 * let the handshake figure out if we maybe can RESEND,
491 * and do not resume/fail pending requests here.
492 * Worst case is we stay suspended for now, which may be
493 * resolved by either re-establishing the replication link, or
494 * the next link failure, or eventually the administrator. */
495 spin_unlock_irq(&resource->req_lock);
503 r = conn_khelper(connection, "fence-peer");
505 switch ((r>>8) & 0xff) {
506 case P_INCONSISTENT: /* peer is inconsistent */
507 ex_to_string = "peer is inconsistent or worse";
509 val.pdsk = D_INCONSISTENT;
511 case P_OUTDATED: /* peer got outdated, or was already outdated */
512 ex_to_string = "peer was fenced";
514 val.pdsk = D_OUTDATED;
516 case P_DOWN: /* peer was down */
517 if (conn_highest_disk(connection) == D_UP_TO_DATE) {
518 /* we will(have) create(d) a new UUID anyways... */
519 ex_to_string = "peer is unreachable, assumed to be dead";
521 val.pdsk = D_OUTDATED;
523 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
526 case P_PRIMARY: /* Peer is primary, voluntarily outdate myself.
527 * This is useful when an unconnected R_SECONDARY is asked to
528 * become R_PRIMARY, but finds the other peer being active. */
529 ex_to_string = "peer is active";
530 drbd_warn(connection, "Peer is primary, outdating myself.\n");
532 val.disk = D_OUTDATED;
535 /* THINK: do we need to handle this
536 * like case 4, or more like case 5? */
537 if (fp != FP_STONITH)
538 drbd_err(connection, "fence-peer() = 7 && fencing != Stonith !!!\n");
539 ex_to_string = "peer was stonithed";
541 val.pdsk = D_OUTDATED;
544 /* The script is broken ... */
545 drbd_err(connection, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
546 return false; /* Eventually leave IO frozen */
549 drbd_info(connection, "fence-peer helper returned %d (%s)\n",
550 (r>>8) & 0xff, ex_to_string);
553 conn_request_state(connection, mask, val, CS_VERBOSE);
554 here, because we might were able to re-establish the connection in the
556 spin_lock_irq(&resource->req_lock);
557 if (connection->cstate < C_WF_REPORT_PARAMS && !test_bit(STATE_SENT, &connection->flags)) {
558 if (connection->connect_cnt != connect_cnt)
559 /* In case the connection was established and droped
560 while the fence-peer handler was running, ignore it */
561 drbd_info(connection, "Ignoring fence-peer exit code\n");
563 _conn_request_state(connection, mask, val, CS_VERBOSE);
565 spin_unlock_irq(&resource->req_lock);
567 return conn_highest_pdsk(connection) <= D_OUTDATED;
570 static int _try_outdate_peer_async(void *data)
572 struct drbd_connection *connection = (struct drbd_connection *)data;
574 conn_try_outdate_peer(connection);
576 kref_put(&connection->kref, drbd_destroy_connection);
580 void conn_try_outdate_peer_async(struct drbd_connection *connection)
582 struct task_struct *opa;
584 kref_get(&connection->kref);
585 /* We may just have force_sig()'ed this thread
586 * to get it out of some blocking network function.
587 * Clear signals; otherwise kthread_run(), which internally uses
588 * wait_on_completion_killable(), will mistake our pending signal
589 * for a new fatal signal and fail. */
590 flush_signals(current);
591 opa = kthread_run(_try_outdate_peer_async, connection, "drbd_async_h");
593 drbd_err(connection, "out of mem, failed to invoke fence-peer helper\n");
594 kref_put(&connection->kref, drbd_destroy_connection);
599 drbd_set_role(struct drbd_device *const device, enum drbd_role new_role, int force)
601 struct drbd_peer_device *const peer_device = first_peer_device(device);
602 struct drbd_connection *const connection = peer_device ? peer_device->connection : NULL;
603 const int max_tries = 4;
604 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
608 union drbd_state mask, val;
610 if (new_role == R_PRIMARY) {
611 struct drbd_connection *connection;
613 /* Detect dead peers as soon as possible. */
616 for_each_connection(connection, device->resource)
617 request_ping(connection);
621 mutex_lock(device->state_mutex);
623 mask.i = 0; mask.role = R_MASK;
624 val.i = 0; val.role = new_role;
626 while (try++ < max_tries) {
627 rv = _drbd_request_state_holding_state_mutex(device, mask, val, CS_WAIT_COMPLETE);
629 /* in case we first succeeded to outdate,
630 * but now suddenly could establish a connection */
631 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
637 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
638 (device->state.disk < D_UP_TO_DATE &&
639 device->state.disk >= D_INCONSISTENT)) {
641 val.disk = D_UP_TO_DATE;
646 if (rv == SS_NO_UP_TO_DATE_DISK &&
647 device->state.disk == D_CONSISTENT && mask.pdsk == 0) {
648 D_ASSERT(device, device->state.pdsk == D_UNKNOWN);
650 if (conn_try_outdate_peer(connection)) {
651 val.disk = D_UP_TO_DATE;
657 if (rv == SS_NOTHING_TO_DO)
659 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
660 if (!conn_try_outdate_peer(connection) && force) {
661 drbd_warn(device, "Forced into split brain situation!\n");
663 val.pdsk = D_OUTDATED;
668 if (rv == SS_TWO_PRIMARIES) {
669 /* Maybe the peer is detected as dead very soon...
670 retry at most once more in this case. */
671 if (try < max_tries) {
675 nc = rcu_dereference(connection->net_conf);
676 timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
678 schedule_timeout_interruptible(timeo);
682 if (rv < SS_SUCCESS) {
683 rv = _drbd_request_state(device, mask, val,
684 CS_VERBOSE + CS_WAIT_COMPLETE);
695 drbd_warn(device, "Forced to consider local data as UpToDate!\n");
697 /* Wait until nothing is on the fly :) */
698 wait_event(device->misc_wait, atomic_read(&device->ap_pending_cnt) == 0);
700 /* FIXME also wait for all pending P_BARRIER_ACK? */
702 if (new_role == R_SECONDARY) {
703 if (get_ldev(device)) {
704 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
708 mutex_lock(&device->resource->conf_update);
709 nc = connection->net_conf;
711 nc->discard_my_data = 0; /* without copy; single bit op is atomic */
712 mutex_unlock(&device->resource->conf_update);
714 if (get_ldev(device)) {
715 if (((device->state.conn < C_CONNECTED ||
716 device->state.pdsk <= D_FAILED)
717 && device->ldev->md.uuid[UI_BITMAP] == 0) || forced)
718 drbd_uuid_new_current(device);
720 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
725 /* writeout of activity log covered areas of the bitmap
726 * to stable storage done in after state change already */
728 if (device->state.conn >= C_WF_REPORT_PARAMS) {
729 /* if this was forced, we should consider sync */
731 drbd_send_uuids(peer_device);
732 drbd_send_current_state(peer_device);
735 drbd_md_sync(device);
736 set_disk_ro(device->vdisk, new_role == R_SECONDARY);
737 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
739 mutex_unlock(device->state_mutex);
743 static const char *from_attrs_err_to_txt(int err)
745 return err == -ENOMSG ? "required attribute missing" :
746 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
747 err == -EEXIST ? "can not change invariant setting" :
748 "invalid attribute value";
751 int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
753 struct drbd_config_context adm_ctx;
754 struct set_role_parms parms;
756 enum drbd_ret_code retcode;
758 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
759 if (!adm_ctx.reply_skb)
761 if (retcode != NO_ERROR)
764 memset(&parms, 0, sizeof(parms));
765 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
766 err = set_role_parms_from_attrs(&parms, info);
768 retcode = ERR_MANDATORY_TAG;
769 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
774 mutex_lock(&adm_ctx.resource->adm_mutex);
776 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
777 retcode = drbd_set_role(adm_ctx.device, R_PRIMARY, parms.assume_uptodate);
779 retcode = drbd_set_role(adm_ctx.device, R_SECONDARY, 0);
781 mutex_unlock(&adm_ctx.resource->adm_mutex);
784 drbd_adm_finish(&adm_ctx, info, retcode);
788 /* Initializes the md.*_offset members, so we are able to find
789 * the on disk meta data.
791 * We currently have two possible layouts:
793 * |----------- md_size_sect ------------------|
794 * [ 4k superblock ][ activity log ][ Bitmap ]
796 * | bm_offset = al_offset + X |
797 * ==> bitmap sectors = md_size_sect - bm_offset
800 * |----------- md_size_sect ------------------|
801 * [data.....][ Bitmap ][ activity log ][ 4k superblock ]
803 * | bm_offset = al_offset - Y |
804 * ==> bitmap sectors = Y = al_offset - bm_offset
806 * Activity log size used to be fixed 32kB,
807 * but is about to become configurable.
809 static void drbd_md_set_sector_offsets(struct drbd_device *device,
810 struct drbd_backing_dev *bdev)
812 sector_t md_size_sect = 0;
813 unsigned int al_size_sect = bdev->md.al_size_4k * 8;
815 bdev->md.md_offset = drbd_md_ss(bdev);
817 switch (bdev->md.meta_dev_idx) {
819 /* v07 style fixed size indexed meta data */
820 bdev->md.md_size_sect = MD_128MB_SECT;
821 bdev->md.al_offset = MD_4kB_SECT;
822 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
824 case DRBD_MD_INDEX_FLEX_EXT:
825 /* just occupy the full device; unit: sectors */
826 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
827 bdev->md.al_offset = MD_4kB_SECT;
828 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
830 case DRBD_MD_INDEX_INTERNAL:
831 case DRBD_MD_INDEX_FLEX_INT:
832 /* al size is still fixed */
833 bdev->md.al_offset = -al_size_sect;
834 /* we need (slightly less than) ~ this much bitmap sectors: */
835 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
836 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
837 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
838 md_size_sect = ALIGN(md_size_sect, 8);
840 /* plus the "drbd meta data super block",
841 * and the activity log; */
842 md_size_sect += MD_4kB_SECT + al_size_sect;
844 bdev->md.md_size_sect = md_size_sect;
845 /* bitmap offset is adjusted by 'super' block size */
846 bdev->md.bm_offset = -md_size_sect + MD_4kB_SECT;
851 /* input size is expected to be in KB */
852 char *ppsize(char *buf, unsigned long long size)
854 /* Needs 9 bytes at max including trailing NUL:
855 * -1ULL ==> "16384 EB" */
856 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
858 while (size >= 10000 && base < sizeof(units)-1) {
860 size = (size >> 10) + !!(size & (1<<9));
863 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
868 /* there is still a theoretical deadlock when called from receiver
869 * on an D_INCONSISTENT R_PRIMARY:
870 * remote READ does inc_ap_bio, receiver would need to receive answer
871 * packet from remote to dec_ap_bio again.
872 * receiver receive_sizes(), comes here,
873 * waits for ap_bio_cnt == 0. -> deadlock.
874 * but this cannot happen, actually, because:
875 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
876 * (not connected, or bad/no disk on peer):
877 * see drbd_fail_request_early, ap_bio_cnt is zero.
878 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
879 * peer may not initiate a resize.
881 /* Note these are not to be confused with
882 * drbd_adm_suspend_io/drbd_adm_resume_io,
883 * which are (sub) state changes triggered by admin (drbdsetup),
884 * and can be long lived.
885 * This changes an device->flag, is triggered by drbd internals,
886 * and should be short-lived. */
887 /* It needs to be a counter, since multiple threads might
888 independently suspend and resume IO. */
889 void drbd_suspend_io(struct drbd_device *device)
891 atomic_inc(&device->suspend_cnt);
892 if (drbd_suspended(device))
894 wait_event(device->misc_wait, !atomic_read(&device->ap_bio_cnt));
897 void drbd_resume_io(struct drbd_device *device)
899 if (atomic_dec_and_test(&device->suspend_cnt))
900 wake_up(&device->misc_wait);
904 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
905 * @device: DRBD device.
907 * Returns 0 on success, negative return values indicate errors.
908 * You should call drbd_md_sync() after calling this function.
910 enum determine_dev_size
911 drbd_determine_dev_size(struct drbd_device *device, enum dds_flags flags, struct resize_parms *rs) __must_hold(local)
913 struct md_offsets_and_sizes {
914 u64 last_agreed_sect;
921 u32 al_stripe_size_4k;
923 sector_t u_size, size;
924 struct drbd_md *md = &device->ldev->md;
928 int md_moved, la_size_changed;
929 enum determine_dev_size rv = DS_UNCHANGED;
931 /* We may change the on-disk offsets of our meta data below. Lock out
932 * anything that may cause meta data IO, to avoid acting on incomplete
933 * layout changes or scribbling over meta data that is in the process
936 * Move is not exactly correct, btw, currently we have all our meta
937 * data in core memory, to "move" it we just write it all out, there
939 drbd_suspend_io(device);
940 buffer = drbd_md_get_buffer(device, __func__); /* Lock meta-data IO */
942 drbd_resume_io(device);
946 /* remember current offset and sizes */
947 prev.last_agreed_sect = md->la_size_sect;
948 prev.md_offset = md->md_offset;
949 prev.al_offset = md->al_offset;
950 prev.bm_offset = md->bm_offset;
951 prev.md_size_sect = md->md_size_sect;
952 prev.al_stripes = md->al_stripes;
953 prev.al_stripe_size_4k = md->al_stripe_size_4k;
956 /* rs is non NULL if we should change the AL layout only */
957 md->al_stripes = rs->al_stripes;
958 md->al_stripe_size_4k = rs->al_stripe_size / 4;
959 md->al_size_4k = (u64)rs->al_stripes * rs->al_stripe_size / 4;
962 drbd_md_set_sector_offsets(device, device->ldev);
965 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
967 size = drbd_new_dev_size(device, device->ldev, u_size, flags & DDSF_FORCED);
969 if (size < prev.last_agreed_sect) {
970 if (rs && u_size == 0) {
971 /* Remove "rs &&" later. This check should always be active, but
972 right now the receiver expects the permissive behavior */
973 drbd_warn(device, "Implicit shrink not allowed. "
974 "Use --size=%llus for explicit shrink.\n",
975 (unsigned long long)size);
976 rv = DS_ERROR_SHRINK;
979 rv = DS_ERROR_SPACE_MD;
980 if (rv != DS_UNCHANGED)
984 if (drbd_get_capacity(device->this_bdev) != size ||
985 drbd_bm_capacity(device) != size) {
987 err = drbd_bm_resize(device, size, !(flags & DDSF_NO_RESYNC));
989 /* currently there is only one error: ENOMEM! */
990 size = drbd_bm_capacity(device);
992 drbd_err(device, "OUT OF MEMORY! "
993 "Could not allocate bitmap!\n");
995 drbd_err(device, "BM resizing failed. "
996 "Leaving size unchanged\n");
1000 /* racy, see comments above. */
1001 drbd_set_my_capacity(device, size);
1002 md->la_size_sect = size;
1003 drbd_info(device, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
1004 (unsigned long long)size>>1);
1009 la_size_changed = (prev.last_agreed_sect != md->la_size_sect);
1011 md_moved = prev.md_offset != md->md_offset
1012 || prev.md_size_sect != md->md_size_sect;
1014 if (la_size_changed || md_moved || rs) {
1017 /* We do some synchronous IO below, which may take some time.
1018 * Clear the timer, to avoid scary "timer expired!" messages,
1019 * "Superblock" is written out at least twice below, anyways. */
1020 del_timer(&device->md_sync_timer);
1022 /* We won't change the "al-extents" setting, we just may need
1023 * to move the on-disk location of the activity log ringbuffer.
1024 * Lock for transaction is good enough, it may well be "dirty"
1025 * or even "starving". */
1026 wait_event(device->al_wait, lc_try_lock_for_transaction(device->act_log));
1028 /* mark current on-disk bitmap and activity log as unreliable */
1029 prev_flags = md->flags;
1030 md->flags |= MDF_FULL_SYNC | MDF_AL_DISABLED;
1031 drbd_md_write(device, buffer);
1033 drbd_al_initialize(device, buffer);
1035 drbd_info(device, "Writing the whole bitmap, %s\n",
1036 la_size_changed && md_moved ? "size changed and md moved" :
1037 la_size_changed ? "size changed" : "md moved");
1038 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
1039 drbd_bitmap_io(device, md_moved ? &drbd_bm_write_all : &drbd_bm_write,
1040 "size changed", BM_LOCKED_MASK);
1042 /* on-disk bitmap and activity log is authoritative again
1043 * (unless there was an IO error meanwhile...) */
1044 md->flags = prev_flags;
1045 drbd_md_write(device, buffer);
1048 drbd_info(device, "Changed AL layout to al-stripes = %d, al-stripe-size-kB = %d\n",
1049 md->al_stripes, md->al_stripe_size_4k * 4);
1052 if (size > prev.last_agreed_sect)
1053 rv = prev.last_agreed_sect ? DS_GREW : DS_GREW_FROM_ZERO;
1054 if (size < prev.last_agreed_sect)
1059 /* restore previous offset and sizes */
1060 md->la_size_sect = prev.last_agreed_sect;
1061 md->md_offset = prev.md_offset;
1062 md->al_offset = prev.al_offset;
1063 md->bm_offset = prev.bm_offset;
1064 md->md_size_sect = prev.md_size_sect;
1065 md->al_stripes = prev.al_stripes;
1066 md->al_stripe_size_4k = prev.al_stripe_size_4k;
1067 md->al_size_4k = (u64)prev.al_stripes * prev.al_stripe_size_4k;
1069 lc_unlock(device->act_log);
1070 wake_up(&device->al_wait);
1071 drbd_md_put_buffer(device);
1072 drbd_resume_io(device);
1078 drbd_new_dev_size(struct drbd_device *device, struct drbd_backing_dev *bdev,
1079 sector_t u_size, int assume_peer_has_space)
1081 sector_t p_size = device->p_size; /* partner's disk size. */
1082 sector_t la_size_sect = bdev->md.la_size_sect; /* last agreed size. */
1083 sector_t m_size; /* my size */
1086 m_size = drbd_get_max_capacity(bdev);
1088 if (device->state.conn < C_CONNECTED && assume_peer_has_space) {
1089 drbd_warn(device, "Resize while not connected was forced by the user!\n");
1093 if (p_size && m_size) {
1094 size = min_t(sector_t, p_size, m_size);
1097 size = la_size_sect;
1098 if (m_size && m_size < size)
1100 if (p_size && p_size < size)
1111 drbd_err(device, "Both nodes diskless!\n");
1115 drbd_err(device, "Requested disk size is too big (%lu > %lu)\n",
1116 (unsigned long)u_size>>1, (unsigned long)size>>1);
1125 * drbd_check_al_size() - Ensures that the AL is of the right size
1126 * @device: DRBD device.
1128 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
1129 * failed, and 0 on success. You should call drbd_md_sync() after you called
1132 static int drbd_check_al_size(struct drbd_device *device, struct disk_conf *dc)
1134 struct lru_cache *n, *t;
1135 struct lc_element *e;
1136 unsigned int in_use;
1139 if (device->act_log &&
1140 device->act_log->nr_elements == dc->al_extents)
1144 t = device->act_log;
1145 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
1146 dc->al_extents, sizeof(struct lc_element), 0);
1149 drbd_err(device, "Cannot allocate act_log lru!\n");
1152 spin_lock_irq(&device->al_lock);
1154 for (i = 0; i < t->nr_elements; i++) {
1155 e = lc_element_by_index(t, i);
1157 drbd_err(device, "refcnt(%d)==%d\n",
1158 e->lc_number, e->refcnt);
1159 in_use += e->refcnt;
1163 device->act_log = n;
1164 spin_unlock_irq(&device->al_lock);
1166 drbd_err(device, "Activity log still in use!\n");
1172 drbd_md_mark_dirty(device); /* we changed device->act_log->nr_elemens */
1176 static void blk_queue_discard_granularity(struct request_queue *q, unsigned int granularity)
1178 q->limits.discard_granularity = granularity;
1181 static unsigned int drbd_max_discard_sectors(struct drbd_connection *connection)
1183 /* when we introduced REQ_WRITE_SAME support, we also bumped
1184 * our maximum supported batch bio size used for discards. */
1185 if (connection->agreed_features & DRBD_FF_WSAME)
1186 return DRBD_MAX_BBIO_SECTORS;
1187 /* before, with DRBD <= 8.4.6, we only allowed up to one AL_EXTENT_SIZE. */
1188 return AL_EXTENT_SIZE >> 9;
1191 static void decide_on_discard_support(struct drbd_device *device,
1192 struct request_queue *q,
1193 struct request_queue *b,
1194 bool discard_zeroes_if_aligned)
1196 /* q = drbd device queue (device->rq_queue)
1197 * b = backing device queue (device->ldev->backing_bdev->bd_disk->queue),
1198 * or NULL if diskless
1200 struct drbd_connection *connection = first_peer_device(device)->connection;
1201 bool can_do = b ? blk_queue_discard(b) : true;
1203 if (can_do && b && !b->limits.discard_zeroes_data && !discard_zeroes_if_aligned) {
1205 drbd_info(device, "discard_zeroes_data=0 and discard_zeroes_if_aligned=no: disabling discards\n");
1207 if (can_do && connection->cstate >= C_CONNECTED && !(connection->agreed_features & DRBD_FF_TRIM)) {
1209 drbd_info(connection, "peer DRBD too old, does not support TRIM: disabling discards\n");
1212 /* We don't care for the granularity, really.
1213 * Stacking limits below should fix it for the local
1214 * device. Whether or not it is a suitable granularity
1215 * on the remote device is not our problem, really. If
1216 * you care, you need to use devices with similar
1217 * topology on all peers. */
1218 blk_queue_discard_granularity(q, 512);
1219 q->limits.max_discard_sectors = drbd_max_discard_sectors(connection);
1220 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
1222 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
1223 blk_queue_discard_granularity(q, 0);
1224 q->limits.max_discard_sectors = 0;
1228 static void fixup_discard_if_not_supported(struct request_queue *q)
1230 /* To avoid confusion, if this queue does not support discard, clear
1231 * max_discard_sectors, which is what lsblk -D reports to the user.
1232 * Older kernels got this wrong in "stack limits".
1234 if (!blk_queue_discard(q)) {
1235 blk_queue_max_discard_sectors(q, 0);
1236 blk_queue_discard_granularity(q, 0);
1240 static void decide_on_write_same_support(struct drbd_device *device,
1241 struct request_queue *q,
1242 struct request_queue *b, struct o_qlim *o)
1244 struct drbd_peer_device *peer_device = first_peer_device(device);
1245 struct drbd_connection *connection = peer_device->connection;
1246 bool can_do = b ? b->limits.max_write_same_sectors : true;
1248 if (can_do && connection->cstate >= C_CONNECTED && !(connection->agreed_features & DRBD_FF_WSAME)) {
1250 drbd_info(peer_device, "peer does not support WRITE_SAME\n");
1254 /* logical block size; queue_logical_block_size(NULL) is 512 */
1255 unsigned int peer_lbs = be32_to_cpu(o->logical_block_size);
1256 unsigned int me_lbs_b = queue_logical_block_size(b);
1257 unsigned int me_lbs = queue_logical_block_size(q);
1259 if (me_lbs_b != me_lbs) {
1261 "logical block size of local backend does not match (drbd:%u, backend:%u); was this a late attach?\n",
1263 /* rather disable write same than trigger some BUG_ON later in the scsi layer. */
1266 if (me_lbs_b != peer_lbs) {
1267 drbd_warn(peer_device, "logical block sizes do not match (me:%u, peer:%u); this may cause problems.\n",
1270 drbd_dbg(peer_device, "logical block size mismatch: WRITE_SAME disabled.\n");
1273 me_lbs = max(me_lbs, me_lbs_b);
1274 /* We cannot change the logical block size of an in-use queue.
1275 * We can only hope that access happens to be properly aligned.
1276 * If not, the peer will likely produce an IO error, and detach. */
1277 if (peer_lbs > me_lbs) {
1278 if (device->state.role != R_PRIMARY) {
1279 blk_queue_logical_block_size(q, peer_lbs);
1280 drbd_warn(peer_device, "logical block size set to %u\n", peer_lbs);
1282 drbd_warn(peer_device,
1283 "current Primary must NOT adjust logical block size (%u -> %u); hope for the best.\n",
1288 if (can_do && !o->write_same_capable) {
1289 /* If we introduce an open-coded write-same loop on the receiving side,
1290 * the peer would present itself as "capable". */
1291 drbd_dbg(peer_device, "WRITE_SAME disabled (peer device not capable)\n");
1296 blk_queue_max_write_same_sectors(q, can_do ? DRBD_MAX_BBIO_SECTORS : 0);
1299 static void drbd_setup_queue_param(struct drbd_device *device, struct drbd_backing_dev *bdev,
1300 unsigned int max_bio_size, struct o_qlim *o)
1302 struct request_queue * const q = device->rq_queue;
1303 unsigned int max_hw_sectors = max_bio_size >> 9;
1304 unsigned int max_segments = 0;
1305 struct request_queue *b = NULL;
1306 struct disk_conf *dc;
1307 bool discard_zeroes_if_aligned = true;
1310 b = bdev->backing_bdev->bd_disk->queue;
1312 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
1314 dc = rcu_dereference(device->ldev->disk_conf);
1315 max_segments = dc->max_bio_bvecs;
1316 discard_zeroes_if_aligned = dc->discard_zeroes_if_aligned;
1319 blk_set_stacking_limits(&q->limits);
1322 blk_queue_max_hw_sectors(q, max_hw_sectors);
1323 /* This is the workaround for "bio would need to, but cannot, be split" */
1324 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
1325 blk_queue_segment_boundary(q, PAGE_SIZE-1);
1326 decide_on_discard_support(device, q, b, discard_zeroes_if_aligned);
1327 decide_on_write_same_support(device, q, b, o);
1330 blk_queue_stack_limits(q, b);
1332 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
1333 drbd_info(device, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
1334 q->backing_dev_info.ra_pages,
1335 b->backing_dev_info.ra_pages);
1336 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1339 fixup_discard_if_not_supported(q);
1342 void drbd_reconsider_queue_parameters(struct drbd_device *device, struct drbd_backing_dev *bdev, struct o_qlim *o)
1344 unsigned int now, new, local, peer;
1346 now = queue_max_hw_sectors(device->rq_queue) << 9;
1347 local = device->local_max_bio_size; /* Eventually last known value, from volatile memory */
1348 peer = device->peer_max_bio_size; /* Eventually last known value, from meta data */
1351 local = queue_max_hw_sectors(bdev->backing_bdev->bd_disk->queue) << 9;
1352 device->local_max_bio_size = local;
1354 local = min(local, DRBD_MAX_BIO_SIZE);
1356 /* We may ignore peer limits if the peer is modern enough.
1357 Because new from 8.3.8 onwards the peer can use multiple
1358 BIOs for a single peer_request */
1359 if (device->state.conn >= C_WF_REPORT_PARAMS) {
1360 if (first_peer_device(device)->connection->agreed_pro_version < 94)
1361 peer = min(device->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
1362 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
1363 else if (first_peer_device(device)->connection->agreed_pro_version == 94)
1364 peer = DRBD_MAX_SIZE_H80_PACKET;
1365 else if (first_peer_device(device)->connection->agreed_pro_version < 100)
1366 peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
1368 peer = DRBD_MAX_BIO_SIZE;
1370 /* We may later detach and re-attach on a disconnected Primary.
1371 * Avoid this setting to jump back in that case.
1372 * We want to store what we know the peer DRBD can handle,
1373 * not what the peer IO backend can handle. */
1374 if (peer > device->peer_max_bio_size)
1375 device->peer_max_bio_size = peer;
1377 new = min(local, peer);
1379 if (device->state.role == R_PRIMARY && new < now)
1380 drbd_err(device, "ASSERT FAILED new < now; (%u < %u)\n", new, now);
1383 drbd_info(device, "max BIO size = %u\n", new);
1385 drbd_setup_queue_param(device, bdev, new, o);
1388 /* Starts the worker thread */
1389 static void conn_reconfig_start(struct drbd_connection *connection)
1391 drbd_thread_start(&connection->worker);
1392 drbd_flush_workqueue(&connection->sender_work);
1395 /* if still unconfigured, stops worker again. */
1396 static void conn_reconfig_done(struct drbd_connection *connection)
1399 spin_lock_irq(&connection->resource->req_lock);
1400 stop_threads = conn_all_vols_unconf(connection) &&
1401 connection->cstate == C_STANDALONE;
1402 spin_unlock_irq(&connection->resource->req_lock);
1404 /* ack_receiver thread and ack_sender workqueue are implicitly
1405 * stopped by receiver in conn_disconnect() */
1406 drbd_thread_stop(&connection->receiver);
1407 drbd_thread_stop(&connection->worker);
1411 /* Make sure IO is suspended before calling this function(). */
1412 static void drbd_suspend_al(struct drbd_device *device)
1416 if (!lc_try_lock(device->act_log)) {
1417 drbd_warn(device, "Failed to lock al in drbd_suspend_al()\n");
1421 drbd_al_shrink(device);
1422 spin_lock_irq(&device->resource->req_lock);
1423 if (device->state.conn < C_CONNECTED)
1424 s = !test_and_set_bit(AL_SUSPENDED, &device->flags);
1425 spin_unlock_irq(&device->resource->req_lock);
1426 lc_unlock(device->act_log);
1429 drbd_info(device, "Suspended AL updates\n");
1433 static bool should_set_defaults(struct genl_info *info)
1435 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1436 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1439 static unsigned int drbd_al_extents_max(struct drbd_backing_dev *bdev)
1441 /* This is limited by 16 bit "slot" numbers,
1442 * and by available on-disk context storage.
1444 * Also (u16)~0 is special (denotes a "free" extent).
1446 * One transaction occupies one 4kB on-disk block,
1447 * we have n such blocks in the on disk ring buffer,
1448 * the "current" transaction may fail (n-1),
1449 * and there is 919 slot numbers context information per transaction.
1451 * 72 transaction blocks amounts to more than 2**16 context slots,
1452 * so cap there first.
1454 const unsigned int max_al_nr = DRBD_AL_EXTENTS_MAX;
1455 const unsigned int sufficient_on_disk =
1456 (max_al_nr + AL_CONTEXT_PER_TRANSACTION -1)
1457 /AL_CONTEXT_PER_TRANSACTION;
1459 unsigned int al_size_4k = bdev->md.al_size_4k;
1461 if (al_size_4k > sufficient_on_disk)
1464 return (al_size_4k - 1) * AL_CONTEXT_PER_TRANSACTION;
1467 static bool write_ordering_changed(struct disk_conf *a, struct disk_conf *b)
1469 return a->disk_barrier != b->disk_barrier ||
1470 a->disk_flushes != b->disk_flushes ||
1471 a->disk_drain != b->disk_drain;
1474 static void sanitize_disk_conf(struct drbd_device *device, struct disk_conf *disk_conf,
1475 struct drbd_backing_dev *nbc)
1477 struct request_queue * const q = nbc->backing_bdev->bd_disk->queue;
1479 if (disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1480 disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
1481 if (disk_conf->al_extents > drbd_al_extents_max(nbc))
1482 disk_conf->al_extents = drbd_al_extents_max(nbc);
1484 if (!blk_queue_discard(q)
1485 || (!q->limits.discard_zeroes_data && !disk_conf->discard_zeroes_if_aligned)) {
1486 if (disk_conf->rs_discard_granularity) {
1487 disk_conf->rs_discard_granularity = 0; /* disable feature */
1488 drbd_info(device, "rs_discard_granularity feature disabled\n");
1492 if (disk_conf->rs_discard_granularity) {
1493 int orig_value = disk_conf->rs_discard_granularity;
1496 if (q->limits.discard_granularity > disk_conf->rs_discard_granularity)
1497 disk_conf->rs_discard_granularity = q->limits.discard_granularity;
1499 remainder = disk_conf->rs_discard_granularity % q->limits.discard_granularity;
1500 disk_conf->rs_discard_granularity += remainder;
1502 if (disk_conf->rs_discard_granularity > q->limits.max_discard_sectors << 9)
1503 disk_conf->rs_discard_granularity = q->limits.max_discard_sectors << 9;
1505 if (disk_conf->rs_discard_granularity != orig_value)
1506 drbd_info(device, "rs_discard_granularity changed to %d\n",
1507 disk_conf->rs_discard_granularity);
1511 static int disk_opts_check_al_size(struct drbd_device *device, struct disk_conf *dc)
1515 if (device->act_log &&
1516 device->act_log->nr_elements == dc->al_extents)
1519 drbd_suspend_io(device);
1520 /* If IO completion is currently blocked, we would likely wait
1521 * "forever" for the activity log to become unused. So we don't. */
1522 if (atomic_read(&device->ap_bio_cnt))
1525 wait_event(device->al_wait, lc_try_lock(device->act_log));
1526 drbd_al_shrink(device);
1527 err = drbd_check_al_size(device, dc);
1528 lc_unlock(device->act_log);
1529 wake_up(&device->al_wait);
1531 drbd_resume_io(device);
1535 int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1537 struct drbd_config_context adm_ctx;
1538 enum drbd_ret_code retcode;
1539 struct drbd_device *device;
1540 struct disk_conf *new_disk_conf, *old_disk_conf;
1541 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
1544 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
1545 if (!adm_ctx.reply_skb)
1547 if (retcode != NO_ERROR)
1550 device = adm_ctx.device;
1551 mutex_lock(&adm_ctx.resource->adm_mutex);
1553 /* we also need a disk
1554 * to change the options on */
1555 if (!get_ldev(device)) {
1556 retcode = ERR_NO_DISK;
1560 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
1561 if (!new_disk_conf) {
1562 retcode = ERR_NOMEM;
1566 mutex_lock(&device->resource->conf_update);
1567 old_disk_conf = device->ldev->disk_conf;
1568 *new_disk_conf = *old_disk_conf;
1569 if (should_set_defaults(info))
1570 set_disk_conf_defaults(new_disk_conf);
1572 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
1573 if (err && err != -ENOMSG) {
1574 retcode = ERR_MANDATORY_TAG;
1575 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
1579 if (!expect(new_disk_conf->resync_rate >= 1))
1580 new_disk_conf->resync_rate = 1;
1582 sanitize_disk_conf(device, new_disk_conf, device->ldev);
1584 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1585 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
1587 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
1588 if (fifo_size != device->rs_plan_s->size) {
1589 new_plan = fifo_alloc(fifo_size);
1591 drbd_err(device, "kmalloc of fifo_buffer failed");
1592 retcode = ERR_NOMEM;
1597 err = disk_opts_check_al_size(device, new_disk_conf);
1599 /* Could be just "busy". Ignore?
1600 * Introduce dedicated error code? */
1601 drbd_msg_put_info(adm_ctx.reply_skb,
1602 "Try again without changing current al-extents setting");
1603 retcode = ERR_NOMEM;
1607 lock_all_resources();
1608 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
1609 if (retcode == NO_ERROR) {
1610 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
1611 drbd_resync_after_changed(device);
1613 unlock_all_resources();
1615 if (retcode != NO_ERROR)
1619 old_plan = device->rs_plan_s;
1620 rcu_assign_pointer(device->rs_plan_s, new_plan);
1623 mutex_unlock(&device->resource->conf_update);
1625 if (new_disk_conf->al_updates)
1626 device->ldev->md.flags &= ~MDF_AL_DISABLED;
1628 device->ldev->md.flags |= MDF_AL_DISABLED;
1630 if (new_disk_conf->md_flushes)
1631 clear_bit(MD_NO_FUA, &device->flags);
1633 set_bit(MD_NO_FUA, &device->flags);
1635 if (write_ordering_changed(old_disk_conf, new_disk_conf))
1636 drbd_bump_write_ordering(device->resource, NULL, WO_BDEV_FLUSH);
1638 if (old_disk_conf->discard_zeroes_if_aligned != new_disk_conf->discard_zeroes_if_aligned)
1639 drbd_reconsider_queue_parameters(device, device->ldev, NULL);
1641 drbd_md_sync(device);
1643 if (device->state.conn >= C_CONNECTED) {
1644 struct drbd_peer_device *peer_device;
1646 for_each_peer_device(peer_device, device)
1647 drbd_send_sync_param(peer_device);
1651 kfree(old_disk_conf);
1653 mod_timer(&device->request_timer, jiffies + HZ);
1657 mutex_unlock(&device->resource->conf_update);
1659 kfree(new_disk_conf);
1664 mutex_unlock(&adm_ctx.resource->adm_mutex);
1666 drbd_adm_finish(&adm_ctx, info, retcode);
1670 static struct block_device *open_backing_dev(struct drbd_device *device,
1671 const char *bdev_path, void *claim_ptr, bool do_bd_link)
1673 struct block_device *bdev;
1676 bdev = blkdev_get_by_path(bdev_path,
1677 FMODE_READ | FMODE_WRITE | FMODE_EXCL, claim_ptr);
1679 drbd_err(device, "open(\"%s\") failed with %ld\n",
1680 bdev_path, PTR_ERR(bdev));
1687 err = bd_link_disk_holder(bdev, device->vdisk);
1689 blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1690 drbd_err(device, "bd_link_disk_holder(\"%s\", ...) failed with %d\n",
1692 bdev = ERR_PTR(err);
1697 static int open_backing_devices(struct drbd_device *device,
1698 struct disk_conf *new_disk_conf,
1699 struct drbd_backing_dev *nbc)
1701 struct block_device *bdev;
1703 bdev = open_backing_dev(device, new_disk_conf->backing_dev, device, true);
1705 return ERR_OPEN_DISK;
1706 nbc->backing_bdev = bdev;
1709 * meta_dev_idx >= 0: external fixed size, possibly multiple
1710 * drbd sharing one meta device. TODO in that case, paranoia
1711 * check that [md_bdev, meta_dev_idx] is not yet used by some
1712 * other drbd minor! (if you use drbd.conf + drbdadm, that
1713 * should check it for you already; but if you don't, or
1714 * someone fooled it, we need to double check here)
1716 bdev = open_backing_dev(device, new_disk_conf->meta_dev,
1717 /* claim ptr: device, if claimed exclusively; shared drbd_m_holder,
1718 * if potentially shared with other drbd minors */
1719 (new_disk_conf->meta_dev_idx < 0) ? (void*)device : (void*)drbd_m_holder,
1720 /* avoid double bd_claim_by_disk() for the same (source,target) tuple,
1721 * as would happen with internal metadata. */
1722 (new_disk_conf->meta_dev_idx != DRBD_MD_INDEX_FLEX_INT &&
1723 new_disk_conf->meta_dev_idx != DRBD_MD_INDEX_INTERNAL));
1725 return ERR_OPEN_MD_DISK;
1726 nbc->md_bdev = bdev;
1730 static void close_backing_dev(struct drbd_device *device, struct block_device *bdev,
1736 bd_unlink_disk_holder(bdev, device->vdisk);
1737 blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1740 void drbd_backing_dev_free(struct drbd_device *device, struct drbd_backing_dev *ldev)
1745 close_backing_dev(device, ldev->md_bdev, ldev->md_bdev != ldev->backing_bdev);
1746 close_backing_dev(device, ldev->backing_bdev, true);
1748 kfree(ldev->disk_conf);
1752 int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
1754 struct drbd_config_context adm_ctx;
1755 struct drbd_device *device;
1756 struct drbd_peer_device *peer_device;
1757 struct drbd_connection *connection;
1759 enum drbd_ret_code retcode;
1760 enum determine_dev_size dd;
1761 sector_t max_possible_sectors;
1762 sector_t min_md_device_sectors;
1763 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
1764 struct disk_conf *new_disk_conf = NULL;
1765 struct lru_cache *resync_lru = NULL;
1766 struct fifo_buffer *new_plan = NULL;
1767 union drbd_state ns, os;
1768 enum drbd_state_rv rv;
1769 struct net_conf *nc;
1771 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
1772 if (!adm_ctx.reply_skb)
1774 if (retcode != NO_ERROR)
1777 device = adm_ctx.device;
1778 mutex_lock(&adm_ctx.resource->adm_mutex);
1779 peer_device = first_peer_device(device);
1780 connection = peer_device->connection;
1781 conn_reconfig_start(connection);
1783 /* if you want to reconfigure, please tear down first */
1784 if (device->state.disk > D_DISKLESS) {
1785 retcode = ERR_DISK_CONFIGURED;
1788 /* It may just now have detached because of IO error. Make sure
1789 * drbd_ldev_destroy is done already, we may end up here very fast,
1790 * e.g. if someone calls attach from the on-io-error handler,
1791 * to realize a "hot spare" feature (not that I'd recommend that) */
1792 wait_event(device->misc_wait, !test_bit(GOING_DISKLESS, &device->flags));
1794 /* make sure there is no leftover from previous force-detach attempts */
1795 clear_bit(FORCE_DETACH, &device->flags);
1796 clear_bit(WAS_IO_ERROR, &device->flags);
1797 clear_bit(WAS_READ_ERROR, &device->flags);
1799 /* and no leftover from previously aborted resync or verify, either */
1800 device->rs_total = 0;
1801 device->rs_failed = 0;
1802 atomic_set(&device->rs_pending_cnt, 0);
1804 /* allocation not in the IO path, drbdsetup context */
1805 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1807 retcode = ERR_NOMEM;
1810 spin_lock_init(&nbc->md.uuid_lock);
1812 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
1813 if (!new_disk_conf) {
1814 retcode = ERR_NOMEM;
1817 nbc->disk_conf = new_disk_conf;
1819 set_disk_conf_defaults(new_disk_conf);
1820 err = disk_conf_from_attrs(new_disk_conf, info);
1822 retcode = ERR_MANDATORY_TAG;
1823 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
1827 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1828 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
1830 new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
1832 retcode = ERR_NOMEM;
1836 if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
1837 retcode = ERR_MD_IDX_INVALID;
1842 nc = rcu_dereference(connection->net_conf);
1844 if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
1846 retcode = ERR_STONITH_AND_PROT_A;
1852 retcode = open_backing_devices(device, new_disk_conf, nbc);
1853 if (retcode != NO_ERROR)
1856 if ((nbc->backing_bdev == nbc->md_bdev) !=
1857 (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1858 new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
1859 retcode = ERR_MD_IDX_INVALID;
1863 resync_lru = lc_create("resync", drbd_bm_ext_cache,
1864 1, 61, sizeof(struct bm_extent),
1865 offsetof(struct bm_extent, lce));
1867 retcode = ERR_NOMEM;
1871 /* Read our meta data super block early.
1872 * This also sets other on-disk offsets. */
1873 retcode = drbd_md_read(device, nbc);
1874 if (retcode != NO_ERROR)
1877 sanitize_disk_conf(device, new_disk_conf, nbc);
1879 if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
1880 drbd_err(device, "max capacity %llu smaller than disk size %llu\n",
1881 (unsigned long long) drbd_get_max_capacity(nbc),
1882 (unsigned long long) new_disk_conf->disk_size);
1883 retcode = ERR_DISK_TOO_SMALL;
1887 if (new_disk_conf->meta_dev_idx < 0) {
1888 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1889 /* at least one MB, otherwise it does not make sense */
1890 min_md_device_sectors = (2<<10);
1892 max_possible_sectors = DRBD_MAX_SECTORS;
1893 min_md_device_sectors = MD_128MB_SECT * (new_disk_conf->meta_dev_idx + 1);
1896 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
1897 retcode = ERR_MD_DISK_TOO_SMALL;
1898 drbd_warn(device, "refusing attach: md-device too small, "
1899 "at least %llu sectors needed for this meta-disk type\n",
1900 (unsigned long long) min_md_device_sectors);
1904 /* Make sure the new disk is big enough
1905 * (we may currently be R_PRIMARY with no local disk...) */
1906 if (drbd_get_max_capacity(nbc) <
1907 drbd_get_capacity(device->this_bdev)) {
1908 retcode = ERR_DISK_TOO_SMALL;
1912 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1914 if (nbc->known_size > max_possible_sectors) {
1915 drbd_warn(device, "==> truncating very big lower level device "
1916 "to currently maximum possible %llu sectors <==\n",
1917 (unsigned long long) max_possible_sectors);
1918 if (new_disk_conf->meta_dev_idx >= 0)
1919 drbd_warn(device, "==>> using internal or flexible "
1920 "meta data may help <<==\n");
1923 drbd_suspend_io(device);
1924 /* also wait for the last barrier ack. */
1925 /* FIXME see also https://daiquiri.linbit/cgi-bin/bugzilla/show_bug.cgi?id=171
1926 * We need a way to either ignore barrier acks for barriers sent before a device
1927 * was attached, or a way to wait for all pending barrier acks to come in.
1928 * As barriers are counted per resource,
1929 * we'd need to suspend io on all devices of a resource.
1931 wait_event(device->misc_wait, !atomic_read(&device->ap_pending_cnt) || drbd_suspended(device));
1932 /* and for any other previously queued work */
1933 drbd_flush_workqueue(&connection->sender_work);
1935 rv = _drbd_request_state(device, NS(disk, D_ATTACHING), CS_VERBOSE);
1936 retcode = rv; /* FIXME: Type mismatch. */
1937 drbd_resume_io(device);
1938 if (rv < SS_SUCCESS)
1941 if (!get_ldev_if_state(device, D_ATTACHING))
1942 goto force_diskless;
1944 if (!device->bitmap) {
1945 if (drbd_bm_init(device)) {
1946 retcode = ERR_NOMEM;
1947 goto force_diskless_dec;
1951 if (device->state.pdsk != D_UP_TO_DATE && device->ed_uuid &&
1952 (device->state.role == R_PRIMARY || device->state.peer == R_PRIMARY) &&
1953 (device->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
1954 drbd_err(device, "Can only attach to data with current UUID=%016llX\n",
1955 (unsigned long long)device->ed_uuid);
1956 retcode = ERR_DATA_NOT_CURRENT;
1957 goto force_diskless_dec;
1960 /* Since we are diskless, fix the activity log first... */
1961 if (drbd_check_al_size(device, new_disk_conf)) {
1962 retcode = ERR_NOMEM;
1963 goto force_diskless_dec;
1966 /* Prevent shrinking of consistent devices ! */
1967 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
1968 drbd_new_dev_size(device, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
1969 drbd_warn(device, "refusing to truncate a consistent device\n");
1970 retcode = ERR_DISK_TOO_SMALL;
1971 goto force_diskless_dec;
1974 lock_all_resources();
1975 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
1976 if (retcode != NO_ERROR) {
1977 unlock_all_resources();
1978 goto force_diskless_dec;
1981 /* Reset the "barriers don't work" bits here, then force meta data to
1982 * be written, to ensure we determine if barriers are supported. */
1983 if (new_disk_conf->md_flushes)
1984 clear_bit(MD_NO_FUA, &device->flags);
1986 set_bit(MD_NO_FUA, &device->flags);
1988 /* Point of no return reached.
1989 * Devices and memory are no longer released by error cleanup below.
1990 * now device takes over responsibility, and the state engine should
1991 * clean it up somewhere. */
1992 D_ASSERT(device, device->ldev == NULL);
1994 device->resync = resync_lru;
1995 device->rs_plan_s = new_plan;
1998 new_disk_conf = NULL;
2001 drbd_resync_after_changed(device);
2002 drbd_bump_write_ordering(device->resource, device->ldev, WO_BDEV_FLUSH);
2003 unlock_all_resources();
2005 if (drbd_md_test_flag(device->ldev, MDF_CRASHED_PRIMARY))
2006 set_bit(CRASHED_PRIMARY, &device->flags);
2008 clear_bit(CRASHED_PRIMARY, &device->flags);
2010 if (drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
2011 !(device->state.role == R_PRIMARY && device->resource->susp_nod))
2012 set_bit(CRASHED_PRIMARY, &device->flags);
2014 device->send_cnt = 0;
2015 device->recv_cnt = 0;
2016 device->read_cnt = 0;
2017 device->writ_cnt = 0;
2019 drbd_reconsider_queue_parameters(device, device->ldev, NULL);
2021 /* If I am currently not R_PRIMARY,
2022 * but meta data primary indicator is set,
2023 * I just now recover from a hard crash,
2024 * and have been R_PRIMARY before that crash.
2026 * Now, if I had no connection before that crash
2027 * (have been degraded R_PRIMARY), chances are that
2028 * I won't find my peer now either.
2030 * In that case, and _only_ in that case,
2031 * we use the degr-wfc-timeout instead of the default,
2032 * so we can automatically recover from a crash of a
2033 * degraded but active "cluster" after a certain timeout.
2035 clear_bit(USE_DEGR_WFC_T, &device->flags);
2036 if (device->state.role != R_PRIMARY &&
2037 drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
2038 !drbd_md_test_flag(device->ldev, MDF_CONNECTED_IND))
2039 set_bit(USE_DEGR_WFC_T, &device->flags);
2041 dd = drbd_determine_dev_size(device, 0, NULL);
2042 if (dd <= DS_ERROR) {
2043 retcode = ERR_NOMEM_BITMAP;
2044 goto force_diskless_dec;
2045 } else if (dd == DS_GREW)
2046 set_bit(RESYNC_AFTER_NEG, &device->flags);
2048 if (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC) ||
2049 (test_bit(CRASHED_PRIMARY, &device->flags) &&
2050 drbd_md_test_flag(device->ldev, MDF_AL_DISABLED))) {
2051 drbd_info(device, "Assuming that all blocks are out of sync "
2052 "(aka FullSync)\n");
2053 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
2054 "set_n_write from attaching", BM_LOCKED_MASK)) {
2055 retcode = ERR_IO_MD_DISK;
2056 goto force_diskless_dec;
2059 if (drbd_bitmap_io(device, &drbd_bm_read,
2060 "read from attaching", BM_LOCKED_MASK)) {
2061 retcode = ERR_IO_MD_DISK;
2062 goto force_diskless_dec;
2066 if (_drbd_bm_total_weight(device) == drbd_bm_bits(device))
2067 drbd_suspend_al(device); /* IO is still suspended here... */
2069 spin_lock_irq(&device->resource->req_lock);
2070 os = drbd_read_state(device);
2072 /* If MDF_CONSISTENT is not set go into inconsistent state,
2073 otherwise investigate MDF_WasUpToDate...
2074 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
2075 otherwise into D_CONSISTENT state.
2077 if (drbd_md_test_flag(device->ldev, MDF_CONSISTENT)) {
2078 if (drbd_md_test_flag(device->ldev, MDF_WAS_UP_TO_DATE))
2079 ns.disk = D_CONSISTENT;
2081 ns.disk = D_OUTDATED;
2083 ns.disk = D_INCONSISTENT;
2086 if (drbd_md_test_flag(device->ldev, MDF_PEER_OUT_DATED))
2087 ns.pdsk = D_OUTDATED;
2090 if (ns.disk == D_CONSISTENT &&
2091 (ns.pdsk == D_OUTDATED || rcu_dereference(device->ldev->disk_conf)->fencing == FP_DONT_CARE))
2092 ns.disk = D_UP_TO_DATE;
2094 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
2095 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
2096 this point, because drbd_request_state() modifies these
2099 if (rcu_dereference(device->ldev->disk_conf)->al_updates)
2100 device->ldev->md.flags &= ~MDF_AL_DISABLED;
2102 device->ldev->md.flags |= MDF_AL_DISABLED;
2106 /* In case we are C_CONNECTED postpone any decision on the new disk
2107 state after the negotiation phase. */
2108 if (device->state.conn == C_CONNECTED) {
2109 device->new_state_tmp.i = ns.i;
2111 ns.disk = D_NEGOTIATING;
2113 /* We expect to receive up-to-date UUIDs soon.
2114 To avoid a race in receive_state, free p_uuid while
2115 holding req_lock. I.e. atomic with the state change */
2116 kfree(device->p_uuid);
2117 device->p_uuid = NULL;
2120 rv = _drbd_set_state(device, ns, CS_VERBOSE, NULL);
2121 spin_unlock_irq(&device->resource->req_lock);
2123 if (rv < SS_SUCCESS)
2124 goto force_diskless_dec;
2126 mod_timer(&device->request_timer, jiffies + HZ);
2128 if (device->state.role == R_PRIMARY)
2129 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
2131 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
2133 drbd_md_mark_dirty(device);
2134 drbd_md_sync(device);
2136 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
2138 conn_reconfig_done(connection);
2139 mutex_unlock(&adm_ctx.resource->adm_mutex);
2140 drbd_adm_finish(&adm_ctx, info, retcode);
2146 drbd_force_state(device, NS(disk, D_DISKLESS));
2147 drbd_md_sync(device);
2149 conn_reconfig_done(connection);
2151 close_backing_dev(device, nbc->md_bdev, nbc->md_bdev != nbc->backing_bdev);
2152 close_backing_dev(device, nbc->backing_bdev, true);
2155 kfree(new_disk_conf);
2156 lc_destroy(resync_lru);
2158 mutex_unlock(&adm_ctx.resource->adm_mutex);
2160 drbd_adm_finish(&adm_ctx, info, retcode);
2164 static int adm_detach(struct drbd_device *device, int force)
2166 enum drbd_state_rv retcode;
2171 set_bit(FORCE_DETACH, &device->flags);
2172 drbd_force_state(device, NS(disk, D_FAILED));
2173 retcode = SS_SUCCESS;
2177 drbd_suspend_io(device); /* so no-one is stuck in drbd_al_begin_io */
2178 buffer = drbd_md_get_buffer(device, __func__); /* make sure there is no in-flight meta-data IO */
2180 retcode = drbd_request_state(device, NS(disk, D_FAILED));
2181 drbd_md_put_buffer(device);
2182 } else /* already <= D_FAILED */
2183 retcode = SS_NOTHING_TO_DO;
2184 /* D_FAILED will transition to DISKLESS. */
2185 drbd_resume_io(device);
2186 ret = wait_event_interruptible(device->misc_wait,
2187 device->state.disk != D_FAILED);
2188 if ((int)retcode == (int)SS_IS_DISKLESS)
2189 retcode = SS_NOTHING_TO_DO;
2196 /* Detaching the disk is a process in multiple stages. First we need to lock
2197 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
2198 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
2199 * internal references as well.
2200 * Only then we have finally detached. */
2201 int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
2203 struct drbd_config_context adm_ctx;
2204 enum drbd_ret_code retcode;
2205 struct detach_parms parms = { };
2208 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
2209 if (!adm_ctx.reply_skb)
2211 if (retcode != NO_ERROR)
2214 if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
2215 err = detach_parms_from_attrs(&parms, info);
2217 retcode = ERR_MANDATORY_TAG;
2218 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
2223 mutex_lock(&adm_ctx.resource->adm_mutex);
2224 retcode = adm_detach(adm_ctx.device, parms.force_detach);
2225 mutex_unlock(&adm_ctx.resource->adm_mutex);
2227 drbd_adm_finish(&adm_ctx, info, retcode);
2231 static bool conn_resync_running(struct drbd_connection *connection)
2233 struct drbd_peer_device *peer_device;
2238 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
2239 struct drbd_device *device = peer_device->device;
2240 if (device->state.conn == C_SYNC_SOURCE ||
2241 device->state.conn == C_SYNC_TARGET ||
2242 device->state.conn == C_PAUSED_SYNC_S ||
2243 device->state.conn == C_PAUSED_SYNC_T) {
2253 static bool conn_ov_running(struct drbd_connection *connection)
2255 struct drbd_peer_device *peer_device;
2260 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
2261 struct drbd_device *device = peer_device->device;
2262 if (device->state.conn == C_VERIFY_S ||
2263 device->state.conn == C_VERIFY_T) {
2273 static enum drbd_ret_code
2274 _check_net_options(struct drbd_connection *connection, struct net_conf *old_net_conf, struct net_conf *new_net_conf)
2276 struct drbd_peer_device *peer_device;
2279 if (old_net_conf && connection->cstate == C_WF_REPORT_PARAMS && connection->agreed_pro_version < 100) {
2280 if (new_net_conf->wire_protocol != old_net_conf->wire_protocol)
2281 return ERR_NEED_APV_100;
2283 if (new_net_conf->two_primaries != old_net_conf->two_primaries)
2284 return ERR_NEED_APV_100;
2286 if (strcmp(new_net_conf->integrity_alg, old_net_conf->integrity_alg))
2287 return ERR_NEED_APV_100;
2290 if (!new_net_conf->two_primaries &&
2291 conn_highest_role(connection) == R_PRIMARY &&
2292 conn_highest_peer(connection) == R_PRIMARY)
2293 return ERR_NEED_ALLOW_TWO_PRI;
2295 if (new_net_conf->two_primaries &&
2296 (new_net_conf->wire_protocol != DRBD_PROT_C))
2297 return ERR_NOT_PROTO_C;
2299 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2300 struct drbd_device *device = peer_device->device;
2301 if (get_ldev(device)) {
2302 enum drbd_fencing_p fp = rcu_dereference(device->ldev->disk_conf)->fencing;
2304 if (new_net_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
2305 return ERR_STONITH_AND_PROT_A;
2307 if (device->state.role == R_PRIMARY && new_net_conf->discard_my_data)
2308 return ERR_DISCARD_IMPOSSIBLE;
2311 if (new_net_conf->on_congestion != OC_BLOCK && new_net_conf->wire_protocol != DRBD_PROT_A)
2312 return ERR_CONG_NOT_PROTO_A;
2317 static enum drbd_ret_code
2318 check_net_options(struct drbd_connection *connection, struct net_conf *new_net_conf)
2320 static enum drbd_ret_code rv;
2321 struct drbd_peer_device *peer_device;
2325 rv = _check_net_options(connection, rcu_dereference(connection->net_conf), new_net_conf);
2328 /* connection->peer_devices protected by genl_lock() here */
2329 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2330 struct drbd_device *device = peer_device->device;
2331 if (!device->bitmap) {
2332 if (drbd_bm_init(device))
2341 struct crypto_ahash *verify_tfm;
2342 struct crypto_ahash *csums_tfm;
2343 struct crypto_shash *cram_hmac_tfm;
2344 struct crypto_ahash *integrity_tfm;
2348 alloc_shash(struct crypto_shash **tfm, char *tfm_name, int err_alg)
2353 *tfm = crypto_alloc_shash(tfm_name, 0, 0);
2363 alloc_ahash(struct crypto_ahash **tfm, char *tfm_name, int err_alg)
2368 *tfm = crypto_alloc_ahash(tfm_name, 0, CRYPTO_ALG_ASYNC);
2377 static enum drbd_ret_code
2378 alloc_crypto(struct crypto *crypto, struct net_conf *new_net_conf)
2380 char hmac_name[CRYPTO_MAX_ALG_NAME];
2381 enum drbd_ret_code rv;
2383 rv = alloc_ahash(&crypto->csums_tfm, new_net_conf->csums_alg,
2387 rv = alloc_ahash(&crypto->verify_tfm, new_net_conf->verify_alg,
2391 rv = alloc_ahash(&crypto->integrity_tfm, new_net_conf->integrity_alg,
2395 if (new_net_conf->cram_hmac_alg[0] != 0) {
2396 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
2397 new_net_conf->cram_hmac_alg);
2399 rv = alloc_shash(&crypto->cram_hmac_tfm, hmac_name,
2406 static void free_crypto(struct crypto *crypto)
2408 crypto_free_shash(crypto->cram_hmac_tfm);
2409 crypto_free_ahash(crypto->integrity_tfm);
2410 crypto_free_ahash(crypto->csums_tfm);
2411 crypto_free_ahash(crypto->verify_tfm);
2414 int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
2416 struct drbd_config_context adm_ctx;
2417 enum drbd_ret_code retcode;
2418 struct drbd_connection *connection;
2419 struct net_conf *old_net_conf, *new_net_conf = NULL;
2421 int ovr; /* online verify running */
2422 int rsr; /* re-sync running */
2423 struct crypto crypto = { };
2425 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_CONNECTION);
2426 if (!adm_ctx.reply_skb)
2428 if (retcode != NO_ERROR)
2431 connection = adm_ctx.connection;
2432 mutex_lock(&adm_ctx.resource->adm_mutex);
2434 new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
2435 if (!new_net_conf) {
2436 retcode = ERR_NOMEM;
2440 conn_reconfig_start(connection);
2442 mutex_lock(&connection->data.mutex);
2443 mutex_lock(&connection->resource->conf_update);
2444 old_net_conf = connection->net_conf;
2446 if (!old_net_conf) {
2447 drbd_msg_put_info(adm_ctx.reply_skb, "net conf missing, try connect");
2448 retcode = ERR_INVALID_REQUEST;
2452 *new_net_conf = *old_net_conf;
2453 if (should_set_defaults(info))
2454 set_net_conf_defaults(new_net_conf);
2456 err = net_conf_from_attrs_for_change(new_net_conf, info);
2457 if (err && err != -ENOMSG) {
2458 retcode = ERR_MANDATORY_TAG;
2459 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
2463 retcode = check_net_options(connection, new_net_conf);
2464 if (retcode != NO_ERROR)
2467 /* re-sync running */
2468 rsr = conn_resync_running(connection);
2469 if (rsr && strcmp(new_net_conf->csums_alg, old_net_conf->csums_alg)) {
2470 retcode = ERR_CSUMS_RESYNC_RUNNING;
2474 /* online verify running */
2475 ovr = conn_ov_running(connection);
2476 if (ovr && strcmp(new_net_conf->verify_alg, old_net_conf->verify_alg)) {
2477 retcode = ERR_VERIFY_RUNNING;
2481 retcode = alloc_crypto(&crypto, new_net_conf);
2482 if (retcode != NO_ERROR)
2485 rcu_assign_pointer(connection->net_conf, new_net_conf);
2488 crypto_free_ahash(connection->csums_tfm);
2489 connection->csums_tfm = crypto.csums_tfm;
2490 crypto.csums_tfm = NULL;
2493 crypto_free_ahash(connection->verify_tfm);
2494 connection->verify_tfm = crypto.verify_tfm;
2495 crypto.verify_tfm = NULL;
2498 crypto_free_ahash(connection->integrity_tfm);
2499 connection->integrity_tfm = crypto.integrity_tfm;
2500 if (connection->cstate >= C_WF_REPORT_PARAMS && connection->agreed_pro_version >= 100)
2501 /* Do this without trying to take connection->data.mutex again. */
2502 __drbd_send_protocol(connection, P_PROTOCOL_UPDATE);
2504 crypto_free_shash(connection->cram_hmac_tfm);
2505 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
2507 mutex_unlock(&connection->resource->conf_update);
2508 mutex_unlock(&connection->data.mutex);
2510 kfree(old_net_conf);
2512 if (connection->cstate >= C_WF_REPORT_PARAMS) {
2513 struct drbd_peer_device *peer_device;
2516 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
2517 drbd_send_sync_param(peer_device);
2523 mutex_unlock(&connection->resource->conf_update);
2524 mutex_unlock(&connection->data.mutex);
2525 free_crypto(&crypto);
2526 kfree(new_net_conf);
2528 conn_reconfig_done(connection);
2530 mutex_unlock(&adm_ctx.resource->adm_mutex);
2532 drbd_adm_finish(&adm_ctx, info, retcode);
2536 static void connection_to_info(struct connection_info *info,
2537 struct drbd_connection *connection)
2539 info->conn_connection_state = connection->cstate;
2540 info->conn_role = conn_highest_peer(connection);
2543 static void peer_device_to_info(struct peer_device_info *info,
2544 struct drbd_peer_device *peer_device)
2546 struct drbd_device *device = peer_device->device;
2548 info->peer_repl_state =
2549 max_t(enum drbd_conns, C_WF_REPORT_PARAMS, device->state.conn);
2550 info->peer_disk_state = device->state.pdsk;
2551 info->peer_resync_susp_user = device->state.user_isp;
2552 info->peer_resync_susp_peer = device->state.peer_isp;
2553 info->peer_resync_susp_dependency = device->state.aftr_isp;
2556 int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
2558 struct connection_info connection_info;
2559 enum drbd_notification_type flags;
2560 unsigned int peer_devices = 0;
2561 struct drbd_config_context adm_ctx;
2562 struct drbd_peer_device *peer_device;
2563 struct net_conf *old_net_conf, *new_net_conf = NULL;
2564 struct crypto crypto = { };
2565 struct drbd_resource *resource;
2566 struct drbd_connection *connection;
2567 enum drbd_ret_code retcode;
2571 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
2573 if (!adm_ctx.reply_skb)
2575 if (retcode != NO_ERROR)
2577 if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
2578 drbd_msg_put_info(adm_ctx.reply_skb, "connection endpoint(s) missing");
2579 retcode = ERR_INVALID_REQUEST;
2583 /* No need for _rcu here. All reconfiguration is
2584 * strictly serialized on genl_lock(). We are protected against
2585 * concurrent reconfiguration/addition/deletion */
2586 for_each_resource(resource, &drbd_resources) {
2587 for_each_connection(connection, resource) {
2588 if (nla_len(adm_ctx.my_addr) == connection->my_addr_len &&
2589 !memcmp(nla_data(adm_ctx.my_addr), &connection->my_addr,
2590 connection->my_addr_len)) {
2591 retcode = ERR_LOCAL_ADDR;
2595 if (nla_len(adm_ctx.peer_addr) == connection->peer_addr_len &&
2596 !memcmp(nla_data(adm_ctx.peer_addr), &connection->peer_addr,
2597 connection->peer_addr_len)) {
2598 retcode = ERR_PEER_ADDR;
2604 mutex_lock(&adm_ctx.resource->adm_mutex);
2605 connection = first_connection(adm_ctx.resource);
2606 conn_reconfig_start(connection);
2608 if (connection->cstate > C_STANDALONE) {
2609 retcode = ERR_NET_CONFIGURED;
2613 /* allocation not in the IO path, drbdsetup / netlink process context */
2614 new_net_conf = kzalloc(sizeof(*new_net_conf), GFP_KERNEL);
2615 if (!new_net_conf) {
2616 retcode = ERR_NOMEM;
2620 set_net_conf_defaults(new_net_conf);
2622 err = net_conf_from_attrs(new_net_conf, info);
2623 if (err && err != -ENOMSG) {
2624 retcode = ERR_MANDATORY_TAG;
2625 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
2629 retcode = check_net_options(connection, new_net_conf);
2630 if (retcode != NO_ERROR)
2633 retcode = alloc_crypto(&crypto, new_net_conf);
2634 if (retcode != NO_ERROR)
2637 ((char *)new_net_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
2639 drbd_flush_workqueue(&connection->sender_work);
2641 mutex_lock(&adm_ctx.resource->conf_update);
2642 old_net_conf = connection->net_conf;
2644 retcode = ERR_NET_CONFIGURED;
2645 mutex_unlock(&adm_ctx.resource->conf_update);
2648 rcu_assign_pointer(connection->net_conf, new_net_conf);
2650 conn_free_crypto(connection);
2651 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
2652 connection->integrity_tfm = crypto.integrity_tfm;
2653 connection->csums_tfm = crypto.csums_tfm;
2654 connection->verify_tfm = crypto.verify_tfm;
2656 connection->my_addr_len = nla_len(adm_ctx.my_addr);
2657 memcpy(&connection->my_addr, nla_data(adm_ctx.my_addr), connection->my_addr_len);
2658 connection->peer_addr_len = nla_len(adm_ctx.peer_addr);
2659 memcpy(&connection->peer_addr, nla_data(adm_ctx.peer_addr), connection->peer_addr_len);
2661 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2665 connection_to_info(&connection_info, connection);
2666 flags = (peer_devices--) ? NOTIFY_CONTINUES : 0;
2667 mutex_lock(¬ification_mutex);
2668 notify_connection_state(NULL, 0, connection, &connection_info, NOTIFY_CREATE | flags);
2669 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2670 struct peer_device_info peer_device_info;
2672 peer_device_to_info(&peer_device_info, peer_device);
2673 flags = (peer_devices--) ? NOTIFY_CONTINUES : 0;
2674 notify_peer_device_state(NULL, 0, peer_device, &peer_device_info, NOTIFY_CREATE | flags);
2676 mutex_unlock(¬ification_mutex);
2677 mutex_unlock(&adm_ctx.resource->conf_update);
2680 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2681 struct drbd_device *device = peer_device->device;
2682 device->send_cnt = 0;
2683 device->recv_cnt = 0;
2687 retcode = conn_request_state(connection, NS(conn, C_UNCONNECTED), CS_VERBOSE);
2689 conn_reconfig_done(connection);
2690 mutex_unlock(&adm_ctx.resource->adm_mutex);
2691 drbd_adm_finish(&adm_ctx, info, retcode);
2695 free_crypto(&crypto);
2696 kfree(new_net_conf);
2698 conn_reconfig_done(connection);
2699 mutex_unlock(&adm_ctx.resource->adm_mutex);
2701 drbd_adm_finish(&adm_ctx, info, retcode);
2705 static enum drbd_state_rv conn_try_disconnect(struct drbd_connection *connection, bool force)
2707 enum drbd_state_rv rv;
2709 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
2710 force ? CS_HARD : 0);
2713 case SS_NOTHING_TO_DO:
2715 case SS_ALREADY_STANDALONE:
2717 case SS_PRIMARY_NOP:
2718 /* Our state checking code wants to see the peer outdated. */
2719 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING, pdsk, D_OUTDATED), 0);
2721 if (rv == SS_OUTDATE_WO_CONN) /* lost connection before graceful disconnect succeeded */
2722 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_VERBOSE);
2725 case SS_CW_FAILED_BY_PEER:
2726 /* The peer probably wants to see us outdated. */
2727 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING,
2728 disk, D_OUTDATED), 0);
2729 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
2730 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
2735 /* no special handling necessary */
2738 if (rv >= SS_SUCCESS) {
2739 enum drbd_state_rv rv2;
2740 /* No one else can reconfigure the network while I am here.
2741 * The state handling only uses drbd_thread_stop_nowait(),
2742 * we want to really wait here until the receiver is no more.
2744 drbd_thread_stop(&connection->receiver);
2746 /* Race breaker. This additional state change request may be
2747 * necessary, if this was a forced disconnect during a receiver
2748 * restart. We may have "killed" the receiver thread just
2749 * after drbd_receiver() returned. Typically, we should be
2750 * C_STANDALONE already, now, and this becomes a no-op.
2752 rv2 = conn_request_state(connection, NS(conn, C_STANDALONE),
2753 CS_VERBOSE | CS_HARD);
2754 if (rv2 < SS_SUCCESS)
2755 drbd_err(connection,
2756 "unexpected rv2=%d in conn_try_disconnect()\n",
2758 /* Unlike in DRBD 9, the state engine has generated
2759 * NOTIFY_DESTROY events before clearing connection->net_conf. */
2764 int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
2766 struct drbd_config_context adm_ctx;
2767 struct disconnect_parms parms;
2768 struct drbd_connection *connection;
2769 enum drbd_state_rv rv;
2770 enum drbd_ret_code retcode;
2773 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_CONNECTION);
2774 if (!adm_ctx.reply_skb)
2776 if (retcode != NO_ERROR)
2779 connection = adm_ctx.connection;
2780 memset(&parms, 0, sizeof(parms));
2781 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
2782 err = disconnect_parms_from_attrs(&parms, info);
2784 retcode = ERR_MANDATORY_TAG;
2785 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
2790 mutex_lock(&adm_ctx.resource->adm_mutex);
2791 rv = conn_try_disconnect(connection, parms.force_disconnect);
2792 if (rv < SS_SUCCESS)
2793 retcode = rv; /* FIXME: Type mismatch. */
2796 mutex_unlock(&adm_ctx.resource->adm_mutex);
2798 drbd_adm_finish(&adm_ctx, info, retcode);
2802 void resync_after_online_grow(struct drbd_device *device)
2804 int iass; /* I am sync source */
2806 drbd_info(device, "Resync of new storage after online grow\n");
2807 if (device->state.role != device->state.peer)
2808 iass = (device->state.role == R_PRIMARY);
2810 iass = test_bit(RESOLVE_CONFLICTS, &first_peer_device(device)->connection->flags);
2813 drbd_start_resync(device, C_SYNC_SOURCE);
2815 _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
2818 int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
2820 struct drbd_config_context adm_ctx;
2821 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
2822 struct resize_parms rs;
2823 struct drbd_device *device;
2824 enum drbd_ret_code retcode;
2825 enum determine_dev_size dd;
2826 bool change_al_layout = false;
2827 enum dds_flags ddsf;
2831 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
2832 if (!adm_ctx.reply_skb)
2834 if (retcode != NO_ERROR)
2837 mutex_lock(&adm_ctx.resource->adm_mutex);
2838 device = adm_ctx.device;
2839 if (!get_ldev(device)) {
2840 retcode = ERR_NO_DISK;
2844 memset(&rs, 0, sizeof(struct resize_parms));
2845 rs.al_stripes = device->ldev->md.al_stripes;
2846 rs.al_stripe_size = device->ldev->md.al_stripe_size_4k * 4;
2847 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
2848 err = resize_parms_from_attrs(&rs, info);
2850 retcode = ERR_MANDATORY_TAG;
2851 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
2856 if (device->state.conn > C_CONNECTED) {
2857 retcode = ERR_RESIZE_RESYNC;
2861 if (device->state.role == R_SECONDARY &&
2862 device->state.peer == R_SECONDARY) {
2863 retcode = ERR_NO_PRIMARY;
2867 if (rs.no_resync && first_peer_device(device)->connection->agreed_pro_version < 93) {
2868 retcode = ERR_NEED_APV_93;
2873 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
2875 if (u_size != (sector_t)rs.resize_size) {
2876 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
2877 if (!new_disk_conf) {
2878 retcode = ERR_NOMEM;
2883 if (device->ldev->md.al_stripes != rs.al_stripes ||
2884 device->ldev->md.al_stripe_size_4k != rs.al_stripe_size / 4) {
2885 u32 al_size_k = rs.al_stripes * rs.al_stripe_size;
2887 if (al_size_k > (16 * 1024 * 1024)) {
2888 retcode = ERR_MD_LAYOUT_TOO_BIG;
2892 if (al_size_k < MD_32kB_SECT/2) {
2893 retcode = ERR_MD_LAYOUT_TOO_SMALL;
2897 if (device->state.conn != C_CONNECTED && !rs.resize_force) {
2898 retcode = ERR_MD_LAYOUT_CONNECTED;
2902 change_al_layout = true;
2905 if (device->ldev->known_size != drbd_get_capacity(device->ldev->backing_bdev))
2906 device->ldev->known_size = drbd_get_capacity(device->ldev->backing_bdev);
2908 if (new_disk_conf) {
2909 mutex_lock(&device->resource->conf_update);
2910 old_disk_conf = device->ldev->disk_conf;
2911 *new_disk_conf = *old_disk_conf;
2912 new_disk_conf->disk_size = (sector_t)rs.resize_size;
2913 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
2914 mutex_unlock(&device->resource->conf_update);
2916 kfree(old_disk_conf);
2917 new_disk_conf = NULL;
2920 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
2921 dd = drbd_determine_dev_size(device, ddsf, change_al_layout ? &rs : NULL);
2922 drbd_md_sync(device);
2924 if (dd == DS_ERROR) {
2925 retcode = ERR_NOMEM_BITMAP;
2927 } else if (dd == DS_ERROR_SPACE_MD) {
2928 retcode = ERR_MD_LAYOUT_NO_FIT;
2930 } else if (dd == DS_ERROR_SHRINK) {
2931 retcode = ERR_IMPLICIT_SHRINK;
2935 if (device->state.conn == C_CONNECTED) {
2937 set_bit(RESIZE_PENDING, &device->flags);
2939 drbd_send_uuids(first_peer_device(device));
2940 drbd_send_sizes(first_peer_device(device), 1, ddsf);
2944 mutex_unlock(&adm_ctx.resource->adm_mutex);
2946 drbd_adm_finish(&adm_ctx, info, retcode);
2951 kfree(new_disk_conf);
2955 int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
2957 struct drbd_config_context adm_ctx;
2958 enum drbd_ret_code retcode;
2959 struct res_opts res_opts;
2962 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
2963 if (!adm_ctx.reply_skb)
2965 if (retcode != NO_ERROR)
2968 res_opts = adm_ctx.resource->res_opts;
2969 if (should_set_defaults(info))
2970 set_res_opts_defaults(&res_opts);
2972 err = res_opts_from_attrs(&res_opts, info);
2973 if (err && err != -ENOMSG) {
2974 retcode = ERR_MANDATORY_TAG;
2975 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
2979 mutex_lock(&adm_ctx.resource->adm_mutex);
2980 err = set_resource_options(adm_ctx.resource, &res_opts);
2982 retcode = ERR_INVALID_REQUEST;
2984 retcode = ERR_NOMEM;
2986 mutex_unlock(&adm_ctx.resource->adm_mutex);
2989 drbd_adm_finish(&adm_ctx, info, retcode);
2993 int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
2995 struct drbd_config_context adm_ctx;
2996 struct drbd_device *device;
2997 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2999 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3000 if (!adm_ctx.reply_skb)
3002 if (retcode != NO_ERROR)
3005 device = adm_ctx.device;
3006 if (!get_ldev(device)) {
3007 retcode = ERR_NO_DISK;
3011 mutex_lock(&adm_ctx.resource->adm_mutex);
3013 /* If there is still bitmap IO pending, probably because of a previous
3014 * resync just being finished, wait for it before requesting a new resync.
3015 * Also wait for it's after_state_ch(). */
3016 drbd_suspend_io(device);
3017 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
3018 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
3020 /* If we happen to be C_STANDALONE R_SECONDARY, just change to
3021 * D_INCONSISTENT, and set all bits in the bitmap. Otherwise,
3022 * try to start a resync handshake as sync target for full sync.
3024 if (device->state.conn == C_STANDALONE && device->state.role == R_SECONDARY) {
3025 retcode = drbd_request_state(device, NS(disk, D_INCONSISTENT));
3026 if (retcode >= SS_SUCCESS) {
3027 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
3028 "set_n_write from invalidate", BM_LOCKED_MASK))
3029 retcode = ERR_IO_MD_DISK;
3032 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_T));
3033 drbd_resume_io(device);
3034 mutex_unlock(&adm_ctx.resource->adm_mutex);
3037 drbd_adm_finish(&adm_ctx, info, retcode);
3041 static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
3042 union drbd_state mask, union drbd_state val)
3044 struct drbd_config_context adm_ctx;
3045 enum drbd_ret_code retcode;
3047 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3048 if (!adm_ctx.reply_skb)
3050 if (retcode != NO_ERROR)
3053 mutex_lock(&adm_ctx.resource->adm_mutex);
3054 retcode = drbd_request_state(adm_ctx.device, mask, val);
3055 mutex_unlock(&adm_ctx.resource->adm_mutex);
3057 drbd_adm_finish(&adm_ctx, info, retcode);
3061 static int drbd_bmio_set_susp_al(struct drbd_device *device) __must_hold(local)
3065 rv = drbd_bmio_set_n_write(device);
3066 drbd_suspend_al(device);
3070 int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
3072 struct drbd_config_context adm_ctx;
3073 int retcode; /* drbd_ret_code, drbd_state_rv */
3074 struct drbd_device *device;
3076 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3077 if (!adm_ctx.reply_skb)
3079 if (retcode != NO_ERROR)
3082 device = adm_ctx.device;
3083 if (!get_ldev(device)) {
3084 retcode = ERR_NO_DISK;
3088 mutex_lock(&adm_ctx.resource->adm_mutex);
3090 /* If there is still bitmap IO pending, probably because of a previous
3091 * resync just being finished, wait for it before requesting a new resync.
3092 * Also wait for it's after_state_ch(). */
3093 drbd_suspend_io(device);
3094 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
3095 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
3097 /* If we happen to be C_STANDALONE R_PRIMARY, just set all bits
3098 * in the bitmap. Otherwise, try to start a resync handshake
3099 * as sync source for full sync.
3101 if (device->state.conn == C_STANDALONE && device->state.role == R_PRIMARY) {
3102 /* The peer will get a resync upon connect anyways. Just make that
3103 into a full resync. */
3104 retcode = drbd_request_state(device, NS(pdsk, D_INCONSISTENT));
3105 if (retcode >= SS_SUCCESS) {
3106 if (drbd_bitmap_io(device, &drbd_bmio_set_susp_al,
3107 "set_n_write from invalidate_peer",
3108 BM_LOCKED_SET_ALLOWED))
3109 retcode = ERR_IO_MD_DISK;
3112 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_S));
3113 drbd_resume_io(device);
3114 mutex_unlock(&adm_ctx.resource->adm_mutex);
3117 drbd_adm_finish(&adm_ctx, info, retcode);
3121 int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
3123 struct drbd_config_context adm_ctx;
3124 enum drbd_ret_code retcode;
3126 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3127 if (!adm_ctx.reply_skb)
3129 if (retcode != NO_ERROR)
3132 mutex_lock(&adm_ctx.resource->adm_mutex);
3133 if (drbd_request_state(adm_ctx.device, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
3134 retcode = ERR_PAUSE_IS_SET;
3135 mutex_unlock(&adm_ctx.resource->adm_mutex);
3137 drbd_adm_finish(&adm_ctx, info, retcode);
3141 int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
3143 struct drbd_config_context adm_ctx;
3144 union drbd_dev_state s;
3145 enum drbd_ret_code retcode;
3147 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3148 if (!adm_ctx.reply_skb)
3150 if (retcode != NO_ERROR)
3153 mutex_lock(&adm_ctx.resource->adm_mutex);
3154 if (drbd_request_state(adm_ctx.device, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
3155 s = adm_ctx.device->state;
3156 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
3157 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
3158 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
3160 retcode = ERR_PAUSE_IS_CLEAR;
3163 mutex_unlock(&adm_ctx.resource->adm_mutex);
3165 drbd_adm_finish(&adm_ctx, info, retcode);
3169 int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
3171 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
3174 int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
3176 struct drbd_config_context adm_ctx;
3177 struct drbd_device *device;
3178 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
3180 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3181 if (!adm_ctx.reply_skb)
3183 if (retcode != NO_ERROR)
3186 mutex_lock(&adm_ctx.resource->adm_mutex);
3187 device = adm_ctx.device;
3188 if (test_bit(NEW_CUR_UUID, &device->flags)) {
3189 if (get_ldev_if_state(device, D_ATTACHING)) {
3190 drbd_uuid_new_current(device);
3193 /* This is effectively a multi-stage "forced down".
3194 * The NEW_CUR_UUID bit is supposedly only set, if we
3195 * lost the replication connection, and are configured
3196 * to freeze IO and wait for some fence-peer handler.
3197 * So we still don't have a replication connection.
3198 * And now we don't have a local disk either. After
3199 * resume, we will fail all pending and new IO, because
3200 * we don't have any data anymore. Which means we will
3201 * eventually be able to terminate all users of this
3202 * device, and then take it down. By bumping the
3203 * "effective" data uuid, we make sure that you really
3204 * need to tear down before you reconfigure, we will
3205 * the refuse to re-connect or re-attach (because no
3206 * matching real data uuid exists).
3209 get_random_bytes(&val, sizeof(u64));
3210 drbd_set_ed_uuid(device, val);
3211 drbd_warn(device, "Resumed without access to data; please tear down before attempting to re-configure.\n");
3213 clear_bit(NEW_CUR_UUID, &device->flags);
3215 drbd_suspend_io(device);
3216 retcode = drbd_request_state(device, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
3217 if (retcode == SS_SUCCESS) {
3218 if (device->state.conn < C_CONNECTED)
3219 tl_clear(first_peer_device(device)->connection);
3220 if (device->state.disk == D_DISKLESS || device->state.disk == D_FAILED)
3221 tl_restart(first_peer_device(device)->connection, FAIL_FROZEN_DISK_IO);
3223 drbd_resume_io(device);
3224 mutex_unlock(&adm_ctx.resource->adm_mutex);
3226 drbd_adm_finish(&adm_ctx, info, retcode);
3230 int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
3232 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
3235 static int nla_put_drbd_cfg_context(struct sk_buff *skb,
3236 struct drbd_resource *resource,
3237 struct drbd_connection *connection,
3238 struct drbd_device *device)
3241 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
3243 goto nla_put_failure;
3245 nla_put_u32(skb, T_ctx_volume, device->vnr))
3246 goto nla_put_failure;
3247 if (nla_put_string(skb, T_ctx_resource_name, resource->name))
3248 goto nla_put_failure;
3250 if (connection->my_addr_len &&
3251 nla_put(skb, T_ctx_my_addr, connection->my_addr_len, &connection->my_addr))
3252 goto nla_put_failure;
3253 if (connection->peer_addr_len &&
3254 nla_put(skb, T_ctx_peer_addr, connection->peer_addr_len, &connection->peer_addr))
3255 goto nla_put_failure;
3257 nla_nest_end(skb, nla);
3262 nla_nest_cancel(skb, nla);
3267 * The generic netlink dump callbacks are called outside the genl_lock(), so
3268 * they cannot use the simple attribute parsing code which uses global
3271 static struct nlattr *find_cfg_context_attr(const struct nlmsghdr *nlh, int attr)
3273 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
3274 const int maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
3277 nla = nla_find(nlmsg_attrdata(nlh, hdrlen), nlmsg_attrlen(nlh, hdrlen),
3278 DRBD_NLA_CFG_CONTEXT);
3281 return drbd_nla_find_nested(maxtype, nla, __nla_type(attr));
3284 static void resource_to_info(struct resource_info *, struct drbd_resource *);
3286 int drbd_adm_dump_resources(struct sk_buff *skb, struct netlink_callback *cb)
3288 struct drbd_genlmsghdr *dh;
3289 struct drbd_resource *resource;
3290 struct resource_info resource_info;
3291 struct resource_statistics resource_statistics;
3296 for_each_resource_rcu(resource, &drbd_resources)
3297 if (resource == (struct drbd_resource *)cb->args[0])
3298 goto found_resource;
3299 err = 0; /* resource was probably deleted */
3302 resource = list_entry(&drbd_resources,
3303 struct drbd_resource, resources);
3306 list_for_each_entry_continue_rcu(resource, &drbd_resources, resources) {
3313 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3314 cb->nlh->nlmsg_seq, &drbd_genl_family,
3315 NLM_F_MULTI, DRBD_ADM_GET_RESOURCES);
3320 dh->ret_code = NO_ERROR;
3321 err = nla_put_drbd_cfg_context(skb, resource, NULL, NULL);
3324 err = res_opts_to_skb(skb, &resource->res_opts, !capable(CAP_SYS_ADMIN));
3327 resource_to_info(&resource_info, resource);
3328 err = resource_info_to_skb(skb, &resource_info, !capable(CAP_SYS_ADMIN));
3331 resource_statistics.res_stat_write_ordering = resource->write_ordering;
3332 err = resource_statistics_to_skb(skb, &resource_statistics, !capable(CAP_SYS_ADMIN));
3335 cb->args[0] = (long)resource;
3336 genlmsg_end(skb, dh);
3346 static void device_to_statistics(struct device_statistics *s,
3347 struct drbd_device *device)
3349 memset(s, 0, sizeof(*s));
3350 s->dev_upper_blocked = !may_inc_ap_bio(device);
3351 if (get_ldev(device)) {
3352 struct drbd_md *md = &device->ldev->md;
3353 u64 *history_uuids = (u64 *)s->history_uuids;
3354 struct request_queue *q;
3357 spin_lock_irq(&md->uuid_lock);
3358 s->dev_current_uuid = md->uuid[UI_CURRENT];
3359 BUILD_BUG_ON(sizeof(s->history_uuids) < UI_HISTORY_END - UI_HISTORY_START + 1);
3360 for (n = 0; n < UI_HISTORY_END - UI_HISTORY_START + 1; n++)
3361 history_uuids[n] = md->uuid[UI_HISTORY_START + n];
3362 for (; n < HISTORY_UUIDS; n++)
3363 history_uuids[n] = 0;
3364 s->history_uuids_len = HISTORY_UUIDS;
3365 spin_unlock_irq(&md->uuid_lock);
3367 s->dev_disk_flags = md->flags;
3368 q = bdev_get_queue(device->ldev->backing_bdev);
3369 s->dev_lower_blocked =
3370 bdi_congested(&q->backing_dev_info,
3371 (1 << WB_async_congested) |
3372 (1 << WB_sync_congested));
3375 s->dev_size = drbd_get_capacity(device->this_bdev);
3376 s->dev_read = device->read_cnt;
3377 s->dev_write = device->writ_cnt;
3378 s->dev_al_writes = device->al_writ_cnt;
3379 s->dev_bm_writes = device->bm_writ_cnt;
3380 s->dev_upper_pending = atomic_read(&device->ap_bio_cnt);
3381 s->dev_lower_pending = atomic_read(&device->local_cnt);
3382 s->dev_al_suspended = test_bit(AL_SUSPENDED, &device->flags);
3383 s->dev_exposed_data_uuid = device->ed_uuid;
3386 static int put_resource_in_arg0(struct netlink_callback *cb, int holder_nr)
3389 struct drbd_resource *resource =
3390 (struct drbd_resource *)cb->args[0];
3391 kref_put(&resource->kref, drbd_destroy_resource);
3397 int drbd_adm_dump_devices_done(struct netlink_callback *cb) {
3398 return put_resource_in_arg0(cb, 7);
3401 static void device_to_info(struct device_info *, struct drbd_device *);
3403 int drbd_adm_dump_devices(struct sk_buff *skb, struct netlink_callback *cb)
3405 struct nlattr *resource_filter;
3406 struct drbd_resource *resource;
3407 struct drbd_device *uninitialized_var(device);
3408 int minor, err, retcode;
3409 struct drbd_genlmsghdr *dh;
3410 struct device_info device_info;
3411 struct device_statistics device_statistics;
3412 struct idr *idr_to_search;
3414 resource = (struct drbd_resource *)cb->args[0];
3415 if (!cb->args[0] && !cb->args[1]) {
3416 resource_filter = find_cfg_context_attr(cb->nlh, T_ctx_resource_name);
3417 if (resource_filter) {
3418 retcode = ERR_RES_NOT_KNOWN;
3419 resource = drbd_find_resource(nla_data(resource_filter));
3422 cb->args[0] = (long)resource;
3427 minor = cb->args[1];
3428 idr_to_search = resource ? &resource->devices : &drbd_devices;
3429 device = idr_get_next(idr_to_search, &minor);
3434 idr_for_each_entry_continue(idr_to_search, device, minor) {
3436 goto put_result; /* only one iteration */
3439 goto out; /* no more devices */
3442 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3443 cb->nlh->nlmsg_seq, &drbd_genl_family,
3444 NLM_F_MULTI, DRBD_ADM_GET_DEVICES);
3448 dh->ret_code = retcode;
3450 if (retcode == NO_ERROR) {
3451 dh->minor = device->minor;
3452 err = nla_put_drbd_cfg_context(skb, device->resource, NULL, device);
3455 if (get_ldev(device)) {
3456 struct disk_conf *disk_conf =
3457 rcu_dereference(device->ldev->disk_conf);
3459 err = disk_conf_to_skb(skb, disk_conf, !capable(CAP_SYS_ADMIN));
3464 device_to_info(&device_info, device);
3465 err = device_info_to_skb(skb, &device_info, !capable(CAP_SYS_ADMIN));
3469 device_to_statistics(&device_statistics, device);
3470 err = device_statistics_to_skb(skb, &device_statistics, !capable(CAP_SYS_ADMIN));
3473 cb->args[1] = minor + 1;
3475 genlmsg_end(skb, dh);
3485 int drbd_adm_dump_connections_done(struct netlink_callback *cb)
3487 return put_resource_in_arg0(cb, 6);
3490 enum { SINGLE_RESOURCE, ITERATE_RESOURCES };
3492 int drbd_adm_dump_connections(struct sk_buff *skb, struct netlink_callback *cb)
3494 struct nlattr *resource_filter;
3495 struct drbd_resource *resource = NULL, *next_resource;
3496 struct drbd_connection *uninitialized_var(connection);
3497 int err = 0, retcode;
3498 struct drbd_genlmsghdr *dh;
3499 struct connection_info connection_info;
3500 struct connection_statistics connection_statistics;
3503 resource = (struct drbd_resource *)cb->args[0];
3505 resource_filter = find_cfg_context_attr(cb->nlh, T_ctx_resource_name);
3506 if (resource_filter) {
3507 retcode = ERR_RES_NOT_KNOWN;
3508 resource = drbd_find_resource(nla_data(resource_filter));
3511 cb->args[0] = (long)resource;
3512 cb->args[1] = SINGLE_RESOURCE;
3516 if (list_empty(&drbd_resources))
3518 resource = list_first_entry(&drbd_resources, struct drbd_resource, resources);
3519 kref_get(&resource->kref);
3520 cb->args[0] = (long)resource;
3521 cb->args[1] = ITERATE_RESOURCES;
3526 mutex_lock(&resource->conf_update);
3529 for_each_connection_rcu(connection, resource)
3530 if (connection == (struct drbd_connection *)cb->args[2])
3531 goto found_connection;
3532 /* connection was probably deleted */
3533 goto no_more_connections;
3535 connection = list_entry(&resource->connections, struct drbd_connection, connections);
3538 list_for_each_entry_continue_rcu(connection, &resource->connections, connections) {
3539 if (!has_net_conf(connection))
3542 goto put_result; /* only one iteration */
3545 no_more_connections:
3546 if (cb->args[1] == ITERATE_RESOURCES) {
3547 for_each_resource_rcu(next_resource, &drbd_resources) {
3548 if (next_resource == resource)
3549 goto found_resource;
3551 /* resource was probably deleted */
3556 list_for_each_entry_continue_rcu(next_resource, &drbd_resources, resources) {
3557 mutex_unlock(&resource->conf_update);
3558 kref_put(&resource->kref, drbd_destroy_resource);
3559 resource = next_resource;
3560 kref_get(&resource->kref);
3561 cb->args[0] = (long)resource;
3565 goto out; /* no more resources */
3568 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3569 cb->nlh->nlmsg_seq, &drbd_genl_family,
3570 NLM_F_MULTI, DRBD_ADM_GET_CONNECTIONS);
3574 dh->ret_code = retcode;
3576 if (retcode == NO_ERROR) {
3577 struct net_conf *net_conf;
3579 err = nla_put_drbd_cfg_context(skb, resource, connection, NULL);
3582 net_conf = rcu_dereference(connection->net_conf);
3584 err = net_conf_to_skb(skb, net_conf, !capable(CAP_SYS_ADMIN));
3588 connection_to_info(&connection_info, connection);
3589 err = connection_info_to_skb(skb, &connection_info, !capable(CAP_SYS_ADMIN));
3592 connection_statistics.conn_congested = test_bit(NET_CONGESTED, &connection->flags);
3593 err = connection_statistics_to_skb(skb, &connection_statistics, !capable(CAP_SYS_ADMIN));
3596 cb->args[2] = (long)connection;
3598 genlmsg_end(skb, dh);
3604 mutex_unlock(&resource->conf_update);
3610 enum mdf_peer_flag {
3611 MDF_PEER_CONNECTED = 1 << 0,
3612 MDF_PEER_OUTDATED = 1 << 1,
3613 MDF_PEER_FENCING = 1 << 2,
3614 MDF_PEER_FULL_SYNC = 1 << 3,
3617 static void peer_device_to_statistics(struct peer_device_statistics *s,
3618 struct drbd_peer_device *peer_device)
3620 struct drbd_device *device = peer_device->device;
3622 memset(s, 0, sizeof(*s));
3623 s->peer_dev_received = device->recv_cnt;
3624 s->peer_dev_sent = device->send_cnt;
3625 s->peer_dev_pending = atomic_read(&device->ap_pending_cnt) +
3626 atomic_read(&device->rs_pending_cnt);
3627 s->peer_dev_unacked = atomic_read(&device->unacked_cnt);
3628 s->peer_dev_out_of_sync = drbd_bm_total_weight(device) << (BM_BLOCK_SHIFT - 9);
3629 s->peer_dev_resync_failed = device->rs_failed << (BM_BLOCK_SHIFT - 9);
3630 if (get_ldev(device)) {
3631 struct drbd_md *md = &device->ldev->md;
3633 spin_lock_irq(&md->uuid_lock);
3634 s->peer_dev_bitmap_uuid = md->uuid[UI_BITMAP];
3635 spin_unlock_irq(&md->uuid_lock);
3637 (drbd_md_test_flag(device->ldev, MDF_CONNECTED_IND) ?
3638 MDF_PEER_CONNECTED : 0) +
3639 (drbd_md_test_flag(device->ldev, MDF_CONSISTENT) &&
3640 !drbd_md_test_flag(device->ldev, MDF_WAS_UP_TO_DATE) ?
3641 MDF_PEER_OUTDATED : 0) +
3642 /* FIXME: MDF_PEER_FENCING? */
3643 (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC) ?
3644 MDF_PEER_FULL_SYNC : 0);
3649 int drbd_adm_dump_peer_devices_done(struct netlink_callback *cb)
3651 return put_resource_in_arg0(cb, 9);
3654 int drbd_adm_dump_peer_devices(struct sk_buff *skb, struct netlink_callback *cb)
3656 struct nlattr *resource_filter;
3657 struct drbd_resource *resource;
3658 struct drbd_device *uninitialized_var(device);
3659 struct drbd_peer_device *peer_device = NULL;
3660 int minor, err, retcode;
3661 struct drbd_genlmsghdr *dh;
3662 struct idr *idr_to_search;
3664 resource = (struct drbd_resource *)cb->args[0];
3665 if (!cb->args[0] && !cb->args[1]) {
3666 resource_filter = find_cfg_context_attr(cb->nlh, T_ctx_resource_name);
3667 if (resource_filter) {
3668 retcode = ERR_RES_NOT_KNOWN;
3669 resource = drbd_find_resource(nla_data(resource_filter));
3673 cb->args[0] = (long)resource;
3677 minor = cb->args[1];
3678 idr_to_search = resource ? &resource->devices : &drbd_devices;
3679 device = idr_find(idr_to_search, minor);
3684 device = idr_get_next(idr_to_search, &minor);
3691 for_each_peer_device(peer_device, device)
3692 if (peer_device == (struct drbd_peer_device *)cb->args[2])
3693 goto found_peer_device;
3694 /* peer device was probably deleted */
3697 /* Make peer_device point to the list head (not the first entry). */
3698 peer_device = list_entry(&device->peer_devices, struct drbd_peer_device, peer_devices);
3701 list_for_each_entry_continue_rcu(peer_device, &device->peer_devices, peer_devices) {
3702 if (!has_net_conf(peer_device->connection))
3705 goto put_result; /* only one iteration */
3710 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3711 cb->nlh->nlmsg_seq, &drbd_genl_family,
3712 NLM_F_MULTI, DRBD_ADM_GET_PEER_DEVICES);
3716 dh->ret_code = retcode;
3718 if (retcode == NO_ERROR) {
3719 struct peer_device_info peer_device_info;
3720 struct peer_device_statistics peer_device_statistics;
3723 err = nla_put_drbd_cfg_context(skb, device->resource, peer_device->connection, device);
3726 peer_device_to_info(&peer_device_info, peer_device);
3727 err = peer_device_info_to_skb(skb, &peer_device_info, !capable(CAP_SYS_ADMIN));
3730 peer_device_to_statistics(&peer_device_statistics, peer_device);
3731 err = peer_device_statistics_to_skb(skb, &peer_device_statistics, !capable(CAP_SYS_ADMIN));
3734 cb->args[1] = minor;
3735 cb->args[2] = (long)peer_device;
3737 genlmsg_end(skb, dh);
3747 * Return the connection of @resource if @resource has exactly one connection.
3749 static struct drbd_connection *the_only_connection(struct drbd_resource *resource)
3751 struct list_head *connections = &resource->connections;
3753 if (list_empty(connections) || connections->next->next != connections)
3755 return list_first_entry(&resource->connections, struct drbd_connection, connections);
3758 static int nla_put_status_info(struct sk_buff *skb, struct drbd_device *device,
3759 const struct sib_info *sib)
3761 struct drbd_resource *resource = device->resource;
3762 struct state_info *si = NULL; /* for sizeof(si->member); */
3766 int exclude_sensitive;
3768 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
3769 * to. So we better exclude_sensitive information.
3771 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
3772 * in the context of the requesting user process. Exclude sensitive
3773 * information, unless current has superuser.
3775 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
3776 * relies on the current implementation of netlink_dump(), which
3777 * executes the dump callback successively from netlink_recvmsg(),
3778 * always in the context of the receiving process */
3779 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
3781 got_ldev = get_ldev(device);
3783 /* We need to add connection name and volume number information still.
3784 * Minor number is in drbd_genlmsghdr. */
3785 if (nla_put_drbd_cfg_context(skb, resource, the_only_connection(resource), device))
3786 goto nla_put_failure;
3788 if (res_opts_to_skb(skb, &device->resource->res_opts, exclude_sensitive))
3789 goto nla_put_failure;
3793 struct disk_conf *disk_conf;
3795 disk_conf = rcu_dereference(device->ldev->disk_conf);
3796 err = disk_conf_to_skb(skb, disk_conf, exclude_sensitive);
3799 struct net_conf *nc;
3801 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
3803 err = net_conf_to_skb(skb, nc, exclude_sensitive);
3807 goto nla_put_failure;
3809 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
3811 goto nla_put_failure;
3812 if (nla_put_u32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY) ||
3813 nla_put_u32(skb, T_current_state, device->state.i) ||
3814 nla_put_u64_0pad(skb, T_ed_uuid, device->ed_uuid) ||
3815 nla_put_u64_0pad(skb, T_capacity,
3816 drbd_get_capacity(device->this_bdev)) ||
3817 nla_put_u64_0pad(skb, T_send_cnt, device->send_cnt) ||
3818 nla_put_u64_0pad(skb, T_recv_cnt, device->recv_cnt) ||
3819 nla_put_u64_0pad(skb, T_read_cnt, device->read_cnt) ||
3820 nla_put_u64_0pad(skb, T_writ_cnt, device->writ_cnt) ||
3821 nla_put_u64_0pad(skb, T_al_writ_cnt, device->al_writ_cnt) ||
3822 nla_put_u64_0pad(skb, T_bm_writ_cnt, device->bm_writ_cnt) ||
3823 nla_put_u32(skb, T_ap_bio_cnt, atomic_read(&device->ap_bio_cnt)) ||
3824 nla_put_u32(skb, T_ap_pending_cnt, atomic_read(&device->ap_pending_cnt)) ||
3825 nla_put_u32(skb, T_rs_pending_cnt, atomic_read(&device->rs_pending_cnt)))
3826 goto nla_put_failure;
3831 spin_lock_irq(&device->ldev->md.uuid_lock);
3832 err = nla_put(skb, T_uuids, sizeof(si->uuids), device->ldev->md.uuid);
3833 spin_unlock_irq(&device->ldev->md.uuid_lock);
3836 goto nla_put_failure;
3838 if (nla_put_u32(skb, T_disk_flags, device->ldev->md.flags) ||
3839 nla_put_u64_0pad(skb, T_bits_total, drbd_bm_bits(device)) ||
3840 nla_put_u64_0pad(skb, T_bits_oos,
3841 drbd_bm_total_weight(device)))
3842 goto nla_put_failure;
3843 if (C_SYNC_SOURCE <= device->state.conn &&
3844 C_PAUSED_SYNC_T >= device->state.conn) {
3845 if (nla_put_u64_0pad(skb, T_bits_rs_total,
3846 device->rs_total) ||
3847 nla_put_u64_0pad(skb, T_bits_rs_failed,
3849 goto nla_put_failure;
3854 switch(sib->sib_reason) {
3855 case SIB_SYNC_PROGRESS:
3856 case SIB_GET_STATUS_REPLY:
3858 case SIB_STATE_CHANGE:
3859 if (nla_put_u32(skb, T_prev_state, sib->os.i) ||
3860 nla_put_u32(skb, T_new_state, sib->ns.i))
3861 goto nla_put_failure;
3863 case SIB_HELPER_POST:
3864 if (nla_put_u32(skb, T_helper_exit_code,
3865 sib->helper_exit_code))
3866 goto nla_put_failure;
3868 case SIB_HELPER_PRE:
3869 if (nla_put_string(skb, T_helper, sib->helper_name))
3870 goto nla_put_failure;
3874 nla_nest_end(skb, nla);
3884 int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
3886 struct drbd_config_context adm_ctx;
3887 enum drbd_ret_code retcode;
3890 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3891 if (!adm_ctx.reply_skb)
3893 if (retcode != NO_ERROR)
3896 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.device, NULL);
3898 nlmsg_free(adm_ctx.reply_skb);
3902 drbd_adm_finish(&adm_ctx, info, retcode);
3906 static int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
3908 struct drbd_device *device;
3909 struct drbd_genlmsghdr *dh;
3910 struct drbd_resource *pos = (struct drbd_resource *)cb->args[0];
3911 struct drbd_resource *resource = NULL;
3912 struct drbd_resource *tmp;
3913 unsigned volume = cb->args[1];
3915 /* Open coded, deferred, iteration:
3916 * for_each_resource_safe(resource, tmp, &drbd_resources) {
3917 * connection = "first connection of resource or undefined";
3918 * idr_for_each_entry(&resource->devices, device, i) {
3922 * where resource is cb->args[0];
3923 * and i is cb->args[1];
3925 * cb->args[2] indicates if we shall loop over all resources,
3926 * or just dump all volumes of a single resource.
3928 * This may miss entries inserted after this dump started,
3929 * or entries deleted before they are reached.
3931 * We need to make sure the device won't disappear while
3932 * we are looking at it, and revalidate our iterators
3933 * on each iteration.
3936 /* synchronize with conn_create()/drbd_destroy_connection() */
3938 /* revalidate iterator position */
3939 for_each_resource_rcu(tmp, &drbd_resources) {
3941 /* first iteration */
3953 device = idr_get_next(&resource->devices, &volume);
3955 /* No more volumes to dump on this resource.
3956 * Advance resource iterator. */
3957 pos = list_entry_rcu(resource->resources.next,
3958 struct drbd_resource, resources);
3959 /* Did we dump any volume of this resource yet? */
3961 /* If we reached the end of the list,
3962 * or only a single resource dump was requested,
3964 if (&pos->resources == &drbd_resources || cb->args[2])
3972 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3973 cb->nlh->nlmsg_seq, &drbd_genl_family,
3974 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
3979 /* This is a connection without a single volume.
3980 * Suprisingly enough, it may have a network
3982 struct drbd_connection *connection;
3985 dh->ret_code = NO_ERROR;
3986 connection = the_only_connection(resource);
3987 if (nla_put_drbd_cfg_context(skb, resource, connection, NULL))
3990 struct net_conf *nc;
3992 nc = rcu_dereference(connection->net_conf);
3993 if (nc && net_conf_to_skb(skb, nc, 1) != 0)
3999 D_ASSERT(device, device->vnr == volume);
4000 D_ASSERT(device, device->resource == resource);
4002 dh->minor = device_to_minor(device);
4003 dh->ret_code = NO_ERROR;
4005 if (nla_put_status_info(skb, device, NULL)) {
4007 genlmsg_cancel(skb, dh);
4011 genlmsg_end(skb, dh);
4016 /* where to start the next iteration */
4017 cb->args[0] = (long)pos;
4018 cb->args[1] = (pos == resource) ? volume + 1 : 0;
4020 /* No more resources/volumes/minors found results in an empty skb.
4021 * Which will terminate the dump. */
4026 * Request status of all resources, or of all volumes within a single resource.
4028 * This is a dump, as the answer may not fit in a single reply skb otherwise.
4029 * Which means we cannot use the family->attrbuf or other such members, because
4030 * dump is NOT protected by the genl_lock(). During dump, we only have access
4031 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
4033 * Once things are setup properly, we call into get_one_status().
4035 int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
4037 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
4039 const char *resource_name;
4040 struct drbd_resource *resource;
4043 /* Is this a followup call? */
4045 /* ... of a single resource dump,
4046 * and the resource iterator has been advanced already? */
4047 if (cb->args[2] && cb->args[2] != cb->args[0])
4048 return 0; /* DONE. */
4052 /* First call (from netlink_dump_start). We need to figure out
4053 * which resource(s) the user wants us to dump. */
4054 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
4055 nlmsg_attrlen(cb->nlh, hdrlen),
4056 DRBD_NLA_CFG_CONTEXT);
4058 /* No explicit context given. Dump all. */
4061 maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
4062 nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
4064 return PTR_ERR(nla);
4065 /* context given, but no name present? */
4068 resource_name = nla_data(nla);
4069 if (!*resource_name)
4071 resource = drbd_find_resource(resource_name);
4075 kref_put(&resource->kref, drbd_destroy_resource); /* get_one_status() revalidates the resource */
4077 /* prime iterators, and set "filter" mode mark:
4078 * only dump this connection. */
4079 cb->args[0] = (long)resource;
4080 /* cb->args[1] = 0; passed in this way. */
4081 cb->args[2] = (long)resource;
4084 return get_one_status(skb, cb);
4087 int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
4089 struct drbd_config_context adm_ctx;
4090 enum drbd_ret_code retcode;
4091 struct timeout_parms tp;
4094 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
4095 if (!adm_ctx.reply_skb)
4097 if (retcode != NO_ERROR)
4101 adm_ctx.device->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
4102 test_bit(USE_DEGR_WFC_T, &adm_ctx.device->flags) ? UT_DEGRADED :
4105 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
4107 nlmsg_free(adm_ctx.reply_skb);
4111 drbd_adm_finish(&adm_ctx, info, retcode);
4115 int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
4117 struct drbd_config_context adm_ctx;
4118 struct drbd_device *device;
4119 enum drbd_ret_code retcode;
4120 struct start_ov_parms parms;
4122 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
4123 if (!adm_ctx.reply_skb)
4125 if (retcode != NO_ERROR)
4128 device = adm_ctx.device;
4130 /* resume from last known position, if possible */
4131 parms.ov_start_sector = device->ov_start_sector;
4132 parms.ov_stop_sector = ULLONG_MAX;
4133 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
4134 int err = start_ov_parms_from_attrs(&parms, info);
4136 retcode = ERR_MANDATORY_TAG;
4137 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
4141 mutex_lock(&adm_ctx.resource->adm_mutex);
4143 /* w_make_ov_request expects position to be aligned */
4144 device->ov_start_sector = parms.ov_start_sector & ~(BM_SECT_PER_BIT-1);
4145 device->ov_stop_sector = parms.ov_stop_sector;
4147 /* If there is still bitmap IO pending, e.g. previous resync or verify
4148 * just being finished, wait for it before requesting a new resync. */
4149 drbd_suspend_io(device);
4150 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
4151 retcode = drbd_request_state(device, NS(conn, C_VERIFY_S));
4152 drbd_resume_io(device);
4154 mutex_unlock(&adm_ctx.resource->adm_mutex);
4156 drbd_adm_finish(&adm_ctx, info, retcode);
4161 int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
4163 struct drbd_config_context adm_ctx;
4164 struct drbd_device *device;
4165 enum drbd_ret_code retcode;
4166 int skip_initial_sync = 0;
4168 struct new_c_uuid_parms args;
4170 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
4171 if (!adm_ctx.reply_skb)
4173 if (retcode != NO_ERROR)
4176 device = adm_ctx.device;
4177 memset(&args, 0, sizeof(args));
4178 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
4179 err = new_c_uuid_parms_from_attrs(&args, info);
4181 retcode = ERR_MANDATORY_TAG;
4182 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
4187 mutex_lock(&adm_ctx.resource->adm_mutex);
4188 mutex_lock(device->state_mutex); /* Protects us against serialized state changes. */
4190 if (!get_ldev(device)) {
4191 retcode = ERR_NO_DISK;
4195 /* this is "skip initial sync", assume to be clean */
4196 if (device->state.conn == C_CONNECTED &&
4197 first_peer_device(device)->connection->agreed_pro_version >= 90 &&
4198 device->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
4199 drbd_info(device, "Preparing to skip initial sync\n");
4200 skip_initial_sync = 1;
4201 } else if (device->state.conn != C_STANDALONE) {
4202 retcode = ERR_CONNECTED;
4206 drbd_uuid_set(device, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
4207 drbd_uuid_new_current(device); /* New current, previous to UI_BITMAP */
4209 if (args.clear_bm) {
4210 err = drbd_bitmap_io(device, &drbd_bmio_clear_n_write,
4211 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
4213 drbd_err(device, "Writing bitmap failed with %d\n", err);
4214 retcode = ERR_IO_MD_DISK;
4216 if (skip_initial_sync) {
4217 drbd_send_uuids_skip_initial_sync(first_peer_device(device));
4218 _drbd_uuid_set(device, UI_BITMAP, 0);
4219 drbd_print_uuids(device, "cleared bitmap UUID");
4220 spin_lock_irq(&device->resource->req_lock);
4221 _drbd_set_state(_NS2(device, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
4223 spin_unlock_irq(&device->resource->req_lock);
4227 drbd_md_sync(device);
4231 mutex_unlock(device->state_mutex);
4232 mutex_unlock(&adm_ctx.resource->adm_mutex);
4234 drbd_adm_finish(&adm_ctx, info, retcode);
4238 static enum drbd_ret_code
4239 drbd_check_resource_name(struct drbd_config_context *adm_ctx)
4241 const char *name = adm_ctx->resource_name;
4242 if (!name || !name[0]) {
4243 drbd_msg_put_info(adm_ctx->reply_skb, "resource name missing");
4244 return ERR_MANDATORY_TAG;
4246 /* if we want to use these in sysfs/configfs/debugfs some day,
4247 * we must not allow slashes */
4248 if (strchr(name, '/')) {
4249 drbd_msg_put_info(adm_ctx->reply_skb, "invalid resource name");
4250 return ERR_INVALID_REQUEST;
4255 static void resource_to_info(struct resource_info *info,
4256 struct drbd_resource *resource)
4258 info->res_role = conn_highest_role(first_connection(resource));
4259 info->res_susp = resource->susp;
4260 info->res_susp_nod = resource->susp_nod;
4261 info->res_susp_fen = resource->susp_fen;
4264 int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
4266 struct drbd_connection *connection;
4267 struct drbd_config_context adm_ctx;
4268 enum drbd_ret_code retcode;
4269 struct res_opts res_opts;
4272 retcode = drbd_adm_prepare(&adm_ctx, skb, info, 0);
4273 if (!adm_ctx.reply_skb)
4275 if (retcode != NO_ERROR)
4278 set_res_opts_defaults(&res_opts);
4279 err = res_opts_from_attrs(&res_opts, info);
4280 if (err && err != -ENOMSG) {
4281 retcode = ERR_MANDATORY_TAG;
4282 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
4286 retcode = drbd_check_resource_name(&adm_ctx);
4287 if (retcode != NO_ERROR)
4290 if (adm_ctx.resource) {
4291 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
4292 retcode = ERR_INVALID_REQUEST;
4293 drbd_msg_put_info(adm_ctx.reply_skb, "resource exists");
4295 /* else: still NO_ERROR */
4299 /* not yet safe for genl_family.parallel_ops */
4300 mutex_lock(&resources_mutex);
4301 connection = conn_create(adm_ctx.resource_name, &res_opts);
4302 mutex_unlock(&resources_mutex);
4305 struct resource_info resource_info;
4307 mutex_lock(¬ification_mutex);
4308 resource_to_info(&resource_info, connection->resource);
4309 notify_resource_state(NULL, 0, connection->resource,
4310 &resource_info, NOTIFY_CREATE);
4311 mutex_unlock(¬ification_mutex);
4313 retcode = ERR_NOMEM;
4316 drbd_adm_finish(&adm_ctx, info, retcode);
4320 static void device_to_info(struct device_info *info,
4321 struct drbd_device *device)
4323 info->dev_disk_state = device->state.disk;
4327 int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info)
4329 struct drbd_config_context adm_ctx;
4330 struct drbd_genlmsghdr *dh = info->userhdr;
4331 enum drbd_ret_code retcode;
4333 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
4334 if (!adm_ctx.reply_skb)
4336 if (retcode != NO_ERROR)
4339 if (dh->minor > MINORMASK) {
4340 drbd_msg_put_info(adm_ctx.reply_skb, "requested minor out of range");
4341 retcode = ERR_INVALID_REQUEST;
4344 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
4345 drbd_msg_put_info(adm_ctx.reply_skb, "requested volume id out of range");
4346 retcode = ERR_INVALID_REQUEST;
4350 /* drbd_adm_prepare made sure already
4351 * that first_peer_device(device)->connection and device->vnr match the request. */
4352 if (adm_ctx.device) {
4353 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
4354 retcode = ERR_MINOR_OR_VOLUME_EXISTS;
4355 /* else: still NO_ERROR */
4359 mutex_lock(&adm_ctx.resource->adm_mutex);
4360 retcode = drbd_create_device(&adm_ctx, dh->minor);
4361 if (retcode == NO_ERROR) {
4362 struct drbd_device *device;
4363 struct drbd_peer_device *peer_device;
4364 struct device_info info;
4365 unsigned int peer_devices = 0;
4366 enum drbd_notification_type flags;
4368 device = minor_to_device(dh->minor);
4369 for_each_peer_device(peer_device, device) {
4370 if (!has_net_conf(peer_device->connection))
4375 device_to_info(&info, device);
4376 mutex_lock(¬ification_mutex);
4377 flags = (peer_devices--) ? NOTIFY_CONTINUES : 0;
4378 notify_device_state(NULL, 0, device, &info, NOTIFY_CREATE | flags);
4379 for_each_peer_device(peer_device, device) {
4380 struct peer_device_info peer_device_info;
4382 if (!has_net_conf(peer_device->connection))
4384 peer_device_to_info(&peer_device_info, peer_device);
4385 flags = (peer_devices--) ? NOTIFY_CONTINUES : 0;
4386 notify_peer_device_state(NULL, 0, peer_device, &peer_device_info,
4387 NOTIFY_CREATE | flags);
4389 mutex_unlock(¬ification_mutex);
4391 mutex_unlock(&adm_ctx.resource->adm_mutex);
4393 drbd_adm_finish(&adm_ctx, info, retcode);
4397 static enum drbd_ret_code adm_del_minor(struct drbd_device *device)
4399 struct drbd_peer_device *peer_device;
4401 if (device->state.disk == D_DISKLESS &&
4402 /* no need to be device->state.conn == C_STANDALONE &&
4403 * we may want to delete a minor from a live replication group.
4405 device->state.role == R_SECONDARY) {
4406 struct drbd_connection *connection =
4407 first_connection(device->resource);
4409 _drbd_request_state(device, NS(conn, C_WF_REPORT_PARAMS),
4410 CS_VERBOSE + CS_WAIT_COMPLETE);
4412 /* If the state engine hasn't stopped the sender thread yet, we
4413 * need to flush the sender work queue before generating the
4414 * DESTROY events here. */
4415 if (get_t_state(&connection->worker) == RUNNING)
4416 drbd_flush_workqueue(&connection->sender_work);
4418 mutex_lock(¬ification_mutex);
4419 for_each_peer_device(peer_device, device) {
4420 if (!has_net_conf(peer_device->connection))
4422 notify_peer_device_state(NULL, 0, peer_device, NULL,
4423 NOTIFY_DESTROY | NOTIFY_CONTINUES);
4425 notify_device_state(NULL, 0, device, NULL, NOTIFY_DESTROY);
4426 mutex_unlock(¬ification_mutex);
4428 drbd_delete_device(device);
4431 return ERR_MINOR_CONFIGURED;
4434 int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info)
4436 struct drbd_config_context adm_ctx;
4437 enum drbd_ret_code retcode;
4439 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
4440 if (!adm_ctx.reply_skb)
4442 if (retcode != NO_ERROR)
4445 mutex_lock(&adm_ctx.resource->adm_mutex);
4446 retcode = adm_del_minor(adm_ctx.device);
4447 mutex_unlock(&adm_ctx.resource->adm_mutex);
4449 drbd_adm_finish(&adm_ctx, info, retcode);
4453 static int adm_del_resource(struct drbd_resource *resource)
4455 struct drbd_connection *connection;
4457 for_each_connection(connection, resource) {
4458 if (connection->cstate > C_STANDALONE)
4459 return ERR_NET_CONFIGURED;
4461 if (!idr_is_empty(&resource->devices))
4462 return ERR_RES_IN_USE;
4464 /* The state engine has stopped the sender thread, so we don't
4465 * need to flush the sender work queue before generating the
4466 * DESTROY event here. */
4467 mutex_lock(¬ification_mutex);
4468 notify_resource_state(NULL, 0, resource, NULL, NOTIFY_DESTROY);
4469 mutex_unlock(¬ification_mutex);
4471 mutex_lock(&resources_mutex);
4472 list_del_rcu(&resource->resources);
4473 mutex_unlock(&resources_mutex);
4474 /* Make sure all threads have actually stopped: state handling only
4475 * does drbd_thread_stop_nowait(). */
4476 list_for_each_entry(connection, &resource->connections, connections)
4477 drbd_thread_stop(&connection->worker);
4479 drbd_free_resource(resource);
4483 int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
4485 struct drbd_config_context adm_ctx;
4486 struct drbd_resource *resource;
4487 struct drbd_connection *connection;
4488 struct drbd_device *device;
4489 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
4492 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
4493 if (!adm_ctx.reply_skb)
4495 if (retcode != NO_ERROR)
4498 resource = adm_ctx.resource;
4499 mutex_lock(&resource->adm_mutex);
4501 for_each_connection(connection, resource) {
4502 struct drbd_peer_device *peer_device;
4504 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
4505 retcode = drbd_set_role(peer_device->device, R_SECONDARY, 0);
4506 if (retcode < SS_SUCCESS) {
4507 drbd_msg_put_info(adm_ctx.reply_skb, "failed to demote");
4512 retcode = conn_try_disconnect(connection, 0);
4513 if (retcode < SS_SUCCESS) {
4514 drbd_msg_put_info(adm_ctx.reply_skb, "failed to disconnect");
4520 idr_for_each_entry(&resource->devices, device, i) {
4521 retcode = adm_detach(device, 0);
4522 if (retcode < SS_SUCCESS || retcode > NO_ERROR) {
4523 drbd_msg_put_info(adm_ctx.reply_skb, "failed to detach");
4528 /* delete volumes */
4529 idr_for_each_entry(&resource->devices, device, i) {
4530 retcode = adm_del_minor(device);
4531 if (retcode != NO_ERROR) {
4532 /* "can not happen" */
4533 drbd_msg_put_info(adm_ctx.reply_skb, "failed to delete volume");
4538 retcode = adm_del_resource(resource);
4540 mutex_unlock(&resource->adm_mutex);
4542 drbd_adm_finish(&adm_ctx, info, retcode);
4546 int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
4548 struct drbd_config_context adm_ctx;
4549 struct drbd_resource *resource;
4550 enum drbd_ret_code retcode;
4552 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
4553 if (!adm_ctx.reply_skb)
4555 if (retcode != NO_ERROR)
4557 resource = adm_ctx.resource;
4559 mutex_lock(&resource->adm_mutex);
4560 retcode = adm_del_resource(resource);
4561 mutex_unlock(&resource->adm_mutex);
4563 drbd_adm_finish(&adm_ctx, info, retcode);
4567 void drbd_bcast_event(struct drbd_device *device, const struct sib_info *sib)
4569 struct sk_buff *msg;
4570 struct drbd_genlmsghdr *d_out;
4574 seq = atomic_inc_return(&drbd_genl_seq);
4575 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4580 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
4581 if (!d_out) /* cannot happen, but anyways. */
4582 goto nla_put_failure;
4583 d_out->minor = device_to_minor(device);
4584 d_out->ret_code = NO_ERROR;
4586 if (nla_put_status_info(msg, device, sib))
4587 goto nla_put_failure;
4588 genlmsg_end(msg, d_out);
4589 err = drbd_genl_multicast_events(msg, GFP_NOWAIT);
4590 /* msg has been consumed or freed in netlink_broadcast() */
4591 if (err && err != -ESRCH)
4599 drbd_err(device, "Error %d while broadcasting event. "
4600 "Event seq:%u sib_reason:%u\n",
4601 err, seq, sib->sib_reason);
4604 static int nla_put_notification_header(struct sk_buff *msg,
4605 enum drbd_notification_type type)
4607 struct drbd_notification_header nh = {
4611 return drbd_notification_header_to_skb(msg, &nh, true);
4614 void notify_resource_state(struct sk_buff *skb,
4616 struct drbd_resource *resource,
4617 struct resource_info *resource_info,
4618 enum drbd_notification_type type)
4620 struct resource_statistics resource_statistics;
4621 struct drbd_genlmsghdr *dh;
4622 bool multicast = false;
4626 seq = atomic_inc_return(¬ify_genl_seq);
4627 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4635 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_RESOURCE_STATE);
4637 goto nla_put_failure;
4639 dh->ret_code = NO_ERROR;
4640 if (nla_put_drbd_cfg_context(skb, resource, NULL, NULL) ||
4641 nla_put_notification_header(skb, type) ||
4642 ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY &&
4643 resource_info_to_skb(skb, resource_info, true)))
4644 goto nla_put_failure;
4645 resource_statistics.res_stat_write_ordering = resource->write_ordering;
4646 err = resource_statistics_to_skb(skb, &resource_statistics, !capable(CAP_SYS_ADMIN));
4648 goto nla_put_failure;
4649 genlmsg_end(skb, dh);
4651 err = drbd_genl_multicast_events(skb, GFP_NOWAIT);
4652 /* skb has been consumed or freed in netlink_broadcast() */
4653 if (err && err != -ESRCH)
4661 drbd_err(resource, "Error %d while broadcasting event. Event seq:%u\n",
4665 void notify_device_state(struct sk_buff *skb,
4667 struct drbd_device *device,
4668 struct device_info *device_info,
4669 enum drbd_notification_type type)
4671 struct device_statistics device_statistics;
4672 struct drbd_genlmsghdr *dh;
4673 bool multicast = false;
4677 seq = atomic_inc_return(¬ify_genl_seq);
4678 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4686 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_DEVICE_STATE);
4688 goto nla_put_failure;
4689 dh->minor = device->minor;
4690 dh->ret_code = NO_ERROR;
4691 if (nla_put_drbd_cfg_context(skb, device->resource, NULL, device) ||
4692 nla_put_notification_header(skb, type) ||
4693 ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY &&
4694 device_info_to_skb(skb, device_info, true)))
4695 goto nla_put_failure;
4696 device_to_statistics(&device_statistics, device);
4697 device_statistics_to_skb(skb, &device_statistics, !capable(CAP_SYS_ADMIN));
4698 genlmsg_end(skb, dh);
4700 err = drbd_genl_multicast_events(skb, GFP_NOWAIT);
4701 /* skb has been consumed or freed in netlink_broadcast() */
4702 if (err && err != -ESRCH)
4710 drbd_err(device, "Error %d while broadcasting event. Event seq:%u\n",
4714 void notify_connection_state(struct sk_buff *skb,
4716 struct drbd_connection *connection,
4717 struct connection_info *connection_info,
4718 enum drbd_notification_type type)
4720 struct connection_statistics connection_statistics;
4721 struct drbd_genlmsghdr *dh;
4722 bool multicast = false;
4726 seq = atomic_inc_return(¬ify_genl_seq);
4727 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4735 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_CONNECTION_STATE);
4737 goto nla_put_failure;
4739 dh->ret_code = NO_ERROR;
4740 if (nla_put_drbd_cfg_context(skb, connection->resource, connection, NULL) ||
4741 nla_put_notification_header(skb, type) ||
4742 ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY &&
4743 connection_info_to_skb(skb, connection_info, true)))
4744 goto nla_put_failure;
4745 connection_statistics.conn_congested = test_bit(NET_CONGESTED, &connection->flags);
4746 connection_statistics_to_skb(skb, &connection_statistics, !capable(CAP_SYS_ADMIN));
4747 genlmsg_end(skb, dh);
4749 err = drbd_genl_multicast_events(skb, GFP_NOWAIT);
4750 /* skb has been consumed or freed in netlink_broadcast() */
4751 if (err && err != -ESRCH)
4759 drbd_err(connection, "Error %d while broadcasting event. Event seq:%u\n",
4763 void notify_peer_device_state(struct sk_buff *skb,
4765 struct drbd_peer_device *peer_device,
4766 struct peer_device_info *peer_device_info,
4767 enum drbd_notification_type type)
4769 struct peer_device_statistics peer_device_statistics;
4770 struct drbd_resource *resource = peer_device->device->resource;
4771 struct drbd_genlmsghdr *dh;
4772 bool multicast = false;
4776 seq = atomic_inc_return(¬ify_genl_seq);
4777 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4785 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_PEER_DEVICE_STATE);
4787 goto nla_put_failure;
4789 dh->ret_code = NO_ERROR;
4790 if (nla_put_drbd_cfg_context(skb, resource, peer_device->connection, peer_device->device) ||
4791 nla_put_notification_header(skb, type) ||
4792 ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY &&
4793 peer_device_info_to_skb(skb, peer_device_info, true)))
4794 goto nla_put_failure;
4795 peer_device_to_statistics(&peer_device_statistics, peer_device);
4796 peer_device_statistics_to_skb(skb, &peer_device_statistics, !capable(CAP_SYS_ADMIN));
4797 genlmsg_end(skb, dh);
4799 err = drbd_genl_multicast_events(skb, GFP_NOWAIT);
4800 /* skb has been consumed or freed in netlink_broadcast() */
4801 if (err && err != -ESRCH)
4809 drbd_err(peer_device, "Error %d while broadcasting event. Event seq:%u\n",
4813 void notify_helper(enum drbd_notification_type type,
4814 struct drbd_device *device, struct drbd_connection *connection,
4815 const char *name, int status)
4817 struct drbd_resource *resource = device ? device->resource : connection->resource;
4818 struct drbd_helper_info helper_info;
4819 unsigned int seq = atomic_inc_return(¬ify_genl_seq);
4820 struct sk_buff *skb = NULL;
4821 struct drbd_genlmsghdr *dh;
4824 strlcpy(helper_info.helper_name, name, sizeof(helper_info.helper_name));
4825 helper_info.helper_name_len = min(strlen(name), sizeof(helper_info.helper_name));
4826 helper_info.helper_status = status;
4828 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4834 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_HELPER);
4837 dh->minor = device ? device->minor : -1;
4838 dh->ret_code = NO_ERROR;
4839 mutex_lock(¬ification_mutex);
4840 if (nla_put_drbd_cfg_context(skb, resource, connection, device) ||
4841 nla_put_notification_header(skb, type) ||
4842 drbd_helper_info_to_skb(skb, &helper_info, true))
4844 genlmsg_end(skb, dh);
4845 err = drbd_genl_multicast_events(skb, GFP_NOWAIT);
4847 /* skb has been consumed or freed in netlink_broadcast() */
4848 if (err && err != -ESRCH)
4850 mutex_unlock(¬ification_mutex);
4854 mutex_unlock(¬ification_mutex);
4857 drbd_err(resource, "Error %d while broadcasting event. Event seq:%u\n",
4861 static void notify_initial_state_done(struct sk_buff *skb, unsigned int seq)
4863 struct drbd_genlmsghdr *dh;
4867 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_INITIAL_STATE_DONE);
4869 goto nla_put_failure;
4871 dh->ret_code = NO_ERROR;
4872 if (nla_put_notification_header(skb, NOTIFY_EXISTS))
4873 goto nla_put_failure;
4874 genlmsg_end(skb, dh);
4879 pr_err("Error %d sending event. Event seq:%u\n", err, seq);
4882 static void free_state_changes(struct list_head *list)
4884 while (!list_empty(list)) {
4885 struct drbd_state_change *state_change =
4886 list_first_entry(list, struct drbd_state_change, list);
4887 list_del(&state_change->list);
4888 forget_state_change(state_change);
4892 static unsigned int notifications_for_state_change(struct drbd_state_change *state_change)
4895 state_change->n_connections +
4896 state_change->n_devices +
4897 state_change->n_devices * state_change->n_connections;
4900 static int get_initial_state(struct sk_buff *skb, struct netlink_callback *cb)
4902 struct drbd_state_change *state_change = (struct drbd_state_change *)cb->args[0];
4903 unsigned int seq = cb->args[2];
4905 enum drbd_notification_type flags = 0;
4907 /* There is no need for taking notification_mutex here: it doesn't
4908 matter if the initial state events mix with later state chage
4909 events; we can always tell the events apart by the NOTIFY_EXISTS
4913 if (cb->args[5] == 1) {
4914 notify_initial_state_done(skb, seq);
4918 if (cb->args[4] < cb->args[3])
4919 flags |= NOTIFY_CONTINUES;
4921 notify_resource_state_change(skb, seq, state_change->resource,
4922 NOTIFY_EXISTS | flags);
4926 if (n < state_change->n_connections) {
4927 notify_connection_state_change(skb, seq, &state_change->connections[n],
4928 NOTIFY_EXISTS | flags);
4931 n -= state_change->n_connections;
4932 if (n < state_change->n_devices) {
4933 notify_device_state_change(skb, seq, &state_change->devices[n],
4934 NOTIFY_EXISTS | flags);
4937 n -= state_change->n_devices;
4938 if (n < state_change->n_devices * state_change->n_connections) {
4939 notify_peer_device_state_change(skb, seq, &state_change->peer_devices[n],
4940 NOTIFY_EXISTS | flags);
4945 if (cb->args[4] == cb->args[3]) {
4946 struct drbd_state_change *next_state_change =
4947 list_entry(state_change->list.next,
4948 struct drbd_state_change, list);
4949 cb->args[0] = (long)next_state_change;
4950 cb->args[3] = notifications_for_state_change(next_state_change);
4957 int drbd_adm_get_initial_state(struct sk_buff *skb, struct netlink_callback *cb)
4959 struct drbd_resource *resource;
4962 if (cb->args[5] >= 1) {
4963 if (cb->args[5] > 1)
4964 return get_initial_state(skb, cb);
4966 struct drbd_state_change *state_change =
4967 (struct drbd_state_change *)cb->args[0];
4969 /* connect list to head */
4970 list_add(&head, &state_change->list);
4971 free_state_changes(&head);
4976 cb->args[5] = 2; /* number of iterations */
4977 mutex_lock(&resources_mutex);
4978 for_each_resource(resource, &drbd_resources) {
4979 struct drbd_state_change *state_change;
4981 state_change = remember_old_state(resource, GFP_KERNEL);
4982 if (!state_change) {
4983 if (!list_empty(&head))
4984 free_state_changes(&head);
4985 mutex_unlock(&resources_mutex);
4988 copy_old_to_new_state_change(state_change);
4989 list_add_tail(&state_change->list, &head);
4990 cb->args[5] += notifications_for_state_change(state_change);
4992 mutex_unlock(&resources_mutex);
4994 if (!list_empty(&head)) {
4995 struct drbd_state_change *state_change =
4996 list_entry(head.next, struct drbd_state_change, list);
4997 cb->args[0] = (long)state_change;
4998 cb->args[3] = notifications_for_state_change(state_change);
4999 list_del(&head); /* detach list from head */
5002 cb->args[2] = cb->nlh->nlmsg_seq;
5003 return get_initial_state(skb, cb);