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 * net_conf_to_skb() serializes the shared secret verbatim. Any path that can 3311 * answer a request from an unprivileged process must pass exclude_sensitive, 3312 * so the secret is blanked in a private copy before it reaches the skb. 3313 */ 3314 static int net_conf_to_skb_sanitized(struct sk_buff *skb, struct net_conf *nc, 3315 bool exclude_sensitive) 3316 { 3317 struct net_conf nc_clean; 3318 3319 if (!exclude_sensitive) 3320 return net_conf_to_skb(skb, nc); 3321 3322 nc_clean = *nc; 3323 memset(nc_clean.shared_secret, 0, sizeof(nc_clean.shared_secret)); 3324 nc_clean.shared_secret_len = 0; 3325 3326 return net_conf_to_skb(skb, &nc_clean); 3327 } 3328 3329 /* 3330 * The generic netlink dump callbacks are called outside the genl_lock(), so 3331 * they cannot use the simple attribute parsing code which uses global 3332 * attribute tables. 3333 */ 3334 static struct nlattr *find_cfg_context_attr(const struct nlmsghdr *nlh, int attr) 3335 { 3336 const unsigned int hdrlen = GENL_HDRLEN + sizeof(struct drbd_genlmsghdr); 3337 struct nlattr *nla; 3338 3339 nla = nla_find(nlmsg_attrdata(nlh, hdrlen), nlmsg_attrlen(nlh, hdrlen), 3340 DRBD_NLA_CFG_CONTEXT); 3341 if (!nla) 3342 return NULL; 3343 return nla_find_nested(nla, attr); 3344 } 3345 3346 static void resource_to_info(struct resource_info *, struct drbd_resource *); 3347 3348 int drbd_nl_get_resources_dumpit(struct sk_buff *skb, struct netlink_callback *cb) 3349 { 3350 struct drbd_genlmsghdr *dh; 3351 struct drbd_resource *resource; 3352 struct resource_info resource_info; 3353 struct resource_statistics resource_statistics; 3354 int err; 3355 3356 rcu_read_lock(); 3357 if (cb->args[0]) { 3358 for_each_resource_rcu(resource, &drbd_resources) 3359 if (resource == (struct drbd_resource *)cb->args[0]) 3360 goto found_resource; 3361 err = 0; /* resource was probably deleted */ 3362 goto out; 3363 } 3364 resource = list_entry(&drbd_resources, 3365 struct drbd_resource, resources); 3366 3367 found_resource: 3368 list_for_each_entry_continue_rcu(resource, &drbd_resources, resources) { 3369 goto put_result; 3370 } 3371 err = 0; 3372 goto out; 3373 3374 put_result: 3375 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, 3376 cb->nlh->nlmsg_seq, &drbd_nl_family, 3377 NLM_F_MULTI, DRBD_ADM_GET_RESOURCES); 3378 err = -ENOMEM; 3379 if (!dh) 3380 goto out; 3381 dh->minor = -1U; 3382 dh->ret_code = NO_ERROR; 3383 err = nla_put_drbd_cfg_context(skb, resource, NULL, NULL); 3384 if (err) 3385 goto out; 3386 err = res_opts_to_skb(skb, &resource->res_opts); 3387 if (err) 3388 goto out; 3389 resource_to_info(&resource_info, resource); 3390 err = resource_info_to_skb(skb, &resource_info); 3391 if (err) 3392 goto out; 3393 resource_statistics.res_stat_write_ordering = resource->write_ordering; 3394 err = resource_statistics_to_skb(skb, &resource_statistics); 3395 if (err) 3396 goto out; 3397 cb->args[0] = (long)resource; 3398 genlmsg_end(skb, dh); 3399 err = 0; 3400 3401 out: 3402 rcu_read_unlock(); 3403 if (err) 3404 return err; 3405 return skb->len; 3406 } 3407 3408 static void device_to_statistics(struct device_statistics *s, 3409 struct drbd_device *device) 3410 { 3411 memset(s, 0, sizeof(*s)); 3412 s->dev_upper_blocked = !may_inc_ap_bio(device); 3413 if (get_ldev(device)) { 3414 struct drbd_md *md = &device->ldev->md; 3415 u64 *history_uuids = (u64 *)s->history_uuids; 3416 int n; 3417 3418 spin_lock_irq(&md->uuid_lock); 3419 s->dev_current_uuid = md->uuid[UI_CURRENT]; 3420 BUILD_BUG_ON(sizeof(s->history_uuids) < UI_HISTORY_END - UI_HISTORY_START + 1); 3421 for (n = 0; n < UI_HISTORY_END - UI_HISTORY_START + 1; n++) 3422 history_uuids[n] = md->uuid[UI_HISTORY_START + n]; 3423 for (; n < HISTORY_UUIDS; n++) 3424 history_uuids[n] = 0; 3425 s->history_uuids_len = HISTORY_UUIDS; 3426 spin_unlock_irq(&md->uuid_lock); 3427 3428 s->dev_disk_flags = md->flags; 3429 put_ldev(device); 3430 } 3431 s->dev_size = get_capacity(device->vdisk); 3432 s->dev_read = device->read_cnt; 3433 s->dev_write = device->writ_cnt; 3434 s->dev_al_writes = device->al_writ_cnt; 3435 s->dev_bm_writes = device->bm_writ_cnt; 3436 s->dev_upper_pending = atomic_read(&device->ap_bio_cnt); 3437 s->dev_lower_pending = atomic_read(&device->local_cnt); 3438 s->dev_al_suspended = test_bit(AL_SUSPENDED, &device->flags); 3439 s->dev_exposed_data_uuid = device->ed_uuid; 3440 } 3441 3442 static int put_resource_in_arg0(struct netlink_callback *cb, int holder_nr) 3443 { 3444 if (cb->args[0]) { 3445 struct drbd_resource *resource = 3446 (struct drbd_resource *)cb->args[0]; 3447 kref_put(&resource->kref, drbd_destroy_resource); 3448 } 3449 3450 return 0; 3451 } 3452 3453 int drbd_adm_dump_devices_done(struct netlink_callback *cb) { 3454 return put_resource_in_arg0(cb, 7); 3455 } 3456 3457 static void device_to_info(struct device_info *, struct drbd_device *); 3458 3459 int drbd_nl_get_devices_dumpit(struct sk_buff *skb, struct netlink_callback *cb) 3460 { 3461 struct nlattr *resource_filter; 3462 struct drbd_resource *resource; 3463 struct drbd_device *device; 3464 int minor, err, retcode; 3465 struct drbd_genlmsghdr *dh; 3466 struct device_info device_info; 3467 struct device_statistics device_statistics; 3468 struct idr *idr_to_search; 3469 3470 resource = (struct drbd_resource *)cb->args[0]; 3471 if (!cb->args[0] && !cb->args[1]) { 3472 resource_filter = find_cfg_context_attr(cb->nlh, 3473 DRBD_A_DRBD_CFG_CONTEXT_CTX_RESOURCE_NAME); 3474 if (resource_filter) { 3475 retcode = ERR_RES_NOT_KNOWN; 3476 resource = drbd_find_resource(nla_data(resource_filter)); 3477 if (!resource) { 3478 rcu_read_lock(); 3479 goto put_result; 3480 } 3481 cb->args[0] = (long)resource; 3482 } 3483 } 3484 3485 rcu_read_lock(); 3486 minor = cb->args[1]; 3487 idr_to_search = resource ? &resource->devices : &drbd_devices; 3488 device = idr_get_next(idr_to_search, &minor); 3489 if (!device) { 3490 err = 0; 3491 goto out; 3492 } 3493 idr_for_each_entry_continue(idr_to_search, device, minor) { 3494 retcode = NO_ERROR; 3495 goto put_result; /* only one iteration */ 3496 } 3497 err = 0; 3498 goto out; /* no more devices */ 3499 3500 put_result: 3501 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, 3502 cb->nlh->nlmsg_seq, &drbd_nl_family, 3503 NLM_F_MULTI, DRBD_ADM_GET_DEVICES); 3504 err = -ENOMEM; 3505 if (!dh) 3506 goto out; 3507 dh->ret_code = retcode; 3508 dh->minor = -1U; 3509 if (retcode == NO_ERROR) { 3510 dh->minor = device->minor; 3511 err = nla_put_drbd_cfg_context(skb, device->resource, NULL, device); 3512 if (err) 3513 goto out; 3514 if (get_ldev(device)) { 3515 struct disk_conf *disk_conf = 3516 rcu_dereference(device->ldev->disk_conf); 3517 3518 err = disk_conf_to_skb(skb, disk_conf); 3519 put_ldev(device); 3520 if (err) 3521 goto out; 3522 } 3523 device_to_info(&device_info, device); 3524 err = device_info_to_skb(skb, &device_info); 3525 if (err) 3526 goto out; 3527 3528 device_to_statistics(&device_statistics, device); 3529 err = device_statistics_to_skb(skb, &device_statistics); 3530 if (err) 3531 goto out; 3532 cb->args[1] = minor + 1; 3533 } 3534 genlmsg_end(skb, dh); 3535 err = 0; 3536 3537 out: 3538 rcu_read_unlock(); 3539 if (err) 3540 return err; 3541 return skb->len; 3542 } 3543 3544 int drbd_adm_dump_connections_done(struct netlink_callback *cb) 3545 { 3546 return put_resource_in_arg0(cb, 6); 3547 } 3548 3549 enum { SINGLE_RESOURCE, ITERATE_RESOURCES }; 3550 3551 int drbd_nl_get_connections_dumpit(struct sk_buff *skb, struct netlink_callback *cb) 3552 { 3553 struct nlattr *resource_filter; 3554 struct drbd_resource *resource = NULL, *next_resource; 3555 struct drbd_connection *connection; 3556 int err = 0, retcode; 3557 struct drbd_genlmsghdr *dh; 3558 struct connection_info connection_info; 3559 struct connection_statistics connection_statistics; 3560 3561 rcu_read_lock(); 3562 resource = (struct drbd_resource *)cb->args[0]; 3563 if (!cb->args[0]) { 3564 resource_filter = find_cfg_context_attr(cb->nlh, 3565 DRBD_A_DRBD_CFG_CONTEXT_CTX_RESOURCE_NAME); 3566 if (resource_filter) { 3567 retcode = ERR_RES_NOT_KNOWN; 3568 resource = drbd_find_resource(nla_data(resource_filter)); 3569 if (!resource) 3570 goto put_result; 3571 cb->args[0] = (long)resource; 3572 cb->args[1] = SINGLE_RESOURCE; 3573 } 3574 } 3575 if (!resource) { 3576 if (list_empty(&drbd_resources)) 3577 goto out; 3578 resource = list_first_entry(&drbd_resources, struct drbd_resource, resources); 3579 kref_get(&resource->kref); 3580 cb->args[0] = (long)resource; 3581 cb->args[1] = ITERATE_RESOURCES; 3582 } 3583 3584 next_resource: 3585 rcu_read_unlock(); 3586 mutex_lock(&resource->conf_update); 3587 rcu_read_lock(); 3588 if (cb->args[2]) { 3589 for_each_connection_rcu(connection, resource) 3590 if (connection == (struct drbd_connection *)cb->args[2]) 3591 goto found_connection; 3592 /* connection was probably deleted */ 3593 goto no_more_connections; 3594 } 3595 connection = list_entry(&resource->connections, struct drbd_connection, connections); 3596 3597 found_connection: 3598 list_for_each_entry_continue_rcu(connection, &resource->connections, connections) { 3599 if (!has_net_conf(connection)) 3600 continue; 3601 retcode = NO_ERROR; 3602 goto put_result; /* only one iteration */ 3603 } 3604 3605 no_more_connections: 3606 if (cb->args[1] == ITERATE_RESOURCES) { 3607 for_each_resource_rcu(next_resource, &drbd_resources) { 3608 if (next_resource == resource) 3609 goto found_resource; 3610 } 3611 /* resource was probably deleted */ 3612 } 3613 goto out; 3614 3615 found_resource: 3616 list_for_each_entry_continue_rcu(next_resource, &drbd_resources, resources) { 3617 mutex_unlock(&resource->conf_update); 3618 kref_put(&resource->kref, drbd_destroy_resource); 3619 resource = next_resource; 3620 kref_get(&resource->kref); 3621 cb->args[0] = (long)resource; 3622 cb->args[2] = 0; 3623 goto next_resource; 3624 } 3625 goto out; /* no more resources */ 3626 3627 put_result: 3628 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, 3629 cb->nlh->nlmsg_seq, &drbd_nl_family, 3630 NLM_F_MULTI, DRBD_ADM_GET_CONNECTIONS); 3631 err = -ENOMEM; 3632 if (!dh) 3633 goto out; 3634 dh->ret_code = retcode; 3635 dh->minor = -1U; 3636 if (retcode == NO_ERROR) { 3637 struct net_conf *net_conf; 3638 3639 err = nla_put_drbd_cfg_context(skb, resource, connection, NULL); 3640 if (err) 3641 goto out; 3642 net_conf = rcu_dereference(connection->net_conf); 3643 if (net_conf) { 3644 err = net_conf_to_skb_sanitized(skb, net_conf, 3645 !capable(CAP_SYS_ADMIN)); 3646 if (err) 3647 goto out; 3648 } 3649 connection_to_info(&connection_info, connection); 3650 err = connection_info_to_skb(skb, &connection_info); 3651 if (err) 3652 goto out; 3653 connection_statistics.conn_congested = test_bit(NET_CONGESTED, &connection->flags); 3654 err = connection_statistics_to_skb(skb, &connection_statistics); 3655 if (err) 3656 goto out; 3657 cb->args[2] = (long)connection; 3658 } 3659 genlmsg_end(skb, dh); 3660 err = 0; 3661 3662 out: 3663 rcu_read_unlock(); 3664 if (resource) 3665 mutex_unlock(&resource->conf_update); 3666 if (err) 3667 return err; 3668 return skb->len; 3669 } 3670 3671 enum mdf_peer_flag { 3672 MDF_PEER_CONNECTED = 1 << 0, 3673 MDF_PEER_OUTDATED = 1 << 1, 3674 MDF_PEER_FENCING = 1 << 2, 3675 MDF_PEER_FULL_SYNC = 1 << 3, 3676 }; 3677 3678 static void peer_device_to_statistics(struct peer_device_statistics *s, 3679 struct drbd_peer_device *peer_device) 3680 { 3681 struct drbd_device *device = peer_device->device; 3682 3683 memset(s, 0, sizeof(*s)); 3684 s->peer_dev_received = device->recv_cnt; 3685 s->peer_dev_sent = device->send_cnt; 3686 s->peer_dev_pending = atomic_read(&device->ap_pending_cnt) + 3687 atomic_read(&device->rs_pending_cnt); 3688 s->peer_dev_unacked = atomic_read(&device->unacked_cnt); 3689 s->peer_dev_out_of_sync = drbd_bm_total_weight(device) << (BM_BLOCK_SHIFT - 9); 3690 s->peer_dev_resync_failed = device->rs_failed << (BM_BLOCK_SHIFT - 9); 3691 if (get_ldev(device)) { 3692 struct drbd_md *md = &device->ldev->md; 3693 3694 spin_lock_irq(&md->uuid_lock); 3695 s->peer_dev_bitmap_uuid = md->uuid[UI_BITMAP]; 3696 spin_unlock_irq(&md->uuid_lock); 3697 s->peer_dev_flags = 3698 (drbd_md_test_flag(device->ldev, MDF_CONNECTED_IND) ? 3699 MDF_PEER_CONNECTED : 0) + 3700 (drbd_md_test_flag(device->ldev, MDF_CONSISTENT) && 3701 !drbd_md_test_flag(device->ldev, MDF_WAS_UP_TO_DATE) ? 3702 MDF_PEER_OUTDATED : 0) + 3703 /* FIXME: MDF_PEER_FENCING? */ 3704 (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC) ? 3705 MDF_PEER_FULL_SYNC : 0); 3706 put_ldev(device); 3707 } 3708 } 3709 3710 int drbd_adm_dump_peer_devices_done(struct netlink_callback *cb) 3711 { 3712 return put_resource_in_arg0(cb, 9); 3713 } 3714 3715 int drbd_nl_get_peer_devices_dumpit(struct sk_buff *skb, struct netlink_callback *cb) 3716 { 3717 struct nlattr *resource_filter; 3718 struct drbd_resource *resource; 3719 struct drbd_device *device; 3720 struct drbd_peer_device *peer_device = NULL; 3721 int minor, err, retcode; 3722 struct drbd_genlmsghdr *dh; 3723 struct idr *idr_to_search; 3724 3725 resource = (struct drbd_resource *)cb->args[0]; 3726 if (!cb->args[0] && !cb->args[1]) { 3727 resource_filter = find_cfg_context_attr(cb->nlh, 3728 DRBD_A_DRBD_CFG_CONTEXT_CTX_RESOURCE_NAME); 3729 if (resource_filter) { 3730 retcode = ERR_RES_NOT_KNOWN; 3731 resource = drbd_find_resource(nla_data(resource_filter)); 3732 if (!resource) { 3733 rcu_read_lock(); 3734 goto put_result; 3735 } 3736 } 3737 cb->args[0] = (long)resource; 3738 } 3739 3740 rcu_read_lock(); 3741 minor = cb->args[1]; 3742 idr_to_search = resource ? &resource->devices : &drbd_devices; 3743 device = idr_find(idr_to_search, minor); 3744 if (!device) { 3745 next_device: 3746 minor++; 3747 cb->args[2] = 0; 3748 device = idr_get_next(idr_to_search, &minor); 3749 if (!device) { 3750 err = 0; 3751 goto out; 3752 } 3753 } 3754 if (cb->args[2]) { 3755 for_each_peer_device(peer_device, device) 3756 if (peer_device == (struct drbd_peer_device *)cb->args[2]) 3757 goto found_peer_device; 3758 /* peer device was probably deleted */ 3759 goto next_device; 3760 } 3761 /* Make peer_device point to the list head (not the first entry). */ 3762 peer_device = list_entry(&device->peer_devices, struct drbd_peer_device, peer_devices); 3763 3764 found_peer_device: 3765 list_for_each_entry_continue_rcu(peer_device, &device->peer_devices, peer_devices) { 3766 if (!has_net_conf(peer_device->connection)) 3767 continue; 3768 retcode = NO_ERROR; 3769 goto put_result; /* only one iteration */ 3770 } 3771 goto next_device; 3772 3773 put_result: 3774 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, 3775 cb->nlh->nlmsg_seq, &drbd_nl_family, 3776 NLM_F_MULTI, DRBD_ADM_GET_PEER_DEVICES); 3777 err = -ENOMEM; 3778 if (!dh) 3779 goto out; 3780 dh->ret_code = retcode; 3781 dh->minor = -1U; 3782 if (retcode == NO_ERROR) { 3783 struct peer_device_info peer_device_info; 3784 struct peer_device_statistics peer_device_statistics; 3785 3786 dh->minor = minor; 3787 err = nla_put_drbd_cfg_context(skb, device->resource, peer_device->connection, device); 3788 if (err) 3789 goto out; 3790 peer_device_to_info(&peer_device_info, peer_device); 3791 err = peer_device_info_to_skb(skb, &peer_device_info); 3792 if (err) 3793 goto out; 3794 peer_device_to_statistics(&peer_device_statistics, peer_device); 3795 err = peer_device_statistics_to_skb(skb, &peer_device_statistics); 3796 if (err) 3797 goto out; 3798 cb->args[1] = minor; 3799 cb->args[2] = (long)peer_device; 3800 } 3801 genlmsg_end(skb, dh); 3802 err = 0; 3803 3804 out: 3805 rcu_read_unlock(); 3806 if (err) 3807 return err; 3808 return skb->len; 3809 } 3810 /* 3811 * Return the connection of @resource if @resource has exactly one connection. 3812 */ 3813 static struct drbd_connection *the_only_connection(struct drbd_resource *resource) 3814 { 3815 struct list_head *connections = &resource->connections; 3816 3817 if (list_empty(connections) || connections->next->next != connections) 3818 return NULL; 3819 return list_first_entry(&resource->connections, struct drbd_connection, connections); 3820 } 3821 3822 static int nla_put_status_info(struct sk_buff *skb, struct drbd_device *device, 3823 const struct sib_info *sib) 3824 { 3825 struct drbd_resource *resource = device->resource; 3826 struct state_info *si = NULL; /* for sizeof(si->member); */ 3827 struct nlattr *nla; 3828 int got_ldev; 3829 int err = 0; 3830 int exclude_sensitive; 3831 3832 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen 3833 * to. So we better exclude_sensitive information. 3834 * 3835 * If sib == NULL, this is drbd_nl_get_status_doit, executed synchronously 3836 * in the context of the requesting user process. Exclude sensitive 3837 * information, unless current has superuser. 3838 * 3839 * NOTE: for drbd_nl_get_status_dumpit(), this is a netlink dump, and 3840 * relies on the current implementation of netlink_dump(), which 3841 * executes the dump callback successively from netlink_recvmsg(), 3842 * always in the context of the receiving process */ 3843 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN); 3844 3845 got_ldev = get_ldev(device); 3846 3847 /* We need to add connection name and volume number information still. 3848 * Minor number is in drbd_genlmsghdr. */ 3849 if (nla_put_drbd_cfg_context(skb, resource, the_only_connection(resource), device)) 3850 goto nla_put_failure; 3851 3852 if (res_opts_to_skb(skb, &device->resource->res_opts)) 3853 goto nla_put_failure; 3854 3855 rcu_read_lock(); 3856 if (got_ldev) { 3857 struct disk_conf *disk_conf; 3858 3859 disk_conf = rcu_dereference(device->ldev->disk_conf); 3860 err = disk_conf_to_skb(skb, disk_conf); 3861 } 3862 if (!err) { 3863 struct net_conf *nc; 3864 3865 nc = rcu_dereference(first_peer_device(device)->connection->net_conf); 3866 if (nc) 3867 err = net_conf_to_skb_sanitized(skb, nc, exclude_sensitive); 3868 } 3869 rcu_read_unlock(); 3870 if (err) 3871 goto nla_put_failure; 3872 3873 nla = nla_nest_start_noflag(skb, DRBD_NLA_STATE_INFO); 3874 if (!nla) 3875 goto nla_put_failure; 3876 if (nla_put_u32(skb, DRBD_A_STATE_INFO_SIB_REASON, 3877 sib ? sib->sib_reason : SIB_GET_STATUS_REPLY) || 3878 nla_put_u32(skb, DRBD_A_STATE_INFO_CURRENT_STATE, 3879 device->state.i) || 3880 nla_put_u64_64bit(skb, DRBD_A_STATE_INFO_ED_UUID, 3881 device->ed_uuid, 0) || 3882 nla_put_u64_64bit(skb, DRBD_A_STATE_INFO_CAPACITY, 3883 get_capacity(device->vdisk), 0) || 3884 nla_put_u64_64bit(skb, DRBD_A_STATE_INFO_SEND_CNT, 3885 device->send_cnt, 0) || 3886 nla_put_u64_64bit(skb, DRBD_A_STATE_INFO_RECV_CNT, 3887 device->recv_cnt, 0) || 3888 nla_put_u64_64bit(skb, DRBD_A_STATE_INFO_READ_CNT, 3889 device->read_cnt, 0) || 3890 nla_put_u64_64bit(skb, DRBD_A_STATE_INFO_WRIT_CNT, 3891 device->writ_cnt, 0) || 3892 nla_put_u64_64bit(skb, DRBD_A_STATE_INFO_AL_WRIT_CNT, 3893 device->al_writ_cnt, 0) || 3894 nla_put_u64_64bit(skb, DRBD_A_STATE_INFO_BM_WRIT_CNT, 3895 device->bm_writ_cnt, 0) || 3896 nla_put_u32(skb, DRBD_A_STATE_INFO_AP_BIO_CNT, 3897 atomic_read(&device->ap_bio_cnt)) || 3898 nla_put_u32(skb, DRBD_A_STATE_INFO_AP_PENDING_CNT, 3899 atomic_read(&device->ap_pending_cnt)) || 3900 nla_put_u32(skb, DRBD_A_STATE_INFO_RS_PENDING_CNT, 3901 atomic_read(&device->rs_pending_cnt))) 3902 goto nla_put_failure; 3903 3904 if (got_ldev) { 3905 int err; 3906 3907 spin_lock_irq(&device->ldev->md.uuid_lock); 3908 err = nla_put(skb, DRBD_A_STATE_INFO_UUIDS, 3909 sizeof(si->uuids), 3910 device->ldev->md.uuid); 3911 spin_unlock_irq(&device->ldev->md.uuid_lock); 3912 3913 if (err) 3914 goto nla_put_failure; 3915 3916 if (nla_put_u32(skb, DRBD_A_STATE_INFO_DISK_FLAGS, device->ldev->md.flags) || 3917 nla_put_u64_64bit(skb, DRBD_A_STATE_INFO_BITS_TOTAL, drbd_bm_bits(device), 0) || 3918 nla_put_u64_64bit(skb, DRBD_A_STATE_INFO_BITS_OOS, 3919 drbd_bm_total_weight(device), 0)) 3920 goto nla_put_failure; 3921 if (C_SYNC_SOURCE <= device->state.conn && 3922 C_PAUSED_SYNC_T >= device->state.conn) { 3923 if (nla_put_u64_64bit(skb, DRBD_A_STATE_INFO_BITS_RS_TOTAL, 3924 device->rs_total, 0) || 3925 nla_put_u64_64bit(skb, DRBD_A_STATE_INFO_BITS_RS_FAILED, 3926 device->rs_failed, 0)) 3927 goto nla_put_failure; 3928 } 3929 } 3930 3931 if (sib) { 3932 switch(sib->sib_reason) { 3933 case SIB_SYNC_PROGRESS: 3934 case SIB_GET_STATUS_REPLY: 3935 break; 3936 case SIB_STATE_CHANGE: 3937 if (nla_put_u32(skb, DRBD_A_STATE_INFO_PREV_STATE, sib->os.i) || 3938 nla_put_u32(skb, DRBD_A_STATE_INFO_NEW_STATE, sib->ns.i)) 3939 goto nla_put_failure; 3940 break; 3941 case SIB_HELPER_POST: 3942 if (nla_put_u32(skb, DRBD_A_STATE_INFO_HELPER_EXIT_CODE, 3943 sib->helper_exit_code)) 3944 goto nla_put_failure; 3945 fallthrough; 3946 case SIB_HELPER_PRE: 3947 if (nla_put_string(skb, DRBD_A_STATE_INFO_HELPER, sib->helper_name)) 3948 goto nla_put_failure; 3949 break; 3950 } 3951 } 3952 nla_nest_end(skb, nla); 3953 3954 if (0) 3955 nla_put_failure: 3956 err = -EMSGSIZE; 3957 if (got_ldev) 3958 put_ldev(device); 3959 return err; 3960 } 3961 3962 int drbd_nl_get_status_doit(struct sk_buff *skb, struct genl_info *info) 3963 { 3964 struct drbd_config_context *adm_ctx = info->user_ptr[0]; 3965 enum drbd_ret_code retcode; 3966 int err; 3967 3968 if (!adm_ctx->reply_skb) 3969 return 0; 3970 retcode = adm_ctx->reply_dh->ret_code; 3971 if (retcode != NO_ERROR) 3972 goto out; 3973 3974 err = nla_put_status_info(adm_ctx->reply_skb, adm_ctx->device, NULL); 3975 if (err) { 3976 nlmsg_free(adm_ctx->reply_skb); 3977 adm_ctx->reply_skb = NULL; 3978 return err; 3979 } 3980 out: 3981 adm_ctx->reply_dh->ret_code = retcode; 3982 return 0; 3983 } 3984 3985 static int get_one_status(struct sk_buff *skb, struct netlink_callback *cb) 3986 { 3987 struct drbd_device *device; 3988 struct drbd_genlmsghdr *dh; 3989 struct drbd_resource *pos = (struct drbd_resource *)cb->args[0]; 3990 struct drbd_resource *resource = NULL; 3991 struct drbd_resource *tmp; 3992 unsigned volume = cb->args[1]; 3993 3994 /* Open coded, deferred, iteration: 3995 * for_each_resource_safe(resource, tmp, &drbd_resources) { 3996 * connection = "first connection of resource or undefined"; 3997 * idr_for_each_entry(&resource->devices, device, i) { 3998 * ... 3999 * } 4000 * } 4001 * where resource is cb->args[0]; 4002 * and i is cb->args[1]; 4003 * 4004 * cb->args[2] indicates if we shall loop over all resources, 4005 * or just dump all volumes of a single resource. 4006 * 4007 * This may miss entries inserted after this dump started, 4008 * or entries deleted before they are reached. 4009 * 4010 * We need to make sure the device won't disappear while 4011 * we are looking at it, and revalidate our iterators 4012 * on each iteration. 4013 */ 4014 4015 /* synchronize with conn_create()/drbd_destroy_connection() */ 4016 rcu_read_lock(); 4017 /* revalidate iterator position */ 4018 for_each_resource_rcu(tmp, &drbd_resources) { 4019 if (pos == NULL) { 4020 /* first iteration */ 4021 pos = tmp; 4022 resource = pos; 4023 break; 4024 } 4025 if (tmp == pos) { 4026 resource = pos; 4027 break; 4028 } 4029 } 4030 if (resource) { 4031 next_resource: 4032 device = idr_get_next(&resource->devices, &volume); 4033 if (!device) { 4034 /* No more volumes to dump on this resource. 4035 * Advance resource iterator. */ 4036 pos = list_entry_rcu(resource->resources.next, 4037 struct drbd_resource, resources); 4038 /* Did we dump any volume of this resource yet? */ 4039 if (volume != 0) { 4040 /* If we reached the end of the list, 4041 * or only a single resource dump was requested, 4042 * we are done. */ 4043 if (&pos->resources == &drbd_resources || cb->args[2]) 4044 goto out; 4045 volume = 0; 4046 resource = pos; 4047 goto next_resource; 4048 } 4049 } 4050 4051 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, 4052 cb->nlh->nlmsg_seq, &drbd_nl_family, 4053 NLM_F_MULTI, DRBD_ADM_GET_STATUS); 4054 if (!dh) 4055 goto out; 4056 4057 if (!device) { 4058 /* This is a connection without a single volume. 4059 * Suprisingly enough, it may have a network 4060 * configuration. */ 4061 struct drbd_connection *connection; 4062 4063 dh->minor = -1U; 4064 dh->ret_code = NO_ERROR; 4065 connection = the_only_connection(resource); 4066 if (nla_put_drbd_cfg_context(skb, resource, connection, NULL)) 4067 goto cancel; 4068 if (connection) { 4069 struct net_conf *nc; 4070 4071 nc = rcu_dereference(connection->net_conf); 4072 if (nc && net_conf_to_skb_sanitized(skb, nc, true) != 0) 4073 goto cancel; 4074 } 4075 goto done; 4076 } 4077 4078 D_ASSERT(device, device->vnr == volume); 4079 D_ASSERT(device, device->resource == resource); 4080 4081 dh->minor = device_to_minor(device); 4082 dh->ret_code = NO_ERROR; 4083 4084 if (nla_put_status_info(skb, device, NULL)) { 4085 cancel: 4086 genlmsg_cancel(skb, dh); 4087 goto out; 4088 } 4089 done: 4090 genlmsg_end(skb, dh); 4091 } 4092 4093 out: 4094 rcu_read_unlock(); 4095 /* where to start the next iteration */ 4096 cb->args[0] = (long)pos; 4097 cb->args[1] = (pos == resource) ? volume + 1 : 0; 4098 4099 /* No more resources/volumes/minors found results in an empty skb. 4100 * Which will terminate the dump. */ 4101 return skb->len; 4102 } 4103 4104 /* 4105 * Request status of all resources, or of all volumes within a single resource. 4106 * 4107 * This is a dump, as the answer may not fit in a single reply skb otherwise. 4108 * Which means we cannot use the family->attrbuf or other such members, because 4109 * dump is NOT protected by the genl_lock(). During dump, we only have access 4110 * to the incoming skb, and need to opencode "parsing" of the nlattr payload. 4111 * 4112 * Once things are setup properly, we call into get_one_status(). 4113 */ 4114 int drbd_nl_get_status_dumpit(struct sk_buff *skb, struct netlink_callback *cb) 4115 { 4116 const unsigned int hdrlen = GENL_HDRLEN + sizeof(struct drbd_genlmsghdr); 4117 struct nlattr *nla; 4118 const char *resource_name; 4119 struct drbd_resource *resource; 4120 4121 /* Is this a followup call? */ 4122 if (cb->args[0]) { 4123 /* ... of a single resource dump, 4124 * and the resource iterator has been advanced already? */ 4125 if (cb->args[2] && cb->args[2] != cb->args[0]) 4126 return 0; /* DONE. */ 4127 goto dump; 4128 } 4129 4130 /* First call (from netlink_dump_start). We need to figure out 4131 * which resource(s) the user wants us to dump. */ 4132 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen), 4133 nlmsg_attrlen(cb->nlh, hdrlen), 4134 DRBD_NLA_CFG_CONTEXT); 4135 4136 /* No explicit context given. Dump all. */ 4137 if (!nla) 4138 goto dump; 4139 nla = nla_find_nested(nla, DRBD_A_DRBD_CFG_CONTEXT_CTX_RESOURCE_NAME); 4140 /* context given, but no name present? */ 4141 if (!nla) 4142 return -EINVAL; 4143 resource_name = nla_data(nla); 4144 if (!*resource_name) 4145 return -ENODEV; 4146 resource = drbd_find_resource(resource_name); 4147 if (!resource) 4148 return -ENODEV; 4149 4150 kref_put(&resource->kref, drbd_destroy_resource); /* get_one_status() revalidates the resource */ 4151 4152 /* prime iterators, and set "filter" mode mark: 4153 * only dump this connection. */ 4154 cb->args[0] = (long)resource; 4155 /* cb->args[1] = 0; passed in this way. */ 4156 cb->args[2] = (long)resource; 4157 4158 dump: 4159 return get_one_status(skb, cb); 4160 } 4161 4162 int drbd_nl_get_timeout_type_doit(struct sk_buff *skb, struct genl_info *info) 4163 { 4164 struct drbd_config_context *adm_ctx = info->user_ptr[0]; 4165 enum drbd_ret_code retcode; 4166 struct timeout_parms tp; 4167 int err; 4168 4169 if (!adm_ctx->reply_skb) 4170 return 0; 4171 retcode = adm_ctx->reply_dh->ret_code; 4172 if (retcode != NO_ERROR) 4173 goto out; 4174 4175 tp.timeout_type = 4176 adm_ctx->device->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED : 4177 test_bit(USE_DEGR_WFC_T, &adm_ctx->device->flags) ? UT_DEGRADED : 4178 UT_DEFAULT; 4179 4180 err = timeout_parms_to_skb(adm_ctx->reply_skb, &tp); 4181 if (err) { 4182 nlmsg_free(adm_ctx->reply_skb); 4183 adm_ctx->reply_skb = NULL; 4184 return err; 4185 } 4186 out: 4187 adm_ctx->reply_dh->ret_code = retcode; 4188 return 0; 4189 } 4190 4191 int drbd_nl_start_ov_doit(struct sk_buff *skb, struct genl_info *info) 4192 { 4193 struct drbd_config_context *adm_ctx = info->user_ptr[0]; 4194 struct drbd_device *device; 4195 enum drbd_ret_code retcode; 4196 struct start_ov_parms parms; 4197 4198 if (!adm_ctx->reply_skb) 4199 return 0; 4200 retcode = adm_ctx->reply_dh->ret_code; 4201 if (retcode != NO_ERROR) 4202 goto out; 4203 4204 device = adm_ctx->device; 4205 4206 /* resume from last known position, if possible */ 4207 parms.ov_start_sector = device->ov_start_sector; 4208 parms.ov_stop_sector = ULLONG_MAX; 4209 if (info->attrs[DRBD_NLA_START_OV_PARMS]) { 4210 int err = start_ov_parms_from_attrs(&parms, info); 4211 if (err) { 4212 retcode = ERR_MANDATORY_TAG; 4213 drbd_msg_put_info(adm_ctx->reply_skb, from_attrs_err_to_txt(err)); 4214 goto out; 4215 } 4216 } 4217 mutex_lock(&adm_ctx->resource->adm_mutex); 4218 4219 /* w_make_ov_request expects position to be aligned */ 4220 device->ov_start_sector = parms.ov_start_sector & ~(BM_SECT_PER_BIT-1); 4221 device->ov_stop_sector = parms.ov_stop_sector; 4222 4223 /* If there is still bitmap IO pending, e.g. previous resync or verify 4224 * just being finished, wait for it before requesting a new resync. */ 4225 drbd_suspend_io(device); 4226 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags)); 4227 retcode = drbd_request_state(device, NS(conn, C_VERIFY_S)); 4228 drbd_resume_io(device); 4229 4230 mutex_unlock(&adm_ctx->resource->adm_mutex); 4231 out: 4232 adm_ctx->reply_dh->ret_code = retcode; 4233 return 0; 4234 } 4235 4236 4237 int drbd_nl_new_c_uuid_doit(struct sk_buff *skb, struct genl_info *info) 4238 { 4239 struct drbd_config_context *adm_ctx = info->user_ptr[0]; 4240 struct drbd_device *device; 4241 enum drbd_ret_code retcode; 4242 int skip_initial_sync = 0; 4243 int err; 4244 struct new_c_uuid_parms args; 4245 4246 if (!adm_ctx->reply_skb) 4247 return 0; 4248 retcode = adm_ctx->reply_dh->ret_code; 4249 if (retcode != NO_ERROR) 4250 goto out_nolock; 4251 4252 device = adm_ctx->device; 4253 memset(&args, 0, sizeof(args)); 4254 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) { 4255 err = new_c_uuid_parms_from_attrs(&args, info); 4256 if (err) { 4257 retcode = ERR_MANDATORY_TAG; 4258 drbd_msg_put_info(adm_ctx->reply_skb, from_attrs_err_to_txt(err)); 4259 goto out_nolock; 4260 } 4261 } 4262 4263 mutex_lock(&adm_ctx->resource->adm_mutex); 4264 mutex_lock(device->state_mutex); /* Protects us against serialized state changes. */ 4265 4266 if (!get_ldev(device)) { 4267 retcode = ERR_NO_DISK; 4268 goto out; 4269 } 4270 4271 /* this is "skip initial sync", assume to be clean */ 4272 if (device->state.conn == C_CONNECTED && 4273 first_peer_device(device)->connection->agreed_pro_version >= 90 && 4274 device->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) { 4275 drbd_info(device, "Preparing to skip initial sync\n"); 4276 skip_initial_sync = 1; 4277 } else if (device->state.conn != C_STANDALONE) { 4278 retcode = ERR_CONNECTED; 4279 goto out_dec; 4280 } 4281 4282 drbd_uuid_set(device, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */ 4283 drbd_uuid_new_current(device); /* New current, previous to UI_BITMAP */ 4284 4285 if (args.clear_bm) { 4286 err = drbd_bitmap_io(device, &drbd_bmio_clear_n_write, 4287 "clear_n_write from new_c_uuid", BM_LOCKED_MASK, NULL); 4288 if (err) { 4289 drbd_err(device, "Writing bitmap failed with %d\n", err); 4290 retcode = ERR_IO_MD_DISK; 4291 } 4292 if (skip_initial_sync) { 4293 drbd_send_uuids_skip_initial_sync(first_peer_device(device)); 4294 _drbd_uuid_set(device, UI_BITMAP, 0); 4295 drbd_print_uuids(device, "cleared bitmap UUID"); 4296 spin_lock_irq(&device->resource->req_lock); 4297 _drbd_set_state(_NS2(device, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE), 4298 CS_VERBOSE, NULL); 4299 spin_unlock_irq(&device->resource->req_lock); 4300 } 4301 } 4302 4303 drbd_md_sync(device); 4304 out_dec: 4305 put_ldev(device); 4306 out: 4307 mutex_unlock(device->state_mutex); 4308 mutex_unlock(&adm_ctx->resource->adm_mutex); 4309 out_nolock: 4310 adm_ctx->reply_dh->ret_code = retcode; 4311 return 0; 4312 } 4313 4314 static enum drbd_ret_code 4315 drbd_check_resource_name(struct drbd_config_context *adm_ctx) 4316 { 4317 const char *name = adm_ctx->resource_name; 4318 if (!name || !name[0]) { 4319 drbd_msg_put_info(adm_ctx->reply_skb, "resource name missing"); 4320 return ERR_MANDATORY_TAG; 4321 } 4322 /* if we want to use these in sysfs/configfs/debugfs some day, 4323 * we must not allow slashes */ 4324 if (strchr(name, '/')) { 4325 drbd_msg_put_info(adm_ctx->reply_skb, "invalid resource name"); 4326 return ERR_INVALID_REQUEST; 4327 } 4328 return NO_ERROR; 4329 } 4330 4331 static void resource_to_info(struct resource_info *info, 4332 struct drbd_resource *resource) 4333 { 4334 info->res_role = conn_highest_role(first_connection(resource)); 4335 info->res_susp = resource->susp; 4336 info->res_susp_nod = resource->susp_nod; 4337 info->res_susp_fen = resource->susp_fen; 4338 } 4339 4340 int drbd_nl_new_resource_doit(struct sk_buff *skb, struct genl_info *info) 4341 { 4342 struct drbd_connection *connection; 4343 struct drbd_config_context *adm_ctx = info->user_ptr[0]; 4344 enum drbd_ret_code retcode; 4345 struct res_opts res_opts; 4346 int err; 4347 4348 if (!adm_ctx->reply_skb) 4349 return 0; 4350 retcode = adm_ctx->reply_dh->ret_code; 4351 if (retcode != NO_ERROR) 4352 goto out; 4353 4354 set_res_opts_defaults(&res_opts); 4355 err = res_opts_from_attrs(&res_opts, info); 4356 if (err && err != -ENOMSG) { 4357 retcode = ERR_MANDATORY_TAG; 4358 drbd_msg_put_info(adm_ctx->reply_skb, from_attrs_err_to_txt(err)); 4359 goto out; 4360 } 4361 4362 retcode = drbd_check_resource_name(adm_ctx); 4363 if (retcode != NO_ERROR) 4364 goto out; 4365 4366 if (adm_ctx->resource) { 4367 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) { 4368 retcode = ERR_INVALID_REQUEST; 4369 drbd_msg_put_info(adm_ctx->reply_skb, "resource exists"); 4370 } 4371 /* else: still NO_ERROR */ 4372 goto out; 4373 } 4374 4375 /* not yet safe for genl_family.parallel_ops */ 4376 mutex_lock(&resources_mutex); 4377 connection = conn_create(adm_ctx->resource_name, &res_opts); 4378 mutex_unlock(&resources_mutex); 4379 4380 if (connection) { 4381 struct resource_info resource_info; 4382 4383 mutex_lock(¬ification_mutex); 4384 resource_to_info(&resource_info, connection->resource); 4385 notify_resource_state(NULL, 0, connection->resource, 4386 &resource_info, NOTIFY_CREATE); 4387 mutex_unlock(¬ification_mutex); 4388 } else 4389 retcode = ERR_NOMEM; 4390 4391 out: 4392 adm_ctx->reply_dh->ret_code = retcode; 4393 return 0; 4394 } 4395 4396 static void device_to_info(struct device_info *info, 4397 struct drbd_device *device) 4398 { 4399 info->dev_disk_state = device->state.disk; 4400 } 4401 4402 4403 int drbd_nl_new_minor_doit(struct sk_buff *skb, struct genl_info *info) 4404 { 4405 struct drbd_config_context *adm_ctx = info->user_ptr[0]; 4406 struct drbd_genlmsghdr *dh = genl_info_userhdr(info); 4407 enum drbd_ret_code retcode; 4408 4409 if (!adm_ctx->reply_skb) 4410 return 0; 4411 retcode = adm_ctx->reply_dh->ret_code; 4412 if (retcode != NO_ERROR) 4413 goto out; 4414 4415 if (dh->minor > MINORMASK) { 4416 drbd_msg_put_info(adm_ctx->reply_skb, "requested minor out of range"); 4417 retcode = ERR_INVALID_REQUEST; 4418 goto out; 4419 } 4420 if (adm_ctx->volume > DRBD_VOLUME_MAX) { 4421 drbd_msg_put_info(adm_ctx->reply_skb, "requested volume id out of range"); 4422 retcode = ERR_INVALID_REQUEST; 4423 goto out; 4424 } 4425 4426 /* drbd_adm_prepare made sure already 4427 * that first_peer_device(device)->connection and device->vnr match the request. */ 4428 if (adm_ctx->device) { 4429 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) 4430 retcode = ERR_MINOR_OR_VOLUME_EXISTS; 4431 /* else: still NO_ERROR */ 4432 goto out; 4433 } 4434 4435 mutex_lock(&adm_ctx->resource->adm_mutex); 4436 retcode = drbd_create_device(adm_ctx, dh->minor); 4437 if (retcode == NO_ERROR) { 4438 struct drbd_device *device; 4439 struct drbd_peer_device *peer_device; 4440 struct device_info info; 4441 unsigned int peer_devices = 0; 4442 enum drbd_notification_type flags; 4443 4444 device = minor_to_device(dh->minor); 4445 for_each_peer_device(peer_device, device) { 4446 if (!has_net_conf(peer_device->connection)) 4447 continue; 4448 peer_devices++; 4449 } 4450 4451 device_to_info(&info, device); 4452 mutex_lock(¬ification_mutex); 4453 flags = (peer_devices--) ? NOTIFY_CONTINUES : 0; 4454 notify_device_state(NULL, 0, device, &info, NOTIFY_CREATE | flags); 4455 for_each_peer_device(peer_device, device) { 4456 struct peer_device_info peer_device_info; 4457 4458 if (!has_net_conf(peer_device->connection)) 4459 continue; 4460 peer_device_to_info(&peer_device_info, peer_device); 4461 flags = (peer_devices--) ? NOTIFY_CONTINUES : 0; 4462 notify_peer_device_state(NULL, 0, peer_device, &peer_device_info, 4463 NOTIFY_CREATE | flags); 4464 } 4465 mutex_unlock(¬ification_mutex); 4466 } 4467 mutex_unlock(&adm_ctx->resource->adm_mutex); 4468 out: 4469 adm_ctx->reply_dh->ret_code = retcode; 4470 return 0; 4471 } 4472 4473 static enum drbd_ret_code adm_del_minor(struct drbd_device *device) 4474 { 4475 struct drbd_peer_device *peer_device; 4476 4477 if (device->state.disk == D_DISKLESS && 4478 /* no need to be device->state.conn == C_STANDALONE && 4479 * we may want to delete a minor from a live replication group. 4480 */ 4481 device->state.role == R_SECONDARY) { 4482 struct drbd_connection *connection = 4483 first_connection(device->resource); 4484 4485 _drbd_request_state(device, NS(conn, C_WF_REPORT_PARAMS), 4486 CS_VERBOSE + CS_WAIT_COMPLETE); 4487 4488 /* If the state engine hasn't stopped the sender thread yet, we 4489 * need to flush the sender work queue before generating the 4490 * DESTROY events here. */ 4491 if (get_t_state(&connection->worker) == RUNNING) 4492 drbd_flush_workqueue(&connection->sender_work); 4493 4494 mutex_lock(¬ification_mutex); 4495 for_each_peer_device(peer_device, device) { 4496 if (!has_net_conf(peer_device->connection)) 4497 continue; 4498 notify_peer_device_state(NULL, 0, peer_device, NULL, 4499 NOTIFY_DESTROY | NOTIFY_CONTINUES); 4500 } 4501 notify_device_state(NULL, 0, device, NULL, NOTIFY_DESTROY); 4502 mutex_unlock(¬ification_mutex); 4503 4504 drbd_delete_device(device); 4505 return NO_ERROR; 4506 } else 4507 return ERR_MINOR_CONFIGURED; 4508 } 4509 4510 int drbd_nl_del_minor_doit(struct sk_buff *skb, struct genl_info *info) 4511 { 4512 struct drbd_config_context *adm_ctx = info->user_ptr[0]; 4513 enum drbd_ret_code retcode; 4514 4515 if (!adm_ctx->reply_skb) 4516 return 0; 4517 retcode = adm_ctx->reply_dh->ret_code; 4518 if (retcode != NO_ERROR) 4519 goto out; 4520 4521 mutex_lock(&adm_ctx->resource->adm_mutex); 4522 retcode = adm_del_minor(adm_ctx->device); 4523 mutex_unlock(&adm_ctx->resource->adm_mutex); 4524 out: 4525 adm_ctx->reply_dh->ret_code = retcode; 4526 return 0; 4527 } 4528 4529 static int adm_del_resource(struct drbd_resource *resource) 4530 { 4531 struct drbd_connection *connection; 4532 4533 for_each_connection(connection, resource) { 4534 if (connection->cstate > C_STANDALONE) 4535 return ERR_NET_CONFIGURED; 4536 } 4537 if (!idr_is_empty(&resource->devices)) 4538 return ERR_RES_IN_USE; 4539 4540 /* The state engine has stopped the sender thread, so we don't 4541 * need to flush the sender work queue before generating the 4542 * DESTROY event here. */ 4543 mutex_lock(¬ification_mutex); 4544 notify_resource_state(NULL, 0, resource, NULL, NOTIFY_DESTROY); 4545 mutex_unlock(¬ification_mutex); 4546 4547 mutex_lock(&resources_mutex); 4548 list_del_rcu(&resource->resources); 4549 mutex_unlock(&resources_mutex); 4550 /* Make sure all threads have actually stopped: state handling only 4551 * does drbd_thread_stop_nowait(). */ 4552 list_for_each_entry(connection, &resource->connections, connections) 4553 drbd_thread_stop(&connection->worker); 4554 synchronize_rcu(); 4555 drbd_free_resource(resource); 4556 return NO_ERROR; 4557 } 4558 4559 int drbd_nl_down_doit(struct sk_buff *skb, struct genl_info *info) 4560 { 4561 struct drbd_config_context *adm_ctx = info->user_ptr[0]; 4562 struct drbd_resource *resource; 4563 struct drbd_connection *connection; 4564 struct drbd_device *device; 4565 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */ 4566 unsigned i; 4567 4568 if (!adm_ctx->reply_skb) 4569 return 0; 4570 retcode = adm_ctx->reply_dh->ret_code; 4571 if (retcode != NO_ERROR) 4572 goto finish; 4573 4574 resource = adm_ctx->resource; 4575 mutex_lock(&resource->adm_mutex); 4576 /* demote */ 4577 for_each_connection(connection, resource) { 4578 struct drbd_peer_device *peer_device; 4579 4580 idr_for_each_entry(&connection->peer_devices, peer_device, i) { 4581 retcode = drbd_set_role(peer_device->device, R_SECONDARY, 0); 4582 if (retcode < SS_SUCCESS) { 4583 drbd_msg_put_info(adm_ctx->reply_skb, "failed to demote"); 4584 goto out; 4585 } 4586 } 4587 4588 retcode = conn_try_disconnect(connection, 0); 4589 if (retcode < SS_SUCCESS) { 4590 drbd_msg_put_info(adm_ctx->reply_skb, "failed to disconnect"); 4591 goto out; 4592 } 4593 } 4594 4595 /* detach */ 4596 idr_for_each_entry(&resource->devices, device, i) { 4597 retcode = adm_detach(device, 0); 4598 if (retcode < SS_SUCCESS || retcode > NO_ERROR) { 4599 drbd_msg_put_info(adm_ctx->reply_skb, "failed to detach"); 4600 goto out; 4601 } 4602 } 4603 4604 /* delete volumes */ 4605 idr_for_each_entry(&resource->devices, device, i) { 4606 retcode = adm_del_minor(device); 4607 if (retcode != NO_ERROR) { 4608 /* "can not happen" */ 4609 drbd_msg_put_info(adm_ctx->reply_skb, "failed to delete volume"); 4610 goto out; 4611 } 4612 } 4613 4614 retcode = adm_del_resource(resource); 4615 out: 4616 mutex_unlock(&resource->adm_mutex); 4617 finish: 4618 adm_ctx->reply_dh->ret_code = retcode; 4619 return 0; 4620 } 4621 4622 int drbd_nl_del_resource_doit(struct sk_buff *skb, struct genl_info *info) 4623 { 4624 struct drbd_config_context *adm_ctx = info->user_ptr[0]; 4625 struct drbd_resource *resource; 4626 enum drbd_ret_code retcode; 4627 4628 if (!adm_ctx->reply_skb) 4629 return 0; 4630 retcode = adm_ctx->reply_dh->ret_code; 4631 if (retcode != NO_ERROR) 4632 goto finish; 4633 resource = adm_ctx->resource; 4634 4635 mutex_lock(&resource->adm_mutex); 4636 retcode = adm_del_resource(resource); 4637 mutex_unlock(&resource->adm_mutex); 4638 finish: 4639 adm_ctx->reply_dh->ret_code = retcode; 4640 return 0; 4641 } 4642 4643 void drbd_bcast_event(struct drbd_device *device, const struct sib_info *sib) 4644 { 4645 struct sk_buff *msg; 4646 struct drbd_genlmsghdr *d_out; 4647 unsigned seq; 4648 int err = -ENOMEM; 4649 4650 seq = atomic_inc_return(&drbd_genl_seq); 4651 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO); 4652 if (!msg) 4653 goto failed; 4654 4655 err = -EMSGSIZE; 4656 d_out = genlmsg_put(msg, 0, seq, &drbd_nl_family, 0, DRBD_ADM_EVENT); 4657 if (!d_out) /* cannot happen, but anyways. */ 4658 goto nla_put_failure; 4659 d_out->minor = device_to_minor(device); 4660 d_out->ret_code = NO_ERROR; 4661 4662 if (nla_put_status_info(msg, device, sib)) 4663 goto nla_put_failure; 4664 genlmsg_end(msg, d_out); 4665 err = drbd_genl_multicast_events(msg, GFP_NOWAIT); 4666 /* msg has been consumed or freed in netlink_broadcast() */ 4667 if (err && err != -ESRCH) 4668 goto failed; 4669 4670 return; 4671 4672 nla_put_failure: 4673 nlmsg_free(msg); 4674 failed: 4675 drbd_err(device, "Error %d while broadcasting event. " 4676 "Event seq:%u sib_reason:%u\n", 4677 err, seq, sib->sib_reason); 4678 } 4679 4680 static int nla_put_notification_header(struct sk_buff *msg, 4681 enum drbd_notification_type type) 4682 { 4683 struct drbd_notification_header nh = { 4684 .nh_type = type, 4685 }; 4686 4687 return drbd_notification_header_to_skb(msg, &nh); 4688 } 4689 4690 int notify_resource_state(struct sk_buff *skb, 4691 unsigned int seq, 4692 struct drbd_resource *resource, 4693 struct resource_info *resource_info, 4694 enum drbd_notification_type type) 4695 { 4696 struct resource_statistics resource_statistics; 4697 struct drbd_genlmsghdr *dh; 4698 bool multicast = false; 4699 int err; 4700 4701 if (!skb) { 4702 seq = atomic_inc_return(¬ify_genl_seq); 4703 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO); 4704 err = -ENOMEM; 4705 if (!skb) 4706 goto failed; 4707 multicast = true; 4708 } 4709 4710 err = -EMSGSIZE; 4711 dh = genlmsg_put(skb, 0, seq, &drbd_nl_family, 0, DRBD_ADM_RESOURCE_STATE); 4712 if (!dh) 4713 goto nla_put_failure; 4714 dh->minor = -1U; 4715 dh->ret_code = NO_ERROR; 4716 if (nla_put_drbd_cfg_context(skb, resource, NULL, NULL) || 4717 nla_put_notification_header(skb, type) || 4718 ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY && 4719 resource_info_to_skb(skb, resource_info))) 4720 goto nla_put_failure; 4721 resource_statistics.res_stat_write_ordering = resource->write_ordering; 4722 err = resource_statistics_to_skb(skb, &resource_statistics); 4723 if (err) 4724 goto nla_put_failure; 4725 genlmsg_end(skb, dh); 4726 if (multicast) { 4727 err = drbd_genl_multicast_events(skb, GFP_NOWAIT); 4728 /* skb has been consumed or freed in netlink_broadcast() */ 4729 if (err && err != -ESRCH) 4730 goto failed; 4731 } 4732 return 0; 4733 4734 nla_put_failure: 4735 nlmsg_free(skb); 4736 failed: 4737 drbd_err(resource, "Error %d while broadcasting event. Event seq:%u\n", 4738 err, seq); 4739 return err; 4740 } 4741 4742 int notify_device_state(struct sk_buff *skb, 4743 unsigned int seq, 4744 struct drbd_device *device, 4745 struct device_info *device_info, 4746 enum drbd_notification_type type) 4747 { 4748 struct device_statistics device_statistics; 4749 struct drbd_genlmsghdr *dh; 4750 bool multicast = false; 4751 int err; 4752 4753 if (!skb) { 4754 seq = atomic_inc_return(¬ify_genl_seq); 4755 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO); 4756 err = -ENOMEM; 4757 if (!skb) 4758 goto failed; 4759 multicast = true; 4760 } 4761 4762 err = -EMSGSIZE; 4763 dh = genlmsg_put(skb, 0, seq, &drbd_nl_family, 0, DRBD_ADM_DEVICE_STATE); 4764 if (!dh) 4765 goto nla_put_failure; 4766 dh->minor = device->minor; 4767 dh->ret_code = NO_ERROR; 4768 if (nla_put_drbd_cfg_context(skb, device->resource, NULL, device) || 4769 nla_put_notification_header(skb, type) || 4770 ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY && 4771 device_info_to_skb(skb, device_info))) 4772 goto nla_put_failure; 4773 device_to_statistics(&device_statistics, device); 4774 device_statistics_to_skb(skb, &device_statistics); 4775 genlmsg_end(skb, dh); 4776 if (multicast) { 4777 err = drbd_genl_multicast_events(skb, GFP_NOWAIT); 4778 /* skb has been consumed or freed in netlink_broadcast() */ 4779 if (err && err != -ESRCH) 4780 goto failed; 4781 } 4782 return 0; 4783 4784 nla_put_failure: 4785 nlmsg_free(skb); 4786 failed: 4787 drbd_err(device, "Error %d while broadcasting event. Event seq:%u\n", 4788 err, seq); 4789 return err; 4790 } 4791 4792 int notify_connection_state(struct sk_buff *skb, 4793 unsigned int seq, 4794 struct drbd_connection *connection, 4795 struct connection_info *connection_info, 4796 enum drbd_notification_type type) 4797 { 4798 struct connection_statistics connection_statistics; 4799 struct drbd_genlmsghdr *dh; 4800 bool multicast = false; 4801 int err; 4802 4803 if (!skb) { 4804 seq = atomic_inc_return(¬ify_genl_seq); 4805 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO); 4806 err = -ENOMEM; 4807 if (!skb) 4808 goto failed; 4809 multicast = true; 4810 } 4811 4812 err = -EMSGSIZE; 4813 dh = genlmsg_put(skb, 0, seq, &drbd_nl_family, 0, DRBD_ADM_CONNECTION_STATE); 4814 if (!dh) 4815 goto nla_put_failure; 4816 dh->minor = -1U; 4817 dh->ret_code = NO_ERROR; 4818 if (nla_put_drbd_cfg_context(skb, connection->resource, connection, NULL) || 4819 nla_put_notification_header(skb, type) || 4820 ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY && 4821 connection_info_to_skb(skb, connection_info))) 4822 goto nla_put_failure; 4823 connection_statistics.conn_congested = test_bit(NET_CONGESTED, &connection->flags); 4824 connection_statistics_to_skb(skb, &connection_statistics); 4825 genlmsg_end(skb, dh); 4826 if (multicast) { 4827 err = drbd_genl_multicast_events(skb, GFP_NOWAIT); 4828 /* skb has been consumed or freed in netlink_broadcast() */ 4829 if (err && err != -ESRCH) 4830 goto failed; 4831 } 4832 return 0; 4833 4834 nla_put_failure: 4835 nlmsg_free(skb); 4836 failed: 4837 drbd_err(connection, "Error %d while broadcasting event. Event seq:%u\n", 4838 err, seq); 4839 return err; 4840 } 4841 4842 int notify_peer_device_state(struct sk_buff *skb, 4843 unsigned int seq, 4844 struct drbd_peer_device *peer_device, 4845 struct peer_device_info *peer_device_info, 4846 enum drbd_notification_type type) 4847 { 4848 struct peer_device_statistics peer_device_statistics; 4849 struct drbd_resource *resource = peer_device->device->resource; 4850 struct drbd_genlmsghdr *dh; 4851 bool multicast = false; 4852 int err; 4853 4854 if (!skb) { 4855 seq = atomic_inc_return(¬ify_genl_seq); 4856 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO); 4857 err = -ENOMEM; 4858 if (!skb) 4859 goto failed; 4860 multicast = true; 4861 } 4862 4863 err = -EMSGSIZE; 4864 dh = genlmsg_put(skb, 0, seq, &drbd_nl_family, 0, DRBD_ADM_PEER_DEVICE_STATE); 4865 if (!dh) 4866 goto nla_put_failure; 4867 dh->minor = -1U; 4868 dh->ret_code = NO_ERROR; 4869 if (nla_put_drbd_cfg_context(skb, resource, peer_device->connection, peer_device->device) || 4870 nla_put_notification_header(skb, type) || 4871 ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY && 4872 peer_device_info_to_skb(skb, peer_device_info))) 4873 goto nla_put_failure; 4874 peer_device_to_statistics(&peer_device_statistics, peer_device); 4875 peer_device_statistics_to_skb(skb, &peer_device_statistics); 4876 genlmsg_end(skb, dh); 4877 if (multicast) { 4878 err = drbd_genl_multicast_events(skb, GFP_NOWAIT); 4879 /* skb has been consumed or freed in netlink_broadcast() */ 4880 if (err && err != -ESRCH) 4881 goto failed; 4882 } 4883 return 0; 4884 4885 nla_put_failure: 4886 nlmsg_free(skb); 4887 failed: 4888 drbd_err(peer_device, "Error %d while broadcasting event. Event seq:%u\n", 4889 err, seq); 4890 return err; 4891 } 4892 4893 void notify_helper(enum drbd_notification_type type, 4894 struct drbd_device *device, struct drbd_connection *connection, 4895 const char *name, int status) 4896 { 4897 struct drbd_resource *resource = device ? device->resource : connection->resource; 4898 struct drbd_helper_info helper_info; 4899 unsigned int seq = atomic_inc_return(¬ify_genl_seq); 4900 struct sk_buff *skb = NULL; 4901 struct drbd_genlmsghdr *dh; 4902 int err; 4903 4904 strscpy(helper_info.helper_name, name, sizeof(helper_info.helper_name)); 4905 helper_info.helper_name_len = min(strlen(name), sizeof(helper_info.helper_name)); 4906 helper_info.helper_status = status; 4907 4908 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO); 4909 err = -ENOMEM; 4910 if (!skb) 4911 goto fail; 4912 4913 err = -EMSGSIZE; 4914 dh = genlmsg_put(skb, 0, seq, &drbd_nl_family, 0, DRBD_ADM_HELPER); 4915 if (!dh) 4916 goto fail; 4917 dh->minor = device ? device->minor : -1; 4918 dh->ret_code = NO_ERROR; 4919 mutex_lock(¬ification_mutex); 4920 if (nla_put_drbd_cfg_context(skb, resource, connection, device) || 4921 nla_put_notification_header(skb, type) || 4922 drbd_helper_info_to_skb(skb, &helper_info)) 4923 goto unlock_fail; 4924 genlmsg_end(skb, dh); 4925 err = drbd_genl_multicast_events(skb, GFP_NOWAIT); 4926 skb = NULL; 4927 /* skb has been consumed or freed in netlink_broadcast() */ 4928 if (err && err != -ESRCH) 4929 goto unlock_fail; 4930 mutex_unlock(¬ification_mutex); 4931 return; 4932 4933 unlock_fail: 4934 mutex_unlock(¬ification_mutex); 4935 fail: 4936 nlmsg_free(skb); 4937 drbd_err(resource, "Error %d while broadcasting event. Event seq:%u\n", 4938 err, seq); 4939 } 4940 4941 static int notify_initial_state_done(struct sk_buff *skb, unsigned int seq) 4942 { 4943 struct drbd_genlmsghdr *dh; 4944 int err; 4945 4946 err = -EMSGSIZE; 4947 dh = genlmsg_put(skb, 0, seq, &drbd_nl_family, 0, DRBD_ADM_INITIAL_STATE_DONE); 4948 if (!dh) 4949 goto nla_put_failure; 4950 dh->minor = -1U; 4951 dh->ret_code = NO_ERROR; 4952 if (nla_put_notification_header(skb, NOTIFY_EXISTS)) 4953 goto nla_put_failure; 4954 genlmsg_end(skb, dh); 4955 return 0; 4956 4957 nla_put_failure: 4958 nlmsg_free(skb); 4959 pr_err("Error %d sending event. Event seq:%u\n", err, seq); 4960 return err; 4961 } 4962 4963 static void free_state_changes(struct list_head *list) 4964 { 4965 while (!list_empty(list)) { 4966 struct drbd_state_change *state_change = 4967 list_first_entry(list, struct drbd_state_change, list); 4968 list_del(&state_change->list); 4969 forget_state_change(state_change); 4970 } 4971 } 4972 4973 static unsigned int notifications_for_state_change(struct drbd_state_change *state_change) 4974 { 4975 return 1 + 4976 state_change->n_connections + 4977 state_change->n_devices + 4978 state_change->n_devices * state_change->n_connections; 4979 } 4980 4981 static int get_initial_state(struct sk_buff *skb, struct netlink_callback *cb) 4982 { 4983 struct drbd_state_change *state_change = (struct drbd_state_change *)cb->args[0]; 4984 unsigned int seq = cb->args[2]; 4985 unsigned int n; 4986 enum drbd_notification_type flags = 0; 4987 int err = 0; 4988 4989 /* There is no need for taking notification_mutex here: it doesn't 4990 matter if the initial state events mix with later state chage 4991 events; we can always tell the events apart by the NOTIFY_EXISTS 4992 flag. */ 4993 4994 cb->args[5]--; 4995 if (cb->args[5] == 1) { 4996 err = notify_initial_state_done(skb, seq); 4997 goto out; 4998 } 4999 n = cb->args[4]++; 5000 if (cb->args[4] < cb->args[3]) 5001 flags |= NOTIFY_CONTINUES; 5002 if (n < 1) { 5003 err = notify_resource_state_change(skb, seq, state_change->resource, 5004 NOTIFY_EXISTS | flags); 5005 goto next; 5006 } 5007 n--; 5008 if (n < state_change->n_connections) { 5009 err = notify_connection_state_change(skb, seq, &state_change->connections[n], 5010 NOTIFY_EXISTS | flags); 5011 goto next; 5012 } 5013 n -= state_change->n_connections; 5014 if (n < state_change->n_devices) { 5015 err = notify_device_state_change(skb, seq, &state_change->devices[n], 5016 NOTIFY_EXISTS | flags); 5017 goto next; 5018 } 5019 n -= state_change->n_devices; 5020 if (n < state_change->n_devices * state_change->n_connections) { 5021 err = notify_peer_device_state_change(skb, seq, &state_change->peer_devices[n], 5022 NOTIFY_EXISTS | flags); 5023 goto next; 5024 } 5025 5026 next: 5027 if (cb->args[4] == cb->args[3]) { 5028 struct drbd_state_change *next_state_change = 5029 list_entry(state_change->list.next, 5030 struct drbd_state_change, list); 5031 cb->args[0] = (long)next_state_change; 5032 cb->args[3] = notifications_for_state_change(next_state_change); 5033 cb->args[4] = 0; 5034 } 5035 out: 5036 if (err) 5037 return err; 5038 else 5039 return skb->len; 5040 } 5041 5042 int drbd_nl_get_initial_state_dumpit(struct sk_buff *skb, struct netlink_callback *cb) 5043 { 5044 struct drbd_resource *resource; 5045 LIST_HEAD(head); 5046 5047 if (cb->args[5] >= 1) { 5048 if (cb->args[5] > 1) 5049 return get_initial_state(skb, cb); 5050 if (cb->args[0]) { 5051 struct drbd_state_change *state_change = 5052 (struct drbd_state_change *)cb->args[0]; 5053 5054 /* connect list to head */ 5055 list_add(&head, &state_change->list); 5056 free_state_changes(&head); 5057 } 5058 return 0; 5059 } 5060 5061 cb->args[5] = 2; /* number of iterations */ 5062 mutex_lock(&resources_mutex); 5063 for_each_resource(resource, &drbd_resources) { 5064 struct drbd_state_change *state_change; 5065 5066 state_change = remember_old_state(resource, GFP_KERNEL); 5067 if (!state_change) { 5068 if (!list_empty(&head)) 5069 free_state_changes(&head); 5070 mutex_unlock(&resources_mutex); 5071 return -ENOMEM; 5072 } 5073 copy_old_to_new_state_change(state_change); 5074 list_add_tail(&state_change->list, &head); 5075 cb->args[5] += notifications_for_state_change(state_change); 5076 } 5077 mutex_unlock(&resources_mutex); 5078 5079 if (!list_empty(&head)) { 5080 struct drbd_state_change *state_change = 5081 list_entry(head.next, struct drbd_state_change, list); 5082 cb->args[0] = (long)state_change; 5083 cb->args[3] = notifications_for_state_change(state_change); 5084 list_del(&head); /* detach list from head */ 5085 } 5086 5087 cb->args[2] = cb->nlh->nlmsg_seq; 5088 return get_initial_state(skb, cb); 5089 } 5090 5091 static const struct genl_multicast_group drbd_nl_mcgrps[] = { 5092 [DRBD_NLGRP_EVENTS] = { .name = "events", }, 5093 }; 5094 5095 struct genl_family drbd_nl_family __ro_after_init = { 5096 .name = "drbd", 5097 .version = DRBD_FAMILY_VERSION, 5098 .hdrsize = NLA_ALIGN(sizeof(struct drbd_genlmsghdr)), 5099 .split_ops = drbd_nl_ops, 5100 .n_split_ops = ARRAY_SIZE(drbd_nl_ops), 5101 .mcgrps = drbd_nl_mcgrps, 5102 .n_mcgrps = ARRAY_SIZE(drbd_nl_mcgrps), 5103 .resv_start_op = 42, 5104 .module = THIS_MODULE, 5105 .netnsok = true, 5106 }; 5107