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 "drbd_wrappers.h"
38 #include <asm/unaligned.h>
39 #include <linux/drbd_limits.h>
40 #include <linux/kthread.h>
42 #include <net/genetlink.h>
45 // int drbd_adm_create_resource(struct sk_buff *skb, struct genl_info *info);
46 // int drbd_adm_delete_resource(struct sk_buff *skb, struct genl_info *info);
48 int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info);
49 int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info);
51 int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info);
52 int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info);
53 int drbd_adm_down(struct sk_buff *skb, struct genl_info *info);
55 int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info);
56 int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info);
57 int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info);
58 int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info);
59 int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info);
60 int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info);
61 int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info);
62 int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info);
63 int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info);
64 int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info);
65 int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info);
66 int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info);
67 int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info);
68 int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info);
69 int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info);
70 int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info);
71 int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info);
72 int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info);
73 int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info);
74 int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info);
76 int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb);
78 #include <linux/drbd_genl_api.h>
80 #include <linux/genl_magic_func.h>
82 /* used blkdev_get_by_path, to claim our meta data device(s) */
83 static char *drbd_m_holder = "Hands off! this is DRBD's meta data device.";
85 /* Configuration is strictly serialized, because generic netlink message
86 * processing is strictly serialized by the genl_lock().
87 * Which means we can use one static global drbd_config_context struct.
89 static struct drbd_config_context {
90 /* assigned from drbd_genlmsghdr */
92 /* assigned from request attributes, if present */
94 #define VOLUME_UNSPECIFIED (-1U)
95 /* pointer into the request skb,
96 * limited lifetime! */
98 struct nlattr *my_addr;
99 struct nlattr *peer_addr;
102 struct sk_buff *reply_skb;
103 /* pointer into reply buffer */
104 struct drbd_genlmsghdr *reply_dh;
105 /* resolved from attributes, if possible */
106 struct drbd_device *device;
107 struct drbd_resource *resource;
108 struct drbd_connection *connection;
111 static void drbd_adm_send_reply(struct sk_buff *skb, struct genl_info *info)
113 genlmsg_end(skb, genlmsg_data(nlmsg_data(nlmsg_hdr(skb))));
114 if (genlmsg_reply(skb, info))
115 printk(KERN_ERR "drbd: error sending genl reply\n");
118 /* Used on a fresh "drbd_adm_prepare"d reply_skb, this cannot fail: The only
119 * reason it could fail was no space in skb, and there are 4k available. */
120 int drbd_msg_put_info(const char *info)
122 struct sk_buff *skb = adm_ctx.reply_skb;
126 if (!info || !info[0])
129 nla = nla_nest_start(skb, DRBD_NLA_CFG_REPLY);
133 err = nla_put_string(skb, T_info_text, info);
135 nla_nest_cancel(skb, nla);
138 nla_nest_end(skb, nla);
142 /* This would be a good candidate for a "pre_doit" hook,
143 * and per-family private info->pointers.
144 * But we need to stay compatible with older kernels.
145 * If it returns successfully, adm_ctx members are valid.
147 #define DRBD_ADM_NEED_MINOR 1
148 #define DRBD_ADM_NEED_RESOURCE 2
149 #define DRBD_ADM_NEED_CONNECTION 4
150 static int drbd_adm_prepare(struct sk_buff *skb, struct genl_info *info,
153 struct drbd_genlmsghdr *d_in = info->userhdr;
154 const u8 cmd = info->genlhdr->cmd;
157 memset(&adm_ctx, 0, sizeof(adm_ctx));
159 /* genl_rcv_msg only checks for CAP_NET_ADMIN on "GENL_ADMIN_PERM" :( */
160 if (cmd != DRBD_ADM_GET_STATUS && !capable(CAP_NET_ADMIN))
163 adm_ctx.reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
164 if (!adm_ctx.reply_skb) {
169 adm_ctx.reply_dh = genlmsg_put_reply(adm_ctx.reply_skb,
170 info, &drbd_genl_family, 0, cmd);
171 /* put of a few bytes into a fresh skb of >= 4k will always succeed.
173 if (!adm_ctx.reply_dh) {
178 adm_ctx.reply_dh->minor = d_in->minor;
179 adm_ctx.reply_dh->ret_code = NO_ERROR;
181 adm_ctx.volume = VOLUME_UNSPECIFIED;
182 if (info->attrs[DRBD_NLA_CFG_CONTEXT]) {
184 /* parse and validate only */
185 err = drbd_cfg_context_from_attrs(NULL, info);
189 /* It was present, and valid,
190 * copy it over to the reply skb. */
191 err = nla_put_nohdr(adm_ctx.reply_skb,
192 info->attrs[DRBD_NLA_CFG_CONTEXT]->nla_len,
193 info->attrs[DRBD_NLA_CFG_CONTEXT]);
197 /* and assign stuff to the global adm_ctx */
198 nla = nested_attr_tb[__nla_type(T_ctx_volume)];
200 adm_ctx.volume = nla_get_u32(nla);
201 nla = nested_attr_tb[__nla_type(T_ctx_resource_name)];
203 adm_ctx.resource_name = nla_data(nla);
204 adm_ctx.my_addr = nested_attr_tb[__nla_type(T_ctx_my_addr)];
205 adm_ctx.peer_addr = nested_attr_tb[__nla_type(T_ctx_peer_addr)];
206 if ((adm_ctx.my_addr &&
207 nla_len(adm_ctx.my_addr) > sizeof(adm_ctx.connection->my_addr)) ||
208 (adm_ctx.peer_addr &&
209 nla_len(adm_ctx.peer_addr) > sizeof(adm_ctx.connection->peer_addr))) {
215 adm_ctx.minor = d_in->minor;
216 adm_ctx.device = minor_to_device(d_in->minor);
217 if (adm_ctx.resource_name) {
218 adm_ctx.resource = drbd_find_resource(adm_ctx.resource_name);
221 if (!adm_ctx.device && (flags & DRBD_ADM_NEED_MINOR)) {
222 drbd_msg_put_info("unknown minor");
223 return ERR_MINOR_INVALID;
225 if (!adm_ctx.resource && (flags & DRBD_ADM_NEED_RESOURCE)) {
226 drbd_msg_put_info("unknown resource");
227 if (adm_ctx.resource_name)
228 return ERR_RES_NOT_KNOWN;
229 return ERR_INVALID_REQUEST;
232 if (flags & DRBD_ADM_NEED_CONNECTION) {
233 if (adm_ctx.resource) {
234 drbd_msg_put_info("no resource name expected");
235 return ERR_INVALID_REQUEST;
237 if (adm_ctx.device) {
238 drbd_msg_put_info("no minor number expected");
239 return ERR_INVALID_REQUEST;
241 if (adm_ctx.my_addr && adm_ctx.peer_addr)
242 adm_ctx.connection = conn_get_by_addrs(nla_data(adm_ctx.my_addr),
243 nla_len(adm_ctx.my_addr),
244 nla_data(adm_ctx.peer_addr),
245 nla_len(adm_ctx.peer_addr));
246 if (!adm_ctx.connection) {
247 drbd_msg_put_info("unknown connection");
248 return ERR_INVALID_REQUEST;
252 /* some more paranoia, if the request was over-determined */
253 if (adm_ctx.device && adm_ctx.resource &&
254 adm_ctx.device->resource != adm_ctx.resource) {
255 pr_warning("request: minor=%u, resource=%s; but that minor belongs to resource %s\n",
256 adm_ctx.minor, adm_ctx.resource->name,
257 adm_ctx.device->resource->name);
258 drbd_msg_put_info("minor exists in different resource");
259 return ERR_INVALID_REQUEST;
261 if (adm_ctx.device &&
262 adm_ctx.volume != VOLUME_UNSPECIFIED &&
263 adm_ctx.volume != adm_ctx.device->vnr) {
264 pr_warning("request: minor=%u, volume=%u; but that minor is volume %u in %s\n",
265 adm_ctx.minor, adm_ctx.volume,
267 adm_ctx.device->resource->name);
268 drbd_msg_put_info("minor exists as different volume");
269 return ERR_INVALID_REQUEST;
275 nlmsg_free(adm_ctx.reply_skb);
276 adm_ctx.reply_skb = NULL;
280 static int drbd_adm_finish(struct genl_info *info, int retcode)
282 if (adm_ctx.connection) {
283 kref_put(&adm_ctx.connection->kref, drbd_destroy_connection);
284 adm_ctx.connection = NULL;
286 if (adm_ctx.resource) {
287 kref_put(&adm_ctx.resource->kref, drbd_destroy_resource);
288 adm_ctx.resource = NULL;
291 if (!adm_ctx.reply_skb)
294 adm_ctx.reply_dh->ret_code = retcode;
295 drbd_adm_send_reply(adm_ctx.reply_skb, info);
299 static void setup_khelper_env(struct drbd_connection *connection, char **envp)
303 /* FIXME: A future version will not allow this case. */
304 if (connection->my_addr_len == 0 || connection->peer_addr_len == 0)
307 switch (((struct sockaddr *)&connection->peer_addr)->sa_family) {
310 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
311 &((struct sockaddr_in6 *)&connection->peer_addr)->sin6_addr);
315 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
316 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
320 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
321 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
323 snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
326 int drbd_khelper(struct drbd_device *device, char *cmd)
328 char *envp[] = { "HOME=/",
330 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
331 (char[20]) { }, /* address family */
332 (char[60]) { }, /* address */
335 char *argv[] = {usermode_helper, cmd, mb, NULL };
336 struct drbd_connection *connection = first_peer_device(device)->connection;
340 if (current == connection->worker.task)
341 set_bit(CALLBACK_PENDING, &connection->flags);
343 snprintf(mb, 12, "minor-%d", device_to_minor(device));
344 setup_khelper_env(connection, envp);
346 /* The helper may take some time.
347 * write out any unsynced meta data changes now */
348 drbd_md_sync(device);
350 drbd_info(device, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
351 sib.sib_reason = SIB_HELPER_PRE;
352 sib.helper_name = cmd;
353 drbd_bcast_event(device, &sib);
354 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
356 drbd_warn(device, "helper command: %s %s %s exit code %u (0x%x)\n",
357 usermode_helper, cmd, mb,
358 (ret >> 8) & 0xff, ret);
360 drbd_info(device, "helper command: %s %s %s exit code %u (0x%x)\n",
361 usermode_helper, cmd, mb,
362 (ret >> 8) & 0xff, ret);
363 sib.sib_reason = SIB_HELPER_POST;
364 sib.helper_exit_code = ret;
365 drbd_bcast_event(device, &sib);
367 if (current == connection->worker.task)
368 clear_bit(CALLBACK_PENDING, &connection->flags);
370 if (ret < 0) /* Ignore any ERRNOs we got. */
376 static int conn_khelper(struct drbd_connection *connection, char *cmd)
378 char *envp[] = { "HOME=/",
380 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
381 (char[20]) { }, /* address family */
382 (char[60]) { }, /* address */
384 char *resource_name = connection->resource->name;
385 char *argv[] = {usermode_helper, cmd, resource_name, NULL };
388 setup_khelper_env(connection, envp);
389 conn_md_sync(connection);
391 drbd_info(connection, "helper command: %s %s %s\n", usermode_helper, cmd, resource_name);
392 /* TODO: conn_bcast_event() ?? */
394 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
396 drbd_warn(connection, "helper command: %s %s %s exit code %u (0x%x)\n",
397 usermode_helper, cmd, resource_name,
398 (ret >> 8) & 0xff, ret);
400 drbd_info(connection, "helper command: %s %s %s exit code %u (0x%x)\n",
401 usermode_helper, cmd, resource_name,
402 (ret >> 8) & 0xff, ret);
403 /* TODO: conn_bcast_event() ?? */
405 if (ret < 0) /* Ignore any ERRNOs we got. */
411 static enum drbd_fencing_p highest_fencing_policy(struct drbd_connection *connection)
413 enum drbd_fencing_p fp = FP_NOT_AVAIL;
414 struct drbd_peer_device *peer_device;
418 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
419 struct drbd_device *device = peer_device->device;
420 if (get_ldev_if_state(device, D_CONSISTENT)) {
421 struct disk_conf *disk_conf =
422 rcu_dereference(peer_device->device->ldev->disk_conf);
423 fp = max_t(enum drbd_fencing_p, fp, disk_conf->fencing);
432 bool conn_try_outdate_peer(struct drbd_connection *connection)
434 unsigned int connect_cnt;
435 union drbd_state mask = { };
436 union drbd_state val = { };
437 enum drbd_fencing_p fp;
441 if (connection->cstate >= C_WF_REPORT_PARAMS) {
442 drbd_err(connection, "Expected cstate < C_WF_REPORT_PARAMS\n");
446 spin_lock_irq(&connection->resource->req_lock);
447 connect_cnt = connection->connect_cnt;
448 spin_unlock_irq(&connection->resource->req_lock);
450 fp = highest_fencing_policy(connection);
453 drbd_warn(connection, "Not fencing peer, I'm not even Consistent myself.\n");
460 r = conn_khelper(connection, "fence-peer");
462 switch ((r>>8) & 0xff) {
463 case 3: /* peer is inconsistent */
464 ex_to_string = "peer is inconsistent or worse";
466 val.pdsk = D_INCONSISTENT;
468 case 4: /* peer got outdated, or was already outdated */
469 ex_to_string = "peer was fenced";
471 val.pdsk = D_OUTDATED;
473 case 5: /* peer was down */
474 if (conn_highest_disk(connection) == D_UP_TO_DATE) {
475 /* we will(have) create(d) a new UUID anyways... */
476 ex_to_string = "peer is unreachable, assumed to be dead";
478 val.pdsk = D_OUTDATED;
480 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
483 case 6: /* Peer is primary, voluntarily outdate myself.
484 * This is useful when an unconnected R_SECONDARY is asked to
485 * become R_PRIMARY, but finds the other peer being active. */
486 ex_to_string = "peer is active";
487 drbd_warn(connection, "Peer is primary, outdating myself.\n");
489 val.disk = D_OUTDATED;
492 if (fp != FP_STONITH)
493 drbd_err(connection, "fence-peer() = 7 && fencing != Stonith !!!\n");
494 ex_to_string = "peer was stonithed";
496 val.pdsk = D_OUTDATED;
499 /* The script is broken ... */
500 drbd_err(connection, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
501 return false; /* Eventually leave IO frozen */
504 drbd_info(connection, "fence-peer helper returned %d (%s)\n",
505 (r>>8) & 0xff, ex_to_string);
510 conn_request_state(connection, mask, val, CS_VERBOSE);
511 here, because we might were able to re-establish the connection in the
513 spin_lock_irq(&connection->resource->req_lock);
514 if (connection->cstate < C_WF_REPORT_PARAMS && !test_bit(STATE_SENT, &connection->flags)) {
515 if (connection->connect_cnt != connect_cnt)
516 /* In case the connection was established and droped
517 while the fence-peer handler was running, ignore it */
518 drbd_info(connection, "Ignoring fence-peer exit code\n");
520 _conn_request_state(connection, mask, val, CS_VERBOSE);
522 spin_unlock_irq(&connection->resource->req_lock);
524 return conn_highest_pdsk(connection) <= D_OUTDATED;
527 static int _try_outdate_peer_async(void *data)
529 struct drbd_connection *connection = (struct drbd_connection *)data;
531 conn_try_outdate_peer(connection);
533 kref_put(&connection->kref, drbd_destroy_connection);
537 void conn_try_outdate_peer_async(struct drbd_connection *connection)
539 struct task_struct *opa;
541 kref_get(&connection->kref);
542 opa = kthread_run(_try_outdate_peer_async, connection, "drbd_async_h");
544 drbd_err(connection, "out of mem, failed to invoke fence-peer helper\n");
545 kref_put(&connection->kref, drbd_destroy_connection);
550 drbd_set_role(struct drbd_device *device, enum drbd_role new_role, int force)
552 const int max_tries = 4;
553 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
557 union drbd_state mask, val;
559 if (new_role == R_PRIMARY) {
560 struct drbd_connection *connection;
562 /* Detect dead peers as soon as possible. */
565 for_each_connection(connection, device->resource)
566 request_ping(connection);
570 mutex_lock(device->state_mutex);
572 mask.i = 0; mask.role = R_MASK;
573 val.i = 0; val.role = new_role;
575 while (try++ < max_tries) {
576 rv = _drbd_request_state(device, mask, val, CS_WAIT_COMPLETE);
578 /* in case we first succeeded to outdate,
579 * but now suddenly could establish a connection */
580 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
586 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
587 (device->state.disk < D_UP_TO_DATE &&
588 device->state.disk >= D_INCONSISTENT)) {
590 val.disk = D_UP_TO_DATE;
595 if (rv == SS_NO_UP_TO_DATE_DISK &&
596 device->state.disk == D_CONSISTENT && mask.pdsk == 0) {
597 D_ASSERT(device, device->state.pdsk == D_UNKNOWN);
599 if (conn_try_outdate_peer(first_peer_device(device)->connection)) {
600 val.disk = D_UP_TO_DATE;
606 if (rv == SS_NOTHING_TO_DO)
608 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
609 if (!conn_try_outdate_peer(first_peer_device(device)->connection) && force) {
610 drbd_warn(device, "Forced into split brain situation!\n");
612 val.pdsk = D_OUTDATED;
617 if (rv == SS_TWO_PRIMARIES) {
618 /* Maybe the peer is detected as dead very soon...
619 retry at most once more in this case. */
622 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
623 timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
625 schedule_timeout_interruptible(timeo);
630 if (rv < SS_SUCCESS) {
631 rv = _drbd_request_state(device, mask, val,
632 CS_VERBOSE + CS_WAIT_COMPLETE);
643 drbd_warn(device, "Forced to consider local data as UpToDate!\n");
645 /* Wait until nothing is on the fly :) */
646 wait_event(device->misc_wait, atomic_read(&device->ap_pending_cnt) == 0);
648 /* FIXME also wait for all pending P_BARRIER_ACK? */
650 if (new_role == R_SECONDARY) {
651 set_disk_ro(device->vdisk, true);
652 if (get_ldev(device)) {
653 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
657 mutex_lock(&device->resource->conf_update);
658 nc = first_peer_device(device)->connection->net_conf;
660 nc->discard_my_data = 0; /* without copy; single bit op is atomic */
661 mutex_unlock(&device->resource->conf_update);
663 set_disk_ro(device->vdisk, false);
664 if (get_ldev(device)) {
665 if (((device->state.conn < C_CONNECTED ||
666 device->state.pdsk <= D_FAILED)
667 && device->ldev->md.uuid[UI_BITMAP] == 0) || forced)
668 drbd_uuid_new_current(device);
670 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
675 /* writeout of activity log covered areas of the bitmap
676 * to stable storage done in after state change already */
678 if (device->state.conn >= C_WF_REPORT_PARAMS) {
679 /* if this was forced, we should consider sync */
681 drbd_send_uuids(first_peer_device(device));
682 drbd_send_current_state(first_peer_device(device));
685 drbd_md_sync(device);
687 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
689 mutex_unlock(device->state_mutex);
693 static const char *from_attrs_err_to_txt(int err)
695 return err == -ENOMSG ? "required attribute missing" :
696 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
697 err == -EEXIST ? "can not change invariant setting" :
698 "invalid attribute value";
701 int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
703 struct set_role_parms parms;
705 enum drbd_ret_code retcode;
707 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
708 if (!adm_ctx.reply_skb)
710 if (retcode != NO_ERROR)
713 memset(&parms, 0, sizeof(parms));
714 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
715 err = set_role_parms_from_attrs(&parms, info);
717 retcode = ERR_MANDATORY_TAG;
718 drbd_msg_put_info(from_attrs_err_to_txt(err));
723 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
724 retcode = drbd_set_role(adm_ctx.device, R_PRIMARY, parms.assume_uptodate);
726 retcode = drbd_set_role(adm_ctx.device, R_SECONDARY, 0);
728 drbd_adm_finish(info, retcode);
732 /* Initializes the md.*_offset members, so we are able to find
733 * the on disk meta data.
735 * We currently have two possible layouts:
737 * |----------- md_size_sect ------------------|
738 * [ 4k superblock ][ activity log ][ Bitmap ]
740 * | bm_offset = al_offset + X |
741 * ==> bitmap sectors = md_size_sect - bm_offset
744 * |----------- md_size_sect ------------------|
745 * [data.....][ Bitmap ][ activity log ][ 4k superblock ]
747 * | bm_offset = al_offset - Y |
748 * ==> bitmap sectors = Y = al_offset - bm_offset
750 * Activity log size used to be fixed 32kB,
751 * but is about to become configurable.
753 static void drbd_md_set_sector_offsets(struct drbd_device *device,
754 struct drbd_backing_dev *bdev)
756 sector_t md_size_sect = 0;
757 unsigned int al_size_sect = bdev->md.al_size_4k * 8;
759 bdev->md.md_offset = drbd_md_ss(bdev);
761 switch (bdev->md.meta_dev_idx) {
763 /* v07 style fixed size indexed meta data */
764 bdev->md.md_size_sect = MD_128MB_SECT;
765 bdev->md.al_offset = MD_4kB_SECT;
766 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
768 case DRBD_MD_INDEX_FLEX_EXT:
769 /* just occupy the full device; unit: sectors */
770 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
771 bdev->md.al_offset = MD_4kB_SECT;
772 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
774 case DRBD_MD_INDEX_INTERNAL:
775 case DRBD_MD_INDEX_FLEX_INT:
776 /* al size is still fixed */
777 bdev->md.al_offset = -al_size_sect;
778 /* we need (slightly less than) ~ this much bitmap sectors: */
779 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
780 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
781 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
782 md_size_sect = ALIGN(md_size_sect, 8);
784 /* plus the "drbd meta data super block",
785 * and the activity log; */
786 md_size_sect += MD_4kB_SECT + al_size_sect;
788 bdev->md.md_size_sect = md_size_sect;
789 /* bitmap offset is adjusted by 'super' block size */
790 bdev->md.bm_offset = -md_size_sect + MD_4kB_SECT;
795 /* input size is expected to be in KB */
796 char *ppsize(char *buf, unsigned long long size)
798 /* Needs 9 bytes at max including trailing NUL:
799 * -1ULL ==> "16384 EB" */
800 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
802 while (size >= 10000 && base < sizeof(units)-1) {
804 size = (size >> 10) + !!(size & (1<<9));
807 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
812 /* there is still a theoretical deadlock when called from receiver
813 * on an D_INCONSISTENT R_PRIMARY:
814 * remote READ does inc_ap_bio, receiver would need to receive answer
815 * packet from remote to dec_ap_bio again.
816 * receiver receive_sizes(), comes here,
817 * waits for ap_bio_cnt == 0. -> deadlock.
818 * but this cannot happen, actually, because:
819 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
820 * (not connected, or bad/no disk on peer):
821 * see drbd_fail_request_early, ap_bio_cnt is zero.
822 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
823 * peer may not initiate a resize.
825 /* Note these are not to be confused with
826 * drbd_adm_suspend_io/drbd_adm_resume_io,
827 * which are (sub) state changes triggered by admin (drbdsetup),
828 * and can be long lived.
829 * This changes an device->flag, is triggered by drbd internals,
830 * and should be short-lived. */
831 void drbd_suspend_io(struct drbd_device *device)
833 set_bit(SUSPEND_IO, &device->flags);
834 if (drbd_suspended(device))
836 wait_event(device->misc_wait, !atomic_read(&device->ap_bio_cnt));
839 void drbd_resume_io(struct drbd_device *device)
841 clear_bit(SUSPEND_IO, &device->flags);
842 wake_up(&device->misc_wait);
846 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
847 * @device: DRBD device.
849 * Returns 0 on success, negative return values indicate errors.
850 * You should call drbd_md_sync() after calling this function.
852 enum determine_dev_size
853 drbd_determine_dev_size(struct drbd_device *device, enum dds_flags flags, struct resize_parms *rs) __must_hold(local)
855 sector_t prev_first_sect, prev_size; /* previous meta location */
856 sector_t la_size_sect, u_size;
857 struct drbd_md *md = &device->ldev->md;
858 u32 prev_al_stripe_size_4k;
864 int md_moved, la_size_changed;
865 enum determine_dev_size rv = DS_UNCHANGED;
868 * application request passes inc_ap_bio,
869 * but then cannot get an AL-reference.
870 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
873 * Suspend IO right here.
874 * still lock the act_log to not trigger ASSERTs there.
876 drbd_suspend_io(device);
877 buffer = drbd_md_get_buffer(device); /* Lock meta-data IO */
879 drbd_resume_io(device);
883 /* no wait necessary anymore, actually we could assert that */
884 wait_event(device->al_wait, lc_try_lock(device->act_log));
886 prev_first_sect = drbd_md_first_sector(device->ldev);
887 prev_size = device->ldev->md.md_size_sect;
888 la_size_sect = device->ldev->md.la_size_sect;
891 /* rs is non NULL if we should change the AL layout only */
893 prev_al_stripes = md->al_stripes;
894 prev_al_stripe_size_4k = md->al_stripe_size_4k;
896 md->al_stripes = rs->al_stripes;
897 md->al_stripe_size_4k = rs->al_stripe_size / 4;
898 md->al_size_4k = (u64)rs->al_stripes * rs->al_stripe_size / 4;
901 drbd_md_set_sector_offsets(device, device->ldev);
904 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
906 size = drbd_new_dev_size(device, device->ldev, u_size, flags & DDSF_FORCED);
908 if (size < la_size_sect) {
909 if (rs && u_size == 0) {
910 /* Remove "rs &&" later. This check should always be active, but
911 right now the receiver expects the permissive behavior */
912 drbd_warn(device, "Implicit shrink not allowed. "
913 "Use --size=%llus for explicit shrink.\n",
914 (unsigned long long)size);
915 rv = DS_ERROR_SHRINK;
918 rv = DS_ERROR_SPACE_MD;
919 if (rv != DS_UNCHANGED)
923 if (drbd_get_capacity(device->this_bdev) != size ||
924 drbd_bm_capacity(device) != size) {
926 err = drbd_bm_resize(device, size, !(flags & DDSF_NO_RESYNC));
928 /* currently there is only one error: ENOMEM! */
929 size = drbd_bm_capacity(device)>>1;
931 drbd_err(device, "OUT OF MEMORY! "
932 "Could not allocate bitmap!\n");
934 drbd_err(device, "BM resizing failed. "
935 "Leaving size unchanged at size = %lu KB\n",
936 (unsigned long)size);
940 /* racy, see comments above. */
941 drbd_set_my_capacity(device, size);
942 device->ldev->md.la_size_sect = size;
943 drbd_info(device, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
944 (unsigned long long)size>>1);
949 la_size_changed = (la_size_sect != device->ldev->md.la_size_sect);
951 md_moved = prev_first_sect != drbd_md_first_sector(device->ldev)
952 || prev_size != device->ldev->md.md_size_sect;
954 if (la_size_changed || md_moved || rs) {
957 drbd_al_shrink(device); /* All extents inactive. */
959 prev_flags = md->flags;
960 md->flags &= ~MDF_PRIMARY_IND;
961 drbd_md_write(device, buffer);
963 drbd_info(device, "Writing the whole bitmap, %s\n",
964 la_size_changed && md_moved ? "size changed and md moved" :
965 la_size_changed ? "size changed" : "md moved");
966 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
967 drbd_bitmap_io(device, md_moved ? &drbd_bm_write_all : &drbd_bm_write,
968 "size changed", BM_LOCKED_MASK);
969 drbd_initialize_al(device, buffer);
971 md->flags = prev_flags;
972 drbd_md_write(device, buffer);
975 drbd_info(device, "Changed AL layout to al-stripes = %d, al-stripe-size-kB = %d\n",
976 md->al_stripes, md->al_stripe_size_4k * 4);
979 if (size > la_size_sect)
980 rv = la_size_sect ? DS_GREW : DS_GREW_FROM_ZERO;
981 if (size < la_size_sect)
987 md->al_stripes = prev_al_stripes;
988 md->al_stripe_size_4k = prev_al_stripe_size_4k;
989 md->al_size_4k = (u64)prev_al_stripes * prev_al_stripe_size_4k;
991 drbd_md_set_sector_offsets(device, device->ldev);
994 lc_unlock(device->act_log);
995 wake_up(&device->al_wait);
996 drbd_md_put_buffer(device);
997 drbd_resume_io(device);
1003 drbd_new_dev_size(struct drbd_device *device, struct drbd_backing_dev *bdev,
1004 sector_t u_size, int assume_peer_has_space)
1006 sector_t p_size = device->p_size; /* partner's disk size. */
1007 sector_t la_size_sect = bdev->md.la_size_sect; /* last agreed size. */
1008 sector_t m_size; /* my size */
1011 m_size = drbd_get_max_capacity(bdev);
1013 if (device->state.conn < C_CONNECTED && assume_peer_has_space) {
1014 drbd_warn(device, "Resize while not connected was forced by the user!\n");
1018 if (p_size && m_size) {
1019 size = min_t(sector_t, p_size, m_size);
1022 size = la_size_sect;
1023 if (m_size && m_size < size)
1025 if (p_size && p_size < size)
1036 drbd_err(device, "Both nodes diskless!\n");
1040 drbd_err(device, "Requested disk size is too big (%lu > %lu)\n",
1041 (unsigned long)u_size>>1, (unsigned long)size>>1);
1050 * drbd_check_al_size() - Ensures that the AL is of the right size
1051 * @device: DRBD device.
1053 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
1054 * failed, and 0 on success. You should call drbd_md_sync() after you called
1057 static int drbd_check_al_size(struct drbd_device *device, struct disk_conf *dc)
1059 struct lru_cache *n, *t;
1060 struct lc_element *e;
1061 unsigned int in_use;
1064 if (device->act_log &&
1065 device->act_log->nr_elements == dc->al_extents)
1069 t = device->act_log;
1070 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
1071 dc->al_extents, sizeof(struct lc_element), 0);
1074 drbd_err(device, "Cannot allocate act_log lru!\n");
1077 spin_lock_irq(&device->al_lock);
1079 for (i = 0; i < t->nr_elements; i++) {
1080 e = lc_element_by_index(t, i);
1082 drbd_err(device, "refcnt(%d)==%d\n",
1083 e->lc_number, e->refcnt);
1084 in_use += e->refcnt;
1088 device->act_log = n;
1089 spin_unlock_irq(&device->al_lock);
1091 drbd_err(device, "Activity log still in use!\n");
1098 drbd_md_mark_dirty(device); /* we changed device->act_log->nr_elemens */
1102 static void drbd_setup_queue_param(struct drbd_device *device, unsigned int max_bio_size)
1104 struct request_queue * const q = device->rq_queue;
1105 unsigned int max_hw_sectors = max_bio_size >> 9;
1106 unsigned int max_segments = 0;
1108 if (get_ldev_if_state(device, D_ATTACHING)) {
1109 struct request_queue * const b = device->ldev->backing_bdev->bd_disk->queue;
1111 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
1113 max_segments = rcu_dereference(device->ldev->disk_conf)->max_bio_bvecs;
1118 blk_queue_logical_block_size(q, 512);
1119 blk_queue_max_hw_sectors(q, max_hw_sectors);
1120 /* This is the workaround for "bio would need to, but cannot, be split" */
1121 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
1122 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
1124 if (get_ldev_if_state(device, D_ATTACHING)) {
1125 struct request_queue * const b = device->ldev->backing_bdev->bd_disk->queue;
1127 blk_queue_stack_limits(q, b);
1129 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
1130 drbd_info(device, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
1131 q->backing_dev_info.ra_pages,
1132 b->backing_dev_info.ra_pages);
1133 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1139 void drbd_reconsider_max_bio_size(struct drbd_device *device)
1141 unsigned int now, new, local, peer;
1143 now = queue_max_hw_sectors(device->rq_queue) << 9;
1144 local = device->local_max_bio_size; /* Eventually last known value, from volatile memory */
1145 peer = device->peer_max_bio_size; /* Eventually last known value, from meta data */
1147 if (get_ldev_if_state(device, D_ATTACHING)) {
1148 local = queue_max_hw_sectors(device->ldev->backing_bdev->bd_disk->queue) << 9;
1149 device->local_max_bio_size = local;
1152 local = min(local, DRBD_MAX_BIO_SIZE);
1154 /* We may ignore peer limits if the peer is modern enough.
1155 Because new from 8.3.8 onwards the peer can use multiple
1156 BIOs for a single peer_request */
1157 if (device->state.conn >= C_WF_REPORT_PARAMS) {
1158 if (first_peer_device(device)->connection->agreed_pro_version < 94)
1159 peer = min(device->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
1160 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
1161 else if (first_peer_device(device)->connection->agreed_pro_version == 94)
1162 peer = DRBD_MAX_SIZE_H80_PACKET;
1163 else if (first_peer_device(device)->connection->agreed_pro_version < 100)
1164 peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
1166 peer = DRBD_MAX_BIO_SIZE;
1169 new = min(local, peer);
1171 if (device->state.role == R_PRIMARY && new < now)
1172 drbd_err(device, "ASSERT FAILED new < now; (%u < %u)\n", new, now);
1175 drbd_info(device, "max BIO size = %u\n", new);
1177 drbd_setup_queue_param(device, new);
1180 /* Starts the worker thread */
1181 static void conn_reconfig_start(struct drbd_connection *connection)
1183 drbd_thread_start(&connection->worker);
1184 drbd_flush_workqueue(&connection->sender_work);
1187 /* if still unconfigured, stops worker again. */
1188 static void conn_reconfig_done(struct drbd_connection *connection)
1191 spin_lock_irq(&connection->resource->req_lock);
1192 stop_threads = conn_all_vols_unconf(connection) &&
1193 connection->cstate == C_STANDALONE;
1194 spin_unlock_irq(&connection->resource->req_lock);
1196 /* asender is implicitly stopped by receiver
1197 * in conn_disconnect() */
1198 drbd_thread_stop(&connection->receiver);
1199 drbd_thread_stop(&connection->worker);
1203 /* Make sure IO is suspended before calling this function(). */
1204 static void drbd_suspend_al(struct drbd_device *device)
1208 if (!lc_try_lock(device->act_log)) {
1209 drbd_warn(device, "Failed to lock al in drbd_suspend_al()\n");
1213 drbd_al_shrink(device);
1214 spin_lock_irq(&device->resource->req_lock);
1215 if (device->state.conn < C_CONNECTED)
1216 s = !test_and_set_bit(AL_SUSPENDED, &device->flags);
1217 spin_unlock_irq(&device->resource->req_lock);
1218 lc_unlock(device->act_log);
1221 drbd_info(device, "Suspended AL updates\n");
1225 static bool should_set_defaults(struct genl_info *info)
1227 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1228 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1231 static unsigned int drbd_al_extents_max(struct drbd_backing_dev *bdev)
1233 /* This is limited by 16 bit "slot" numbers,
1234 * and by available on-disk context storage.
1236 * Also (u16)~0 is special (denotes a "free" extent).
1238 * One transaction occupies one 4kB on-disk block,
1239 * we have n such blocks in the on disk ring buffer,
1240 * the "current" transaction may fail (n-1),
1241 * and there is 919 slot numbers context information per transaction.
1243 * 72 transaction blocks amounts to more than 2**16 context slots,
1244 * so cap there first.
1246 const unsigned int max_al_nr = DRBD_AL_EXTENTS_MAX;
1247 const unsigned int sufficient_on_disk =
1248 (max_al_nr + AL_CONTEXT_PER_TRANSACTION -1)
1249 /AL_CONTEXT_PER_TRANSACTION;
1251 unsigned int al_size_4k = bdev->md.al_size_4k;
1253 if (al_size_4k > sufficient_on_disk)
1256 return (al_size_4k - 1) * AL_CONTEXT_PER_TRANSACTION;
1259 int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1261 enum drbd_ret_code retcode;
1262 struct drbd_device *device;
1263 struct disk_conf *new_disk_conf, *old_disk_conf;
1264 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
1267 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1268 if (!adm_ctx.reply_skb)
1270 if (retcode != NO_ERROR)
1273 device = adm_ctx.device;
1275 /* we also need a disk
1276 * to change the options on */
1277 if (!get_ldev(device)) {
1278 retcode = ERR_NO_DISK;
1282 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
1283 if (!new_disk_conf) {
1284 retcode = ERR_NOMEM;
1288 mutex_lock(&device->resource->conf_update);
1289 old_disk_conf = device->ldev->disk_conf;
1290 *new_disk_conf = *old_disk_conf;
1291 if (should_set_defaults(info))
1292 set_disk_conf_defaults(new_disk_conf);
1294 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
1295 if (err && err != -ENOMSG) {
1296 retcode = ERR_MANDATORY_TAG;
1297 drbd_msg_put_info(from_attrs_err_to_txt(err));
1301 if (!expect(new_disk_conf->resync_rate >= 1))
1302 new_disk_conf->resync_rate = 1;
1304 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1305 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
1306 if (new_disk_conf->al_extents > drbd_al_extents_max(device->ldev))
1307 new_disk_conf->al_extents = drbd_al_extents_max(device->ldev);
1309 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1310 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
1312 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
1313 if (fifo_size != device->rs_plan_s->size) {
1314 new_plan = fifo_alloc(fifo_size);
1316 drbd_err(device, "kmalloc of fifo_buffer failed");
1317 retcode = ERR_NOMEM;
1322 drbd_suspend_io(device);
1323 wait_event(device->al_wait, lc_try_lock(device->act_log));
1324 drbd_al_shrink(device);
1325 err = drbd_check_al_size(device, new_disk_conf);
1326 lc_unlock(device->act_log);
1327 wake_up(&device->al_wait);
1328 drbd_resume_io(device);
1331 retcode = ERR_NOMEM;
1335 write_lock_irq(&global_state_lock);
1336 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
1337 if (retcode == NO_ERROR) {
1338 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
1339 drbd_resync_after_changed(device);
1341 write_unlock_irq(&global_state_lock);
1343 if (retcode != NO_ERROR)
1347 old_plan = device->rs_plan_s;
1348 rcu_assign_pointer(device->rs_plan_s, new_plan);
1351 mutex_unlock(&device->resource->conf_update);
1353 if (new_disk_conf->al_updates)
1354 device->ldev->md.flags &= ~MDF_AL_DISABLED;
1356 device->ldev->md.flags |= MDF_AL_DISABLED;
1358 if (new_disk_conf->md_flushes)
1359 clear_bit(MD_NO_FUA, &device->flags);
1361 set_bit(MD_NO_FUA, &device->flags);
1363 drbd_bump_write_ordering(first_peer_device(device)->connection, WO_bdev_flush);
1365 drbd_md_sync(device);
1367 if (device->state.conn >= C_CONNECTED) {
1368 struct drbd_peer_device *peer_device;
1370 for_each_peer_device(peer_device, device)
1371 drbd_send_sync_param(peer_device);
1375 kfree(old_disk_conf);
1377 mod_timer(&device->request_timer, jiffies + HZ);
1381 mutex_unlock(&device->resource->conf_update);
1383 kfree(new_disk_conf);
1388 drbd_adm_finish(info, retcode);
1392 int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
1394 struct drbd_device *device;
1396 enum drbd_ret_code retcode;
1397 enum determine_dev_size dd;
1398 sector_t max_possible_sectors;
1399 sector_t min_md_device_sectors;
1400 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
1401 struct disk_conf *new_disk_conf = NULL;
1402 struct block_device *bdev;
1403 struct lru_cache *resync_lru = NULL;
1404 struct fifo_buffer *new_plan = NULL;
1405 union drbd_state ns, os;
1406 enum drbd_state_rv rv;
1407 struct net_conf *nc;
1409 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1410 if (!adm_ctx.reply_skb)
1412 if (retcode != NO_ERROR)
1415 device = adm_ctx.device;
1416 conn_reconfig_start(first_peer_device(device)->connection);
1418 /* if you want to reconfigure, please tear down first */
1419 if (device->state.disk > D_DISKLESS) {
1420 retcode = ERR_DISK_CONFIGURED;
1423 /* It may just now have detached because of IO error. Make sure
1424 * drbd_ldev_destroy is done already, we may end up here very fast,
1425 * e.g. if someone calls attach from the on-io-error handler,
1426 * to realize a "hot spare" feature (not that I'd recommend that) */
1427 wait_event(device->misc_wait, !atomic_read(&device->local_cnt));
1429 /* make sure there is no leftover from previous force-detach attempts */
1430 clear_bit(FORCE_DETACH, &device->flags);
1431 clear_bit(WAS_IO_ERROR, &device->flags);
1432 clear_bit(WAS_READ_ERROR, &device->flags);
1434 /* and no leftover from previously aborted resync or verify, either */
1435 device->rs_total = 0;
1436 device->rs_failed = 0;
1437 atomic_set(&device->rs_pending_cnt, 0);
1439 /* allocation not in the IO path, drbdsetup context */
1440 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1442 retcode = ERR_NOMEM;
1445 spin_lock_init(&nbc->md.uuid_lock);
1447 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
1448 if (!new_disk_conf) {
1449 retcode = ERR_NOMEM;
1452 nbc->disk_conf = new_disk_conf;
1454 set_disk_conf_defaults(new_disk_conf);
1455 err = disk_conf_from_attrs(new_disk_conf, info);
1457 retcode = ERR_MANDATORY_TAG;
1458 drbd_msg_put_info(from_attrs_err_to_txt(err));
1462 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1463 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
1465 new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
1467 retcode = ERR_NOMEM;
1471 if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
1472 retcode = ERR_MD_IDX_INVALID;
1476 write_lock_irq(&global_state_lock);
1477 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
1478 write_unlock_irq(&global_state_lock);
1479 if (retcode != NO_ERROR)
1483 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
1485 if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
1487 retcode = ERR_STONITH_AND_PROT_A;
1493 bdev = blkdev_get_by_path(new_disk_conf->backing_dev,
1494 FMODE_READ | FMODE_WRITE | FMODE_EXCL, device);
1496 drbd_err(device, "open(\"%s\") failed with %ld\n", new_disk_conf->backing_dev,
1498 retcode = ERR_OPEN_DISK;
1501 nbc->backing_bdev = bdev;
1504 * meta_dev_idx >= 0: external fixed size, possibly multiple
1505 * drbd sharing one meta device. TODO in that case, paranoia
1506 * check that [md_bdev, meta_dev_idx] is not yet used by some
1507 * other drbd minor! (if you use drbd.conf + drbdadm, that
1508 * should check it for you already; but if you don't, or
1509 * someone fooled it, we need to double check here)
1511 bdev = blkdev_get_by_path(new_disk_conf->meta_dev,
1512 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
1513 (new_disk_conf->meta_dev_idx < 0) ?
1514 (void *)device : (void *)drbd_m_holder);
1516 drbd_err(device, "open(\"%s\") failed with %ld\n", new_disk_conf->meta_dev,
1518 retcode = ERR_OPEN_MD_DISK;
1521 nbc->md_bdev = bdev;
1523 if ((nbc->backing_bdev == nbc->md_bdev) !=
1524 (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1525 new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
1526 retcode = ERR_MD_IDX_INVALID;
1530 resync_lru = lc_create("resync", drbd_bm_ext_cache,
1531 1, 61, sizeof(struct bm_extent),
1532 offsetof(struct bm_extent, lce));
1534 retcode = ERR_NOMEM;
1538 /* Read our meta data super block early.
1539 * This also sets other on-disk offsets. */
1540 retcode = drbd_md_read(device, nbc);
1541 if (retcode != NO_ERROR)
1544 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1545 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
1546 if (new_disk_conf->al_extents > drbd_al_extents_max(nbc))
1547 new_disk_conf->al_extents = drbd_al_extents_max(nbc);
1549 if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
1550 drbd_err(device, "max capacity %llu smaller than disk size %llu\n",
1551 (unsigned long long) drbd_get_max_capacity(nbc),
1552 (unsigned long long) new_disk_conf->disk_size);
1553 retcode = ERR_DISK_TOO_SMALL;
1557 if (new_disk_conf->meta_dev_idx < 0) {
1558 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1559 /* at least one MB, otherwise it does not make sense */
1560 min_md_device_sectors = (2<<10);
1562 max_possible_sectors = DRBD_MAX_SECTORS;
1563 min_md_device_sectors = MD_128MB_SECT * (new_disk_conf->meta_dev_idx + 1);
1566 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
1567 retcode = ERR_MD_DISK_TOO_SMALL;
1568 drbd_warn(device, "refusing attach: md-device too small, "
1569 "at least %llu sectors needed for this meta-disk type\n",
1570 (unsigned long long) min_md_device_sectors);
1574 /* Make sure the new disk is big enough
1575 * (we may currently be R_PRIMARY with no local disk...) */
1576 if (drbd_get_max_capacity(nbc) <
1577 drbd_get_capacity(device->this_bdev)) {
1578 retcode = ERR_DISK_TOO_SMALL;
1582 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1584 if (nbc->known_size > max_possible_sectors) {
1585 drbd_warn(device, "==> truncating very big lower level device "
1586 "to currently maximum possible %llu sectors <==\n",
1587 (unsigned long long) max_possible_sectors);
1588 if (new_disk_conf->meta_dev_idx >= 0)
1589 drbd_warn(device, "==>> using internal or flexible "
1590 "meta data may help <<==\n");
1593 drbd_suspend_io(device);
1594 /* also wait for the last barrier ack. */
1595 /* FIXME see also https://daiquiri.linbit/cgi-bin/bugzilla/show_bug.cgi?id=171
1596 * We need a way to either ignore barrier acks for barriers sent before a device
1597 * was attached, or a way to wait for all pending barrier acks to come in.
1598 * As barriers are counted per resource,
1599 * we'd need to suspend io on all devices of a resource.
1601 wait_event(device->misc_wait, !atomic_read(&device->ap_pending_cnt) || drbd_suspended(device));
1602 /* and for any other previously queued work */
1603 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
1605 rv = _drbd_request_state(device, NS(disk, D_ATTACHING), CS_VERBOSE);
1606 retcode = rv; /* FIXME: Type mismatch. */
1607 drbd_resume_io(device);
1608 if (rv < SS_SUCCESS)
1611 if (!get_ldev_if_state(device, D_ATTACHING))
1612 goto force_diskless;
1614 if (!device->bitmap) {
1615 if (drbd_bm_init(device)) {
1616 retcode = ERR_NOMEM;
1617 goto force_diskless_dec;
1621 if (device->state.conn < C_CONNECTED &&
1622 device->state.role == R_PRIMARY &&
1623 (device->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
1624 drbd_err(device, "Can only attach to data with current UUID=%016llX\n",
1625 (unsigned long long)device->ed_uuid);
1626 retcode = ERR_DATA_NOT_CURRENT;
1627 goto force_diskless_dec;
1630 /* Since we are diskless, fix the activity log first... */
1631 if (drbd_check_al_size(device, new_disk_conf)) {
1632 retcode = ERR_NOMEM;
1633 goto force_diskless_dec;
1636 /* Prevent shrinking of consistent devices ! */
1637 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
1638 drbd_new_dev_size(device, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
1639 drbd_warn(device, "refusing to truncate a consistent device\n");
1640 retcode = ERR_DISK_TOO_SMALL;
1641 goto force_diskless_dec;
1644 /* Reset the "barriers don't work" bits here, then force meta data to
1645 * be written, to ensure we determine if barriers are supported. */
1646 if (new_disk_conf->md_flushes)
1647 clear_bit(MD_NO_FUA, &device->flags);
1649 set_bit(MD_NO_FUA, &device->flags);
1651 /* Point of no return reached.
1652 * Devices and memory are no longer released by error cleanup below.
1653 * now device takes over responsibility, and the state engine should
1654 * clean it up somewhere. */
1655 D_ASSERT(device, device->ldev == NULL);
1657 device->resync = resync_lru;
1658 device->rs_plan_s = new_plan;
1661 new_disk_conf = NULL;
1664 drbd_bump_write_ordering(first_peer_device(device)->connection, WO_bdev_flush);
1666 if (drbd_md_test_flag(device->ldev, MDF_CRASHED_PRIMARY))
1667 set_bit(CRASHED_PRIMARY, &device->flags);
1669 clear_bit(CRASHED_PRIMARY, &device->flags);
1671 if (drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
1672 !(device->state.role == R_PRIMARY && device->resource->susp_nod))
1673 set_bit(CRASHED_PRIMARY, &device->flags);
1675 device->send_cnt = 0;
1676 device->recv_cnt = 0;
1677 device->read_cnt = 0;
1678 device->writ_cnt = 0;
1680 drbd_reconsider_max_bio_size(device);
1682 /* If I am currently not R_PRIMARY,
1683 * but meta data primary indicator is set,
1684 * I just now recover from a hard crash,
1685 * and have been R_PRIMARY before that crash.
1687 * Now, if I had no connection before that crash
1688 * (have been degraded R_PRIMARY), chances are that
1689 * I won't find my peer now either.
1691 * In that case, and _only_ in that case,
1692 * we use the degr-wfc-timeout instead of the default,
1693 * so we can automatically recover from a crash of a
1694 * degraded but active "cluster" after a certain timeout.
1696 clear_bit(USE_DEGR_WFC_T, &device->flags);
1697 if (device->state.role != R_PRIMARY &&
1698 drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
1699 !drbd_md_test_flag(device->ldev, MDF_CONNECTED_IND))
1700 set_bit(USE_DEGR_WFC_T, &device->flags);
1702 dd = drbd_determine_dev_size(device, 0, NULL);
1703 if (dd <= DS_ERROR) {
1704 retcode = ERR_NOMEM_BITMAP;
1705 goto force_diskless_dec;
1706 } else if (dd == DS_GREW)
1707 set_bit(RESYNC_AFTER_NEG, &device->flags);
1709 if (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC) ||
1710 (test_bit(CRASHED_PRIMARY, &device->flags) &&
1711 drbd_md_test_flag(device->ldev, MDF_AL_DISABLED))) {
1712 drbd_info(device, "Assuming that all blocks are out of sync "
1713 "(aka FullSync)\n");
1714 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
1715 "set_n_write from attaching", BM_LOCKED_MASK)) {
1716 retcode = ERR_IO_MD_DISK;
1717 goto force_diskless_dec;
1720 if (drbd_bitmap_io(device, &drbd_bm_read,
1721 "read from attaching", BM_LOCKED_MASK)) {
1722 retcode = ERR_IO_MD_DISK;
1723 goto force_diskless_dec;
1727 if (_drbd_bm_total_weight(device) == drbd_bm_bits(device))
1728 drbd_suspend_al(device); /* IO is still suspended here... */
1730 spin_lock_irq(&device->resource->req_lock);
1731 os = drbd_read_state(device);
1733 /* If MDF_CONSISTENT is not set go into inconsistent state,
1734 otherwise investigate MDF_WasUpToDate...
1735 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1736 otherwise into D_CONSISTENT state.
1738 if (drbd_md_test_flag(device->ldev, MDF_CONSISTENT)) {
1739 if (drbd_md_test_flag(device->ldev, MDF_WAS_UP_TO_DATE))
1740 ns.disk = D_CONSISTENT;
1742 ns.disk = D_OUTDATED;
1744 ns.disk = D_INCONSISTENT;
1747 if (drbd_md_test_flag(device->ldev, MDF_PEER_OUT_DATED))
1748 ns.pdsk = D_OUTDATED;
1751 if (ns.disk == D_CONSISTENT &&
1752 (ns.pdsk == D_OUTDATED || rcu_dereference(device->ldev->disk_conf)->fencing == FP_DONT_CARE))
1753 ns.disk = D_UP_TO_DATE;
1755 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1756 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1757 this point, because drbd_request_state() modifies these
1760 if (rcu_dereference(device->ldev->disk_conf)->al_updates)
1761 device->ldev->md.flags &= ~MDF_AL_DISABLED;
1763 device->ldev->md.flags |= MDF_AL_DISABLED;
1767 /* In case we are C_CONNECTED postpone any decision on the new disk
1768 state after the negotiation phase. */
1769 if (device->state.conn == C_CONNECTED) {
1770 device->new_state_tmp.i = ns.i;
1772 ns.disk = D_NEGOTIATING;
1774 /* We expect to receive up-to-date UUIDs soon.
1775 To avoid a race in receive_state, free p_uuid while
1776 holding req_lock. I.e. atomic with the state change */
1777 kfree(device->p_uuid);
1778 device->p_uuid = NULL;
1781 rv = _drbd_set_state(device, ns, CS_VERBOSE, NULL);
1782 spin_unlock_irq(&device->resource->req_lock);
1784 if (rv < SS_SUCCESS)
1785 goto force_diskless_dec;
1787 mod_timer(&device->request_timer, jiffies + HZ);
1789 if (device->state.role == R_PRIMARY)
1790 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
1792 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
1794 drbd_md_mark_dirty(device);
1795 drbd_md_sync(device);
1797 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
1799 conn_reconfig_done(first_peer_device(device)->connection);
1800 drbd_adm_finish(info, retcode);
1806 drbd_force_state(device, NS(disk, D_DISKLESS));
1807 drbd_md_sync(device);
1809 conn_reconfig_done(first_peer_device(device)->connection);
1811 if (nbc->backing_bdev)
1812 blkdev_put(nbc->backing_bdev,
1813 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1815 blkdev_put(nbc->md_bdev,
1816 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1819 kfree(new_disk_conf);
1820 lc_destroy(resync_lru);
1824 drbd_adm_finish(info, retcode);
1828 static int adm_detach(struct drbd_device *device, int force)
1830 enum drbd_state_rv retcode;
1834 set_bit(FORCE_DETACH, &device->flags);
1835 drbd_force_state(device, NS(disk, D_FAILED));
1836 retcode = SS_SUCCESS;
1840 drbd_suspend_io(device); /* so no-one is stuck in drbd_al_begin_io */
1841 drbd_md_get_buffer(device); /* make sure there is no in-flight meta-data IO */
1842 retcode = drbd_request_state(device, NS(disk, D_FAILED));
1843 drbd_md_put_buffer(device);
1844 /* D_FAILED will transition to DISKLESS. */
1845 ret = wait_event_interruptible(device->misc_wait,
1846 device->state.disk != D_FAILED);
1847 drbd_resume_io(device);
1848 if ((int)retcode == (int)SS_IS_DISKLESS)
1849 retcode = SS_NOTHING_TO_DO;
1856 /* Detaching the disk is a process in multiple stages. First we need to lock
1857 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1858 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1859 * internal references as well.
1860 * Only then we have finally detached. */
1861 int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
1863 enum drbd_ret_code retcode;
1864 struct detach_parms parms = { };
1867 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1868 if (!adm_ctx.reply_skb)
1870 if (retcode != NO_ERROR)
1873 if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
1874 err = detach_parms_from_attrs(&parms, info);
1876 retcode = ERR_MANDATORY_TAG;
1877 drbd_msg_put_info(from_attrs_err_to_txt(err));
1882 retcode = adm_detach(adm_ctx.device, parms.force_detach);
1884 drbd_adm_finish(info, retcode);
1888 static bool conn_resync_running(struct drbd_connection *connection)
1890 struct drbd_peer_device *peer_device;
1895 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1896 struct drbd_device *device = peer_device->device;
1897 if (device->state.conn == C_SYNC_SOURCE ||
1898 device->state.conn == C_SYNC_TARGET ||
1899 device->state.conn == C_PAUSED_SYNC_S ||
1900 device->state.conn == C_PAUSED_SYNC_T) {
1910 static bool conn_ov_running(struct drbd_connection *connection)
1912 struct drbd_peer_device *peer_device;
1917 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1918 struct drbd_device *device = peer_device->device;
1919 if (device->state.conn == C_VERIFY_S ||
1920 device->state.conn == C_VERIFY_T) {
1930 static enum drbd_ret_code
1931 _check_net_options(struct drbd_connection *connection, struct net_conf *old_net_conf, struct net_conf *new_net_conf)
1933 struct drbd_peer_device *peer_device;
1936 if (old_net_conf && connection->cstate == C_WF_REPORT_PARAMS && connection->agreed_pro_version < 100) {
1937 if (new_net_conf->wire_protocol != old_net_conf->wire_protocol)
1938 return ERR_NEED_APV_100;
1940 if (new_net_conf->two_primaries != old_net_conf->two_primaries)
1941 return ERR_NEED_APV_100;
1943 if (strcmp(new_net_conf->integrity_alg, old_net_conf->integrity_alg))
1944 return ERR_NEED_APV_100;
1947 if (!new_net_conf->two_primaries &&
1948 conn_highest_role(connection) == R_PRIMARY &&
1949 conn_highest_peer(connection) == R_PRIMARY)
1950 return ERR_NEED_ALLOW_TWO_PRI;
1952 if (new_net_conf->two_primaries &&
1953 (new_net_conf->wire_protocol != DRBD_PROT_C))
1954 return ERR_NOT_PROTO_C;
1956 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
1957 struct drbd_device *device = peer_device->device;
1958 if (get_ldev(device)) {
1959 enum drbd_fencing_p fp = rcu_dereference(device->ldev->disk_conf)->fencing;
1961 if (new_net_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
1962 return ERR_STONITH_AND_PROT_A;
1964 if (device->state.role == R_PRIMARY && new_net_conf->discard_my_data)
1965 return ERR_DISCARD_IMPOSSIBLE;
1968 if (new_net_conf->on_congestion != OC_BLOCK && new_net_conf->wire_protocol != DRBD_PROT_A)
1969 return ERR_CONG_NOT_PROTO_A;
1974 static enum drbd_ret_code
1975 check_net_options(struct drbd_connection *connection, struct net_conf *new_net_conf)
1977 static enum drbd_ret_code rv;
1978 struct drbd_peer_device *peer_device;
1982 rv = _check_net_options(connection, rcu_dereference(connection->net_conf), new_net_conf);
1985 /* connection->volumes protected by genl_lock() here */
1986 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
1987 struct drbd_device *device = peer_device->device;
1988 if (!device->bitmap) {
1989 if (drbd_bm_init(device))
1998 struct crypto_hash *verify_tfm;
1999 struct crypto_hash *csums_tfm;
2000 struct crypto_hash *cram_hmac_tfm;
2001 struct crypto_hash *integrity_tfm;
2005 alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
2010 *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
2019 static enum drbd_ret_code
2020 alloc_crypto(struct crypto *crypto, struct net_conf *new_net_conf)
2022 char hmac_name[CRYPTO_MAX_ALG_NAME];
2023 enum drbd_ret_code rv;
2025 rv = alloc_hash(&crypto->csums_tfm, new_net_conf->csums_alg,
2029 rv = alloc_hash(&crypto->verify_tfm, new_net_conf->verify_alg,
2033 rv = alloc_hash(&crypto->integrity_tfm, new_net_conf->integrity_alg,
2037 if (new_net_conf->cram_hmac_alg[0] != 0) {
2038 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
2039 new_net_conf->cram_hmac_alg);
2041 rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
2048 static void free_crypto(struct crypto *crypto)
2050 crypto_free_hash(crypto->cram_hmac_tfm);
2051 crypto_free_hash(crypto->integrity_tfm);
2052 crypto_free_hash(crypto->csums_tfm);
2053 crypto_free_hash(crypto->verify_tfm);
2056 int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
2058 enum drbd_ret_code retcode;
2059 struct drbd_connection *connection;
2060 struct net_conf *old_net_conf, *new_net_conf = NULL;
2062 int ovr; /* online verify running */
2063 int rsr; /* re-sync running */
2064 struct crypto crypto = { };
2066 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
2067 if (!adm_ctx.reply_skb)
2069 if (retcode != NO_ERROR)
2072 connection = adm_ctx.connection;
2074 new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
2075 if (!new_net_conf) {
2076 retcode = ERR_NOMEM;
2080 conn_reconfig_start(connection);
2082 mutex_lock(&connection->data.mutex);
2083 mutex_lock(&connection->resource->conf_update);
2084 old_net_conf = connection->net_conf;
2086 if (!old_net_conf) {
2087 drbd_msg_put_info("net conf missing, try connect");
2088 retcode = ERR_INVALID_REQUEST;
2092 *new_net_conf = *old_net_conf;
2093 if (should_set_defaults(info))
2094 set_net_conf_defaults(new_net_conf);
2096 err = net_conf_from_attrs_for_change(new_net_conf, info);
2097 if (err && err != -ENOMSG) {
2098 retcode = ERR_MANDATORY_TAG;
2099 drbd_msg_put_info(from_attrs_err_to_txt(err));
2103 retcode = check_net_options(connection, new_net_conf);
2104 if (retcode != NO_ERROR)
2107 /* re-sync running */
2108 rsr = conn_resync_running(connection);
2109 if (rsr && strcmp(new_net_conf->csums_alg, old_net_conf->csums_alg)) {
2110 retcode = ERR_CSUMS_RESYNC_RUNNING;
2114 /* online verify running */
2115 ovr = conn_ov_running(connection);
2116 if (ovr && strcmp(new_net_conf->verify_alg, old_net_conf->verify_alg)) {
2117 retcode = ERR_VERIFY_RUNNING;
2121 retcode = alloc_crypto(&crypto, new_net_conf);
2122 if (retcode != NO_ERROR)
2125 rcu_assign_pointer(connection->net_conf, new_net_conf);
2128 crypto_free_hash(connection->csums_tfm);
2129 connection->csums_tfm = crypto.csums_tfm;
2130 crypto.csums_tfm = NULL;
2133 crypto_free_hash(connection->verify_tfm);
2134 connection->verify_tfm = crypto.verify_tfm;
2135 crypto.verify_tfm = NULL;
2138 crypto_free_hash(connection->integrity_tfm);
2139 connection->integrity_tfm = crypto.integrity_tfm;
2140 if (connection->cstate >= C_WF_REPORT_PARAMS && connection->agreed_pro_version >= 100)
2141 /* Do this without trying to take connection->data.mutex again. */
2142 __drbd_send_protocol(connection, P_PROTOCOL_UPDATE);
2144 crypto_free_hash(connection->cram_hmac_tfm);
2145 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
2147 mutex_unlock(&connection->resource->conf_update);
2148 mutex_unlock(&connection->data.mutex);
2150 kfree(old_net_conf);
2152 if (connection->cstate >= C_WF_REPORT_PARAMS) {
2153 struct drbd_peer_device *peer_device;
2156 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
2157 drbd_send_sync_param(peer_device);
2163 mutex_unlock(&connection->resource->conf_update);
2164 mutex_unlock(&connection->data.mutex);
2165 free_crypto(&crypto);
2166 kfree(new_net_conf);
2168 conn_reconfig_done(connection);
2170 drbd_adm_finish(info, retcode);
2174 int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
2176 struct drbd_peer_device *peer_device;
2177 struct net_conf *old_net_conf, *new_net_conf = NULL;
2178 struct crypto crypto = { };
2179 struct drbd_resource *resource;
2180 struct drbd_connection *connection;
2181 enum drbd_ret_code retcode;
2185 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
2187 if (!adm_ctx.reply_skb)
2189 if (retcode != NO_ERROR)
2191 if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
2192 drbd_msg_put_info("connection endpoint(s) missing");
2193 retcode = ERR_INVALID_REQUEST;
2197 /* No need for _rcu here. All reconfiguration is
2198 * strictly serialized on genl_lock(). We are protected against
2199 * concurrent reconfiguration/addition/deletion */
2200 for_each_resource(resource, &drbd_resources) {
2201 for_each_connection(connection, resource) {
2202 if (nla_len(adm_ctx.my_addr) == connection->my_addr_len &&
2203 !memcmp(nla_data(adm_ctx.my_addr), &connection->my_addr,
2204 connection->my_addr_len)) {
2205 retcode = ERR_LOCAL_ADDR;
2209 if (nla_len(adm_ctx.peer_addr) == connection->peer_addr_len &&
2210 !memcmp(nla_data(adm_ctx.peer_addr), &connection->peer_addr,
2211 connection->peer_addr_len)) {
2212 retcode = ERR_PEER_ADDR;
2218 connection = first_connection(adm_ctx.resource);
2219 conn_reconfig_start(connection);
2221 if (connection->cstate > C_STANDALONE) {
2222 retcode = ERR_NET_CONFIGURED;
2226 /* allocation not in the IO path, drbdsetup / netlink process context */
2227 new_net_conf = kzalloc(sizeof(*new_net_conf), GFP_KERNEL);
2228 if (!new_net_conf) {
2229 retcode = ERR_NOMEM;
2233 set_net_conf_defaults(new_net_conf);
2235 err = net_conf_from_attrs(new_net_conf, info);
2236 if (err && err != -ENOMSG) {
2237 retcode = ERR_MANDATORY_TAG;
2238 drbd_msg_put_info(from_attrs_err_to_txt(err));
2242 retcode = check_net_options(connection, new_net_conf);
2243 if (retcode != NO_ERROR)
2246 retcode = alloc_crypto(&crypto, new_net_conf);
2247 if (retcode != NO_ERROR)
2250 ((char *)new_net_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
2252 drbd_flush_workqueue(&connection->sender_work);
2254 mutex_lock(&adm_ctx.resource->conf_update);
2255 old_net_conf = connection->net_conf;
2257 retcode = ERR_NET_CONFIGURED;
2258 mutex_unlock(&adm_ctx.resource->conf_update);
2261 rcu_assign_pointer(connection->net_conf, new_net_conf);
2263 conn_free_crypto(connection);
2264 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
2265 connection->integrity_tfm = crypto.integrity_tfm;
2266 connection->csums_tfm = crypto.csums_tfm;
2267 connection->verify_tfm = crypto.verify_tfm;
2269 connection->my_addr_len = nla_len(adm_ctx.my_addr);
2270 memcpy(&connection->my_addr, nla_data(adm_ctx.my_addr), connection->my_addr_len);
2271 connection->peer_addr_len = nla_len(adm_ctx.peer_addr);
2272 memcpy(&connection->peer_addr, nla_data(adm_ctx.peer_addr), connection->peer_addr_len);
2274 mutex_unlock(&adm_ctx.resource->conf_update);
2277 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2278 struct drbd_device *device = peer_device->device;
2279 device->send_cnt = 0;
2280 device->recv_cnt = 0;
2284 retcode = conn_request_state(connection, NS(conn, C_UNCONNECTED), CS_VERBOSE);
2286 conn_reconfig_done(connection);
2287 drbd_adm_finish(info, retcode);
2291 free_crypto(&crypto);
2292 kfree(new_net_conf);
2294 conn_reconfig_done(connection);
2296 drbd_adm_finish(info, retcode);
2300 static enum drbd_state_rv conn_try_disconnect(struct drbd_connection *connection, bool force)
2302 enum drbd_state_rv rv;
2304 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
2305 force ? CS_HARD : 0);
2308 case SS_NOTHING_TO_DO:
2310 case SS_ALREADY_STANDALONE:
2312 case SS_PRIMARY_NOP:
2313 /* Our state checking code wants to see the peer outdated. */
2314 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING, pdsk, D_OUTDATED), 0);
2316 if (rv == SS_OUTDATE_WO_CONN) /* lost connection before graceful disconnect succeeded */
2317 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_VERBOSE);
2320 case SS_CW_FAILED_BY_PEER:
2321 /* The peer probably wants to see us outdated. */
2322 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING,
2323 disk, D_OUTDATED), 0);
2324 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
2325 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
2330 /* no special handling necessary */
2333 if (rv >= SS_SUCCESS) {
2334 enum drbd_state_rv rv2;
2335 /* No one else can reconfigure the network while I am here.
2336 * The state handling only uses drbd_thread_stop_nowait(),
2337 * we want to really wait here until the receiver is no more.
2339 drbd_thread_stop(&connection->receiver);
2341 /* Race breaker. This additional state change request may be
2342 * necessary, if this was a forced disconnect during a receiver
2343 * restart. We may have "killed" the receiver thread just
2344 * after drbd_receiver() returned. Typically, we should be
2345 * C_STANDALONE already, now, and this becomes a no-op.
2347 rv2 = conn_request_state(connection, NS(conn, C_STANDALONE),
2348 CS_VERBOSE | CS_HARD);
2349 if (rv2 < SS_SUCCESS)
2350 drbd_err(connection,
2351 "unexpected rv2=%d in conn_try_disconnect()\n",
2357 int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
2359 struct disconnect_parms parms;
2360 struct drbd_connection *connection;
2361 enum drbd_state_rv rv;
2362 enum drbd_ret_code retcode;
2365 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
2366 if (!adm_ctx.reply_skb)
2368 if (retcode != NO_ERROR)
2371 connection = adm_ctx.connection;
2372 memset(&parms, 0, sizeof(parms));
2373 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
2374 err = disconnect_parms_from_attrs(&parms, info);
2376 retcode = ERR_MANDATORY_TAG;
2377 drbd_msg_put_info(from_attrs_err_to_txt(err));
2382 rv = conn_try_disconnect(connection, parms.force_disconnect);
2383 if (rv < SS_SUCCESS)
2384 retcode = rv; /* FIXME: Type mismatch. */
2388 drbd_adm_finish(info, retcode);
2392 void resync_after_online_grow(struct drbd_device *device)
2394 int iass; /* I am sync source */
2396 drbd_info(device, "Resync of new storage after online grow\n");
2397 if (device->state.role != device->state.peer)
2398 iass = (device->state.role == R_PRIMARY);
2400 iass = test_bit(RESOLVE_CONFLICTS, &first_peer_device(device)->connection->flags);
2403 drbd_start_resync(device, C_SYNC_SOURCE);
2405 _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
2408 int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
2410 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
2411 struct resize_parms rs;
2412 struct drbd_device *device;
2413 enum drbd_ret_code retcode;
2414 enum determine_dev_size dd;
2415 bool change_al_layout = false;
2416 enum dds_flags ddsf;
2420 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2421 if (!adm_ctx.reply_skb)
2423 if (retcode != NO_ERROR)
2426 device = adm_ctx.device;
2427 if (!get_ldev(device)) {
2428 retcode = ERR_NO_DISK;
2432 memset(&rs, 0, sizeof(struct resize_parms));
2433 rs.al_stripes = device->ldev->md.al_stripes;
2434 rs.al_stripe_size = device->ldev->md.al_stripe_size_4k * 4;
2435 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
2436 err = resize_parms_from_attrs(&rs, info);
2438 retcode = ERR_MANDATORY_TAG;
2439 drbd_msg_put_info(from_attrs_err_to_txt(err));
2444 if (device->state.conn > C_CONNECTED) {
2445 retcode = ERR_RESIZE_RESYNC;
2449 if (device->state.role == R_SECONDARY &&
2450 device->state.peer == R_SECONDARY) {
2451 retcode = ERR_NO_PRIMARY;
2455 if (rs.no_resync && first_peer_device(device)->connection->agreed_pro_version < 93) {
2456 retcode = ERR_NEED_APV_93;
2461 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
2463 if (u_size != (sector_t)rs.resize_size) {
2464 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
2465 if (!new_disk_conf) {
2466 retcode = ERR_NOMEM;
2471 if (device->ldev->md.al_stripes != rs.al_stripes ||
2472 device->ldev->md.al_stripe_size_4k != rs.al_stripe_size / 4) {
2473 u32 al_size_k = rs.al_stripes * rs.al_stripe_size;
2475 if (al_size_k > (16 * 1024 * 1024)) {
2476 retcode = ERR_MD_LAYOUT_TOO_BIG;
2480 if (al_size_k < MD_32kB_SECT/2) {
2481 retcode = ERR_MD_LAYOUT_TOO_SMALL;
2485 if (device->state.conn != C_CONNECTED) {
2486 retcode = ERR_MD_LAYOUT_CONNECTED;
2490 change_al_layout = true;
2493 if (device->ldev->known_size != drbd_get_capacity(device->ldev->backing_bdev))
2494 device->ldev->known_size = drbd_get_capacity(device->ldev->backing_bdev);
2496 if (new_disk_conf) {
2497 mutex_lock(&device->resource->conf_update);
2498 old_disk_conf = device->ldev->disk_conf;
2499 *new_disk_conf = *old_disk_conf;
2500 new_disk_conf->disk_size = (sector_t)rs.resize_size;
2501 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
2502 mutex_unlock(&device->resource->conf_update);
2504 kfree(old_disk_conf);
2507 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
2508 dd = drbd_determine_dev_size(device, ddsf, change_al_layout ? &rs : NULL);
2509 drbd_md_sync(device);
2511 if (dd == DS_ERROR) {
2512 retcode = ERR_NOMEM_BITMAP;
2514 } else if (dd == DS_ERROR_SPACE_MD) {
2515 retcode = ERR_MD_LAYOUT_NO_FIT;
2517 } else if (dd == DS_ERROR_SHRINK) {
2518 retcode = ERR_IMPLICIT_SHRINK;
2522 if (device->state.conn == C_CONNECTED) {
2524 set_bit(RESIZE_PENDING, &device->flags);
2526 drbd_send_uuids(first_peer_device(device));
2527 drbd_send_sizes(first_peer_device(device), 1, ddsf);
2531 drbd_adm_finish(info, retcode);
2539 int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
2541 enum drbd_ret_code retcode;
2542 struct res_opts res_opts;
2545 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
2546 if (!adm_ctx.reply_skb)
2548 if (retcode != NO_ERROR)
2551 res_opts = adm_ctx.resource->res_opts;
2552 if (should_set_defaults(info))
2553 set_res_opts_defaults(&res_opts);
2555 err = res_opts_from_attrs(&res_opts, info);
2556 if (err && err != -ENOMSG) {
2557 retcode = ERR_MANDATORY_TAG;
2558 drbd_msg_put_info(from_attrs_err_to_txt(err));
2562 err = set_resource_options(adm_ctx.resource, &res_opts);
2564 retcode = ERR_INVALID_REQUEST;
2566 retcode = ERR_NOMEM;
2570 drbd_adm_finish(info, retcode);
2574 int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
2576 struct drbd_device *device;
2577 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2579 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2580 if (!adm_ctx.reply_skb)
2582 if (retcode != NO_ERROR)
2585 device = adm_ctx.device;
2587 /* If there is still bitmap IO pending, probably because of a previous
2588 * resync just being finished, wait for it before requesting a new resync.
2589 * Also wait for it's after_state_ch(). */
2590 drbd_suspend_io(device);
2591 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
2592 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
2594 /* If we happen to be C_STANDALONE R_SECONDARY, just change to
2595 * D_INCONSISTENT, and set all bits in the bitmap. Otherwise,
2596 * try to start a resync handshake as sync target for full sync.
2598 if (device->state.conn == C_STANDALONE && device->state.role == R_SECONDARY) {
2599 retcode = drbd_request_state(device, NS(disk, D_INCONSISTENT));
2600 if (retcode >= SS_SUCCESS) {
2601 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
2602 "set_n_write from invalidate", BM_LOCKED_MASK))
2603 retcode = ERR_IO_MD_DISK;
2606 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_T));
2607 drbd_resume_io(device);
2610 drbd_adm_finish(info, retcode);
2614 static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2615 union drbd_state mask, union drbd_state val)
2617 enum drbd_ret_code retcode;
2619 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2620 if (!adm_ctx.reply_skb)
2622 if (retcode != NO_ERROR)
2625 retcode = drbd_request_state(adm_ctx.device, mask, val);
2627 drbd_adm_finish(info, retcode);
2631 static int drbd_bmio_set_susp_al(struct drbd_device *device)
2635 rv = drbd_bmio_set_n_write(device);
2636 drbd_suspend_al(device);
2640 int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
2642 int retcode; /* drbd_ret_code, drbd_state_rv */
2643 struct drbd_device *device;
2645 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2646 if (!adm_ctx.reply_skb)
2648 if (retcode != NO_ERROR)
2651 device = adm_ctx.device;
2653 /* If there is still bitmap IO pending, probably because of a previous
2654 * resync just being finished, wait for it before requesting a new resync.
2655 * Also wait for it's after_state_ch(). */
2656 drbd_suspend_io(device);
2657 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
2658 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
2660 /* If we happen to be C_STANDALONE R_PRIMARY, just set all bits
2661 * in the bitmap. Otherwise, try to start a resync handshake
2662 * as sync source for full sync.
2664 if (device->state.conn == C_STANDALONE && device->state.role == R_PRIMARY) {
2665 /* The peer will get a resync upon connect anyways. Just make that
2666 into a full resync. */
2667 retcode = drbd_request_state(device, NS(pdsk, D_INCONSISTENT));
2668 if (retcode >= SS_SUCCESS) {
2669 if (drbd_bitmap_io(device, &drbd_bmio_set_susp_al,
2670 "set_n_write from invalidate_peer",
2671 BM_LOCKED_SET_ALLOWED))
2672 retcode = ERR_IO_MD_DISK;
2675 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_S));
2676 drbd_resume_io(device);
2679 drbd_adm_finish(info, retcode);
2683 int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
2685 enum drbd_ret_code retcode;
2687 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2688 if (!adm_ctx.reply_skb)
2690 if (retcode != NO_ERROR)
2693 if (drbd_request_state(adm_ctx.device, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
2694 retcode = ERR_PAUSE_IS_SET;
2696 drbd_adm_finish(info, retcode);
2700 int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
2702 union drbd_dev_state s;
2703 enum drbd_ret_code retcode;
2705 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2706 if (!adm_ctx.reply_skb)
2708 if (retcode != NO_ERROR)
2711 if (drbd_request_state(adm_ctx.device, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2712 s = adm_ctx.device->state;
2713 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2714 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2715 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2717 retcode = ERR_PAUSE_IS_CLEAR;
2722 drbd_adm_finish(info, retcode);
2726 int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
2728 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
2731 int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
2733 struct drbd_device *device;
2734 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2736 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2737 if (!adm_ctx.reply_skb)
2739 if (retcode != NO_ERROR)
2742 device = adm_ctx.device;
2743 if (test_bit(NEW_CUR_UUID, &device->flags)) {
2744 drbd_uuid_new_current(device);
2745 clear_bit(NEW_CUR_UUID, &device->flags);
2747 drbd_suspend_io(device);
2748 retcode = drbd_request_state(device, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
2749 if (retcode == SS_SUCCESS) {
2750 if (device->state.conn < C_CONNECTED)
2751 tl_clear(first_peer_device(device)->connection);
2752 if (device->state.disk == D_DISKLESS || device->state.disk == D_FAILED)
2753 tl_restart(first_peer_device(device)->connection, FAIL_FROZEN_DISK_IO);
2755 drbd_resume_io(device);
2758 drbd_adm_finish(info, retcode);
2762 int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
2764 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
2767 static int nla_put_drbd_cfg_context(struct sk_buff *skb,
2768 struct drbd_resource *resource,
2769 struct drbd_connection *connection,
2770 struct drbd_device *device)
2773 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
2775 goto nla_put_failure;
2777 nla_put_u32(skb, T_ctx_volume, device->vnr))
2778 goto nla_put_failure;
2779 if (nla_put_string(skb, T_ctx_resource_name, resource->name))
2780 goto nla_put_failure;
2782 if (connection->my_addr_len &&
2783 nla_put(skb, T_ctx_my_addr, connection->my_addr_len, &connection->my_addr))
2784 goto nla_put_failure;
2785 if (connection->peer_addr_len &&
2786 nla_put(skb, T_ctx_peer_addr, connection->peer_addr_len, &connection->peer_addr))
2787 goto nla_put_failure;
2789 nla_nest_end(skb, nla);
2794 nla_nest_cancel(skb, nla);
2799 * Return the connection of @resource if @resource has exactly one connection.
2801 static struct drbd_connection *the_only_connection(struct drbd_resource *resource)
2803 struct list_head *connections = &resource->connections;
2805 if (list_empty(connections) || connections->next->next != connections)
2807 return list_first_entry(&resource->connections, struct drbd_connection, connections);
2810 int nla_put_status_info(struct sk_buff *skb, struct drbd_device *device,
2811 const struct sib_info *sib)
2813 struct drbd_resource *resource = device->resource;
2814 struct state_info *si = NULL; /* for sizeof(si->member); */
2818 int exclude_sensitive;
2820 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2821 * to. So we better exclude_sensitive information.
2823 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2824 * in the context of the requesting user process. Exclude sensitive
2825 * information, unless current has superuser.
2827 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2828 * relies on the current implementation of netlink_dump(), which
2829 * executes the dump callback successively from netlink_recvmsg(),
2830 * always in the context of the receiving process */
2831 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
2833 got_ldev = get_ldev(device);
2835 /* We need to add connection name and volume number information still.
2836 * Minor number is in drbd_genlmsghdr. */
2837 if (nla_put_drbd_cfg_context(skb, resource, the_only_connection(resource), device))
2838 goto nla_put_failure;
2840 if (res_opts_to_skb(skb, &device->resource->res_opts, exclude_sensitive))
2841 goto nla_put_failure;
2845 struct disk_conf *disk_conf;
2847 disk_conf = rcu_dereference(device->ldev->disk_conf);
2848 err = disk_conf_to_skb(skb, disk_conf, exclude_sensitive);
2851 struct net_conf *nc;
2853 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
2855 err = net_conf_to_skb(skb, nc, exclude_sensitive);
2859 goto nla_put_failure;
2861 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
2863 goto nla_put_failure;
2864 if (nla_put_u32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY) ||
2865 nla_put_u32(skb, T_current_state, device->state.i) ||
2866 nla_put_u64(skb, T_ed_uuid, device->ed_uuid) ||
2867 nla_put_u64(skb, T_capacity, drbd_get_capacity(device->this_bdev)) ||
2868 nla_put_u64(skb, T_send_cnt, device->send_cnt) ||
2869 nla_put_u64(skb, T_recv_cnt, device->recv_cnt) ||
2870 nla_put_u64(skb, T_read_cnt, device->read_cnt) ||
2871 nla_put_u64(skb, T_writ_cnt, device->writ_cnt) ||
2872 nla_put_u64(skb, T_al_writ_cnt, device->al_writ_cnt) ||
2873 nla_put_u64(skb, T_bm_writ_cnt, device->bm_writ_cnt) ||
2874 nla_put_u32(skb, T_ap_bio_cnt, atomic_read(&device->ap_bio_cnt)) ||
2875 nla_put_u32(skb, T_ap_pending_cnt, atomic_read(&device->ap_pending_cnt)) ||
2876 nla_put_u32(skb, T_rs_pending_cnt, atomic_read(&device->rs_pending_cnt)))
2877 goto nla_put_failure;
2882 spin_lock_irq(&device->ldev->md.uuid_lock);
2883 err = nla_put(skb, T_uuids, sizeof(si->uuids), device->ldev->md.uuid);
2884 spin_unlock_irq(&device->ldev->md.uuid_lock);
2887 goto nla_put_failure;
2889 if (nla_put_u32(skb, T_disk_flags, device->ldev->md.flags) ||
2890 nla_put_u64(skb, T_bits_total, drbd_bm_bits(device)) ||
2891 nla_put_u64(skb, T_bits_oos, drbd_bm_total_weight(device)))
2892 goto nla_put_failure;
2893 if (C_SYNC_SOURCE <= device->state.conn &&
2894 C_PAUSED_SYNC_T >= device->state.conn) {
2895 if (nla_put_u64(skb, T_bits_rs_total, device->rs_total) ||
2896 nla_put_u64(skb, T_bits_rs_failed, device->rs_failed))
2897 goto nla_put_failure;
2902 switch(sib->sib_reason) {
2903 case SIB_SYNC_PROGRESS:
2904 case SIB_GET_STATUS_REPLY:
2906 case SIB_STATE_CHANGE:
2907 if (nla_put_u32(skb, T_prev_state, sib->os.i) ||
2908 nla_put_u32(skb, T_new_state, sib->ns.i))
2909 goto nla_put_failure;
2911 case SIB_HELPER_POST:
2912 if (nla_put_u32(skb, T_helper_exit_code,
2913 sib->helper_exit_code))
2914 goto nla_put_failure;
2916 case SIB_HELPER_PRE:
2917 if (nla_put_string(skb, T_helper, sib->helper_name))
2918 goto nla_put_failure;
2922 nla_nest_end(skb, nla);
2932 int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
2934 enum drbd_ret_code retcode;
2937 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2938 if (!adm_ctx.reply_skb)
2940 if (retcode != NO_ERROR)
2943 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.device, NULL);
2945 nlmsg_free(adm_ctx.reply_skb);
2949 drbd_adm_finish(info, retcode);
2953 static int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
2955 struct drbd_device *device;
2956 struct drbd_genlmsghdr *dh;
2957 struct drbd_resource *pos = (struct drbd_resource *)cb->args[0];
2958 struct drbd_resource *resource = NULL;
2959 struct drbd_resource *tmp;
2960 unsigned volume = cb->args[1];
2962 /* Open coded, deferred, iteration:
2963 * for_each_resource_safe(resource, tmp, &drbd_resources) {
2964 * connection = "first connection of resource or undefined";
2965 * idr_for_each_entry(&resource->devices, device, i) {
2969 * where resource is cb->args[0];
2970 * and i is cb->args[1];
2972 * cb->args[2] indicates if we shall loop over all resources,
2973 * or just dump all volumes of a single resource.
2975 * This may miss entries inserted after this dump started,
2976 * or entries deleted before they are reached.
2978 * We need to make sure the device won't disappear while
2979 * we are looking at it, and revalidate our iterators
2980 * on each iteration.
2983 /* synchronize with conn_create()/drbd_destroy_connection() */
2985 /* revalidate iterator position */
2986 for_each_resource_rcu(tmp, &drbd_resources) {
2988 /* first iteration */
3000 device = idr_get_next(&resource->devices, &volume);
3002 /* No more volumes to dump on this resource.
3003 * Advance resource iterator. */
3004 pos = list_entry_rcu(resource->resources.next,
3005 struct drbd_resource, resources);
3006 /* Did we dump any volume of this resource yet? */
3008 /* If we reached the end of the list,
3009 * or only a single resource dump was requested,
3011 if (&pos->resources == &drbd_resources || cb->args[2])
3019 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3020 cb->nlh->nlmsg_seq, &drbd_genl_family,
3021 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
3026 /* This is a connection without a single volume.
3027 * Suprisingly enough, it may have a network
3029 struct drbd_connection *connection;
3032 dh->ret_code = NO_ERROR;
3033 connection = the_only_connection(resource);
3034 if (nla_put_drbd_cfg_context(skb, resource, connection, NULL))
3037 struct net_conf *nc;
3039 nc = rcu_dereference(connection->net_conf);
3040 if (nc && net_conf_to_skb(skb, nc, 1) != 0)
3046 D_ASSERT(device, device->vnr == volume);
3047 D_ASSERT(device, device->resource == resource);
3049 dh->minor = device_to_minor(device);
3050 dh->ret_code = NO_ERROR;
3052 if (nla_put_status_info(skb, device, NULL)) {
3054 genlmsg_cancel(skb, dh);
3058 genlmsg_end(skb, dh);
3063 /* where to start the next iteration */
3064 cb->args[0] = (long)pos;
3065 cb->args[1] = (pos == resource) ? volume + 1 : 0;
3067 /* No more resources/volumes/minors found results in an empty skb.
3068 * Which will terminate the dump. */
3073 * Request status of all resources, or of all volumes within a single resource.
3075 * This is a dump, as the answer may not fit in a single reply skb otherwise.
3076 * Which means we cannot use the family->attrbuf or other such members, because
3077 * dump is NOT protected by the genl_lock(). During dump, we only have access
3078 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
3080 * Once things are setup properly, we call into get_one_status().
3082 int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
3084 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
3086 const char *resource_name;
3087 struct drbd_resource *resource;
3090 /* Is this a followup call? */
3092 /* ... of a single resource dump,
3093 * and the resource iterator has been advanced already? */
3094 if (cb->args[2] && cb->args[2] != cb->args[0])
3095 return 0; /* DONE. */
3099 /* First call (from netlink_dump_start). We need to figure out
3100 * which resource(s) the user wants us to dump. */
3101 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
3102 nlmsg_attrlen(cb->nlh, hdrlen),
3103 DRBD_NLA_CFG_CONTEXT);
3105 /* No explicit context given. Dump all. */
3108 maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
3109 nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
3111 return PTR_ERR(nla);
3112 /* context given, but no name present? */
3115 resource_name = nla_data(nla);
3116 if (!*resource_name)
3118 resource = drbd_find_resource(resource_name);
3122 kref_put(&resource->kref, drbd_destroy_resource); /* get_one_status() revalidates the resource */
3124 /* prime iterators, and set "filter" mode mark:
3125 * only dump this connection. */
3126 cb->args[0] = (long)resource;
3127 /* cb->args[1] = 0; passed in this way. */
3128 cb->args[2] = (long)resource;
3131 return get_one_status(skb, cb);
3134 int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
3136 enum drbd_ret_code retcode;
3137 struct timeout_parms tp;
3140 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3141 if (!adm_ctx.reply_skb)
3143 if (retcode != NO_ERROR)
3147 adm_ctx.device->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
3148 test_bit(USE_DEGR_WFC_T, &adm_ctx.device->flags) ? UT_DEGRADED :
3151 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
3153 nlmsg_free(adm_ctx.reply_skb);
3157 drbd_adm_finish(info, retcode);
3161 int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
3163 struct drbd_device *device;
3164 enum drbd_ret_code retcode;
3165 struct start_ov_parms parms;
3167 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3168 if (!adm_ctx.reply_skb)
3170 if (retcode != NO_ERROR)
3173 device = adm_ctx.device;
3175 /* resume from last known position, if possible */
3176 parms.ov_start_sector = device->ov_start_sector;
3177 parms.ov_stop_sector = ULLONG_MAX;
3178 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
3179 int err = start_ov_parms_from_attrs(&parms, info);
3181 retcode = ERR_MANDATORY_TAG;
3182 drbd_msg_put_info(from_attrs_err_to_txt(err));
3186 /* w_make_ov_request expects position to be aligned */
3187 device->ov_start_sector = parms.ov_start_sector & ~(BM_SECT_PER_BIT-1);
3188 device->ov_stop_sector = parms.ov_stop_sector;
3190 /* If there is still bitmap IO pending, e.g. previous resync or verify
3191 * just being finished, wait for it before requesting a new resync. */
3192 drbd_suspend_io(device);
3193 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
3194 retcode = drbd_request_state(device, NS(conn, C_VERIFY_S));
3195 drbd_resume_io(device);
3197 drbd_adm_finish(info, retcode);
3202 int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
3204 struct drbd_device *device;
3205 enum drbd_ret_code retcode;
3206 int skip_initial_sync = 0;
3208 struct new_c_uuid_parms args;
3210 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3211 if (!adm_ctx.reply_skb)
3213 if (retcode != NO_ERROR)
3216 device = adm_ctx.device;
3217 memset(&args, 0, sizeof(args));
3218 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
3219 err = new_c_uuid_parms_from_attrs(&args, info);
3221 retcode = ERR_MANDATORY_TAG;
3222 drbd_msg_put_info(from_attrs_err_to_txt(err));
3227 mutex_lock(device->state_mutex); /* Protects us against serialized state changes. */
3229 if (!get_ldev(device)) {
3230 retcode = ERR_NO_DISK;
3234 /* this is "skip initial sync", assume to be clean */
3235 if (device->state.conn == C_CONNECTED &&
3236 first_peer_device(device)->connection->agreed_pro_version >= 90 &&
3237 device->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
3238 drbd_info(device, "Preparing to skip initial sync\n");
3239 skip_initial_sync = 1;
3240 } else if (device->state.conn != C_STANDALONE) {
3241 retcode = ERR_CONNECTED;
3245 drbd_uuid_set(device, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
3246 drbd_uuid_new_current(device); /* New current, previous to UI_BITMAP */
3248 if (args.clear_bm) {
3249 err = drbd_bitmap_io(device, &drbd_bmio_clear_n_write,
3250 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
3252 drbd_err(device, "Writing bitmap failed with %d\n", err);
3253 retcode = ERR_IO_MD_DISK;
3255 if (skip_initial_sync) {
3256 drbd_send_uuids_skip_initial_sync(first_peer_device(device));
3257 _drbd_uuid_set(device, UI_BITMAP, 0);
3258 drbd_print_uuids(device, "cleared bitmap UUID");
3259 spin_lock_irq(&device->resource->req_lock);
3260 _drbd_set_state(_NS2(device, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3262 spin_unlock_irq(&device->resource->req_lock);
3266 drbd_md_sync(device);
3270 mutex_unlock(device->state_mutex);
3272 drbd_adm_finish(info, retcode);
3276 static enum drbd_ret_code
3277 drbd_check_resource_name(const char *name)
3279 if (!name || !name[0]) {
3280 drbd_msg_put_info("resource name missing");
3281 return ERR_MANDATORY_TAG;
3283 /* if we want to use these in sysfs/configfs/debugfs some day,
3284 * we must not allow slashes */
3285 if (strchr(name, '/')) {
3286 drbd_msg_put_info("invalid resource name");
3287 return ERR_INVALID_REQUEST;
3292 int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
3294 enum drbd_ret_code retcode;
3295 struct res_opts res_opts;
3298 retcode = drbd_adm_prepare(skb, info, 0);
3299 if (!adm_ctx.reply_skb)
3301 if (retcode != NO_ERROR)
3304 set_res_opts_defaults(&res_opts);
3305 err = res_opts_from_attrs(&res_opts, info);
3306 if (err && err != -ENOMSG) {
3307 retcode = ERR_MANDATORY_TAG;
3308 drbd_msg_put_info(from_attrs_err_to_txt(err));
3312 retcode = drbd_check_resource_name(adm_ctx.resource_name);
3313 if (retcode != NO_ERROR)
3316 if (adm_ctx.resource) {
3317 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
3318 retcode = ERR_INVALID_REQUEST;
3319 drbd_msg_put_info("resource exists");
3321 /* else: still NO_ERROR */
3325 if (!conn_create(adm_ctx.resource_name, &res_opts))
3326 retcode = ERR_NOMEM;
3328 drbd_adm_finish(info, retcode);
3332 int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info)
3334 struct drbd_genlmsghdr *dh = info->userhdr;
3335 enum drbd_ret_code retcode;
3337 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
3338 if (!adm_ctx.reply_skb)
3340 if (retcode != NO_ERROR)
3343 if (dh->minor > MINORMASK) {
3344 drbd_msg_put_info("requested minor out of range");
3345 retcode = ERR_INVALID_REQUEST;
3348 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
3349 drbd_msg_put_info("requested volume id out of range");
3350 retcode = ERR_INVALID_REQUEST;
3354 /* drbd_adm_prepare made sure already
3355 * that first_peer_device(device)->connection and device->vnr match the request. */
3356 if (adm_ctx.device) {
3357 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
3358 retcode = ERR_MINOR_EXISTS;
3359 /* else: still NO_ERROR */
3363 retcode = drbd_create_device(adm_ctx.resource, dh->minor, adm_ctx.volume);
3365 drbd_adm_finish(info, retcode);
3369 static enum drbd_ret_code adm_del_minor(struct drbd_device *device)
3371 if (device->state.disk == D_DISKLESS &&
3372 /* no need to be device->state.conn == C_STANDALONE &&
3373 * we may want to delete a minor from a live replication group.
3375 device->state.role == R_SECONDARY) {
3376 _drbd_request_state(device, NS(conn, C_WF_REPORT_PARAMS),
3377 CS_VERBOSE + CS_WAIT_COMPLETE);
3378 drbd_delete_device(device);
3381 return ERR_MINOR_CONFIGURED;
3384 int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info)
3386 enum drbd_ret_code retcode;
3388 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3389 if (!adm_ctx.reply_skb)
3391 if (retcode != NO_ERROR)
3394 retcode = adm_del_minor(adm_ctx.device);
3396 drbd_adm_finish(info, retcode);
3400 int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
3402 struct drbd_resource *resource;
3403 struct drbd_connection *connection;
3404 struct drbd_device *device;
3405 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
3408 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
3409 if (!adm_ctx.reply_skb)
3411 if (retcode != NO_ERROR)
3414 resource = adm_ctx.resource;
3416 for_each_connection(connection, resource) {
3417 struct drbd_peer_device *peer_device;
3419 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
3420 retcode = drbd_set_role(peer_device->device, R_SECONDARY, 0);
3421 if (retcode < SS_SUCCESS) {
3422 drbd_msg_put_info("failed to demote");
3427 retcode = conn_try_disconnect(connection, 0);
3428 if (retcode < SS_SUCCESS) {
3429 drbd_msg_put_info("failed to disconnect");
3435 idr_for_each_entry(&resource->devices, device, i) {
3436 retcode = adm_detach(device, 0);
3437 if (retcode < SS_SUCCESS || retcode > NO_ERROR) {
3438 drbd_msg_put_info("failed to detach");
3443 /* If we reach this, all volumes (of this connection) are Secondary,
3444 * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
3445 * actually stopped, state handling only does drbd_thread_stop_nowait(). */
3446 for_each_connection(connection, resource)
3447 drbd_thread_stop(&connection->worker);
3449 /* Now, nothing can fail anymore */
3451 /* delete volumes */
3452 idr_for_each_entry(&resource->devices, device, i) {
3453 retcode = adm_del_minor(device);
3454 if (retcode != NO_ERROR) {
3455 /* "can not happen" */
3456 drbd_msg_put_info("failed to delete volume");
3461 list_del_rcu(&resource->resources);
3463 drbd_free_resource(resource);
3467 drbd_adm_finish(info, retcode);
3471 int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
3473 struct drbd_resource *resource;
3474 struct drbd_connection *connection;
3475 enum drbd_ret_code retcode;
3477 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
3478 if (!adm_ctx.reply_skb)
3480 if (retcode != NO_ERROR)
3483 resource = adm_ctx.resource;
3484 for_each_connection(connection, resource) {
3485 if (connection->cstate > C_STANDALONE) {
3486 retcode = ERR_NET_CONFIGURED;
3490 if (!idr_is_empty(&resource->devices)) {
3491 retcode = ERR_RES_IN_USE;
3495 list_del_rcu(&resource->resources);
3496 for_each_connection(connection, resource)
3497 drbd_thread_stop(&connection->worker);
3499 drbd_free_resource(resource);
3502 drbd_adm_finish(info, retcode);
3506 void drbd_bcast_event(struct drbd_device *device, const struct sib_info *sib)
3508 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
3509 struct sk_buff *msg;
3510 struct drbd_genlmsghdr *d_out;
3514 if (sib->sib_reason == SIB_SYNC_PROGRESS) {
3515 if (time_after(jiffies, device->rs_last_bcast + HZ))
3516 device->rs_last_bcast = jiffies;
3521 seq = atomic_inc_return(&drbd_genl_seq);
3522 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
3527 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
3528 if (!d_out) /* cannot happen, but anyways. */
3529 goto nla_put_failure;
3530 d_out->minor = device_to_minor(device);
3531 d_out->ret_code = NO_ERROR;
3533 if (nla_put_status_info(msg, device, sib))
3534 goto nla_put_failure;
3535 genlmsg_end(msg, d_out);
3536 err = drbd_genl_multicast_events(msg, 0);
3537 /* msg has been consumed or freed in netlink_broadcast() */
3538 if (err && err != -ESRCH)
3546 drbd_err(device, "Error %d while broadcasting event. "
3547 "Event seq:%u sib_reason:%u\n",
3548 err, seq, sib->sib_reason);