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
38 #include <sys/types.h>
39 #include <sys/param.h>
43 #include <sys/malloc.h>
45 #include <sys/vnode.h>
48 MALLOC_DEFINE(M_DMDMIRROR, "dm_dmirror", "Device Mapper Target DMIRROR");
51 #define MEDIA_UNSTABLE 0x0001
52 #define MEDIA_READ_DEGRADED 0x0002
53 #define MEDIA_WRITE_DEGRADED 0x0004
54 #define MEDIA_MASTER 0x0008
55 #define UNINITIALIZED 0x0010
56 #define OLD_UNSTABLE 0x0020
57 #define OLD_MSATER 0x0040
59 /* dmirror disk flags */
60 #define DISK_ONLINE 0x0001
63 #define dmirror_set_bio_disk(bio, x) ((bio)->bio_caller_info1.ptr = (x))
64 #define dmirror_get_bio_disk(bio) ((bio)?((bio)->bio_caller_info1.ptr):NULL)
65 #define dmirror_set_bio_seg(bio, x) ((bio)->bio_caller_info2.offset = (x))
66 #define dmirror_get_bio_segno(bio) ((bio)?((bio)->bio_caller_info2.offset):0)
68 #define dmirror_set_bio_retries(bio, x) ((bio)->bio_caller_info3.value = (x))
69 #define dmirror_get_bio_retries(bio) ((bio)?((bio)->bio_caller_info3.value):0)
71 #define dmirror_set_bio_mbuf(bio, x) ((bio)->bio_caller_info3.ptr = (x))
72 #define dmirror_get_bio_mbuf(bio) ((bio)?((bio)->bio_caller_info3.ptr):NULL)
76 /* Segment descriptor for each logical segment */
77 typedef struct segdesc {
78 uint32_t flags; /* Flags, including state */
79 uint32_t zf_bitmap; /* Zero-fill bitmap */
84 /* XXX: some timestamp/serial */
87 typedef struct dmirror_disk {
92 typedef struct target_dmirror_config {
94 dmirror_disk_t disks[4];
98 } dm_target_dmirror_config_t;
102 dmirror_clone_bio(struct bio *obio)
112 bp->b_vp = mbp->b_vp;
113 bp->b_cmd = mbp->b_cmd;
114 bp->b_data = (char *)mbp->b_data;
115 bp->b_resid = bp->b_bcount = mbp->b_bcount;
116 bp->b_bufsize = bp->b_bcount;
119 bio->bio_offset = obio->bio_offset;
125 dmirror_write_done(struct bio *bio)
129 struct bio *obio, *mbio;
132 disk = dmirror_get_bio_disk(bio);
133 segno = dmirror_get_bio_segno(bio);
134 mbio = dmirror_get_bio_mbuf(bio);
136 if (bio->bio_buf->b_flags & B_ERROR) {
145 dmirror_issue_write(dmirror_disk_t disk, struct bio *bio)
147 dmirror_set_bio_disk(bio, disk);
148 dmirror_set_bio_segno(bio, SEGNO_FROM_OFFSET(bio->bio_offset));
150 bio->bio_done = dmirror_write_done;
151 vn_strategy(disk->pdev, bio);
155 dmirror_write(dm_target_crypt_config_t config, struct bio *bio)
157 dmirror_disk_t disk, m_disk;
158 struct bio *wbio1, *wbio2;
162 for(i = 0; i < XXX config->ndisks; i++) {
163 disk = &config->disks[i];
164 segdesc = SEGDESC_FROM_OFFSET(disk, bio->bio_offset);
165 if (segdesc->flags & MEDIA_MASTER) {
172 dmirror_set_bio_mbuf(bio, NULL);
173 dmirror_issue_write(m_disk, bio);
175 wbio1 = dmirror_clone_bio(bio);
176 wbio2 = dmirror_clone_bio(bio);
177 dmirror_set_bio_mbuf(wbio1, bio);
178 dmirror_set_bio_mbuf(wbio2, bio);
179 dmirror_issue_write(XXX disk1, wbio1);
180 dmirror_issue_write(XXX disk2, wbio2);
186 segdesc_set_flag(dmirror_disk_t disk, off_t segno, int flag)
189 * XXX: set the flag on the in-memory descriptor and write back to disks.
196 segdesc_clear_flag(dmirror_disk_t disk, off_t segno, int flag)
199 * XXX: set the flag on the in-memory descriptor and write back to disks.
205 dmirror_read_done(struct bio *bio)
212 disk = dmirror_get_bio_disk(bio);
213 segno = dmirror_get_bio_segno(bio);
214 retries = dmirror_get_bio_retries(bio);
216 if (bio->bio_buf->b_flags & B_ERROR) {
217 /* read failed, so redispatch to a different disk */
218 segdesc_set_flag(disk, segno, MEDIA_READ_DEGRADED);
219 /* XXX: set other disk to master, if possible */
220 if (retries < disk->config->max_retries) {
221 dmirror_set_bio_retries(bio, retries + 1);
223 * XXX: how do we restore the bio to health? Like this?
225 bio->bio_buf->b_flags &= ~(B_ERROR | B_INVAL);
227 * XXX: something tells me that dispatching stuff from a
228 * biodone routine is not the greatest idea
230 dmirror_issue_read(next_disk, bio);
240 dmirror_issue_read(dmirror_disk_t disk, struct bio *bio)
242 dmirror_set_bio_disk(bio, disk);
243 dmirror_set_bio_segno(bio, SEGNO_FROM_OFFSET(bio->bio_offset));
245 bio->bio_done = dmirror_read_done;
246 vn_strategy(disk->pdev, bio);
250 dmirror_read(dm_target_crypt_config_t config, struct bio *bio)
252 dmirror_disk_t disk, m_disk;
256 for(i = 0; i < XXX config->ndisks; i++) {
257 disk = &config->disks[i];
258 segdesc = SEGDESC_FROM_OFFSET(disk, bio->bio_offset);
259 if (segdesc->flags & MEDIA_MASTER) {
272 segdesc = SEGDESC_FROM_OFFSET(m_disk, bio->bio_offset);
273 if (segdesc->flags & UNINITIALIZED) {
276 dmirror_issue_read(m_disk, bio);
278 /* dispatch read to any disk */
279 /* but try not to send to a READ_DEGRADED drive */
281 for (i = 0; i < config->ndisks; i++) {
282 disk = &config->disks[i];
283 segdesc = SEGDESC_FROM_OFFSET(disk, bio->bio_offset);
284 if (!(segdesc->flags & MEDIA_READ_DEGRADED)) {
289 /* XXX: do the uninitialized magic here, too */
292 * XXX: we found some non-degraded disk. We might want to
293 * optimize performance by sending reads to different disks,
294 * not just the first one.
296 dmirror_set_bio_retries(bio, 0);
297 dmirror_issue_read(m_disk, bio);
299 /* XXX: all disks are read degraded, just sent to any */
300 m_disk = &config->disks[i];
301 dmirror_set_bio_retries(bio, 0);
302 dmirror_issue_read(m_disk, bio);
307 /* Strategy routine called from dm_strategy. */
309 * Do IO operation, called from dmstrategy routine.
312 dm_target_dmirror_strategy(dm_table_entry_t * table_en, struct buf * bp)
314 struct bio *bio, *split_bio1, *split_bio2;
316 off_t bseg, eseg, seg_end;
318 int split_transaction = 0;
320 dm_target_crypt_config_t *priv;
321 priv = table_en->target_config;
323 if ((bp->b_cmd == BUF_CMD_READ) || (bp->b_cmd == BUF_CMD_WRITE)) {
324 /* Get rid of stuff we can't really handle */
325 if (((bp->b_bcount % DEV_BSIZE) != 0) || (bp->b_bcount == 0)) {
326 kprintf("dm_target_dmirror_strategy: can't really handle bp->b_bcount = %d\n", bp->b_bcount);
327 bp->b_error = EINVAL;
328 bp->b_flags |= B_ERROR | B_INVAL;
329 biodone(&bp->b_bio1);
333 bseg = SEGNO_FROM_OFFSET(bp->b_bio1.bio_offset);
334 eseg = SEGNO_FROM_OFFSET(bp->b_bio1.bio_offset + bp->b_resid);
335 seg_end = OFFSET_FROM_SEGNO(eseg);
338 split_transaction = 1;
339 /* fsb = first segment bytes (bytes in the first segment) */
340 fsb = seg_end - bp->b_bio1.bio_offset;
342 nestbuf = getpbuf(NULL);
343 nestiobuf_setup(&bp->b_bio1, nestbuf, 0, fsb);
344 split_bio1 = push_bio(&nestbuf->b_bio1);
345 split_bio1->bio_offset = bp->b_bio1.bio_offset +
346 priv->block_offset*DEV_BSIZE;
348 nestbuf = getpbuf(NULL);
349 nestiobuf_setup(&bp->b_bio1, nestbuf, fsb, bp->b_resid - fsb);
350 split_bio2 = push_bio(&nestbuf->b_bio1);
351 split_bio2->bio_offset = bp->b_bio1.bio_offset + fsb +
352 priv->block_offset*DEV_BSIZE;
358 if (split_transaction) {
359 dmirror_read(priv, split_bio1);
360 dmirror_read(priv, split_bio2);
362 bio = push_bio(&bp->b_bio1);
363 bio->bio_offset = bp->b_bio1.bio_offset + priv->block_offset*DEV_BSIZE;
364 dmirror_read(priv, bio);
369 if (split_transaction) {
370 dmirror_write(priv, split_bio1);
371 dmirror_write(priv, split_bio2);
373 bio = push_bio(&bp->b_bio1);
374 bio->bio_offset = bp->b_bio1.bio_offset + priv->block_offset*DEV_BSIZE;
375 dmirror_write(priv, bio);
381 vn_strategy(priv->pdev[0]->pdev_vnode, &bp->b_bio1);
382 vn_strategy(priv->pdev[1]->pdev_vnode, &bp->b_bio1);
389 /* XXX: add missing dm functions */