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(device
);
682 drbd_send_current_state(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 conn_flush_workqueue(connection
);
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 drbd_send_sync_param(device
);
1371 kfree(old_disk_conf
);
1373 mod_timer(&device
->request_timer
, jiffies
+ HZ
);
1377 mutex_unlock(&device
->resource
->conf_update
);
1379 kfree(new_disk_conf
);
1384 drbd_adm_finish(info
, retcode
);
1388 int drbd_adm_attach(struct sk_buff
*skb
, struct genl_info
*info
)
1390 struct drbd_device
*device
;
1392 enum drbd_ret_code retcode
;
1393 enum determine_dev_size dd
;
1394 sector_t max_possible_sectors
;
1395 sector_t min_md_device_sectors
;
1396 struct drbd_backing_dev
*nbc
= NULL
; /* new_backing_conf */
1397 struct disk_conf
*new_disk_conf
= NULL
;
1398 struct block_device
*bdev
;
1399 struct lru_cache
*resync_lru
= NULL
;
1400 struct fifo_buffer
*new_plan
= NULL
;
1401 union drbd_state ns
, os
;
1402 enum drbd_state_rv rv
;
1403 struct net_conf
*nc
;
1405 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
1406 if (!adm_ctx
.reply_skb
)
1408 if (retcode
!= NO_ERROR
)
1411 device
= adm_ctx
.device
;
1412 conn_reconfig_start(first_peer_device(device
)->connection
);
1414 /* if you want to reconfigure, please tear down first */
1415 if (device
->state
.disk
> D_DISKLESS
) {
1416 retcode
= ERR_DISK_CONFIGURED
;
1419 /* It may just now have detached because of IO error. Make sure
1420 * drbd_ldev_destroy is done already, we may end up here very fast,
1421 * e.g. if someone calls attach from the on-io-error handler,
1422 * to realize a "hot spare" feature (not that I'd recommend that) */
1423 wait_event(device
->misc_wait
, !atomic_read(&device
->local_cnt
));
1425 /* make sure there is no leftover from previous force-detach attempts */
1426 clear_bit(FORCE_DETACH
, &device
->flags
);
1427 clear_bit(WAS_IO_ERROR
, &device
->flags
);
1428 clear_bit(WAS_READ_ERROR
, &device
->flags
);
1430 /* and no leftover from previously aborted resync or verify, either */
1431 device
->rs_total
= 0;
1432 device
->rs_failed
= 0;
1433 atomic_set(&device
->rs_pending_cnt
, 0);
1435 /* allocation not in the IO path, drbdsetup context */
1436 nbc
= kzalloc(sizeof(struct drbd_backing_dev
), GFP_KERNEL
);
1438 retcode
= ERR_NOMEM
;
1441 spin_lock_init(&nbc
->md
.uuid_lock
);
1443 new_disk_conf
= kzalloc(sizeof(struct disk_conf
), GFP_KERNEL
);
1444 if (!new_disk_conf
) {
1445 retcode
= ERR_NOMEM
;
1448 nbc
->disk_conf
= new_disk_conf
;
1450 set_disk_conf_defaults(new_disk_conf
);
1451 err
= disk_conf_from_attrs(new_disk_conf
, info
);
1453 retcode
= ERR_MANDATORY_TAG
;
1454 drbd_msg_put_info(from_attrs_err_to_txt(err
));
1458 if (new_disk_conf
->c_plan_ahead
> DRBD_C_PLAN_AHEAD_MAX
)
1459 new_disk_conf
->c_plan_ahead
= DRBD_C_PLAN_AHEAD_MAX
;
1461 new_plan
= fifo_alloc((new_disk_conf
->c_plan_ahead
* 10 * SLEEP_TIME
) / HZ
);
1463 retcode
= ERR_NOMEM
;
1467 if (new_disk_conf
->meta_dev_idx
< DRBD_MD_INDEX_FLEX_INT
) {
1468 retcode
= ERR_MD_IDX_INVALID
;
1472 write_lock_irq(&global_state_lock
);
1473 retcode
= drbd_resync_after_valid(device
, new_disk_conf
->resync_after
);
1474 write_unlock_irq(&global_state_lock
);
1475 if (retcode
!= NO_ERROR
)
1479 nc
= rcu_dereference(first_peer_device(device
)->connection
->net_conf
);
1481 if (new_disk_conf
->fencing
== FP_STONITH
&& nc
->wire_protocol
== DRBD_PROT_A
) {
1483 retcode
= ERR_STONITH_AND_PROT_A
;
1489 bdev
= blkdev_get_by_path(new_disk_conf
->backing_dev
,
1490 FMODE_READ
| FMODE_WRITE
| FMODE_EXCL
, device
);
1492 drbd_err(device
, "open(\"%s\") failed with %ld\n", new_disk_conf
->backing_dev
,
1494 retcode
= ERR_OPEN_DISK
;
1497 nbc
->backing_bdev
= bdev
;
1500 * meta_dev_idx >= 0: external fixed size, possibly multiple
1501 * drbd sharing one meta device. TODO in that case, paranoia
1502 * check that [md_bdev, meta_dev_idx] is not yet used by some
1503 * other drbd minor! (if you use drbd.conf + drbdadm, that
1504 * should check it for you already; but if you don't, or
1505 * someone fooled it, we need to double check here)
1507 bdev
= blkdev_get_by_path(new_disk_conf
->meta_dev
,
1508 FMODE_READ
| FMODE_WRITE
| FMODE_EXCL
,
1509 (new_disk_conf
->meta_dev_idx
< 0) ?
1510 (void *)device
: (void *)drbd_m_holder
);
1512 drbd_err(device
, "open(\"%s\") failed with %ld\n", new_disk_conf
->meta_dev
,
1514 retcode
= ERR_OPEN_MD_DISK
;
1517 nbc
->md_bdev
= bdev
;
1519 if ((nbc
->backing_bdev
== nbc
->md_bdev
) !=
1520 (new_disk_conf
->meta_dev_idx
== DRBD_MD_INDEX_INTERNAL
||
1521 new_disk_conf
->meta_dev_idx
== DRBD_MD_INDEX_FLEX_INT
)) {
1522 retcode
= ERR_MD_IDX_INVALID
;
1526 resync_lru
= lc_create("resync", drbd_bm_ext_cache
,
1527 1, 61, sizeof(struct bm_extent
),
1528 offsetof(struct bm_extent
, lce
));
1530 retcode
= ERR_NOMEM
;
1534 /* Read our meta data super block early.
1535 * This also sets other on-disk offsets. */
1536 retcode
= drbd_md_read(device
, nbc
);
1537 if (retcode
!= NO_ERROR
)
1540 if (new_disk_conf
->al_extents
< DRBD_AL_EXTENTS_MIN
)
1541 new_disk_conf
->al_extents
= DRBD_AL_EXTENTS_MIN
;
1542 if (new_disk_conf
->al_extents
> drbd_al_extents_max(nbc
))
1543 new_disk_conf
->al_extents
= drbd_al_extents_max(nbc
);
1545 if (drbd_get_max_capacity(nbc
) < new_disk_conf
->disk_size
) {
1546 drbd_err(device
, "max capacity %llu smaller than disk size %llu\n",
1547 (unsigned long long) drbd_get_max_capacity(nbc
),
1548 (unsigned long long) new_disk_conf
->disk_size
);
1549 retcode
= ERR_DISK_TOO_SMALL
;
1553 if (new_disk_conf
->meta_dev_idx
< 0) {
1554 max_possible_sectors
= DRBD_MAX_SECTORS_FLEX
;
1555 /* at least one MB, otherwise it does not make sense */
1556 min_md_device_sectors
= (2<<10);
1558 max_possible_sectors
= DRBD_MAX_SECTORS
;
1559 min_md_device_sectors
= MD_128MB_SECT
* (new_disk_conf
->meta_dev_idx
+ 1);
1562 if (drbd_get_capacity(nbc
->md_bdev
) < min_md_device_sectors
) {
1563 retcode
= ERR_MD_DISK_TOO_SMALL
;
1564 drbd_warn(device
, "refusing attach: md-device too small, "
1565 "at least %llu sectors needed for this meta-disk type\n",
1566 (unsigned long long) min_md_device_sectors
);
1570 /* Make sure the new disk is big enough
1571 * (we may currently be R_PRIMARY with no local disk...) */
1572 if (drbd_get_max_capacity(nbc
) <
1573 drbd_get_capacity(device
->this_bdev
)) {
1574 retcode
= ERR_DISK_TOO_SMALL
;
1578 nbc
->known_size
= drbd_get_capacity(nbc
->backing_bdev
);
1580 if (nbc
->known_size
> max_possible_sectors
) {
1581 drbd_warn(device
, "==> truncating very big lower level device "
1582 "to currently maximum possible %llu sectors <==\n",
1583 (unsigned long long) max_possible_sectors
);
1584 if (new_disk_conf
->meta_dev_idx
>= 0)
1585 drbd_warn(device
, "==>> using internal or flexible "
1586 "meta data may help <<==\n");
1589 drbd_suspend_io(device
);
1590 /* also wait for the last barrier ack. */
1591 /* FIXME see also https://daiquiri.linbit/cgi-bin/bugzilla/show_bug.cgi?id=171
1592 * We need a way to either ignore barrier acks for barriers sent before a device
1593 * was attached, or a way to wait for all pending barrier acks to come in.
1594 * As barriers are counted per resource,
1595 * we'd need to suspend io on all devices of a resource.
1597 wait_event(device
->misc_wait
, !atomic_read(&device
->ap_pending_cnt
) || drbd_suspended(device
));
1598 /* and for any other previously queued work */
1599 drbd_flush_workqueue(device
);
1601 rv
= _drbd_request_state(device
, NS(disk
, D_ATTACHING
), CS_VERBOSE
);
1602 retcode
= rv
; /* FIXME: Type mismatch. */
1603 drbd_resume_io(device
);
1604 if (rv
< SS_SUCCESS
)
1607 if (!get_ldev_if_state(device
, D_ATTACHING
))
1608 goto force_diskless
;
1610 if (!device
->bitmap
) {
1611 if (drbd_bm_init(device
)) {
1612 retcode
= ERR_NOMEM
;
1613 goto force_diskless_dec
;
1617 if (device
->state
.conn
< C_CONNECTED
&&
1618 device
->state
.role
== R_PRIMARY
&&
1619 (device
->ed_uuid
& ~((u64
)1)) != (nbc
->md
.uuid
[UI_CURRENT
] & ~((u64
)1))) {
1620 drbd_err(device
, "Can only attach to data with current UUID=%016llX\n",
1621 (unsigned long long)device
->ed_uuid
);
1622 retcode
= ERR_DATA_NOT_CURRENT
;
1623 goto force_diskless_dec
;
1626 /* Since we are diskless, fix the activity log first... */
1627 if (drbd_check_al_size(device
, new_disk_conf
)) {
1628 retcode
= ERR_NOMEM
;
1629 goto force_diskless_dec
;
1632 /* Prevent shrinking of consistent devices ! */
1633 if (drbd_md_test_flag(nbc
, MDF_CONSISTENT
) &&
1634 drbd_new_dev_size(device
, nbc
, nbc
->disk_conf
->disk_size
, 0) < nbc
->md
.la_size_sect
) {
1635 drbd_warn(device
, "refusing to truncate a consistent device\n");
1636 retcode
= ERR_DISK_TOO_SMALL
;
1637 goto force_diskless_dec
;
1640 /* Reset the "barriers don't work" bits here, then force meta data to
1641 * be written, to ensure we determine if barriers are supported. */
1642 if (new_disk_conf
->md_flushes
)
1643 clear_bit(MD_NO_FUA
, &device
->flags
);
1645 set_bit(MD_NO_FUA
, &device
->flags
);
1647 /* Point of no return reached.
1648 * Devices and memory are no longer released by error cleanup below.
1649 * now device takes over responsibility, and the state engine should
1650 * clean it up somewhere. */
1651 D_ASSERT(device
, device
->ldev
== NULL
);
1653 device
->resync
= resync_lru
;
1654 device
->rs_plan_s
= new_plan
;
1657 new_disk_conf
= NULL
;
1660 drbd_bump_write_ordering(first_peer_device(device
)->connection
, WO_bdev_flush
);
1662 if (drbd_md_test_flag(device
->ldev
, MDF_CRASHED_PRIMARY
))
1663 set_bit(CRASHED_PRIMARY
, &device
->flags
);
1665 clear_bit(CRASHED_PRIMARY
, &device
->flags
);
1667 if (drbd_md_test_flag(device
->ldev
, MDF_PRIMARY_IND
) &&
1668 !(device
->state
.role
== R_PRIMARY
&& device
->resource
->susp_nod
))
1669 set_bit(CRASHED_PRIMARY
, &device
->flags
);
1671 device
->send_cnt
= 0;
1672 device
->recv_cnt
= 0;
1673 device
->read_cnt
= 0;
1674 device
->writ_cnt
= 0;
1676 drbd_reconsider_max_bio_size(device
);
1678 /* If I am currently not R_PRIMARY,
1679 * but meta data primary indicator is set,
1680 * I just now recover from a hard crash,
1681 * and have been R_PRIMARY before that crash.
1683 * Now, if I had no connection before that crash
1684 * (have been degraded R_PRIMARY), chances are that
1685 * I won't find my peer now either.
1687 * In that case, and _only_ in that case,
1688 * we use the degr-wfc-timeout instead of the default,
1689 * so we can automatically recover from a crash of a
1690 * degraded but active "cluster" after a certain timeout.
1692 clear_bit(USE_DEGR_WFC_T
, &device
->flags
);
1693 if (device
->state
.role
!= R_PRIMARY
&&
1694 drbd_md_test_flag(device
->ldev
, MDF_PRIMARY_IND
) &&
1695 !drbd_md_test_flag(device
->ldev
, MDF_CONNECTED_IND
))
1696 set_bit(USE_DEGR_WFC_T
, &device
->flags
);
1698 dd
= drbd_determine_dev_size(device
, 0, NULL
);
1699 if (dd
<= DS_ERROR
) {
1700 retcode
= ERR_NOMEM_BITMAP
;
1701 goto force_diskless_dec
;
1702 } else if (dd
== DS_GREW
)
1703 set_bit(RESYNC_AFTER_NEG
, &device
->flags
);
1705 if (drbd_md_test_flag(device
->ldev
, MDF_FULL_SYNC
) ||
1706 (test_bit(CRASHED_PRIMARY
, &device
->flags
) &&
1707 drbd_md_test_flag(device
->ldev
, MDF_AL_DISABLED
))) {
1708 drbd_info(device
, "Assuming that all blocks are out of sync "
1709 "(aka FullSync)\n");
1710 if (drbd_bitmap_io(device
, &drbd_bmio_set_n_write
,
1711 "set_n_write from attaching", BM_LOCKED_MASK
)) {
1712 retcode
= ERR_IO_MD_DISK
;
1713 goto force_diskless_dec
;
1716 if (drbd_bitmap_io(device
, &drbd_bm_read
,
1717 "read from attaching", BM_LOCKED_MASK
)) {
1718 retcode
= ERR_IO_MD_DISK
;
1719 goto force_diskless_dec
;
1723 if (_drbd_bm_total_weight(device
) == drbd_bm_bits(device
))
1724 drbd_suspend_al(device
); /* IO is still suspended here... */
1726 spin_lock_irq(&device
->resource
->req_lock
);
1727 os
= drbd_read_state(device
);
1729 /* If MDF_CONSISTENT is not set go into inconsistent state,
1730 otherwise investigate MDF_WasUpToDate...
1731 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1732 otherwise into D_CONSISTENT state.
1734 if (drbd_md_test_flag(device
->ldev
, MDF_CONSISTENT
)) {
1735 if (drbd_md_test_flag(device
->ldev
, MDF_WAS_UP_TO_DATE
))
1736 ns
.disk
= D_CONSISTENT
;
1738 ns
.disk
= D_OUTDATED
;
1740 ns
.disk
= D_INCONSISTENT
;
1743 if (drbd_md_test_flag(device
->ldev
, MDF_PEER_OUT_DATED
))
1744 ns
.pdsk
= D_OUTDATED
;
1747 if (ns
.disk
== D_CONSISTENT
&&
1748 (ns
.pdsk
== D_OUTDATED
|| rcu_dereference(device
->ldev
->disk_conf
)->fencing
== FP_DONT_CARE
))
1749 ns
.disk
= D_UP_TO_DATE
;
1751 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1752 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1753 this point, because drbd_request_state() modifies these
1756 if (rcu_dereference(device
->ldev
->disk_conf
)->al_updates
)
1757 device
->ldev
->md
.flags
&= ~MDF_AL_DISABLED
;
1759 device
->ldev
->md
.flags
|= MDF_AL_DISABLED
;
1763 /* In case we are C_CONNECTED postpone any decision on the new disk
1764 state after the negotiation phase. */
1765 if (device
->state
.conn
== C_CONNECTED
) {
1766 device
->new_state_tmp
.i
= ns
.i
;
1768 ns
.disk
= D_NEGOTIATING
;
1770 /* We expect to receive up-to-date UUIDs soon.
1771 To avoid a race in receive_state, free p_uuid while
1772 holding req_lock. I.e. atomic with the state change */
1773 kfree(device
->p_uuid
);
1774 device
->p_uuid
= NULL
;
1777 rv
= _drbd_set_state(device
, ns
, CS_VERBOSE
, NULL
);
1778 spin_unlock_irq(&device
->resource
->req_lock
);
1780 if (rv
< SS_SUCCESS
)
1781 goto force_diskless_dec
;
1783 mod_timer(&device
->request_timer
, jiffies
+ HZ
);
1785 if (device
->state
.role
== R_PRIMARY
)
1786 device
->ldev
->md
.uuid
[UI_CURRENT
] |= (u64
)1;
1788 device
->ldev
->md
.uuid
[UI_CURRENT
] &= ~(u64
)1;
1790 drbd_md_mark_dirty(device
);
1791 drbd_md_sync(device
);
1793 kobject_uevent(&disk_to_dev(device
->vdisk
)->kobj
, KOBJ_CHANGE
);
1795 conn_reconfig_done(first_peer_device(device
)->connection
);
1796 drbd_adm_finish(info
, retcode
);
1802 drbd_force_state(device
, NS(disk
, D_DISKLESS
));
1803 drbd_md_sync(device
);
1805 conn_reconfig_done(first_peer_device(device
)->connection
);
1807 if (nbc
->backing_bdev
)
1808 blkdev_put(nbc
->backing_bdev
,
1809 FMODE_READ
| FMODE_WRITE
| FMODE_EXCL
);
1811 blkdev_put(nbc
->md_bdev
,
1812 FMODE_READ
| FMODE_WRITE
| FMODE_EXCL
);
1815 kfree(new_disk_conf
);
1816 lc_destroy(resync_lru
);
1820 drbd_adm_finish(info
, retcode
);
1824 static int adm_detach(struct drbd_device
*device
, int force
)
1826 enum drbd_state_rv retcode
;
1830 set_bit(FORCE_DETACH
, &device
->flags
);
1831 drbd_force_state(device
, NS(disk
, D_FAILED
));
1832 retcode
= SS_SUCCESS
;
1836 drbd_suspend_io(device
); /* so no-one is stuck in drbd_al_begin_io */
1837 drbd_md_get_buffer(device
); /* make sure there is no in-flight meta-data IO */
1838 retcode
= drbd_request_state(device
, NS(disk
, D_FAILED
));
1839 drbd_md_put_buffer(device
);
1840 /* D_FAILED will transition to DISKLESS. */
1841 ret
= wait_event_interruptible(device
->misc_wait
,
1842 device
->state
.disk
!= D_FAILED
);
1843 drbd_resume_io(device
);
1844 if ((int)retcode
== (int)SS_IS_DISKLESS
)
1845 retcode
= SS_NOTHING_TO_DO
;
1852 /* Detaching the disk is a process in multiple stages. First we need to lock
1853 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1854 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1855 * internal references as well.
1856 * Only then we have finally detached. */
1857 int drbd_adm_detach(struct sk_buff
*skb
, struct genl_info
*info
)
1859 enum drbd_ret_code retcode
;
1860 struct detach_parms parms
= { };
1863 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
1864 if (!adm_ctx
.reply_skb
)
1866 if (retcode
!= NO_ERROR
)
1869 if (info
->attrs
[DRBD_NLA_DETACH_PARMS
]) {
1870 err
= detach_parms_from_attrs(&parms
, info
);
1872 retcode
= ERR_MANDATORY_TAG
;
1873 drbd_msg_put_info(from_attrs_err_to_txt(err
));
1878 retcode
= adm_detach(adm_ctx
.device
, parms
.force_detach
);
1880 drbd_adm_finish(info
, retcode
);
1884 static bool conn_resync_running(struct drbd_connection
*connection
)
1886 struct drbd_peer_device
*peer_device
;
1891 idr_for_each_entry(&connection
->peer_devices
, peer_device
, vnr
) {
1892 struct drbd_device
*device
= peer_device
->device
;
1893 if (device
->state
.conn
== C_SYNC_SOURCE
||
1894 device
->state
.conn
== C_SYNC_TARGET
||
1895 device
->state
.conn
== C_PAUSED_SYNC_S
||
1896 device
->state
.conn
== C_PAUSED_SYNC_T
) {
1906 static bool conn_ov_running(struct drbd_connection
*connection
)
1908 struct drbd_peer_device
*peer_device
;
1913 idr_for_each_entry(&connection
->peer_devices
, peer_device
, vnr
) {
1914 struct drbd_device
*device
= peer_device
->device
;
1915 if (device
->state
.conn
== C_VERIFY_S
||
1916 device
->state
.conn
== C_VERIFY_T
) {
1926 static enum drbd_ret_code
1927 _check_net_options(struct drbd_connection
*connection
, struct net_conf
*old_net_conf
, struct net_conf
*new_net_conf
)
1929 struct drbd_peer_device
*peer_device
;
1932 if (old_net_conf
&& connection
->cstate
== C_WF_REPORT_PARAMS
&& connection
->agreed_pro_version
< 100) {
1933 if (new_net_conf
->wire_protocol
!= old_net_conf
->wire_protocol
)
1934 return ERR_NEED_APV_100
;
1936 if (new_net_conf
->two_primaries
!= old_net_conf
->two_primaries
)
1937 return ERR_NEED_APV_100
;
1939 if (strcmp(new_net_conf
->integrity_alg
, old_net_conf
->integrity_alg
))
1940 return ERR_NEED_APV_100
;
1943 if (!new_net_conf
->two_primaries
&&
1944 conn_highest_role(connection
) == R_PRIMARY
&&
1945 conn_highest_peer(connection
) == R_PRIMARY
)
1946 return ERR_NEED_ALLOW_TWO_PRI
;
1948 if (new_net_conf
->two_primaries
&&
1949 (new_net_conf
->wire_protocol
!= DRBD_PROT_C
))
1950 return ERR_NOT_PROTO_C
;
1952 idr_for_each_entry(&connection
->peer_devices
, peer_device
, i
) {
1953 struct drbd_device
*device
= peer_device
->device
;
1954 if (get_ldev(device
)) {
1955 enum drbd_fencing_p fp
= rcu_dereference(device
->ldev
->disk_conf
)->fencing
;
1957 if (new_net_conf
->wire_protocol
== DRBD_PROT_A
&& fp
== FP_STONITH
)
1958 return ERR_STONITH_AND_PROT_A
;
1960 if (device
->state
.role
== R_PRIMARY
&& new_net_conf
->discard_my_data
)
1961 return ERR_DISCARD_IMPOSSIBLE
;
1964 if (new_net_conf
->on_congestion
!= OC_BLOCK
&& new_net_conf
->wire_protocol
!= DRBD_PROT_A
)
1965 return ERR_CONG_NOT_PROTO_A
;
1970 static enum drbd_ret_code
1971 check_net_options(struct drbd_connection
*connection
, struct net_conf
*new_net_conf
)
1973 static enum drbd_ret_code rv
;
1974 struct drbd_peer_device
*peer_device
;
1978 rv
= _check_net_options(connection
, rcu_dereference(connection
->net_conf
), new_net_conf
);
1981 /* connection->volumes protected by genl_lock() here */
1982 idr_for_each_entry(&connection
->peer_devices
, peer_device
, i
) {
1983 struct drbd_device
*device
= peer_device
->device
;
1984 if (!device
->bitmap
) {
1985 if (drbd_bm_init(device
))
1994 struct crypto_hash
*verify_tfm
;
1995 struct crypto_hash
*csums_tfm
;
1996 struct crypto_hash
*cram_hmac_tfm
;
1997 struct crypto_hash
*integrity_tfm
;
2001 alloc_hash(struct crypto_hash
**tfm
, char *tfm_name
, int err_alg
)
2006 *tfm
= crypto_alloc_hash(tfm_name
, 0, CRYPTO_ALG_ASYNC
);
2015 static enum drbd_ret_code
2016 alloc_crypto(struct crypto
*crypto
, struct net_conf
*new_net_conf
)
2018 char hmac_name
[CRYPTO_MAX_ALG_NAME
];
2019 enum drbd_ret_code rv
;
2021 rv
= alloc_hash(&crypto
->csums_tfm
, new_net_conf
->csums_alg
,
2025 rv
= alloc_hash(&crypto
->verify_tfm
, new_net_conf
->verify_alg
,
2029 rv
= alloc_hash(&crypto
->integrity_tfm
, new_net_conf
->integrity_alg
,
2033 if (new_net_conf
->cram_hmac_alg
[0] != 0) {
2034 snprintf(hmac_name
, CRYPTO_MAX_ALG_NAME
, "hmac(%s)",
2035 new_net_conf
->cram_hmac_alg
);
2037 rv
= alloc_hash(&crypto
->cram_hmac_tfm
, hmac_name
,
2044 static void free_crypto(struct crypto
*crypto
)
2046 crypto_free_hash(crypto
->cram_hmac_tfm
);
2047 crypto_free_hash(crypto
->integrity_tfm
);
2048 crypto_free_hash(crypto
->csums_tfm
);
2049 crypto_free_hash(crypto
->verify_tfm
);
2052 int drbd_adm_net_opts(struct sk_buff
*skb
, struct genl_info
*info
)
2054 enum drbd_ret_code retcode
;
2055 struct drbd_connection
*connection
;
2056 struct net_conf
*old_net_conf
, *new_net_conf
= NULL
;
2058 int ovr
; /* online verify running */
2059 int rsr
; /* re-sync running */
2060 struct crypto crypto
= { };
2062 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_CONNECTION
);
2063 if (!adm_ctx
.reply_skb
)
2065 if (retcode
!= NO_ERROR
)
2068 connection
= adm_ctx
.connection
;
2070 new_net_conf
= kzalloc(sizeof(struct net_conf
), GFP_KERNEL
);
2071 if (!new_net_conf
) {
2072 retcode
= ERR_NOMEM
;
2076 conn_reconfig_start(connection
);
2078 mutex_lock(&connection
->data
.mutex
);
2079 mutex_lock(&connection
->resource
->conf_update
);
2080 old_net_conf
= connection
->net_conf
;
2082 if (!old_net_conf
) {
2083 drbd_msg_put_info("net conf missing, try connect");
2084 retcode
= ERR_INVALID_REQUEST
;
2088 *new_net_conf
= *old_net_conf
;
2089 if (should_set_defaults(info
))
2090 set_net_conf_defaults(new_net_conf
);
2092 err
= net_conf_from_attrs_for_change(new_net_conf
, info
);
2093 if (err
&& err
!= -ENOMSG
) {
2094 retcode
= ERR_MANDATORY_TAG
;
2095 drbd_msg_put_info(from_attrs_err_to_txt(err
));
2099 retcode
= check_net_options(connection
, new_net_conf
);
2100 if (retcode
!= NO_ERROR
)
2103 /* re-sync running */
2104 rsr
= conn_resync_running(connection
);
2105 if (rsr
&& strcmp(new_net_conf
->csums_alg
, old_net_conf
->csums_alg
)) {
2106 retcode
= ERR_CSUMS_RESYNC_RUNNING
;
2110 /* online verify running */
2111 ovr
= conn_ov_running(connection
);
2112 if (ovr
&& strcmp(new_net_conf
->verify_alg
, old_net_conf
->verify_alg
)) {
2113 retcode
= ERR_VERIFY_RUNNING
;
2117 retcode
= alloc_crypto(&crypto
, new_net_conf
);
2118 if (retcode
!= NO_ERROR
)
2121 rcu_assign_pointer(connection
->net_conf
, new_net_conf
);
2124 crypto_free_hash(connection
->csums_tfm
);
2125 connection
->csums_tfm
= crypto
.csums_tfm
;
2126 crypto
.csums_tfm
= NULL
;
2129 crypto_free_hash(connection
->verify_tfm
);
2130 connection
->verify_tfm
= crypto
.verify_tfm
;
2131 crypto
.verify_tfm
= NULL
;
2134 crypto_free_hash(connection
->integrity_tfm
);
2135 connection
->integrity_tfm
= crypto
.integrity_tfm
;
2136 if (connection
->cstate
>= C_WF_REPORT_PARAMS
&& connection
->agreed_pro_version
>= 100)
2137 /* Do this without trying to take connection->data.mutex again. */
2138 __drbd_send_protocol(connection
, P_PROTOCOL_UPDATE
);
2140 crypto_free_hash(connection
->cram_hmac_tfm
);
2141 connection
->cram_hmac_tfm
= crypto
.cram_hmac_tfm
;
2143 mutex_unlock(&connection
->resource
->conf_update
);
2144 mutex_unlock(&connection
->data
.mutex
);
2146 kfree(old_net_conf
);
2148 if (connection
->cstate
>= C_WF_REPORT_PARAMS
)
2149 drbd_send_sync_param(minor_to_device(conn_lowest_minor(connection
)));
2154 mutex_unlock(&connection
->resource
->conf_update
);
2155 mutex_unlock(&connection
->data
.mutex
);
2156 free_crypto(&crypto
);
2157 kfree(new_net_conf
);
2159 conn_reconfig_done(connection
);
2161 drbd_adm_finish(info
, retcode
);
2165 int drbd_adm_connect(struct sk_buff
*skb
, struct genl_info
*info
)
2167 struct drbd_peer_device
*peer_device
;
2168 struct net_conf
*old_net_conf
, *new_net_conf
= NULL
;
2169 struct crypto crypto
= { };
2170 struct drbd_resource
*resource
;
2171 struct drbd_connection
*connection
;
2172 enum drbd_ret_code retcode
;
2176 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_RESOURCE
);
2178 if (!adm_ctx
.reply_skb
)
2180 if (retcode
!= NO_ERROR
)
2182 if (!(adm_ctx
.my_addr
&& adm_ctx
.peer_addr
)) {
2183 drbd_msg_put_info("connection endpoint(s) missing");
2184 retcode
= ERR_INVALID_REQUEST
;
2188 /* No need for _rcu here. All reconfiguration is
2189 * strictly serialized on genl_lock(). We are protected against
2190 * concurrent reconfiguration/addition/deletion */
2191 for_each_resource(resource
, &drbd_resources
) {
2192 for_each_connection(connection
, resource
) {
2193 if (nla_len(adm_ctx
.my_addr
) == connection
->my_addr_len
&&
2194 !memcmp(nla_data(adm_ctx
.my_addr
), &connection
->my_addr
,
2195 connection
->my_addr_len
)) {
2196 retcode
= ERR_LOCAL_ADDR
;
2200 if (nla_len(adm_ctx
.peer_addr
) == connection
->peer_addr_len
&&
2201 !memcmp(nla_data(adm_ctx
.peer_addr
), &connection
->peer_addr
,
2202 connection
->peer_addr_len
)) {
2203 retcode
= ERR_PEER_ADDR
;
2209 connection
= first_connection(adm_ctx
.resource
);
2210 conn_reconfig_start(connection
);
2212 if (connection
->cstate
> C_STANDALONE
) {
2213 retcode
= ERR_NET_CONFIGURED
;
2217 /* allocation not in the IO path, drbdsetup / netlink process context */
2218 new_net_conf
= kzalloc(sizeof(*new_net_conf
), GFP_KERNEL
);
2219 if (!new_net_conf
) {
2220 retcode
= ERR_NOMEM
;
2224 set_net_conf_defaults(new_net_conf
);
2226 err
= net_conf_from_attrs(new_net_conf
, info
);
2227 if (err
&& err
!= -ENOMSG
) {
2228 retcode
= ERR_MANDATORY_TAG
;
2229 drbd_msg_put_info(from_attrs_err_to_txt(err
));
2233 retcode
= check_net_options(connection
, new_net_conf
);
2234 if (retcode
!= NO_ERROR
)
2237 retcode
= alloc_crypto(&crypto
, new_net_conf
);
2238 if (retcode
!= NO_ERROR
)
2241 ((char *)new_net_conf
->shared_secret
)[SHARED_SECRET_MAX
-1] = 0;
2243 conn_flush_workqueue(connection
);
2245 mutex_lock(&adm_ctx
.resource
->conf_update
);
2246 old_net_conf
= connection
->net_conf
;
2248 retcode
= ERR_NET_CONFIGURED
;
2249 mutex_unlock(&adm_ctx
.resource
->conf_update
);
2252 rcu_assign_pointer(connection
->net_conf
, new_net_conf
);
2254 conn_free_crypto(connection
);
2255 connection
->cram_hmac_tfm
= crypto
.cram_hmac_tfm
;
2256 connection
->integrity_tfm
= crypto
.integrity_tfm
;
2257 connection
->csums_tfm
= crypto
.csums_tfm
;
2258 connection
->verify_tfm
= crypto
.verify_tfm
;
2260 connection
->my_addr_len
= nla_len(adm_ctx
.my_addr
);
2261 memcpy(&connection
->my_addr
, nla_data(adm_ctx
.my_addr
), connection
->my_addr_len
);
2262 connection
->peer_addr_len
= nla_len(adm_ctx
.peer_addr
);
2263 memcpy(&connection
->peer_addr
, nla_data(adm_ctx
.peer_addr
), connection
->peer_addr_len
);
2265 mutex_unlock(&adm_ctx
.resource
->conf_update
);
2268 idr_for_each_entry(&connection
->peer_devices
, peer_device
, i
) {
2269 struct drbd_device
*device
= peer_device
->device
;
2270 device
->send_cnt
= 0;
2271 device
->recv_cnt
= 0;
2275 retcode
= conn_request_state(connection
, NS(conn
, C_UNCONNECTED
), CS_VERBOSE
);
2277 conn_reconfig_done(connection
);
2278 drbd_adm_finish(info
, retcode
);
2282 free_crypto(&crypto
);
2283 kfree(new_net_conf
);
2285 conn_reconfig_done(connection
);
2287 drbd_adm_finish(info
, retcode
);
2291 static enum drbd_state_rv
conn_try_disconnect(struct drbd_connection
*connection
, bool force
)
2293 enum drbd_state_rv rv
;
2295 rv
= conn_request_state(connection
, NS(conn
, C_DISCONNECTING
),
2296 force
? CS_HARD
: 0);
2299 case SS_NOTHING_TO_DO
:
2301 case SS_ALREADY_STANDALONE
:
2303 case SS_PRIMARY_NOP
:
2304 /* Our state checking code wants to see the peer outdated. */
2305 rv
= conn_request_state(connection
, NS2(conn
, C_DISCONNECTING
, pdsk
, D_OUTDATED
), 0);
2307 if (rv
== SS_OUTDATE_WO_CONN
) /* lost connection before graceful disconnect succeeded */
2308 rv
= conn_request_state(connection
, NS(conn
, C_DISCONNECTING
), CS_VERBOSE
);
2311 case SS_CW_FAILED_BY_PEER
:
2312 /* The peer probably wants to see us outdated. */
2313 rv
= conn_request_state(connection
, NS2(conn
, C_DISCONNECTING
,
2314 disk
, D_OUTDATED
), 0);
2315 if (rv
== SS_IS_DISKLESS
|| rv
== SS_LOWER_THAN_OUTDATED
) {
2316 rv
= conn_request_state(connection
, NS(conn
, C_DISCONNECTING
),
2321 /* no special handling necessary */
2324 if (rv
>= SS_SUCCESS
) {
2325 enum drbd_state_rv rv2
;
2326 /* No one else can reconfigure the network while I am here.
2327 * The state handling only uses drbd_thread_stop_nowait(),
2328 * we want to really wait here until the receiver is no more.
2330 drbd_thread_stop(&connection
->receiver
);
2332 /* Race breaker. This additional state change request may be
2333 * necessary, if this was a forced disconnect during a receiver
2334 * restart. We may have "killed" the receiver thread just
2335 * after drbd_receiver() returned. Typically, we should be
2336 * C_STANDALONE already, now, and this becomes a no-op.
2338 rv2
= conn_request_state(connection
, NS(conn
, C_STANDALONE
),
2339 CS_VERBOSE
| CS_HARD
);
2340 if (rv2
< SS_SUCCESS
)
2341 drbd_err(connection
,
2342 "unexpected rv2=%d in conn_try_disconnect()\n",
2348 int drbd_adm_disconnect(struct sk_buff
*skb
, struct genl_info
*info
)
2350 struct disconnect_parms parms
;
2351 struct drbd_connection
*connection
;
2352 enum drbd_state_rv rv
;
2353 enum drbd_ret_code retcode
;
2356 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_CONNECTION
);
2357 if (!adm_ctx
.reply_skb
)
2359 if (retcode
!= NO_ERROR
)
2362 connection
= adm_ctx
.connection
;
2363 memset(&parms
, 0, sizeof(parms
));
2364 if (info
->attrs
[DRBD_NLA_DISCONNECT_PARMS
]) {
2365 err
= disconnect_parms_from_attrs(&parms
, info
);
2367 retcode
= ERR_MANDATORY_TAG
;
2368 drbd_msg_put_info(from_attrs_err_to_txt(err
));
2373 rv
= conn_try_disconnect(connection
, parms
.force_disconnect
);
2374 if (rv
< SS_SUCCESS
)
2375 retcode
= rv
; /* FIXME: Type mismatch. */
2379 drbd_adm_finish(info
, retcode
);
2383 void resync_after_online_grow(struct drbd_device
*device
)
2385 int iass
; /* I am sync source */
2387 drbd_info(device
, "Resync of new storage after online grow\n");
2388 if (device
->state
.role
!= device
->state
.peer
)
2389 iass
= (device
->state
.role
== R_PRIMARY
);
2391 iass
= test_bit(RESOLVE_CONFLICTS
, &first_peer_device(device
)->connection
->flags
);
2394 drbd_start_resync(device
, C_SYNC_SOURCE
);
2396 _drbd_request_state(device
, NS(conn
, C_WF_SYNC_UUID
), CS_VERBOSE
+ CS_SERIALIZE
);
2399 int drbd_adm_resize(struct sk_buff
*skb
, struct genl_info
*info
)
2401 struct disk_conf
*old_disk_conf
, *new_disk_conf
= NULL
;
2402 struct resize_parms rs
;
2403 struct drbd_device
*device
;
2404 enum drbd_ret_code retcode
;
2405 enum determine_dev_size dd
;
2406 bool change_al_layout
= false;
2407 enum dds_flags ddsf
;
2411 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
2412 if (!adm_ctx
.reply_skb
)
2414 if (retcode
!= NO_ERROR
)
2417 device
= adm_ctx
.device
;
2418 if (!get_ldev(device
)) {
2419 retcode
= ERR_NO_DISK
;
2423 memset(&rs
, 0, sizeof(struct resize_parms
));
2424 rs
.al_stripes
= device
->ldev
->md
.al_stripes
;
2425 rs
.al_stripe_size
= device
->ldev
->md
.al_stripe_size_4k
* 4;
2426 if (info
->attrs
[DRBD_NLA_RESIZE_PARMS
]) {
2427 err
= resize_parms_from_attrs(&rs
, info
);
2429 retcode
= ERR_MANDATORY_TAG
;
2430 drbd_msg_put_info(from_attrs_err_to_txt(err
));
2435 if (device
->state
.conn
> C_CONNECTED
) {
2436 retcode
= ERR_RESIZE_RESYNC
;
2440 if (device
->state
.role
== R_SECONDARY
&&
2441 device
->state
.peer
== R_SECONDARY
) {
2442 retcode
= ERR_NO_PRIMARY
;
2446 if (rs
.no_resync
&& first_peer_device(device
)->connection
->agreed_pro_version
< 93) {
2447 retcode
= ERR_NEED_APV_93
;
2452 u_size
= rcu_dereference(device
->ldev
->disk_conf
)->disk_size
;
2454 if (u_size
!= (sector_t
)rs
.resize_size
) {
2455 new_disk_conf
= kmalloc(sizeof(struct disk_conf
), GFP_KERNEL
);
2456 if (!new_disk_conf
) {
2457 retcode
= ERR_NOMEM
;
2462 if (device
->ldev
->md
.al_stripes
!= rs
.al_stripes
||
2463 device
->ldev
->md
.al_stripe_size_4k
!= rs
.al_stripe_size
/ 4) {
2464 u32 al_size_k
= rs
.al_stripes
* rs
.al_stripe_size
;
2466 if (al_size_k
> (16 * 1024 * 1024)) {
2467 retcode
= ERR_MD_LAYOUT_TOO_BIG
;
2471 if (al_size_k
< MD_32kB_SECT
/2) {
2472 retcode
= ERR_MD_LAYOUT_TOO_SMALL
;
2476 if (device
->state
.conn
!= C_CONNECTED
) {
2477 retcode
= ERR_MD_LAYOUT_CONNECTED
;
2481 change_al_layout
= true;
2484 if (device
->ldev
->known_size
!= drbd_get_capacity(device
->ldev
->backing_bdev
))
2485 device
->ldev
->known_size
= drbd_get_capacity(device
->ldev
->backing_bdev
);
2487 if (new_disk_conf
) {
2488 mutex_lock(&device
->resource
->conf_update
);
2489 old_disk_conf
= device
->ldev
->disk_conf
;
2490 *new_disk_conf
= *old_disk_conf
;
2491 new_disk_conf
->disk_size
= (sector_t
)rs
.resize_size
;
2492 rcu_assign_pointer(device
->ldev
->disk_conf
, new_disk_conf
);
2493 mutex_unlock(&device
->resource
->conf_update
);
2495 kfree(old_disk_conf
);
2498 ddsf
= (rs
.resize_force
? DDSF_FORCED
: 0) | (rs
.no_resync
? DDSF_NO_RESYNC
: 0);
2499 dd
= drbd_determine_dev_size(device
, ddsf
, change_al_layout
? &rs
: NULL
);
2500 drbd_md_sync(device
);
2502 if (dd
== DS_ERROR
) {
2503 retcode
= ERR_NOMEM_BITMAP
;
2505 } else if (dd
== DS_ERROR_SPACE_MD
) {
2506 retcode
= ERR_MD_LAYOUT_NO_FIT
;
2508 } else if (dd
== DS_ERROR_SHRINK
) {
2509 retcode
= ERR_IMPLICIT_SHRINK
;
2513 if (device
->state
.conn
== C_CONNECTED
) {
2515 set_bit(RESIZE_PENDING
, &device
->flags
);
2517 drbd_send_uuids(device
);
2518 drbd_send_sizes(device
, 1, ddsf
);
2522 drbd_adm_finish(info
, retcode
);
2530 int drbd_adm_resource_opts(struct sk_buff
*skb
, struct genl_info
*info
)
2532 enum drbd_ret_code retcode
;
2533 struct res_opts res_opts
;
2536 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_RESOURCE
);
2537 if (!adm_ctx
.reply_skb
)
2539 if (retcode
!= NO_ERROR
)
2542 res_opts
= adm_ctx
.resource
->res_opts
;
2543 if (should_set_defaults(info
))
2544 set_res_opts_defaults(&res_opts
);
2546 err
= res_opts_from_attrs(&res_opts
, info
);
2547 if (err
&& err
!= -ENOMSG
) {
2548 retcode
= ERR_MANDATORY_TAG
;
2549 drbd_msg_put_info(from_attrs_err_to_txt(err
));
2553 err
= set_resource_options(adm_ctx
.resource
, &res_opts
);
2555 retcode
= ERR_INVALID_REQUEST
;
2557 retcode
= ERR_NOMEM
;
2561 drbd_adm_finish(info
, retcode
);
2565 int drbd_adm_invalidate(struct sk_buff
*skb
, struct genl_info
*info
)
2567 struct drbd_device
*device
;
2568 int retcode
; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2570 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
2571 if (!adm_ctx
.reply_skb
)
2573 if (retcode
!= NO_ERROR
)
2576 device
= adm_ctx
.device
;
2578 /* If there is still bitmap IO pending, probably because of a previous
2579 * resync just being finished, wait for it before requesting a new resync.
2580 * Also wait for it's after_state_ch(). */
2581 drbd_suspend_io(device
);
2582 wait_event(device
->misc_wait
, !test_bit(BITMAP_IO
, &device
->flags
));
2583 drbd_flush_workqueue(device
);
2585 /* If we happen to be C_STANDALONE R_SECONDARY, just change to
2586 * D_INCONSISTENT, and set all bits in the bitmap. Otherwise,
2587 * try to start a resync handshake as sync target for full sync.
2589 if (device
->state
.conn
== C_STANDALONE
&& device
->state
.role
== R_SECONDARY
) {
2590 retcode
= drbd_request_state(device
, NS(disk
, D_INCONSISTENT
));
2591 if (retcode
>= SS_SUCCESS
) {
2592 if (drbd_bitmap_io(device
, &drbd_bmio_set_n_write
,
2593 "set_n_write from invalidate", BM_LOCKED_MASK
))
2594 retcode
= ERR_IO_MD_DISK
;
2597 retcode
= drbd_request_state(device
, NS(conn
, C_STARTING_SYNC_T
));
2598 drbd_resume_io(device
);
2601 drbd_adm_finish(info
, retcode
);
2605 static int drbd_adm_simple_request_state(struct sk_buff
*skb
, struct genl_info
*info
,
2606 union drbd_state mask
, union drbd_state val
)
2608 enum drbd_ret_code retcode
;
2610 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
2611 if (!adm_ctx
.reply_skb
)
2613 if (retcode
!= NO_ERROR
)
2616 retcode
= drbd_request_state(adm_ctx
.device
, mask
, val
);
2618 drbd_adm_finish(info
, retcode
);
2622 static int drbd_bmio_set_susp_al(struct drbd_device
*device
)
2626 rv
= drbd_bmio_set_n_write(device
);
2627 drbd_suspend_al(device
);
2631 int drbd_adm_invalidate_peer(struct sk_buff
*skb
, struct genl_info
*info
)
2633 int retcode
; /* drbd_ret_code, drbd_state_rv */
2634 struct drbd_device
*device
;
2636 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
2637 if (!adm_ctx
.reply_skb
)
2639 if (retcode
!= NO_ERROR
)
2642 device
= adm_ctx
.device
;
2644 /* If there is still bitmap IO pending, probably because of a previous
2645 * resync just being finished, wait for it before requesting a new resync.
2646 * Also wait for it's after_state_ch(). */
2647 drbd_suspend_io(device
);
2648 wait_event(device
->misc_wait
, !test_bit(BITMAP_IO
, &device
->flags
));
2649 drbd_flush_workqueue(device
);
2651 /* If we happen to be C_STANDALONE R_PRIMARY, just set all bits
2652 * in the bitmap. Otherwise, try to start a resync handshake
2653 * as sync source for full sync.
2655 if (device
->state
.conn
== C_STANDALONE
&& device
->state
.role
== R_PRIMARY
) {
2656 /* The peer will get a resync upon connect anyways. Just make that
2657 into a full resync. */
2658 retcode
= drbd_request_state(device
, NS(pdsk
, D_INCONSISTENT
));
2659 if (retcode
>= SS_SUCCESS
) {
2660 if (drbd_bitmap_io(device
, &drbd_bmio_set_susp_al
,
2661 "set_n_write from invalidate_peer",
2662 BM_LOCKED_SET_ALLOWED
))
2663 retcode
= ERR_IO_MD_DISK
;
2666 retcode
= drbd_request_state(device
, NS(conn
, C_STARTING_SYNC_S
));
2667 drbd_resume_io(device
);
2670 drbd_adm_finish(info
, retcode
);
2674 int drbd_adm_pause_sync(struct sk_buff
*skb
, struct genl_info
*info
)
2676 enum drbd_ret_code retcode
;
2678 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
2679 if (!adm_ctx
.reply_skb
)
2681 if (retcode
!= NO_ERROR
)
2684 if (drbd_request_state(adm_ctx
.device
, NS(user_isp
, 1)) == SS_NOTHING_TO_DO
)
2685 retcode
= ERR_PAUSE_IS_SET
;
2687 drbd_adm_finish(info
, retcode
);
2691 int drbd_adm_resume_sync(struct sk_buff
*skb
, struct genl_info
*info
)
2693 union drbd_dev_state s
;
2694 enum drbd_ret_code retcode
;
2696 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
2697 if (!adm_ctx
.reply_skb
)
2699 if (retcode
!= NO_ERROR
)
2702 if (drbd_request_state(adm_ctx
.device
, NS(user_isp
, 0)) == SS_NOTHING_TO_DO
) {
2703 s
= adm_ctx
.device
->state
;
2704 if (s
.conn
== C_PAUSED_SYNC_S
|| s
.conn
== C_PAUSED_SYNC_T
) {
2705 retcode
= s
.aftr_isp
? ERR_PIC_AFTER_DEP
:
2706 s
.peer_isp
? ERR_PIC_PEER_DEP
: ERR_PAUSE_IS_CLEAR
;
2708 retcode
= ERR_PAUSE_IS_CLEAR
;
2713 drbd_adm_finish(info
, retcode
);
2717 int drbd_adm_suspend_io(struct sk_buff
*skb
, struct genl_info
*info
)
2719 return drbd_adm_simple_request_state(skb
, info
, NS(susp
, 1));
2722 int drbd_adm_resume_io(struct sk_buff
*skb
, struct genl_info
*info
)
2724 struct drbd_device
*device
;
2725 int retcode
; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2727 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
2728 if (!adm_ctx
.reply_skb
)
2730 if (retcode
!= NO_ERROR
)
2733 device
= adm_ctx
.device
;
2734 if (test_bit(NEW_CUR_UUID
, &device
->flags
)) {
2735 drbd_uuid_new_current(device
);
2736 clear_bit(NEW_CUR_UUID
, &device
->flags
);
2738 drbd_suspend_io(device
);
2739 retcode
= drbd_request_state(device
, NS3(susp
, 0, susp_nod
, 0, susp_fen
, 0));
2740 if (retcode
== SS_SUCCESS
) {
2741 if (device
->state
.conn
< C_CONNECTED
)
2742 tl_clear(first_peer_device(device
)->connection
);
2743 if (device
->state
.disk
== D_DISKLESS
|| device
->state
.disk
== D_FAILED
)
2744 tl_restart(first_peer_device(device
)->connection
, FAIL_FROZEN_DISK_IO
);
2746 drbd_resume_io(device
);
2749 drbd_adm_finish(info
, retcode
);
2753 int drbd_adm_outdate(struct sk_buff
*skb
, struct genl_info
*info
)
2755 return drbd_adm_simple_request_state(skb
, info
, NS(disk
, D_OUTDATED
));
2758 static int nla_put_drbd_cfg_context(struct sk_buff
*skb
,
2759 struct drbd_resource
*resource
,
2760 struct drbd_connection
*connection
,
2761 struct drbd_device
*device
)
2764 nla
= nla_nest_start(skb
, DRBD_NLA_CFG_CONTEXT
);
2766 goto nla_put_failure
;
2768 nla_put_u32(skb
, T_ctx_volume
, device
->vnr
))
2769 goto nla_put_failure
;
2770 if (nla_put_string(skb
, T_ctx_resource_name
, connection
->resource
->name
))
2771 goto nla_put_failure
;
2773 if (connection
->my_addr_len
&&
2774 nla_put(skb
, T_ctx_my_addr
, connection
->my_addr_len
, &connection
->my_addr
))
2775 goto nla_put_failure
;
2776 if (connection
->peer_addr_len
&&
2777 nla_put(skb
, T_ctx_peer_addr
, connection
->peer_addr_len
, &connection
->peer_addr
))
2778 goto nla_put_failure
;
2780 nla_nest_end(skb
, nla
);
2785 nla_nest_cancel(skb
, nla
);
2790 * Return the connection of @resource if @resource has exactly one connection.
2792 static struct drbd_connection
*the_only_connection(struct drbd_resource
*resource
)
2794 struct list_head
*connections
= &resource
->connections
;
2796 if (list_empty(connections
) || connections
->next
->next
!= connections
)
2798 return list_first_entry(&resource
->connections
, struct drbd_connection
, connections
);
2801 int nla_put_status_info(struct sk_buff
*skb
, struct drbd_device
*device
,
2802 const struct sib_info
*sib
)
2804 struct drbd_resource
*resource
= device
->resource
;
2805 struct state_info
*si
= NULL
; /* for sizeof(si->member); */
2809 int exclude_sensitive
;
2811 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2812 * to. So we better exclude_sensitive information.
2814 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2815 * in the context of the requesting user process. Exclude sensitive
2816 * information, unless current has superuser.
2818 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2819 * relies on the current implementation of netlink_dump(), which
2820 * executes the dump callback successively from netlink_recvmsg(),
2821 * always in the context of the receiving process */
2822 exclude_sensitive
= sib
|| !capable(CAP_SYS_ADMIN
);
2824 got_ldev
= get_ldev(device
);
2826 /* We need to add connection name and volume number information still.
2827 * Minor number is in drbd_genlmsghdr. */
2828 if (nla_put_drbd_cfg_context(skb
, resource
, the_only_connection(resource
), device
))
2829 goto nla_put_failure
;
2831 if (res_opts_to_skb(skb
, &device
->resource
->res_opts
, exclude_sensitive
))
2832 goto nla_put_failure
;
2836 struct disk_conf
*disk_conf
;
2838 disk_conf
= rcu_dereference(device
->ldev
->disk_conf
);
2839 err
= disk_conf_to_skb(skb
, disk_conf
, exclude_sensitive
);
2842 struct net_conf
*nc
;
2844 nc
= rcu_dereference(first_peer_device(device
)->connection
->net_conf
);
2846 err
= net_conf_to_skb(skb
, nc
, exclude_sensitive
);
2850 goto nla_put_failure
;
2852 nla
= nla_nest_start(skb
, DRBD_NLA_STATE_INFO
);
2854 goto nla_put_failure
;
2855 if (nla_put_u32(skb
, T_sib_reason
, sib
? sib
->sib_reason
: SIB_GET_STATUS_REPLY
) ||
2856 nla_put_u32(skb
, T_current_state
, device
->state
.i
) ||
2857 nla_put_u64(skb
, T_ed_uuid
, device
->ed_uuid
) ||
2858 nla_put_u64(skb
, T_capacity
, drbd_get_capacity(device
->this_bdev
)) ||
2859 nla_put_u64(skb
, T_send_cnt
, device
->send_cnt
) ||
2860 nla_put_u64(skb
, T_recv_cnt
, device
->recv_cnt
) ||
2861 nla_put_u64(skb
, T_read_cnt
, device
->read_cnt
) ||
2862 nla_put_u64(skb
, T_writ_cnt
, device
->writ_cnt
) ||
2863 nla_put_u64(skb
, T_al_writ_cnt
, device
->al_writ_cnt
) ||
2864 nla_put_u64(skb
, T_bm_writ_cnt
, device
->bm_writ_cnt
) ||
2865 nla_put_u32(skb
, T_ap_bio_cnt
, atomic_read(&device
->ap_bio_cnt
)) ||
2866 nla_put_u32(skb
, T_ap_pending_cnt
, atomic_read(&device
->ap_pending_cnt
)) ||
2867 nla_put_u32(skb
, T_rs_pending_cnt
, atomic_read(&device
->rs_pending_cnt
)))
2868 goto nla_put_failure
;
2873 spin_lock_irq(&device
->ldev
->md
.uuid_lock
);
2874 err
= nla_put(skb
, T_uuids
, sizeof(si
->uuids
), device
->ldev
->md
.uuid
);
2875 spin_unlock_irq(&device
->ldev
->md
.uuid_lock
);
2878 goto nla_put_failure
;
2880 if (nla_put_u32(skb
, T_disk_flags
, device
->ldev
->md
.flags
) ||
2881 nla_put_u64(skb
, T_bits_total
, drbd_bm_bits(device
)) ||
2882 nla_put_u64(skb
, T_bits_oos
, drbd_bm_total_weight(device
)))
2883 goto nla_put_failure
;
2884 if (C_SYNC_SOURCE
<= device
->state
.conn
&&
2885 C_PAUSED_SYNC_T
>= device
->state
.conn
) {
2886 if (nla_put_u64(skb
, T_bits_rs_total
, device
->rs_total
) ||
2887 nla_put_u64(skb
, T_bits_rs_failed
, device
->rs_failed
))
2888 goto nla_put_failure
;
2893 switch(sib
->sib_reason
) {
2894 case SIB_SYNC_PROGRESS
:
2895 case SIB_GET_STATUS_REPLY
:
2897 case SIB_STATE_CHANGE
:
2898 if (nla_put_u32(skb
, T_prev_state
, sib
->os
.i
) ||
2899 nla_put_u32(skb
, T_new_state
, sib
->ns
.i
))
2900 goto nla_put_failure
;
2902 case SIB_HELPER_POST
:
2903 if (nla_put_u32(skb
, T_helper_exit_code
,
2904 sib
->helper_exit_code
))
2905 goto nla_put_failure
;
2907 case SIB_HELPER_PRE
:
2908 if (nla_put_string(skb
, T_helper
, sib
->helper_name
))
2909 goto nla_put_failure
;
2913 nla_nest_end(skb
, nla
);
2923 int drbd_adm_get_status(struct sk_buff
*skb
, struct genl_info
*info
)
2925 enum drbd_ret_code retcode
;
2928 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
2929 if (!adm_ctx
.reply_skb
)
2931 if (retcode
!= NO_ERROR
)
2934 err
= nla_put_status_info(adm_ctx
.reply_skb
, adm_ctx
.device
, NULL
);
2936 nlmsg_free(adm_ctx
.reply_skb
);
2940 drbd_adm_finish(info
, retcode
);
2944 static int get_one_status(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2946 struct drbd_device
*device
;
2947 struct drbd_genlmsghdr
*dh
;
2948 struct drbd_resource
*pos
= (struct drbd_resource
*)cb
->args
[0];
2949 struct drbd_resource
*resource
= NULL
;
2950 struct drbd_resource
*tmp
;
2951 unsigned volume
= cb
->args
[1];
2953 /* Open coded, deferred, iteration:
2954 * for_each_resource_safe(resource, tmp, &drbd_resources) {
2955 * connection = "first connection of resource or undefined";
2956 * idr_for_each_entry(&resource->devices, device, i) {
2960 * where resource is cb->args[0];
2961 * and i is cb->args[1];
2963 * cb->args[2] indicates if we shall loop over all resources,
2964 * or just dump all volumes of a single resource.
2966 * This may miss entries inserted after this dump started,
2967 * or entries deleted before they are reached.
2969 * We need to make sure the device won't disappear while
2970 * we are looking at it, and revalidate our iterators
2971 * on each iteration.
2974 /* synchronize with conn_create()/drbd_destroy_connection() */
2976 /* revalidate iterator position */
2977 for_each_resource_rcu(tmp
, &drbd_resources
) {
2979 /* first iteration */
2991 device
= idr_get_next(&resource
->devices
, &volume
);
2993 /* No more volumes to dump on this resource.
2994 * Advance resource iterator. */
2995 pos
= list_entry_rcu(resource
->resources
.next
,
2996 struct drbd_resource
, resources
);
2997 /* Did we dump any volume of this resource yet? */
2999 /* If we reached the end of the list,
3000 * or only a single resource dump was requested,
3002 if (&pos
->resources
== &drbd_resources
|| cb
->args
[2])
3010 dh
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
,
3011 cb
->nlh
->nlmsg_seq
, &drbd_genl_family
,
3012 NLM_F_MULTI
, DRBD_ADM_GET_STATUS
);
3017 /* This is a connection without a single volume.
3018 * Suprisingly enough, it may have a network
3020 struct drbd_connection
*connection
;
3023 dh
->ret_code
= NO_ERROR
;
3024 connection
= the_only_connection(resource
);
3025 if (nla_put_drbd_cfg_context(skb
, resource
, connection
, NULL
))
3028 struct net_conf
*nc
;
3030 nc
= rcu_dereference(connection
->net_conf
);
3031 if (nc
&& net_conf_to_skb(skb
, nc
, 1) != 0)
3037 D_ASSERT(device
, device
->vnr
== volume
);
3038 D_ASSERT(device
, device
->resource
== resource
);
3040 dh
->minor
= device_to_minor(device
);
3041 dh
->ret_code
= NO_ERROR
;
3043 if (nla_put_status_info(skb
, device
, NULL
)) {
3045 genlmsg_cancel(skb
, dh
);
3049 genlmsg_end(skb
, dh
);
3054 /* where to start the next iteration */
3055 cb
->args
[0] = (long)pos
;
3056 cb
->args
[1] = (pos
== resource
) ? volume
+ 1 : 0;
3058 /* No more resources/volumes/minors found results in an empty skb.
3059 * Which will terminate the dump. */
3064 * Request status of all resources, or of all volumes within a single resource.
3066 * This is a dump, as the answer may not fit in a single reply skb otherwise.
3067 * Which means we cannot use the family->attrbuf or other such members, because
3068 * dump is NOT protected by the genl_lock(). During dump, we only have access
3069 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
3071 * Once things are setup properly, we call into get_one_status().
3073 int drbd_adm_get_status_all(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3075 const unsigned hdrlen
= GENL_HDRLEN
+ GENL_MAGIC_FAMILY_HDRSZ
;
3077 const char *resource_name
;
3078 struct drbd_resource
*resource
;
3081 /* Is this a followup call? */
3083 /* ... of a single resource dump,
3084 * and the resource iterator has been advanced already? */
3085 if (cb
->args
[2] && cb
->args
[2] != cb
->args
[0])
3086 return 0; /* DONE. */
3090 /* First call (from netlink_dump_start). We need to figure out
3091 * which resource(s) the user wants us to dump. */
3092 nla
= nla_find(nlmsg_attrdata(cb
->nlh
, hdrlen
),
3093 nlmsg_attrlen(cb
->nlh
, hdrlen
),
3094 DRBD_NLA_CFG_CONTEXT
);
3096 /* No explicit context given. Dump all. */
3099 maxtype
= ARRAY_SIZE(drbd_cfg_context_nl_policy
) - 1;
3100 nla
= drbd_nla_find_nested(maxtype
, nla
, __nla_type(T_ctx_resource_name
));
3102 return PTR_ERR(nla
);
3103 /* context given, but no name present? */
3106 resource_name
= nla_data(nla
);
3107 if (!*resource_name
)
3109 resource
= drbd_find_resource(resource_name
);
3113 kref_put(&resource
->kref
, drbd_destroy_resource
); /* get_one_status() revalidates the resource */
3115 /* prime iterators, and set "filter" mode mark:
3116 * only dump this connection. */
3117 cb
->args
[0] = (long)resource
;
3118 /* cb->args[1] = 0; passed in this way. */
3119 cb
->args
[2] = (long)resource
;
3122 return get_one_status(skb
, cb
);
3125 int drbd_adm_get_timeout_type(struct sk_buff
*skb
, struct genl_info
*info
)
3127 enum drbd_ret_code retcode
;
3128 struct timeout_parms tp
;
3131 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
3132 if (!adm_ctx
.reply_skb
)
3134 if (retcode
!= NO_ERROR
)
3138 adm_ctx
.device
->state
.pdsk
== D_OUTDATED
? UT_PEER_OUTDATED
:
3139 test_bit(USE_DEGR_WFC_T
, &adm_ctx
.device
->flags
) ? UT_DEGRADED
:
3142 err
= timeout_parms_to_priv_skb(adm_ctx
.reply_skb
, &tp
);
3144 nlmsg_free(adm_ctx
.reply_skb
);
3148 drbd_adm_finish(info
, retcode
);
3152 int drbd_adm_start_ov(struct sk_buff
*skb
, struct genl_info
*info
)
3154 struct drbd_device
*device
;
3155 enum drbd_ret_code retcode
;
3156 struct start_ov_parms parms
;
3158 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
3159 if (!adm_ctx
.reply_skb
)
3161 if (retcode
!= NO_ERROR
)
3164 device
= adm_ctx
.device
;
3166 /* resume from last known position, if possible */
3167 parms
.ov_start_sector
= device
->ov_start_sector
;
3168 parms
.ov_stop_sector
= ULLONG_MAX
;
3169 if (info
->attrs
[DRBD_NLA_START_OV_PARMS
]) {
3170 int err
= start_ov_parms_from_attrs(&parms
, info
);
3172 retcode
= ERR_MANDATORY_TAG
;
3173 drbd_msg_put_info(from_attrs_err_to_txt(err
));
3177 /* w_make_ov_request expects position to be aligned */
3178 device
->ov_start_sector
= parms
.ov_start_sector
& ~(BM_SECT_PER_BIT
-1);
3179 device
->ov_stop_sector
= parms
.ov_stop_sector
;
3181 /* If there is still bitmap IO pending, e.g. previous resync or verify
3182 * just being finished, wait for it before requesting a new resync. */
3183 drbd_suspend_io(device
);
3184 wait_event(device
->misc_wait
, !test_bit(BITMAP_IO
, &device
->flags
));
3185 retcode
= drbd_request_state(device
, NS(conn
, C_VERIFY_S
));
3186 drbd_resume_io(device
);
3188 drbd_adm_finish(info
, retcode
);
3193 int drbd_adm_new_c_uuid(struct sk_buff
*skb
, struct genl_info
*info
)
3195 struct drbd_device
*device
;
3196 enum drbd_ret_code retcode
;
3197 int skip_initial_sync
= 0;
3199 struct new_c_uuid_parms args
;
3201 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
3202 if (!adm_ctx
.reply_skb
)
3204 if (retcode
!= NO_ERROR
)
3207 device
= adm_ctx
.device
;
3208 memset(&args
, 0, sizeof(args
));
3209 if (info
->attrs
[DRBD_NLA_NEW_C_UUID_PARMS
]) {
3210 err
= new_c_uuid_parms_from_attrs(&args
, info
);
3212 retcode
= ERR_MANDATORY_TAG
;
3213 drbd_msg_put_info(from_attrs_err_to_txt(err
));
3218 mutex_lock(device
->state_mutex
); /* Protects us against serialized state changes. */
3220 if (!get_ldev(device
)) {
3221 retcode
= ERR_NO_DISK
;
3225 /* this is "skip initial sync", assume to be clean */
3226 if (device
->state
.conn
== C_CONNECTED
&&
3227 first_peer_device(device
)->connection
->agreed_pro_version
>= 90 &&
3228 device
->ldev
->md
.uuid
[UI_CURRENT
] == UUID_JUST_CREATED
&& args
.clear_bm
) {
3229 drbd_info(device
, "Preparing to skip initial sync\n");
3230 skip_initial_sync
= 1;
3231 } else if (device
->state
.conn
!= C_STANDALONE
) {
3232 retcode
= ERR_CONNECTED
;
3236 drbd_uuid_set(device
, UI_BITMAP
, 0); /* Rotate UI_BITMAP to History 1, etc... */
3237 drbd_uuid_new_current(device
); /* New current, previous to UI_BITMAP */
3239 if (args
.clear_bm
) {
3240 err
= drbd_bitmap_io(device
, &drbd_bmio_clear_n_write
,
3241 "clear_n_write from new_c_uuid", BM_LOCKED_MASK
);
3243 drbd_err(device
, "Writing bitmap failed with %d\n", err
);
3244 retcode
= ERR_IO_MD_DISK
;
3246 if (skip_initial_sync
) {
3247 drbd_send_uuids_skip_initial_sync(device
);
3248 _drbd_uuid_set(device
, UI_BITMAP
, 0);
3249 drbd_print_uuids(device
, "cleared bitmap UUID");
3250 spin_lock_irq(&device
->resource
->req_lock
);
3251 _drbd_set_state(_NS2(device
, disk
, D_UP_TO_DATE
, pdsk
, D_UP_TO_DATE
),
3253 spin_unlock_irq(&device
->resource
->req_lock
);
3257 drbd_md_sync(device
);
3261 mutex_unlock(device
->state_mutex
);
3263 drbd_adm_finish(info
, retcode
);
3267 static enum drbd_ret_code
3268 drbd_check_resource_name(const char *name
)
3270 if (!name
|| !name
[0]) {
3271 drbd_msg_put_info("resource name missing");
3272 return ERR_MANDATORY_TAG
;
3274 /* if we want to use these in sysfs/configfs/debugfs some day,
3275 * we must not allow slashes */
3276 if (strchr(name
, '/')) {
3277 drbd_msg_put_info("invalid resource name");
3278 return ERR_INVALID_REQUEST
;
3283 int drbd_adm_new_resource(struct sk_buff
*skb
, struct genl_info
*info
)
3285 enum drbd_ret_code retcode
;
3286 struct res_opts res_opts
;
3289 retcode
= drbd_adm_prepare(skb
, info
, 0);
3290 if (!adm_ctx
.reply_skb
)
3292 if (retcode
!= NO_ERROR
)
3295 set_res_opts_defaults(&res_opts
);
3296 err
= res_opts_from_attrs(&res_opts
, info
);
3297 if (err
&& err
!= -ENOMSG
) {
3298 retcode
= ERR_MANDATORY_TAG
;
3299 drbd_msg_put_info(from_attrs_err_to_txt(err
));
3303 retcode
= drbd_check_resource_name(adm_ctx
.resource_name
);
3304 if (retcode
!= NO_ERROR
)
3307 if (adm_ctx
.resource
) {
3308 if (info
->nlhdr
->nlmsg_flags
& NLM_F_EXCL
) {
3309 retcode
= ERR_INVALID_REQUEST
;
3310 drbd_msg_put_info("resource exists");
3312 /* else: still NO_ERROR */
3316 if (!conn_create(adm_ctx
.resource_name
, &res_opts
))
3317 retcode
= ERR_NOMEM
;
3319 drbd_adm_finish(info
, retcode
);
3323 int drbd_adm_new_minor(struct sk_buff
*skb
, struct genl_info
*info
)
3325 struct drbd_genlmsghdr
*dh
= info
->userhdr
;
3326 enum drbd_ret_code retcode
;
3328 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_RESOURCE
);
3329 if (!adm_ctx
.reply_skb
)
3331 if (retcode
!= NO_ERROR
)
3334 if (dh
->minor
> MINORMASK
) {
3335 drbd_msg_put_info("requested minor out of range");
3336 retcode
= ERR_INVALID_REQUEST
;
3339 if (adm_ctx
.volume
> DRBD_VOLUME_MAX
) {
3340 drbd_msg_put_info("requested volume id out of range");
3341 retcode
= ERR_INVALID_REQUEST
;
3345 /* drbd_adm_prepare made sure already
3346 * that first_peer_device(device)->connection and device->vnr match the request. */
3347 if (adm_ctx
.device
) {
3348 if (info
->nlhdr
->nlmsg_flags
& NLM_F_EXCL
)
3349 retcode
= ERR_MINOR_EXISTS
;
3350 /* else: still NO_ERROR */
3354 retcode
= drbd_create_device(adm_ctx
.resource
, dh
->minor
, adm_ctx
.volume
);
3356 drbd_adm_finish(info
, retcode
);
3360 static enum drbd_ret_code
adm_del_minor(struct drbd_device
*device
)
3362 if (device
->state
.disk
== D_DISKLESS
&&
3363 /* no need to be device->state.conn == C_STANDALONE &&
3364 * we may want to delete a minor from a live replication group.
3366 device
->state
.role
== R_SECONDARY
) {
3367 _drbd_request_state(device
, NS(conn
, C_WF_REPORT_PARAMS
),
3368 CS_VERBOSE
+ CS_WAIT_COMPLETE
);
3369 drbd_delete_device(device
);
3372 return ERR_MINOR_CONFIGURED
;
3375 int drbd_adm_del_minor(struct sk_buff
*skb
, struct genl_info
*info
)
3377 enum drbd_ret_code retcode
;
3379 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
3380 if (!adm_ctx
.reply_skb
)
3382 if (retcode
!= NO_ERROR
)
3385 retcode
= adm_del_minor(adm_ctx
.device
);
3387 drbd_adm_finish(info
, retcode
);
3391 int drbd_adm_down(struct sk_buff
*skb
, struct genl_info
*info
)
3393 struct drbd_resource
*resource
;
3394 struct drbd_connection
*connection
;
3395 struct drbd_device
*device
;
3396 int retcode
; /* enum drbd_ret_code rsp. enum drbd_state_rv */
3399 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_RESOURCE
);
3400 if (!adm_ctx
.reply_skb
)
3402 if (retcode
!= NO_ERROR
)
3405 resource
= adm_ctx
.resource
;
3407 for_each_connection(connection
, resource
) {
3408 struct drbd_peer_device
*peer_device
;
3410 idr_for_each_entry(&connection
->peer_devices
, peer_device
, i
) {
3411 retcode
= drbd_set_role(peer_device
->device
, R_SECONDARY
, 0);
3412 if (retcode
< SS_SUCCESS
) {
3413 drbd_msg_put_info("failed to demote");
3418 retcode
= conn_try_disconnect(connection
, 0);
3419 if (retcode
< SS_SUCCESS
) {
3420 drbd_msg_put_info("failed to disconnect");
3426 idr_for_each_entry(&resource
->devices
, device
, i
) {
3427 retcode
= adm_detach(device
, 0);
3428 if (retcode
< SS_SUCCESS
|| retcode
> NO_ERROR
) {
3429 drbd_msg_put_info("failed to detach");
3434 /* If we reach this, all volumes (of this connection) are Secondary,
3435 * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
3436 * actually stopped, state handling only does drbd_thread_stop_nowait(). */
3437 for_each_connection(connection
, resource
)
3438 drbd_thread_stop(&connection
->worker
);
3440 /* Now, nothing can fail anymore */
3442 /* delete volumes */
3443 idr_for_each_entry(&resource
->devices
, device
, i
) {
3444 retcode
= adm_del_minor(device
);
3445 if (retcode
!= NO_ERROR
) {
3446 /* "can not happen" */
3447 drbd_msg_put_info("failed to delete volume");
3452 list_del_rcu(&resource
->resources
);
3454 drbd_free_resource(resource
);
3458 drbd_adm_finish(info
, retcode
);
3462 int drbd_adm_del_resource(struct sk_buff
*skb
, struct genl_info
*info
)
3464 struct drbd_resource
*resource
;
3465 struct drbd_connection
*connection
;
3466 enum drbd_ret_code retcode
;
3468 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_RESOURCE
);
3469 if (!adm_ctx
.reply_skb
)
3471 if (retcode
!= NO_ERROR
)
3474 resource
= adm_ctx
.resource
;
3475 for_each_connection(connection
, resource
) {
3476 if (connection
->cstate
> C_STANDALONE
) {
3477 retcode
= ERR_NET_CONFIGURED
;
3481 if (!idr_is_empty(&resource
->devices
)) {
3482 retcode
= ERR_RES_IN_USE
;
3486 list_del_rcu(&resource
->resources
);
3487 for_each_connection(connection
, resource
)
3488 drbd_thread_stop(&connection
->worker
);
3490 drbd_free_resource(resource
);
3493 drbd_adm_finish(info
, retcode
);
3497 void drbd_bcast_event(struct drbd_device
*device
, const struct sib_info
*sib
)
3499 static atomic_t drbd_genl_seq
= ATOMIC_INIT(2); /* two. */
3500 struct sk_buff
*msg
;
3501 struct drbd_genlmsghdr
*d_out
;
3505 if (sib
->sib_reason
== SIB_SYNC_PROGRESS
) {
3506 if (time_after(jiffies
, device
->rs_last_bcast
+ HZ
))
3507 device
->rs_last_bcast
= jiffies
;
3512 seq
= atomic_inc_return(&drbd_genl_seq
);
3513 msg
= genlmsg_new(NLMSG_GOODSIZE
, GFP_NOIO
);
3518 d_out
= genlmsg_put(msg
, 0, seq
, &drbd_genl_family
, 0, DRBD_EVENT
);
3519 if (!d_out
) /* cannot happen, but anyways. */
3520 goto nla_put_failure
;
3521 d_out
->minor
= device_to_minor(device
);
3522 d_out
->ret_code
= NO_ERROR
;
3524 if (nla_put_status_info(msg
, device
, sib
))
3525 goto nla_put_failure
;
3526 genlmsg_end(msg
, d_out
);
3527 err
= drbd_genl_multicast_events(msg
, 0);
3528 /* msg has been consumed or freed in netlink_broadcast() */
3529 if (err
&& err
!= -ESRCH
)
3537 drbd_err(device
, "Error %d while broadcasting event. "
3538 "Event seq:%u sib_reason:%u\n",
3539 err
, seq
, sib
->sib_reason
);