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