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