2 * Copyright (c) 2010 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Alex Hornung <ahornung@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * This file implements initial version of a mirror target
39 #include <sys/malloc.h>
42 #include <dev/disk/dm/dm.h>
43 MALLOC_DEFINE(M_DMDMIRROR, "dm_dmirror", "Device Mapper Target DMIRROR");
46 #define MEDIA_UNSTABLE 0x0001
47 #define MEDIA_READ_DEGRADED 0x0002
48 #define MEDIA_WRITE_DEGRADED 0x0004
49 #define MEDIA_MASTER 0x0008
50 #define UNINITIALIZED 0x0010
51 #define OLD_UNSTABLE 0x0020
52 #define OLD_MSATER 0x0040
54 /* dmirror disk flags */
55 #define DISK_ONLINE 0x0001
58 #define dmirror_set_bio_disk(bio, x) ((bio)->bio_caller_info1.ptr = (x))
59 #define dmirror_get_bio_disk(bio) ((bio)?((bio)->bio_caller_info1.ptr):NULL)
60 #define dmirror_set_bio_seg(bio, x) ((bio)->bio_caller_info2.offset = (x))
61 #define dmirror_get_bio_segno(bio) ((bio)?((bio)->bio_caller_info2.offset):0)
63 #define dmirror_set_bio_retries(bio, x) ((bio)->bio_caller_info3.value = (x))
64 #define dmirror_get_bio_retries(bio) ((bio)?((bio)->bio_caller_info3.value):0)
66 #define dmirror_set_bio_mbuf(bio, x) ((bio)->bio_caller_info3.ptr = (x))
67 #define dmirror_get_bio_mbuf(bio) ((bio)?((bio)->bio_caller_info3.ptr):NULL)
71 /* Segment descriptor for each logical segment */
72 typedef struct segdesc {
73 uint32_t flags; /* Flags, including state */
74 uint32_t zf_bitmap; /* Zero-fill bitmap */
79 /* XXX: some timestamp/serial */
82 typedef struct dmirror_disk {
87 typedef struct target_dmirror_config {
89 dmirror_disk_t disks[4];
93 } dm_target_dmirror_config_t;
97 dmirror_clone_bio(struct bio *obio)
107 bp->b_vp = mbp->b_vp;
108 bp->b_cmd = mbp->b_cmd;
109 bp->b_data = (char *)mbp->b_data;
110 bp->b_resid = bp->b_bcount = mbp->b_bcount;
111 bp->b_bufsize = bp->b_bcount;
114 bio->bio_offset = obio->bio_offset;
120 dmirror_write_done(struct bio *bio)
124 struct bio *obio, *mbio;
127 disk = dmirror_get_bio_disk(bio);
128 segno = dmirror_get_bio_segno(bio);
129 mbio = dmirror_get_bio_mbuf(bio);
131 if (bio->bio_buf->b_flags & B_ERROR) {
140 dmirror_issue_write(dmirror_disk_t disk, struct bio *bio)
142 dmirror_set_bio_disk(bio, disk);
143 dmirror_set_bio_segno(bio, SEGNO_FROM_OFFSET(bio->bio_offset));
145 bio->bio_done = dmirror_write_done;
146 vn_strategy(disk->pdev, bio);
150 dmirror_write(dm_target_crypt_config_t config, struct bio *bio)
152 dmirror_disk_t disk, m_disk;
153 struct bio *wbio1, *wbio2;
157 for(i = 0; i < XXX config->ndisks; i++) {
158 disk = &config->disks[i];
159 segdesc = SEGDESC_FROM_OFFSET(disk, bio->bio_offset);
160 if (segdesc->flags & MEDIA_MASTER) {
167 dmirror_set_bio_mbuf(bio, NULL);
168 dmirror_issue_write(m_disk, bio);
170 wbio1 = dmirror_clone_bio(bio);
171 wbio2 = dmirror_clone_bio(bio);
172 dmirror_set_bio_mbuf(wbio1, bio);
173 dmirror_set_bio_mbuf(wbio2, bio);
174 dmirror_issue_write(XXX disk1, wbio1);
175 dmirror_issue_write(XXX disk2, wbio2);
181 segdesc_set_flag(dmirror_disk_t disk, off_t segno, int flag)
184 * XXX: set the flag on the in-memory descriptor and write back to disks.
191 segdesc_clear_flag(dmirror_disk_t disk, off_t segno, int flag)
194 * XXX: set the flag on the in-memory descriptor and write back to disks.
200 dmirror_read_done(struct bio *bio)
207 disk = dmirror_get_bio_disk(bio);
208 segno = dmirror_get_bio_segno(bio);
209 retries = dmirror_get_bio_retries(bio);
211 if (bio->bio_buf->b_flags & B_ERROR) {
212 /* read failed, so redispatch to a different disk */
213 segdesc_set_flag(disk, segno, MEDIA_READ_DEGRADED);
214 /* XXX: set other disk to master, if possible */
215 if (retries < disk->config->max_retries) {
216 dmirror_set_bio_retries(bio, retries + 1);
218 * XXX: how do we restore the bio to health? Like this?
220 bio->bio_buf->b_flags &= ~(B_ERROR | B_INVAL);
222 * XXX: something tells me that dispatching stuff from a
223 * biodone routine is not the greatest idea
225 dmirror_issue_read(next_disk, bio);
235 dmirror_issue_read(dmirror_disk_t disk, struct bio *bio)
237 dmirror_set_bio_disk(bio, disk);
238 dmirror_set_bio_segno(bio, SEGNO_FROM_OFFSET(bio->bio_offset));
240 bio->bio_done = dmirror_read_done;
241 vn_strategy(disk->pdev, bio);
245 dmirror_read(dm_target_crypt_config_t config, struct bio *bio)
247 dmirror_disk_t disk, m_disk;
251 for(i = 0; i < XXX config->ndisks; i++) {
252 disk = &config->disks[i];
253 segdesc = SEGDESC_FROM_OFFSET(disk, bio->bio_offset);
254 if (segdesc->flags & MEDIA_MASTER) {
267 segdesc = SEGDESC_FROM_OFFSET(m_disk, bio->bio_offset);
268 if (segdesc->flags & UNINITIALIZED) {
271 dmirror_issue_read(m_disk, bio);
273 /* dispatch read to any disk */
274 /* but try not to send to a READ_DEGRADED drive */
276 for (i = 0; i < config->ndisks; i++) {
277 disk = &config->disks[i];
278 segdesc = SEGDESC_FROM_OFFSET(disk, bio->bio_offset);
279 if (!(segdesc->flags & MEDIA_READ_DEGRADED)) {
284 /* XXX: do the uninitialized magic here, too */
287 * XXX: we found some non-degraded disk. We might want to
288 * optimize performance by sending reads to different disks,
289 * not just the first one.
291 dmirror_set_bio_retries(bio, 0);
292 dmirror_issue_read(m_disk, bio);
294 /* XXX: all disks are read degraded, just sent to any */
295 m_disk = &config->disks[i];
296 dmirror_set_bio_retries(bio, 0);
297 dmirror_issue_read(m_disk, bio);
302 /* Strategy routine called from dm_strategy. */
304 * Do IO operation, called from dmstrategy routine.
307 dm_target_dmirror_strategy(dm_table_entry_t *table_en, struct buf *bp)
309 struct bio *bio, *split_bio1, *split_bio2;
311 off_t bseg, eseg, seg_end;
313 int split_transaction = 0;
315 dm_target_crypt_config_t *priv;
316 priv = table_en->target_config;
318 if ((bp->b_cmd == BUF_CMD_READ) || (bp->b_cmd == BUF_CMD_WRITE)) {
319 /* Get rid of stuff we can't really handle */
320 if (((bp->b_bcount % DEV_BSIZE) != 0) || (bp->b_bcount == 0)) {
321 kprintf("dm_target_dmirror_strategy: can't really handle bp->b_bcount = %d\n", bp->b_bcount);
322 bp->b_error = EINVAL;
323 bp->b_flags |= B_ERROR | B_INVAL;
324 biodone(&bp->b_bio1);
328 bseg = SEGNO_FROM_OFFSET(bp->b_bio1.bio_offset);
329 eseg = SEGNO_FROM_OFFSET(bp->b_bio1.bio_offset + bp->b_resid);
330 seg_end = OFFSET_FROM_SEGNO(eseg);
333 split_transaction = 1;
334 /* fsb = first segment bytes (bytes in the first segment) */
335 fsb = seg_end - bp->b_bio1.bio_offset;
337 nestbuf = getpbuf(NULL);
338 nestiobuf_setup(&bp->b_bio1, nestbuf, 0, fsb);
339 split_bio1 = push_bio(&nestbuf->b_bio1);
340 split_bio1->bio_offset = bp->b_bio1.bio_offset +
341 priv->block_offset*DEV_BSIZE;
343 nestbuf = getpbuf(NULL);
344 nestiobuf_setup(&bp->b_bio1, nestbuf, fsb, bp->b_resid - fsb);
345 split_bio2 = push_bio(&nestbuf->b_bio1);
346 split_bio2->bio_offset = bp->b_bio1.bio_offset + fsb +
347 priv->block_offset*DEV_BSIZE;
353 if (split_transaction) {
354 dmirror_read(priv, split_bio1);
355 dmirror_read(priv, split_bio2);
357 bio = push_bio(&bp->b_bio1);
358 bio->bio_offset = bp->b_bio1.bio_offset + priv->block_offset*DEV_BSIZE;
359 dmirror_read(priv, bio);
364 if (split_transaction) {
365 dmirror_write(priv, split_bio1);
366 dmirror_write(priv, split_bio2);
368 bio = push_bio(&bp->b_bio1);
369 bio->bio_offset = bp->b_bio1.bio_offset + priv->block_offset*DEV_BSIZE;
370 dmirror_write(priv, bio);
376 vn_strategy(priv->pdev[0]->pdev_vnode, &bp->b_bio1);
377 vn_strategy(priv->pdev[1]->pdev_vnode, &bp->b_bio1);
384 /* XXX: add missing dm functions */