1/*- 2 * Copyright (c) 2002 Poul-Henning Kamp 3 * Copyright (c) 2002 Networks Associates Technology, Inc. 4 * All rights reserved. 5 * 6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp 7 * and NAI Labs, the Security Research Division of Network Associates, Inc. 8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 9 * DARPA CHATS research program. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. The names of the authors may not be used to endorse or promote 20 * products derived from this software without specific prior written 21 * permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 *
|
35 * $FreeBSD: head/sys/geom/geom_slice.c 93778 2002-04-04 09:58:20Z phk $
|
35 * $FreeBSD: head/sys/geom/geom_slice.c 94287 2002-04-09 15:43:32Z phk $ |
36 */ 37 38 39#include <sys/param.h> 40#ifndef _KERNEL 41#include <stdio.h> 42#include <unistd.h> 43#include <stdlib.h> 44#include <signal.h> 45#include <err.h> 46#else 47#include <sys/systm.h> 48#include <sys/kernel.h> 49#include <sys/malloc.h> 50#include <sys/bio.h> 51#include <sys/sysctl.h> 52#include <sys/proc.h> 53#include <sys/kthread.h> 54#include <sys/lock.h> 55#include <sys/mutex.h> 56#endif 57#include <sys/errno.h> 58#include <sys/sbuf.h> 59#include <geom/geom.h> 60#include <geom/geom_slice.h> 61#include <machine/stdarg.h> 62 63static g_orphan_t g_slice_orphan; 64static g_access_t g_slice_access; 65static g_start_t g_slice_start; 66 67struct g_slicer * 68g_slice_init(unsigned nslice, unsigned scsize) 69{ 70 struct g_slicer *gsp; 71 72 gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO); 73 gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO); 74 gsp->slices = g_malloc(nslice * sizeof(struct g_slice), M_WAITOK | M_ZERO); 75 gsp->nslice = nslice; 76 return (gsp); 77} 78 79static int 80g_slice_access(struct g_provider *pp, int dr, int dw, int de) 81{ 82 int error, i; 83 struct g_geom *gp; 84 struct g_consumer *cp; 85 struct g_provider *pp2; 86 struct g_slicer *gsp; 87 struct g_slice *gsl, *gsl2; 88 89 gp = pp->geom; 90 cp = LIST_FIRST(&gp->consumer); 91 KASSERT (cp != NULL, ("g_slice_access but no consumer")); 92 gsp = gp->softc; 93 gsl = &gsp->slices[pp->index]; 94 for (i = 0; i < gsp->nslice; i++) { 95 gsl2 = &gsp->slices[i]; 96 if (gsl2->length == 0) 97 continue; 98 if (i == pp->index) 99 continue; 100 if (gsl->offset + gsl->length <= gsl2->offset) 101 continue; 102 if (gsl2->offset + gsl2->length <= gsl->offset) 103 continue; 104 /* overlap */ 105 pp2 = gsl2->provider; 106 if ((pp->acw + dw) > 0 && pp2->ace > 0) 107 return (EPERM); 108 if ((pp->ace + de) > 0 && pp2->acw > 0) 109 return (EPERM); 110 } 111 /* On first open, grab an extra "exclusive" bit */ 112 if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0) 113 de++; 114 /* ... and let go of it on last close */ 115 if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1) 116 de--; 117 error = g_access_rel(cp, dr, dw, de); 118 pp->mediasize = gsp->slices[pp->index].length; 119 return (error); 120} 121 122static void 123g_slice_start(struct bio *bp) 124{ 125 struct bio *bp2; 126 struct g_provider *pp; 127 struct g_geom *gp; 128 struct g_consumer *cp; 129 struct g_slicer *gsp; 130 struct g_slice *gsl; 131 int index;
|
132 off_t t; |
133 134 pp = bp->bio_to; 135 gp = pp->geom; 136 gsp = gp->softc; 137 cp = LIST_FIRST(&gp->consumer); 138 index = pp->index;
|
139 gsl = &gsp->slices[index]; |
140 switch(bp->bio_cmd) { 141 case BIO_READ: 142 case BIO_WRITE: 143 case BIO_DELETE:
|
142 gsl = &gsp->slices[index];
|
144 if (bp->bio_offset > gsl->length) { 145 bp->bio_error = EINVAL; /* XXX: EWHAT ? */ 146 g_io_deliver(bp); 147 return; 148 } 149 bp2 = g_clone_bio(bp); 150 if (bp2->bio_offset + bp2->bio_length > gsl->length) 151 bp2->bio_length = gsl->length - bp2->bio_offset; 152 bp2->bio_done = g_std_done; 153 bp2->bio_offset += gsl->offset; 154 g_io_request(bp2, cp); 155 return; 156 case BIO_GETATTR: 157 case BIO_SETATTR:
|
158 /* Give the real method a chance to override */ 159 if (gsp->start(bp)) 160 return; |
161 if (g_haveattr_off_t(bp, "GEOM::mediasize", 162 gsp->slices[index].length)) 163 return;
|
160 if (gsp->start(bp))
|
164 if (!strcmp("GEOM::frontstuff", bp->bio_attribute)) { 165 t = gsp->cfrontstuff; 166 if (gsp->frontstuff > t) 167 t = gsp->frontstuff; 168 t -= gsl->offset; 169 if (t < 0) 170 t = 0; 171 if (t > gsl->length) 172 t = gsl->length; 173 g_haveattr_off_t(bp, "GEOM::frontstuff", t); |
174 return;
|
175 } |
176 bp2 = g_clone_bio(bp); 177 bp2->bio_done = g_std_done; 178 g_io_request(bp2, cp); 179 break; 180 default: 181 bp->bio_error = EOPNOTSUPP; 182 g_io_deliver(bp); 183 return; 184 } 185} 186 187void 188g_slice_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp) 189{ 190 struct g_mbr_softc *mp; 191 struct g_slicer *gsp; 192 193 gsp = gp->softc; 194 mp = gsp->softc;
|
195 if (gp != NULL) { 196 sbuf_printf(sb, "%s<frontstuff>%llu</frontstuff>\n", 197 indent, (unsigned long long)gsp->frontstuff); 198 } |
199 if (pp != NULL) { 200 sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index); 201 sbuf_printf(sb, "%s<length>%llu</length>\n", 202 indent, (unsigned long long)gsp->slices[pp->index].length); 203 sbuf_printf(sb, "%s<seclength>%llu</seclength>\n", indent, 204 (unsigned long long)gsp->slices[pp->index].length / 512); 205 sbuf_printf(sb, "%s<offset>%llu</offset>\n", indent, 206 (unsigned long long)gsp->slices[pp->index].offset); 207 sbuf_printf(sb, "%s<secoffset>%llu</secoffset>\n", indent, 208 (unsigned long long)gsp->slices[pp->index].offset / 512); 209 } 210} 211 212struct g_provider * 213g_slice_addslice(struct g_geom *gp, int index, off_t offset, off_t length, char *fmt, ...) 214{ 215 struct g_provider *pp; 216 struct g_slicer *gsp; 217 va_list ap; 218 struct sbuf *sb; 219 220 g_trace(G_T_TOPOLOGY, "g_slice_addslice()"); 221 g_topology_lock(); 222 gsp = gp->softc; 223 va_start(ap, fmt); 224 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 225 sbuf_vprintf(sb, fmt, ap); 226 sbuf_finish(sb); 227 pp = g_new_providerf(gp, sbuf_data(sb)); 228 229 pp->index = index; 230 gsp->slices[index].length = length; 231 gsp->slices[index].offset = offset; 232 gsp->slices[index].provider = pp; 233 sbuf_delete(sb); 234 g_topology_unlock(); 235 return(pp); 236} 237 238struct g_geom * 239g_slice_new(struct g_class *mp, int slices, struct g_provider *pp, struct g_consumer **cpp, void *extrap, int extra, g_slice_start_t *start) 240{ 241 struct g_geom *gp; 242 struct g_slicer *gsp; 243 struct g_consumer *cp; 244 void **vp;
|
227 int error;
|
245 int error, i; |
246 247 g_topology_assert(); 248 vp = (void **)extrap; 249 gp = g_new_geomf(mp, "%s", pp->name); 250 gsp = g_slice_init(slices, extra); 251 gsp->start = start; 252 gp->access = g_slice_access; 253 gp->orphan = g_slice_orphan; 254 gp->softc = gsp; 255 gp->start = g_slice_start; 256 gp->spoiled = g_std_spoiled; 257 gp->dumpconf = g_slice_dumpconf; 258 cp = g_new_consumer(gp); 259 g_attach(cp, pp); 260 error = g_access_rel(cp, 1, 0, 0); 261 if (error) { 262 g_dettach(cp); 263 g_destroy_consumer(cp); 264 g_free(gsp->slices); 265 g_free(gp->softc); 266 g_destroy_geom(gp); 267 return (NULL); 268 }
|
269 /* Find out if there are any magic bytes on the consumer */ 270 i = sizeof gsp->cfrontstuff; 271 error = g_io_getattr("GEOM::frontstuff", cp, &i, &gsp->cfrontstuff); 272 if (error) 273 gsp->cfrontstuff = 0; |
274 *vp = gsp->softc; 275 *cpp = cp; 276 return (gp); 277} 278 279static void 280g_slice_orphan(struct g_consumer *cp) 281{ 282 struct g_geom *gp; 283 struct g_provider *pp; 284 int error; 285 286 g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name); 287 g_topology_assert(); 288 KASSERT(cp->provider->error != 0, 289 ("g_slice_orphan with error == 0")); 290 291 gp = cp->geom; 292 gp->flags |= G_GEOM_WITHER; 293 /* First prevent any new requests */ 294 error = cp->provider->error; 295 LIST_FOREACH(pp, &gp->provider, provider) 296 g_orphan_provider(pp, error); 297 298 return; 299}
|