]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/geom/mirror/g_mirror.h
MFC r332182:
[FreeBSD/FreeBSD.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_CANDELETE            0x0000000000000040ULL
63
64 /* Per-disk flags which are recorded in on-disk metadata. */
65 #define G_MIRROR_DISK_FLAG_MASK         (G_MIRROR_DISK_FLAG_DIRTY |     \
66                                          G_MIRROR_DISK_FLAG_SYNCHRONIZING | \
67                                          G_MIRROR_DISK_FLAG_FORCE_SYNC | \
68                                          G_MIRROR_DISK_FLAG_INACTIVE | \
69                                          G_MIRROR_DISK_FLAG_CANDELETE)
70
71 #define G_MIRROR_DEVICE_FLAG_NOAUTOSYNC 0x0000000000000001ULL
72 #define G_MIRROR_DEVICE_FLAG_NOFAILSYNC 0x0000000000000002ULL
73
74 /* Mirror flags which are recorded in on-disk metadata. */
75 #define G_MIRROR_DEVICE_FLAG_MASK       (G_MIRROR_DEVICE_FLAG_NOAUTOSYNC | \
76                                          G_MIRROR_DEVICE_FLAG_NOFAILSYNC)
77
78 #ifdef _KERNEL
79 extern int g_mirror_debug;
80
81 #define G_MIRROR_DEBUG(lvl, ...)        do {                            \
82         if (g_mirror_debug >= (lvl)) {                                  \
83                 printf("GEOM_MIRROR");                                  \
84                 if (g_mirror_debug > 0)                                 \
85                         printf("[%u]", lvl);                            \
86                 printf(": ");                                           \
87                 printf(__VA_ARGS__);                                    \
88                 printf("\n");                                           \
89         }                                                               \
90 } while (0)
91 #define G_MIRROR_LOGREQ(lvl, bp, ...)   do {                            \
92         if (g_mirror_debug >= (lvl)) {                                  \
93                 printf("GEOM_MIRROR");                                  \
94                 if (g_mirror_debug > 0)                                 \
95                         printf("[%u]", lvl);                            \
96                 printf(": ");                                           \
97                 printf(__VA_ARGS__);                                    \
98                 printf(" ");                                            \
99                 g_print_bio(bp);                                        \
100                 printf("\n");                                           \
101         }                                                               \
102 } while (0)
103
104 #define G_MIRROR_BIO_FLAG_REGULAR       0x01
105 #define G_MIRROR_BIO_FLAG_SYNC          0x02
106
107 /*
108  * Informations needed for synchronization.
109  */
110 struct g_mirror_disk_sync {
111         struct g_consumer *ds_consumer; /* Consumer connected to our mirror. */
112         off_t             ds_offset;    /* Offset of next request to send. */
113         off_t             ds_offset_done; /* Offset of already synchronized
114                                            region. */
115         time_t            ds_update_ts; /* Time of last metadata update. */
116         u_int             ds_syncid;    /* Disk's synchronization ID. */
117         u_int             ds_inflight;  /* Number of in-flight sync requests. */
118         struct bio      **ds_bios;      /* BIOs for synchronization I/O. */
119 };
120
121 /*
122  * Informations needed for synchronization.
123  */
124 struct g_mirror_device_sync {
125         struct g_geom   *ds_geom;       /* Synchronization geom. */
126         u_int            ds_ndisks;     /* Number of disks in SYNCHRONIZING
127                                            state. */
128 };
129
130 #define G_MIRROR_DISK_STATE_NONE                0
131 #define G_MIRROR_DISK_STATE_NEW                 1
132 #define G_MIRROR_DISK_STATE_ACTIVE              2
133 #define G_MIRROR_DISK_STATE_STALE               3
134 #define G_MIRROR_DISK_STATE_SYNCHRONIZING       4
135 #define G_MIRROR_DISK_STATE_DISCONNECTED        5
136 #define G_MIRROR_DISK_STATE_DESTROY             6
137 struct g_mirror_disk {
138         uint32_t         d_id;          /* Disk ID. */
139         struct g_consumer *d_consumer;  /* Consumer. */
140         struct g_mirror_softc   *d_softc; /* Back-pointer to softc. */
141         int              d_state;       /* Disk state. */
142         u_int            d_priority;    /* Disk priority. */
143         u_int            load;          /* Averaged queue length */
144         off_t            d_last_offset; /* Last read offset */
145         uint64_t         d_flags;       /* Additional flags. */
146         u_int            d_genid;       /* Disk's generation ID. */
147         struct g_mirror_disk_sync d_sync;/* Sync information. */
148         LIST_ENTRY(g_mirror_disk) d_next;
149 };
150 #define d_name  d_consumer->provider->name
151
152 #define G_MIRROR_EVENT_DONTWAIT 0x1
153 #define G_MIRROR_EVENT_WAIT     0x2
154 #define G_MIRROR_EVENT_DEVICE   0x4
155 #define G_MIRROR_EVENT_DONE     0x8
156 struct g_mirror_event {
157         struct g_mirror_disk    *e_disk;
158         int                      e_state;
159         int                      e_flags;
160         int                      e_error;
161         TAILQ_ENTRY(g_mirror_event) e_next;
162 };
163
164 #define G_MIRROR_DEVICE_FLAG_DESTROY    0x0100000000000000ULL
165 #define G_MIRROR_DEVICE_FLAG_DRAIN      0x0200000000000000ULL
166 #define G_MIRROR_DEVICE_FLAG_CLOSEWAIT  0x0400000000000000ULL
167 #define G_MIRROR_DEVICE_FLAG_TASTING    0x0800000000000000ULL
168 #define G_MIRROR_DEVICE_FLAG_WIPE       0x1000000000000000ULL
169
170 #define G_MIRROR_DEVICE_STATE_STARTING          0
171 #define G_MIRROR_DEVICE_STATE_RUNNING           1
172
173 #define G_MIRROR_TYPE_MANUAL    0
174 #define G_MIRROR_TYPE_AUTOMATIC 1
175
176 /* Bump syncid on first write. */
177 #define G_MIRROR_BUMP_SYNCID            0x1
178 /* Bump genid immediately. */
179 #define G_MIRROR_BUMP_GENID             0x2
180 /* Bump syncid immediately. */
181 #define G_MIRROR_BUMP_SYNCID_NOW        0x4
182 struct g_mirror_softc {
183         u_int           sc_type;        /* Device type (manual/automatic). */
184         u_int           sc_state;       /* Device state. */
185         uint32_t        sc_slice;       /* Slice size. */
186         uint8_t         sc_balance;     /* Balance algorithm. */
187         uint64_t        sc_mediasize;   /* Device size. */
188         uint32_t        sc_sectorsize;  /* Sector size. */
189         uint64_t        sc_flags;       /* Additional flags. */
190
191         struct g_geom   *sc_geom;
192         struct g_provider *sc_provider;
193         int             sc_provider_open;
194
195         uint32_t        sc_id;          /* Mirror unique ID. */
196
197         struct sx        sc_lock;
198         struct bio_queue sc_queue;
199         struct mtx       sc_queue_mtx;
200         struct proc     *sc_worker;
201         struct bio_queue sc_inflight; /* In-flight regular write requests. */
202         struct bio_queue sc_regular_delayed; /* Delayed I/O requests due to
203                                                 collision with sync requests. */
204         struct bio_queue sc_sync_delayed; /* Delayed sync requests due to
205                                              collision with regular requests. */
206
207         LIST_HEAD(, g_mirror_disk) sc_disks;
208         u_int           sc_ndisks;      /* Number of disks. */
209         struct g_mirror_disk *sc_hint;
210
211         u_int           sc_genid;       /* Generation ID. */
212         u_int           sc_syncid;      /* Synchronization ID. */
213         int             sc_bump_id;
214         struct g_mirror_device_sync sc_sync;
215         int             sc_idle;        /* DIRTY flags removed. */
216         time_t          sc_last_write;
217         u_int           sc_writes;
218         u_int           sc_refcnt;      /* Number of softc references */
219
220         TAILQ_HEAD(, g_mirror_event) sc_events;
221         struct mtx      sc_events_mtx;
222
223         struct callout  sc_callout;
224
225         struct root_hold_token *sc_rootmount;
226
227         struct mtx       sc_done_mtx;
228 };
229 #define sc_name sc_geom->name
230
231 struct g_mirror_metadata;
232
233 u_int g_mirror_ndisks(struct g_mirror_softc *sc, int state);
234 struct g_geom * g_mirror_create(struct g_class *mp,
235     const struct g_mirror_metadata *md, u_int type);
236 #define G_MIRROR_DESTROY_SOFT           0
237 #define G_MIRROR_DESTROY_DELAYED        1
238 #define G_MIRROR_DESTROY_HARD           2
239 int g_mirror_destroy(struct g_mirror_softc *sc, int how);
240 int g_mirror_event_send(void *arg, int state, int flags);
241 struct g_mirror_metadata;
242 int g_mirror_add_disk(struct g_mirror_softc *sc, struct g_provider *pp,
243     struct g_mirror_metadata *md);
244 int g_mirror_read_metadata(struct g_consumer *cp, struct g_mirror_metadata *md);
245 void g_mirror_fill_metadata(struct g_mirror_softc *sc,
246     struct g_mirror_disk *disk, struct g_mirror_metadata *md);
247 void g_mirror_update_metadata(struct g_mirror_disk *disk);
248
249 g_ctl_req_t g_mirror_config;
250 #endif  /* _KERNEL */
251
252 struct g_mirror_metadata {
253         char            md_magic[16];   /* Magic value. */
254         uint32_t        md_version;     /* Version number. */
255         char            md_name[16];    /* Mirror name. */
256         uint32_t        md_mid;         /* Mirror unique ID. */
257         uint32_t        md_did;         /* Disk unique ID. */
258         uint8_t         md_all;         /* Number of disks in mirror. */
259         uint32_t        md_genid;       /* Generation ID. */
260         uint32_t        md_syncid;      /* Synchronization ID. */
261         uint8_t         md_priority;    /* Disk priority. */
262         uint32_t        md_slice;       /* Slice size. */
263         uint8_t         md_balance;     /* Balance type. */
264         uint64_t        md_mediasize;   /* Size of the smallest
265                                            disk in mirror. */
266         uint32_t        md_sectorsize;  /* Sector size. */
267         uint64_t        md_sync_offset; /* Synchronized offset. */
268         uint64_t        md_mflags;      /* Additional mirror flags. */
269         uint64_t        md_dflags;      /* Additional disk flags. */
270         char            md_provider[16]; /* Hardcoded provider. */
271         uint64_t        md_provsize;    /* Provider's size. */
272         u_char          md_hash[16];    /* MD5 hash. */
273 };
274 static __inline void
275 mirror_metadata_encode(struct g_mirror_metadata *md, u_char *data)
276 {
277         MD5_CTX ctx;
278
279         bcopy(md->md_magic, data, 16);
280         le32enc(data + 16, md->md_version);
281         bcopy(md->md_name, data + 20, 16);
282         le32enc(data + 36, md->md_mid);
283         le32enc(data + 40, md->md_did);
284         *(data + 44) = md->md_all;
285         le32enc(data + 45, md->md_genid);
286         le32enc(data + 49, md->md_syncid);
287         *(data + 53) = md->md_priority;
288         le32enc(data + 54, md->md_slice);
289         *(data + 58) = md->md_balance;
290         le64enc(data + 59, md->md_mediasize);
291         le32enc(data + 67, md->md_sectorsize);
292         le64enc(data + 71, md->md_sync_offset);
293         le64enc(data + 79, md->md_mflags);
294         le64enc(data + 87, md->md_dflags);
295         bcopy(md->md_provider, data + 95, 16);
296         le64enc(data + 111, md->md_provsize);
297         MD5Init(&ctx);
298         MD5Update(&ctx, data, 119);
299         MD5Final(md->md_hash, &ctx);
300         bcopy(md->md_hash, data + 119, 16);
301 }
302 static __inline int
303 mirror_metadata_decode_v0v1(const u_char *data, struct g_mirror_metadata *md)
304 {
305         MD5_CTX ctx;
306
307         bcopy(data + 20, md->md_name, 16);
308         md->md_mid = le32dec(data + 36);
309         md->md_did = le32dec(data + 40);
310         md->md_all = *(data + 44);
311         md->md_syncid = le32dec(data + 45);
312         md->md_priority = *(data + 49);
313         md->md_slice = le32dec(data + 50);
314         md->md_balance = *(data + 54);
315         md->md_mediasize = le64dec(data + 55);
316         md->md_sectorsize = le32dec(data + 63);
317         md->md_sync_offset = le64dec(data + 67);
318         md->md_mflags = le64dec(data + 75);
319         md->md_dflags = le64dec(data + 83);
320         bcopy(data + 91, md->md_provider, 16);
321         bcopy(data + 107, md->md_hash, 16);
322         MD5Init(&ctx);
323         MD5Update(&ctx, data, 107);
324         MD5Final(md->md_hash, &ctx);
325         if (bcmp(md->md_hash, data + 107, 16) != 0)
326                 return (EINVAL);
327
328         /* New fields. */
329         md->md_genid = 0;
330         md->md_provsize = 0;
331
332         return (0);
333 }
334 static __inline int
335 mirror_metadata_decode_v2(const u_char *data, struct g_mirror_metadata *md)
336 {
337         MD5_CTX ctx;
338
339         bcopy(data + 20, md->md_name, 16);
340         md->md_mid = le32dec(data + 36);
341         md->md_did = le32dec(data + 40);
342         md->md_all = *(data + 44);
343         md->md_genid = le32dec(data + 45);
344         md->md_syncid = le32dec(data + 49);
345         md->md_priority = *(data + 53);
346         md->md_slice = le32dec(data + 54);
347         md->md_balance = *(data + 58);
348         md->md_mediasize = le64dec(data + 59);
349         md->md_sectorsize = le32dec(data + 67);
350         md->md_sync_offset = le64dec(data + 71);
351         md->md_mflags = le64dec(data + 79);
352         md->md_dflags = le64dec(data + 87);
353         bcopy(data + 95, md->md_provider, 16);
354         bcopy(data + 111, md->md_hash, 16);
355         MD5Init(&ctx);
356         MD5Update(&ctx, data, 111);
357         MD5Final(md->md_hash, &ctx);
358         if (bcmp(md->md_hash, data + 111, 16) != 0)
359                 return (EINVAL);
360
361         /* New fields. */
362         md->md_provsize = 0;
363
364         return (0);
365 }
366 static __inline int
367 mirror_metadata_decode_v3v4(const u_char *data, struct g_mirror_metadata *md)
368 {
369         MD5_CTX ctx;
370
371         bcopy(data + 20, md->md_name, 16);
372         md->md_mid = le32dec(data + 36);
373         md->md_did = le32dec(data + 40);
374         md->md_all = *(data + 44);
375         md->md_genid = le32dec(data + 45);
376         md->md_syncid = le32dec(data + 49);
377         md->md_priority = *(data + 53);
378         md->md_slice = le32dec(data + 54);
379         md->md_balance = *(data + 58);
380         md->md_mediasize = le64dec(data + 59);
381         md->md_sectorsize = le32dec(data + 67);
382         md->md_sync_offset = le64dec(data + 71);
383         md->md_mflags = le64dec(data + 79);
384         md->md_dflags = le64dec(data + 87);
385         bcopy(data + 95, md->md_provider, 16);
386         md->md_provsize = le64dec(data + 111);
387         bcopy(data + 119, md->md_hash, 16);
388         MD5Init(&ctx);
389         MD5Update(&ctx, data, 119);
390         MD5Final(md->md_hash, &ctx);
391         if (bcmp(md->md_hash, data + 119, 16) != 0)
392                 return (EINVAL);
393         return (0);
394 }
395 static __inline int
396 mirror_metadata_decode(const u_char *data, struct g_mirror_metadata *md)
397 {
398         int error;
399
400         bcopy(data, md->md_magic, 16);
401         md->md_version = le32dec(data + 16);
402         switch (md->md_version) {
403         case 0:
404         case 1:
405                 error = mirror_metadata_decode_v0v1(data, md);
406                 break;
407         case 2:
408                 error = mirror_metadata_decode_v2(data, md);
409                 break;
410         case 3:
411         case 4:
412                 error = mirror_metadata_decode_v3v4(data, md);
413                 break;
414         default:
415                 error = EINVAL;
416                 break;
417         }
418         return (error);
419 }
420
421 static __inline const char *
422 balance_name(u_int balance)
423 {
424         static const char *algorithms[] = {
425                 [G_MIRROR_BALANCE_NONE] = "none",
426                 [G_MIRROR_BALANCE_ROUND_ROBIN] = "round-robin",
427                 [G_MIRROR_BALANCE_LOAD] = "load",
428                 [G_MIRROR_BALANCE_SPLIT] = "split",
429                 [G_MIRROR_BALANCE_PREFER] = "prefer",
430                 [G_MIRROR_BALANCE_MAX + 1] = "unknown"
431         };
432
433         if (balance > G_MIRROR_BALANCE_MAX)
434                 balance = G_MIRROR_BALANCE_MAX + 1;
435
436         return (algorithms[balance]);
437 }
438
439 static __inline int
440 balance_id(const char *name)
441 {
442         static const char *algorithms[] = {
443                 [G_MIRROR_BALANCE_NONE] = "none",
444                 [G_MIRROR_BALANCE_ROUND_ROBIN] = "round-robin",
445                 [G_MIRROR_BALANCE_LOAD] = "load",
446                 [G_MIRROR_BALANCE_SPLIT] = "split",
447                 [G_MIRROR_BALANCE_PREFER] = "prefer"
448         };
449         int n;
450
451         for (n = G_MIRROR_BALANCE_MIN; n <= G_MIRROR_BALANCE_MAX; n++) {
452                 if (strcmp(name, algorithms[n]) == 0)
453                         return (n);
454         }
455         return (-1);
456 }
457
458 static __inline void
459 mirror_metadata_dump(const struct g_mirror_metadata *md)
460 {
461         static const char hex[] = "0123456789abcdef";
462         char hash[16 * 2 + 1];
463         u_int i;
464
465         printf("     magic: %s\n", md->md_magic);
466         printf("   version: %u\n", (u_int)md->md_version);
467         printf("      name: %s\n", md->md_name);
468         printf("       mid: %u\n", (u_int)md->md_mid);
469         printf("       did: %u\n", (u_int)md->md_did);
470         printf("       all: %u\n", (u_int)md->md_all);
471         printf("     genid: %u\n", (u_int)md->md_genid);
472         printf("    syncid: %u\n", (u_int)md->md_syncid);
473         printf("  priority: %u\n", (u_int)md->md_priority);
474         printf("     slice: %u\n", (u_int)md->md_slice);
475         printf("   balance: %s\n", balance_name((u_int)md->md_balance));
476         printf(" mediasize: %jd\n", (intmax_t)md->md_mediasize);
477         printf("sectorsize: %u\n", (u_int)md->md_sectorsize);
478         printf("syncoffset: %jd\n", (intmax_t)md->md_sync_offset);
479         printf("    mflags:");
480         if (md->md_mflags == 0)
481                 printf(" NONE");
482         else {
483                 if ((md->md_mflags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
484                         printf(" NOFAILSYNC");
485                 if ((md->md_mflags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0)
486                         printf(" NOAUTOSYNC");
487         }
488         printf("\n");
489         printf("    dflags:");
490         if (md->md_dflags == 0)
491                 printf(" NONE");
492         else {
493                 if ((md->md_dflags & G_MIRROR_DISK_FLAG_DIRTY) != 0)
494                         printf(" DIRTY");
495                 if ((md->md_dflags & G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0)
496                         printf(" SYNCHRONIZING");
497                 if ((md->md_dflags & G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0)
498                         printf(" FORCE_SYNC");
499                 if ((md->md_dflags & G_MIRROR_DISK_FLAG_INACTIVE) != 0)
500                         printf(" INACTIVE");
501         }
502         printf("\n");
503         printf("hcprovider: %s\n", md->md_provider);
504         printf("  provsize: %ju\n", (uintmax_t)md->md_provsize);
505         bzero(hash, sizeof(hash));
506         for (i = 0; i < 16; i++) {
507                 hash[i * 2] = hex[md->md_hash[i] >> 4];
508                 hash[i * 2 + 1] = hex[md->md_hash[i] & 0x0f];
509         }
510         printf("  MD5 hash: %s\n", hash);
511 }
512 #endif  /* !_G_MIRROR_H_ */