Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | drbd_nl.c |
| 3 | |
| 4 | This file is part of DRBD by Philipp Reisner and Lars Ellenberg. |
| 5 | |
| 6 | Copyright (C) 2001-2008, LINBIT Information Technologies GmbH. |
| 7 | Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>. |
| 8 | Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>. |
| 9 | |
| 10 | drbd is free software; you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation; either version 2, or (at your option) |
| 13 | any later version. |
| 14 | |
| 15 | drbd is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with drbd; see the file COPYING. If not, write to |
| 22 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 23 | |
| 24 | */ |
| 25 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 26 | #include <linux/module.h> |
| 27 | #include <linux/drbd.h> |
| 28 | #include <linux/in.h> |
| 29 | #include <linux/fs.h> |
| 30 | #include <linux/file.h> |
| 31 | #include <linux/slab.h> |
| 32 | #include <linux/connector.h> |
| 33 | #include <linux/blkpg.h> |
| 34 | #include <linux/cpumask.h> |
| 35 | #include "drbd_int.h" |
Philipp Reisner | 265be2d | 2010-05-31 10:14:17 +0200 | [diff] [blame^] | 36 | #include "drbd_req.h" |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 37 | #include "drbd_wrappers.h" |
| 38 | #include <asm/unaligned.h> |
| 39 | #include <linux/drbd_tag_magic.h> |
| 40 | #include <linux/drbd_limits.h> |
| 41 | |
| 42 | static unsigned short *tl_add_blob(unsigned short *, enum drbd_tags, const void *, int); |
| 43 | static unsigned short *tl_add_str(unsigned short *, enum drbd_tags, const char *); |
| 44 | static unsigned short *tl_add_int(unsigned short *, enum drbd_tags, const void *); |
| 45 | |
| 46 | /* see get_sb_bdev and bd_claim */ |
| 47 | static char *drbd_m_holder = "Hands off! this is DRBD's meta data device."; |
| 48 | |
| 49 | /* Generate the tag_list to struct functions */ |
| 50 | #define NL_PACKET(name, number, fields) \ |
| 51 | static int name ## _from_tags(struct drbd_conf *mdev, \ |
| 52 | unsigned short *tags, struct name *arg) __attribute__ ((unused)); \ |
| 53 | static int name ## _from_tags(struct drbd_conf *mdev, \ |
| 54 | unsigned short *tags, struct name *arg) \ |
| 55 | { \ |
| 56 | int tag; \ |
| 57 | int dlen; \ |
| 58 | \ |
| 59 | while ((tag = get_unaligned(tags++)) != TT_END) { \ |
| 60 | dlen = get_unaligned(tags++); \ |
| 61 | switch (tag_number(tag)) { \ |
| 62 | fields \ |
| 63 | default: \ |
| 64 | if (tag & T_MANDATORY) { \ |
| 65 | dev_err(DEV, "Unknown tag: %d\n", tag_number(tag)); \ |
| 66 | return 0; \ |
| 67 | } \ |
| 68 | } \ |
| 69 | tags = (unsigned short *)((char *)tags + dlen); \ |
| 70 | } \ |
| 71 | return 1; \ |
| 72 | } |
| 73 | #define NL_INTEGER(pn, pr, member) \ |
| 74 | case pn: /* D_ASSERT( tag_type(tag) == TT_INTEGER ); */ \ |
| 75 | arg->member = get_unaligned((int *)(tags)); \ |
| 76 | break; |
| 77 | #define NL_INT64(pn, pr, member) \ |
| 78 | case pn: /* D_ASSERT( tag_type(tag) == TT_INT64 ); */ \ |
| 79 | arg->member = get_unaligned((u64 *)(tags)); \ |
| 80 | break; |
| 81 | #define NL_BIT(pn, pr, member) \ |
| 82 | case pn: /* D_ASSERT( tag_type(tag) == TT_BIT ); */ \ |
| 83 | arg->member = *(char *)(tags) ? 1 : 0; \ |
| 84 | break; |
| 85 | #define NL_STRING(pn, pr, member, len) \ |
| 86 | case pn: /* D_ASSERT( tag_type(tag) == TT_STRING ); */ \ |
| 87 | if (dlen > len) { \ |
| 88 | dev_err(DEV, "arg too long: %s (%u wanted, max len: %u bytes)\n", \ |
| 89 | #member, dlen, (unsigned int)len); \ |
| 90 | return 0; \ |
| 91 | } \ |
| 92 | arg->member ## _len = dlen; \ |
| 93 | memcpy(arg->member, tags, min_t(size_t, dlen, len)); \ |
| 94 | break; |
| 95 | #include "linux/drbd_nl.h" |
| 96 | |
| 97 | /* Generate the struct to tag_list functions */ |
| 98 | #define NL_PACKET(name, number, fields) \ |
| 99 | static unsigned short* \ |
| 100 | name ## _to_tags(struct drbd_conf *mdev, \ |
| 101 | struct name *arg, unsigned short *tags) __attribute__ ((unused)); \ |
| 102 | static unsigned short* \ |
| 103 | name ## _to_tags(struct drbd_conf *mdev, \ |
| 104 | struct name *arg, unsigned short *tags) \ |
| 105 | { \ |
| 106 | fields \ |
| 107 | return tags; \ |
| 108 | } |
| 109 | |
| 110 | #define NL_INTEGER(pn, pr, member) \ |
| 111 | put_unaligned(pn | pr | TT_INTEGER, tags++); \ |
| 112 | put_unaligned(sizeof(int), tags++); \ |
| 113 | put_unaligned(arg->member, (int *)tags); \ |
| 114 | tags = (unsigned short *)((char *)tags+sizeof(int)); |
| 115 | #define NL_INT64(pn, pr, member) \ |
| 116 | put_unaligned(pn | pr | TT_INT64, tags++); \ |
| 117 | put_unaligned(sizeof(u64), tags++); \ |
| 118 | put_unaligned(arg->member, (u64 *)tags); \ |
| 119 | tags = (unsigned short *)((char *)tags+sizeof(u64)); |
| 120 | #define NL_BIT(pn, pr, member) \ |
| 121 | put_unaligned(pn | pr | TT_BIT, tags++); \ |
| 122 | put_unaligned(sizeof(char), tags++); \ |
| 123 | *(char *)tags = arg->member; \ |
| 124 | tags = (unsigned short *)((char *)tags+sizeof(char)); |
| 125 | #define NL_STRING(pn, pr, member, len) \ |
| 126 | put_unaligned(pn | pr | TT_STRING, tags++); \ |
| 127 | put_unaligned(arg->member ## _len, tags++); \ |
| 128 | memcpy(tags, arg->member, arg->member ## _len); \ |
| 129 | tags = (unsigned short *)((char *)tags + arg->member ## _len); |
| 130 | #include "linux/drbd_nl.h" |
| 131 | |
| 132 | void drbd_bcast_ev_helper(struct drbd_conf *mdev, char *helper_name); |
| 133 | void drbd_nl_send_reply(struct cn_msg *, int); |
| 134 | |
| 135 | int drbd_khelper(struct drbd_conf *mdev, char *cmd) |
| 136 | { |
| 137 | char *envp[] = { "HOME=/", |
| 138 | "TERM=linux", |
| 139 | "PATH=/sbin:/usr/sbin:/bin:/usr/bin", |
| 140 | NULL, /* Will be set to address family */ |
| 141 | NULL, /* Will be set to address */ |
| 142 | NULL }; |
| 143 | |
| 144 | char mb[12], af[20], ad[60], *afs; |
| 145 | char *argv[] = {usermode_helper, cmd, mb, NULL }; |
| 146 | int ret; |
| 147 | |
| 148 | snprintf(mb, 12, "minor-%d", mdev_to_minor(mdev)); |
| 149 | |
| 150 | if (get_net_conf(mdev)) { |
| 151 | switch (((struct sockaddr *)mdev->net_conf->peer_addr)->sa_family) { |
| 152 | case AF_INET6: |
| 153 | afs = "ipv6"; |
| 154 | snprintf(ad, 60, "DRBD_PEER_ADDRESS=%pI6", |
| 155 | &((struct sockaddr_in6 *)mdev->net_conf->peer_addr)->sin6_addr); |
| 156 | break; |
| 157 | case AF_INET: |
| 158 | afs = "ipv4"; |
| 159 | snprintf(ad, 60, "DRBD_PEER_ADDRESS=%pI4", |
| 160 | &((struct sockaddr_in *)mdev->net_conf->peer_addr)->sin_addr); |
| 161 | break; |
| 162 | default: |
| 163 | afs = "ssocks"; |
| 164 | snprintf(ad, 60, "DRBD_PEER_ADDRESS=%pI4", |
| 165 | &((struct sockaddr_in *)mdev->net_conf->peer_addr)->sin_addr); |
| 166 | } |
| 167 | snprintf(af, 20, "DRBD_PEER_AF=%s", afs); |
| 168 | envp[3]=af; |
| 169 | envp[4]=ad; |
| 170 | put_net_conf(mdev); |
| 171 | } |
| 172 | |
| 173 | dev_info(DEV, "helper command: %s %s %s\n", usermode_helper, cmd, mb); |
| 174 | |
| 175 | drbd_bcast_ev_helper(mdev, cmd); |
| 176 | ret = call_usermodehelper(usermode_helper, argv, envp, 1); |
| 177 | if (ret) |
| 178 | dev_warn(DEV, "helper command: %s %s %s exit code %u (0x%x)\n", |
| 179 | usermode_helper, cmd, mb, |
| 180 | (ret >> 8) & 0xff, ret); |
| 181 | else |
| 182 | dev_info(DEV, "helper command: %s %s %s exit code %u (0x%x)\n", |
| 183 | usermode_helper, cmd, mb, |
| 184 | (ret >> 8) & 0xff, ret); |
| 185 | |
| 186 | if (ret < 0) /* Ignore any ERRNOs we got. */ |
| 187 | ret = 0; |
| 188 | |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | enum drbd_disk_state drbd_try_outdate_peer(struct drbd_conf *mdev) |
| 193 | { |
| 194 | char *ex_to_string; |
| 195 | int r; |
| 196 | enum drbd_disk_state nps; |
| 197 | enum drbd_fencing_p fp; |
| 198 | |
| 199 | D_ASSERT(mdev->state.pdsk == D_UNKNOWN); |
| 200 | |
| 201 | if (get_ldev_if_state(mdev, D_CONSISTENT)) { |
| 202 | fp = mdev->ldev->dc.fencing; |
| 203 | put_ldev(mdev); |
| 204 | } else { |
| 205 | dev_warn(DEV, "Not fencing peer, I'm not even Consistent myself.\n"); |
| 206 | return mdev->state.pdsk; |
| 207 | } |
| 208 | |
| 209 | if (fp == FP_STONITH) |
| 210 | _drbd_request_state(mdev, NS(susp, 1), CS_WAIT_COMPLETE); |
| 211 | |
| 212 | r = drbd_khelper(mdev, "fence-peer"); |
| 213 | |
| 214 | switch ((r>>8) & 0xff) { |
| 215 | case 3: /* peer is inconsistent */ |
| 216 | ex_to_string = "peer is inconsistent or worse"; |
| 217 | nps = D_INCONSISTENT; |
| 218 | break; |
| 219 | case 4: /* peer got outdated, or was already outdated */ |
| 220 | ex_to_string = "peer was fenced"; |
| 221 | nps = D_OUTDATED; |
| 222 | break; |
| 223 | case 5: /* peer was down */ |
| 224 | if (mdev->state.disk == D_UP_TO_DATE) { |
| 225 | /* we will(have) create(d) a new UUID anyways... */ |
| 226 | ex_to_string = "peer is unreachable, assumed to be dead"; |
| 227 | nps = D_OUTDATED; |
| 228 | } else { |
| 229 | ex_to_string = "peer unreachable, doing nothing since disk != UpToDate"; |
| 230 | nps = mdev->state.pdsk; |
| 231 | } |
| 232 | break; |
| 233 | case 6: /* Peer is primary, voluntarily outdate myself. |
| 234 | * This is useful when an unconnected R_SECONDARY is asked to |
| 235 | * become R_PRIMARY, but finds the other peer being active. */ |
| 236 | ex_to_string = "peer is active"; |
| 237 | dev_warn(DEV, "Peer is primary, outdating myself.\n"); |
| 238 | nps = D_UNKNOWN; |
| 239 | _drbd_request_state(mdev, NS(disk, D_OUTDATED), CS_WAIT_COMPLETE); |
| 240 | break; |
| 241 | case 7: |
| 242 | if (fp != FP_STONITH) |
| 243 | dev_err(DEV, "fence-peer() = 7 && fencing != Stonith !!!\n"); |
| 244 | ex_to_string = "peer was stonithed"; |
| 245 | nps = D_OUTDATED; |
| 246 | break; |
| 247 | default: |
| 248 | /* The script is broken ... */ |
| 249 | nps = D_UNKNOWN; |
| 250 | dev_err(DEV, "fence-peer helper broken, returned %d\n", (r>>8)&0xff); |
| 251 | return nps; |
| 252 | } |
| 253 | |
| 254 | dev_info(DEV, "fence-peer helper returned %d (%s)\n", |
| 255 | (r>>8) & 0xff, ex_to_string); |
| 256 | return nps; |
| 257 | } |
| 258 | |
| 259 | |
| 260 | int drbd_set_role(struct drbd_conf *mdev, enum drbd_role new_role, int force) |
| 261 | { |
| 262 | const int max_tries = 4; |
| 263 | int r = 0; |
| 264 | int try = 0; |
| 265 | int forced = 0; |
| 266 | union drbd_state mask, val; |
| 267 | enum drbd_disk_state nps; |
| 268 | |
| 269 | if (new_role == R_PRIMARY) |
| 270 | request_ping(mdev); /* Detect a dead peer ASAP */ |
| 271 | |
| 272 | mutex_lock(&mdev->state_mutex); |
| 273 | |
| 274 | mask.i = 0; mask.role = R_MASK; |
| 275 | val.i = 0; val.role = new_role; |
| 276 | |
| 277 | while (try++ < max_tries) { |
| 278 | r = _drbd_request_state(mdev, mask, val, CS_WAIT_COMPLETE); |
| 279 | |
| 280 | /* in case we first succeeded to outdate, |
| 281 | * but now suddenly could establish a connection */ |
| 282 | if (r == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) { |
| 283 | val.pdsk = 0; |
| 284 | mask.pdsk = 0; |
| 285 | continue; |
| 286 | } |
| 287 | |
| 288 | if (r == SS_NO_UP_TO_DATE_DISK && force && |
Philipp Reisner | d10a33c | 2010-03-04 15:11:39 +0100 | [diff] [blame] | 289 | (mdev->state.disk < D_UP_TO_DATE && |
| 290 | mdev->state.disk >= D_INCONSISTENT)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 291 | mask.disk = D_MASK; |
| 292 | val.disk = D_UP_TO_DATE; |
| 293 | forced = 1; |
| 294 | continue; |
| 295 | } |
| 296 | |
| 297 | if (r == SS_NO_UP_TO_DATE_DISK && |
| 298 | mdev->state.disk == D_CONSISTENT && mask.pdsk == 0) { |
| 299 | D_ASSERT(mdev->state.pdsk == D_UNKNOWN); |
| 300 | nps = drbd_try_outdate_peer(mdev); |
| 301 | |
| 302 | if (nps == D_OUTDATED || nps == D_INCONSISTENT) { |
| 303 | val.disk = D_UP_TO_DATE; |
| 304 | mask.disk = D_MASK; |
| 305 | } |
| 306 | |
| 307 | val.pdsk = nps; |
| 308 | mask.pdsk = D_MASK; |
| 309 | |
| 310 | continue; |
| 311 | } |
| 312 | |
| 313 | if (r == SS_NOTHING_TO_DO) |
| 314 | goto fail; |
| 315 | if (r == SS_PRIMARY_NOP && mask.pdsk == 0) { |
| 316 | nps = drbd_try_outdate_peer(mdev); |
| 317 | |
| 318 | if (force && nps > D_OUTDATED) { |
| 319 | dev_warn(DEV, "Forced into split brain situation!\n"); |
| 320 | nps = D_OUTDATED; |
| 321 | } |
| 322 | |
| 323 | mask.pdsk = D_MASK; |
| 324 | val.pdsk = nps; |
| 325 | |
| 326 | continue; |
| 327 | } |
| 328 | if (r == SS_TWO_PRIMARIES) { |
| 329 | /* Maybe the peer is detected as dead very soon... |
| 330 | retry at most once more in this case. */ |
| 331 | __set_current_state(TASK_INTERRUPTIBLE); |
| 332 | schedule_timeout((mdev->net_conf->ping_timeo+1)*HZ/10); |
| 333 | if (try < max_tries) |
| 334 | try = max_tries - 1; |
| 335 | continue; |
| 336 | } |
| 337 | if (r < SS_SUCCESS) { |
| 338 | r = _drbd_request_state(mdev, mask, val, |
| 339 | CS_VERBOSE + CS_WAIT_COMPLETE); |
| 340 | if (r < SS_SUCCESS) |
| 341 | goto fail; |
| 342 | } |
| 343 | break; |
| 344 | } |
| 345 | |
| 346 | if (r < SS_SUCCESS) |
| 347 | goto fail; |
| 348 | |
| 349 | if (forced) |
| 350 | dev_warn(DEV, "Forced to consider local data as UpToDate!\n"); |
| 351 | |
| 352 | /* Wait until nothing is on the fly :) */ |
| 353 | wait_event(mdev->misc_wait, atomic_read(&mdev->ap_pending_cnt) == 0); |
| 354 | |
| 355 | if (new_role == R_SECONDARY) { |
| 356 | set_disk_ro(mdev->vdisk, TRUE); |
| 357 | if (get_ldev(mdev)) { |
| 358 | mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1; |
| 359 | put_ldev(mdev); |
| 360 | } |
| 361 | } else { |
| 362 | if (get_net_conf(mdev)) { |
| 363 | mdev->net_conf->want_lose = 0; |
| 364 | put_net_conf(mdev); |
| 365 | } |
| 366 | set_disk_ro(mdev->vdisk, FALSE); |
| 367 | if (get_ldev(mdev)) { |
| 368 | if (((mdev->state.conn < C_CONNECTED || |
| 369 | mdev->state.pdsk <= D_FAILED) |
| 370 | && mdev->ldev->md.uuid[UI_BITMAP] == 0) || forced) |
| 371 | drbd_uuid_new_current(mdev); |
| 372 | |
| 373 | mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1; |
| 374 | put_ldev(mdev); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | if ((new_role == R_SECONDARY) && get_ldev(mdev)) { |
| 379 | drbd_al_to_on_disk_bm(mdev); |
| 380 | put_ldev(mdev); |
| 381 | } |
| 382 | |
| 383 | if (mdev->state.conn >= C_WF_REPORT_PARAMS) { |
| 384 | /* if this was forced, we should consider sync */ |
| 385 | if (forced) |
| 386 | drbd_send_uuids(mdev); |
| 387 | drbd_send_state(mdev); |
| 388 | } |
| 389 | |
| 390 | drbd_md_sync(mdev); |
| 391 | |
| 392 | kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE); |
| 393 | fail: |
| 394 | mutex_unlock(&mdev->state_mutex); |
| 395 | return r; |
| 396 | } |
| 397 | |
| 398 | |
| 399 | static int drbd_nl_primary(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 400 | struct drbd_nl_cfg_reply *reply) |
| 401 | { |
| 402 | struct primary primary_args; |
| 403 | |
| 404 | memset(&primary_args, 0, sizeof(struct primary)); |
| 405 | if (!primary_from_tags(mdev, nlp->tag_list, &primary_args)) { |
| 406 | reply->ret_code = ERR_MANDATORY_TAG; |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | reply->ret_code = |
Philipp Reisner | 1f55243 | 2010-03-04 15:51:01 +0100 | [diff] [blame] | 411 | drbd_set_role(mdev, R_PRIMARY, primary_args.primary_force); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | static int drbd_nl_secondary(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 417 | struct drbd_nl_cfg_reply *reply) |
| 418 | { |
| 419 | reply->ret_code = drbd_set_role(mdev, R_SECONDARY, 0); |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | /* initializes the md.*_offset members, so we are able to find |
| 425 | * the on disk meta data */ |
| 426 | static void drbd_md_set_sector_offsets(struct drbd_conf *mdev, |
| 427 | struct drbd_backing_dev *bdev) |
| 428 | { |
| 429 | sector_t md_size_sect = 0; |
| 430 | switch (bdev->dc.meta_dev_idx) { |
| 431 | default: |
| 432 | /* v07 style fixed size indexed meta data */ |
| 433 | bdev->md.md_size_sect = MD_RESERVED_SECT; |
| 434 | bdev->md.md_offset = drbd_md_ss__(mdev, bdev); |
| 435 | bdev->md.al_offset = MD_AL_OFFSET; |
| 436 | bdev->md.bm_offset = MD_BM_OFFSET; |
| 437 | break; |
| 438 | case DRBD_MD_INDEX_FLEX_EXT: |
| 439 | /* just occupy the full device; unit: sectors */ |
| 440 | bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev); |
| 441 | bdev->md.md_offset = 0; |
| 442 | bdev->md.al_offset = MD_AL_OFFSET; |
| 443 | bdev->md.bm_offset = MD_BM_OFFSET; |
| 444 | break; |
| 445 | case DRBD_MD_INDEX_INTERNAL: |
| 446 | case DRBD_MD_INDEX_FLEX_INT: |
| 447 | bdev->md.md_offset = drbd_md_ss__(mdev, bdev); |
| 448 | /* al size is still fixed */ |
| 449 | bdev->md.al_offset = -MD_AL_MAX_SIZE; |
| 450 | /* we need (slightly less than) ~ this much bitmap sectors: */ |
| 451 | md_size_sect = drbd_get_capacity(bdev->backing_bdev); |
| 452 | md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT); |
| 453 | md_size_sect = BM_SECT_TO_EXT(md_size_sect); |
| 454 | md_size_sect = ALIGN(md_size_sect, 8); |
| 455 | |
| 456 | /* plus the "drbd meta data super block", |
| 457 | * and the activity log; */ |
| 458 | md_size_sect += MD_BM_OFFSET; |
| 459 | |
| 460 | bdev->md.md_size_sect = md_size_sect; |
| 461 | /* bitmap offset is adjusted by 'super' block size */ |
| 462 | bdev->md.bm_offset = -md_size_sect + MD_AL_OFFSET; |
| 463 | break; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | char *ppsize(char *buf, unsigned long long size) |
| 468 | { |
| 469 | /* Needs 9 bytes at max. */ |
| 470 | static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' }; |
| 471 | int base = 0; |
| 472 | while (size >= 10000) { |
| 473 | /* shift + round */ |
| 474 | size = (size >> 10) + !!(size & (1<<9)); |
| 475 | base++; |
| 476 | } |
| 477 | sprintf(buf, "%lu %cB", (long)size, units[base]); |
| 478 | |
| 479 | return buf; |
| 480 | } |
| 481 | |
| 482 | /* there is still a theoretical deadlock when called from receiver |
| 483 | * on an D_INCONSISTENT R_PRIMARY: |
| 484 | * remote READ does inc_ap_bio, receiver would need to receive answer |
| 485 | * packet from remote to dec_ap_bio again. |
| 486 | * receiver receive_sizes(), comes here, |
| 487 | * waits for ap_bio_cnt == 0. -> deadlock. |
| 488 | * but this cannot happen, actually, because: |
| 489 | * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable |
| 490 | * (not connected, or bad/no disk on peer): |
| 491 | * see drbd_fail_request_early, ap_bio_cnt is zero. |
| 492 | * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET: |
| 493 | * peer may not initiate a resize. |
| 494 | */ |
| 495 | void drbd_suspend_io(struct drbd_conf *mdev) |
| 496 | { |
| 497 | set_bit(SUSPEND_IO, &mdev->flags); |
Philipp Reisner | 265be2d | 2010-05-31 10:14:17 +0200 | [diff] [blame^] | 498 | if (mdev->state.susp) |
| 499 | return; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 500 | wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt)); |
| 501 | } |
| 502 | |
| 503 | void drbd_resume_io(struct drbd_conf *mdev) |
| 504 | { |
| 505 | clear_bit(SUSPEND_IO, &mdev->flags); |
| 506 | wake_up(&mdev->misc_wait); |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * drbd_determine_dev_size() - Sets the right device size obeying all constraints |
| 511 | * @mdev: DRBD device. |
| 512 | * |
| 513 | * Returns 0 on success, negative return values indicate errors. |
| 514 | * You should call drbd_md_sync() after calling this function. |
| 515 | */ |
Philipp Reisner | d845030 | 2010-03-24 15:51:26 +0100 | [diff] [blame] | 516 | enum determine_dev_size drbd_determin_dev_size(struct drbd_conf *mdev, enum dds_flags flags) __must_hold(local) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 517 | { |
| 518 | sector_t prev_first_sect, prev_size; /* previous meta location */ |
| 519 | sector_t la_size; |
| 520 | sector_t size; |
| 521 | char ppb[10]; |
| 522 | |
| 523 | int md_moved, la_size_changed; |
| 524 | enum determine_dev_size rv = unchanged; |
| 525 | |
| 526 | /* race: |
| 527 | * application request passes inc_ap_bio, |
| 528 | * but then cannot get an AL-reference. |
| 529 | * this function later may wait on ap_bio_cnt == 0. -> deadlock. |
| 530 | * |
| 531 | * to avoid that: |
| 532 | * Suspend IO right here. |
| 533 | * still lock the act_log to not trigger ASSERTs there. |
| 534 | */ |
| 535 | drbd_suspend_io(mdev); |
| 536 | |
| 537 | /* no wait necessary anymore, actually we could assert that */ |
| 538 | wait_event(mdev->al_wait, lc_try_lock(mdev->act_log)); |
| 539 | |
| 540 | prev_first_sect = drbd_md_first_sector(mdev->ldev); |
| 541 | prev_size = mdev->ldev->md.md_size_sect; |
| 542 | la_size = mdev->ldev->md.la_size_sect; |
| 543 | |
| 544 | /* TODO: should only be some assert here, not (re)init... */ |
| 545 | drbd_md_set_sector_offsets(mdev, mdev->ldev); |
| 546 | |
Philipp Reisner | d845030 | 2010-03-24 15:51:26 +0100 | [diff] [blame] | 547 | size = drbd_new_dev_size(mdev, mdev->ldev, flags & DDSF_FORCED); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 548 | |
| 549 | if (drbd_get_capacity(mdev->this_bdev) != size || |
| 550 | drbd_bm_capacity(mdev) != size) { |
| 551 | int err; |
Philipp Reisner | 02d9a94 | 2010-03-24 16:23:03 +0100 | [diff] [blame] | 552 | err = drbd_bm_resize(mdev, size, !(flags & DDSF_NO_RESYNC)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 553 | if (unlikely(err)) { |
| 554 | /* currently there is only one error: ENOMEM! */ |
| 555 | size = drbd_bm_capacity(mdev)>>1; |
| 556 | if (size == 0) { |
| 557 | dev_err(DEV, "OUT OF MEMORY! " |
| 558 | "Could not allocate bitmap!\n"); |
| 559 | } else { |
| 560 | dev_err(DEV, "BM resizing failed. " |
| 561 | "Leaving size unchanged at size = %lu KB\n", |
| 562 | (unsigned long)size); |
| 563 | } |
| 564 | rv = dev_size_error; |
| 565 | } |
| 566 | /* racy, see comments above. */ |
| 567 | drbd_set_my_capacity(mdev, size); |
| 568 | mdev->ldev->md.la_size_sect = size; |
| 569 | dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1), |
| 570 | (unsigned long long)size>>1); |
| 571 | } |
| 572 | if (rv == dev_size_error) |
| 573 | goto out; |
| 574 | |
| 575 | la_size_changed = (la_size != mdev->ldev->md.la_size_sect); |
| 576 | |
| 577 | md_moved = prev_first_sect != drbd_md_first_sector(mdev->ldev) |
| 578 | || prev_size != mdev->ldev->md.md_size_sect; |
| 579 | |
| 580 | if (la_size_changed || md_moved) { |
| 581 | drbd_al_shrink(mdev); /* All extents inactive. */ |
| 582 | dev_info(DEV, "Writing the whole bitmap, %s\n", |
| 583 | la_size_changed && md_moved ? "size changed and md moved" : |
| 584 | la_size_changed ? "size changed" : "md moved"); |
| 585 | rv = drbd_bitmap_io(mdev, &drbd_bm_write, "size changed"); /* does drbd_resume_io() ! */ |
| 586 | drbd_md_mark_dirty(mdev); |
| 587 | } |
| 588 | |
| 589 | if (size > la_size) |
| 590 | rv = grew; |
| 591 | if (size < la_size) |
| 592 | rv = shrunk; |
| 593 | out: |
| 594 | lc_unlock(mdev->act_log); |
| 595 | wake_up(&mdev->al_wait); |
| 596 | drbd_resume_io(mdev); |
| 597 | |
| 598 | return rv; |
| 599 | } |
| 600 | |
| 601 | sector_t |
Philipp Reisner | a393db6 | 2009-12-22 13:35:52 +0100 | [diff] [blame] | 602 | drbd_new_dev_size(struct drbd_conf *mdev, struct drbd_backing_dev *bdev, int assume_peer_has_space) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 603 | { |
| 604 | sector_t p_size = mdev->p_size; /* partner's disk size. */ |
| 605 | sector_t la_size = bdev->md.la_size_sect; /* last agreed size. */ |
| 606 | sector_t m_size; /* my size */ |
| 607 | sector_t u_size = bdev->dc.disk_size; /* size requested by user. */ |
| 608 | sector_t size = 0; |
| 609 | |
| 610 | m_size = drbd_get_max_capacity(bdev); |
| 611 | |
Philipp Reisner | a393db6 | 2009-12-22 13:35:52 +0100 | [diff] [blame] | 612 | if (mdev->state.conn < C_CONNECTED && assume_peer_has_space) { |
| 613 | dev_warn(DEV, "Resize while not connected was forced by the user!\n"); |
| 614 | p_size = m_size; |
| 615 | } |
| 616 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 617 | if (p_size && m_size) { |
| 618 | size = min_t(sector_t, p_size, m_size); |
| 619 | } else { |
| 620 | if (la_size) { |
| 621 | size = la_size; |
| 622 | if (m_size && m_size < size) |
| 623 | size = m_size; |
| 624 | if (p_size && p_size < size) |
| 625 | size = p_size; |
| 626 | } else { |
| 627 | if (m_size) |
| 628 | size = m_size; |
| 629 | if (p_size) |
| 630 | size = p_size; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | if (size == 0) |
| 635 | dev_err(DEV, "Both nodes diskless!\n"); |
| 636 | |
| 637 | if (u_size) { |
| 638 | if (u_size > size) |
| 639 | dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n", |
| 640 | (unsigned long)u_size>>1, (unsigned long)size>>1); |
| 641 | else |
| 642 | size = u_size; |
| 643 | } |
| 644 | |
| 645 | return size; |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * drbd_check_al_size() - Ensures that the AL is of the right size |
| 650 | * @mdev: DRBD device. |
| 651 | * |
| 652 | * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation |
| 653 | * failed, and 0 on success. You should call drbd_md_sync() after you called |
| 654 | * this function. |
| 655 | */ |
| 656 | static int drbd_check_al_size(struct drbd_conf *mdev) |
| 657 | { |
| 658 | struct lru_cache *n, *t; |
| 659 | struct lc_element *e; |
| 660 | unsigned int in_use; |
| 661 | int i; |
| 662 | |
| 663 | ERR_IF(mdev->sync_conf.al_extents < 7) |
| 664 | mdev->sync_conf.al_extents = 127; |
| 665 | |
| 666 | if (mdev->act_log && |
| 667 | mdev->act_log->nr_elements == mdev->sync_conf.al_extents) |
| 668 | return 0; |
| 669 | |
| 670 | in_use = 0; |
| 671 | t = mdev->act_log; |
| 672 | n = lc_create("act_log", drbd_al_ext_cache, |
| 673 | mdev->sync_conf.al_extents, sizeof(struct lc_element), 0); |
| 674 | |
| 675 | if (n == NULL) { |
| 676 | dev_err(DEV, "Cannot allocate act_log lru!\n"); |
| 677 | return -ENOMEM; |
| 678 | } |
| 679 | spin_lock_irq(&mdev->al_lock); |
| 680 | if (t) { |
| 681 | for (i = 0; i < t->nr_elements; i++) { |
| 682 | e = lc_element_by_index(t, i); |
| 683 | if (e->refcnt) |
| 684 | dev_err(DEV, "refcnt(%d)==%d\n", |
| 685 | e->lc_number, e->refcnt); |
| 686 | in_use += e->refcnt; |
| 687 | } |
| 688 | } |
| 689 | if (!in_use) |
| 690 | mdev->act_log = n; |
| 691 | spin_unlock_irq(&mdev->al_lock); |
| 692 | if (in_use) { |
| 693 | dev_err(DEV, "Activity log still in use!\n"); |
| 694 | lc_destroy(n); |
| 695 | return -EBUSY; |
| 696 | } else { |
| 697 | if (t) |
| 698 | lc_destroy(t); |
| 699 | } |
| 700 | drbd_md_mark_dirty(mdev); /* we changed mdev->act_log->nr_elemens */ |
| 701 | return 0; |
| 702 | } |
| 703 | |
| 704 | void drbd_setup_queue_param(struct drbd_conf *mdev, unsigned int max_seg_s) __must_hold(local) |
| 705 | { |
| 706 | struct request_queue * const q = mdev->rq_queue; |
| 707 | struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue; |
| 708 | int max_segments = mdev->ldev->dc.max_bio_bvecs; |
| 709 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 710 | max_seg_s = min(queue_max_sectors(b) * queue_logical_block_size(b), max_seg_s); |
| 711 | |
Martin K. Petersen | 086fa5f | 2010-02-26 00:20:38 -0500 | [diff] [blame] | 712 | blk_queue_max_hw_sectors(q, max_seg_s >> 9); |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 713 | blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 714 | blk_queue_max_segment_size(q, max_seg_s); |
| 715 | blk_queue_logical_block_size(q, 512); |
| 716 | blk_queue_segment_boundary(q, PAGE_SIZE-1); |
| 717 | blk_stack_limits(&q->limits, &b->limits, 0); |
| 718 | |
| 719 | if (b->merge_bvec_fn) |
| 720 | dev_warn(DEV, "Backing device's merge_bvec_fn() = %p\n", |
| 721 | b->merge_bvec_fn); |
| 722 | dev_info(DEV, "max_segment_size ( = BIO size ) = %u\n", queue_max_segment_size(q)); |
| 723 | |
| 724 | if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) { |
| 725 | dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n", |
| 726 | q->backing_dev_info.ra_pages, |
| 727 | b->backing_dev_info.ra_pages); |
| 728 | q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | /* serialize deconfig (worker exiting, doing cleanup) |
| 733 | * and reconfig (drbdsetup disk, drbdsetup net) |
| 734 | * |
| 735 | * wait for a potentially exiting worker, then restart it, |
| 736 | * or start a new one. |
| 737 | */ |
| 738 | static void drbd_reconfig_start(struct drbd_conf *mdev) |
| 739 | { |
Lars Ellenberg | 6c6c795 | 2009-11-16 15:48:54 +0100 | [diff] [blame] | 740 | wait_event(mdev->state_wait, !test_and_set_bit(CONFIG_PENDING, &mdev->flags)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 741 | wait_event(mdev->state_wait, !test_bit(DEVICE_DYING, &mdev->flags)); |
| 742 | drbd_thread_start(&mdev->worker); |
| 743 | } |
| 744 | |
| 745 | /* if still unconfigured, stops worker again. |
| 746 | * if configured now, clears CONFIG_PENDING. |
| 747 | * wakes potential waiters */ |
| 748 | static void drbd_reconfig_done(struct drbd_conf *mdev) |
| 749 | { |
| 750 | spin_lock_irq(&mdev->req_lock); |
| 751 | if (mdev->state.disk == D_DISKLESS && |
| 752 | mdev->state.conn == C_STANDALONE && |
| 753 | mdev->state.role == R_SECONDARY) { |
| 754 | set_bit(DEVICE_DYING, &mdev->flags); |
| 755 | drbd_thread_stop_nowait(&mdev->worker); |
| 756 | } else |
| 757 | clear_bit(CONFIG_PENDING, &mdev->flags); |
| 758 | spin_unlock_irq(&mdev->req_lock); |
| 759 | wake_up(&mdev->state_wait); |
| 760 | } |
| 761 | |
| 762 | /* does always return 0; |
| 763 | * interesting return code is in reply->ret_code */ |
| 764 | static int drbd_nl_disk_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 765 | struct drbd_nl_cfg_reply *reply) |
| 766 | { |
| 767 | enum drbd_ret_codes retcode; |
| 768 | enum determine_dev_size dd; |
| 769 | sector_t max_possible_sectors; |
| 770 | sector_t min_md_device_sectors; |
| 771 | struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */ |
| 772 | struct inode *inode, *inode2; |
| 773 | struct lru_cache *resync_lru = NULL; |
| 774 | union drbd_state ns, os; |
| 775 | int rv; |
| 776 | int cp_discovered = 0; |
| 777 | int logical_block_size; |
| 778 | |
| 779 | drbd_reconfig_start(mdev); |
| 780 | |
| 781 | /* if you want to reconfigure, please tear down first */ |
| 782 | if (mdev->state.disk > D_DISKLESS) { |
| 783 | retcode = ERR_DISK_CONFIGURED; |
| 784 | goto fail; |
| 785 | } |
| 786 | |
| 787 | /* allocation not in the IO path, cqueue thread context */ |
| 788 | nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL); |
| 789 | if (!nbc) { |
| 790 | retcode = ERR_NOMEM; |
| 791 | goto fail; |
| 792 | } |
| 793 | |
| 794 | nbc->dc.disk_size = DRBD_DISK_SIZE_SECT_DEF; |
| 795 | nbc->dc.on_io_error = DRBD_ON_IO_ERROR_DEF; |
| 796 | nbc->dc.fencing = DRBD_FENCING_DEF; |
| 797 | nbc->dc.max_bio_bvecs = DRBD_MAX_BIO_BVECS_DEF; |
| 798 | |
| 799 | if (!disk_conf_from_tags(mdev, nlp->tag_list, &nbc->dc)) { |
| 800 | retcode = ERR_MANDATORY_TAG; |
| 801 | goto fail; |
| 802 | } |
| 803 | |
| 804 | if (nbc->dc.meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) { |
| 805 | retcode = ERR_MD_IDX_INVALID; |
| 806 | goto fail; |
| 807 | } |
| 808 | |
| 809 | nbc->lo_file = filp_open(nbc->dc.backing_dev, O_RDWR, 0); |
| 810 | if (IS_ERR(nbc->lo_file)) { |
| 811 | dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.backing_dev, |
| 812 | PTR_ERR(nbc->lo_file)); |
| 813 | nbc->lo_file = NULL; |
| 814 | retcode = ERR_OPEN_DISK; |
| 815 | goto fail; |
| 816 | } |
| 817 | |
| 818 | inode = nbc->lo_file->f_dentry->d_inode; |
| 819 | |
| 820 | if (!S_ISBLK(inode->i_mode)) { |
| 821 | retcode = ERR_DISK_NOT_BDEV; |
| 822 | goto fail; |
| 823 | } |
| 824 | |
| 825 | nbc->md_file = filp_open(nbc->dc.meta_dev, O_RDWR, 0); |
| 826 | if (IS_ERR(nbc->md_file)) { |
| 827 | dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.meta_dev, |
| 828 | PTR_ERR(nbc->md_file)); |
| 829 | nbc->md_file = NULL; |
| 830 | retcode = ERR_OPEN_MD_DISK; |
| 831 | goto fail; |
| 832 | } |
| 833 | |
| 834 | inode2 = nbc->md_file->f_dentry->d_inode; |
| 835 | |
| 836 | if (!S_ISBLK(inode2->i_mode)) { |
| 837 | retcode = ERR_MD_NOT_BDEV; |
| 838 | goto fail; |
| 839 | } |
| 840 | |
| 841 | nbc->backing_bdev = inode->i_bdev; |
| 842 | if (bd_claim(nbc->backing_bdev, mdev)) { |
| 843 | printk(KERN_ERR "drbd: bd_claim(%p,%p); failed [%p;%p;%u]\n", |
| 844 | nbc->backing_bdev, mdev, |
| 845 | nbc->backing_bdev->bd_holder, |
| 846 | nbc->backing_bdev->bd_contains->bd_holder, |
| 847 | nbc->backing_bdev->bd_holders); |
| 848 | retcode = ERR_BDCLAIM_DISK; |
| 849 | goto fail; |
| 850 | } |
| 851 | |
| 852 | resync_lru = lc_create("resync", drbd_bm_ext_cache, |
| 853 | 61, sizeof(struct bm_extent), |
| 854 | offsetof(struct bm_extent, lce)); |
| 855 | if (!resync_lru) { |
| 856 | retcode = ERR_NOMEM; |
| 857 | goto release_bdev_fail; |
| 858 | } |
| 859 | |
| 860 | /* meta_dev_idx >= 0: external fixed size, |
| 861 | * possibly multiple drbd sharing one meta device. |
| 862 | * TODO in that case, paranoia check that [md_bdev, meta_dev_idx] is |
| 863 | * not yet used by some other drbd minor! |
| 864 | * (if you use drbd.conf + drbdadm, |
| 865 | * that should check it for you already; but if you don't, or someone |
| 866 | * fooled it, we need to double check here) */ |
| 867 | nbc->md_bdev = inode2->i_bdev; |
| 868 | if (bd_claim(nbc->md_bdev, (nbc->dc.meta_dev_idx < 0) ? (void *)mdev |
| 869 | : (void *) drbd_m_holder)) { |
| 870 | retcode = ERR_BDCLAIM_MD_DISK; |
| 871 | goto release_bdev_fail; |
| 872 | } |
| 873 | |
| 874 | if ((nbc->backing_bdev == nbc->md_bdev) != |
| 875 | (nbc->dc.meta_dev_idx == DRBD_MD_INDEX_INTERNAL || |
| 876 | nbc->dc.meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) { |
| 877 | retcode = ERR_MD_IDX_INVALID; |
| 878 | goto release_bdev2_fail; |
| 879 | } |
| 880 | |
| 881 | /* RT - for drbd_get_max_capacity() DRBD_MD_INDEX_FLEX_INT */ |
| 882 | drbd_md_set_sector_offsets(mdev, nbc); |
| 883 | |
| 884 | if (drbd_get_max_capacity(nbc) < nbc->dc.disk_size) { |
| 885 | dev_err(DEV, "max capacity %llu smaller than disk size %llu\n", |
| 886 | (unsigned long long) drbd_get_max_capacity(nbc), |
| 887 | (unsigned long long) nbc->dc.disk_size); |
| 888 | retcode = ERR_DISK_TO_SMALL; |
| 889 | goto release_bdev2_fail; |
| 890 | } |
| 891 | |
| 892 | if (nbc->dc.meta_dev_idx < 0) { |
| 893 | max_possible_sectors = DRBD_MAX_SECTORS_FLEX; |
| 894 | /* at least one MB, otherwise it does not make sense */ |
| 895 | min_md_device_sectors = (2<<10); |
| 896 | } else { |
| 897 | max_possible_sectors = DRBD_MAX_SECTORS; |
| 898 | min_md_device_sectors = MD_RESERVED_SECT * (nbc->dc.meta_dev_idx + 1); |
| 899 | } |
| 900 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 901 | if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) { |
| 902 | retcode = ERR_MD_DISK_TO_SMALL; |
| 903 | dev_warn(DEV, "refusing attach: md-device too small, " |
| 904 | "at least %llu sectors needed for this meta-disk type\n", |
| 905 | (unsigned long long) min_md_device_sectors); |
| 906 | goto release_bdev2_fail; |
| 907 | } |
| 908 | |
| 909 | /* Make sure the new disk is big enough |
| 910 | * (we may currently be R_PRIMARY with no local disk...) */ |
| 911 | if (drbd_get_max_capacity(nbc) < |
| 912 | drbd_get_capacity(mdev->this_bdev)) { |
| 913 | retcode = ERR_DISK_TO_SMALL; |
| 914 | goto release_bdev2_fail; |
| 915 | } |
| 916 | |
| 917 | nbc->known_size = drbd_get_capacity(nbc->backing_bdev); |
| 918 | |
Lars Ellenberg | 1352994 | 2009-10-12 19:07:49 +0200 | [diff] [blame] | 919 | if (nbc->known_size > max_possible_sectors) { |
| 920 | dev_warn(DEV, "==> truncating very big lower level device " |
| 921 | "to currently maximum possible %llu sectors <==\n", |
| 922 | (unsigned long long) max_possible_sectors); |
| 923 | if (nbc->dc.meta_dev_idx >= 0) |
| 924 | dev_warn(DEV, "==>> using internal or flexible " |
| 925 | "meta data may help <<==\n"); |
| 926 | } |
| 927 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 928 | drbd_suspend_io(mdev); |
| 929 | /* also wait for the last barrier ack. */ |
| 930 | wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_pending_cnt)); |
| 931 | /* and for any other previously queued work */ |
| 932 | drbd_flush_workqueue(mdev); |
| 933 | |
| 934 | retcode = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE); |
| 935 | drbd_resume_io(mdev); |
| 936 | if (retcode < SS_SUCCESS) |
| 937 | goto release_bdev2_fail; |
| 938 | |
| 939 | if (!get_ldev_if_state(mdev, D_ATTACHING)) |
| 940 | goto force_diskless; |
| 941 | |
| 942 | drbd_md_set_sector_offsets(mdev, nbc); |
| 943 | |
Lars Ellenberg | 4aa83b7 | 2010-02-26 16:53:24 +0100 | [diff] [blame] | 944 | /* allocate a second IO page if logical_block_size != 512 */ |
| 945 | logical_block_size = bdev_logical_block_size(nbc->md_bdev); |
| 946 | if (logical_block_size == 0) |
| 947 | logical_block_size = MD_SECTOR_SIZE; |
| 948 | |
| 949 | if (logical_block_size != MD_SECTOR_SIZE) { |
| 950 | if (!mdev->md_io_tmpp) { |
| 951 | struct page *page = alloc_page(GFP_NOIO); |
| 952 | if (!page) |
| 953 | goto force_diskless_dec; |
| 954 | |
| 955 | dev_warn(DEV, "Meta data's bdev logical_block_size = %d != %d\n", |
| 956 | logical_block_size, MD_SECTOR_SIZE); |
| 957 | dev_warn(DEV, "Workaround engaged (has performance impact).\n"); |
| 958 | |
| 959 | mdev->md_io_tmpp = page; |
| 960 | } |
| 961 | } |
| 962 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 963 | if (!mdev->bitmap) { |
| 964 | if (drbd_bm_init(mdev)) { |
| 965 | retcode = ERR_NOMEM; |
| 966 | goto force_diskless_dec; |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | retcode = drbd_md_read(mdev, nbc); |
| 971 | if (retcode != NO_ERROR) |
| 972 | goto force_diskless_dec; |
| 973 | |
| 974 | if (mdev->state.conn < C_CONNECTED && |
| 975 | mdev->state.role == R_PRIMARY && |
| 976 | (mdev->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) { |
| 977 | dev_err(DEV, "Can only attach to data with current UUID=%016llX\n", |
| 978 | (unsigned long long)mdev->ed_uuid); |
| 979 | retcode = ERR_DATA_NOT_CURRENT; |
| 980 | goto force_diskless_dec; |
| 981 | } |
| 982 | |
| 983 | /* Since we are diskless, fix the activity log first... */ |
| 984 | if (drbd_check_al_size(mdev)) { |
| 985 | retcode = ERR_NOMEM; |
| 986 | goto force_diskless_dec; |
| 987 | } |
| 988 | |
| 989 | /* Prevent shrinking of consistent devices ! */ |
| 990 | if (drbd_md_test_flag(nbc, MDF_CONSISTENT) && |
Philipp Reisner | a393db6 | 2009-12-22 13:35:52 +0100 | [diff] [blame] | 991 | drbd_new_dev_size(mdev, nbc, 0) < nbc->md.la_size_sect) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 992 | dev_warn(DEV, "refusing to truncate a consistent device\n"); |
| 993 | retcode = ERR_DISK_TO_SMALL; |
| 994 | goto force_diskless_dec; |
| 995 | } |
| 996 | |
| 997 | if (!drbd_al_read_log(mdev, nbc)) { |
| 998 | retcode = ERR_IO_MD_DISK; |
| 999 | goto force_diskless_dec; |
| 1000 | } |
| 1001 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1002 | /* Reset the "barriers don't work" bits here, then force meta data to |
| 1003 | * be written, to ensure we determine if barriers are supported. */ |
| 1004 | if (nbc->dc.no_md_flush) |
| 1005 | set_bit(MD_NO_BARRIER, &mdev->flags); |
| 1006 | else |
| 1007 | clear_bit(MD_NO_BARRIER, &mdev->flags); |
| 1008 | |
| 1009 | /* Point of no return reached. |
| 1010 | * Devices and memory are no longer released by error cleanup below. |
| 1011 | * now mdev takes over responsibility, and the state engine should |
| 1012 | * clean it up somewhere. */ |
| 1013 | D_ASSERT(mdev->ldev == NULL); |
| 1014 | mdev->ldev = nbc; |
| 1015 | mdev->resync = resync_lru; |
| 1016 | nbc = NULL; |
| 1017 | resync_lru = NULL; |
| 1018 | |
| 1019 | mdev->write_ordering = WO_bio_barrier; |
| 1020 | drbd_bump_write_ordering(mdev, WO_bio_barrier); |
| 1021 | |
| 1022 | if (drbd_md_test_flag(mdev->ldev, MDF_CRASHED_PRIMARY)) |
| 1023 | set_bit(CRASHED_PRIMARY, &mdev->flags); |
| 1024 | else |
| 1025 | clear_bit(CRASHED_PRIMARY, &mdev->flags); |
| 1026 | |
| 1027 | if (drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND)) { |
| 1028 | set_bit(CRASHED_PRIMARY, &mdev->flags); |
| 1029 | cp_discovered = 1; |
| 1030 | } |
| 1031 | |
| 1032 | mdev->send_cnt = 0; |
| 1033 | mdev->recv_cnt = 0; |
| 1034 | mdev->read_cnt = 0; |
| 1035 | mdev->writ_cnt = 0; |
| 1036 | |
| 1037 | drbd_setup_queue_param(mdev, DRBD_MAX_SEGMENT_SIZE); |
| 1038 | |
| 1039 | /* If I am currently not R_PRIMARY, |
| 1040 | * but meta data primary indicator is set, |
| 1041 | * I just now recover from a hard crash, |
| 1042 | * and have been R_PRIMARY before that crash. |
| 1043 | * |
| 1044 | * Now, if I had no connection before that crash |
| 1045 | * (have been degraded R_PRIMARY), chances are that |
| 1046 | * I won't find my peer now either. |
| 1047 | * |
| 1048 | * In that case, and _only_ in that case, |
| 1049 | * we use the degr-wfc-timeout instead of the default, |
| 1050 | * so we can automatically recover from a crash of a |
| 1051 | * degraded but active "cluster" after a certain timeout. |
| 1052 | */ |
| 1053 | clear_bit(USE_DEGR_WFC_T, &mdev->flags); |
| 1054 | if (mdev->state.role != R_PRIMARY && |
| 1055 | drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) && |
| 1056 | !drbd_md_test_flag(mdev->ldev, MDF_CONNECTED_IND)) |
| 1057 | set_bit(USE_DEGR_WFC_T, &mdev->flags); |
| 1058 | |
Philipp Reisner | a393db6 | 2009-12-22 13:35:52 +0100 | [diff] [blame] | 1059 | dd = drbd_determin_dev_size(mdev, 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1060 | if (dd == dev_size_error) { |
| 1061 | retcode = ERR_NOMEM_BITMAP; |
| 1062 | goto force_diskless_dec; |
| 1063 | } else if (dd == grew) |
| 1064 | set_bit(RESYNC_AFTER_NEG, &mdev->flags); |
| 1065 | |
| 1066 | if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) { |
| 1067 | dev_info(DEV, "Assuming that all blocks are out of sync " |
| 1068 | "(aka FullSync)\n"); |
| 1069 | if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from attaching")) { |
| 1070 | retcode = ERR_IO_MD_DISK; |
| 1071 | goto force_diskless_dec; |
| 1072 | } |
| 1073 | } else { |
| 1074 | if (drbd_bitmap_io(mdev, &drbd_bm_read, "read from attaching") < 0) { |
| 1075 | retcode = ERR_IO_MD_DISK; |
| 1076 | goto force_diskless_dec; |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | if (cp_discovered) { |
| 1081 | drbd_al_apply_to_bm(mdev); |
| 1082 | drbd_al_to_on_disk_bm(mdev); |
| 1083 | } |
| 1084 | |
| 1085 | spin_lock_irq(&mdev->req_lock); |
| 1086 | os = mdev->state; |
| 1087 | ns.i = os.i; |
| 1088 | /* If MDF_CONSISTENT is not set go into inconsistent state, |
| 1089 | otherwise investigate MDF_WasUpToDate... |
| 1090 | If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state, |
| 1091 | otherwise into D_CONSISTENT state. |
| 1092 | */ |
| 1093 | if (drbd_md_test_flag(mdev->ldev, MDF_CONSISTENT)) { |
| 1094 | if (drbd_md_test_flag(mdev->ldev, MDF_WAS_UP_TO_DATE)) |
| 1095 | ns.disk = D_CONSISTENT; |
| 1096 | else |
| 1097 | ns.disk = D_OUTDATED; |
| 1098 | } else { |
| 1099 | ns.disk = D_INCONSISTENT; |
| 1100 | } |
| 1101 | |
| 1102 | if (drbd_md_test_flag(mdev->ldev, MDF_PEER_OUT_DATED)) |
| 1103 | ns.pdsk = D_OUTDATED; |
| 1104 | |
| 1105 | if ( ns.disk == D_CONSISTENT && |
| 1106 | (ns.pdsk == D_OUTDATED || mdev->ldev->dc.fencing == FP_DONT_CARE)) |
| 1107 | ns.disk = D_UP_TO_DATE; |
| 1108 | |
| 1109 | /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND, |
| 1110 | MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before |
| 1111 | this point, because drbd_request_state() modifies these |
| 1112 | flags. */ |
| 1113 | |
| 1114 | /* In case we are C_CONNECTED postpone any decision on the new disk |
| 1115 | state after the negotiation phase. */ |
| 1116 | if (mdev->state.conn == C_CONNECTED) { |
| 1117 | mdev->new_state_tmp.i = ns.i; |
| 1118 | ns.i = os.i; |
| 1119 | ns.disk = D_NEGOTIATING; |
Philipp Reisner | dc66c74 | 2010-06-02 14:31:29 +0200 | [diff] [blame] | 1120 | |
| 1121 | /* We expect to receive up-to-date UUIDs soon. |
| 1122 | To avoid a race in receive_state, free p_uuid while |
| 1123 | holding req_lock. I.e. atomic with the state change */ |
| 1124 | kfree(mdev->p_uuid); |
| 1125 | mdev->p_uuid = NULL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL); |
| 1129 | ns = mdev->state; |
| 1130 | spin_unlock_irq(&mdev->req_lock); |
| 1131 | |
| 1132 | if (rv < SS_SUCCESS) |
| 1133 | goto force_diskless_dec; |
| 1134 | |
| 1135 | if (mdev->state.role == R_PRIMARY) |
| 1136 | mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1; |
| 1137 | else |
| 1138 | mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1; |
| 1139 | |
| 1140 | drbd_md_mark_dirty(mdev); |
| 1141 | drbd_md_sync(mdev); |
| 1142 | |
| 1143 | kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE); |
| 1144 | put_ldev(mdev); |
| 1145 | reply->ret_code = retcode; |
| 1146 | drbd_reconfig_done(mdev); |
| 1147 | return 0; |
| 1148 | |
| 1149 | force_diskless_dec: |
| 1150 | put_ldev(mdev); |
| 1151 | force_diskless: |
| 1152 | drbd_force_state(mdev, NS(disk, D_DISKLESS)); |
| 1153 | drbd_md_sync(mdev); |
| 1154 | release_bdev2_fail: |
| 1155 | if (nbc) |
| 1156 | bd_release(nbc->md_bdev); |
| 1157 | release_bdev_fail: |
| 1158 | if (nbc) |
| 1159 | bd_release(nbc->backing_bdev); |
| 1160 | fail: |
| 1161 | if (nbc) { |
| 1162 | if (nbc->lo_file) |
| 1163 | fput(nbc->lo_file); |
| 1164 | if (nbc->md_file) |
| 1165 | fput(nbc->md_file); |
| 1166 | kfree(nbc); |
| 1167 | } |
| 1168 | lc_destroy(resync_lru); |
| 1169 | |
| 1170 | reply->ret_code = retcode; |
| 1171 | drbd_reconfig_done(mdev); |
| 1172 | return 0; |
| 1173 | } |
| 1174 | |
| 1175 | static int drbd_nl_detach(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1176 | struct drbd_nl_cfg_reply *reply) |
| 1177 | { |
| 1178 | reply->ret_code = drbd_request_state(mdev, NS(disk, D_DISKLESS)); |
| 1179 | return 0; |
| 1180 | } |
| 1181 | |
| 1182 | static int drbd_nl_net_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1183 | struct drbd_nl_cfg_reply *reply) |
| 1184 | { |
| 1185 | int i, ns; |
| 1186 | enum drbd_ret_codes retcode; |
| 1187 | struct net_conf *new_conf = NULL; |
| 1188 | struct crypto_hash *tfm = NULL; |
| 1189 | struct crypto_hash *integrity_w_tfm = NULL; |
| 1190 | struct crypto_hash *integrity_r_tfm = NULL; |
| 1191 | struct hlist_head *new_tl_hash = NULL; |
| 1192 | struct hlist_head *new_ee_hash = NULL; |
| 1193 | struct drbd_conf *odev; |
| 1194 | char hmac_name[CRYPTO_MAX_ALG_NAME]; |
| 1195 | void *int_dig_out = NULL; |
| 1196 | void *int_dig_in = NULL; |
| 1197 | void *int_dig_vv = NULL; |
| 1198 | struct sockaddr *new_my_addr, *new_peer_addr, *taken_addr; |
| 1199 | |
| 1200 | drbd_reconfig_start(mdev); |
| 1201 | |
| 1202 | if (mdev->state.conn > C_STANDALONE) { |
| 1203 | retcode = ERR_NET_CONFIGURED; |
| 1204 | goto fail; |
| 1205 | } |
| 1206 | |
| 1207 | /* allocation not in the IO path, cqueue thread context */ |
Julia Lawall | 2db4e42 | 2010-05-13 22:02:21 +0200 | [diff] [blame] | 1208 | new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1209 | if (!new_conf) { |
| 1210 | retcode = ERR_NOMEM; |
| 1211 | goto fail; |
| 1212 | } |
| 1213 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1214 | new_conf->timeout = DRBD_TIMEOUT_DEF; |
| 1215 | new_conf->try_connect_int = DRBD_CONNECT_INT_DEF; |
| 1216 | new_conf->ping_int = DRBD_PING_INT_DEF; |
| 1217 | new_conf->max_epoch_size = DRBD_MAX_EPOCH_SIZE_DEF; |
| 1218 | new_conf->max_buffers = DRBD_MAX_BUFFERS_DEF; |
| 1219 | new_conf->unplug_watermark = DRBD_UNPLUG_WATERMARK_DEF; |
| 1220 | new_conf->sndbuf_size = DRBD_SNDBUF_SIZE_DEF; |
| 1221 | new_conf->rcvbuf_size = DRBD_RCVBUF_SIZE_DEF; |
| 1222 | new_conf->ko_count = DRBD_KO_COUNT_DEF; |
| 1223 | new_conf->after_sb_0p = DRBD_AFTER_SB_0P_DEF; |
| 1224 | new_conf->after_sb_1p = DRBD_AFTER_SB_1P_DEF; |
| 1225 | new_conf->after_sb_2p = DRBD_AFTER_SB_2P_DEF; |
| 1226 | new_conf->want_lose = 0; |
| 1227 | new_conf->two_primaries = 0; |
| 1228 | new_conf->wire_protocol = DRBD_PROT_C; |
| 1229 | new_conf->ping_timeo = DRBD_PING_TIMEO_DEF; |
| 1230 | new_conf->rr_conflict = DRBD_RR_CONFLICT_DEF; |
| 1231 | |
| 1232 | if (!net_conf_from_tags(mdev, nlp->tag_list, new_conf)) { |
| 1233 | retcode = ERR_MANDATORY_TAG; |
| 1234 | goto fail; |
| 1235 | } |
| 1236 | |
| 1237 | if (new_conf->two_primaries |
| 1238 | && (new_conf->wire_protocol != DRBD_PROT_C)) { |
| 1239 | retcode = ERR_NOT_PROTO_C; |
| 1240 | goto fail; |
| 1241 | }; |
| 1242 | |
| 1243 | if (mdev->state.role == R_PRIMARY && new_conf->want_lose) { |
| 1244 | retcode = ERR_DISCARD; |
| 1245 | goto fail; |
| 1246 | } |
| 1247 | |
| 1248 | retcode = NO_ERROR; |
| 1249 | |
| 1250 | new_my_addr = (struct sockaddr *)&new_conf->my_addr; |
| 1251 | new_peer_addr = (struct sockaddr *)&new_conf->peer_addr; |
| 1252 | for (i = 0; i < minor_count; i++) { |
| 1253 | odev = minor_to_mdev(i); |
| 1254 | if (!odev || odev == mdev) |
| 1255 | continue; |
| 1256 | if (get_net_conf(odev)) { |
| 1257 | taken_addr = (struct sockaddr *)&odev->net_conf->my_addr; |
| 1258 | if (new_conf->my_addr_len == odev->net_conf->my_addr_len && |
| 1259 | !memcmp(new_my_addr, taken_addr, new_conf->my_addr_len)) |
| 1260 | retcode = ERR_LOCAL_ADDR; |
| 1261 | |
| 1262 | taken_addr = (struct sockaddr *)&odev->net_conf->peer_addr; |
| 1263 | if (new_conf->peer_addr_len == odev->net_conf->peer_addr_len && |
| 1264 | !memcmp(new_peer_addr, taken_addr, new_conf->peer_addr_len)) |
| 1265 | retcode = ERR_PEER_ADDR; |
| 1266 | |
| 1267 | put_net_conf(odev); |
| 1268 | if (retcode != NO_ERROR) |
| 1269 | goto fail; |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | if (new_conf->cram_hmac_alg[0] != 0) { |
| 1274 | snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)", |
| 1275 | new_conf->cram_hmac_alg); |
| 1276 | tfm = crypto_alloc_hash(hmac_name, 0, CRYPTO_ALG_ASYNC); |
| 1277 | if (IS_ERR(tfm)) { |
| 1278 | tfm = NULL; |
| 1279 | retcode = ERR_AUTH_ALG; |
| 1280 | goto fail; |
| 1281 | } |
| 1282 | |
Philipp Reisner | 0798219 | 2009-12-28 16:58:38 +0100 | [diff] [blame] | 1283 | if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1284 | retcode = ERR_AUTH_ALG_ND; |
| 1285 | goto fail; |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | if (new_conf->integrity_alg[0]) { |
| 1290 | integrity_w_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC); |
| 1291 | if (IS_ERR(integrity_w_tfm)) { |
| 1292 | integrity_w_tfm = NULL; |
| 1293 | retcode=ERR_INTEGRITY_ALG; |
| 1294 | goto fail; |
| 1295 | } |
| 1296 | |
| 1297 | if (!drbd_crypto_is_hash(crypto_hash_tfm(integrity_w_tfm))) { |
| 1298 | retcode=ERR_INTEGRITY_ALG_ND; |
| 1299 | goto fail; |
| 1300 | } |
| 1301 | |
| 1302 | integrity_r_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC); |
| 1303 | if (IS_ERR(integrity_r_tfm)) { |
| 1304 | integrity_r_tfm = NULL; |
| 1305 | retcode=ERR_INTEGRITY_ALG; |
| 1306 | goto fail; |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | ns = new_conf->max_epoch_size/8; |
| 1311 | if (mdev->tl_hash_s != ns) { |
| 1312 | new_tl_hash = kzalloc(ns*sizeof(void *), GFP_KERNEL); |
| 1313 | if (!new_tl_hash) { |
| 1314 | retcode = ERR_NOMEM; |
| 1315 | goto fail; |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | ns = new_conf->max_buffers/8; |
| 1320 | if (new_conf->two_primaries && (mdev->ee_hash_s != ns)) { |
| 1321 | new_ee_hash = kzalloc(ns*sizeof(void *), GFP_KERNEL); |
| 1322 | if (!new_ee_hash) { |
| 1323 | retcode = ERR_NOMEM; |
| 1324 | goto fail; |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0; |
| 1329 | |
| 1330 | if (integrity_w_tfm) { |
| 1331 | i = crypto_hash_digestsize(integrity_w_tfm); |
| 1332 | int_dig_out = kmalloc(i, GFP_KERNEL); |
| 1333 | if (!int_dig_out) { |
| 1334 | retcode = ERR_NOMEM; |
| 1335 | goto fail; |
| 1336 | } |
| 1337 | int_dig_in = kmalloc(i, GFP_KERNEL); |
| 1338 | if (!int_dig_in) { |
| 1339 | retcode = ERR_NOMEM; |
| 1340 | goto fail; |
| 1341 | } |
| 1342 | int_dig_vv = kmalloc(i, GFP_KERNEL); |
| 1343 | if (!int_dig_vv) { |
| 1344 | retcode = ERR_NOMEM; |
| 1345 | goto fail; |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | if (!mdev->bitmap) { |
| 1350 | if(drbd_bm_init(mdev)) { |
| 1351 | retcode = ERR_NOMEM; |
| 1352 | goto fail; |
| 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | spin_lock_irq(&mdev->req_lock); |
| 1357 | if (mdev->net_conf != NULL) { |
| 1358 | retcode = ERR_NET_CONFIGURED; |
| 1359 | spin_unlock_irq(&mdev->req_lock); |
| 1360 | goto fail; |
| 1361 | } |
| 1362 | mdev->net_conf = new_conf; |
| 1363 | |
| 1364 | mdev->send_cnt = 0; |
| 1365 | mdev->recv_cnt = 0; |
| 1366 | |
| 1367 | if (new_tl_hash) { |
| 1368 | kfree(mdev->tl_hash); |
| 1369 | mdev->tl_hash_s = mdev->net_conf->max_epoch_size/8; |
| 1370 | mdev->tl_hash = new_tl_hash; |
| 1371 | } |
| 1372 | |
| 1373 | if (new_ee_hash) { |
| 1374 | kfree(mdev->ee_hash); |
| 1375 | mdev->ee_hash_s = mdev->net_conf->max_buffers/8; |
| 1376 | mdev->ee_hash = new_ee_hash; |
| 1377 | } |
| 1378 | |
| 1379 | crypto_free_hash(mdev->cram_hmac_tfm); |
| 1380 | mdev->cram_hmac_tfm = tfm; |
| 1381 | |
| 1382 | crypto_free_hash(mdev->integrity_w_tfm); |
| 1383 | mdev->integrity_w_tfm = integrity_w_tfm; |
| 1384 | |
| 1385 | crypto_free_hash(mdev->integrity_r_tfm); |
| 1386 | mdev->integrity_r_tfm = integrity_r_tfm; |
| 1387 | |
| 1388 | kfree(mdev->int_dig_out); |
| 1389 | kfree(mdev->int_dig_in); |
| 1390 | kfree(mdev->int_dig_vv); |
| 1391 | mdev->int_dig_out=int_dig_out; |
| 1392 | mdev->int_dig_in=int_dig_in; |
| 1393 | mdev->int_dig_vv=int_dig_vv; |
| 1394 | spin_unlock_irq(&mdev->req_lock); |
| 1395 | |
| 1396 | retcode = _drbd_request_state(mdev, NS(conn, C_UNCONNECTED), CS_VERBOSE); |
| 1397 | |
| 1398 | kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE); |
| 1399 | reply->ret_code = retcode; |
| 1400 | drbd_reconfig_done(mdev); |
| 1401 | return 0; |
| 1402 | |
| 1403 | fail: |
| 1404 | kfree(int_dig_out); |
| 1405 | kfree(int_dig_in); |
| 1406 | kfree(int_dig_vv); |
| 1407 | crypto_free_hash(tfm); |
| 1408 | crypto_free_hash(integrity_w_tfm); |
| 1409 | crypto_free_hash(integrity_r_tfm); |
| 1410 | kfree(new_tl_hash); |
| 1411 | kfree(new_ee_hash); |
| 1412 | kfree(new_conf); |
| 1413 | |
| 1414 | reply->ret_code = retcode; |
| 1415 | drbd_reconfig_done(mdev); |
| 1416 | return 0; |
| 1417 | } |
| 1418 | |
| 1419 | static int drbd_nl_disconnect(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1420 | struct drbd_nl_cfg_reply *reply) |
| 1421 | { |
| 1422 | int retcode; |
| 1423 | |
| 1424 | retcode = _drbd_request_state(mdev, NS(conn, C_DISCONNECTING), CS_ORDERED); |
| 1425 | |
| 1426 | if (retcode == SS_NOTHING_TO_DO) |
| 1427 | goto done; |
| 1428 | else if (retcode == SS_ALREADY_STANDALONE) |
| 1429 | goto done; |
| 1430 | else if (retcode == SS_PRIMARY_NOP) { |
| 1431 | /* Our statche checking code wants to see the peer outdated. */ |
| 1432 | retcode = drbd_request_state(mdev, NS2(conn, C_DISCONNECTING, |
| 1433 | pdsk, D_OUTDATED)); |
| 1434 | } else if (retcode == SS_CW_FAILED_BY_PEER) { |
| 1435 | /* The peer probably wants to see us outdated. */ |
| 1436 | retcode = _drbd_request_state(mdev, NS2(conn, C_DISCONNECTING, |
| 1437 | disk, D_OUTDATED), |
| 1438 | CS_ORDERED); |
| 1439 | if (retcode == SS_IS_DISKLESS || retcode == SS_LOWER_THAN_OUTDATED) { |
| 1440 | drbd_force_state(mdev, NS(conn, C_DISCONNECTING)); |
| 1441 | retcode = SS_SUCCESS; |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | if (retcode < SS_SUCCESS) |
| 1446 | goto fail; |
| 1447 | |
| 1448 | if (wait_event_interruptible(mdev->state_wait, |
| 1449 | mdev->state.conn != C_DISCONNECTING)) { |
| 1450 | /* Do not test for mdev->state.conn == C_STANDALONE, since |
| 1451 | someone else might connect us in the mean time! */ |
| 1452 | retcode = ERR_INTR; |
| 1453 | goto fail; |
| 1454 | } |
| 1455 | |
| 1456 | done: |
| 1457 | retcode = NO_ERROR; |
| 1458 | fail: |
| 1459 | drbd_md_sync(mdev); |
| 1460 | reply->ret_code = retcode; |
| 1461 | return 0; |
| 1462 | } |
| 1463 | |
| 1464 | void resync_after_online_grow(struct drbd_conf *mdev) |
| 1465 | { |
| 1466 | int iass; /* I am sync source */ |
| 1467 | |
| 1468 | dev_info(DEV, "Resync of new storage after online grow\n"); |
| 1469 | if (mdev->state.role != mdev->state.peer) |
| 1470 | iass = (mdev->state.role == R_PRIMARY); |
| 1471 | else |
| 1472 | iass = test_bit(DISCARD_CONCURRENT, &mdev->flags); |
| 1473 | |
| 1474 | if (iass) |
| 1475 | drbd_start_resync(mdev, C_SYNC_SOURCE); |
| 1476 | else |
| 1477 | _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE); |
| 1478 | } |
| 1479 | |
| 1480 | static int drbd_nl_resize(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1481 | struct drbd_nl_cfg_reply *reply) |
| 1482 | { |
| 1483 | struct resize rs; |
| 1484 | int retcode = NO_ERROR; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1485 | enum determine_dev_size dd; |
Philipp Reisner | 6495d2c | 2010-03-24 16:07:04 +0100 | [diff] [blame] | 1486 | enum dds_flags ddsf; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1487 | |
| 1488 | memset(&rs, 0, sizeof(struct resize)); |
| 1489 | if (!resize_from_tags(mdev, nlp->tag_list, &rs)) { |
| 1490 | retcode = ERR_MANDATORY_TAG; |
| 1491 | goto fail; |
| 1492 | } |
| 1493 | |
| 1494 | if (mdev->state.conn > C_CONNECTED) { |
| 1495 | retcode = ERR_RESIZE_RESYNC; |
| 1496 | goto fail; |
| 1497 | } |
| 1498 | |
| 1499 | if (mdev->state.role == R_SECONDARY && |
| 1500 | mdev->state.peer == R_SECONDARY) { |
| 1501 | retcode = ERR_NO_PRIMARY; |
| 1502 | goto fail; |
| 1503 | } |
| 1504 | |
| 1505 | if (!get_ldev(mdev)) { |
| 1506 | retcode = ERR_NO_DISK; |
| 1507 | goto fail; |
| 1508 | } |
| 1509 | |
Philipp Reisner | 6495d2c | 2010-03-24 16:07:04 +0100 | [diff] [blame] | 1510 | if (rs.no_resync && mdev->agreed_pro_version < 93) { |
| 1511 | retcode = ERR_NEED_APV_93; |
| 1512 | goto fail; |
| 1513 | } |
| 1514 | |
Philipp Reisner | 087c249 | 2010-03-26 13:49:56 +0100 | [diff] [blame] | 1515 | if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1516 | mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1517 | |
| 1518 | mdev->ldev->dc.disk_size = (sector_t)rs.resize_size; |
Philipp Reisner | 6495d2c | 2010-03-24 16:07:04 +0100 | [diff] [blame] | 1519 | ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0); |
| 1520 | dd = drbd_determin_dev_size(mdev, ddsf); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1521 | drbd_md_sync(mdev); |
| 1522 | put_ldev(mdev); |
| 1523 | if (dd == dev_size_error) { |
| 1524 | retcode = ERR_NOMEM_BITMAP; |
| 1525 | goto fail; |
| 1526 | } |
| 1527 | |
Philipp Reisner | 087c249 | 2010-03-26 13:49:56 +0100 | [diff] [blame] | 1528 | if (mdev->state.conn == C_CONNECTED) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1529 | if (dd == grew) |
| 1530 | set_bit(RESIZE_PENDING, &mdev->flags); |
| 1531 | |
| 1532 | drbd_send_uuids(mdev); |
Philipp Reisner | 6495d2c | 2010-03-24 16:07:04 +0100 | [diff] [blame] | 1533 | drbd_send_sizes(mdev, 1, ddsf); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | fail: |
| 1537 | reply->ret_code = retcode; |
| 1538 | return 0; |
| 1539 | } |
| 1540 | |
| 1541 | static int drbd_nl_syncer_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1542 | struct drbd_nl_cfg_reply *reply) |
| 1543 | { |
| 1544 | int retcode = NO_ERROR; |
| 1545 | int err; |
| 1546 | int ovr; /* online verify running */ |
| 1547 | int rsr; /* re-sync running */ |
| 1548 | struct crypto_hash *verify_tfm = NULL; |
| 1549 | struct crypto_hash *csums_tfm = NULL; |
| 1550 | struct syncer_conf sc; |
| 1551 | cpumask_var_t new_cpu_mask; |
| 1552 | |
| 1553 | if (!zalloc_cpumask_var(&new_cpu_mask, GFP_KERNEL)) { |
| 1554 | retcode = ERR_NOMEM; |
| 1555 | goto fail; |
| 1556 | } |
| 1557 | |
| 1558 | if (nlp->flags & DRBD_NL_SET_DEFAULTS) { |
| 1559 | memset(&sc, 0, sizeof(struct syncer_conf)); |
| 1560 | sc.rate = DRBD_RATE_DEF; |
| 1561 | sc.after = DRBD_AFTER_DEF; |
| 1562 | sc.al_extents = DRBD_AL_EXTENTS_DEF; |
Philipp Reisner | 265be2d | 2010-05-31 10:14:17 +0200 | [diff] [blame^] | 1563 | sc.on_no_data = DRBD_ON_NO_DATA_DEF; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1564 | } else |
| 1565 | memcpy(&sc, &mdev->sync_conf, sizeof(struct syncer_conf)); |
| 1566 | |
| 1567 | if (!syncer_conf_from_tags(mdev, nlp->tag_list, &sc)) { |
| 1568 | retcode = ERR_MANDATORY_TAG; |
| 1569 | goto fail; |
| 1570 | } |
| 1571 | |
| 1572 | /* re-sync running */ |
| 1573 | rsr = ( mdev->state.conn == C_SYNC_SOURCE || |
| 1574 | mdev->state.conn == C_SYNC_TARGET || |
| 1575 | mdev->state.conn == C_PAUSED_SYNC_S || |
| 1576 | mdev->state.conn == C_PAUSED_SYNC_T ); |
| 1577 | |
| 1578 | if (rsr && strcmp(sc.csums_alg, mdev->sync_conf.csums_alg)) { |
| 1579 | retcode = ERR_CSUMS_RESYNC_RUNNING; |
| 1580 | goto fail; |
| 1581 | } |
| 1582 | |
| 1583 | if (!rsr && sc.csums_alg[0]) { |
| 1584 | csums_tfm = crypto_alloc_hash(sc.csums_alg, 0, CRYPTO_ALG_ASYNC); |
| 1585 | if (IS_ERR(csums_tfm)) { |
| 1586 | csums_tfm = NULL; |
| 1587 | retcode = ERR_CSUMS_ALG; |
| 1588 | goto fail; |
| 1589 | } |
| 1590 | |
| 1591 | if (!drbd_crypto_is_hash(crypto_hash_tfm(csums_tfm))) { |
| 1592 | retcode = ERR_CSUMS_ALG_ND; |
| 1593 | goto fail; |
| 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | /* online verify running */ |
| 1598 | ovr = (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T); |
| 1599 | |
| 1600 | if (ovr) { |
| 1601 | if (strcmp(sc.verify_alg, mdev->sync_conf.verify_alg)) { |
| 1602 | retcode = ERR_VERIFY_RUNNING; |
| 1603 | goto fail; |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | if (!ovr && sc.verify_alg[0]) { |
| 1608 | verify_tfm = crypto_alloc_hash(sc.verify_alg, 0, CRYPTO_ALG_ASYNC); |
| 1609 | if (IS_ERR(verify_tfm)) { |
| 1610 | verify_tfm = NULL; |
| 1611 | retcode = ERR_VERIFY_ALG; |
| 1612 | goto fail; |
| 1613 | } |
| 1614 | |
| 1615 | if (!drbd_crypto_is_hash(crypto_hash_tfm(verify_tfm))) { |
| 1616 | retcode = ERR_VERIFY_ALG_ND; |
| 1617 | goto fail; |
| 1618 | } |
| 1619 | } |
| 1620 | |
| 1621 | /* silently ignore cpu mask on UP kernel */ |
| 1622 | if (nr_cpu_ids > 1 && sc.cpu_mask[0] != 0) { |
| 1623 | err = __bitmap_parse(sc.cpu_mask, 32, 0, |
| 1624 | cpumask_bits(new_cpu_mask), nr_cpu_ids); |
| 1625 | if (err) { |
| 1626 | dev_warn(DEV, "__bitmap_parse() failed with %d\n", err); |
| 1627 | retcode = ERR_CPU_MASK_PARSE; |
| 1628 | goto fail; |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | ERR_IF (sc.rate < 1) sc.rate = 1; |
| 1633 | ERR_IF (sc.al_extents < 7) sc.al_extents = 127; /* arbitrary minimum */ |
| 1634 | #define AL_MAX ((MD_AL_MAX_SIZE-1) * AL_EXTENTS_PT) |
| 1635 | if (sc.al_extents > AL_MAX) { |
| 1636 | dev_err(DEV, "sc.al_extents > %d\n", AL_MAX); |
| 1637 | sc.al_extents = AL_MAX; |
| 1638 | } |
| 1639 | #undef AL_MAX |
| 1640 | |
| 1641 | /* most sanity checks done, try to assign the new sync-after |
| 1642 | * dependency. need to hold the global lock in there, |
| 1643 | * to avoid a race in the dependency loop check. */ |
| 1644 | retcode = drbd_alter_sa(mdev, sc.after); |
| 1645 | if (retcode != NO_ERROR) |
| 1646 | goto fail; |
| 1647 | |
| 1648 | /* ok, assign the rest of it as well. |
| 1649 | * lock against receive_SyncParam() */ |
| 1650 | spin_lock(&mdev->peer_seq_lock); |
| 1651 | mdev->sync_conf = sc; |
| 1652 | |
| 1653 | if (!rsr) { |
| 1654 | crypto_free_hash(mdev->csums_tfm); |
| 1655 | mdev->csums_tfm = csums_tfm; |
| 1656 | csums_tfm = NULL; |
| 1657 | } |
| 1658 | |
| 1659 | if (!ovr) { |
| 1660 | crypto_free_hash(mdev->verify_tfm); |
| 1661 | mdev->verify_tfm = verify_tfm; |
| 1662 | verify_tfm = NULL; |
| 1663 | } |
| 1664 | spin_unlock(&mdev->peer_seq_lock); |
| 1665 | |
| 1666 | if (get_ldev(mdev)) { |
| 1667 | wait_event(mdev->al_wait, lc_try_lock(mdev->act_log)); |
| 1668 | drbd_al_shrink(mdev); |
| 1669 | err = drbd_check_al_size(mdev); |
| 1670 | lc_unlock(mdev->act_log); |
| 1671 | wake_up(&mdev->al_wait); |
| 1672 | |
| 1673 | put_ldev(mdev); |
| 1674 | drbd_md_sync(mdev); |
| 1675 | |
| 1676 | if (err) { |
| 1677 | retcode = ERR_NOMEM; |
| 1678 | goto fail; |
| 1679 | } |
| 1680 | } |
| 1681 | |
| 1682 | if (mdev->state.conn >= C_CONNECTED) |
| 1683 | drbd_send_sync_param(mdev, &sc); |
| 1684 | |
| 1685 | if (!cpumask_equal(mdev->cpu_mask, new_cpu_mask)) { |
| 1686 | cpumask_copy(mdev->cpu_mask, new_cpu_mask); |
| 1687 | drbd_calc_cpu_mask(mdev); |
| 1688 | mdev->receiver.reset_cpu_mask = 1; |
| 1689 | mdev->asender.reset_cpu_mask = 1; |
| 1690 | mdev->worker.reset_cpu_mask = 1; |
| 1691 | } |
| 1692 | |
| 1693 | kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE); |
| 1694 | fail: |
| 1695 | free_cpumask_var(new_cpu_mask); |
| 1696 | crypto_free_hash(csums_tfm); |
| 1697 | crypto_free_hash(verify_tfm); |
| 1698 | reply->ret_code = retcode; |
| 1699 | return 0; |
| 1700 | } |
| 1701 | |
| 1702 | static int drbd_nl_invalidate(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1703 | struct drbd_nl_cfg_reply *reply) |
| 1704 | { |
| 1705 | int retcode; |
| 1706 | |
| 1707 | retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T), CS_ORDERED); |
| 1708 | |
| 1709 | if (retcode < SS_SUCCESS && retcode != SS_NEED_CONNECTION) |
| 1710 | retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T)); |
| 1711 | |
| 1712 | while (retcode == SS_NEED_CONNECTION) { |
| 1713 | spin_lock_irq(&mdev->req_lock); |
| 1714 | if (mdev->state.conn < C_CONNECTED) |
| 1715 | retcode = _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_VERBOSE, NULL); |
| 1716 | spin_unlock_irq(&mdev->req_lock); |
| 1717 | |
| 1718 | if (retcode != SS_NEED_CONNECTION) |
| 1719 | break; |
| 1720 | |
| 1721 | retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T)); |
| 1722 | } |
| 1723 | |
| 1724 | reply->ret_code = retcode; |
| 1725 | return 0; |
| 1726 | } |
| 1727 | |
| 1728 | static int drbd_nl_invalidate_peer(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1729 | struct drbd_nl_cfg_reply *reply) |
| 1730 | { |
| 1731 | |
| 1732 | reply->ret_code = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S)); |
| 1733 | |
| 1734 | return 0; |
| 1735 | } |
| 1736 | |
| 1737 | static int drbd_nl_pause_sync(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1738 | struct drbd_nl_cfg_reply *reply) |
| 1739 | { |
| 1740 | int retcode = NO_ERROR; |
| 1741 | |
| 1742 | if (drbd_request_state(mdev, NS(user_isp, 1)) == SS_NOTHING_TO_DO) |
| 1743 | retcode = ERR_PAUSE_IS_SET; |
| 1744 | |
| 1745 | reply->ret_code = retcode; |
| 1746 | return 0; |
| 1747 | } |
| 1748 | |
| 1749 | static int drbd_nl_resume_sync(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1750 | struct drbd_nl_cfg_reply *reply) |
| 1751 | { |
| 1752 | int retcode = NO_ERROR; |
| 1753 | |
| 1754 | if (drbd_request_state(mdev, NS(user_isp, 0)) == SS_NOTHING_TO_DO) |
| 1755 | retcode = ERR_PAUSE_IS_CLEAR; |
| 1756 | |
| 1757 | reply->ret_code = retcode; |
| 1758 | return 0; |
| 1759 | } |
| 1760 | |
| 1761 | static int drbd_nl_suspend_io(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1762 | struct drbd_nl_cfg_reply *reply) |
| 1763 | { |
| 1764 | reply->ret_code = drbd_request_state(mdev, NS(susp, 1)); |
| 1765 | |
| 1766 | return 0; |
| 1767 | } |
| 1768 | |
| 1769 | static int drbd_nl_resume_io(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1770 | struct drbd_nl_cfg_reply *reply) |
| 1771 | { |
Philipp Reisner | 265be2d | 2010-05-31 10:14:17 +0200 | [diff] [blame^] | 1772 | drbd_suspend_io(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1773 | reply->ret_code = drbd_request_state(mdev, NS(susp, 0)); |
Philipp Reisner | 265be2d | 2010-05-31 10:14:17 +0200 | [diff] [blame^] | 1774 | if (reply->ret_code == SS_SUCCESS) { |
| 1775 | if (mdev->state.conn < C_CONNECTED) |
| 1776 | tl_clear(mdev); |
| 1777 | if (mdev->state.disk == D_DISKLESS || mdev->state.disk == D_FAILED) |
| 1778 | tl_restart(mdev, fail_frozen_disk_io); |
| 1779 | } |
| 1780 | drbd_resume_io(mdev); |
| 1781 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1782 | return 0; |
| 1783 | } |
| 1784 | |
| 1785 | static int drbd_nl_outdate(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1786 | struct drbd_nl_cfg_reply *reply) |
| 1787 | { |
| 1788 | reply->ret_code = drbd_request_state(mdev, NS(disk, D_OUTDATED)); |
| 1789 | return 0; |
| 1790 | } |
| 1791 | |
| 1792 | static int drbd_nl_get_config(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1793 | struct drbd_nl_cfg_reply *reply) |
| 1794 | { |
| 1795 | unsigned short *tl; |
| 1796 | |
| 1797 | tl = reply->tag_list; |
| 1798 | |
| 1799 | if (get_ldev(mdev)) { |
| 1800 | tl = disk_conf_to_tags(mdev, &mdev->ldev->dc, tl); |
| 1801 | put_ldev(mdev); |
| 1802 | } |
| 1803 | |
| 1804 | if (get_net_conf(mdev)) { |
| 1805 | tl = net_conf_to_tags(mdev, mdev->net_conf, tl); |
| 1806 | put_net_conf(mdev); |
| 1807 | } |
| 1808 | tl = syncer_conf_to_tags(mdev, &mdev->sync_conf, tl); |
| 1809 | |
| 1810 | put_unaligned(TT_END, tl++); /* Close the tag list */ |
| 1811 | |
| 1812 | return (int)((char *)tl - (char *)reply->tag_list); |
| 1813 | } |
| 1814 | |
| 1815 | static int drbd_nl_get_state(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1816 | struct drbd_nl_cfg_reply *reply) |
| 1817 | { |
| 1818 | unsigned short *tl = reply->tag_list; |
| 1819 | union drbd_state s = mdev->state; |
| 1820 | unsigned long rs_left; |
| 1821 | unsigned int res; |
| 1822 | |
| 1823 | tl = get_state_to_tags(mdev, (struct get_state *)&s, tl); |
| 1824 | |
| 1825 | /* no local ref, no bitmap, no syncer progress. */ |
| 1826 | if (s.conn >= C_SYNC_SOURCE && s.conn <= C_PAUSED_SYNC_T) { |
| 1827 | if (get_ldev(mdev)) { |
| 1828 | drbd_get_syncer_progress(mdev, &rs_left, &res); |
| 1829 | tl = tl_add_int(tl, T_sync_progress, &res); |
| 1830 | put_ldev(mdev); |
| 1831 | } |
| 1832 | } |
| 1833 | put_unaligned(TT_END, tl++); /* Close the tag list */ |
| 1834 | |
| 1835 | return (int)((char *)tl - (char *)reply->tag_list); |
| 1836 | } |
| 1837 | |
| 1838 | static int drbd_nl_get_uuids(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1839 | struct drbd_nl_cfg_reply *reply) |
| 1840 | { |
| 1841 | unsigned short *tl; |
| 1842 | |
| 1843 | tl = reply->tag_list; |
| 1844 | |
| 1845 | if (get_ldev(mdev)) { |
| 1846 | tl = tl_add_blob(tl, T_uuids, mdev->ldev->md.uuid, UI_SIZE*sizeof(u64)); |
| 1847 | tl = tl_add_int(tl, T_uuids_flags, &mdev->ldev->md.flags); |
| 1848 | put_ldev(mdev); |
| 1849 | } |
| 1850 | put_unaligned(TT_END, tl++); /* Close the tag list */ |
| 1851 | |
| 1852 | return (int)((char *)tl - (char *)reply->tag_list); |
| 1853 | } |
| 1854 | |
| 1855 | /** |
| 1856 | * drbd_nl_get_timeout_flag() - Used by drbdsetup to find out which timeout value to use |
| 1857 | * @mdev: DRBD device. |
| 1858 | * @nlp: Netlink/connector packet from drbdsetup |
| 1859 | * @reply: Reply packet for drbdsetup |
| 1860 | */ |
| 1861 | static int drbd_nl_get_timeout_flag(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1862 | struct drbd_nl_cfg_reply *reply) |
| 1863 | { |
| 1864 | unsigned short *tl; |
| 1865 | char rv; |
| 1866 | |
| 1867 | tl = reply->tag_list; |
| 1868 | |
| 1869 | rv = mdev->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED : |
| 1870 | test_bit(USE_DEGR_WFC_T, &mdev->flags) ? UT_DEGRADED : UT_DEFAULT; |
| 1871 | |
| 1872 | tl = tl_add_blob(tl, T_use_degraded, &rv, sizeof(rv)); |
| 1873 | put_unaligned(TT_END, tl++); /* Close the tag list */ |
| 1874 | |
| 1875 | return (int)((char *)tl - (char *)reply->tag_list); |
| 1876 | } |
| 1877 | |
| 1878 | static int drbd_nl_start_ov(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1879 | struct drbd_nl_cfg_reply *reply) |
| 1880 | { |
| 1881 | /* default to resume from last known position, if possible */ |
| 1882 | struct start_ov args = |
| 1883 | { .start_sector = mdev->ov_start_sector }; |
| 1884 | |
| 1885 | if (!start_ov_from_tags(mdev, nlp->tag_list, &args)) { |
| 1886 | reply->ret_code = ERR_MANDATORY_TAG; |
| 1887 | return 0; |
| 1888 | } |
| 1889 | /* w_make_ov_request expects position to be aligned */ |
| 1890 | mdev->ov_start_sector = args.start_sector & ~BM_SECT_PER_BIT; |
| 1891 | reply->ret_code = drbd_request_state(mdev,NS(conn,C_VERIFY_S)); |
| 1892 | return 0; |
| 1893 | } |
| 1894 | |
| 1895 | |
| 1896 | static int drbd_nl_new_c_uuid(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1897 | struct drbd_nl_cfg_reply *reply) |
| 1898 | { |
| 1899 | int retcode = NO_ERROR; |
| 1900 | int skip_initial_sync = 0; |
| 1901 | int err; |
| 1902 | |
| 1903 | struct new_c_uuid args; |
| 1904 | |
| 1905 | memset(&args, 0, sizeof(struct new_c_uuid)); |
| 1906 | if (!new_c_uuid_from_tags(mdev, nlp->tag_list, &args)) { |
| 1907 | reply->ret_code = ERR_MANDATORY_TAG; |
| 1908 | return 0; |
| 1909 | } |
| 1910 | |
| 1911 | mutex_lock(&mdev->state_mutex); /* Protects us against serialized state changes. */ |
| 1912 | |
| 1913 | if (!get_ldev(mdev)) { |
| 1914 | retcode = ERR_NO_DISK; |
| 1915 | goto out; |
| 1916 | } |
| 1917 | |
| 1918 | /* this is "skip initial sync", assume to be clean */ |
| 1919 | if (mdev->state.conn == C_CONNECTED && mdev->agreed_pro_version >= 90 && |
| 1920 | mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) { |
| 1921 | dev_info(DEV, "Preparing to skip initial sync\n"); |
| 1922 | skip_initial_sync = 1; |
| 1923 | } else if (mdev->state.conn != C_STANDALONE) { |
| 1924 | retcode = ERR_CONNECTED; |
| 1925 | goto out_dec; |
| 1926 | } |
| 1927 | |
| 1928 | drbd_uuid_set(mdev, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */ |
| 1929 | drbd_uuid_new_current(mdev); /* New current, previous to UI_BITMAP */ |
| 1930 | |
| 1931 | if (args.clear_bm) { |
| 1932 | err = drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write, "clear_n_write from new_c_uuid"); |
| 1933 | if (err) { |
| 1934 | dev_err(DEV, "Writing bitmap failed with %d\n",err); |
| 1935 | retcode = ERR_IO_MD_DISK; |
| 1936 | } |
| 1937 | if (skip_initial_sync) { |
| 1938 | drbd_send_uuids_skip_initial_sync(mdev); |
| 1939 | _drbd_uuid_set(mdev, UI_BITMAP, 0); |
| 1940 | spin_lock_irq(&mdev->req_lock); |
| 1941 | _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE), |
| 1942 | CS_VERBOSE, NULL); |
| 1943 | spin_unlock_irq(&mdev->req_lock); |
| 1944 | } |
| 1945 | } |
| 1946 | |
| 1947 | drbd_md_sync(mdev); |
| 1948 | out_dec: |
| 1949 | put_ldev(mdev); |
| 1950 | out: |
| 1951 | mutex_unlock(&mdev->state_mutex); |
| 1952 | |
| 1953 | reply->ret_code = retcode; |
| 1954 | return 0; |
| 1955 | } |
| 1956 | |
| 1957 | static struct drbd_conf *ensure_mdev(struct drbd_nl_cfg_req *nlp) |
| 1958 | { |
| 1959 | struct drbd_conf *mdev; |
| 1960 | |
| 1961 | if (nlp->drbd_minor >= minor_count) |
| 1962 | return NULL; |
| 1963 | |
| 1964 | mdev = minor_to_mdev(nlp->drbd_minor); |
| 1965 | |
| 1966 | if (!mdev && (nlp->flags & DRBD_NL_CREATE_DEVICE)) { |
| 1967 | struct gendisk *disk = NULL; |
| 1968 | mdev = drbd_new_device(nlp->drbd_minor); |
| 1969 | |
| 1970 | spin_lock_irq(&drbd_pp_lock); |
| 1971 | if (minor_table[nlp->drbd_minor] == NULL) { |
| 1972 | minor_table[nlp->drbd_minor] = mdev; |
| 1973 | disk = mdev->vdisk; |
| 1974 | mdev = NULL; |
| 1975 | } /* else: we lost the race */ |
| 1976 | spin_unlock_irq(&drbd_pp_lock); |
| 1977 | |
| 1978 | if (disk) /* we won the race above */ |
| 1979 | /* in case we ever add a drbd_delete_device(), |
| 1980 | * don't forget the del_gendisk! */ |
| 1981 | add_disk(disk); |
| 1982 | else /* we lost the race above */ |
| 1983 | drbd_free_mdev(mdev); |
| 1984 | |
| 1985 | mdev = minor_to_mdev(nlp->drbd_minor); |
| 1986 | } |
| 1987 | |
| 1988 | return mdev; |
| 1989 | } |
| 1990 | |
| 1991 | struct cn_handler_struct { |
| 1992 | int (*function)(struct drbd_conf *, |
| 1993 | struct drbd_nl_cfg_req *, |
| 1994 | struct drbd_nl_cfg_reply *); |
| 1995 | int reply_body_size; |
| 1996 | }; |
| 1997 | |
| 1998 | static struct cn_handler_struct cnd_table[] = { |
| 1999 | [ P_primary ] = { &drbd_nl_primary, 0 }, |
| 2000 | [ P_secondary ] = { &drbd_nl_secondary, 0 }, |
| 2001 | [ P_disk_conf ] = { &drbd_nl_disk_conf, 0 }, |
| 2002 | [ P_detach ] = { &drbd_nl_detach, 0 }, |
| 2003 | [ P_net_conf ] = { &drbd_nl_net_conf, 0 }, |
| 2004 | [ P_disconnect ] = { &drbd_nl_disconnect, 0 }, |
| 2005 | [ P_resize ] = { &drbd_nl_resize, 0 }, |
| 2006 | [ P_syncer_conf ] = { &drbd_nl_syncer_conf, 0 }, |
| 2007 | [ P_invalidate ] = { &drbd_nl_invalidate, 0 }, |
| 2008 | [ P_invalidate_peer ] = { &drbd_nl_invalidate_peer, 0 }, |
| 2009 | [ P_pause_sync ] = { &drbd_nl_pause_sync, 0 }, |
| 2010 | [ P_resume_sync ] = { &drbd_nl_resume_sync, 0 }, |
| 2011 | [ P_suspend_io ] = { &drbd_nl_suspend_io, 0 }, |
| 2012 | [ P_resume_io ] = { &drbd_nl_resume_io, 0 }, |
| 2013 | [ P_outdate ] = { &drbd_nl_outdate, 0 }, |
| 2014 | [ P_get_config ] = { &drbd_nl_get_config, |
| 2015 | sizeof(struct syncer_conf_tag_len_struct) + |
| 2016 | sizeof(struct disk_conf_tag_len_struct) + |
| 2017 | sizeof(struct net_conf_tag_len_struct) }, |
| 2018 | [ P_get_state ] = { &drbd_nl_get_state, |
| 2019 | sizeof(struct get_state_tag_len_struct) + |
| 2020 | sizeof(struct sync_progress_tag_len_struct) }, |
| 2021 | [ P_get_uuids ] = { &drbd_nl_get_uuids, |
| 2022 | sizeof(struct get_uuids_tag_len_struct) }, |
| 2023 | [ P_get_timeout_flag ] = { &drbd_nl_get_timeout_flag, |
| 2024 | sizeof(struct get_timeout_flag_tag_len_struct)}, |
| 2025 | [ P_start_ov ] = { &drbd_nl_start_ov, 0 }, |
| 2026 | [ P_new_c_uuid ] = { &drbd_nl_new_c_uuid, 0 }, |
| 2027 | }; |
| 2028 | |
Philipp Reisner | 9f5180e | 2009-10-06 09:30:14 +0200 | [diff] [blame] | 2029 | static void drbd_connector_callback(struct cn_msg *req, struct netlink_skb_parms *nsp) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2030 | { |
| 2031 | struct drbd_nl_cfg_req *nlp = (struct drbd_nl_cfg_req *)req->data; |
| 2032 | struct cn_handler_struct *cm; |
| 2033 | struct cn_msg *cn_reply; |
| 2034 | struct drbd_nl_cfg_reply *reply; |
| 2035 | struct drbd_conf *mdev; |
| 2036 | int retcode, rr; |
| 2037 | int reply_size = sizeof(struct cn_msg) |
| 2038 | + sizeof(struct drbd_nl_cfg_reply) |
| 2039 | + sizeof(short int); |
| 2040 | |
| 2041 | if (!try_module_get(THIS_MODULE)) { |
| 2042 | printk(KERN_ERR "drbd: try_module_get() failed!\n"); |
| 2043 | return; |
| 2044 | } |
| 2045 | |
Philipp Reisner | 9f5180e | 2009-10-06 09:30:14 +0200 | [diff] [blame] | 2046 | if (!cap_raised(nsp->eff_cap, CAP_SYS_ADMIN)) { |
| 2047 | retcode = ERR_PERM; |
| 2048 | goto fail; |
| 2049 | } |
| 2050 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2051 | mdev = ensure_mdev(nlp); |
| 2052 | if (!mdev) { |
| 2053 | retcode = ERR_MINOR_INVALID; |
| 2054 | goto fail; |
| 2055 | } |
| 2056 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2057 | if (nlp->packet_type >= P_nl_after_last_packet) { |
| 2058 | retcode = ERR_PACKET_NR; |
| 2059 | goto fail; |
| 2060 | } |
| 2061 | |
| 2062 | cm = cnd_table + nlp->packet_type; |
| 2063 | |
| 2064 | /* This may happen if packet number is 0: */ |
| 2065 | if (cm->function == NULL) { |
| 2066 | retcode = ERR_PACKET_NR; |
| 2067 | goto fail; |
| 2068 | } |
| 2069 | |
| 2070 | reply_size += cm->reply_body_size; |
| 2071 | |
| 2072 | /* allocation not in the IO path, cqueue thread context */ |
| 2073 | cn_reply = kmalloc(reply_size, GFP_KERNEL); |
| 2074 | if (!cn_reply) { |
| 2075 | retcode = ERR_NOMEM; |
| 2076 | goto fail; |
| 2077 | } |
| 2078 | reply = (struct drbd_nl_cfg_reply *) cn_reply->data; |
| 2079 | |
| 2080 | reply->packet_type = |
| 2081 | cm->reply_body_size ? nlp->packet_type : P_nl_after_last_packet; |
| 2082 | reply->minor = nlp->drbd_minor; |
| 2083 | reply->ret_code = NO_ERROR; /* Might by modified by cm->function. */ |
| 2084 | /* reply->tag_list; might be modified by cm->function. */ |
| 2085 | |
| 2086 | rr = cm->function(mdev, nlp, reply); |
| 2087 | |
| 2088 | cn_reply->id = req->id; |
| 2089 | cn_reply->seq = req->seq; |
| 2090 | cn_reply->ack = req->ack + 1; |
| 2091 | cn_reply->len = sizeof(struct drbd_nl_cfg_reply) + rr; |
| 2092 | cn_reply->flags = 0; |
| 2093 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2094 | rr = cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_KERNEL); |
| 2095 | if (rr && rr != -ESRCH) |
| 2096 | printk(KERN_INFO "drbd: cn_netlink_send()=%d\n", rr); |
| 2097 | |
| 2098 | kfree(cn_reply); |
| 2099 | module_put(THIS_MODULE); |
| 2100 | return; |
| 2101 | fail: |
| 2102 | drbd_nl_send_reply(req, retcode); |
| 2103 | module_put(THIS_MODULE); |
| 2104 | } |
| 2105 | |
| 2106 | static atomic_t drbd_nl_seq = ATOMIC_INIT(2); /* two. */ |
| 2107 | |
| 2108 | static unsigned short * |
| 2109 | __tl_add_blob(unsigned short *tl, enum drbd_tags tag, const void *data, |
| 2110 | unsigned short len, int nul_terminated) |
| 2111 | { |
| 2112 | unsigned short l = tag_descriptions[tag_number(tag)].max_len; |
| 2113 | len = (len < l) ? len : l; |
| 2114 | put_unaligned(tag, tl++); |
| 2115 | put_unaligned(len, tl++); |
| 2116 | memcpy(tl, data, len); |
| 2117 | tl = (unsigned short*)((char*)tl + len); |
| 2118 | if (nul_terminated) |
| 2119 | *((char*)tl - 1) = 0; |
| 2120 | return tl; |
| 2121 | } |
| 2122 | |
| 2123 | static unsigned short * |
| 2124 | tl_add_blob(unsigned short *tl, enum drbd_tags tag, const void *data, int len) |
| 2125 | { |
| 2126 | return __tl_add_blob(tl, tag, data, len, 0); |
| 2127 | } |
| 2128 | |
| 2129 | static unsigned short * |
| 2130 | tl_add_str(unsigned short *tl, enum drbd_tags tag, const char *str) |
| 2131 | { |
| 2132 | return __tl_add_blob(tl, tag, str, strlen(str)+1, 0); |
| 2133 | } |
| 2134 | |
| 2135 | static unsigned short * |
| 2136 | tl_add_int(unsigned short *tl, enum drbd_tags tag, const void *val) |
| 2137 | { |
| 2138 | put_unaligned(tag, tl++); |
| 2139 | switch(tag_type(tag)) { |
| 2140 | case TT_INTEGER: |
| 2141 | put_unaligned(sizeof(int), tl++); |
| 2142 | put_unaligned(*(int *)val, (int *)tl); |
| 2143 | tl = (unsigned short*)((char*)tl+sizeof(int)); |
| 2144 | break; |
| 2145 | case TT_INT64: |
| 2146 | put_unaligned(sizeof(u64), tl++); |
| 2147 | put_unaligned(*(u64 *)val, (u64 *)tl); |
| 2148 | tl = (unsigned short*)((char*)tl+sizeof(u64)); |
| 2149 | break; |
| 2150 | default: |
| 2151 | /* someone did something stupid. */ |
| 2152 | ; |
| 2153 | } |
| 2154 | return tl; |
| 2155 | } |
| 2156 | |
| 2157 | void drbd_bcast_state(struct drbd_conf *mdev, union drbd_state state) |
| 2158 | { |
| 2159 | char buffer[sizeof(struct cn_msg)+ |
| 2160 | sizeof(struct drbd_nl_cfg_reply)+ |
| 2161 | sizeof(struct get_state_tag_len_struct)+ |
| 2162 | sizeof(short int)]; |
| 2163 | struct cn_msg *cn_reply = (struct cn_msg *) buffer; |
| 2164 | struct drbd_nl_cfg_reply *reply = |
| 2165 | (struct drbd_nl_cfg_reply *)cn_reply->data; |
| 2166 | unsigned short *tl = reply->tag_list; |
| 2167 | |
| 2168 | /* dev_warn(DEV, "drbd_bcast_state() got called\n"); */ |
| 2169 | |
| 2170 | tl = get_state_to_tags(mdev, (struct get_state *)&state, tl); |
| 2171 | |
| 2172 | put_unaligned(TT_END, tl++); /* Close the tag list */ |
| 2173 | |
| 2174 | cn_reply->id.idx = CN_IDX_DRBD; |
| 2175 | cn_reply->id.val = CN_VAL_DRBD; |
| 2176 | |
| 2177 | cn_reply->seq = atomic_add_return(1, &drbd_nl_seq); |
| 2178 | cn_reply->ack = 0; /* not used here. */ |
| 2179 | cn_reply->len = sizeof(struct drbd_nl_cfg_reply) + |
| 2180 | (int)((char *)tl - (char *)reply->tag_list); |
| 2181 | cn_reply->flags = 0; |
| 2182 | |
| 2183 | reply->packet_type = P_get_state; |
| 2184 | reply->minor = mdev_to_minor(mdev); |
| 2185 | reply->ret_code = NO_ERROR; |
| 2186 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2187 | cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO); |
| 2188 | } |
| 2189 | |
| 2190 | void drbd_bcast_ev_helper(struct drbd_conf *mdev, char *helper_name) |
| 2191 | { |
| 2192 | char buffer[sizeof(struct cn_msg)+ |
| 2193 | sizeof(struct drbd_nl_cfg_reply)+ |
| 2194 | sizeof(struct call_helper_tag_len_struct)+ |
| 2195 | sizeof(short int)]; |
| 2196 | struct cn_msg *cn_reply = (struct cn_msg *) buffer; |
| 2197 | struct drbd_nl_cfg_reply *reply = |
| 2198 | (struct drbd_nl_cfg_reply *)cn_reply->data; |
| 2199 | unsigned short *tl = reply->tag_list; |
| 2200 | |
| 2201 | /* dev_warn(DEV, "drbd_bcast_state() got called\n"); */ |
| 2202 | |
| 2203 | tl = tl_add_str(tl, T_helper, helper_name); |
| 2204 | put_unaligned(TT_END, tl++); /* Close the tag list */ |
| 2205 | |
| 2206 | cn_reply->id.idx = CN_IDX_DRBD; |
| 2207 | cn_reply->id.val = CN_VAL_DRBD; |
| 2208 | |
| 2209 | cn_reply->seq = atomic_add_return(1, &drbd_nl_seq); |
| 2210 | cn_reply->ack = 0; /* not used here. */ |
| 2211 | cn_reply->len = sizeof(struct drbd_nl_cfg_reply) + |
| 2212 | (int)((char *)tl - (char *)reply->tag_list); |
| 2213 | cn_reply->flags = 0; |
| 2214 | |
| 2215 | reply->packet_type = P_call_helper; |
| 2216 | reply->minor = mdev_to_minor(mdev); |
| 2217 | reply->ret_code = NO_ERROR; |
| 2218 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2219 | cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO); |
| 2220 | } |
| 2221 | |
| 2222 | void drbd_bcast_ee(struct drbd_conf *mdev, |
| 2223 | const char *reason, const int dgs, |
| 2224 | const char* seen_hash, const char* calc_hash, |
| 2225 | const struct drbd_epoch_entry* e) |
| 2226 | { |
| 2227 | struct cn_msg *cn_reply; |
| 2228 | struct drbd_nl_cfg_reply *reply; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2229 | unsigned short *tl; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 2230 | struct page *page; |
| 2231 | unsigned len; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2232 | |
| 2233 | if (!e) |
| 2234 | return; |
| 2235 | if (!reason || !reason[0]) |
| 2236 | return; |
| 2237 | |
| 2238 | /* apparently we have to memcpy twice, first to prepare the data for the |
| 2239 | * struct cn_msg, then within cn_netlink_send from the cn_msg to the |
| 2240 | * netlink skb. */ |
| 2241 | /* receiver thread context, which is not in the writeout path (of this node), |
| 2242 | * but may be in the writeout path of the _other_ node. |
| 2243 | * GFP_NOIO to avoid potential "distributed deadlock". */ |
| 2244 | cn_reply = kmalloc( |
| 2245 | sizeof(struct cn_msg)+ |
| 2246 | sizeof(struct drbd_nl_cfg_reply)+ |
| 2247 | sizeof(struct dump_ee_tag_len_struct)+ |
| 2248 | sizeof(short int), |
| 2249 | GFP_NOIO); |
| 2250 | |
| 2251 | if (!cn_reply) { |
| 2252 | dev_err(DEV, "could not kmalloc buffer for drbd_bcast_ee, sector %llu, size %u\n", |
| 2253 | (unsigned long long)e->sector, e->size); |
| 2254 | return; |
| 2255 | } |
| 2256 | |
| 2257 | reply = (struct drbd_nl_cfg_reply*)cn_reply->data; |
| 2258 | tl = reply->tag_list; |
| 2259 | |
| 2260 | tl = tl_add_str(tl, T_dump_ee_reason, reason); |
| 2261 | tl = tl_add_blob(tl, T_seen_digest, seen_hash, dgs); |
| 2262 | tl = tl_add_blob(tl, T_calc_digest, calc_hash, dgs); |
| 2263 | tl = tl_add_int(tl, T_ee_sector, &e->sector); |
| 2264 | tl = tl_add_int(tl, T_ee_block_id, &e->block_id); |
| 2265 | |
| 2266 | put_unaligned(T_ee_data, tl++); |
| 2267 | put_unaligned(e->size, tl++); |
| 2268 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 2269 | len = e->size; |
| 2270 | page = e->pages; |
| 2271 | page_chain_for_each(page) { |
| 2272 | void *d = kmap_atomic(page, KM_USER0); |
| 2273 | unsigned l = min_t(unsigned, len, PAGE_SIZE); |
| 2274 | memcpy(tl, d, l); |
| 2275 | kunmap_atomic(d, KM_USER0); |
| 2276 | tl = (unsigned short*)((char*)tl + l); |
| 2277 | len -= l; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2278 | } |
| 2279 | put_unaligned(TT_END, tl++); /* Close the tag list */ |
| 2280 | |
| 2281 | cn_reply->id.idx = CN_IDX_DRBD; |
| 2282 | cn_reply->id.val = CN_VAL_DRBD; |
| 2283 | |
| 2284 | cn_reply->seq = atomic_add_return(1,&drbd_nl_seq); |
| 2285 | cn_reply->ack = 0; // not used here. |
| 2286 | cn_reply->len = sizeof(struct drbd_nl_cfg_reply) + |
| 2287 | (int)((char*)tl - (char*)reply->tag_list); |
| 2288 | cn_reply->flags = 0; |
| 2289 | |
| 2290 | reply->packet_type = P_dump_ee; |
| 2291 | reply->minor = mdev_to_minor(mdev); |
| 2292 | reply->ret_code = NO_ERROR; |
| 2293 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2294 | cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO); |
| 2295 | kfree(cn_reply); |
| 2296 | } |
| 2297 | |
| 2298 | void drbd_bcast_sync_progress(struct drbd_conf *mdev) |
| 2299 | { |
| 2300 | char buffer[sizeof(struct cn_msg)+ |
| 2301 | sizeof(struct drbd_nl_cfg_reply)+ |
| 2302 | sizeof(struct sync_progress_tag_len_struct)+ |
| 2303 | sizeof(short int)]; |
| 2304 | struct cn_msg *cn_reply = (struct cn_msg *) buffer; |
| 2305 | struct drbd_nl_cfg_reply *reply = |
| 2306 | (struct drbd_nl_cfg_reply *)cn_reply->data; |
| 2307 | unsigned short *tl = reply->tag_list; |
| 2308 | unsigned long rs_left; |
| 2309 | unsigned int res; |
| 2310 | |
| 2311 | /* no local ref, no bitmap, no syncer progress, no broadcast. */ |
| 2312 | if (!get_ldev(mdev)) |
| 2313 | return; |
| 2314 | drbd_get_syncer_progress(mdev, &rs_left, &res); |
| 2315 | put_ldev(mdev); |
| 2316 | |
| 2317 | tl = tl_add_int(tl, T_sync_progress, &res); |
| 2318 | put_unaligned(TT_END, tl++); /* Close the tag list */ |
| 2319 | |
| 2320 | cn_reply->id.idx = CN_IDX_DRBD; |
| 2321 | cn_reply->id.val = CN_VAL_DRBD; |
| 2322 | |
| 2323 | cn_reply->seq = atomic_add_return(1, &drbd_nl_seq); |
| 2324 | cn_reply->ack = 0; /* not used here. */ |
| 2325 | cn_reply->len = sizeof(struct drbd_nl_cfg_reply) + |
| 2326 | (int)((char *)tl - (char *)reply->tag_list); |
| 2327 | cn_reply->flags = 0; |
| 2328 | |
| 2329 | reply->packet_type = P_sync_progress; |
| 2330 | reply->minor = mdev_to_minor(mdev); |
| 2331 | reply->ret_code = NO_ERROR; |
| 2332 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2333 | cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO); |
| 2334 | } |
| 2335 | |
| 2336 | int __init drbd_nl_init(void) |
| 2337 | { |
| 2338 | static struct cb_id cn_id_drbd; |
| 2339 | int err, try=10; |
| 2340 | |
| 2341 | cn_id_drbd.val = CN_VAL_DRBD; |
| 2342 | do { |
| 2343 | cn_id_drbd.idx = cn_idx; |
| 2344 | err = cn_add_callback(&cn_id_drbd, "cn_drbd", &drbd_connector_callback); |
| 2345 | if (!err) |
| 2346 | break; |
| 2347 | cn_idx = (cn_idx + CN_IDX_STEP); |
| 2348 | } while (try--); |
| 2349 | |
| 2350 | if (err) { |
| 2351 | printk(KERN_ERR "drbd: cn_drbd failed to register\n"); |
| 2352 | return err; |
| 2353 | } |
| 2354 | |
| 2355 | return 0; |
| 2356 | } |
| 2357 | |
| 2358 | void drbd_nl_cleanup(void) |
| 2359 | { |
| 2360 | static struct cb_id cn_id_drbd; |
| 2361 | |
| 2362 | cn_id_drbd.idx = cn_idx; |
| 2363 | cn_id_drbd.val = CN_VAL_DRBD; |
| 2364 | |
| 2365 | cn_del_callback(&cn_id_drbd); |
| 2366 | } |
| 2367 | |
| 2368 | void drbd_nl_send_reply(struct cn_msg *req, int ret_code) |
| 2369 | { |
| 2370 | char buffer[sizeof(struct cn_msg)+sizeof(struct drbd_nl_cfg_reply)]; |
| 2371 | struct cn_msg *cn_reply = (struct cn_msg *) buffer; |
| 2372 | struct drbd_nl_cfg_reply *reply = |
| 2373 | (struct drbd_nl_cfg_reply *)cn_reply->data; |
| 2374 | int rr; |
| 2375 | |
| 2376 | cn_reply->id = req->id; |
| 2377 | |
| 2378 | cn_reply->seq = req->seq; |
| 2379 | cn_reply->ack = req->ack + 1; |
| 2380 | cn_reply->len = sizeof(struct drbd_nl_cfg_reply); |
| 2381 | cn_reply->flags = 0; |
| 2382 | |
| 2383 | reply->minor = ((struct drbd_nl_cfg_req *)req->data)->drbd_minor; |
| 2384 | reply->ret_code = ret_code; |
| 2385 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2386 | rr = cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO); |
| 2387 | if (rr && rr != -ESRCH) |
| 2388 | printk(KERN_INFO "drbd: cn_netlink_send()=%d\n", rr); |
| 2389 | } |
| 2390 | |