blob: 9dfe58a096252dc711d75dbc29f693278ddba9b4 [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
Lars Ellenberg1090c052010-07-19 17:41:04 +0200175 /* The helper may take some time.
176 * write out any unsynced meta data changes now */
177 drbd_md_sync(mdev);
178
Philipp Reisnerb411b362009-09-25 16:07:19 -0700179 dev_info(DEV, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
180
181 drbd_bcast_ev_helper(mdev, cmd);
182 ret = call_usermodehelper(usermode_helper, argv, envp, 1);
183 if (ret)
184 dev_warn(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
185 usermode_helper, cmd, mb,
186 (ret >> 8) & 0xff, ret);
187 else
188 dev_info(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
189 usermode_helper, cmd, mb,
190 (ret >> 8) & 0xff, ret);
191
192 if (ret < 0) /* Ignore any ERRNOs we got. */
193 ret = 0;
194
195 return ret;
196}
197
198enum drbd_disk_state drbd_try_outdate_peer(struct drbd_conf *mdev)
199{
200 char *ex_to_string;
201 int r;
202 enum drbd_disk_state nps;
203 enum drbd_fencing_p fp;
204
205 D_ASSERT(mdev->state.pdsk == D_UNKNOWN);
206
207 if (get_ldev_if_state(mdev, D_CONSISTENT)) {
208 fp = mdev->ldev->dc.fencing;
209 put_ldev(mdev);
210 } else {
211 dev_warn(DEV, "Not fencing peer, I'm not even Consistent myself.\n");
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200212 nps = mdev->state.pdsk;
213 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700214 }
215
Philipp Reisnerb411b362009-09-25 16:07:19 -0700216 r = drbd_khelper(mdev, "fence-peer");
217
218 switch ((r>>8) & 0xff) {
219 case 3: /* peer is inconsistent */
220 ex_to_string = "peer is inconsistent or worse";
221 nps = D_INCONSISTENT;
222 break;
223 case 4: /* peer got outdated, or was already outdated */
224 ex_to_string = "peer was fenced";
225 nps = D_OUTDATED;
226 break;
227 case 5: /* peer was down */
228 if (mdev->state.disk == D_UP_TO_DATE) {
229 /* we will(have) create(d) a new UUID anyways... */
230 ex_to_string = "peer is unreachable, assumed to be dead";
231 nps = D_OUTDATED;
232 } else {
233 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
234 nps = mdev->state.pdsk;
235 }
236 break;
237 case 6: /* Peer is primary, voluntarily outdate myself.
238 * This is useful when an unconnected R_SECONDARY is asked to
239 * become R_PRIMARY, but finds the other peer being active. */
240 ex_to_string = "peer is active";
241 dev_warn(DEV, "Peer is primary, outdating myself.\n");
242 nps = D_UNKNOWN;
243 _drbd_request_state(mdev, NS(disk, D_OUTDATED), CS_WAIT_COMPLETE);
244 break;
245 case 7:
246 if (fp != FP_STONITH)
247 dev_err(DEV, "fence-peer() = 7 && fencing != Stonith !!!\n");
248 ex_to_string = "peer was stonithed";
249 nps = D_OUTDATED;
250 break;
251 default:
252 /* The script is broken ... */
253 nps = D_UNKNOWN;
254 dev_err(DEV, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
255 return nps;
256 }
257
258 dev_info(DEV, "fence-peer helper returned %d (%s)\n",
259 (r>>8) & 0xff, ex_to_string);
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200260
261out:
262 if (mdev->state.susp_fen && nps >= D_UNKNOWN) {
263 /* The handler was not successful... unfreeze here, the
264 state engine can not unfreeze... */
265 _drbd_request_state(mdev, NS(susp_fen, 0), CS_VERBOSE);
266 }
267
Philipp Reisnerb411b362009-09-25 16:07:19 -0700268 return nps;
269}
270
Philipp Reisner87f7be42010-06-11 13:56:33 +0200271static int _try_outdate_peer_async(void *data)
272{
273 struct drbd_conf *mdev = (struct drbd_conf *)data;
274 enum drbd_disk_state nps;
Philipp Reisner21423fa2011-05-17 14:19:41 +0200275 union drbd_state ns;
Philipp Reisner87f7be42010-06-11 13:56:33 +0200276
277 nps = drbd_try_outdate_peer(mdev);
Philipp Reisner21423fa2011-05-17 14:19:41 +0200278
279 /* Not using
280 drbd_request_state(mdev, NS(pdsk, nps));
281 here, because we might were able to re-establish the connection in the
282 meantime.
283 */
284 spin_lock_irq(&mdev->req_lock);
285 ns = mdev->state;
286 if (ns.conn < C_WF_REPORT_PARAMS) {
287 ns.pdsk = nps;
288 _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
289 }
290 spin_unlock_irq(&mdev->req_lock);
Philipp Reisner87f7be42010-06-11 13:56:33 +0200291
292 return 0;
293}
294
295void drbd_try_outdate_peer_async(struct drbd_conf *mdev)
296{
297 struct task_struct *opa;
298
299 opa = kthread_run(_try_outdate_peer_async, mdev, "drbd%d_a_helper", mdev_to_minor(mdev));
300 if (IS_ERR(opa))
301 dev_err(DEV, "out of mem, failed to invoke fence-peer helper\n");
302}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700303
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100304enum drbd_state_rv
305drbd_set_role(struct drbd_conf *mdev, enum drbd_role new_role, int force)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700306{
307 const int max_tries = 4;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100308 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700309 int try = 0;
310 int forced = 0;
311 union drbd_state mask, val;
312 enum drbd_disk_state nps;
313
314 if (new_role == R_PRIMARY)
315 request_ping(mdev); /* Detect a dead peer ASAP */
316
317 mutex_lock(&mdev->state_mutex);
318
319 mask.i = 0; mask.role = R_MASK;
320 val.i = 0; val.role = new_role;
321
322 while (try++ < max_tries) {
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100323 rv = _drbd_request_state(mdev, mask, val, CS_WAIT_COMPLETE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700324
325 /* in case we first succeeded to outdate,
326 * but now suddenly could establish a connection */
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100327 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700328 val.pdsk = 0;
329 mask.pdsk = 0;
330 continue;
331 }
332
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100333 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
Philipp Reisnerd10a33c2010-03-04 15:11:39 +0100334 (mdev->state.disk < D_UP_TO_DATE &&
335 mdev->state.disk >= D_INCONSISTENT)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700336 mask.disk = D_MASK;
337 val.disk = D_UP_TO_DATE;
338 forced = 1;
339 continue;
340 }
341
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100342 if (rv == SS_NO_UP_TO_DATE_DISK &&
Philipp Reisnerb411b362009-09-25 16:07:19 -0700343 mdev->state.disk == D_CONSISTENT && mask.pdsk == 0) {
344 D_ASSERT(mdev->state.pdsk == D_UNKNOWN);
345 nps = drbd_try_outdate_peer(mdev);
346
347 if (nps == D_OUTDATED || nps == D_INCONSISTENT) {
348 val.disk = D_UP_TO_DATE;
349 mask.disk = D_MASK;
350 }
351
352 val.pdsk = nps;
353 mask.pdsk = D_MASK;
354
355 continue;
356 }
357
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100358 if (rv == SS_NOTHING_TO_DO)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700359 goto fail;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100360 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700361 nps = drbd_try_outdate_peer(mdev);
362
363 if (force && nps > D_OUTDATED) {
364 dev_warn(DEV, "Forced into split brain situation!\n");
365 nps = D_OUTDATED;
366 }
367
368 mask.pdsk = D_MASK;
369 val.pdsk = nps;
370
371 continue;
372 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100373 if (rv == SS_TWO_PRIMARIES) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700374 /* Maybe the peer is detected as dead very soon...
375 retry at most once more in this case. */
Philipp Reisner20ee6392011-01-18 15:28:59 +0100376 schedule_timeout_interruptible((mdev->net_conf->ping_timeo+1)*HZ/10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700377 if (try < max_tries)
378 try = max_tries - 1;
379 continue;
380 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100381 if (rv < SS_SUCCESS) {
382 rv = _drbd_request_state(mdev, mask, val,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700383 CS_VERBOSE + CS_WAIT_COMPLETE);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100384 if (rv < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700385 goto fail;
386 }
387 break;
388 }
389
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100390 if (rv < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700391 goto fail;
392
393 if (forced)
394 dev_warn(DEV, "Forced to consider local data as UpToDate!\n");
395
396 /* Wait until nothing is on the fly :) */
397 wait_event(mdev->misc_wait, atomic_read(&mdev->ap_pending_cnt) == 0);
398
399 if (new_role == R_SECONDARY) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100400 set_disk_ro(mdev->vdisk, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700401 if (get_ldev(mdev)) {
402 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
403 put_ldev(mdev);
404 }
405 } else {
406 if (get_net_conf(mdev)) {
407 mdev->net_conf->want_lose = 0;
408 put_net_conf(mdev);
409 }
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100410 set_disk_ro(mdev->vdisk, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700411 if (get_ldev(mdev)) {
412 if (((mdev->state.conn < C_CONNECTED ||
413 mdev->state.pdsk <= D_FAILED)
414 && mdev->ldev->md.uuid[UI_BITMAP] == 0) || forced)
415 drbd_uuid_new_current(mdev);
416
417 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
418 put_ldev(mdev);
419 }
420 }
421
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100422 /* writeout of activity log covered areas of the bitmap
423 * to stable storage done in after state change already */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700424
425 if (mdev->state.conn >= C_WF_REPORT_PARAMS) {
426 /* if this was forced, we should consider sync */
427 if (forced)
428 drbd_send_uuids(mdev);
429 drbd_send_state(mdev);
430 }
431
432 drbd_md_sync(mdev);
433
434 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
435 fail:
436 mutex_unlock(&mdev->state_mutex);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100437 return rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700438}
439
Lars Ellenbergef50a3e2010-09-01 14:39:30 +0200440static struct drbd_conf *ensure_mdev(int minor, int create)
441{
442 struct drbd_conf *mdev;
443
444 if (minor >= minor_count)
445 return NULL;
446
447 mdev = minor_to_mdev(minor);
448
449 if (!mdev && create) {
450 struct gendisk *disk = NULL;
451 mdev = drbd_new_device(minor);
452
453 spin_lock_irq(&drbd_pp_lock);
454 if (minor_table[minor] == NULL) {
455 minor_table[minor] = mdev;
456 disk = mdev->vdisk;
457 mdev = NULL;
458 } /* else: we lost the race */
459 spin_unlock_irq(&drbd_pp_lock);
460
461 if (disk) /* we won the race above */
462 /* in case we ever add a drbd_delete_device(),
463 * don't forget the del_gendisk! */
464 add_disk(disk);
465 else /* we lost the race above */
466 drbd_free_mdev(mdev);
467
468 mdev = minor_to_mdev(minor);
469 }
470
471 return mdev;
472}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700473
474static int drbd_nl_primary(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
475 struct drbd_nl_cfg_reply *reply)
476{
477 struct primary primary_args;
478
479 memset(&primary_args, 0, sizeof(struct primary));
480 if (!primary_from_tags(mdev, nlp->tag_list, &primary_args)) {
481 reply->ret_code = ERR_MANDATORY_TAG;
482 return 0;
483 }
484
485 reply->ret_code =
Philipp Reisner1f552432010-03-04 15:51:01 +0100486 drbd_set_role(mdev, R_PRIMARY, primary_args.primary_force);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700487
488 return 0;
489}
490
491static int drbd_nl_secondary(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
492 struct drbd_nl_cfg_reply *reply)
493{
494 reply->ret_code = drbd_set_role(mdev, R_SECONDARY, 0);
495
496 return 0;
497}
498
499/* initializes the md.*_offset members, so we are able to find
500 * the on disk meta data */
501static void drbd_md_set_sector_offsets(struct drbd_conf *mdev,
502 struct drbd_backing_dev *bdev)
503{
504 sector_t md_size_sect = 0;
505 switch (bdev->dc.meta_dev_idx) {
506 default:
507 /* v07 style fixed size indexed meta data */
508 bdev->md.md_size_sect = MD_RESERVED_SECT;
509 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
510 bdev->md.al_offset = MD_AL_OFFSET;
511 bdev->md.bm_offset = MD_BM_OFFSET;
512 break;
513 case DRBD_MD_INDEX_FLEX_EXT:
514 /* just occupy the full device; unit: sectors */
515 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
516 bdev->md.md_offset = 0;
517 bdev->md.al_offset = MD_AL_OFFSET;
518 bdev->md.bm_offset = MD_BM_OFFSET;
519 break;
520 case DRBD_MD_INDEX_INTERNAL:
521 case DRBD_MD_INDEX_FLEX_INT:
522 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
523 /* al size is still fixed */
524 bdev->md.al_offset = -MD_AL_MAX_SIZE;
525 /* we need (slightly less than) ~ this much bitmap sectors: */
526 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
527 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
528 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
529 md_size_sect = ALIGN(md_size_sect, 8);
530
531 /* plus the "drbd meta data super block",
532 * and the activity log; */
533 md_size_sect += MD_BM_OFFSET;
534
535 bdev->md.md_size_sect = md_size_sect;
536 /* bitmap offset is adjusted by 'super' block size */
537 bdev->md.bm_offset = -md_size_sect + MD_AL_OFFSET;
538 break;
539 }
540}
541
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100542/* input size is expected to be in KB */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700543char *ppsize(char *buf, unsigned long long size)
544{
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100545 /* Needs 9 bytes at max including trailing NUL:
546 * -1ULL ==> "16384 EB" */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700547 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
548 int base = 0;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100549 while (size >= 10000 && base < sizeof(units)-1) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700550 /* shift + round */
551 size = (size >> 10) + !!(size & (1<<9));
552 base++;
553 }
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100554 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700555
556 return buf;
557}
558
559/* there is still a theoretical deadlock when called from receiver
560 * on an D_INCONSISTENT R_PRIMARY:
561 * remote READ does inc_ap_bio, receiver would need to receive answer
562 * packet from remote to dec_ap_bio again.
563 * receiver receive_sizes(), comes here,
564 * waits for ap_bio_cnt == 0. -> deadlock.
565 * but this cannot happen, actually, because:
566 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
567 * (not connected, or bad/no disk on peer):
568 * see drbd_fail_request_early, ap_bio_cnt is zero.
569 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
570 * peer may not initiate a resize.
571 */
572void drbd_suspend_io(struct drbd_conf *mdev)
573{
574 set_bit(SUSPEND_IO, &mdev->flags);
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200575 if (is_susp(mdev->state))
Philipp Reisner265be2d2010-05-31 10:14:17 +0200576 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700577 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
578}
579
580void drbd_resume_io(struct drbd_conf *mdev)
581{
582 clear_bit(SUSPEND_IO, &mdev->flags);
583 wake_up(&mdev->misc_wait);
584}
585
586/**
587 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
588 * @mdev: DRBD device.
589 *
590 * Returns 0 on success, negative return values indicate errors.
591 * You should call drbd_md_sync() after calling this function.
592 */
Philipp Reisnerd8450302010-03-24 15:51:26 +0100593enum 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 -0700594{
595 sector_t prev_first_sect, prev_size; /* previous meta location */
596 sector_t la_size;
597 sector_t size;
598 char ppb[10];
599
600 int md_moved, la_size_changed;
601 enum determine_dev_size rv = unchanged;
602
603 /* race:
604 * application request passes inc_ap_bio,
605 * but then cannot get an AL-reference.
606 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
607 *
608 * to avoid that:
609 * Suspend IO right here.
610 * still lock the act_log to not trigger ASSERTs there.
611 */
612 drbd_suspend_io(mdev);
613
614 /* no wait necessary anymore, actually we could assert that */
615 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
616
617 prev_first_sect = drbd_md_first_sector(mdev->ldev);
618 prev_size = mdev->ldev->md.md_size_sect;
619 la_size = mdev->ldev->md.la_size_sect;
620
621 /* TODO: should only be some assert here, not (re)init... */
622 drbd_md_set_sector_offsets(mdev, mdev->ldev);
623
Philipp Reisnerd8450302010-03-24 15:51:26 +0100624 size = drbd_new_dev_size(mdev, mdev->ldev, flags & DDSF_FORCED);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700625
626 if (drbd_get_capacity(mdev->this_bdev) != size ||
627 drbd_bm_capacity(mdev) != size) {
628 int err;
Philipp Reisner02d9a942010-03-24 16:23:03 +0100629 err = drbd_bm_resize(mdev, size, !(flags & DDSF_NO_RESYNC));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700630 if (unlikely(err)) {
631 /* currently there is only one error: ENOMEM! */
632 size = drbd_bm_capacity(mdev)>>1;
633 if (size == 0) {
634 dev_err(DEV, "OUT OF MEMORY! "
635 "Could not allocate bitmap!\n");
636 } else {
637 dev_err(DEV, "BM resizing failed. "
638 "Leaving size unchanged at size = %lu KB\n",
639 (unsigned long)size);
640 }
641 rv = dev_size_error;
642 }
643 /* racy, see comments above. */
644 drbd_set_my_capacity(mdev, size);
645 mdev->ldev->md.la_size_sect = size;
646 dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
647 (unsigned long long)size>>1);
648 }
649 if (rv == dev_size_error)
650 goto out;
651
652 la_size_changed = (la_size != mdev->ldev->md.la_size_sect);
653
654 md_moved = prev_first_sect != drbd_md_first_sector(mdev->ldev)
655 || prev_size != mdev->ldev->md.md_size_sect;
656
657 if (la_size_changed || md_moved) {
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100658 int err;
659
Philipp Reisnerb411b362009-09-25 16:07:19 -0700660 drbd_al_shrink(mdev); /* All extents inactive. */
661 dev_info(DEV, "Writing the whole bitmap, %s\n",
662 la_size_changed && md_moved ? "size changed and md moved" :
663 la_size_changed ? "size changed" : "md moved");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100664 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
665 err = drbd_bitmap_io(mdev, &drbd_bm_write,
666 "size changed", BM_LOCKED_MASK);
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100667 if (err) {
668 rv = dev_size_error;
669 goto out;
670 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700671 drbd_md_mark_dirty(mdev);
672 }
673
674 if (size > la_size)
675 rv = grew;
676 if (size < la_size)
677 rv = shrunk;
678out:
679 lc_unlock(mdev->act_log);
680 wake_up(&mdev->al_wait);
681 drbd_resume_io(mdev);
682
683 return rv;
684}
685
686sector_t
Philipp Reisnera393db62009-12-22 13:35:52 +0100687drbd_new_dev_size(struct drbd_conf *mdev, struct drbd_backing_dev *bdev, int assume_peer_has_space)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700688{
689 sector_t p_size = mdev->p_size; /* partner's disk size. */
690 sector_t la_size = bdev->md.la_size_sect; /* last agreed size. */
691 sector_t m_size; /* my size */
692 sector_t u_size = bdev->dc.disk_size; /* size requested by user. */
693 sector_t size = 0;
694
695 m_size = drbd_get_max_capacity(bdev);
696
Philipp Reisnera393db62009-12-22 13:35:52 +0100697 if (mdev->state.conn < C_CONNECTED && assume_peer_has_space) {
698 dev_warn(DEV, "Resize while not connected was forced by the user!\n");
699 p_size = m_size;
700 }
701
Philipp Reisnerb411b362009-09-25 16:07:19 -0700702 if (p_size && m_size) {
703 size = min_t(sector_t, p_size, m_size);
704 } else {
705 if (la_size) {
706 size = la_size;
707 if (m_size && m_size < size)
708 size = m_size;
709 if (p_size && p_size < size)
710 size = p_size;
711 } else {
712 if (m_size)
713 size = m_size;
714 if (p_size)
715 size = p_size;
716 }
717 }
718
719 if (size == 0)
720 dev_err(DEV, "Both nodes diskless!\n");
721
722 if (u_size) {
723 if (u_size > size)
724 dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n",
725 (unsigned long)u_size>>1, (unsigned long)size>>1);
726 else
727 size = u_size;
728 }
729
730 return size;
731}
732
733/**
734 * drbd_check_al_size() - Ensures that the AL is of the right size
735 * @mdev: DRBD device.
736 *
737 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
738 * failed, and 0 on success. You should call drbd_md_sync() after you called
739 * this function.
740 */
741static int drbd_check_al_size(struct drbd_conf *mdev)
742{
743 struct lru_cache *n, *t;
744 struct lc_element *e;
745 unsigned int in_use;
746 int i;
747
748 ERR_IF(mdev->sync_conf.al_extents < 7)
749 mdev->sync_conf.al_extents = 127;
750
751 if (mdev->act_log &&
752 mdev->act_log->nr_elements == mdev->sync_conf.al_extents)
753 return 0;
754
755 in_use = 0;
756 t = mdev->act_log;
757 n = lc_create("act_log", drbd_al_ext_cache,
758 mdev->sync_conf.al_extents, sizeof(struct lc_element), 0);
759
760 if (n == NULL) {
761 dev_err(DEV, "Cannot allocate act_log lru!\n");
762 return -ENOMEM;
763 }
764 spin_lock_irq(&mdev->al_lock);
765 if (t) {
766 for (i = 0; i < t->nr_elements; i++) {
767 e = lc_element_by_index(t, i);
768 if (e->refcnt)
769 dev_err(DEV, "refcnt(%d)==%d\n",
770 e->lc_number, e->refcnt);
771 in_use += e->refcnt;
772 }
773 }
774 if (!in_use)
775 mdev->act_log = n;
776 spin_unlock_irq(&mdev->al_lock);
777 if (in_use) {
778 dev_err(DEV, "Activity log still in use!\n");
779 lc_destroy(n);
780 return -EBUSY;
781 } else {
782 if (t)
783 lc_destroy(t);
784 }
785 drbd_md_mark_dirty(mdev); /* we changed mdev->act_log->nr_elemens */
786 return 0;
787}
788
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100789void drbd_setup_queue_param(struct drbd_conf *mdev, unsigned int max_bio_size) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700790{
791 struct request_queue * const q = mdev->rq_queue;
792 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
793 int max_segments = mdev->ldev->dc.max_bio_bvecs;
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100794 int max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700795
Philipp Reisnerb411b362009-09-25 16:07:19 -0700796 blk_queue_logical_block_size(q, 512);
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100797 blk_queue_max_hw_sectors(q, max_hw_sectors);
798 /* This is the workaround for "bio would need to, but cannot, be split" */
799 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
800 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
801 blk_queue_stack_limits(q, b);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700802
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100803 dev_info(DEV, "max BIO size = %u\n", queue_max_hw_sectors(q) << 9);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700804
805 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
806 dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
807 q->backing_dev_info.ra_pages,
808 b->backing_dev_info.ra_pages);
809 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
810 }
811}
812
813/* serialize deconfig (worker exiting, doing cleanup)
814 * and reconfig (drbdsetup disk, drbdsetup net)
815 *
Lars Ellenbergc518d042010-09-01 09:50:23 +0200816 * Wait for a potentially exiting worker, then restart it,
817 * or start a new one. Flush any pending work, there may still be an
818 * after_state_change queued.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700819 */
820static void drbd_reconfig_start(struct drbd_conf *mdev)
821{
Lars Ellenberg6c6c7952009-11-16 15:48:54 +0100822 wait_event(mdev->state_wait, !test_and_set_bit(CONFIG_PENDING, &mdev->flags));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700823 wait_event(mdev->state_wait, !test_bit(DEVICE_DYING, &mdev->flags));
824 drbd_thread_start(&mdev->worker);
Lars Ellenbergc518d042010-09-01 09:50:23 +0200825 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700826}
827
828/* if still unconfigured, stops worker again.
829 * if configured now, clears CONFIG_PENDING.
830 * wakes potential waiters */
831static void drbd_reconfig_done(struct drbd_conf *mdev)
832{
833 spin_lock_irq(&mdev->req_lock);
834 if (mdev->state.disk == D_DISKLESS &&
835 mdev->state.conn == C_STANDALONE &&
836 mdev->state.role == R_SECONDARY) {
837 set_bit(DEVICE_DYING, &mdev->flags);
838 drbd_thread_stop_nowait(&mdev->worker);
839 } else
840 clear_bit(CONFIG_PENDING, &mdev->flags);
841 spin_unlock_irq(&mdev->req_lock);
842 wake_up(&mdev->state_wait);
843}
844
Philipp Reisner07782862010-08-31 12:00:50 +0200845/* Make sure IO is suspended before calling this function(). */
846static void drbd_suspend_al(struct drbd_conf *mdev)
847{
848 int s = 0;
849
850 if (lc_try_lock(mdev->act_log)) {
851 drbd_al_shrink(mdev);
852 lc_unlock(mdev->act_log);
853 } else {
854 dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n");
855 return;
856 }
857
858 spin_lock_irq(&mdev->req_lock);
859 if (mdev->state.conn < C_CONNECTED)
860 s = !test_and_set_bit(AL_SUSPENDED, &mdev->flags);
861
862 spin_unlock_irq(&mdev->req_lock);
863
864 if (s)
865 dev_info(DEV, "Suspended AL updates\n");
866}
867
Philipp Reisnerb411b362009-09-25 16:07:19 -0700868/* does always return 0;
869 * interesting return code is in reply->ret_code */
870static int drbd_nl_disk_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
871 struct drbd_nl_cfg_reply *reply)
872{
Andreas Gruenbacher116676c2010-12-08 13:33:11 +0100873 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700874 enum determine_dev_size dd;
875 sector_t max_possible_sectors;
876 sector_t min_md_device_sectors;
877 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
Tejun Heoe525fd82010-11-13 11:55:17 +0100878 struct block_device *bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700879 struct lru_cache *resync_lru = NULL;
880 union drbd_state ns, os;
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100881 unsigned int max_bio_size;
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +0100882 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700883 int cp_discovered = 0;
884 int logical_block_size;
885
886 drbd_reconfig_start(mdev);
887
888 /* if you want to reconfigure, please tear down first */
889 if (mdev->state.disk > D_DISKLESS) {
890 retcode = ERR_DISK_CONFIGURED;
891 goto fail;
892 }
Lars Ellenberg82f59cc2010-10-16 12:13:47 +0200893 /* It may just now have detached because of IO error. Make sure
894 * drbd_ldev_destroy is done already, we may end up here very fast,
895 * e.g. if someone calls attach from the on-io-error handler,
896 * to realize a "hot spare" feature (not that I'd recommend that) */
897 wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700898
899 /* allocation not in the IO path, cqueue thread context */
900 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
901 if (!nbc) {
902 retcode = ERR_NOMEM;
903 goto fail;
904 }
905
906 nbc->dc.disk_size = DRBD_DISK_SIZE_SECT_DEF;
907 nbc->dc.on_io_error = DRBD_ON_IO_ERROR_DEF;
908 nbc->dc.fencing = DRBD_FENCING_DEF;
909 nbc->dc.max_bio_bvecs = DRBD_MAX_BIO_BVECS_DEF;
910
911 if (!disk_conf_from_tags(mdev, nlp->tag_list, &nbc->dc)) {
912 retcode = ERR_MANDATORY_TAG;
913 goto fail;
914 }
915
916 if (nbc->dc.meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
917 retcode = ERR_MD_IDX_INVALID;
918 goto fail;
919 }
920
Philipp Reisner47ff2d02010-06-18 13:56:57 +0200921 if (get_net_conf(mdev)) {
922 int prot = mdev->net_conf->wire_protocol;
923 put_net_conf(mdev);
924 if (nbc->dc.fencing == FP_STONITH && prot == DRBD_PROT_A) {
925 retcode = ERR_STONITH_AND_PROT_A;
926 goto fail;
927 }
928 }
929
Tejun Heod4d77622010-11-13 11:55:18 +0100930 bdev = blkdev_get_by_path(nbc->dc.backing_dev,
931 FMODE_READ | FMODE_WRITE | FMODE_EXCL, mdev);
Tejun Heoe525fd82010-11-13 11:55:17 +0100932 if (IS_ERR(bdev)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700933 dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.backing_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +0100934 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700935 retcode = ERR_OPEN_DISK;
936 goto fail;
937 }
Tejun Heoe525fd82010-11-13 11:55:17 +0100938 nbc->backing_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700939
Tejun Heoe525fd82010-11-13 11:55:17 +0100940 /*
941 * meta_dev_idx >= 0: external fixed size, possibly multiple
942 * drbd sharing one meta device. TODO in that case, paranoia
943 * check that [md_bdev, meta_dev_idx] is not yet used by some
944 * other drbd minor! (if you use drbd.conf + drbdadm, that
945 * should check it for you already; but if you don't, or
946 * someone fooled it, we need to double check here)
947 */
Tejun Heod4d77622010-11-13 11:55:18 +0100948 bdev = blkdev_get_by_path(nbc->dc.meta_dev,
949 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
950 (nbc->dc.meta_dev_idx < 0) ?
951 (void *)mdev : (void *)drbd_m_holder);
Tejun Heoe525fd82010-11-13 11:55:17 +0100952 if (IS_ERR(bdev)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700953 dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.meta_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +0100954 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700955 retcode = ERR_OPEN_MD_DISK;
956 goto fail;
957 }
Tejun Heoe525fd82010-11-13 11:55:17 +0100958 nbc->md_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700959
Tejun Heoe525fd82010-11-13 11:55:17 +0100960 if ((nbc->backing_bdev == nbc->md_bdev) !=
961 (nbc->dc.meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
962 nbc->dc.meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
963 retcode = ERR_MD_IDX_INVALID;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700964 goto fail;
965 }
966
967 resync_lru = lc_create("resync", drbd_bm_ext_cache,
968 61, sizeof(struct bm_extent),
969 offsetof(struct bm_extent, lce));
970 if (!resync_lru) {
971 retcode = ERR_NOMEM;
Tejun Heoe525fd82010-11-13 11:55:17 +0100972 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700973 }
974
975 /* RT - for drbd_get_max_capacity() DRBD_MD_INDEX_FLEX_INT */
976 drbd_md_set_sector_offsets(mdev, nbc);
977
978 if (drbd_get_max_capacity(nbc) < nbc->dc.disk_size) {
979 dev_err(DEV, "max capacity %llu smaller than disk size %llu\n",
980 (unsigned long long) drbd_get_max_capacity(nbc),
981 (unsigned long long) nbc->dc.disk_size);
982 retcode = ERR_DISK_TO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +0100983 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700984 }
985
986 if (nbc->dc.meta_dev_idx < 0) {
987 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
988 /* at least one MB, otherwise it does not make sense */
989 min_md_device_sectors = (2<<10);
990 } else {
991 max_possible_sectors = DRBD_MAX_SECTORS;
992 min_md_device_sectors = MD_RESERVED_SECT * (nbc->dc.meta_dev_idx + 1);
993 }
994
Philipp Reisnerb411b362009-09-25 16:07:19 -0700995 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
996 retcode = ERR_MD_DISK_TO_SMALL;
997 dev_warn(DEV, "refusing attach: md-device too small, "
998 "at least %llu sectors needed for this meta-disk type\n",
999 (unsigned long long) min_md_device_sectors);
Tejun Heoe525fd82010-11-13 11:55:17 +01001000 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001001 }
1002
1003 /* Make sure the new disk is big enough
1004 * (we may currently be R_PRIMARY with no local disk...) */
1005 if (drbd_get_max_capacity(nbc) <
1006 drbd_get_capacity(mdev->this_bdev)) {
1007 retcode = ERR_DISK_TO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001008 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001009 }
1010
1011 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1012
Lars Ellenberg13529942009-10-12 19:07:49 +02001013 if (nbc->known_size > max_possible_sectors) {
1014 dev_warn(DEV, "==> truncating very big lower level device "
1015 "to currently maximum possible %llu sectors <==\n",
1016 (unsigned long long) max_possible_sectors);
1017 if (nbc->dc.meta_dev_idx >= 0)
1018 dev_warn(DEV, "==>> using internal or flexible "
1019 "meta data may help <<==\n");
1020 }
1021
Philipp Reisnerb411b362009-09-25 16:07:19 -07001022 drbd_suspend_io(mdev);
1023 /* also wait for the last barrier ack. */
Philipp Reisnerfb22c402010-09-08 23:20:21 +02001024 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_pending_cnt) || is_susp(mdev->state));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001025 /* and for any other previously queued work */
1026 drbd_flush_workqueue(mdev);
1027
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001028 rv = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE);
1029 retcode = rv; /* FIXME: Type mismatch. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001030 drbd_resume_io(mdev);
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001031 if (rv < SS_SUCCESS)
Tejun Heoe525fd82010-11-13 11:55:17 +01001032 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001033
1034 if (!get_ldev_if_state(mdev, D_ATTACHING))
1035 goto force_diskless;
1036
1037 drbd_md_set_sector_offsets(mdev, nbc);
1038
Lars Ellenberg4aa83b72010-02-26 16:53:24 +01001039 /* allocate a second IO page if logical_block_size != 512 */
1040 logical_block_size = bdev_logical_block_size(nbc->md_bdev);
1041 if (logical_block_size == 0)
1042 logical_block_size = MD_SECTOR_SIZE;
1043
1044 if (logical_block_size != MD_SECTOR_SIZE) {
1045 if (!mdev->md_io_tmpp) {
1046 struct page *page = alloc_page(GFP_NOIO);
1047 if (!page)
1048 goto force_diskless_dec;
1049
1050 dev_warn(DEV, "Meta data's bdev logical_block_size = %d != %d\n",
1051 logical_block_size, MD_SECTOR_SIZE);
1052 dev_warn(DEV, "Workaround engaged (has performance impact).\n");
1053
1054 mdev->md_io_tmpp = page;
1055 }
1056 }
1057
Philipp Reisnerb411b362009-09-25 16:07:19 -07001058 if (!mdev->bitmap) {
1059 if (drbd_bm_init(mdev)) {
1060 retcode = ERR_NOMEM;
1061 goto force_diskless_dec;
1062 }
1063 }
1064
1065 retcode = drbd_md_read(mdev, nbc);
1066 if (retcode != NO_ERROR)
1067 goto force_diskless_dec;
1068
1069 if (mdev->state.conn < C_CONNECTED &&
1070 mdev->state.role == R_PRIMARY &&
1071 (mdev->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
1072 dev_err(DEV, "Can only attach to data with current UUID=%016llX\n",
1073 (unsigned long long)mdev->ed_uuid);
1074 retcode = ERR_DATA_NOT_CURRENT;
1075 goto force_diskless_dec;
1076 }
1077
1078 /* Since we are diskless, fix the activity log first... */
1079 if (drbd_check_al_size(mdev)) {
1080 retcode = ERR_NOMEM;
1081 goto force_diskless_dec;
1082 }
1083
1084 /* Prevent shrinking of consistent devices ! */
1085 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
Philipp Reisnera393db62009-12-22 13:35:52 +01001086 drbd_new_dev_size(mdev, nbc, 0) < nbc->md.la_size_sect) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001087 dev_warn(DEV, "refusing to truncate a consistent device\n");
1088 retcode = ERR_DISK_TO_SMALL;
1089 goto force_diskless_dec;
1090 }
1091
1092 if (!drbd_al_read_log(mdev, nbc)) {
1093 retcode = ERR_IO_MD_DISK;
1094 goto force_diskless_dec;
1095 }
1096
Philipp Reisnerb411b362009-09-25 16:07:19 -07001097 /* Reset the "barriers don't work" bits here, then force meta data to
1098 * be written, to ensure we determine if barriers are supported. */
1099 if (nbc->dc.no_md_flush)
Philipp Reisnera8a4e512010-08-25 10:21:04 +02001100 set_bit(MD_NO_FUA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001101 else
Philipp Reisnera8a4e512010-08-25 10:21:04 +02001102 clear_bit(MD_NO_FUA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001103
1104 /* Point of no return reached.
1105 * Devices and memory are no longer released by error cleanup below.
1106 * now mdev takes over responsibility, and the state engine should
1107 * clean it up somewhere. */
1108 D_ASSERT(mdev->ldev == NULL);
1109 mdev->ldev = nbc;
1110 mdev->resync = resync_lru;
1111 nbc = NULL;
1112 resync_lru = NULL;
1113
Philipp Reisner2451fc32010-08-24 13:43:11 +02001114 mdev->write_ordering = WO_bdev_flush;
1115 drbd_bump_write_ordering(mdev, WO_bdev_flush);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001116
1117 if (drbd_md_test_flag(mdev->ldev, MDF_CRASHED_PRIMARY))
1118 set_bit(CRASHED_PRIMARY, &mdev->flags);
1119 else
1120 clear_bit(CRASHED_PRIMARY, &mdev->flags);
1121
Philipp Reisner894c6a92010-06-18 16:03:20 +02001122 if (drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
Philipp Reisnerfb22c402010-09-08 23:20:21 +02001123 !(mdev->state.role == R_PRIMARY && mdev->state.susp_nod)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001124 set_bit(CRASHED_PRIMARY, &mdev->flags);
1125 cp_discovered = 1;
1126 }
1127
1128 mdev->send_cnt = 0;
1129 mdev->recv_cnt = 0;
1130 mdev->read_cnt = 0;
1131 mdev->writ_cnt = 0;
1132
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001133 max_bio_size = DRBD_MAX_BIO_SIZE;
Lars Ellenberg8979d9c2010-09-14 15:56:29 +02001134 if (mdev->state.conn == C_CONNECTED) {
1135 /* We are Primary, Connected, and now attach a new local
1136 * backing store. We must not increase the user visible maximum
1137 * bio size on this device to something the peer may not be
1138 * able to handle. */
1139 if (mdev->agreed_pro_version < 94)
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001140 max_bio_size = queue_max_hw_sectors(mdev->rq_queue) << 9;
Lars Ellenberg8979d9c2010-09-14 15:56:29 +02001141 else if (mdev->agreed_pro_version == 94)
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001142 max_bio_size = DRBD_MAX_SIZE_H80_PACKET;
Lars Ellenberg8979d9c2010-09-14 15:56:29 +02001143 /* else: drbd 8.3.9 and later, stay with default */
1144 }
1145
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001146 drbd_setup_queue_param(mdev, max_bio_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001147
1148 /* If I am currently not R_PRIMARY,
1149 * but meta data primary indicator is set,
1150 * I just now recover from a hard crash,
1151 * and have been R_PRIMARY before that crash.
1152 *
1153 * Now, if I had no connection before that crash
1154 * (have been degraded R_PRIMARY), chances are that
1155 * I won't find my peer now either.
1156 *
1157 * In that case, and _only_ in that case,
1158 * we use the degr-wfc-timeout instead of the default,
1159 * so we can automatically recover from a crash of a
1160 * degraded but active "cluster" after a certain timeout.
1161 */
1162 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
1163 if (mdev->state.role != R_PRIMARY &&
1164 drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
1165 !drbd_md_test_flag(mdev->ldev, MDF_CONNECTED_IND))
1166 set_bit(USE_DEGR_WFC_T, &mdev->flags);
1167
Philipp Reisnera393db62009-12-22 13:35:52 +01001168 dd = drbd_determin_dev_size(mdev, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001169 if (dd == dev_size_error) {
1170 retcode = ERR_NOMEM_BITMAP;
1171 goto force_diskless_dec;
1172 } else if (dd == grew)
1173 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
1174
1175 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
1176 dev_info(DEV, "Assuming that all blocks are out of sync "
1177 "(aka FullSync)\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001178 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write,
1179 "set_n_write from attaching", BM_LOCKED_MASK)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001180 retcode = ERR_IO_MD_DISK;
1181 goto force_diskless_dec;
1182 }
1183 } else {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001184 if (drbd_bitmap_io(mdev, &drbd_bm_read,
1185 "read from attaching", BM_LOCKED_MASK) < 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001186 retcode = ERR_IO_MD_DISK;
1187 goto force_diskless_dec;
1188 }
1189 }
1190
1191 if (cp_discovered) {
1192 drbd_al_apply_to_bm(mdev);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001193 if (drbd_bitmap_io(mdev, &drbd_bm_write,
1194 "crashed primary apply AL", BM_LOCKED_MASK)) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001195 retcode = ERR_IO_MD_DISK;
1196 goto force_diskless_dec;
1197 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001198 }
1199
Philipp Reisner07782862010-08-31 12:00:50 +02001200 if (_drbd_bm_total_weight(mdev) == drbd_bm_bits(mdev))
1201 drbd_suspend_al(mdev); /* IO is still suspended here... */
1202
Philipp Reisnerb411b362009-09-25 16:07:19 -07001203 spin_lock_irq(&mdev->req_lock);
1204 os = mdev->state;
1205 ns.i = os.i;
1206 /* If MDF_CONSISTENT is not set go into inconsistent state,
1207 otherwise investigate MDF_WasUpToDate...
1208 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1209 otherwise into D_CONSISTENT state.
1210 */
1211 if (drbd_md_test_flag(mdev->ldev, MDF_CONSISTENT)) {
1212 if (drbd_md_test_flag(mdev->ldev, MDF_WAS_UP_TO_DATE))
1213 ns.disk = D_CONSISTENT;
1214 else
1215 ns.disk = D_OUTDATED;
1216 } else {
1217 ns.disk = D_INCONSISTENT;
1218 }
1219
1220 if (drbd_md_test_flag(mdev->ldev, MDF_PEER_OUT_DATED))
1221 ns.pdsk = D_OUTDATED;
1222
1223 if ( ns.disk == D_CONSISTENT &&
1224 (ns.pdsk == D_OUTDATED || mdev->ldev->dc.fencing == FP_DONT_CARE))
1225 ns.disk = D_UP_TO_DATE;
1226
1227 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1228 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1229 this point, because drbd_request_state() modifies these
1230 flags. */
1231
1232 /* In case we are C_CONNECTED postpone any decision on the new disk
1233 state after the negotiation phase. */
1234 if (mdev->state.conn == C_CONNECTED) {
1235 mdev->new_state_tmp.i = ns.i;
1236 ns.i = os.i;
1237 ns.disk = D_NEGOTIATING;
Philipp Reisnerdc66c742010-06-02 14:31:29 +02001238
1239 /* We expect to receive up-to-date UUIDs soon.
1240 To avoid a race in receive_state, free p_uuid while
1241 holding req_lock. I.e. atomic with the state change */
1242 kfree(mdev->p_uuid);
1243 mdev->p_uuid = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001244 }
1245
1246 rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
1247 ns = mdev->state;
1248 spin_unlock_irq(&mdev->req_lock);
1249
1250 if (rv < SS_SUCCESS)
1251 goto force_diskless_dec;
1252
1253 if (mdev->state.role == R_PRIMARY)
1254 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
1255 else
1256 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
1257
1258 drbd_md_mark_dirty(mdev);
1259 drbd_md_sync(mdev);
1260
1261 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1262 put_ldev(mdev);
1263 reply->ret_code = retcode;
1264 drbd_reconfig_done(mdev);
1265 return 0;
1266
1267 force_diskless_dec:
1268 put_ldev(mdev);
1269 force_diskless:
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001270 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001271 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001272 fail:
1273 if (nbc) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001274 if (nbc->backing_bdev)
1275 blkdev_put(nbc->backing_bdev,
1276 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1277 if (nbc->md_bdev)
1278 blkdev_put(nbc->md_bdev,
1279 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001280 kfree(nbc);
1281 }
1282 lc_destroy(resync_lru);
1283
1284 reply->ret_code = retcode;
1285 drbd_reconfig_done(mdev);
1286 return 0;
1287}
1288
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001289/* Detaching the disk is a process in multiple stages. First we need to lock
1290 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1291 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1292 * internal references as well.
1293 * Only then we have finally detached. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001294static int drbd_nl_detach(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1295 struct drbd_nl_cfg_reply *reply)
1296{
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001297 drbd_suspend_io(mdev); /* so no-one is stuck in drbd_al_begin_io */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001298 reply->ret_code = drbd_request_state(mdev, NS(disk, D_DISKLESS));
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001299 if (mdev->state.disk == D_DISKLESS)
1300 wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
1301 drbd_resume_io(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001302 return 0;
1303}
1304
1305static int drbd_nl_net_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1306 struct drbd_nl_cfg_reply *reply)
1307{
1308 int i, ns;
Andreas Gruenbacher116676c2010-12-08 13:33:11 +01001309 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001310 struct net_conf *new_conf = NULL;
1311 struct crypto_hash *tfm = NULL;
1312 struct crypto_hash *integrity_w_tfm = NULL;
1313 struct crypto_hash *integrity_r_tfm = NULL;
1314 struct hlist_head *new_tl_hash = NULL;
1315 struct hlist_head *new_ee_hash = NULL;
1316 struct drbd_conf *odev;
1317 char hmac_name[CRYPTO_MAX_ALG_NAME];
1318 void *int_dig_out = NULL;
1319 void *int_dig_in = NULL;
1320 void *int_dig_vv = NULL;
1321 struct sockaddr *new_my_addr, *new_peer_addr, *taken_addr;
1322
1323 drbd_reconfig_start(mdev);
1324
1325 if (mdev->state.conn > C_STANDALONE) {
1326 retcode = ERR_NET_CONFIGURED;
1327 goto fail;
1328 }
1329
1330 /* allocation not in the IO path, cqueue thread context */
Julia Lawall2db4e422010-05-13 22:02:21 +02001331 new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001332 if (!new_conf) {
1333 retcode = ERR_NOMEM;
1334 goto fail;
1335 }
1336
Philipp Reisnerb411b362009-09-25 16:07:19 -07001337 new_conf->timeout = DRBD_TIMEOUT_DEF;
1338 new_conf->try_connect_int = DRBD_CONNECT_INT_DEF;
1339 new_conf->ping_int = DRBD_PING_INT_DEF;
1340 new_conf->max_epoch_size = DRBD_MAX_EPOCH_SIZE_DEF;
1341 new_conf->max_buffers = DRBD_MAX_BUFFERS_DEF;
1342 new_conf->unplug_watermark = DRBD_UNPLUG_WATERMARK_DEF;
1343 new_conf->sndbuf_size = DRBD_SNDBUF_SIZE_DEF;
1344 new_conf->rcvbuf_size = DRBD_RCVBUF_SIZE_DEF;
1345 new_conf->ko_count = DRBD_KO_COUNT_DEF;
1346 new_conf->after_sb_0p = DRBD_AFTER_SB_0P_DEF;
1347 new_conf->after_sb_1p = DRBD_AFTER_SB_1P_DEF;
1348 new_conf->after_sb_2p = DRBD_AFTER_SB_2P_DEF;
1349 new_conf->want_lose = 0;
1350 new_conf->two_primaries = 0;
1351 new_conf->wire_protocol = DRBD_PROT_C;
1352 new_conf->ping_timeo = DRBD_PING_TIMEO_DEF;
1353 new_conf->rr_conflict = DRBD_RR_CONFLICT_DEF;
Philipp Reisner422028b2010-10-27 11:12:07 +02001354 new_conf->on_congestion = DRBD_ON_CONGESTION_DEF;
1355 new_conf->cong_extents = DRBD_CONG_EXTENTS_DEF;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001356
1357 if (!net_conf_from_tags(mdev, nlp->tag_list, new_conf)) {
1358 retcode = ERR_MANDATORY_TAG;
1359 goto fail;
1360 }
1361
1362 if (new_conf->two_primaries
1363 && (new_conf->wire_protocol != DRBD_PROT_C)) {
1364 retcode = ERR_NOT_PROTO_C;
1365 goto fail;
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001366 }
1367
1368 if (get_ldev(mdev)) {
1369 enum drbd_fencing_p fp = mdev->ldev->dc.fencing;
1370 put_ldev(mdev);
1371 if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH) {
1372 retcode = ERR_STONITH_AND_PROT_A;
1373 goto fail;
1374 }
1375 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001376
Philipp Reisner422028b2010-10-27 11:12:07 +02001377 if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A) {
1378 retcode = ERR_CONG_NOT_PROTO_A;
1379 goto fail;
1380 }
1381
Philipp Reisnerb411b362009-09-25 16:07:19 -07001382 if (mdev->state.role == R_PRIMARY && new_conf->want_lose) {
1383 retcode = ERR_DISCARD;
1384 goto fail;
1385 }
1386
1387 retcode = NO_ERROR;
1388
1389 new_my_addr = (struct sockaddr *)&new_conf->my_addr;
1390 new_peer_addr = (struct sockaddr *)&new_conf->peer_addr;
1391 for (i = 0; i < minor_count; i++) {
1392 odev = minor_to_mdev(i);
1393 if (!odev || odev == mdev)
1394 continue;
1395 if (get_net_conf(odev)) {
1396 taken_addr = (struct sockaddr *)&odev->net_conf->my_addr;
1397 if (new_conf->my_addr_len == odev->net_conf->my_addr_len &&
1398 !memcmp(new_my_addr, taken_addr, new_conf->my_addr_len))
1399 retcode = ERR_LOCAL_ADDR;
1400
1401 taken_addr = (struct sockaddr *)&odev->net_conf->peer_addr;
1402 if (new_conf->peer_addr_len == odev->net_conf->peer_addr_len &&
1403 !memcmp(new_peer_addr, taken_addr, new_conf->peer_addr_len))
1404 retcode = ERR_PEER_ADDR;
1405
1406 put_net_conf(odev);
1407 if (retcode != NO_ERROR)
1408 goto fail;
1409 }
1410 }
1411
1412 if (new_conf->cram_hmac_alg[0] != 0) {
1413 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
1414 new_conf->cram_hmac_alg);
1415 tfm = crypto_alloc_hash(hmac_name, 0, CRYPTO_ALG_ASYNC);
1416 if (IS_ERR(tfm)) {
1417 tfm = NULL;
1418 retcode = ERR_AUTH_ALG;
1419 goto fail;
1420 }
1421
Philipp Reisner07982192009-12-28 16:58:38 +01001422 if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001423 retcode = ERR_AUTH_ALG_ND;
1424 goto fail;
1425 }
1426 }
1427
1428 if (new_conf->integrity_alg[0]) {
1429 integrity_w_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC);
1430 if (IS_ERR(integrity_w_tfm)) {
1431 integrity_w_tfm = NULL;
1432 retcode=ERR_INTEGRITY_ALG;
1433 goto fail;
1434 }
1435
1436 if (!drbd_crypto_is_hash(crypto_hash_tfm(integrity_w_tfm))) {
1437 retcode=ERR_INTEGRITY_ALG_ND;
1438 goto fail;
1439 }
1440
1441 integrity_r_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC);
1442 if (IS_ERR(integrity_r_tfm)) {
1443 integrity_r_tfm = NULL;
1444 retcode=ERR_INTEGRITY_ALG;
1445 goto fail;
1446 }
1447 }
1448
1449 ns = new_conf->max_epoch_size/8;
1450 if (mdev->tl_hash_s != ns) {
1451 new_tl_hash = kzalloc(ns*sizeof(void *), GFP_KERNEL);
1452 if (!new_tl_hash) {
1453 retcode = ERR_NOMEM;
1454 goto fail;
1455 }
1456 }
1457
1458 ns = new_conf->max_buffers/8;
1459 if (new_conf->two_primaries && (mdev->ee_hash_s != ns)) {
1460 new_ee_hash = kzalloc(ns*sizeof(void *), GFP_KERNEL);
1461 if (!new_ee_hash) {
1462 retcode = ERR_NOMEM;
1463 goto fail;
1464 }
1465 }
1466
1467 ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
1468
1469 if (integrity_w_tfm) {
1470 i = crypto_hash_digestsize(integrity_w_tfm);
1471 int_dig_out = kmalloc(i, GFP_KERNEL);
1472 if (!int_dig_out) {
1473 retcode = ERR_NOMEM;
1474 goto fail;
1475 }
1476 int_dig_in = kmalloc(i, GFP_KERNEL);
1477 if (!int_dig_in) {
1478 retcode = ERR_NOMEM;
1479 goto fail;
1480 }
1481 int_dig_vv = kmalloc(i, GFP_KERNEL);
1482 if (!int_dig_vv) {
1483 retcode = ERR_NOMEM;
1484 goto fail;
1485 }
1486 }
1487
1488 if (!mdev->bitmap) {
1489 if(drbd_bm_init(mdev)) {
1490 retcode = ERR_NOMEM;
1491 goto fail;
1492 }
1493 }
1494
Philipp Reisnerf70b35112010-06-24 14:34:40 +02001495 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001496 spin_lock_irq(&mdev->req_lock);
1497 if (mdev->net_conf != NULL) {
1498 retcode = ERR_NET_CONFIGURED;
1499 spin_unlock_irq(&mdev->req_lock);
1500 goto fail;
1501 }
1502 mdev->net_conf = new_conf;
1503
1504 mdev->send_cnt = 0;
1505 mdev->recv_cnt = 0;
1506
1507 if (new_tl_hash) {
1508 kfree(mdev->tl_hash);
1509 mdev->tl_hash_s = mdev->net_conf->max_epoch_size/8;
1510 mdev->tl_hash = new_tl_hash;
1511 }
1512
1513 if (new_ee_hash) {
1514 kfree(mdev->ee_hash);
1515 mdev->ee_hash_s = mdev->net_conf->max_buffers/8;
1516 mdev->ee_hash = new_ee_hash;
1517 }
1518
1519 crypto_free_hash(mdev->cram_hmac_tfm);
1520 mdev->cram_hmac_tfm = tfm;
1521
1522 crypto_free_hash(mdev->integrity_w_tfm);
1523 mdev->integrity_w_tfm = integrity_w_tfm;
1524
1525 crypto_free_hash(mdev->integrity_r_tfm);
1526 mdev->integrity_r_tfm = integrity_r_tfm;
1527
1528 kfree(mdev->int_dig_out);
1529 kfree(mdev->int_dig_in);
1530 kfree(mdev->int_dig_vv);
1531 mdev->int_dig_out=int_dig_out;
1532 mdev->int_dig_in=int_dig_in;
1533 mdev->int_dig_vv=int_dig_vv;
Philipp Reisnerf70b35112010-06-24 14:34:40 +02001534 retcode = _drbd_set_state(_NS(mdev, conn, C_UNCONNECTED), CS_VERBOSE, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001535 spin_unlock_irq(&mdev->req_lock);
1536
Philipp Reisnerb411b362009-09-25 16:07:19 -07001537 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1538 reply->ret_code = retcode;
1539 drbd_reconfig_done(mdev);
1540 return 0;
1541
1542fail:
1543 kfree(int_dig_out);
1544 kfree(int_dig_in);
1545 kfree(int_dig_vv);
1546 crypto_free_hash(tfm);
1547 crypto_free_hash(integrity_w_tfm);
1548 crypto_free_hash(integrity_r_tfm);
1549 kfree(new_tl_hash);
1550 kfree(new_ee_hash);
1551 kfree(new_conf);
1552
1553 reply->ret_code = retcode;
1554 drbd_reconfig_done(mdev);
1555 return 0;
1556}
1557
1558static int drbd_nl_disconnect(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1559 struct drbd_nl_cfg_reply *reply)
1560{
1561 int retcode;
Philipp Reisner2561b9c2010-12-03 15:22:48 +01001562 struct disconnect dc;
1563
1564 memset(&dc, 0, sizeof(struct disconnect));
1565 if (!disconnect_from_tags(mdev, nlp->tag_list, &dc)) {
1566 retcode = ERR_MANDATORY_TAG;
1567 goto fail;
1568 }
1569
1570 if (dc.force) {
1571 spin_lock_irq(&mdev->req_lock);
1572 if (mdev->state.conn >= C_WF_CONNECTION)
1573 _drbd_set_state(_NS(mdev, conn, C_DISCONNECTING), CS_HARD, NULL);
1574 spin_unlock_irq(&mdev->req_lock);
1575 goto done;
1576 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001577
1578 retcode = _drbd_request_state(mdev, NS(conn, C_DISCONNECTING), CS_ORDERED);
1579
1580 if (retcode == SS_NOTHING_TO_DO)
1581 goto done;
1582 else if (retcode == SS_ALREADY_STANDALONE)
1583 goto done;
1584 else if (retcode == SS_PRIMARY_NOP) {
1585 /* Our statche checking code wants to see the peer outdated. */
1586 retcode = drbd_request_state(mdev, NS2(conn, C_DISCONNECTING,
1587 pdsk, D_OUTDATED));
1588 } else if (retcode == SS_CW_FAILED_BY_PEER) {
1589 /* The peer probably wants to see us outdated. */
1590 retcode = _drbd_request_state(mdev, NS2(conn, C_DISCONNECTING,
1591 disk, D_OUTDATED),
1592 CS_ORDERED);
1593 if (retcode == SS_IS_DISKLESS || retcode == SS_LOWER_THAN_OUTDATED) {
1594 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
1595 retcode = SS_SUCCESS;
1596 }
1597 }
1598
1599 if (retcode < SS_SUCCESS)
1600 goto fail;
1601
1602 if (wait_event_interruptible(mdev->state_wait,
1603 mdev->state.conn != C_DISCONNECTING)) {
1604 /* Do not test for mdev->state.conn == C_STANDALONE, since
1605 someone else might connect us in the mean time! */
1606 retcode = ERR_INTR;
1607 goto fail;
1608 }
1609
1610 done:
1611 retcode = NO_ERROR;
1612 fail:
1613 drbd_md_sync(mdev);
1614 reply->ret_code = retcode;
1615 return 0;
1616}
1617
1618void resync_after_online_grow(struct drbd_conf *mdev)
1619{
1620 int iass; /* I am sync source */
1621
1622 dev_info(DEV, "Resync of new storage after online grow\n");
1623 if (mdev->state.role != mdev->state.peer)
1624 iass = (mdev->state.role == R_PRIMARY);
1625 else
1626 iass = test_bit(DISCARD_CONCURRENT, &mdev->flags);
1627
1628 if (iass)
1629 drbd_start_resync(mdev, C_SYNC_SOURCE);
1630 else
1631 _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
1632}
1633
1634static int drbd_nl_resize(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1635 struct drbd_nl_cfg_reply *reply)
1636{
1637 struct resize rs;
1638 int retcode = NO_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001639 enum determine_dev_size dd;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01001640 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001641
1642 memset(&rs, 0, sizeof(struct resize));
1643 if (!resize_from_tags(mdev, nlp->tag_list, &rs)) {
1644 retcode = ERR_MANDATORY_TAG;
1645 goto fail;
1646 }
1647
1648 if (mdev->state.conn > C_CONNECTED) {
1649 retcode = ERR_RESIZE_RESYNC;
1650 goto fail;
1651 }
1652
1653 if (mdev->state.role == R_SECONDARY &&
1654 mdev->state.peer == R_SECONDARY) {
1655 retcode = ERR_NO_PRIMARY;
1656 goto fail;
1657 }
1658
1659 if (!get_ldev(mdev)) {
1660 retcode = ERR_NO_DISK;
1661 goto fail;
1662 }
1663
Philipp Reisner6495d2c2010-03-24 16:07:04 +01001664 if (rs.no_resync && mdev->agreed_pro_version < 93) {
1665 retcode = ERR_NEED_APV_93;
1666 goto fail;
1667 }
1668
Philipp Reisner087c2492010-03-26 13:49:56 +01001669 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001670 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001671
1672 mdev->ldev->dc.disk_size = (sector_t)rs.resize_size;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01001673 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
1674 dd = drbd_determin_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001675 drbd_md_sync(mdev);
1676 put_ldev(mdev);
1677 if (dd == dev_size_error) {
1678 retcode = ERR_NOMEM_BITMAP;
1679 goto fail;
1680 }
1681
Philipp Reisner087c2492010-03-26 13:49:56 +01001682 if (mdev->state.conn == C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001683 if (dd == grew)
1684 set_bit(RESIZE_PENDING, &mdev->flags);
1685
1686 drbd_send_uuids(mdev);
Philipp Reisner6495d2c2010-03-24 16:07:04 +01001687 drbd_send_sizes(mdev, 1, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001688 }
1689
1690 fail:
1691 reply->ret_code = retcode;
1692 return 0;
1693}
1694
1695static int drbd_nl_syncer_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1696 struct drbd_nl_cfg_reply *reply)
1697{
1698 int retcode = NO_ERROR;
1699 int err;
1700 int ovr; /* online verify running */
1701 int rsr; /* re-sync running */
1702 struct crypto_hash *verify_tfm = NULL;
1703 struct crypto_hash *csums_tfm = NULL;
1704 struct syncer_conf sc;
1705 cpumask_var_t new_cpu_mask;
Philipp Reisner778f2712010-07-06 11:14:00 +02001706 int *rs_plan_s = NULL;
1707 int fifo_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001708
1709 if (!zalloc_cpumask_var(&new_cpu_mask, GFP_KERNEL)) {
1710 retcode = ERR_NOMEM;
1711 goto fail;
1712 }
1713
1714 if (nlp->flags & DRBD_NL_SET_DEFAULTS) {
1715 memset(&sc, 0, sizeof(struct syncer_conf));
1716 sc.rate = DRBD_RATE_DEF;
1717 sc.after = DRBD_AFTER_DEF;
1718 sc.al_extents = DRBD_AL_EXTENTS_DEF;
Philipp Reisner265be2d2010-05-31 10:14:17 +02001719 sc.on_no_data = DRBD_ON_NO_DATA_DEF;
Philipp Reisner9a31d712010-07-05 13:42:03 +02001720 sc.c_plan_ahead = DRBD_C_PLAN_AHEAD_DEF;
1721 sc.c_delay_target = DRBD_C_DELAY_TARGET_DEF;
1722 sc.c_fill_target = DRBD_C_FILL_TARGET_DEF;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001723 sc.c_max_rate = DRBD_C_MAX_RATE_DEF;
1724 sc.c_min_rate = DRBD_C_MIN_RATE_DEF;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001725 } else
1726 memcpy(&sc, &mdev->sync_conf, sizeof(struct syncer_conf));
1727
1728 if (!syncer_conf_from_tags(mdev, nlp->tag_list, &sc)) {
1729 retcode = ERR_MANDATORY_TAG;
1730 goto fail;
1731 }
1732
1733 /* re-sync running */
1734 rsr = ( mdev->state.conn == C_SYNC_SOURCE ||
1735 mdev->state.conn == C_SYNC_TARGET ||
1736 mdev->state.conn == C_PAUSED_SYNC_S ||
1737 mdev->state.conn == C_PAUSED_SYNC_T );
1738
1739 if (rsr && strcmp(sc.csums_alg, mdev->sync_conf.csums_alg)) {
1740 retcode = ERR_CSUMS_RESYNC_RUNNING;
1741 goto fail;
1742 }
1743
1744 if (!rsr && sc.csums_alg[0]) {
1745 csums_tfm = crypto_alloc_hash(sc.csums_alg, 0, CRYPTO_ALG_ASYNC);
1746 if (IS_ERR(csums_tfm)) {
1747 csums_tfm = NULL;
1748 retcode = ERR_CSUMS_ALG;
1749 goto fail;
1750 }
1751
1752 if (!drbd_crypto_is_hash(crypto_hash_tfm(csums_tfm))) {
1753 retcode = ERR_CSUMS_ALG_ND;
1754 goto fail;
1755 }
1756 }
1757
1758 /* online verify running */
1759 ovr = (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T);
1760
1761 if (ovr) {
1762 if (strcmp(sc.verify_alg, mdev->sync_conf.verify_alg)) {
1763 retcode = ERR_VERIFY_RUNNING;
1764 goto fail;
1765 }
1766 }
1767
1768 if (!ovr && sc.verify_alg[0]) {
1769 verify_tfm = crypto_alloc_hash(sc.verify_alg, 0, CRYPTO_ALG_ASYNC);
1770 if (IS_ERR(verify_tfm)) {
1771 verify_tfm = NULL;
1772 retcode = ERR_VERIFY_ALG;
1773 goto fail;
1774 }
1775
1776 if (!drbd_crypto_is_hash(crypto_hash_tfm(verify_tfm))) {
1777 retcode = ERR_VERIFY_ALG_ND;
1778 goto fail;
1779 }
1780 }
1781
1782 /* silently ignore cpu mask on UP kernel */
1783 if (nr_cpu_ids > 1 && sc.cpu_mask[0] != 0) {
1784 err = __bitmap_parse(sc.cpu_mask, 32, 0,
1785 cpumask_bits(new_cpu_mask), nr_cpu_ids);
1786 if (err) {
1787 dev_warn(DEV, "__bitmap_parse() failed with %d\n", err);
1788 retcode = ERR_CPU_MASK_PARSE;
1789 goto fail;
1790 }
1791 }
1792
1793 ERR_IF (sc.rate < 1) sc.rate = 1;
1794 ERR_IF (sc.al_extents < 7) sc.al_extents = 127; /* arbitrary minimum */
1795#define AL_MAX ((MD_AL_MAX_SIZE-1) * AL_EXTENTS_PT)
1796 if (sc.al_extents > AL_MAX) {
1797 dev_err(DEV, "sc.al_extents > %d\n", AL_MAX);
1798 sc.al_extents = AL_MAX;
1799 }
1800#undef AL_MAX
1801
Lars Ellenbergef50a3e2010-09-01 14:39:30 +02001802 /* to avoid spurious errors when configuring minors before configuring
1803 * the minors they depend on: if necessary, first create the minor we
1804 * depend on */
1805 if (sc.after >= 0)
1806 ensure_mdev(sc.after, 1);
1807
Philipp Reisnerb411b362009-09-25 16:07:19 -07001808 /* most sanity checks done, try to assign the new sync-after
1809 * dependency. need to hold the global lock in there,
1810 * to avoid a race in the dependency loop check. */
1811 retcode = drbd_alter_sa(mdev, sc.after);
1812 if (retcode != NO_ERROR)
1813 goto fail;
1814
Philipp Reisner778f2712010-07-06 11:14:00 +02001815 fifo_size = (sc.c_plan_ahead * 10 * SLEEP_TIME) / HZ;
1816 if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) {
1817 rs_plan_s = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL);
1818 if (!rs_plan_s) {
1819 dev_err(DEV, "kmalloc of fifo_buffer failed");
1820 retcode = ERR_NOMEM;
1821 goto fail;
1822 }
1823 }
1824
Philipp Reisnerb411b362009-09-25 16:07:19 -07001825 /* ok, assign the rest of it as well.
1826 * lock against receive_SyncParam() */
1827 spin_lock(&mdev->peer_seq_lock);
1828 mdev->sync_conf = sc;
1829
1830 if (!rsr) {
1831 crypto_free_hash(mdev->csums_tfm);
1832 mdev->csums_tfm = csums_tfm;
1833 csums_tfm = NULL;
1834 }
1835
1836 if (!ovr) {
1837 crypto_free_hash(mdev->verify_tfm);
1838 mdev->verify_tfm = verify_tfm;
1839 verify_tfm = NULL;
1840 }
Philipp Reisner778f2712010-07-06 11:14:00 +02001841
1842 if (fifo_size != mdev->rs_plan_s.size) {
1843 kfree(mdev->rs_plan_s.values);
1844 mdev->rs_plan_s.values = rs_plan_s;
1845 mdev->rs_plan_s.size = fifo_size;
1846 mdev->rs_planed = 0;
1847 rs_plan_s = NULL;
1848 }
1849
Philipp Reisnerb411b362009-09-25 16:07:19 -07001850 spin_unlock(&mdev->peer_seq_lock);
1851
1852 if (get_ldev(mdev)) {
1853 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
1854 drbd_al_shrink(mdev);
1855 err = drbd_check_al_size(mdev);
1856 lc_unlock(mdev->act_log);
1857 wake_up(&mdev->al_wait);
1858
1859 put_ldev(mdev);
1860 drbd_md_sync(mdev);
1861
1862 if (err) {
1863 retcode = ERR_NOMEM;
1864 goto fail;
1865 }
1866 }
1867
1868 if (mdev->state.conn >= C_CONNECTED)
1869 drbd_send_sync_param(mdev, &sc);
1870
1871 if (!cpumask_equal(mdev->cpu_mask, new_cpu_mask)) {
1872 cpumask_copy(mdev->cpu_mask, new_cpu_mask);
1873 drbd_calc_cpu_mask(mdev);
1874 mdev->receiver.reset_cpu_mask = 1;
1875 mdev->asender.reset_cpu_mask = 1;
1876 mdev->worker.reset_cpu_mask = 1;
1877 }
1878
1879 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1880fail:
Philipp Reisner778f2712010-07-06 11:14:00 +02001881 kfree(rs_plan_s);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001882 free_cpumask_var(new_cpu_mask);
1883 crypto_free_hash(csums_tfm);
1884 crypto_free_hash(verify_tfm);
1885 reply->ret_code = retcode;
1886 return 0;
1887}
1888
1889static int drbd_nl_invalidate(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1890 struct drbd_nl_cfg_reply *reply)
1891{
1892 int retcode;
1893
Lars Ellenberg194bfb32011-01-18 10:38:01 +01001894 /* If there is still bitmap IO pending, probably because of a previous
1895 * resync just being finished, wait for it before requesting a new resync. */
1896 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
1897
Philipp Reisnerb411b362009-09-25 16:07:19 -07001898 retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T), CS_ORDERED);
1899
1900 if (retcode < SS_SUCCESS && retcode != SS_NEED_CONNECTION)
1901 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
1902
1903 while (retcode == SS_NEED_CONNECTION) {
1904 spin_lock_irq(&mdev->req_lock);
1905 if (mdev->state.conn < C_CONNECTED)
1906 retcode = _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_VERBOSE, NULL);
1907 spin_unlock_irq(&mdev->req_lock);
1908
1909 if (retcode != SS_NEED_CONNECTION)
1910 break;
1911
1912 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
1913 }
1914
1915 reply->ret_code = retcode;
1916 return 0;
1917}
1918
Philipp Reisner07782862010-08-31 12:00:50 +02001919static int drbd_bmio_set_susp_al(struct drbd_conf *mdev)
1920{
1921 int rv;
1922
1923 rv = drbd_bmio_set_n_write(mdev);
1924 drbd_suspend_al(mdev);
1925 return rv;
1926}
1927
Philipp Reisnerb411b362009-09-25 16:07:19 -07001928static int drbd_nl_invalidate_peer(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1929 struct drbd_nl_cfg_reply *reply)
1930{
Philipp Reisner07782862010-08-31 12:00:50 +02001931 int retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001932
Lars Ellenberg194bfb32011-01-18 10:38:01 +01001933 /* If there is still bitmap IO pending, probably because of a previous
1934 * resync just being finished, wait for it before requesting a new resync. */
1935 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
1936
Philipp Reisner07782862010-08-31 12:00:50 +02001937 retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S), CS_ORDERED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001938
Philipp Reisner07782862010-08-31 12:00:50 +02001939 if (retcode < SS_SUCCESS) {
1940 if (retcode == SS_NEED_CONNECTION && mdev->state.role == R_PRIMARY) {
1941 /* The peer will get a resync upon connect anyways. Just make that
1942 into a full resync. */
1943 retcode = drbd_request_state(mdev, NS(pdsk, D_INCONSISTENT));
1944 if (retcode >= SS_SUCCESS) {
Philipp Reisner07782862010-08-31 12:00:50 +02001945 if (drbd_bitmap_io(mdev, &drbd_bmio_set_susp_al,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001946 "set_n_write from invalidate_peer",
1947 BM_LOCKED_SET_ALLOWED))
Philipp Reisner07782862010-08-31 12:00:50 +02001948 retcode = ERR_IO_MD_DISK;
1949 }
1950 } else
1951 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S));
1952 }
1953
1954 reply->ret_code = retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001955 return 0;
1956}
1957
1958static int drbd_nl_pause_sync(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1959 struct drbd_nl_cfg_reply *reply)
1960{
1961 int retcode = NO_ERROR;
1962
1963 if (drbd_request_state(mdev, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
1964 retcode = ERR_PAUSE_IS_SET;
1965
1966 reply->ret_code = retcode;
1967 return 0;
1968}
1969
1970static int drbd_nl_resume_sync(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1971 struct drbd_nl_cfg_reply *reply)
1972{
1973 int retcode = NO_ERROR;
Philipp Reisnercd88d032011-01-20 11:46:41 +01001974 union drbd_state s;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001975
Philipp Reisnercd88d032011-01-20 11:46:41 +01001976 if (drbd_request_state(mdev, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
1977 s = mdev->state;
1978 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
1979 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
1980 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
1981 } else {
1982 retcode = ERR_PAUSE_IS_CLEAR;
1983 }
1984 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001985
1986 reply->ret_code = retcode;
1987 return 0;
1988}
1989
1990static int drbd_nl_suspend_io(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1991 struct drbd_nl_cfg_reply *reply)
1992{
1993 reply->ret_code = drbd_request_state(mdev, NS(susp, 1));
1994
1995 return 0;
1996}
1997
1998static int drbd_nl_resume_io(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1999 struct drbd_nl_cfg_reply *reply)
2000{
Philipp Reisner43a51822010-06-11 11:26:34 +02002001 if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
2002 drbd_uuid_new_current(mdev);
2003 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner43a51822010-06-11 11:26:34 +02002004 }
Philipp Reisner265be2d2010-05-31 10:14:17 +02002005 drbd_suspend_io(mdev);
Philipp Reisnerfb22c402010-09-08 23:20:21 +02002006 reply->ret_code = drbd_request_state(mdev, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
Philipp Reisner265be2d2010-05-31 10:14:17 +02002007 if (reply->ret_code == SS_SUCCESS) {
2008 if (mdev->state.conn < C_CONNECTED)
2009 tl_clear(mdev);
2010 if (mdev->state.disk == D_DISKLESS || mdev->state.disk == D_FAILED)
2011 tl_restart(mdev, fail_frozen_disk_io);
2012 }
2013 drbd_resume_io(mdev);
2014
Philipp Reisnerb411b362009-09-25 16:07:19 -07002015 return 0;
2016}
2017
2018static int drbd_nl_outdate(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2019 struct drbd_nl_cfg_reply *reply)
2020{
2021 reply->ret_code = drbd_request_state(mdev, NS(disk, D_OUTDATED));
2022 return 0;
2023}
2024
2025static int drbd_nl_get_config(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2026 struct drbd_nl_cfg_reply *reply)
2027{
2028 unsigned short *tl;
2029
2030 tl = reply->tag_list;
2031
2032 if (get_ldev(mdev)) {
2033 tl = disk_conf_to_tags(mdev, &mdev->ldev->dc, tl);
2034 put_ldev(mdev);
2035 }
2036
2037 if (get_net_conf(mdev)) {
2038 tl = net_conf_to_tags(mdev, mdev->net_conf, tl);
2039 put_net_conf(mdev);
2040 }
2041 tl = syncer_conf_to_tags(mdev, &mdev->sync_conf, tl);
2042
2043 put_unaligned(TT_END, tl++); /* Close the tag list */
2044
2045 return (int)((char *)tl - (char *)reply->tag_list);
2046}
2047
2048static int drbd_nl_get_state(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2049 struct drbd_nl_cfg_reply *reply)
2050{
2051 unsigned short *tl = reply->tag_list;
2052 union drbd_state s = mdev->state;
2053 unsigned long rs_left;
2054 unsigned int res;
2055
2056 tl = get_state_to_tags(mdev, (struct get_state *)&s, tl);
2057
2058 /* no local ref, no bitmap, no syncer progress. */
2059 if (s.conn >= C_SYNC_SOURCE && s.conn <= C_PAUSED_SYNC_T) {
2060 if (get_ldev(mdev)) {
2061 drbd_get_syncer_progress(mdev, &rs_left, &res);
2062 tl = tl_add_int(tl, T_sync_progress, &res);
2063 put_ldev(mdev);
2064 }
2065 }
2066 put_unaligned(TT_END, tl++); /* Close the tag list */
2067
2068 return (int)((char *)tl - (char *)reply->tag_list);
2069}
2070
2071static int drbd_nl_get_uuids(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2072 struct drbd_nl_cfg_reply *reply)
2073{
2074 unsigned short *tl;
2075
2076 tl = reply->tag_list;
2077
2078 if (get_ldev(mdev)) {
2079 tl = tl_add_blob(tl, T_uuids, mdev->ldev->md.uuid, UI_SIZE*sizeof(u64));
2080 tl = tl_add_int(tl, T_uuids_flags, &mdev->ldev->md.flags);
2081 put_ldev(mdev);
2082 }
2083 put_unaligned(TT_END, tl++); /* Close the tag list */
2084
2085 return (int)((char *)tl - (char *)reply->tag_list);
2086}
2087
2088/**
2089 * drbd_nl_get_timeout_flag() - Used by drbdsetup to find out which timeout value to use
2090 * @mdev: DRBD device.
2091 * @nlp: Netlink/connector packet from drbdsetup
2092 * @reply: Reply packet for drbdsetup
2093 */
2094static int drbd_nl_get_timeout_flag(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2095 struct drbd_nl_cfg_reply *reply)
2096{
2097 unsigned short *tl;
2098 char rv;
2099
2100 tl = reply->tag_list;
2101
2102 rv = mdev->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
2103 test_bit(USE_DEGR_WFC_T, &mdev->flags) ? UT_DEGRADED : UT_DEFAULT;
2104
2105 tl = tl_add_blob(tl, T_use_degraded, &rv, sizeof(rv));
2106 put_unaligned(TT_END, tl++); /* Close the tag list */
2107
2108 return (int)((char *)tl - (char *)reply->tag_list);
2109}
2110
2111static int drbd_nl_start_ov(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2112 struct drbd_nl_cfg_reply *reply)
2113{
2114 /* default to resume from last known position, if possible */
2115 struct start_ov args =
2116 { .start_sector = mdev->ov_start_sector };
2117
2118 if (!start_ov_from_tags(mdev, nlp->tag_list, &args)) {
2119 reply->ret_code = ERR_MANDATORY_TAG;
2120 return 0;
2121 }
Lars Ellenberg873b0d52011-01-21 22:53:48 +01002122
2123 /* If there is still bitmap IO pending, e.g. previous resync or verify
2124 * just being finished, wait for it before requesting a new resync. */
2125 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
2126
Philipp Reisnerb411b362009-09-25 16:07:19 -07002127 /* w_make_ov_request expects position to be aligned */
2128 mdev->ov_start_sector = args.start_sector & ~BM_SECT_PER_BIT;
2129 reply->ret_code = drbd_request_state(mdev,NS(conn,C_VERIFY_S));
2130 return 0;
2131}
2132
2133
2134static int drbd_nl_new_c_uuid(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2135 struct drbd_nl_cfg_reply *reply)
2136{
2137 int retcode = NO_ERROR;
2138 int skip_initial_sync = 0;
2139 int err;
2140
2141 struct new_c_uuid args;
2142
2143 memset(&args, 0, sizeof(struct new_c_uuid));
2144 if (!new_c_uuid_from_tags(mdev, nlp->tag_list, &args)) {
2145 reply->ret_code = ERR_MANDATORY_TAG;
2146 return 0;
2147 }
2148
2149 mutex_lock(&mdev->state_mutex); /* Protects us against serialized state changes. */
2150
2151 if (!get_ldev(mdev)) {
2152 retcode = ERR_NO_DISK;
2153 goto out;
2154 }
2155
2156 /* this is "skip initial sync", assume to be clean */
2157 if (mdev->state.conn == C_CONNECTED && mdev->agreed_pro_version >= 90 &&
2158 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
2159 dev_info(DEV, "Preparing to skip initial sync\n");
2160 skip_initial_sync = 1;
2161 } else if (mdev->state.conn != C_STANDALONE) {
2162 retcode = ERR_CONNECTED;
2163 goto out_dec;
2164 }
2165
2166 drbd_uuid_set(mdev, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
2167 drbd_uuid_new_current(mdev); /* New current, previous to UI_BITMAP */
2168
2169 if (args.clear_bm) {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002170 err = drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
2171 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002172 if (err) {
2173 dev_err(DEV, "Writing bitmap failed with %d\n",err);
2174 retcode = ERR_IO_MD_DISK;
2175 }
2176 if (skip_initial_sync) {
2177 drbd_send_uuids_skip_initial_sync(mdev);
2178 _drbd_uuid_set(mdev, UI_BITMAP, 0);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002179 drbd_print_uuids(mdev, "cleared bitmap UUID");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002180 spin_lock_irq(&mdev->req_lock);
2181 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
2182 CS_VERBOSE, NULL);
2183 spin_unlock_irq(&mdev->req_lock);
2184 }
2185 }
2186
2187 drbd_md_sync(mdev);
2188out_dec:
2189 put_ldev(mdev);
2190out:
2191 mutex_unlock(&mdev->state_mutex);
2192
2193 reply->ret_code = retcode;
2194 return 0;
2195}
2196
Philipp Reisnerb411b362009-09-25 16:07:19 -07002197struct cn_handler_struct {
2198 int (*function)(struct drbd_conf *,
2199 struct drbd_nl_cfg_req *,
2200 struct drbd_nl_cfg_reply *);
2201 int reply_body_size;
2202};
2203
2204static struct cn_handler_struct cnd_table[] = {
2205 [ P_primary ] = { &drbd_nl_primary, 0 },
2206 [ P_secondary ] = { &drbd_nl_secondary, 0 },
2207 [ P_disk_conf ] = { &drbd_nl_disk_conf, 0 },
2208 [ P_detach ] = { &drbd_nl_detach, 0 },
2209 [ P_net_conf ] = { &drbd_nl_net_conf, 0 },
2210 [ P_disconnect ] = { &drbd_nl_disconnect, 0 },
2211 [ P_resize ] = { &drbd_nl_resize, 0 },
2212 [ P_syncer_conf ] = { &drbd_nl_syncer_conf, 0 },
2213 [ P_invalidate ] = { &drbd_nl_invalidate, 0 },
2214 [ P_invalidate_peer ] = { &drbd_nl_invalidate_peer, 0 },
2215 [ P_pause_sync ] = { &drbd_nl_pause_sync, 0 },
2216 [ P_resume_sync ] = { &drbd_nl_resume_sync, 0 },
2217 [ P_suspend_io ] = { &drbd_nl_suspend_io, 0 },
2218 [ P_resume_io ] = { &drbd_nl_resume_io, 0 },
2219 [ P_outdate ] = { &drbd_nl_outdate, 0 },
2220 [ P_get_config ] = { &drbd_nl_get_config,
2221 sizeof(struct syncer_conf_tag_len_struct) +
2222 sizeof(struct disk_conf_tag_len_struct) +
2223 sizeof(struct net_conf_tag_len_struct) },
2224 [ P_get_state ] = { &drbd_nl_get_state,
2225 sizeof(struct get_state_tag_len_struct) +
2226 sizeof(struct sync_progress_tag_len_struct) },
2227 [ P_get_uuids ] = { &drbd_nl_get_uuids,
2228 sizeof(struct get_uuids_tag_len_struct) },
2229 [ P_get_timeout_flag ] = { &drbd_nl_get_timeout_flag,
2230 sizeof(struct get_timeout_flag_tag_len_struct)},
2231 [ P_start_ov ] = { &drbd_nl_start_ov, 0 },
2232 [ P_new_c_uuid ] = { &drbd_nl_new_c_uuid, 0 },
2233};
2234
Philipp Reisner9f5180e2009-10-06 09:30:14 +02002235static void drbd_connector_callback(struct cn_msg *req, struct netlink_skb_parms *nsp)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002236{
2237 struct drbd_nl_cfg_req *nlp = (struct drbd_nl_cfg_req *)req->data;
2238 struct cn_handler_struct *cm;
2239 struct cn_msg *cn_reply;
2240 struct drbd_nl_cfg_reply *reply;
2241 struct drbd_conf *mdev;
2242 int retcode, rr;
2243 int reply_size = sizeof(struct cn_msg)
2244 + sizeof(struct drbd_nl_cfg_reply)
2245 + sizeof(short int);
2246
2247 if (!try_module_get(THIS_MODULE)) {
2248 printk(KERN_ERR "drbd: try_module_get() failed!\n");
2249 return;
2250 }
2251
Patrick McHardy01a16b22011-03-03 13:32:07 -08002252 if (!cap_raised(current_cap(), CAP_SYS_ADMIN)) {
Philipp Reisner9f5180e2009-10-06 09:30:14 +02002253 retcode = ERR_PERM;
2254 goto fail;
2255 }
2256
Lars Ellenbergef50a3e2010-09-01 14:39:30 +02002257 mdev = ensure_mdev(nlp->drbd_minor,
2258 (nlp->flags & DRBD_NL_CREATE_DEVICE));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002259 if (!mdev) {
2260 retcode = ERR_MINOR_INVALID;
2261 goto fail;
2262 }
2263
Lars Ellenberg42ff2692010-11-24 10:11:14 +01002264 if (nlp->packet_type >= P_nl_after_last_packet ||
2265 nlp->packet_type == P_return_code_only) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002266 retcode = ERR_PACKET_NR;
2267 goto fail;
2268 }
2269
2270 cm = cnd_table + nlp->packet_type;
2271
2272 /* This may happen if packet number is 0: */
2273 if (cm->function == NULL) {
2274 retcode = ERR_PACKET_NR;
2275 goto fail;
2276 }
2277
2278 reply_size += cm->reply_body_size;
2279
2280 /* allocation not in the IO path, cqueue thread context */
Lars Ellenberg3e3a7762010-11-24 10:41:45 +01002281 cn_reply = kzalloc(reply_size, GFP_KERNEL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002282 if (!cn_reply) {
2283 retcode = ERR_NOMEM;
2284 goto fail;
2285 }
2286 reply = (struct drbd_nl_cfg_reply *) cn_reply->data;
2287
2288 reply->packet_type =
Lars Ellenberg42ff2692010-11-24 10:11:14 +01002289 cm->reply_body_size ? nlp->packet_type : P_return_code_only;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002290 reply->minor = nlp->drbd_minor;
2291 reply->ret_code = NO_ERROR; /* Might by modified by cm->function. */
2292 /* reply->tag_list; might be modified by cm->function. */
2293
2294 rr = cm->function(mdev, nlp, reply);
2295
2296 cn_reply->id = req->id;
2297 cn_reply->seq = req->seq;
2298 cn_reply->ack = req->ack + 1;
2299 cn_reply->len = sizeof(struct drbd_nl_cfg_reply) + rr;
2300 cn_reply->flags = 0;
2301
Philipp Reisnerb411b362009-09-25 16:07:19 -07002302 rr = cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_KERNEL);
2303 if (rr && rr != -ESRCH)
2304 printk(KERN_INFO "drbd: cn_netlink_send()=%d\n", rr);
2305
2306 kfree(cn_reply);
2307 module_put(THIS_MODULE);
2308 return;
2309 fail:
2310 drbd_nl_send_reply(req, retcode);
2311 module_put(THIS_MODULE);
2312}
2313
2314static atomic_t drbd_nl_seq = ATOMIC_INIT(2); /* two. */
2315
2316static unsigned short *
2317__tl_add_blob(unsigned short *tl, enum drbd_tags tag, const void *data,
2318 unsigned short len, int nul_terminated)
2319{
2320 unsigned short l = tag_descriptions[tag_number(tag)].max_len;
2321 len = (len < l) ? len : l;
2322 put_unaligned(tag, tl++);
2323 put_unaligned(len, tl++);
2324 memcpy(tl, data, len);
2325 tl = (unsigned short*)((char*)tl + len);
2326 if (nul_terminated)
2327 *((char*)tl - 1) = 0;
2328 return tl;
2329}
2330
2331static unsigned short *
2332tl_add_blob(unsigned short *tl, enum drbd_tags tag, const void *data, int len)
2333{
2334 return __tl_add_blob(tl, tag, data, len, 0);
2335}
2336
2337static unsigned short *
2338tl_add_str(unsigned short *tl, enum drbd_tags tag, const char *str)
2339{
2340 return __tl_add_blob(tl, tag, str, strlen(str)+1, 0);
2341}
2342
2343static unsigned short *
2344tl_add_int(unsigned short *tl, enum drbd_tags tag, const void *val)
2345{
2346 put_unaligned(tag, tl++);
2347 switch(tag_type(tag)) {
2348 case TT_INTEGER:
2349 put_unaligned(sizeof(int), tl++);
2350 put_unaligned(*(int *)val, (int *)tl);
2351 tl = (unsigned short*)((char*)tl+sizeof(int));
2352 break;
2353 case TT_INT64:
2354 put_unaligned(sizeof(u64), tl++);
2355 put_unaligned(*(u64 *)val, (u64 *)tl);
2356 tl = (unsigned short*)((char*)tl+sizeof(u64));
2357 break;
2358 default:
2359 /* someone did something stupid. */
2360 ;
2361 }
2362 return tl;
2363}
2364
2365void drbd_bcast_state(struct drbd_conf *mdev, union drbd_state state)
2366{
2367 char buffer[sizeof(struct cn_msg)+
2368 sizeof(struct drbd_nl_cfg_reply)+
2369 sizeof(struct get_state_tag_len_struct)+
2370 sizeof(short int)];
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 unsigned short *tl = reply->tag_list;
2375
2376 /* dev_warn(DEV, "drbd_bcast_state() got called\n"); */
2377
2378 tl = get_state_to_tags(mdev, (struct get_state *)&state, tl);
2379
2380 put_unaligned(TT_END, tl++); /* Close the tag list */
2381
2382 cn_reply->id.idx = CN_IDX_DRBD;
2383 cn_reply->id.val = CN_VAL_DRBD;
2384
2385 cn_reply->seq = atomic_add_return(1, &drbd_nl_seq);
2386 cn_reply->ack = 0; /* not used here. */
2387 cn_reply->len = sizeof(struct drbd_nl_cfg_reply) +
2388 (int)((char *)tl - (char *)reply->tag_list);
2389 cn_reply->flags = 0;
2390
2391 reply->packet_type = P_get_state;
2392 reply->minor = mdev_to_minor(mdev);
2393 reply->ret_code = NO_ERROR;
2394
Philipp Reisnerb411b362009-09-25 16:07:19 -07002395 cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO);
2396}
2397
2398void drbd_bcast_ev_helper(struct drbd_conf *mdev, char *helper_name)
2399{
2400 char buffer[sizeof(struct cn_msg)+
2401 sizeof(struct drbd_nl_cfg_reply)+
2402 sizeof(struct call_helper_tag_len_struct)+
2403 sizeof(short int)];
2404 struct cn_msg *cn_reply = (struct cn_msg *) buffer;
2405 struct drbd_nl_cfg_reply *reply =
2406 (struct drbd_nl_cfg_reply *)cn_reply->data;
2407 unsigned short *tl = reply->tag_list;
2408
2409 /* dev_warn(DEV, "drbd_bcast_state() got called\n"); */
2410
2411 tl = tl_add_str(tl, T_helper, helper_name);
2412 put_unaligned(TT_END, tl++); /* Close the tag list */
2413
2414 cn_reply->id.idx = CN_IDX_DRBD;
2415 cn_reply->id.val = CN_VAL_DRBD;
2416
2417 cn_reply->seq = atomic_add_return(1, &drbd_nl_seq);
2418 cn_reply->ack = 0; /* not used here. */
2419 cn_reply->len = sizeof(struct drbd_nl_cfg_reply) +
2420 (int)((char *)tl - (char *)reply->tag_list);
2421 cn_reply->flags = 0;
2422
2423 reply->packet_type = P_call_helper;
2424 reply->minor = mdev_to_minor(mdev);
2425 reply->ret_code = NO_ERROR;
2426
Philipp Reisnerb411b362009-09-25 16:07:19 -07002427 cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO);
2428}
2429
2430void drbd_bcast_ee(struct drbd_conf *mdev,
2431 const char *reason, const int dgs,
2432 const char* seen_hash, const char* calc_hash,
2433 const struct drbd_epoch_entry* e)
2434{
2435 struct cn_msg *cn_reply;
2436 struct drbd_nl_cfg_reply *reply;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002437 unsigned short *tl;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002438 struct page *page;
2439 unsigned len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002440
2441 if (!e)
2442 return;
2443 if (!reason || !reason[0])
2444 return;
2445
2446 /* apparently we have to memcpy twice, first to prepare the data for the
2447 * struct cn_msg, then within cn_netlink_send from the cn_msg to the
2448 * netlink skb. */
2449 /* receiver thread context, which is not in the writeout path (of this node),
2450 * but may be in the writeout path of the _other_ node.
2451 * GFP_NOIO to avoid potential "distributed deadlock". */
Lars Ellenberg3e3a7762010-11-24 10:41:45 +01002452 cn_reply = kzalloc(
Philipp Reisnerb411b362009-09-25 16:07:19 -07002453 sizeof(struct cn_msg)+
2454 sizeof(struct drbd_nl_cfg_reply)+
2455 sizeof(struct dump_ee_tag_len_struct)+
2456 sizeof(short int),
2457 GFP_NOIO);
2458
2459 if (!cn_reply) {
2460 dev_err(DEV, "could not kmalloc buffer for drbd_bcast_ee, sector %llu, size %u\n",
2461 (unsigned long long)e->sector, e->size);
2462 return;
2463 }
2464
2465 reply = (struct drbd_nl_cfg_reply*)cn_reply->data;
2466 tl = reply->tag_list;
2467
2468 tl = tl_add_str(tl, T_dump_ee_reason, reason);
2469 tl = tl_add_blob(tl, T_seen_digest, seen_hash, dgs);
2470 tl = tl_add_blob(tl, T_calc_digest, calc_hash, dgs);
2471 tl = tl_add_int(tl, T_ee_sector, &e->sector);
2472 tl = tl_add_int(tl, T_ee_block_id, &e->block_id);
2473
Lars Ellenberg3129b1b2010-11-11 10:47:05 +01002474 /* dump the first 32k */
2475 len = min_t(unsigned, e->size, 32 << 10);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002476 put_unaligned(T_ee_data, tl++);
Lars Ellenberg3129b1b2010-11-11 10:47:05 +01002477 put_unaligned(len, tl++);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002478
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002479 page = e->pages;
2480 page_chain_for_each(page) {
2481 void *d = kmap_atomic(page, KM_USER0);
2482 unsigned l = min_t(unsigned, len, PAGE_SIZE);
2483 memcpy(tl, d, l);
2484 kunmap_atomic(d, KM_USER0);
2485 tl = (unsigned short*)((char*)tl + l);
2486 len -= l;
Lars Ellenberg3129b1b2010-11-11 10:47:05 +01002487 if (len == 0)
2488 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002489 }
2490 put_unaligned(TT_END, tl++); /* Close the tag list */
2491
2492 cn_reply->id.idx = CN_IDX_DRBD;
2493 cn_reply->id.val = CN_VAL_DRBD;
2494
2495 cn_reply->seq = atomic_add_return(1,&drbd_nl_seq);
2496 cn_reply->ack = 0; // not used here.
2497 cn_reply->len = sizeof(struct drbd_nl_cfg_reply) +
2498 (int)((char*)tl - (char*)reply->tag_list);
2499 cn_reply->flags = 0;
2500
2501 reply->packet_type = P_dump_ee;
2502 reply->minor = mdev_to_minor(mdev);
2503 reply->ret_code = NO_ERROR;
2504
Philipp Reisnerb411b362009-09-25 16:07:19 -07002505 cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO);
2506 kfree(cn_reply);
2507}
2508
2509void drbd_bcast_sync_progress(struct drbd_conf *mdev)
2510{
2511 char buffer[sizeof(struct cn_msg)+
2512 sizeof(struct drbd_nl_cfg_reply)+
2513 sizeof(struct sync_progress_tag_len_struct)+
2514 sizeof(short int)];
2515 struct cn_msg *cn_reply = (struct cn_msg *) buffer;
2516 struct drbd_nl_cfg_reply *reply =
2517 (struct drbd_nl_cfg_reply *)cn_reply->data;
2518 unsigned short *tl = reply->tag_list;
2519 unsigned long rs_left;
2520 unsigned int res;
2521
2522 /* no local ref, no bitmap, no syncer progress, no broadcast. */
2523 if (!get_ldev(mdev))
2524 return;
2525 drbd_get_syncer_progress(mdev, &rs_left, &res);
2526 put_ldev(mdev);
2527
2528 tl = tl_add_int(tl, T_sync_progress, &res);
2529 put_unaligned(TT_END, tl++); /* Close the tag list */
2530
2531 cn_reply->id.idx = CN_IDX_DRBD;
2532 cn_reply->id.val = CN_VAL_DRBD;
2533
2534 cn_reply->seq = atomic_add_return(1, &drbd_nl_seq);
2535 cn_reply->ack = 0; /* not used here. */
2536 cn_reply->len = sizeof(struct drbd_nl_cfg_reply) +
2537 (int)((char *)tl - (char *)reply->tag_list);
2538 cn_reply->flags = 0;
2539
2540 reply->packet_type = P_sync_progress;
2541 reply->minor = mdev_to_minor(mdev);
2542 reply->ret_code = NO_ERROR;
2543
Philipp Reisnerb411b362009-09-25 16:07:19 -07002544 cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO);
2545}
2546
2547int __init drbd_nl_init(void)
2548{
2549 static struct cb_id cn_id_drbd;
2550 int err, try=10;
2551
2552 cn_id_drbd.val = CN_VAL_DRBD;
2553 do {
2554 cn_id_drbd.idx = cn_idx;
2555 err = cn_add_callback(&cn_id_drbd, "cn_drbd", &drbd_connector_callback);
2556 if (!err)
2557 break;
2558 cn_idx = (cn_idx + CN_IDX_STEP);
2559 } while (try--);
2560
2561 if (err) {
2562 printk(KERN_ERR "drbd: cn_drbd failed to register\n");
2563 return err;
2564 }
2565
2566 return 0;
2567}
2568
2569void drbd_nl_cleanup(void)
2570{
2571 static struct cb_id cn_id_drbd;
2572
2573 cn_id_drbd.idx = cn_idx;
2574 cn_id_drbd.val = CN_VAL_DRBD;
2575
2576 cn_del_callback(&cn_id_drbd);
2577}
2578
2579void drbd_nl_send_reply(struct cn_msg *req, int ret_code)
2580{
2581 char buffer[sizeof(struct cn_msg)+sizeof(struct drbd_nl_cfg_reply)];
2582 struct cn_msg *cn_reply = (struct cn_msg *) buffer;
2583 struct drbd_nl_cfg_reply *reply =
2584 (struct drbd_nl_cfg_reply *)cn_reply->data;
2585 int rr;
2586
Lars Ellenberg3e3a7762010-11-24 10:41:45 +01002587 memset(buffer, 0, sizeof(buffer));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002588 cn_reply->id = req->id;
2589
2590 cn_reply->seq = req->seq;
2591 cn_reply->ack = req->ack + 1;
2592 cn_reply->len = sizeof(struct drbd_nl_cfg_reply);
2593 cn_reply->flags = 0;
2594
Lars Ellenberg42ff2692010-11-24 10:11:14 +01002595 reply->packet_type = P_return_code_only;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002596 reply->minor = ((struct drbd_nl_cfg_req *)req->data)->drbd_minor;
2597 reply->ret_code = ret_code;
2598
Philipp Reisnerb411b362009-09-25 16:07:19 -07002599 rr = cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO);
2600 if (rr && rr != -ESRCH)
2601 printk(KERN_INFO "drbd: cn_netlink_send()=%d\n", rr);
2602}
2603