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