1 /* $NetBSD: rf_paritymap.h,v 1.2 2010/03/14 21:11:41 jld Exp $ */
2 
3 /*-
4  * Copyright (c) 2009 Jed Davis.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/mutex.h>
30 #include <sys/param.h>
31 #include <sys/rwlock.h>
32 #include <sys/types.h>
33 
34 #include <dev/raidframe/raidframevar.h>
35 
36 /* RF_PARITYMAP_N* in raidframevar.h */
37 
38 #define RF_PMLABEL_VALID 1
39 #define RF_PMLABEL_WASUSED 2
40 #define RF_PMLABEL_DISABLE 4
41 
42 /*
43  * On-disk format: a single bit for each region; if the bit is clear,
44  * then the parity is clean.
45  */
46 struct rf_paritymap_ondisk
47 {
48         /* XXX Do these really need to be volatile? */
49           volatile char bits[RF_PARITYMAP_NBYTE];
50 };
51 
52 /* In-core per-region state: a byte for each, encoded as follows. */
53 struct rf_paritymap_current
54 {
55           volatile int8_t state[RF_PARITYMAP_NREG];
56           /*
57            * Values:
58            * if x == 0, the region may be written out as clean
59            * if x > 0, then x outstanding IOs to that region
60            * if x < 0, then there was recently IO; periodically increment x
61            */
62 };
63 
64 /* The entire state. */
65 struct rf_paritymap
66 {
67           struct rf_paritymap_ondisk *disk_boot, *disk_now;
68           struct rf_paritymap_current *current;
69 
70           /*
71            * This lock will be held while component disks' caches are
72            * flushed, which could take many milliseconds, so it should
73            * not be taken where that kind of delay is unacceptable.
74            * Contention on this lock is not, however, expected to be a
75            * performance bottleneck.
76            */
77           kmutex_t lock;
78           /*
79            * The flags field, below, has its own lock so that
80            * inter-thread communication can occur without taking the
81            * overall lock.  Ordering is lock -> lk_flags.
82            */
83           kmutex_t lk_flags;
84 
85           RF_Raid_t *raid;
86           daddr_t region_size;
87           callout_t ticker;
88           struct rf_pmparams params;
89           volatile int flags;
90           struct rf_pmctrs ctrs;
91 };
92 
93 void rf_paritymap_status(struct rf_paritymap *, struct rf_pmstat *);
94 
95 int rf_paritymap_test(struct rf_paritymap *, daddr_t);
96 void rf_paritymap_begin_region(struct rf_paritymap *, unsigned);
97 void rf_paritymap_begin(struct rf_paritymap *, daddr_t, daddr_t);
98 void rf_paritymap_end_region(struct rf_paritymap *, unsigned);
99 void rf_paritymap_end(struct rf_paritymap *, daddr_t, daddr_t);
100 
101 void rf_paritymap_checkwork(struct rf_paritymap *);
102 void rf_paritymap_invalidate(struct rf_paritymap *);
103 void rf_paritymap_forceclean(struct rf_paritymap *);
104 void rf_paritymap_write(struct rf_paritymap *);
105 
106 int rf_paritymap_init(struct rf_paritymap *, RF_Raid_t *,
107     const struct rf_pmparams *);
108 void rf_paritymap_destroy(struct rf_paritymap *, int);
109 
110 int rf_paritymap_rewrite(struct rf_paritymap *);
111 
112 int rf_paritymap_merge(struct rf_paritymap_ondisk *,
113     struct rf_paritymap_ondisk *);
114 
115 int rf_paritymap_ineligible(RF_Raid_t *);
116 void rf_paritymap_attach(RF_Raid_t *, int);
117 void rf_paritymap_detach(RF_Raid_t *); /* Not while the RAID is live! */
118 
119 int rf_paritymap_get_disable(RF_Raid_t *);
120 void rf_paritymap_set_disable(RF_Raid_t *, int);
121 
122 int rf_paritymap_set_params(struct rf_paritymap *,
123     const struct rf_pmparams *, int);
124 
125 void rf_paritymap_init_label(struct rf_paritymap *,
126     RF_ComponentLabel_t *);
127