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