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