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 #include <linux/module.h>
27 #include <linux/drbd.h>
30 #include <linux/file.h>
31 #include <linux/slab.h>
32 #include <linux/blkpg.h>
33 #include <linux/cpumask.h>
35 #include "drbd_protocol.h"
37 #include <asm/unaligned.h>
38 #include <linux/drbd_limits.h>
39 #include <linux/kthread.h>
41 #include <net/genetlink.h>
44 // int drbd_adm_create_resource(struct sk_buff *skb, struct genl_info *info);
45 // int drbd_adm_delete_resource(struct sk_buff *skb, struct genl_info *info);
47 int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info);
48 int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info);
50 int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info);
51 int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info);
52 int drbd_adm_down(struct sk_buff *skb, struct genl_info *info);
54 int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info);
55 int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info);
56 int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info);
57 int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info);
58 int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info);
59 int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info);
60 int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info);
61 int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info);
62 int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info);
63 int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info);
64 int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info);
65 int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info);
66 int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info);
67 int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info);
68 int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info);
69 int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info);
70 int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info);
71 int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info);
72 int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info);
73 int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info);
75 int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb);
77 #include <linux/drbd_genl_api.h>
79 #include <linux/genl_magic_func.h>
81 /* used blkdev_get_by_path, to claim our meta data device(s) */
82 static char *drbd_m_holder = "Hands off! this is DRBD's meta data device.";
84 static void drbd_adm_send_reply(struct sk_buff *skb, struct genl_info *info)
86 genlmsg_end(skb, genlmsg_data(nlmsg_data(nlmsg_hdr(skb))));
87 if (genlmsg_reply(skb, info))
88 printk(KERN_ERR "drbd: error sending genl reply\n");
91 /* Used on a fresh "drbd_adm_prepare"d reply_skb, this cannot fail: The only
92 * reason it could fail was no space in skb, and there are 4k available. */
93 int drbd_msg_put_info(struct sk_buff *skb, const char *info)
98 if (!info || !info[0])
101 nla = nla_nest_start(skb, DRBD_NLA_CFG_REPLY);
105 err = nla_put_string(skb, T_info_text, info);
107 nla_nest_cancel(skb, nla);
110 nla_nest_end(skb, nla);
114 /* This would be a good candidate for a "pre_doit" hook,
115 * and per-family private info->pointers.
116 * But we need to stay compatible with older kernels.
117 * If it returns successfully, adm_ctx members are valid.
119 * At this point, we still rely on the global genl_lock().
120 * If we want to avoid that, and allow "genl_family.parallel_ops", we may need
121 * to add additional synchronization against object destruction/modification.
123 #define DRBD_ADM_NEED_MINOR 1
124 #define DRBD_ADM_NEED_RESOURCE 2
125 #define DRBD_ADM_NEED_CONNECTION 4
126 static int drbd_adm_prepare(struct drbd_config_context *adm_ctx,
127 struct sk_buff *skb, struct genl_info *info, unsigned flags)
129 struct drbd_genlmsghdr *d_in = info->userhdr;
130 const u8 cmd = info->genlhdr->cmd;
133 memset(adm_ctx, 0, sizeof(*adm_ctx));
135 /* genl_rcv_msg only checks for CAP_NET_ADMIN on "GENL_ADMIN_PERM" :( */
136 if (cmd != DRBD_ADM_GET_STATUS && !capable(CAP_NET_ADMIN))
139 adm_ctx->reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
140 if (!adm_ctx->reply_skb) {
145 adm_ctx->reply_dh = genlmsg_put_reply(adm_ctx->reply_skb,
146 info, &drbd_genl_family, 0, cmd);
147 /* put of a few bytes into a fresh skb of >= 4k will always succeed.
149 if (!adm_ctx->reply_dh) {
154 adm_ctx->reply_dh->minor = d_in->minor;
155 adm_ctx->reply_dh->ret_code = NO_ERROR;
157 adm_ctx->volume = VOLUME_UNSPECIFIED;
158 if (info->attrs[DRBD_NLA_CFG_CONTEXT]) {
160 /* parse and validate only */
161 err = drbd_cfg_context_from_attrs(NULL, info);
165 /* It was present, and valid,
166 * copy it over to the reply skb. */
167 err = nla_put_nohdr(adm_ctx->reply_skb,
168 info->attrs[DRBD_NLA_CFG_CONTEXT]->nla_len,
169 info->attrs[DRBD_NLA_CFG_CONTEXT]);
173 /* and assign stuff to the adm_ctx */
174 nla = nested_attr_tb[__nla_type(T_ctx_volume)];
176 adm_ctx->volume = nla_get_u32(nla);
177 nla = nested_attr_tb[__nla_type(T_ctx_resource_name)];
179 adm_ctx->resource_name = nla_data(nla);
180 adm_ctx->my_addr = nested_attr_tb[__nla_type(T_ctx_my_addr)];
181 adm_ctx->peer_addr = nested_attr_tb[__nla_type(T_ctx_peer_addr)];
182 if ((adm_ctx->my_addr &&
183 nla_len(adm_ctx->my_addr) > sizeof(adm_ctx->connection->my_addr)) ||
184 (adm_ctx->peer_addr &&
185 nla_len(adm_ctx->peer_addr) > sizeof(adm_ctx->connection->peer_addr))) {
191 adm_ctx->minor = d_in->minor;
192 adm_ctx->device = minor_to_device(d_in->minor);
194 /* We are protected by the global genl_lock().
195 * But we may explicitly drop it/retake it in drbd_adm_set_role(),
196 * so make sure this object stays around. */
198 kref_get(&adm_ctx->device->kref);
200 if (adm_ctx->resource_name) {
201 adm_ctx->resource = drbd_find_resource(adm_ctx->resource_name);
204 if (!adm_ctx->device && (flags & DRBD_ADM_NEED_MINOR)) {
205 drbd_msg_put_info(adm_ctx->reply_skb, "unknown minor");
206 return ERR_MINOR_INVALID;
208 if (!adm_ctx->resource && (flags & DRBD_ADM_NEED_RESOURCE)) {
209 drbd_msg_put_info(adm_ctx->reply_skb, "unknown resource");
210 if (adm_ctx->resource_name)
211 return ERR_RES_NOT_KNOWN;
212 return ERR_INVALID_REQUEST;
215 if (flags & DRBD_ADM_NEED_CONNECTION) {
216 if (adm_ctx->resource) {
217 drbd_msg_put_info(adm_ctx->reply_skb, "no resource name expected");
218 return ERR_INVALID_REQUEST;
220 if (adm_ctx->device) {
221 drbd_msg_put_info(adm_ctx->reply_skb, "no minor number expected");
222 return ERR_INVALID_REQUEST;
224 if (adm_ctx->my_addr && adm_ctx->peer_addr)
225 adm_ctx->connection = conn_get_by_addrs(nla_data(adm_ctx->my_addr),
226 nla_len(adm_ctx->my_addr),
227 nla_data(adm_ctx->peer_addr),
228 nla_len(adm_ctx->peer_addr));
229 if (!adm_ctx->connection) {
230 drbd_msg_put_info(adm_ctx->reply_skb, "unknown connection");
231 return ERR_INVALID_REQUEST;
235 /* some more paranoia, if the request was over-determined */
236 if (adm_ctx->device && adm_ctx->resource &&
237 adm_ctx->device->resource != adm_ctx->resource) {
238 pr_warning("request: minor=%u, resource=%s; but that minor belongs to resource %s\n",
239 adm_ctx->minor, adm_ctx->resource->name,
240 adm_ctx->device->resource->name);
241 drbd_msg_put_info(adm_ctx->reply_skb, "minor exists in different resource");
242 return ERR_INVALID_REQUEST;
244 if (adm_ctx->device &&
245 adm_ctx->volume != VOLUME_UNSPECIFIED &&
246 adm_ctx->volume != adm_ctx->device->vnr) {
247 pr_warning("request: minor=%u, volume=%u; but that minor is volume %u in %s\n",
248 adm_ctx->minor, adm_ctx->volume,
249 adm_ctx->device->vnr,
250 adm_ctx->device->resource->name);
251 drbd_msg_put_info(adm_ctx->reply_skb, "minor exists as different volume");
252 return ERR_INVALID_REQUEST;
255 /* still, provide adm_ctx->resource always, if possible. */
256 if (!adm_ctx->resource) {
257 adm_ctx->resource = adm_ctx->device ? adm_ctx->device->resource
258 : adm_ctx->connection ? adm_ctx->connection->resource : NULL;
259 if (adm_ctx->resource)
260 kref_get(&adm_ctx->resource->kref);
266 nlmsg_free(adm_ctx->reply_skb);
267 adm_ctx->reply_skb = NULL;
271 static int drbd_adm_finish(struct drbd_config_context *adm_ctx,
272 struct genl_info *info, int retcode)
274 if (adm_ctx->device) {
275 kref_put(&adm_ctx->device->kref, drbd_destroy_device);
276 adm_ctx->device = NULL;
278 if (adm_ctx->connection) {
279 kref_put(&adm_ctx->connection->kref, &drbd_destroy_connection);
280 adm_ctx->connection = NULL;
282 if (adm_ctx->resource) {
283 kref_put(&adm_ctx->resource->kref, drbd_destroy_resource);
284 adm_ctx->resource = NULL;
287 if (!adm_ctx->reply_skb)
290 adm_ctx->reply_dh->ret_code = retcode;
291 drbd_adm_send_reply(adm_ctx->reply_skb, info);
295 static void setup_khelper_env(struct drbd_connection *connection, char **envp)
299 /* FIXME: A future version will not allow this case. */
300 if (connection->my_addr_len == 0 || connection->peer_addr_len == 0)
303 switch (((struct sockaddr *)&connection->peer_addr)->sa_family) {
306 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
307 &((struct sockaddr_in6 *)&connection->peer_addr)->sin6_addr);
311 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
312 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
316 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
317 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
319 snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
322 int drbd_khelper(struct drbd_device *device, char *cmd)
324 char *envp[] = { "HOME=/",
326 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
327 (char[20]) { }, /* address family */
328 (char[60]) { }, /* address */
331 char *argv[] = {usermode_helper, cmd, mb, NULL };
332 struct drbd_connection *connection = first_peer_device(device)->connection;
336 if (current == connection->worker.task)
337 set_bit(CALLBACK_PENDING, &connection->flags);
339 snprintf(mb, 12, "minor-%d", device_to_minor(device));
340 setup_khelper_env(connection, envp);
342 /* The helper may take some time.
343 * write out any unsynced meta data changes now */
344 drbd_md_sync(device);
346 drbd_info(device, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
347 sib.sib_reason = SIB_HELPER_PRE;
348 sib.helper_name = cmd;
349 drbd_bcast_event(device, &sib);
350 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
352 drbd_warn(device, "helper command: %s %s %s exit code %u (0x%x)\n",
353 usermode_helper, cmd, mb,
354 (ret >> 8) & 0xff, ret);
356 drbd_info(device, "helper command: %s %s %s exit code %u (0x%x)\n",
357 usermode_helper, cmd, mb,
358 (ret >> 8) & 0xff, ret);
359 sib.sib_reason = SIB_HELPER_POST;
360 sib.helper_exit_code = ret;
361 drbd_bcast_event(device, &sib);
363 if (current == connection->worker.task)
364 clear_bit(CALLBACK_PENDING, &connection->flags);
366 if (ret < 0) /* Ignore any ERRNOs we got. */
372 static int conn_khelper(struct drbd_connection *connection, char *cmd)
374 char *envp[] = { "HOME=/",
376 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
377 (char[20]) { }, /* address family */
378 (char[60]) { }, /* address */
380 char *resource_name = connection->resource->name;
381 char *argv[] = {usermode_helper, cmd, resource_name, NULL };
384 setup_khelper_env(connection, envp);
385 conn_md_sync(connection);
387 drbd_info(connection, "helper command: %s %s %s\n", usermode_helper, cmd, resource_name);
388 /* TODO: conn_bcast_event() ?? */
390 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
392 drbd_warn(connection, "helper command: %s %s %s exit code %u (0x%x)\n",
393 usermode_helper, cmd, resource_name,
394 (ret >> 8) & 0xff, ret);
396 drbd_info(connection, "helper command: %s %s %s exit code %u (0x%x)\n",
397 usermode_helper, cmd, resource_name,
398 (ret >> 8) & 0xff, ret);
399 /* TODO: conn_bcast_event() ?? */
401 if (ret < 0) /* Ignore any ERRNOs we got. */
407 static enum drbd_fencing_p highest_fencing_policy(struct drbd_connection *connection)
409 enum drbd_fencing_p fp = FP_NOT_AVAIL;
410 struct drbd_peer_device *peer_device;
414 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
415 struct drbd_device *device = peer_device->device;
416 if (get_ldev_if_state(device, D_CONSISTENT)) {
417 struct disk_conf *disk_conf =
418 rcu_dereference(peer_device->device->ldev->disk_conf);
419 fp = max_t(enum drbd_fencing_p, fp, disk_conf->fencing);
425 if (fp == FP_NOT_AVAIL) {
426 /* IO Suspending works on the whole resource.
427 Do it only for one device. */
429 peer_device = idr_get_next(&connection->peer_devices, &vnr);
430 drbd_change_state(peer_device->device, CS_VERBOSE | CS_HARD, NS(susp_fen, 0));
436 bool conn_try_outdate_peer(struct drbd_connection *connection)
438 unsigned int connect_cnt;
439 union drbd_state mask = { };
440 union drbd_state val = { };
441 enum drbd_fencing_p fp;
445 spin_lock_irq(&connection->resource->req_lock);
446 if (connection->cstate >= C_WF_REPORT_PARAMS) {
447 drbd_err(connection, "Expected cstate < C_WF_REPORT_PARAMS\n");
448 spin_unlock_irq(&connection->resource->req_lock);
452 connect_cnt = connection->connect_cnt;
453 spin_unlock_irq(&connection->resource->req_lock);
455 fp = highest_fencing_policy(connection);
458 drbd_warn(connection, "Not fencing peer, I'm not even Consistent myself.\n");
465 r = conn_khelper(connection, "fence-peer");
467 switch ((r>>8) & 0xff) {
468 case 3: /* peer is inconsistent */
469 ex_to_string = "peer is inconsistent or worse";
471 val.pdsk = D_INCONSISTENT;
473 case 4: /* peer got outdated, or was already outdated */
474 ex_to_string = "peer was fenced";
476 val.pdsk = D_OUTDATED;
478 case 5: /* peer was down */
479 if (conn_highest_disk(connection) == D_UP_TO_DATE) {
480 /* we will(have) create(d) a new UUID anyways... */
481 ex_to_string = "peer is unreachable, assumed to be dead";
483 val.pdsk = D_OUTDATED;
485 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
488 case 6: /* Peer is primary, voluntarily outdate myself.
489 * This is useful when an unconnected R_SECONDARY is asked to
490 * become R_PRIMARY, but finds the other peer being active. */
491 ex_to_string = "peer is active";
492 drbd_warn(connection, "Peer is primary, outdating myself.\n");
494 val.disk = D_OUTDATED;
497 if (fp != FP_STONITH)
498 drbd_err(connection, "fence-peer() = 7 && fencing != Stonith !!!\n");
499 ex_to_string = "peer was stonithed";
501 val.pdsk = D_OUTDATED;
504 /* The script is broken ... */
505 drbd_err(connection, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
506 return false; /* Eventually leave IO frozen */
509 drbd_info(connection, "fence-peer helper returned %d (%s)\n",
510 (r>>8) & 0xff, ex_to_string);
515 conn_request_state(connection, mask, val, CS_VERBOSE);
516 here, because we might were able to re-establish the connection in the
518 spin_lock_irq(&connection->resource->req_lock);
519 if (connection->cstate < C_WF_REPORT_PARAMS && !test_bit(STATE_SENT, &connection->flags)) {
520 if (connection->connect_cnt != connect_cnt)
521 /* In case the connection was established and droped
522 while the fence-peer handler was running, ignore it */
523 drbd_info(connection, "Ignoring fence-peer exit code\n");
525 _conn_request_state(connection, mask, val, CS_VERBOSE);
527 spin_unlock_irq(&connection->resource->req_lock);
529 return conn_highest_pdsk(connection) <= D_OUTDATED;
532 static int _try_outdate_peer_async(void *data)
534 struct drbd_connection *connection = (struct drbd_connection *)data;
536 conn_try_outdate_peer(connection);
538 kref_put(&connection->kref, drbd_destroy_connection);
542 void conn_try_outdate_peer_async(struct drbd_connection *connection)
544 struct task_struct *opa;
546 kref_get(&connection->kref);
547 opa = kthread_run(_try_outdate_peer_async, connection, "drbd_async_h");
549 drbd_err(connection, "out of mem, failed to invoke fence-peer helper\n");
550 kref_put(&connection->kref, drbd_destroy_connection);
555 drbd_set_role(struct drbd_device *device, enum drbd_role new_role, int force)
557 const int max_tries = 4;
558 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
562 union drbd_state mask, val;
564 if (new_role == R_PRIMARY) {
565 struct drbd_connection *connection;
567 /* Detect dead peers as soon as possible. */
570 for_each_connection(connection, device->resource)
571 request_ping(connection);
575 mutex_lock(device->state_mutex);
577 mask.i = 0; mask.role = R_MASK;
578 val.i = 0; val.role = new_role;
580 while (try++ < max_tries) {
581 rv = _drbd_request_state(device, mask, val, CS_WAIT_COMPLETE);
583 /* in case we first succeeded to outdate,
584 * but now suddenly could establish a connection */
585 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
591 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
592 (device->state.disk < D_UP_TO_DATE &&
593 device->state.disk >= D_INCONSISTENT)) {
595 val.disk = D_UP_TO_DATE;
600 if (rv == SS_NO_UP_TO_DATE_DISK &&
601 device->state.disk == D_CONSISTENT && mask.pdsk == 0) {
602 D_ASSERT(device, device->state.pdsk == D_UNKNOWN);
604 if (conn_try_outdate_peer(first_peer_device(device)->connection)) {
605 val.disk = D_UP_TO_DATE;
611 if (rv == SS_NOTHING_TO_DO)
613 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
614 if (!conn_try_outdate_peer(first_peer_device(device)->connection) && force) {
615 drbd_warn(device, "Forced into split brain situation!\n");
617 val.pdsk = D_OUTDATED;
622 if (rv == SS_TWO_PRIMARIES) {
623 /* Maybe the peer is detected as dead very soon...
624 retry at most once more in this case. */
627 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
628 timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
630 schedule_timeout_interruptible(timeo);
635 if (rv < SS_SUCCESS) {
636 rv = _drbd_request_state(device, mask, val,
637 CS_VERBOSE + CS_WAIT_COMPLETE);
648 drbd_warn(device, "Forced to consider local data as UpToDate!\n");
650 /* Wait until nothing is on the fly :) */
651 wait_event(device->misc_wait, atomic_read(&device->ap_pending_cnt) == 0);
653 /* FIXME also wait for all pending P_BARRIER_ACK? */
655 if (new_role == R_SECONDARY) {
656 set_disk_ro(device->vdisk, true);
657 if (get_ldev(device)) {
658 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
662 /* Called from drbd_adm_set_role only.
663 * We are still holding the conf_update mutex. */
664 nc = first_peer_device(device)->connection->net_conf;
666 nc->discard_my_data = 0; /* without copy; single bit op is atomic */
668 set_disk_ro(device->vdisk, false);
669 if (get_ldev(device)) {
670 if (((device->state.conn < C_CONNECTED ||
671 device->state.pdsk <= D_FAILED)
672 && device->ldev->md.uuid[UI_BITMAP] == 0) || forced)
673 drbd_uuid_new_current(device);
675 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
680 /* writeout of activity log covered areas of the bitmap
681 * to stable storage done in after state change already */
683 if (device->state.conn >= C_WF_REPORT_PARAMS) {
684 /* if this was forced, we should consider sync */
686 drbd_send_uuids(first_peer_device(device));
687 drbd_send_current_state(first_peer_device(device));
690 drbd_md_sync(device);
692 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
694 mutex_unlock(device->state_mutex);
698 static const char *from_attrs_err_to_txt(int err)
700 return err == -ENOMSG ? "required attribute missing" :
701 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
702 err == -EEXIST ? "can not change invariant setting" :
703 "invalid attribute value";
706 int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
708 struct drbd_config_context adm_ctx;
709 struct set_role_parms parms;
711 enum drbd_ret_code retcode;
713 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
714 if (!adm_ctx.reply_skb)
716 if (retcode != NO_ERROR)
719 memset(&parms, 0, sizeof(parms));
720 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
721 err = set_role_parms_from_attrs(&parms, info);
723 retcode = ERR_MANDATORY_TAG;
724 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
729 mutex_lock(&adm_ctx.resource->adm_mutex);
731 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
732 retcode = drbd_set_role(adm_ctx.device, R_PRIMARY, parms.assume_uptodate);
734 retcode = drbd_set_role(adm_ctx.device, R_SECONDARY, 0);
736 mutex_unlock(&adm_ctx.resource->adm_mutex);
739 drbd_adm_finish(&adm_ctx, info, retcode);
743 /* Initializes the md.*_offset members, so we are able to find
744 * the on disk meta data.
746 * We currently have two possible layouts:
748 * |----------- md_size_sect ------------------|
749 * [ 4k superblock ][ activity log ][ Bitmap ]
751 * | bm_offset = al_offset + X |
752 * ==> bitmap sectors = md_size_sect - bm_offset
755 * |----------- md_size_sect ------------------|
756 * [data.....][ Bitmap ][ activity log ][ 4k superblock ]
758 * | bm_offset = al_offset - Y |
759 * ==> bitmap sectors = Y = al_offset - bm_offset
761 * Activity log size used to be fixed 32kB,
762 * but is about to become configurable.
764 static void drbd_md_set_sector_offsets(struct drbd_device *device,
765 struct drbd_backing_dev *bdev)
767 sector_t md_size_sect = 0;
768 unsigned int al_size_sect = bdev->md.al_size_4k * 8;
770 bdev->md.md_offset = drbd_md_ss(bdev);
772 switch (bdev->md.meta_dev_idx) {
774 /* v07 style fixed size indexed meta data */
775 bdev->md.md_size_sect = MD_128MB_SECT;
776 bdev->md.al_offset = MD_4kB_SECT;
777 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
779 case DRBD_MD_INDEX_FLEX_EXT:
780 /* just occupy the full device; unit: sectors */
781 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
782 bdev->md.al_offset = MD_4kB_SECT;
783 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
785 case DRBD_MD_INDEX_INTERNAL:
786 case DRBD_MD_INDEX_FLEX_INT:
787 /* al size is still fixed */
788 bdev->md.al_offset = -al_size_sect;
789 /* we need (slightly less than) ~ this much bitmap sectors: */
790 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
791 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
792 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
793 md_size_sect = ALIGN(md_size_sect, 8);
795 /* plus the "drbd meta data super block",
796 * and the activity log; */
797 md_size_sect += MD_4kB_SECT + al_size_sect;
799 bdev->md.md_size_sect = md_size_sect;
800 /* bitmap offset is adjusted by 'super' block size */
801 bdev->md.bm_offset = -md_size_sect + MD_4kB_SECT;
806 /* input size is expected to be in KB */
807 char *ppsize(char *buf, unsigned long long size)
809 /* Needs 9 bytes at max including trailing NUL:
810 * -1ULL ==> "16384 EB" */
811 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
813 while (size >= 10000 && base < sizeof(units)-1) {
815 size = (size >> 10) + !!(size & (1<<9));
818 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
823 /* there is still a theoretical deadlock when called from receiver
824 * on an D_INCONSISTENT R_PRIMARY:
825 * remote READ does inc_ap_bio, receiver would need to receive answer
826 * packet from remote to dec_ap_bio again.
827 * receiver receive_sizes(), comes here,
828 * waits for ap_bio_cnt == 0. -> deadlock.
829 * but this cannot happen, actually, because:
830 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
831 * (not connected, or bad/no disk on peer):
832 * see drbd_fail_request_early, ap_bio_cnt is zero.
833 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
834 * peer may not initiate a resize.
836 /* Note these are not to be confused with
837 * drbd_adm_suspend_io/drbd_adm_resume_io,
838 * which are (sub) state changes triggered by admin (drbdsetup),
839 * and can be long lived.
840 * This changes an device->flag, is triggered by drbd internals,
841 * and should be short-lived. */
842 void drbd_suspend_io(struct drbd_device *device)
844 set_bit(SUSPEND_IO, &device->flags);
845 if (drbd_suspended(device))
847 wait_event(device->misc_wait, !atomic_read(&device->ap_bio_cnt));
850 void drbd_resume_io(struct drbd_device *device)
852 clear_bit(SUSPEND_IO, &device->flags);
853 wake_up(&device->misc_wait);
857 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
858 * @device: DRBD device.
860 * Returns 0 on success, negative return values indicate errors.
861 * You should call drbd_md_sync() after calling this function.
863 enum determine_dev_size
864 drbd_determine_dev_size(struct drbd_device *device, enum dds_flags flags, struct resize_parms *rs) __must_hold(local)
866 sector_t prev_first_sect, prev_size; /* previous meta location */
867 sector_t la_size_sect, u_size;
868 struct drbd_md *md = &device->ldev->md;
869 u32 prev_al_stripe_size_4k;
875 int md_moved, la_size_changed;
876 enum determine_dev_size rv = DS_UNCHANGED;
879 * application request passes inc_ap_bio,
880 * but then cannot get an AL-reference.
881 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
884 * Suspend IO right here.
885 * still lock the act_log to not trigger ASSERTs there.
887 drbd_suspend_io(device);
888 buffer = drbd_md_get_buffer(device); /* Lock meta-data IO */
890 drbd_resume_io(device);
894 /* no wait necessary anymore, actually we could assert that */
895 wait_event(device->al_wait, lc_try_lock(device->act_log));
897 prev_first_sect = drbd_md_first_sector(device->ldev);
898 prev_size = device->ldev->md.md_size_sect;
899 la_size_sect = device->ldev->md.la_size_sect;
902 /* rs is non NULL if we should change the AL layout only */
904 prev_al_stripes = md->al_stripes;
905 prev_al_stripe_size_4k = md->al_stripe_size_4k;
907 md->al_stripes = rs->al_stripes;
908 md->al_stripe_size_4k = rs->al_stripe_size / 4;
909 md->al_size_4k = (u64)rs->al_stripes * rs->al_stripe_size / 4;
912 drbd_md_set_sector_offsets(device, device->ldev);
915 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
917 size = drbd_new_dev_size(device, device->ldev, u_size, flags & DDSF_FORCED);
919 if (size < la_size_sect) {
920 if (rs && u_size == 0) {
921 /* Remove "rs &&" later. This check should always be active, but
922 right now the receiver expects the permissive behavior */
923 drbd_warn(device, "Implicit shrink not allowed. "
924 "Use --size=%llus for explicit shrink.\n",
925 (unsigned long long)size);
926 rv = DS_ERROR_SHRINK;
929 rv = DS_ERROR_SPACE_MD;
930 if (rv != DS_UNCHANGED)
934 if (drbd_get_capacity(device->this_bdev) != size ||
935 drbd_bm_capacity(device) != size) {
937 err = drbd_bm_resize(device, size, !(flags & DDSF_NO_RESYNC));
939 /* currently there is only one error: ENOMEM! */
940 size = drbd_bm_capacity(device)>>1;
942 drbd_err(device, "OUT OF MEMORY! "
943 "Could not allocate bitmap!\n");
945 drbd_err(device, "BM resizing failed. "
946 "Leaving size unchanged at size = %lu KB\n",
947 (unsigned long)size);
951 /* racy, see comments above. */
952 drbd_set_my_capacity(device, size);
953 device->ldev->md.la_size_sect = size;
954 drbd_info(device, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
955 (unsigned long long)size>>1);
960 la_size_changed = (la_size_sect != device->ldev->md.la_size_sect);
962 md_moved = prev_first_sect != drbd_md_first_sector(device->ldev)
963 || prev_size != device->ldev->md.md_size_sect;
965 if (la_size_changed || md_moved || rs) {
968 drbd_al_shrink(device); /* All extents inactive. */
970 prev_flags = md->flags;
971 md->flags &= ~MDF_PRIMARY_IND;
972 drbd_md_write(device, buffer);
974 drbd_info(device, "Writing the whole bitmap, %s\n",
975 la_size_changed && md_moved ? "size changed and md moved" :
976 la_size_changed ? "size changed" : "md moved");
977 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
978 drbd_bitmap_io(device, md_moved ? &drbd_bm_write_all : &drbd_bm_write,
979 "size changed", BM_LOCKED_MASK);
980 drbd_initialize_al(device, buffer);
982 md->flags = prev_flags;
983 drbd_md_write(device, buffer);
986 drbd_info(device, "Changed AL layout to al-stripes = %d, al-stripe-size-kB = %d\n",
987 md->al_stripes, md->al_stripe_size_4k * 4);
990 if (size > la_size_sect)
991 rv = la_size_sect ? DS_GREW : DS_GREW_FROM_ZERO;
992 if (size < la_size_sect)
998 md->al_stripes = prev_al_stripes;
999 md->al_stripe_size_4k = prev_al_stripe_size_4k;
1000 md->al_size_4k = (u64)prev_al_stripes * prev_al_stripe_size_4k;
1002 drbd_md_set_sector_offsets(device, device->ldev);
1005 lc_unlock(device->act_log);
1006 wake_up(&device->al_wait);
1007 drbd_md_put_buffer(device);
1008 drbd_resume_io(device);
1014 drbd_new_dev_size(struct drbd_device *device, struct drbd_backing_dev *bdev,
1015 sector_t u_size, int assume_peer_has_space)
1017 sector_t p_size = device->p_size; /* partner's disk size. */
1018 sector_t la_size_sect = bdev->md.la_size_sect; /* last agreed size. */
1019 sector_t m_size; /* my size */
1022 m_size = drbd_get_max_capacity(bdev);
1024 if (device->state.conn < C_CONNECTED && assume_peer_has_space) {
1025 drbd_warn(device, "Resize while not connected was forced by the user!\n");
1029 if (p_size && m_size) {
1030 size = min_t(sector_t, p_size, m_size);
1033 size = la_size_sect;
1034 if (m_size && m_size < size)
1036 if (p_size && p_size < size)
1047 drbd_err(device, "Both nodes diskless!\n");
1051 drbd_err(device, "Requested disk size is too big (%lu > %lu)\n",
1052 (unsigned long)u_size>>1, (unsigned long)size>>1);
1061 * drbd_check_al_size() - Ensures that the AL is of the right size
1062 * @device: DRBD device.
1064 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
1065 * failed, and 0 on success. You should call drbd_md_sync() after you called
1068 static int drbd_check_al_size(struct drbd_device *device, struct disk_conf *dc)
1070 struct lru_cache *n, *t;
1071 struct lc_element *e;
1072 unsigned int in_use;
1075 if (device->act_log &&
1076 device->act_log->nr_elements == dc->al_extents)
1080 t = device->act_log;
1081 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
1082 dc->al_extents, sizeof(struct lc_element), 0);
1085 drbd_err(device, "Cannot allocate act_log lru!\n");
1088 spin_lock_irq(&device->al_lock);
1090 for (i = 0; i < t->nr_elements; i++) {
1091 e = lc_element_by_index(t, i);
1093 drbd_err(device, "refcnt(%d)==%d\n",
1094 e->lc_number, e->refcnt);
1095 in_use += e->refcnt;
1099 device->act_log = n;
1100 spin_unlock_irq(&device->al_lock);
1102 drbd_err(device, "Activity log still in use!\n");
1109 drbd_md_mark_dirty(device); /* we changed device->act_log->nr_elemens */
1113 static void drbd_setup_queue_param(struct drbd_device *device, unsigned int max_bio_size)
1115 struct request_queue * const q = device->rq_queue;
1116 unsigned int max_hw_sectors = max_bio_size >> 9;
1117 unsigned int max_segments = 0;
1118 struct request_queue *b = NULL;
1120 if (get_ldev_if_state(device, D_ATTACHING)) {
1121 b = device->ldev->backing_bdev->bd_disk->queue;
1123 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
1125 max_segments = rcu_dereference(device->ldev->disk_conf)->max_bio_bvecs;
1128 blk_set_stacking_limits(&q->limits);
1129 blk_queue_max_write_same_sectors(q, 0);
1132 blk_queue_logical_block_size(q, 512);
1133 blk_queue_max_hw_sectors(q, max_hw_sectors);
1134 /* This is the workaround for "bio would need to, but cannot, be split" */
1135 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
1136 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
1139 struct drbd_connection *connection = first_peer_device(device)->connection;
1141 if (blk_queue_discard(b) &&
1142 (connection->cstate < C_CONNECTED || connection->agreed_features & FF_TRIM)) {
1143 /* For now, don't allow more than one activity log extent worth of data
1144 * to be discarded in one go. We may need to rework drbd_al_begin_io()
1145 * to allow for even larger discard ranges */
1146 q->limits.max_discard_sectors = DRBD_MAX_DISCARD_SECTORS;
1148 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
1149 /* REALLY? Is stacking secdiscard "legal"? */
1150 if (blk_queue_secdiscard(b))
1151 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q);
1153 q->limits.max_discard_sectors = 0;
1154 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
1155 queue_flag_clear_unlocked(QUEUE_FLAG_SECDISCARD, q);
1158 blk_queue_stack_limits(q, b);
1160 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
1161 drbd_info(device, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
1162 q->backing_dev_info.ra_pages,
1163 b->backing_dev_info.ra_pages);
1164 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1170 void drbd_reconsider_max_bio_size(struct drbd_device *device)
1172 unsigned int now, new, local, peer;
1174 now = queue_max_hw_sectors(device->rq_queue) << 9;
1175 local = device->local_max_bio_size; /* Eventually last known value, from volatile memory */
1176 peer = device->peer_max_bio_size; /* Eventually last known value, from meta data */
1178 if (get_ldev_if_state(device, D_ATTACHING)) {
1179 local = queue_max_hw_sectors(device->ldev->backing_bdev->bd_disk->queue) << 9;
1180 device->local_max_bio_size = local;
1183 local = min(local, DRBD_MAX_BIO_SIZE);
1185 /* We may ignore peer limits if the peer is modern enough.
1186 Because new from 8.3.8 onwards the peer can use multiple
1187 BIOs for a single peer_request */
1188 if (device->state.conn >= C_WF_REPORT_PARAMS) {
1189 if (first_peer_device(device)->connection->agreed_pro_version < 94)
1190 peer = min(device->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
1191 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
1192 else if (first_peer_device(device)->connection->agreed_pro_version == 94)
1193 peer = DRBD_MAX_SIZE_H80_PACKET;
1194 else if (first_peer_device(device)->connection->agreed_pro_version < 100)
1195 peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
1197 peer = DRBD_MAX_BIO_SIZE;
1199 /* We may later detach and re-attach on a disconnected Primary.
1200 * Avoid this setting to jump back in that case.
1201 * We want to store what we know the peer DRBD can handle,
1202 * not what the peer IO backend can handle. */
1203 if (peer > device->peer_max_bio_size)
1204 device->peer_max_bio_size = peer;
1206 new = min(local, peer);
1208 if (device->state.role == R_PRIMARY && new < now)
1209 drbd_err(device, "ASSERT FAILED new < now; (%u < %u)\n", new, now);
1212 drbd_info(device, "max BIO size = %u\n", new);
1214 drbd_setup_queue_param(device, new);
1217 /* Starts the worker thread */
1218 static void conn_reconfig_start(struct drbd_connection *connection)
1220 drbd_thread_start(&connection->worker);
1221 drbd_flush_workqueue(&connection->sender_work);
1224 /* if still unconfigured, stops worker again. */
1225 static void conn_reconfig_done(struct drbd_connection *connection)
1228 spin_lock_irq(&connection->resource->req_lock);
1229 stop_threads = conn_all_vols_unconf(connection) &&
1230 connection->cstate == C_STANDALONE;
1231 spin_unlock_irq(&connection->resource->req_lock);
1233 /* asender is implicitly stopped by receiver
1234 * in conn_disconnect() */
1235 drbd_thread_stop(&connection->receiver);
1236 drbd_thread_stop(&connection->worker);
1240 /* Make sure IO is suspended before calling this function(). */
1241 static void drbd_suspend_al(struct drbd_device *device)
1245 if (!lc_try_lock(device->act_log)) {
1246 drbd_warn(device, "Failed to lock al in drbd_suspend_al()\n");
1250 drbd_al_shrink(device);
1251 spin_lock_irq(&device->resource->req_lock);
1252 if (device->state.conn < C_CONNECTED)
1253 s = !test_and_set_bit(AL_SUSPENDED, &device->flags);
1254 spin_unlock_irq(&device->resource->req_lock);
1255 lc_unlock(device->act_log);
1258 drbd_info(device, "Suspended AL updates\n");
1262 static bool should_set_defaults(struct genl_info *info)
1264 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1265 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1268 static unsigned int drbd_al_extents_max(struct drbd_backing_dev *bdev)
1270 /* This is limited by 16 bit "slot" numbers,
1271 * and by available on-disk context storage.
1273 * Also (u16)~0 is special (denotes a "free" extent).
1275 * One transaction occupies one 4kB on-disk block,
1276 * we have n such blocks in the on disk ring buffer,
1277 * the "current" transaction may fail (n-1),
1278 * and there is 919 slot numbers context information per transaction.
1280 * 72 transaction blocks amounts to more than 2**16 context slots,
1281 * so cap there first.
1283 const unsigned int max_al_nr = DRBD_AL_EXTENTS_MAX;
1284 const unsigned int sufficient_on_disk =
1285 (max_al_nr + AL_CONTEXT_PER_TRANSACTION -1)
1286 /AL_CONTEXT_PER_TRANSACTION;
1288 unsigned int al_size_4k = bdev->md.al_size_4k;
1290 if (al_size_4k > sufficient_on_disk)
1293 return (al_size_4k - 1) * AL_CONTEXT_PER_TRANSACTION;
1296 int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1298 struct drbd_config_context adm_ctx;
1299 enum drbd_ret_code retcode;
1300 struct drbd_device *device;
1301 struct disk_conf *new_disk_conf, *old_disk_conf;
1302 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
1305 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
1306 if (!adm_ctx.reply_skb)
1308 if (retcode != NO_ERROR)
1311 device = adm_ctx.device;
1312 mutex_lock(&adm_ctx.resource->adm_mutex);
1314 /* we also need a disk
1315 * to change the options on */
1316 if (!get_ldev(device)) {
1317 retcode = ERR_NO_DISK;
1321 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
1322 if (!new_disk_conf) {
1323 retcode = ERR_NOMEM;
1327 mutex_lock(&device->resource->conf_update);
1328 old_disk_conf = device->ldev->disk_conf;
1329 *new_disk_conf = *old_disk_conf;
1330 if (should_set_defaults(info))
1331 set_disk_conf_defaults(new_disk_conf);
1333 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
1334 if (err && err != -ENOMSG) {
1335 retcode = ERR_MANDATORY_TAG;
1336 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
1340 if (!expect(new_disk_conf->resync_rate >= 1))
1341 new_disk_conf->resync_rate = 1;
1343 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1344 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
1345 if (new_disk_conf->al_extents > drbd_al_extents_max(device->ldev))
1346 new_disk_conf->al_extents = drbd_al_extents_max(device->ldev);
1348 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1349 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
1351 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
1352 if (fifo_size != device->rs_plan_s->size) {
1353 new_plan = fifo_alloc(fifo_size);
1355 drbd_err(device, "kmalloc of fifo_buffer failed");
1356 retcode = ERR_NOMEM;
1361 drbd_suspend_io(device);
1362 wait_event(device->al_wait, lc_try_lock(device->act_log));
1363 drbd_al_shrink(device);
1364 err = drbd_check_al_size(device, new_disk_conf);
1365 lc_unlock(device->act_log);
1366 wake_up(&device->al_wait);
1367 drbd_resume_io(device);
1370 retcode = ERR_NOMEM;
1374 write_lock_irq(&global_state_lock);
1375 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
1376 if (retcode == NO_ERROR) {
1377 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
1378 drbd_resync_after_changed(device);
1380 write_unlock_irq(&global_state_lock);
1382 if (retcode != NO_ERROR)
1386 old_plan = device->rs_plan_s;
1387 rcu_assign_pointer(device->rs_plan_s, new_plan);
1390 mutex_unlock(&device->resource->conf_update);
1392 if (new_disk_conf->al_updates)
1393 device->ldev->md.flags &= ~MDF_AL_DISABLED;
1395 device->ldev->md.flags |= MDF_AL_DISABLED;
1397 if (new_disk_conf->md_flushes)
1398 clear_bit(MD_NO_FUA, &device->flags);
1400 set_bit(MD_NO_FUA, &device->flags);
1402 drbd_bump_write_ordering(first_peer_device(device)->connection, WO_bdev_flush);
1404 drbd_md_sync(device);
1406 if (device->state.conn >= C_CONNECTED) {
1407 struct drbd_peer_device *peer_device;
1409 for_each_peer_device(peer_device, device)
1410 drbd_send_sync_param(peer_device);
1414 kfree(old_disk_conf);
1416 mod_timer(&device->request_timer, jiffies + HZ);
1420 mutex_unlock(&device->resource->conf_update);
1422 kfree(new_disk_conf);
1427 mutex_unlock(&adm_ctx.resource->adm_mutex);
1429 drbd_adm_finish(&adm_ctx, info, retcode);
1433 int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
1435 struct drbd_config_context adm_ctx;
1436 struct drbd_device *device;
1438 enum drbd_ret_code retcode;
1439 enum determine_dev_size dd;
1440 sector_t max_possible_sectors;
1441 sector_t min_md_device_sectors;
1442 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
1443 struct disk_conf *new_disk_conf = NULL;
1444 struct block_device *bdev;
1445 struct lru_cache *resync_lru = NULL;
1446 struct fifo_buffer *new_plan = NULL;
1447 union drbd_state ns, os;
1448 enum drbd_state_rv rv;
1449 struct net_conf *nc;
1451 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
1452 if (!adm_ctx.reply_skb)
1454 if (retcode != NO_ERROR)
1457 device = adm_ctx.device;
1458 mutex_lock(&adm_ctx.resource->adm_mutex);
1459 conn_reconfig_start(first_peer_device(device)->connection);
1461 /* if you want to reconfigure, please tear down first */
1462 if (device->state.disk > D_DISKLESS) {
1463 retcode = ERR_DISK_CONFIGURED;
1466 /* It may just now have detached because of IO error. Make sure
1467 * drbd_ldev_destroy is done already, we may end up here very fast,
1468 * e.g. if someone calls attach from the on-io-error handler,
1469 * to realize a "hot spare" feature (not that I'd recommend that) */
1470 wait_event(device->misc_wait, !atomic_read(&device->local_cnt));
1472 /* make sure there is no leftover from previous force-detach attempts */
1473 clear_bit(FORCE_DETACH, &device->flags);
1474 clear_bit(WAS_IO_ERROR, &device->flags);
1475 clear_bit(WAS_READ_ERROR, &device->flags);
1477 /* and no leftover from previously aborted resync or verify, either */
1478 device->rs_total = 0;
1479 device->rs_failed = 0;
1480 atomic_set(&device->rs_pending_cnt, 0);
1482 /* allocation not in the IO path, drbdsetup context */
1483 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1485 retcode = ERR_NOMEM;
1488 spin_lock_init(&nbc->md.uuid_lock);
1490 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
1491 if (!new_disk_conf) {
1492 retcode = ERR_NOMEM;
1495 nbc->disk_conf = new_disk_conf;
1497 set_disk_conf_defaults(new_disk_conf);
1498 err = disk_conf_from_attrs(new_disk_conf, info);
1500 retcode = ERR_MANDATORY_TAG;
1501 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
1505 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1506 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
1508 new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
1510 retcode = ERR_NOMEM;
1514 if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
1515 retcode = ERR_MD_IDX_INVALID;
1519 write_lock_irq(&global_state_lock);
1520 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
1521 write_unlock_irq(&global_state_lock);
1522 if (retcode != NO_ERROR)
1526 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
1528 if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
1530 retcode = ERR_STONITH_AND_PROT_A;
1536 bdev = blkdev_get_by_path(new_disk_conf->backing_dev,
1537 FMODE_READ | FMODE_WRITE | FMODE_EXCL, device);
1539 drbd_err(device, "open(\"%s\") failed with %ld\n", new_disk_conf->backing_dev,
1541 retcode = ERR_OPEN_DISK;
1544 nbc->backing_bdev = bdev;
1547 * meta_dev_idx >= 0: external fixed size, possibly multiple
1548 * drbd sharing one meta device. TODO in that case, paranoia
1549 * check that [md_bdev, meta_dev_idx] is not yet used by some
1550 * other drbd minor! (if you use drbd.conf + drbdadm, that
1551 * should check it for you already; but if you don't, or
1552 * someone fooled it, we need to double check here)
1554 bdev = blkdev_get_by_path(new_disk_conf->meta_dev,
1555 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
1556 (new_disk_conf->meta_dev_idx < 0) ?
1557 (void *)device : (void *)drbd_m_holder);
1559 drbd_err(device, "open(\"%s\") failed with %ld\n", new_disk_conf->meta_dev,
1561 retcode = ERR_OPEN_MD_DISK;
1564 nbc->md_bdev = bdev;
1566 if ((nbc->backing_bdev == nbc->md_bdev) !=
1567 (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1568 new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
1569 retcode = ERR_MD_IDX_INVALID;
1573 resync_lru = lc_create("resync", drbd_bm_ext_cache,
1574 1, 61, sizeof(struct bm_extent),
1575 offsetof(struct bm_extent, lce));
1577 retcode = ERR_NOMEM;
1581 /* Read our meta data super block early.
1582 * This also sets other on-disk offsets. */
1583 retcode = drbd_md_read(device, nbc);
1584 if (retcode != NO_ERROR)
1587 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1588 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
1589 if (new_disk_conf->al_extents > drbd_al_extents_max(nbc))
1590 new_disk_conf->al_extents = drbd_al_extents_max(nbc);
1592 if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
1593 drbd_err(device, "max capacity %llu smaller than disk size %llu\n",
1594 (unsigned long long) drbd_get_max_capacity(nbc),
1595 (unsigned long long) new_disk_conf->disk_size);
1596 retcode = ERR_DISK_TOO_SMALL;
1600 if (new_disk_conf->meta_dev_idx < 0) {
1601 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1602 /* at least one MB, otherwise it does not make sense */
1603 min_md_device_sectors = (2<<10);
1605 max_possible_sectors = DRBD_MAX_SECTORS;
1606 min_md_device_sectors = MD_128MB_SECT * (new_disk_conf->meta_dev_idx + 1);
1609 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
1610 retcode = ERR_MD_DISK_TOO_SMALL;
1611 drbd_warn(device, "refusing attach: md-device too small, "
1612 "at least %llu sectors needed for this meta-disk type\n",
1613 (unsigned long long) min_md_device_sectors);
1617 /* Make sure the new disk is big enough
1618 * (we may currently be R_PRIMARY with no local disk...) */
1619 if (drbd_get_max_capacity(nbc) <
1620 drbd_get_capacity(device->this_bdev)) {
1621 retcode = ERR_DISK_TOO_SMALL;
1625 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1627 if (nbc->known_size > max_possible_sectors) {
1628 drbd_warn(device, "==> truncating very big lower level device "
1629 "to currently maximum possible %llu sectors <==\n",
1630 (unsigned long long) max_possible_sectors);
1631 if (new_disk_conf->meta_dev_idx >= 0)
1632 drbd_warn(device, "==>> using internal or flexible "
1633 "meta data may help <<==\n");
1636 drbd_suspend_io(device);
1637 /* also wait for the last barrier ack. */
1638 /* FIXME see also https://daiquiri.linbit/cgi-bin/bugzilla/show_bug.cgi?id=171
1639 * We need a way to either ignore barrier acks for barriers sent before a device
1640 * was attached, or a way to wait for all pending barrier acks to come in.
1641 * As barriers are counted per resource,
1642 * we'd need to suspend io on all devices of a resource.
1644 wait_event(device->misc_wait, !atomic_read(&device->ap_pending_cnt) || drbd_suspended(device));
1645 /* and for any other previously queued work */
1646 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
1648 rv = _drbd_request_state(device, NS(disk, D_ATTACHING), CS_VERBOSE);
1649 retcode = rv; /* FIXME: Type mismatch. */
1650 drbd_resume_io(device);
1651 if (rv < SS_SUCCESS)
1654 if (!get_ldev_if_state(device, D_ATTACHING))
1655 goto force_diskless;
1657 if (!device->bitmap) {
1658 if (drbd_bm_init(device)) {
1659 retcode = ERR_NOMEM;
1660 goto force_diskless_dec;
1664 if (device->state.conn < C_CONNECTED &&
1665 device->state.role == R_PRIMARY && device->ed_uuid &&
1666 (device->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
1667 drbd_err(device, "Can only attach to data with current UUID=%016llX\n",
1668 (unsigned long long)device->ed_uuid);
1669 retcode = ERR_DATA_NOT_CURRENT;
1670 goto force_diskless_dec;
1673 /* Since we are diskless, fix the activity log first... */
1674 if (drbd_check_al_size(device, new_disk_conf)) {
1675 retcode = ERR_NOMEM;
1676 goto force_diskless_dec;
1679 /* Prevent shrinking of consistent devices ! */
1680 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
1681 drbd_new_dev_size(device, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
1682 drbd_warn(device, "refusing to truncate a consistent device\n");
1683 retcode = ERR_DISK_TOO_SMALL;
1684 goto force_diskless_dec;
1687 /* Reset the "barriers don't work" bits here, then force meta data to
1688 * be written, to ensure we determine if barriers are supported. */
1689 if (new_disk_conf->md_flushes)
1690 clear_bit(MD_NO_FUA, &device->flags);
1692 set_bit(MD_NO_FUA, &device->flags);
1694 /* Point of no return reached.
1695 * Devices and memory are no longer released by error cleanup below.
1696 * now device takes over responsibility, and the state engine should
1697 * clean it up somewhere. */
1698 D_ASSERT(device, device->ldev == NULL);
1700 device->resync = resync_lru;
1701 device->rs_plan_s = new_plan;
1704 new_disk_conf = NULL;
1707 drbd_bump_write_ordering(first_peer_device(device)->connection, WO_bdev_flush);
1709 if (drbd_md_test_flag(device->ldev, MDF_CRASHED_PRIMARY))
1710 set_bit(CRASHED_PRIMARY, &device->flags);
1712 clear_bit(CRASHED_PRIMARY, &device->flags);
1714 if (drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
1715 !(device->state.role == R_PRIMARY && device->resource->susp_nod))
1716 set_bit(CRASHED_PRIMARY, &device->flags);
1718 device->send_cnt = 0;
1719 device->recv_cnt = 0;
1720 device->read_cnt = 0;
1721 device->writ_cnt = 0;
1723 drbd_reconsider_max_bio_size(device);
1725 /* If I am currently not R_PRIMARY,
1726 * but meta data primary indicator is set,
1727 * I just now recover from a hard crash,
1728 * and have been R_PRIMARY before that crash.
1730 * Now, if I had no connection before that crash
1731 * (have been degraded R_PRIMARY), chances are that
1732 * I won't find my peer now either.
1734 * In that case, and _only_ in that case,
1735 * we use the degr-wfc-timeout instead of the default,
1736 * so we can automatically recover from a crash of a
1737 * degraded but active "cluster" after a certain timeout.
1739 clear_bit(USE_DEGR_WFC_T, &device->flags);
1740 if (device->state.role != R_PRIMARY &&
1741 drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
1742 !drbd_md_test_flag(device->ldev, MDF_CONNECTED_IND))
1743 set_bit(USE_DEGR_WFC_T, &device->flags);
1745 dd = drbd_determine_dev_size(device, 0, NULL);
1746 if (dd <= DS_ERROR) {
1747 retcode = ERR_NOMEM_BITMAP;
1748 goto force_diskless_dec;
1749 } else if (dd == DS_GREW)
1750 set_bit(RESYNC_AFTER_NEG, &device->flags);
1752 if (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC) ||
1753 (test_bit(CRASHED_PRIMARY, &device->flags) &&
1754 drbd_md_test_flag(device->ldev, MDF_AL_DISABLED))) {
1755 drbd_info(device, "Assuming that all blocks are out of sync "
1756 "(aka FullSync)\n");
1757 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
1758 "set_n_write from attaching", BM_LOCKED_MASK)) {
1759 retcode = ERR_IO_MD_DISK;
1760 goto force_diskless_dec;
1763 if (drbd_bitmap_io(device, &drbd_bm_read,
1764 "read from attaching", BM_LOCKED_MASK)) {
1765 retcode = ERR_IO_MD_DISK;
1766 goto force_diskless_dec;
1770 if (_drbd_bm_total_weight(device) == drbd_bm_bits(device))
1771 drbd_suspend_al(device); /* IO is still suspended here... */
1773 spin_lock_irq(&device->resource->req_lock);
1774 os = drbd_read_state(device);
1776 /* If MDF_CONSISTENT is not set go into inconsistent state,
1777 otherwise investigate MDF_WasUpToDate...
1778 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1779 otherwise into D_CONSISTENT state.
1781 if (drbd_md_test_flag(device->ldev, MDF_CONSISTENT)) {
1782 if (drbd_md_test_flag(device->ldev, MDF_WAS_UP_TO_DATE))
1783 ns.disk = D_CONSISTENT;
1785 ns.disk = D_OUTDATED;
1787 ns.disk = D_INCONSISTENT;
1790 if (drbd_md_test_flag(device->ldev, MDF_PEER_OUT_DATED))
1791 ns.pdsk = D_OUTDATED;
1794 if (ns.disk == D_CONSISTENT &&
1795 (ns.pdsk == D_OUTDATED || rcu_dereference(device->ldev->disk_conf)->fencing == FP_DONT_CARE))
1796 ns.disk = D_UP_TO_DATE;
1798 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1799 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1800 this point, because drbd_request_state() modifies these
1803 if (rcu_dereference(device->ldev->disk_conf)->al_updates)
1804 device->ldev->md.flags &= ~MDF_AL_DISABLED;
1806 device->ldev->md.flags |= MDF_AL_DISABLED;
1810 /* In case we are C_CONNECTED postpone any decision on the new disk
1811 state after the negotiation phase. */
1812 if (device->state.conn == C_CONNECTED) {
1813 device->new_state_tmp.i = ns.i;
1815 ns.disk = D_NEGOTIATING;
1817 /* We expect to receive up-to-date UUIDs soon.
1818 To avoid a race in receive_state, free p_uuid while
1819 holding req_lock. I.e. atomic with the state change */
1820 kfree(device->p_uuid);
1821 device->p_uuid = NULL;
1824 rv = _drbd_set_state(device, ns, CS_VERBOSE, NULL);
1825 spin_unlock_irq(&device->resource->req_lock);
1827 if (rv < SS_SUCCESS)
1828 goto force_diskless_dec;
1830 mod_timer(&device->request_timer, jiffies + HZ);
1832 if (device->state.role == R_PRIMARY)
1833 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
1835 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
1837 drbd_md_mark_dirty(device);
1838 drbd_md_sync(device);
1840 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
1842 conn_reconfig_done(first_peer_device(device)->connection);
1843 mutex_unlock(&adm_ctx.resource->adm_mutex);
1844 drbd_adm_finish(&adm_ctx, info, retcode);
1850 drbd_force_state(device, NS(disk, D_DISKLESS));
1851 drbd_md_sync(device);
1853 conn_reconfig_done(first_peer_device(device)->connection);
1855 if (nbc->backing_bdev)
1856 blkdev_put(nbc->backing_bdev,
1857 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1859 blkdev_put(nbc->md_bdev,
1860 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1863 kfree(new_disk_conf);
1864 lc_destroy(resync_lru);
1866 mutex_unlock(&adm_ctx.resource->adm_mutex);
1868 drbd_adm_finish(&adm_ctx, info, retcode);
1872 static int adm_detach(struct drbd_device *device, int force)
1874 enum drbd_state_rv retcode;
1878 set_bit(FORCE_DETACH, &device->flags);
1879 drbd_force_state(device, NS(disk, D_FAILED));
1880 retcode = SS_SUCCESS;
1884 drbd_suspend_io(device); /* so no-one is stuck in drbd_al_begin_io */
1885 drbd_md_get_buffer(device); /* make sure there is no in-flight meta-data IO */
1886 retcode = drbd_request_state(device, NS(disk, D_FAILED));
1887 drbd_md_put_buffer(device);
1888 /* D_FAILED will transition to DISKLESS. */
1889 ret = wait_event_interruptible(device->misc_wait,
1890 device->state.disk != D_FAILED);
1891 drbd_resume_io(device);
1892 if ((int)retcode == (int)SS_IS_DISKLESS)
1893 retcode = SS_NOTHING_TO_DO;
1900 /* Detaching the disk is a process in multiple stages. First we need to lock
1901 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1902 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1903 * internal references as well.
1904 * Only then we have finally detached. */
1905 int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
1907 struct drbd_config_context adm_ctx;
1908 enum drbd_ret_code retcode;
1909 struct detach_parms parms = { };
1912 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
1913 if (!adm_ctx.reply_skb)
1915 if (retcode != NO_ERROR)
1918 if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
1919 err = detach_parms_from_attrs(&parms, info);
1921 retcode = ERR_MANDATORY_TAG;
1922 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
1927 mutex_lock(&adm_ctx.resource->adm_mutex);
1928 retcode = adm_detach(adm_ctx.device, parms.force_detach);
1929 mutex_unlock(&adm_ctx.resource->adm_mutex);
1931 drbd_adm_finish(&adm_ctx, info, retcode);
1935 static bool conn_resync_running(struct drbd_connection *connection)
1937 struct drbd_peer_device *peer_device;
1942 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1943 struct drbd_device *device = peer_device->device;
1944 if (device->state.conn == C_SYNC_SOURCE ||
1945 device->state.conn == C_SYNC_TARGET ||
1946 device->state.conn == C_PAUSED_SYNC_S ||
1947 device->state.conn == C_PAUSED_SYNC_T) {
1957 static bool conn_ov_running(struct drbd_connection *connection)
1959 struct drbd_peer_device *peer_device;
1964 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1965 struct drbd_device *device = peer_device->device;
1966 if (device->state.conn == C_VERIFY_S ||
1967 device->state.conn == C_VERIFY_T) {
1977 static enum drbd_ret_code
1978 _check_net_options(struct drbd_connection *connection, struct net_conf *old_net_conf, struct net_conf *new_net_conf)
1980 struct drbd_peer_device *peer_device;
1983 if (old_net_conf && connection->cstate == C_WF_REPORT_PARAMS && connection->agreed_pro_version < 100) {
1984 if (new_net_conf->wire_protocol != old_net_conf->wire_protocol)
1985 return ERR_NEED_APV_100;
1987 if (new_net_conf->two_primaries != old_net_conf->two_primaries)
1988 return ERR_NEED_APV_100;
1990 if (strcmp(new_net_conf->integrity_alg, old_net_conf->integrity_alg))
1991 return ERR_NEED_APV_100;
1994 if (!new_net_conf->two_primaries &&
1995 conn_highest_role(connection) == R_PRIMARY &&
1996 conn_highest_peer(connection) == R_PRIMARY)
1997 return ERR_NEED_ALLOW_TWO_PRI;
1999 if (new_net_conf->two_primaries &&
2000 (new_net_conf->wire_protocol != DRBD_PROT_C))
2001 return ERR_NOT_PROTO_C;
2003 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2004 struct drbd_device *device = peer_device->device;
2005 if (get_ldev(device)) {
2006 enum drbd_fencing_p fp = rcu_dereference(device->ldev->disk_conf)->fencing;
2008 if (new_net_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
2009 return ERR_STONITH_AND_PROT_A;
2011 if (device->state.role == R_PRIMARY && new_net_conf->discard_my_data)
2012 return ERR_DISCARD_IMPOSSIBLE;
2015 if (new_net_conf->on_congestion != OC_BLOCK && new_net_conf->wire_protocol != DRBD_PROT_A)
2016 return ERR_CONG_NOT_PROTO_A;
2021 static enum drbd_ret_code
2022 check_net_options(struct drbd_connection *connection, struct net_conf *new_net_conf)
2024 static enum drbd_ret_code rv;
2025 struct drbd_peer_device *peer_device;
2029 rv = _check_net_options(connection, rcu_dereference(connection->net_conf), new_net_conf);
2032 /* connection->volumes protected by genl_lock() here */
2033 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2034 struct drbd_device *device = peer_device->device;
2035 if (!device->bitmap) {
2036 if (drbd_bm_init(device))
2045 struct crypto_hash *verify_tfm;
2046 struct crypto_hash *csums_tfm;
2047 struct crypto_hash *cram_hmac_tfm;
2048 struct crypto_hash *integrity_tfm;
2052 alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
2057 *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
2066 static enum drbd_ret_code
2067 alloc_crypto(struct crypto *crypto, struct net_conf *new_net_conf)
2069 char hmac_name[CRYPTO_MAX_ALG_NAME];
2070 enum drbd_ret_code rv;
2072 rv = alloc_hash(&crypto->csums_tfm, new_net_conf->csums_alg,
2076 rv = alloc_hash(&crypto->verify_tfm, new_net_conf->verify_alg,
2080 rv = alloc_hash(&crypto->integrity_tfm, new_net_conf->integrity_alg,
2084 if (new_net_conf->cram_hmac_alg[0] != 0) {
2085 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
2086 new_net_conf->cram_hmac_alg);
2088 rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
2095 static void free_crypto(struct crypto *crypto)
2097 crypto_free_hash(crypto->cram_hmac_tfm);
2098 crypto_free_hash(crypto->integrity_tfm);
2099 crypto_free_hash(crypto->csums_tfm);
2100 crypto_free_hash(crypto->verify_tfm);
2103 int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
2105 struct drbd_config_context adm_ctx;
2106 enum drbd_ret_code retcode;
2107 struct drbd_connection *connection;
2108 struct net_conf *old_net_conf, *new_net_conf = NULL;
2110 int ovr; /* online verify running */
2111 int rsr; /* re-sync running */
2112 struct crypto crypto = { };
2114 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_CONNECTION);
2115 if (!adm_ctx.reply_skb)
2117 if (retcode != NO_ERROR)
2120 connection = adm_ctx.connection;
2121 mutex_lock(&adm_ctx.resource->adm_mutex);
2123 new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
2124 if (!new_net_conf) {
2125 retcode = ERR_NOMEM;
2129 conn_reconfig_start(connection);
2131 mutex_lock(&connection->data.mutex);
2132 mutex_lock(&connection->resource->conf_update);
2133 old_net_conf = connection->net_conf;
2135 if (!old_net_conf) {
2136 drbd_msg_put_info(adm_ctx.reply_skb, "net conf missing, try connect");
2137 retcode = ERR_INVALID_REQUEST;
2141 *new_net_conf = *old_net_conf;
2142 if (should_set_defaults(info))
2143 set_net_conf_defaults(new_net_conf);
2145 err = net_conf_from_attrs_for_change(new_net_conf, info);
2146 if (err && err != -ENOMSG) {
2147 retcode = ERR_MANDATORY_TAG;
2148 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
2152 retcode = check_net_options(connection, new_net_conf);
2153 if (retcode != NO_ERROR)
2156 /* re-sync running */
2157 rsr = conn_resync_running(connection);
2158 if (rsr && strcmp(new_net_conf->csums_alg, old_net_conf->csums_alg)) {
2159 retcode = ERR_CSUMS_RESYNC_RUNNING;
2163 /* online verify running */
2164 ovr = conn_ov_running(connection);
2165 if (ovr && strcmp(new_net_conf->verify_alg, old_net_conf->verify_alg)) {
2166 retcode = ERR_VERIFY_RUNNING;
2170 retcode = alloc_crypto(&crypto, new_net_conf);
2171 if (retcode != NO_ERROR)
2174 rcu_assign_pointer(connection->net_conf, new_net_conf);
2177 crypto_free_hash(connection->csums_tfm);
2178 connection->csums_tfm = crypto.csums_tfm;
2179 crypto.csums_tfm = NULL;
2182 crypto_free_hash(connection->verify_tfm);
2183 connection->verify_tfm = crypto.verify_tfm;
2184 crypto.verify_tfm = NULL;
2187 crypto_free_hash(connection->integrity_tfm);
2188 connection->integrity_tfm = crypto.integrity_tfm;
2189 if (connection->cstate >= C_WF_REPORT_PARAMS && connection->agreed_pro_version >= 100)
2190 /* Do this without trying to take connection->data.mutex again. */
2191 __drbd_send_protocol(connection, P_PROTOCOL_UPDATE);
2193 crypto_free_hash(connection->cram_hmac_tfm);
2194 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
2196 mutex_unlock(&connection->resource->conf_update);
2197 mutex_unlock(&connection->data.mutex);
2199 kfree(old_net_conf);
2201 if (connection->cstate >= C_WF_REPORT_PARAMS) {
2202 struct drbd_peer_device *peer_device;
2205 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
2206 drbd_send_sync_param(peer_device);
2212 mutex_unlock(&connection->resource->conf_update);
2213 mutex_unlock(&connection->data.mutex);
2214 free_crypto(&crypto);
2215 kfree(new_net_conf);
2217 conn_reconfig_done(connection);
2219 mutex_unlock(&adm_ctx.resource->adm_mutex);
2221 drbd_adm_finish(&adm_ctx, info, retcode);
2225 int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
2227 struct drbd_config_context adm_ctx;
2228 struct drbd_peer_device *peer_device;
2229 struct net_conf *old_net_conf, *new_net_conf = NULL;
2230 struct crypto crypto = { };
2231 struct drbd_resource *resource;
2232 struct drbd_connection *connection;
2233 enum drbd_ret_code retcode;
2237 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
2239 if (!adm_ctx.reply_skb)
2241 if (retcode != NO_ERROR)
2243 if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
2244 drbd_msg_put_info(adm_ctx.reply_skb, "connection endpoint(s) missing");
2245 retcode = ERR_INVALID_REQUEST;
2249 /* No need for _rcu here. All reconfiguration is
2250 * strictly serialized on genl_lock(). We are protected against
2251 * concurrent reconfiguration/addition/deletion */
2252 for_each_resource(resource, &drbd_resources) {
2253 for_each_connection(connection, resource) {
2254 if (nla_len(adm_ctx.my_addr) == connection->my_addr_len &&
2255 !memcmp(nla_data(adm_ctx.my_addr), &connection->my_addr,
2256 connection->my_addr_len)) {
2257 retcode = ERR_LOCAL_ADDR;
2261 if (nla_len(adm_ctx.peer_addr) == connection->peer_addr_len &&
2262 !memcmp(nla_data(adm_ctx.peer_addr), &connection->peer_addr,
2263 connection->peer_addr_len)) {
2264 retcode = ERR_PEER_ADDR;
2270 mutex_lock(&adm_ctx.resource->adm_mutex);
2271 connection = first_connection(adm_ctx.resource);
2272 conn_reconfig_start(connection);
2274 if (connection->cstate > C_STANDALONE) {
2275 retcode = ERR_NET_CONFIGURED;
2279 /* allocation not in the IO path, drbdsetup / netlink process context */
2280 new_net_conf = kzalloc(sizeof(*new_net_conf), GFP_KERNEL);
2281 if (!new_net_conf) {
2282 retcode = ERR_NOMEM;
2286 set_net_conf_defaults(new_net_conf);
2288 err = net_conf_from_attrs(new_net_conf, info);
2289 if (err && err != -ENOMSG) {
2290 retcode = ERR_MANDATORY_TAG;
2291 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
2295 retcode = check_net_options(connection, new_net_conf);
2296 if (retcode != NO_ERROR)
2299 retcode = alloc_crypto(&crypto, new_net_conf);
2300 if (retcode != NO_ERROR)
2303 ((char *)new_net_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
2305 drbd_flush_workqueue(&connection->sender_work);
2307 mutex_lock(&adm_ctx.resource->conf_update);
2308 old_net_conf = connection->net_conf;
2310 retcode = ERR_NET_CONFIGURED;
2311 mutex_unlock(&adm_ctx.resource->conf_update);
2314 rcu_assign_pointer(connection->net_conf, new_net_conf);
2316 conn_free_crypto(connection);
2317 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
2318 connection->integrity_tfm = crypto.integrity_tfm;
2319 connection->csums_tfm = crypto.csums_tfm;
2320 connection->verify_tfm = crypto.verify_tfm;
2322 connection->my_addr_len = nla_len(adm_ctx.my_addr);
2323 memcpy(&connection->my_addr, nla_data(adm_ctx.my_addr), connection->my_addr_len);
2324 connection->peer_addr_len = nla_len(adm_ctx.peer_addr);
2325 memcpy(&connection->peer_addr, nla_data(adm_ctx.peer_addr), connection->peer_addr_len);
2327 mutex_unlock(&adm_ctx.resource->conf_update);
2330 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2331 struct drbd_device *device = peer_device->device;
2332 device->send_cnt = 0;
2333 device->recv_cnt = 0;
2337 retcode = conn_request_state(connection, NS(conn, C_UNCONNECTED), CS_VERBOSE);
2339 conn_reconfig_done(connection);
2340 mutex_unlock(&adm_ctx.resource->adm_mutex);
2341 drbd_adm_finish(&adm_ctx, info, retcode);
2345 free_crypto(&crypto);
2346 kfree(new_net_conf);
2348 conn_reconfig_done(connection);
2349 mutex_unlock(&adm_ctx.resource->adm_mutex);
2351 drbd_adm_finish(&adm_ctx, info, retcode);
2355 static enum drbd_state_rv conn_try_disconnect(struct drbd_connection *connection, bool force)
2357 enum drbd_state_rv rv;
2359 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
2360 force ? CS_HARD : 0);
2363 case SS_NOTHING_TO_DO:
2365 case SS_ALREADY_STANDALONE:
2367 case SS_PRIMARY_NOP:
2368 /* Our state checking code wants to see the peer outdated. */
2369 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING, pdsk, D_OUTDATED), 0);
2371 if (rv == SS_OUTDATE_WO_CONN) /* lost connection before graceful disconnect succeeded */
2372 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_VERBOSE);
2375 case SS_CW_FAILED_BY_PEER:
2376 /* The peer probably wants to see us outdated. */
2377 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING,
2378 disk, D_OUTDATED), 0);
2379 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
2380 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
2385 /* no special handling necessary */
2388 if (rv >= SS_SUCCESS) {
2389 enum drbd_state_rv rv2;
2390 /* No one else can reconfigure the network while I am here.
2391 * The state handling only uses drbd_thread_stop_nowait(),
2392 * we want to really wait here until the receiver is no more.
2394 drbd_thread_stop(&connection->receiver);
2396 /* Race breaker. This additional state change request may be
2397 * necessary, if this was a forced disconnect during a receiver
2398 * restart. We may have "killed" the receiver thread just
2399 * after drbd_receiver() returned. Typically, we should be
2400 * C_STANDALONE already, now, and this becomes a no-op.
2402 rv2 = conn_request_state(connection, NS(conn, C_STANDALONE),
2403 CS_VERBOSE | CS_HARD);
2404 if (rv2 < SS_SUCCESS)
2405 drbd_err(connection,
2406 "unexpected rv2=%d in conn_try_disconnect()\n",
2412 int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
2414 struct drbd_config_context adm_ctx;
2415 struct disconnect_parms parms;
2416 struct drbd_connection *connection;
2417 enum drbd_state_rv rv;
2418 enum drbd_ret_code retcode;
2421 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_CONNECTION);
2422 if (!adm_ctx.reply_skb)
2424 if (retcode != NO_ERROR)
2427 connection = adm_ctx.connection;
2428 memset(&parms, 0, sizeof(parms));
2429 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
2430 err = disconnect_parms_from_attrs(&parms, info);
2432 retcode = ERR_MANDATORY_TAG;
2433 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
2438 mutex_lock(&adm_ctx.resource->adm_mutex);
2439 rv = conn_try_disconnect(connection, parms.force_disconnect);
2440 if (rv < SS_SUCCESS)
2441 retcode = rv; /* FIXME: Type mismatch. */
2444 mutex_unlock(&adm_ctx.resource->adm_mutex);
2446 drbd_adm_finish(&adm_ctx, info, retcode);
2450 void resync_after_online_grow(struct drbd_device *device)
2452 int iass; /* I am sync source */
2454 drbd_info(device, "Resync of new storage after online grow\n");
2455 if (device->state.role != device->state.peer)
2456 iass = (device->state.role == R_PRIMARY);
2458 iass = test_bit(RESOLVE_CONFLICTS, &first_peer_device(device)->connection->flags);
2461 drbd_start_resync(device, C_SYNC_SOURCE);
2463 _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
2466 int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
2468 struct drbd_config_context adm_ctx;
2469 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
2470 struct resize_parms rs;
2471 struct drbd_device *device;
2472 enum drbd_ret_code retcode;
2473 enum determine_dev_size dd;
2474 bool change_al_layout = false;
2475 enum dds_flags ddsf;
2479 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
2480 if (!adm_ctx.reply_skb)
2482 if (retcode != NO_ERROR)
2485 mutex_lock(&adm_ctx.resource->adm_mutex);
2486 device = adm_ctx.device;
2487 if (!get_ldev(device)) {
2488 retcode = ERR_NO_DISK;
2492 memset(&rs, 0, sizeof(struct resize_parms));
2493 rs.al_stripes = device->ldev->md.al_stripes;
2494 rs.al_stripe_size = device->ldev->md.al_stripe_size_4k * 4;
2495 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
2496 err = resize_parms_from_attrs(&rs, info);
2498 retcode = ERR_MANDATORY_TAG;
2499 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
2504 if (device->state.conn > C_CONNECTED) {
2505 retcode = ERR_RESIZE_RESYNC;
2509 if (device->state.role == R_SECONDARY &&
2510 device->state.peer == R_SECONDARY) {
2511 retcode = ERR_NO_PRIMARY;
2515 if (rs.no_resync && first_peer_device(device)->connection->agreed_pro_version < 93) {
2516 retcode = ERR_NEED_APV_93;
2521 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
2523 if (u_size != (sector_t)rs.resize_size) {
2524 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
2525 if (!new_disk_conf) {
2526 retcode = ERR_NOMEM;
2531 if (device->ldev->md.al_stripes != rs.al_stripes ||
2532 device->ldev->md.al_stripe_size_4k != rs.al_stripe_size / 4) {
2533 u32 al_size_k = rs.al_stripes * rs.al_stripe_size;
2535 if (al_size_k > (16 * 1024 * 1024)) {
2536 retcode = ERR_MD_LAYOUT_TOO_BIG;
2540 if (al_size_k < MD_32kB_SECT/2) {
2541 retcode = ERR_MD_LAYOUT_TOO_SMALL;
2545 if (device->state.conn != C_CONNECTED && !rs.resize_force) {
2546 retcode = ERR_MD_LAYOUT_CONNECTED;
2550 change_al_layout = true;
2553 if (device->ldev->known_size != drbd_get_capacity(device->ldev->backing_bdev))
2554 device->ldev->known_size = drbd_get_capacity(device->ldev->backing_bdev);
2556 if (new_disk_conf) {
2557 mutex_lock(&device->resource->conf_update);
2558 old_disk_conf = device->ldev->disk_conf;
2559 *new_disk_conf = *old_disk_conf;
2560 new_disk_conf->disk_size = (sector_t)rs.resize_size;
2561 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
2562 mutex_unlock(&device->resource->conf_update);
2564 kfree(old_disk_conf);
2567 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
2568 dd = drbd_determine_dev_size(device, ddsf, change_al_layout ? &rs : NULL);
2569 drbd_md_sync(device);
2571 if (dd == DS_ERROR) {
2572 retcode = ERR_NOMEM_BITMAP;
2574 } else if (dd == DS_ERROR_SPACE_MD) {
2575 retcode = ERR_MD_LAYOUT_NO_FIT;
2577 } else if (dd == DS_ERROR_SHRINK) {
2578 retcode = ERR_IMPLICIT_SHRINK;
2582 if (device->state.conn == C_CONNECTED) {
2584 set_bit(RESIZE_PENDING, &device->flags);
2586 drbd_send_uuids(first_peer_device(device));
2587 drbd_send_sizes(first_peer_device(device), 1, ddsf);
2591 mutex_unlock(&adm_ctx.resource->adm_mutex);
2593 drbd_adm_finish(&adm_ctx, info, retcode);
2601 int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
2603 struct drbd_config_context adm_ctx;
2604 enum drbd_ret_code retcode;
2605 struct res_opts res_opts;
2608 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
2609 if (!adm_ctx.reply_skb)
2611 if (retcode != NO_ERROR)
2614 res_opts = adm_ctx.resource->res_opts;
2615 if (should_set_defaults(info))
2616 set_res_opts_defaults(&res_opts);
2618 err = res_opts_from_attrs(&res_opts, info);
2619 if (err && err != -ENOMSG) {
2620 retcode = ERR_MANDATORY_TAG;
2621 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
2625 mutex_lock(&adm_ctx.resource->adm_mutex);
2626 err = set_resource_options(adm_ctx.resource, &res_opts);
2628 retcode = ERR_INVALID_REQUEST;
2630 retcode = ERR_NOMEM;
2632 mutex_unlock(&adm_ctx.resource->adm_mutex);
2635 drbd_adm_finish(&adm_ctx, info, retcode);
2639 int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
2641 struct drbd_config_context adm_ctx;
2642 struct drbd_device *device;
2643 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2645 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
2646 if (!adm_ctx.reply_skb)
2648 if (retcode != NO_ERROR)
2651 mutex_lock(&adm_ctx.resource->adm_mutex);
2652 device = adm_ctx.device;
2654 /* If there is still bitmap IO pending, probably because of a previous
2655 * resync just being finished, wait for it before requesting a new resync.
2656 * Also wait for it's after_state_ch(). */
2657 drbd_suspend_io(device);
2658 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
2659 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
2661 /* If we happen to be C_STANDALONE R_SECONDARY, just change to
2662 * D_INCONSISTENT, and set all bits in the bitmap. Otherwise,
2663 * try to start a resync handshake as sync target for full sync.
2665 if (device->state.conn == C_STANDALONE && device->state.role == R_SECONDARY) {
2666 retcode = drbd_request_state(device, NS(disk, D_INCONSISTENT));
2667 if (retcode >= SS_SUCCESS) {
2668 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
2669 "set_n_write from invalidate", BM_LOCKED_MASK))
2670 retcode = ERR_IO_MD_DISK;
2673 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_T));
2674 drbd_resume_io(device);
2675 mutex_unlock(&adm_ctx.resource->adm_mutex);
2677 drbd_adm_finish(&adm_ctx, info, retcode);
2681 static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2682 union drbd_state mask, union drbd_state val)
2684 struct drbd_config_context adm_ctx;
2685 enum drbd_ret_code retcode;
2687 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
2688 if (!adm_ctx.reply_skb)
2690 if (retcode != NO_ERROR)
2693 mutex_lock(&adm_ctx.resource->adm_mutex);
2694 retcode = drbd_request_state(adm_ctx.device, mask, val);
2695 mutex_unlock(&adm_ctx.resource->adm_mutex);
2697 drbd_adm_finish(&adm_ctx, info, retcode);
2701 static int drbd_bmio_set_susp_al(struct drbd_device *device)
2705 rv = drbd_bmio_set_n_write(device);
2706 drbd_suspend_al(device);
2710 int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
2712 struct drbd_config_context adm_ctx;
2713 int retcode; /* drbd_ret_code, drbd_state_rv */
2714 struct drbd_device *device;
2716 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
2717 if (!adm_ctx.reply_skb)
2719 if (retcode != NO_ERROR)
2722 mutex_lock(&adm_ctx.resource->adm_mutex);
2723 device = adm_ctx.device;
2725 /* If there is still bitmap IO pending, probably because of a previous
2726 * resync just being finished, wait for it before requesting a new resync.
2727 * Also wait for it's after_state_ch(). */
2728 drbd_suspend_io(device);
2729 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
2730 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
2732 /* If we happen to be C_STANDALONE R_PRIMARY, just set all bits
2733 * in the bitmap. Otherwise, try to start a resync handshake
2734 * as sync source for full sync.
2736 if (device->state.conn == C_STANDALONE && device->state.role == R_PRIMARY) {
2737 /* The peer will get a resync upon connect anyways. Just make that
2738 into a full resync. */
2739 retcode = drbd_request_state(device, NS(pdsk, D_INCONSISTENT));
2740 if (retcode >= SS_SUCCESS) {
2741 if (drbd_bitmap_io(device, &drbd_bmio_set_susp_al,
2742 "set_n_write from invalidate_peer",
2743 BM_LOCKED_SET_ALLOWED))
2744 retcode = ERR_IO_MD_DISK;
2747 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_S));
2748 drbd_resume_io(device);
2749 mutex_unlock(&adm_ctx.resource->adm_mutex);
2751 drbd_adm_finish(&adm_ctx, info, retcode);
2755 int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
2757 struct drbd_config_context adm_ctx;
2758 enum drbd_ret_code retcode;
2760 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
2761 if (!adm_ctx.reply_skb)
2763 if (retcode != NO_ERROR)
2766 mutex_lock(&adm_ctx.resource->adm_mutex);
2767 if (drbd_request_state(adm_ctx.device, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
2768 retcode = ERR_PAUSE_IS_SET;
2769 mutex_unlock(&adm_ctx.resource->adm_mutex);
2771 drbd_adm_finish(&adm_ctx, info, retcode);
2775 int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
2777 struct drbd_config_context adm_ctx;
2778 union drbd_dev_state s;
2779 enum drbd_ret_code retcode;
2781 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
2782 if (!adm_ctx.reply_skb)
2784 if (retcode != NO_ERROR)
2787 mutex_lock(&adm_ctx.resource->adm_mutex);
2788 if (drbd_request_state(adm_ctx.device, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2789 s = adm_ctx.device->state;
2790 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2791 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2792 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2794 retcode = ERR_PAUSE_IS_CLEAR;
2797 mutex_unlock(&adm_ctx.resource->adm_mutex);
2799 drbd_adm_finish(&adm_ctx, info, retcode);
2803 int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
2805 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
2808 int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
2810 struct drbd_config_context adm_ctx;
2811 struct drbd_device *device;
2812 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2814 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
2815 if (!adm_ctx.reply_skb)
2817 if (retcode != NO_ERROR)
2820 mutex_lock(&adm_ctx.resource->adm_mutex);
2821 device = adm_ctx.device;
2822 if (test_bit(NEW_CUR_UUID, &device->flags)) {
2823 drbd_uuid_new_current(device);
2824 clear_bit(NEW_CUR_UUID, &device->flags);
2826 drbd_suspend_io(device);
2827 retcode = drbd_request_state(device, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
2828 if (retcode == SS_SUCCESS) {
2829 if (device->state.conn < C_CONNECTED)
2830 tl_clear(first_peer_device(device)->connection);
2831 if (device->state.disk == D_DISKLESS || device->state.disk == D_FAILED)
2832 tl_restart(first_peer_device(device)->connection, FAIL_FROZEN_DISK_IO);
2834 drbd_resume_io(device);
2835 mutex_unlock(&adm_ctx.resource->adm_mutex);
2837 drbd_adm_finish(&adm_ctx, info, retcode);
2841 int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
2843 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
2846 static int nla_put_drbd_cfg_context(struct sk_buff *skb,
2847 struct drbd_resource *resource,
2848 struct drbd_connection *connection,
2849 struct drbd_device *device)
2852 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
2854 goto nla_put_failure;
2856 nla_put_u32(skb, T_ctx_volume, device->vnr))
2857 goto nla_put_failure;
2858 if (nla_put_string(skb, T_ctx_resource_name, resource->name))
2859 goto nla_put_failure;
2861 if (connection->my_addr_len &&
2862 nla_put(skb, T_ctx_my_addr, connection->my_addr_len, &connection->my_addr))
2863 goto nla_put_failure;
2864 if (connection->peer_addr_len &&
2865 nla_put(skb, T_ctx_peer_addr, connection->peer_addr_len, &connection->peer_addr))
2866 goto nla_put_failure;
2868 nla_nest_end(skb, nla);
2873 nla_nest_cancel(skb, nla);
2878 * Return the connection of @resource if @resource has exactly one connection.
2880 static struct drbd_connection *the_only_connection(struct drbd_resource *resource)
2882 struct list_head *connections = &resource->connections;
2884 if (list_empty(connections) || connections->next->next != connections)
2886 return list_first_entry(&resource->connections, struct drbd_connection, connections);
2889 int nla_put_status_info(struct sk_buff *skb, struct drbd_device *device,
2890 const struct sib_info *sib)
2892 struct drbd_resource *resource = device->resource;
2893 struct state_info *si = NULL; /* for sizeof(si->member); */
2897 int exclude_sensitive;
2899 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2900 * to. So we better exclude_sensitive information.
2902 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2903 * in the context of the requesting user process. Exclude sensitive
2904 * information, unless current has superuser.
2906 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2907 * relies on the current implementation of netlink_dump(), which
2908 * executes the dump callback successively from netlink_recvmsg(),
2909 * always in the context of the receiving process */
2910 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
2912 got_ldev = get_ldev(device);
2914 /* We need to add connection name and volume number information still.
2915 * Minor number is in drbd_genlmsghdr. */
2916 if (nla_put_drbd_cfg_context(skb, resource, the_only_connection(resource), device))
2917 goto nla_put_failure;
2919 if (res_opts_to_skb(skb, &device->resource->res_opts, exclude_sensitive))
2920 goto nla_put_failure;
2924 struct disk_conf *disk_conf;
2926 disk_conf = rcu_dereference(device->ldev->disk_conf);
2927 err = disk_conf_to_skb(skb, disk_conf, exclude_sensitive);
2930 struct net_conf *nc;
2932 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
2934 err = net_conf_to_skb(skb, nc, exclude_sensitive);
2938 goto nla_put_failure;
2940 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
2942 goto nla_put_failure;
2943 if (nla_put_u32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY) ||
2944 nla_put_u32(skb, T_current_state, device->state.i) ||
2945 nla_put_u64(skb, T_ed_uuid, device->ed_uuid) ||
2946 nla_put_u64(skb, T_capacity, drbd_get_capacity(device->this_bdev)) ||
2947 nla_put_u64(skb, T_send_cnt, device->send_cnt) ||
2948 nla_put_u64(skb, T_recv_cnt, device->recv_cnt) ||
2949 nla_put_u64(skb, T_read_cnt, device->read_cnt) ||
2950 nla_put_u64(skb, T_writ_cnt, device->writ_cnt) ||
2951 nla_put_u64(skb, T_al_writ_cnt, device->al_writ_cnt) ||
2952 nla_put_u64(skb, T_bm_writ_cnt, device->bm_writ_cnt) ||
2953 nla_put_u32(skb, T_ap_bio_cnt, atomic_read(&device->ap_bio_cnt)) ||
2954 nla_put_u32(skb, T_ap_pending_cnt, atomic_read(&device->ap_pending_cnt)) ||
2955 nla_put_u32(skb, T_rs_pending_cnt, atomic_read(&device->rs_pending_cnt)))
2956 goto nla_put_failure;
2961 spin_lock_irq(&device->ldev->md.uuid_lock);
2962 err = nla_put(skb, T_uuids, sizeof(si->uuids), device->ldev->md.uuid);
2963 spin_unlock_irq(&device->ldev->md.uuid_lock);
2966 goto nla_put_failure;
2968 if (nla_put_u32(skb, T_disk_flags, device->ldev->md.flags) ||
2969 nla_put_u64(skb, T_bits_total, drbd_bm_bits(device)) ||
2970 nla_put_u64(skb, T_bits_oos, drbd_bm_total_weight(device)))
2971 goto nla_put_failure;
2972 if (C_SYNC_SOURCE <= device->state.conn &&
2973 C_PAUSED_SYNC_T >= device->state.conn) {
2974 if (nla_put_u64(skb, T_bits_rs_total, device->rs_total) ||
2975 nla_put_u64(skb, T_bits_rs_failed, device->rs_failed))
2976 goto nla_put_failure;
2981 switch(sib->sib_reason) {
2982 case SIB_SYNC_PROGRESS:
2983 case SIB_GET_STATUS_REPLY:
2985 case SIB_STATE_CHANGE:
2986 if (nla_put_u32(skb, T_prev_state, sib->os.i) ||
2987 nla_put_u32(skb, T_new_state, sib->ns.i))
2988 goto nla_put_failure;
2990 case SIB_HELPER_POST:
2991 if (nla_put_u32(skb, T_helper_exit_code,
2992 sib->helper_exit_code))
2993 goto nla_put_failure;
2995 case SIB_HELPER_PRE:
2996 if (nla_put_string(skb, T_helper, sib->helper_name))
2997 goto nla_put_failure;
3001 nla_nest_end(skb, nla);
3011 int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
3013 struct drbd_config_context adm_ctx;
3014 enum drbd_ret_code retcode;
3017 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3018 if (!adm_ctx.reply_skb)
3020 if (retcode != NO_ERROR)
3023 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.device, NULL);
3025 nlmsg_free(adm_ctx.reply_skb);
3029 drbd_adm_finish(&adm_ctx, info, retcode);
3033 static int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
3035 struct drbd_device *device;
3036 struct drbd_genlmsghdr *dh;
3037 struct drbd_resource *pos = (struct drbd_resource *)cb->args[0];
3038 struct drbd_resource *resource = NULL;
3039 struct drbd_resource *tmp;
3040 unsigned volume = cb->args[1];
3042 /* Open coded, deferred, iteration:
3043 * for_each_resource_safe(resource, tmp, &drbd_resources) {
3044 * connection = "first connection of resource or undefined";
3045 * idr_for_each_entry(&resource->devices, device, i) {
3049 * where resource is cb->args[0];
3050 * and i is cb->args[1];
3052 * cb->args[2] indicates if we shall loop over all resources,
3053 * or just dump all volumes of a single resource.
3055 * This may miss entries inserted after this dump started,
3056 * or entries deleted before they are reached.
3058 * We need to make sure the device won't disappear while
3059 * we are looking at it, and revalidate our iterators
3060 * on each iteration.
3063 /* synchronize with conn_create()/drbd_destroy_connection() */
3065 /* revalidate iterator position */
3066 for_each_resource_rcu(tmp, &drbd_resources) {
3068 /* first iteration */
3080 device = idr_get_next(&resource->devices, &volume);
3082 /* No more volumes to dump on this resource.
3083 * Advance resource iterator. */
3084 pos = list_entry_rcu(resource->resources.next,
3085 struct drbd_resource, resources);
3086 /* Did we dump any volume of this resource yet? */
3088 /* If we reached the end of the list,
3089 * or only a single resource dump was requested,
3091 if (&pos->resources == &drbd_resources || cb->args[2])
3099 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3100 cb->nlh->nlmsg_seq, &drbd_genl_family,
3101 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
3106 /* This is a connection without a single volume.
3107 * Suprisingly enough, it may have a network
3109 struct drbd_connection *connection;
3112 dh->ret_code = NO_ERROR;
3113 connection = the_only_connection(resource);
3114 if (nla_put_drbd_cfg_context(skb, resource, connection, NULL))
3117 struct net_conf *nc;
3119 nc = rcu_dereference(connection->net_conf);
3120 if (nc && net_conf_to_skb(skb, nc, 1) != 0)
3126 D_ASSERT(device, device->vnr == volume);
3127 D_ASSERT(device, device->resource == resource);
3129 dh->minor = device_to_minor(device);
3130 dh->ret_code = NO_ERROR;
3132 if (nla_put_status_info(skb, device, NULL)) {
3134 genlmsg_cancel(skb, dh);
3138 genlmsg_end(skb, dh);
3143 /* where to start the next iteration */
3144 cb->args[0] = (long)pos;
3145 cb->args[1] = (pos == resource) ? volume + 1 : 0;
3147 /* No more resources/volumes/minors found results in an empty skb.
3148 * Which will terminate the dump. */
3153 * Request status of all resources, or of all volumes within a single resource.
3155 * This is a dump, as the answer may not fit in a single reply skb otherwise.
3156 * Which means we cannot use the family->attrbuf or other such members, because
3157 * dump is NOT protected by the genl_lock(). During dump, we only have access
3158 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
3160 * Once things are setup properly, we call into get_one_status().
3162 int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
3164 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
3166 const char *resource_name;
3167 struct drbd_resource *resource;
3170 /* Is this a followup call? */
3172 /* ... of a single resource dump,
3173 * and the resource iterator has been advanced already? */
3174 if (cb->args[2] && cb->args[2] != cb->args[0])
3175 return 0; /* DONE. */
3179 /* First call (from netlink_dump_start). We need to figure out
3180 * which resource(s) the user wants us to dump. */
3181 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
3182 nlmsg_attrlen(cb->nlh, hdrlen),
3183 DRBD_NLA_CFG_CONTEXT);
3185 /* No explicit context given. Dump all. */
3188 maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
3189 nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
3191 return PTR_ERR(nla);
3192 /* context given, but no name present? */
3195 resource_name = nla_data(nla);
3196 if (!*resource_name)
3198 resource = drbd_find_resource(resource_name);
3202 kref_put(&resource->kref, drbd_destroy_resource); /* get_one_status() revalidates the resource */
3204 /* prime iterators, and set "filter" mode mark:
3205 * only dump this connection. */
3206 cb->args[0] = (long)resource;
3207 /* cb->args[1] = 0; passed in this way. */
3208 cb->args[2] = (long)resource;
3211 return get_one_status(skb, cb);
3214 int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
3216 struct drbd_config_context adm_ctx;
3217 enum drbd_ret_code retcode;
3218 struct timeout_parms tp;
3221 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3222 if (!adm_ctx.reply_skb)
3224 if (retcode != NO_ERROR)
3228 adm_ctx.device->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
3229 test_bit(USE_DEGR_WFC_T, &adm_ctx.device->flags) ? UT_DEGRADED :
3232 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
3234 nlmsg_free(adm_ctx.reply_skb);
3238 drbd_adm_finish(&adm_ctx, info, retcode);
3242 int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
3244 struct drbd_config_context adm_ctx;
3245 struct drbd_device *device;
3246 enum drbd_ret_code retcode;
3247 struct start_ov_parms parms;
3249 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3250 if (!adm_ctx.reply_skb)
3252 if (retcode != NO_ERROR)
3255 device = adm_ctx.device;
3257 /* resume from last known position, if possible */
3258 parms.ov_start_sector = device->ov_start_sector;
3259 parms.ov_stop_sector = ULLONG_MAX;
3260 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
3261 int err = start_ov_parms_from_attrs(&parms, info);
3263 retcode = ERR_MANDATORY_TAG;
3264 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
3268 mutex_lock(&adm_ctx.resource->adm_mutex);
3270 /* w_make_ov_request expects position to be aligned */
3271 device->ov_start_sector = parms.ov_start_sector & ~(BM_SECT_PER_BIT-1);
3272 device->ov_stop_sector = parms.ov_stop_sector;
3274 /* If there is still bitmap IO pending, e.g. previous resync or verify
3275 * just being finished, wait for it before requesting a new resync. */
3276 drbd_suspend_io(device);
3277 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
3278 retcode = drbd_request_state(device, NS(conn, C_VERIFY_S));
3279 drbd_resume_io(device);
3281 mutex_unlock(&adm_ctx.resource->adm_mutex);
3283 drbd_adm_finish(&adm_ctx, info, retcode);
3288 int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
3290 struct drbd_config_context adm_ctx;
3291 struct drbd_device *device;
3292 enum drbd_ret_code retcode;
3293 int skip_initial_sync = 0;
3295 struct new_c_uuid_parms args;
3297 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3298 if (!adm_ctx.reply_skb)
3300 if (retcode != NO_ERROR)
3303 device = adm_ctx.device;
3304 memset(&args, 0, sizeof(args));
3305 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
3306 err = new_c_uuid_parms_from_attrs(&args, info);
3308 retcode = ERR_MANDATORY_TAG;
3309 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
3314 mutex_lock(&adm_ctx.resource->adm_mutex);
3315 mutex_lock(device->state_mutex); /* Protects us against serialized state changes. */
3317 if (!get_ldev(device)) {
3318 retcode = ERR_NO_DISK;
3322 /* this is "skip initial sync", assume to be clean */
3323 if (device->state.conn == C_CONNECTED &&
3324 first_peer_device(device)->connection->agreed_pro_version >= 90 &&
3325 device->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
3326 drbd_info(device, "Preparing to skip initial sync\n");
3327 skip_initial_sync = 1;
3328 } else if (device->state.conn != C_STANDALONE) {
3329 retcode = ERR_CONNECTED;
3333 drbd_uuid_set(device, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
3334 drbd_uuid_new_current(device); /* New current, previous to UI_BITMAP */
3336 if (args.clear_bm) {
3337 err = drbd_bitmap_io(device, &drbd_bmio_clear_n_write,
3338 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
3340 drbd_err(device, "Writing bitmap failed with %d\n", err);
3341 retcode = ERR_IO_MD_DISK;
3343 if (skip_initial_sync) {
3344 drbd_send_uuids_skip_initial_sync(first_peer_device(device));
3345 _drbd_uuid_set(device, UI_BITMAP, 0);
3346 drbd_print_uuids(device, "cleared bitmap UUID");
3347 spin_lock_irq(&device->resource->req_lock);
3348 _drbd_set_state(_NS2(device, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3350 spin_unlock_irq(&device->resource->req_lock);
3354 drbd_md_sync(device);
3358 mutex_unlock(device->state_mutex);
3359 mutex_unlock(&adm_ctx.resource->adm_mutex);
3361 drbd_adm_finish(&adm_ctx, info, retcode);
3365 static enum drbd_ret_code
3366 drbd_check_resource_name(struct drbd_config_context *adm_ctx)
3368 const char *name = adm_ctx->resource_name;
3369 if (!name || !name[0]) {
3370 drbd_msg_put_info(adm_ctx->reply_skb, "resource name missing");
3371 return ERR_MANDATORY_TAG;
3373 /* if we want to use these in sysfs/configfs/debugfs some day,
3374 * we must not allow slashes */
3375 if (strchr(name, '/')) {
3376 drbd_msg_put_info(adm_ctx->reply_skb, "invalid resource name");
3377 return ERR_INVALID_REQUEST;
3382 int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
3384 struct drbd_config_context adm_ctx;
3385 enum drbd_ret_code retcode;
3386 struct res_opts res_opts;
3389 retcode = drbd_adm_prepare(&adm_ctx, skb, info, 0);
3390 if (!adm_ctx.reply_skb)
3392 if (retcode != NO_ERROR)
3395 set_res_opts_defaults(&res_opts);
3396 err = res_opts_from_attrs(&res_opts, info);
3397 if (err && err != -ENOMSG) {
3398 retcode = ERR_MANDATORY_TAG;
3399 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
3403 retcode = drbd_check_resource_name(&adm_ctx);
3404 if (retcode != NO_ERROR)
3407 if (adm_ctx.resource) {
3408 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
3409 retcode = ERR_INVALID_REQUEST;
3410 drbd_msg_put_info(adm_ctx.reply_skb, "resource exists");
3412 /* else: still NO_ERROR */
3416 /* not yet safe for genl_family.parallel_ops */
3417 if (!conn_create(adm_ctx.resource_name, &res_opts))
3418 retcode = ERR_NOMEM;
3420 drbd_adm_finish(&adm_ctx, info, retcode);
3424 int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info)
3426 struct drbd_config_context adm_ctx;
3427 struct drbd_genlmsghdr *dh = info->userhdr;
3428 enum drbd_ret_code retcode;
3430 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
3431 if (!adm_ctx.reply_skb)
3433 if (retcode != NO_ERROR)
3436 if (dh->minor > MINORMASK) {
3437 drbd_msg_put_info(adm_ctx.reply_skb, "requested minor out of range");
3438 retcode = ERR_INVALID_REQUEST;
3441 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
3442 drbd_msg_put_info(adm_ctx.reply_skb, "requested volume id out of range");
3443 retcode = ERR_INVALID_REQUEST;
3447 /* drbd_adm_prepare made sure already
3448 * that first_peer_device(device)->connection and device->vnr match the request. */
3449 if (adm_ctx.device) {
3450 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
3451 retcode = ERR_MINOR_EXISTS;
3452 /* else: still NO_ERROR */
3456 mutex_lock(&adm_ctx.resource->adm_mutex);
3457 retcode = drbd_create_device(&adm_ctx, dh->minor);
3458 mutex_unlock(&adm_ctx.resource->adm_mutex);
3460 drbd_adm_finish(&adm_ctx, info, retcode);
3464 static enum drbd_ret_code adm_del_minor(struct drbd_device *device)
3466 if (device->state.disk == D_DISKLESS &&
3467 /* no need to be device->state.conn == C_STANDALONE &&
3468 * we may want to delete a minor from a live replication group.
3470 device->state.role == R_SECONDARY) {
3471 _drbd_request_state(device, NS(conn, C_WF_REPORT_PARAMS),
3472 CS_VERBOSE + CS_WAIT_COMPLETE);
3473 drbd_delete_device(device);
3476 return ERR_MINOR_CONFIGURED;
3479 int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info)
3481 struct drbd_config_context adm_ctx;
3482 enum drbd_ret_code retcode;
3484 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3485 if (!adm_ctx.reply_skb)
3487 if (retcode != NO_ERROR)
3490 mutex_lock(&adm_ctx.resource->adm_mutex);
3491 retcode = adm_del_minor(adm_ctx.device);
3492 mutex_unlock(&adm_ctx.resource->adm_mutex);
3494 drbd_adm_finish(&adm_ctx, info, retcode);
3498 int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
3500 struct drbd_config_context adm_ctx;
3501 struct drbd_resource *resource;
3502 struct drbd_connection *connection;
3503 struct drbd_device *device;
3504 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
3507 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
3508 if (!adm_ctx.reply_skb)
3510 if (retcode != NO_ERROR)
3513 resource = adm_ctx.resource;
3514 mutex_lock(&resource->adm_mutex);
3516 for_each_connection(connection, resource) {
3517 struct drbd_peer_device *peer_device;
3519 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
3520 retcode = drbd_set_role(peer_device->device, R_SECONDARY, 0);
3521 if (retcode < SS_SUCCESS) {
3522 drbd_msg_put_info(adm_ctx.reply_skb, "failed to demote");
3527 retcode = conn_try_disconnect(connection, 0);
3528 if (retcode < SS_SUCCESS) {
3529 drbd_msg_put_info(adm_ctx.reply_skb, "failed to disconnect");
3535 idr_for_each_entry(&resource->devices, device, i) {
3536 retcode = adm_detach(device, 0);
3537 if (retcode < SS_SUCCESS || retcode > NO_ERROR) {
3538 drbd_msg_put_info(adm_ctx.reply_skb, "failed to detach");
3543 /* If we reach this, all volumes (of this connection) are Secondary,
3544 * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
3545 * actually stopped, state handling only does drbd_thread_stop_nowait(). */
3546 for_each_connection(connection, resource)
3547 drbd_thread_stop(&connection->worker);
3549 /* Now, nothing can fail anymore */
3551 /* delete volumes */
3552 idr_for_each_entry(&resource->devices, device, i) {
3553 retcode = adm_del_minor(device);
3554 if (retcode != NO_ERROR) {
3555 /* "can not happen" */
3556 drbd_msg_put_info(adm_ctx.reply_skb, "failed to delete volume");
3561 list_del_rcu(&resource->resources);
3563 drbd_free_resource(resource);
3566 mutex_unlock(&resource->adm_mutex);
3568 drbd_adm_finish(&adm_ctx, info, retcode);
3572 int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
3574 struct drbd_config_context adm_ctx;
3575 struct drbd_resource *resource;
3576 struct drbd_connection *connection;
3577 enum drbd_ret_code retcode;
3579 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
3580 if (!adm_ctx.reply_skb)
3582 if (retcode != NO_ERROR)
3585 resource = adm_ctx.resource;
3586 mutex_lock(&resource->adm_mutex);
3587 for_each_connection(connection, resource) {
3588 if (connection->cstate > C_STANDALONE) {
3589 retcode = ERR_NET_CONFIGURED;
3593 if (!idr_is_empty(&resource->devices)) {
3594 retcode = ERR_RES_IN_USE;
3598 list_del_rcu(&resource->resources);
3599 for_each_connection(connection, resource)
3600 drbd_thread_stop(&connection->worker);
3602 drbd_free_resource(resource);
3605 mutex_unlock(&resource->adm_mutex);
3607 drbd_adm_finish(&adm_ctx, info, retcode);
3611 void drbd_bcast_event(struct drbd_device *device, const struct sib_info *sib)
3613 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
3614 struct sk_buff *msg;
3615 struct drbd_genlmsghdr *d_out;
3619 if (sib->sib_reason == SIB_SYNC_PROGRESS) {
3620 if (time_after(jiffies, device->rs_last_bcast + HZ))
3621 device->rs_last_bcast = jiffies;
3626 seq = atomic_inc_return(&drbd_genl_seq);
3627 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
3632 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
3633 if (!d_out) /* cannot happen, but anyways. */
3634 goto nla_put_failure;
3635 d_out->minor = device_to_minor(device);
3636 d_out->ret_code = NO_ERROR;
3638 if (nla_put_status_info(msg, device, sib))
3639 goto nla_put_failure;
3640 genlmsg_end(msg, d_out);
3641 err = drbd_genl_multicast_events(msg, 0);
3642 /* msg has been consumed or freed in netlink_broadcast() */
3643 if (err && err != -ESRCH)
3651 drbd_err(device, "Error %d while broadcasting event. "
3652 "Event seq:%u sib_reason:%u\n",
3653 err, seq, sib->sib_reason);