]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/geom/mirror/g_mirror.h
MFC r235599:
[FreeBSD/stable/9.git] / sys / geom / mirror / g_mirror.h
1 /*-
2  * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #ifndef _G_MIRROR_H_
30 #define _G_MIRROR_H_
31
32 #include <sys/endian.h>
33 #include <sys/md5.h>
34
35 #define G_MIRROR_CLASS_NAME     "MIRROR"
36
37 #define G_MIRROR_MAGIC          "GEOM::MIRROR"
38 /*
39  * Version history:
40  * 0 - Initial version number.
41  * 1 - Added 'prefer' balance algorithm.
42  * 2 - Added md_genid field to metadata.
43  * 3 - Added md_provsize field to metadata.
44  * 4 - Added 'no failure synchronization' flag.
45  */
46 #define G_MIRROR_VERSION        4
47
48 #define G_MIRROR_BALANCE_NONE           0
49 #define G_MIRROR_BALANCE_ROUND_ROBIN    1
50 #define G_MIRROR_BALANCE_LOAD           2
51 #define G_MIRROR_BALANCE_SPLIT          3
52 #define G_MIRROR_BALANCE_PREFER         4
53 #define G_MIRROR_BALANCE_MIN            G_MIRROR_BALANCE_NONE
54 #define G_MIRROR_BALANCE_MAX            G_MIRROR_BALANCE_PREFER
55
56 #define G_MIRROR_DISK_FLAG_DIRTY                0x0000000000000001ULL
57 #define G_MIRROR_DISK_FLAG_SYNCHRONIZING        0x0000000000000002ULL
58 #define G_MIRROR_DISK_FLAG_FORCE_SYNC           0x0000000000000004ULL
59 #define G_MIRROR_DISK_FLAG_INACTIVE             0x0000000000000008ULL
60 #define G_MIRROR_DISK_FLAG_HARDCODED            0x0000000000000010ULL
61 #define G_MIRROR_DISK_FLAG_BROKEN               0x0000000000000020ULL
62 #define G_MIRROR_DISK_FLAG_MASK         (G_MIRROR_DISK_FLAG_DIRTY |     \
63                                          G_MIRROR_DISK_FLAG_SYNCHRONIZING | \
64                                          G_MIRROR_DISK_FLAG_FORCE_SYNC | \
65                                          G_MIRROR_DISK_FLAG_INACTIVE)
66
67 #define G_MIRROR_DEVICE_FLAG_NOAUTOSYNC 0x0000000000000001ULL
68 #define G_MIRROR_DEVICE_FLAG_NOFAILSYNC 0x0000000000000002ULL
69 #define G_MIRROR_DEVICE_FLAG_MASK       (G_MIRROR_DEVICE_FLAG_NOAUTOSYNC | \
70                                          G_MIRROR_DEVICE_FLAG_NOFAILSYNC)
71
72 #ifdef _KERNEL
73 extern u_int g_mirror_debug;
74
75 #define G_MIRROR_DEBUG(lvl, ...)        do {                            \
76         if (g_mirror_debug >= (lvl)) {                                  \
77                 printf("GEOM_MIRROR");                                  \
78                 if (g_mirror_debug > 0)                                 \
79                         printf("[%u]", lvl);                            \
80                 printf(": ");                                           \
81                 printf(__VA_ARGS__);                                    \
82                 printf("\n");                                           \
83         }                                                               \
84 } while (0)
85 #define G_MIRROR_LOGREQ(lvl, bp, ...)   do {                            \
86         if (g_mirror_debug >= (lvl)) {                                  \
87                 printf("GEOM_MIRROR");                                  \
88                 if (g_mirror_debug > 0)                                 \
89                         printf("[%u]", lvl);                            \
90                 printf(": ");                                           \
91                 printf(__VA_ARGS__);                                    \
92                 printf(" ");                                            \
93                 g_print_bio(bp);                                        \
94                 printf("\n");                                           \
95         }                                                               \
96 } while (0)
97
98 #define G_MIRROR_BIO_FLAG_REGULAR       0x01
99 #define G_MIRROR_BIO_FLAG_SYNC          0x02
100
101 /*
102  * Informations needed for synchronization.
103  */
104 struct g_mirror_disk_sync {
105         struct g_consumer *ds_consumer; /* Consumer connected to our mirror. */
106         off_t             ds_offset;    /* Offset of next request to send. */
107         off_t             ds_offset_done; /* Offset of already synchronized
108                                            region. */
109         u_int             ds_syncid;    /* Disk's synchronization ID. */
110         u_int             ds_inflight;  /* Number of in-flight sync requests. */
111         struct bio      **ds_bios;      /* BIOs for synchronization I/O. */
112 };
113
114 /*
115  * Informations needed for synchronization.
116  */
117 struct g_mirror_device_sync {
118         struct g_geom   *ds_geom;       /* Synchronization geom. */
119         u_int            ds_ndisks;     /* Number of disks in SYNCHRONIZING
120                                            state. */
121 };
122
123 #define G_MIRROR_DISK_STATE_NONE                0
124 #define G_MIRROR_DISK_STATE_NEW                 1
125 #define G_MIRROR_DISK_STATE_ACTIVE              2
126 #define G_MIRROR_DISK_STATE_STALE               3
127 #define G_MIRROR_DISK_STATE_SYNCHRONIZING       4
128 #define G_MIRROR_DISK_STATE_DISCONNECTED        5
129 #define G_MIRROR_DISK_STATE_DESTROY             6
130 struct g_mirror_disk {
131         uint32_t         d_id;          /* Disk ID. */
132         struct g_consumer *d_consumer;  /* Consumer. */
133         struct g_mirror_softc   *d_softc; /* Back-pointer to softc. */
134         int              d_state;       /* Disk state. */
135         u_int            d_priority;    /* Disk priority. */
136         u_int            load;          /* Averaged queue length */
137         off_t            d_last_offset; /* Last read offset */
138         uint64_t         d_flags;       /* Additional flags. */
139         u_int            d_genid;       /* Disk's generation ID. */
140         struct g_mirror_disk_sync d_sync;/* Sync information. */
141         LIST_ENTRY(g_mirror_disk) d_next;
142 };
143 #define d_name  d_consumer->provider->name
144
145 #define G_MIRROR_EVENT_DONTWAIT 0x1
146 #define G_MIRROR_EVENT_WAIT     0x2
147 #define G_MIRROR_EVENT_DEVICE   0x4
148 #define G_MIRROR_EVENT_DONE     0x8
149 struct g_mirror_event {
150         struct g_mirror_disk    *e_disk;
151         int                      e_state;
152         int                      e_flags;
153         int                      e_error;
154         TAILQ_ENTRY(g_mirror_event) e_next;
155 };
156
157 #define G_MIRROR_DEVICE_FLAG_DESTROY    0x0100000000000000ULL
158 #define G_MIRROR_DEVICE_FLAG_WAIT       0x0200000000000000ULL
159 #define G_MIRROR_DEVICE_FLAG_DESTROYING 0x0400000000000000ULL
160 #define G_MIRROR_DEVICE_FLAG_TASTING    0x0800000000000000ULL
161
162 #define G_MIRROR_DEVICE_STATE_STARTING          0
163 #define G_MIRROR_DEVICE_STATE_RUNNING           1
164
165 /* Bump syncid on first write. */
166 #define G_MIRROR_BUMP_SYNCID    0x1
167 /* Bump genid immediately. */
168 #define G_MIRROR_BUMP_GENID     0x2
169 struct g_mirror_softc {
170         u_int           sc_state;       /* Device state. */
171         uint32_t        sc_slice;       /* Slice size. */
172         uint8_t         sc_balance;     /* Balance algorithm. */
173         uint64_t        sc_mediasize;   /* Device size. */
174         uint32_t        sc_sectorsize;  /* Sector size. */
175         uint64_t        sc_flags;       /* Additional flags. */
176
177         struct g_geom   *sc_geom;
178         struct g_provider *sc_provider;
179
180         uint32_t        sc_id;          /* Mirror unique ID. */
181
182         struct sx        sc_lock;
183         struct bio_queue_head sc_queue;
184         struct mtx       sc_queue_mtx;
185         struct proc     *sc_worker;
186         struct bio_queue_head sc_regular_delayed; /* Delayed I/O requests due
187                                                      collision with sync
188                                                      requests. */
189         struct bio_queue_head sc_inflight; /* In-flight regular write
190                                               requests. */
191         struct bio_queue_head sc_sync_delayed; /* Delayed sync requests due
192                                                   collision with regular
193                                                   requests. */
194
195         LIST_HEAD(, g_mirror_disk) sc_disks;
196         u_int           sc_ndisks;      /* Number of disks. */
197         struct g_mirror_disk *sc_hint;
198
199         u_int           sc_genid;       /* Generation ID. */
200         u_int           sc_syncid;      /* Synchronization ID. */
201         int             sc_bump_id;
202         struct g_mirror_device_sync sc_sync;
203         int             sc_idle;        /* DIRTY flags removed. */
204         time_t          sc_last_write;
205         u_int           sc_writes;
206
207         TAILQ_HEAD(, g_mirror_event) sc_events;
208         struct mtx      sc_events_mtx;
209
210         struct callout  sc_callout;
211
212         struct root_hold_token *sc_rootmount;
213 };
214 #define sc_name sc_geom->name
215
216 u_int g_mirror_ndisks(struct g_mirror_softc *sc, int state);
217 #define G_MIRROR_DESTROY_SOFT           0
218 #define G_MIRROR_DESTROY_DELAYED        1
219 #define G_MIRROR_DESTROY_HARD           2
220 int g_mirror_destroy(struct g_mirror_softc *sc, int how);
221 int g_mirror_event_send(void *arg, int state, int flags);
222 struct g_mirror_metadata;
223 int g_mirror_add_disk(struct g_mirror_softc *sc, struct g_provider *pp,
224     struct g_mirror_metadata *md);
225 int g_mirror_read_metadata(struct g_consumer *cp, struct g_mirror_metadata *md);
226 void g_mirror_fill_metadata(struct g_mirror_softc *sc,
227     struct g_mirror_disk *disk, struct g_mirror_metadata *md);
228 void g_mirror_update_metadata(struct g_mirror_disk *disk);
229
230 g_ctl_req_t g_mirror_config;
231 #endif  /* _KERNEL */
232
233 struct g_mirror_metadata {
234         char            md_magic[16];   /* Magic value. */
235         uint32_t        md_version;     /* Version number. */
236         char            md_name[16];    /* Mirror name. */
237         uint32_t        md_mid;         /* Mirror unique ID. */
238         uint32_t        md_did;         /* Disk unique ID. */
239         uint8_t         md_all;         /* Number of disks in mirror. */
240         uint32_t        md_genid;       /* Generation ID. */
241         uint32_t        md_syncid;      /* Synchronization ID. */
242         uint8_t         md_priority;    /* Disk priority. */
243         uint32_t        md_slice;       /* Slice size. */
244         uint8_t         md_balance;     /* Balance type. */
245         uint64_t        md_mediasize;   /* Size of the smallest
246                                            disk in mirror. */
247         uint32_t        md_sectorsize;  /* Sector size. */
248         uint64_t        md_sync_offset; /* Synchronized offset. */
249         uint64_t        md_mflags;      /* Additional mirror flags. */
250         uint64_t        md_dflags;      /* Additional disk flags. */
251         char            md_provider[16]; /* Hardcoded provider. */
252         uint64_t        md_provsize;    /* Provider's size. */
253         u_char          md_hash[16];    /* MD5 hash. */
254 };
255 static __inline void
256 mirror_metadata_encode(struct g_mirror_metadata *md, u_char *data)
257 {
258         MD5_CTX ctx;
259
260         bcopy(md->md_magic, data, 16);
261         le32enc(data + 16, md->md_version);
262         bcopy(md->md_name, data + 20, 16);
263         le32enc(data + 36, md->md_mid);
264         le32enc(data + 40, md->md_did);
265         *(data + 44) = md->md_all;
266         le32enc(data + 45, md->md_genid);
267         le32enc(data + 49, md->md_syncid);
268         *(data + 53) = md->md_priority;
269         le32enc(data + 54, md->md_slice);
270         *(data + 58) = md->md_balance;
271         le64enc(data + 59, md->md_mediasize);
272         le32enc(data + 67, md->md_sectorsize);
273         le64enc(data + 71, md->md_sync_offset);
274         le64enc(data + 79, md->md_mflags);
275         le64enc(data + 87, md->md_dflags);
276         bcopy(md->md_provider, data + 95, 16);
277         le64enc(data + 111, md->md_provsize);
278         MD5Init(&ctx);
279         MD5Update(&ctx, data, 119);
280         MD5Final(md->md_hash, &ctx);
281         bcopy(md->md_hash, data + 119, 16);
282 }
283 static __inline int
284 mirror_metadata_decode_v0v1(const u_char *data, struct g_mirror_metadata *md)
285 {
286         MD5_CTX ctx;
287
288         bcopy(data + 20, md->md_name, 16);
289         md->md_mid = le32dec(data + 36);
290         md->md_did = le32dec(data + 40);
291         md->md_all = *(data + 44);
292         md->md_syncid = le32dec(data + 45);
293         md->md_priority = *(data + 49);
294         md->md_slice = le32dec(data + 50);
295         md->md_balance = *(data + 54);
296         md->md_mediasize = le64dec(data + 55);
297         md->md_sectorsize = le32dec(data + 63);
298         md->md_sync_offset = le64dec(data + 67);
299         md->md_mflags = le64dec(data + 75);
300         md->md_dflags = le64dec(data + 83);
301         bcopy(data + 91, md->md_provider, 16);
302         bcopy(data + 107, md->md_hash, 16);
303         MD5Init(&ctx);
304         MD5Update(&ctx, data, 107);
305         MD5Final(md->md_hash, &ctx);
306         if (bcmp(md->md_hash, data + 107, 16) != 0)
307                 return (EINVAL);
308
309         /* New fields. */
310         md->md_genid = 0;
311         md->md_provsize = 0;
312
313         return (0);
314 }
315 static __inline int
316 mirror_metadata_decode_v2(const u_char *data, struct g_mirror_metadata *md)
317 {
318         MD5_CTX ctx;
319
320         bcopy(data + 20, md->md_name, 16);
321         md->md_mid = le32dec(data + 36);
322         md->md_did = le32dec(data + 40);
323         md->md_all = *(data + 44);
324         md->md_genid = le32dec(data + 45);
325         md->md_syncid = le32dec(data + 49);
326         md->md_priority = *(data + 53);
327         md->md_slice = le32dec(data + 54);
328         md->md_balance = *(data + 58);
329         md->md_mediasize = le64dec(data + 59);
330         md->md_sectorsize = le32dec(data + 67);
331         md->md_sync_offset = le64dec(data + 71);
332         md->md_mflags = le64dec(data + 79);
333         md->md_dflags = le64dec(data + 87);
334         bcopy(data + 95, md->md_provider, 16);
335         bcopy(data + 111, md->md_hash, 16);
336         MD5Init(&ctx);
337         MD5Update(&ctx, data, 111);
338         MD5Final(md->md_hash, &ctx);
339         if (bcmp(md->md_hash, data + 111, 16) != 0)
340                 return (EINVAL);
341
342         /* New fields. */
343         md->md_provsize = 0;
344
345         return (0);
346 }
347 static __inline int
348 mirror_metadata_decode_v3v4(const u_char *data, struct g_mirror_metadata *md)
349 {
350         MD5_CTX ctx;
351
352         bcopy(data + 20, md->md_name, 16);
353         md->md_mid = le32dec(data + 36);
354         md->md_did = le32dec(data + 40);
355         md->md_all = *(data + 44);
356         md->md_genid = le32dec(data + 45);
357         md->md_syncid = le32dec(data + 49);
358         md->md_priority = *(data + 53);
359         md->md_slice = le32dec(data + 54);
360         md->md_balance = *(data + 58);
361         md->md_mediasize = le64dec(data + 59);
362         md->md_sectorsize = le32dec(data + 67);
363         md->md_sync_offset = le64dec(data + 71);
364         md->md_mflags = le64dec(data + 79);
365         md->md_dflags = le64dec(data + 87);
366         bcopy(data + 95, md->md_provider, 16);
367         md->md_provsize = le64dec(data + 111);
368         bcopy(data + 119, md->md_hash, 16);
369         MD5Init(&ctx);
370         MD5Update(&ctx, data, 119);
371         MD5Final(md->md_hash, &ctx);
372         if (bcmp(md->md_hash, data + 119, 16) != 0)
373                 return (EINVAL);
374         return (0);
375 }
376 static __inline int
377 mirror_metadata_decode(const u_char *data, struct g_mirror_metadata *md)
378 {
379         int error;
380
381         bcopy(data, md->md_magic, 16);
382         md->md_version = le32dec(data + 16);
383         switch (md->md_version) {
384         case 0:
385         case 1:
386                 error = mirror_metadata_decode_v0v1(data, md);
387                 break;
388         case 2:
389                 error = mirror_metadata_decode_v2(data, md);
390                 break;
391         case 3:
392         case 4:
393                 error = mirror_metadata_decode_v3v4(data, md);
394                 break;
395         default:
396                 error = EINVAL;
397                 break;
398         }
399         return (error);
400 }
401
402 static __inline const char *
403 balance_name(u_int balance)
404 {
405         static const char *algorithms[] = {
406                 [G_MIRROR_BALANCE_NONE] = "none",
407                 [G_MIRROR_BALANCE_ROUND_ROBIN] = "round-robin",
408                 [G_MIRROR_BALANCE_LOAD] = "load",
409                 [G_MIRROR_BALANCE_SPLIT] = "split",
410                 [G_MIRROR_BALANCE_PREFER] = "prefer",
411                 [G_MIRROR_BALANCE_MAX + 1] = "unknown"
412         };
413
414         if (balance > G_MIRROR_BALANCE_MAX)
415                 balance = G_MIRROR_BALANCE_MAX + 1;
416
417         return (algorithms[balance]);
418 }
419
420 static __inline int
421 balance_id(const char *name)
422 {
423         static const char *algorithms[] = {
424                 [G_MIRROR_BALANCE_NONE] = "none",
425                 [G_MIRROR_BALANCE_ROUND_ROBIN] = "round-robin",
426                 [G_MIRROR_BALANCE_LOAD] = "load",
427                 [G_MIRROR_BALANCE_SPLIT] = "split",
428                 [G_MIRROR_BALANCE_PREFER] = "prefer"
429         };
430         int n;
431
432         for (n = G_MIRROR_BALANCE_MIN; n <= G_MIRROR_BALANCE_MAX; n++) {
433                 if (strcmp(name, algorithms[n]) == 0)
434                         return (n);
435         }
436         return (-1);
437 }
438
439 static __inline void
440 mirror_metadata_dump(const struct g_mirror_metadata *md)
441 {
442         static const char hex[] = "0123456789abcdef";
443         char hash[16 * 2 + 1];
444         u_int i;
445
446         printf("     magic: %s\n", md->md_magic);
447         printf("   version: %u\n", (u_int)md->md_version);
448         printf("      name: %s\n", md->md_name);
449         printf("       mid: %u\n", (u_int)md->md_mid);
450         printf("       did: %u\n", (u_int)md->md_did);
451         printf("       all: %u\n", (u_int)md->md_all);
452         printf("     genid: %u\n", (u_int)md->md_genid);
453         printf("    syncid: %u\n", (u_int)md->md_syncid);
454         printf("  priority: %u\n", (u_int)md->md_priority);
455         printf("     slice: %u\n", (u_int)md->md_slice);
456         printf("   balance: %s\n", balance_name((u_int)md->md_balance));
457         printf(" mediasize: %jd\n", (intmax_t)md->md_mediasize);
458         printf("sectorsize: %u\n", (u_int)md->md_sectorsize);
459         printf("syncoffset: %jd\n", (intmax_t)md->md_sync_offset);
460         printf("    mflags:");
461         if (md->md_mflags == 0)
462                 printf(" NONE");
463         else {
464                 if ((md->md_mflags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
465                         printf(" NOFAILSYNC");
466                 if ((md->md_mflags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0)
467                         printf(" NOAUTOSYNC");
468         }
469         printf("\n");
470         printf("    dflags:");
471         if (md->md_dflags == 0)
472                 printf(" NONE");
473         else {
474                 if ((md->md_dflags & G_MIRROR_DISK_FLAG_DIRTY) != 0)
475                         printf(" DIRTY");
476                 if ((md->md_dflags & G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0)
477                         printf(" SYNCHRONIZING");
478                 if ((md->md_dflags & G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0)
479                         printf(" FORCE_SYNC");
480                 if ((md->md_dflags & G_MIRROR_DISK_FLAG_INACTIVE) != 0)
481                         printf(" INACTIVE");
482         }
483         printf("\n");
484         printf("hcprovider: %s\n", md->md_provider);
485         printf("  provsize: %ju\n", (uintmax_t)md->md_provsize);
486         bzero(hash, sizeof(hash));
487         for (i = 0; i < 16; i++) {
488                 hash[i * 2] = hex[md->md_hash[i] >> 4];
489                 hash[i * 2 + 1] = hex[md->md_hash[i] & 0x0f];
490         }
491         printf("  MD5 hash: %s\n", hash);
492 }
493 #endif  /* !_G_MIRROR_H_ */