]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/geom/geom_slice.c
This commit was generated by cvs2svn to compensate for changes in r131377,
[FreeBSD/FreeBSD.git] / sys / geom / geom_slice.c
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
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/bio.h>
44 #include <sys/sysctl.h>
45 #include <sys/proc.h>
46 #include <sys/kthread.h>
47 #include <sys/lock.h>
48 #include <sys/mutex.h>
49 #include <sys/errno.h>
50 #include <sys/sbuf.h>
51 #include <geom/geom.h>
52 #include <geom/geom_slice.h>
53 #include <machine/stdarg.h>
54
55 static g_orphan_t g_slice_orphan;
56 static g_access_t g_slice_access;
57 static g_start_t g_slice_start;
58
59 static struct g_slicer *
60 g_slice_alloc(unsigned nslice, unsigned scsize)
61 {
62         struct g_slicer *gsp;
63
64         gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO);
65         if (scsize > 0)
66                 gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO);
67         else
68                 gsp->softc = NULL;
69         gsp->slices = g_malloc(nslice * sizeof(struct g_slice),
70             M_WAITOK | M_ZERO);
71         gsp->nslice = nslice;
72         return (gsp);
73 }
74
75 static void
76 g_slice_free(struct g_slicer *gsp)
77 {
78
79         g_free(gsp->slices);
80         if (gsp->hotspot != NULL)
81                 g_free(gsp->hotspot);
82         g_free(gsp->softc);
83         g_free(gsp);
84 }
85
86 static int
87 g_slice_access(struct g_provider *pp, int dr, int dw, int de)
88 {
89         int error;
90         u_int u;
91         struct g_geom *gp;
92         struct g_consumer *cp;
93         struct g_provider *pp2;
94         struct g_slicer *gsp;
95         struct g_slice *gsl, *gsl2;
96
97         gp = pp->geom;
98         cp = LIST_FIRST(&gp->consumer);
99         KASSERT (cp != NULL, ("g_slice_access but no consumer"));
100         gsp = gp->softc;
101         gsl = &gsp->slices[pp->index];
102         for (u = 0; u < gsp->nslice; u++) {
103                 gsl2 = &gsp->slices[u];
104                 if (gsl2->length == 0)
105                         continue;
106                 if (u == pp->index)
107                         continue;
108                 if (gsl->offset + gsl->length <= gsl2->offset)
109                         continue;
110                 if (gsl2->offset + gsl2->length <= gsl->offset)
111                         continue;
112                 /* overlap */
113                 pp2 = gsl2->provider;
114                 if ((pp->acw + dw) > 0 && pp2->ace > 0)
115                         return (EPERM);
116                 if ((pp->ace + de) > 0 && pp2->acw > 0)
117                         return (EPERM);
118         }
119         /* On first open, grab an extra "exclusive" bit */
120         if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
121                 de++;
122         /* ... and let go of it on last close */
123         if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
124                 de--;
125         error = g_access(cp, dr, dw, de);
126         return (error);
127 }
128
129 /*
130  * XXX: It should be possible to specify here if we should finish all of the
131  * XXX: bio, or only the non-hot bits.  This would get messy if there were
132  * XXX: two hot spots in the same bio, so for now we simply finish off the
133  * XXX: entire bio.  Modifying hot data on the way to disk is frowned on
134  * XXX: so making that considerably harder is not a bad idea anyway.
135  */
136 void
137 g_slice_finish_hot(struct bio *bp)
138 {
139         struct bio *bp2;
140         struct g_geom *gp;
141         struct g_consumer *cp;
142         struct g_slicer *gsp;
143         struct g_slice *gsl;
144         int idx;
145
146         KASSERT(bp->bio_to != NULL,
147             ("NULL bio_to in g_slice_finish_hot(%p)", bp));
148         KASSERT(bp->bio_from != NULL,
149             ("NULL bio_from in g_slice_finish_hot(%p)", bp));
150         gp = bp->bio_to->geom;
151         gsp = gp->softc;
152         cp = LIST_FIRST(&gp->consumer);
153         KASSERT(cp != NULL, ("NULL consumer in g_slice_finish_hot(%p)", bp));
154         idx = bp->bio_to->index;
155         gsl = &gsp->slices[idx];
156
157         bp2 = g_clone_bio(bp);
158         if (bp2 == NULL) {
159                 g_io_deliver(bp, ENOMEM);
160                 return;
161         }
162         if (bp2->bio_offset + bp2->bio_length > gsl->length)
163                 bp2->bio_length = gsl->length - bp2->bio_offset;
164         bp2->bio_done = g_std_done;
165         bp2->bio_offset += gsl->offset;
166         g_io_request(bp2, cp);
167         return;
168 }
169
170 static void
171 g_slice_start(struct bio *bp)
172 {
173         struct bio *bp2;
174         struct g_provider *pp;
175         struct g_geom *gp;
176         struct g_consumer *cp;
177         struct g_slicer *gsp;
178         struct g_slice *gsl;
179         struct g_slice_hot *ghp;
180         int idx, error;
181         u_int m_index;
182         off_t t;
183
184         pp = bp->bio_to;
185         gp = pp->geom;
186         gsp = gp->softc;
187         cp = LIST_FIRST(&gp->consumer);
188         idx = pp->index;
189         gsl = &gsp->slices[idx];
190         switch(bp->bio_cmd) {
191         case BIO_READ:
192         case BIO_WRITE:
193         case BIO_DELETE:
194                 if (bp->bio_offset > gsl->length) {
195                         g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */
196                         return;
197                 }
198                 /*
199                  * Check if we collide with any hot spaces, and call the
200                  * method once if so.
201                  */
202                 t = bp->bio_offset + gsl->offset;
203                 for (m_index = 0; m_index < gsp->nhotspot; m_index++) {
204                         ghp = &gsp->hotspot[m_index];
205                         if (t >= ghp->offset + ghp->length)
206                                 continue;
207                         if (t + bp->bio_length <= ghp->offset)
208                                 continue;
209                         switch(bp->bio_cmd) {
210                         case BIO_READ:          idx = ghp->ract; break;
211                         case BIO_WRITE:         idx = ghp->wact; break;
212                         case BIO_DELETE:        idx = ghp->dact; break;
213                         }
214                         switch(idx) {
215                         case G_SLICE_HOT_ALLOW:
216                                 /* Fall out and continue normal processing */
217                                 continue;
218                         case G_SLICE_HOT_DENY:
219                                 g_io_deliver(bp, EROFS);
220                                 return;
221                         case G_SLICE_HOT_START:
222                                 error = gsp->start(bp);
223                                 if (error && error != EJUSTRETURN)
224                                         g_io_deliver(bp, error);
225                                 return;
226                         case G_SLICE_HOT_CALL:
227                                 error = g_post_event(gsp->hot, bp, M_NOWAIT,
228                                     gp, NULL);
229                                 if (error)
230                                         g_io_deliver(bp, error);
231                                 return;
232                         }
233                         break;
234                 }
235                 bp2 = g_clone_bio(bp);
236                 if (bp2 == NULL) {
237                         g_io_deliver(bp, ENOMEM);
238                         return;
239                 }
240                 if (bp2->bio_offset + bp2->bio_length > gsl->length)
241                         bp2->bio_length = gsl->length - bp2->bio_offset;
242                 bp2->bio_done = g_std_done;
243                 bp2->bio_offset += gsl->offset;
244                 g_io_request(bp2, cp);
245                 return;
246         case BIO_GETATTR:
247                 /* Give the real method a chance to override */
248                 if (gsp->start != NULL && gsp->start(bp))
249                         return;
250                 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
251                         struct g_kerneldump *gkd;
252
253                         gkd = (struct g_kerneldump *)bp->bio_data;
254                         gkd->offset += gsp->slices[idx].offset;
255                         if (gkd->length > gsp->slices[idx].length)
256                                 gkd->length = gsp->slices[idx].length;
257                         /* now, pass it on downwards... */
258                 }
259                 bp2 = g_clone_bio(bp);
260                 if (bp2 == NULL) {
261                         g_io_deliver(bp, ENOMEM);
262                         return;
263                 }
264                 bp2->bio_done = g_std_done;
265                 g_io_request(bp2, cp);
266                 break;
267         default:
268                 g_io_deliver(bp, EOPNOTSUPP);
269                 return;
270         }
271 }
272
273 void
274 g_slice_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
275 {
276         struct g_slicer *gsp;
277
278         gsp = gp->softc;
279         if (indent == NULL) {
280                 sbuf_printf(sb, " i %u", pp->index);
281                 sbuf_printf(sb, " o %ju", 
282                     (uintmax_t)gsp->slices[pp->index].offset);
283                 return;
284         }
285         if (pp != NULL) {
286                 sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index);
287                 sbuf_printf(sb, "%s<length>%ju</length>\n",
288                     indent, (uintmax_t)gsp->slices[pp->index].length);
289                 sbuf_printf(sb, "%s<seclength>%ju</seclength>\n", indent,
290                     (uintmax_t)gsp->slices[pp->index].length / 512);
291                 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
292                     (uintmax_t)gsp->slices[pp->index].offset);
293                 sbuf_printf(sb, "%s<secoffset>%ju</secoffset>\n", indent,
294                     (uintmax_t)gsp->slices[pp->index].offset / 512);
295         }
296 }
297
298 int
299 g_slice_config(struct g_geom *gp, u_int idx, int how, off_t offset, off_t length, u_int sectorsize, const char *fmt, ...)
300 {
301         struct g_provider *pp, *pp2;
302         struct g_slicer *gsp;
303         struct g_slice *gsl;
304         va_list ap;
305         struct sbuf *sb;
306         int acc;
307
308         g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)",
309              gp->name, idx, how);
310         g_topology_assert();
311         gsp = gp->softc;
312         if (idx >= gsp->nslice)
313                 return(EINVAL);
314         gsl = &gsp->slices[idx];
315         pp = gsl->provider;
316         if (pp != NULL)
317                 acc = pp->acr + pp->acw + pp->ace;
318         else
319                 acc = 0;
320         if (acc != 0 && how != G_SLICE_CONFIG_FORCE) {
321                 if (length < gsl->length)
322                         return(EBUSY);
323                 if (offset != gsl->offset)
324                         return(EBUSY);
325         }
326         /* XXX: check offset + length <= MEDIASIZE */
327         if (how == G_SLICE_CONFIG_CHECK)
328                 return (0);
329         gsl->length = length;
330         gsl->offset = offset;
331         gsl->sectorsize = sectorsize;
332         if (length == 0) {
333                 if (pp == NULL)
334                         return (0);
335                 if (bootverbose)
336                         printf("GEOM: Deconfigure %s\n", pp->name);
337                 g_orphan_provider(pp, ENXIO);
338                 gsl->provider = NULL;
339                 gsp->nprovider--;
340                 return (0);
341         }
342         if (pp != NULL) {
343                 if (bootverbose)
344                         printf("GEOM: Reconfigure %s, start %jd length %jd end %jd\n",
345                             pp->name, (intmax_t)offset, (intmax_t)length,
346                             (intmax_t)(offset + length - 1));
347                 pp->mediasize = gsl->length;
348                 return (0);
349         }
350         sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
351         va_start(ap, fmt);
352         sbuf_vprintf(sb, fmt, ap);
353         va_end(ap);
354         sbuf_finish(sb);
355         pp = g_new_providerf(gp, sbuf_data(sb));
356         pp2 = LIST_FIRST(&gp->consumer)->provider;
357         pp->flags = pp2->flags & G_PF_CANDELETE;
358         if (pp2->stripesize > 0) {
359                 pp->stripesize = pp2->stripesize;
360                 pp->stripeoffset = (pp2->stripeoffset + offset) % pp->stripesize;
361         }
362         if (bootverbose)
363                 printf("GEOM: Configure %s, start %jd length %jd end %jd\n",
364                     pp->name, (intmax_t)offset, (intmax_t)length,
365                     (intmax_t)(offset + length - 1));
366         pp->index = idx;
367         pp->mediasize = gsl->length;
368         pp->sectorsize = gsl->sectorsize;
369         gsl->provider = pp;
370         gsp->nprovider++;
371         g_error_provider(pp, 0);
372         sbuf_delete(sb);
373         return(0);
374 }
375
376 /*
377  * Configure "hotspots".  A hotspot is a piece of the parent device which
378  * this particular slicer cares about for some reason.  Typically because
379  * it contains meta-data used to configure the slicer.
380  * A hotspot is identified by its index number. The offset and length are
381  * relative to the parent device, and the three "?act" fields specify
382  * what action to take on BIO_READ, BIO_DELETE and BIO_WRITE.
383  *
384  * XXX: There may be a race relative to g_slice_start() here, if an existing
385  * XXX: hotspot is changed wile I/O is happening.  Should this become a problem
386  * XXX: we can protect the hotspot stuff with a mutex.
387  */
388
389 int
390 g_slice_conf_hot(struct g_geom *gp, u_int idx, off_t offset, off_t length, int ract, int dact, int wact)
391 {
392         struct g_slicer *gsp;
393         struct g_slice_hot *gsl, *gsl2;
394
395         g_trace(G_T_TOPOLOGY, "g_slice_conf_hot(%s, idx: %d, off: %jd, len: %jd)",
396             gp->name, idx, (intmax_t)offset, (intmax_t)length);
397         g_topology_assert();
398         gsp = gp->softc;
399         gsl = gsp->hotspot;
400         if(idx >= gsp->nhotspot) {
401                 gsl2 = g_malloc((idx + 1) * sizeof *gsl2, M_WAITOK | M_ZERO);
402                 if (gsp->hotspot != NULL)
403                         bcopy(gsp->hotspot, gsl2, gsp->nhotspot * sizeof *gsl2);
404                 gsp->hotspot = gsl2;
405                 if (gsp->hotspot != NULL)
406                         g_free(gsl);
407                 gsl = gsl2;
408                 gsp->nhotspot = idx + 1;
409         }
410         gsl[idx].offset = offset;
411         gsl[idx].length = length;
412         KASSERT(!((ract | dact | wact) & G_SLICE_HOT_START)
413             || gsp->start != NULL, ("G_SLICE_HOT_START but no slice->start"));
414         /* XXX: check that we _have_ a start function if HOT_START specified */
415         gsl[idx].ract = ract;
416         gsl[idx].dact = dact;
417         gsl[idx].wact = wact;
418         return (0);
419 }
420
421 void
422 g_slice_spoiled(struct g_consumer *cp)
423 {
424         struct g_geom *gp;
425         struct g_slicer *gsp;
426
427         g_topology_assert();
428         gp = cp->geom;
429         g_trace(G_T_TOPOLOGY, "g_slice_spoiled(%p/%s)", cp, gp->name);
430         gsp = gp->softc;
431         gp->softc = NULL;
432         g_slice_free(gsp);
433         g_wither_geom(gp, ENXIO);
434 }
435
436 int
437 g_slice_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
438 {
439
440         g_slice_spoiled(LIST_FIRST(&gp->consumer));
441         return (0);
442 }
443
444 struct g_geom *
445 g_slice_new(struct g_class *mp, u_int slices, struct g_provider *pp, struct g_consumer **cpp, void *extrap, int extra, g_slice_start_t *start)
446 {
447         struct g_geom *gp;
448         struct g_slicer *gsp;
449         struct g_consumer *cp;
450         void **vp;
451         int error;
452
453         g_topology_assert();
454         vp = (void **)extrap;
455         gp = g_new_geomf(mp, "%s", pp->name);
456         gsp = g_slice_alloc(slices, extra);
457         gsp->start = start;
458         gp->access = g_slice_access;
459         gp->orphan = g_slice_orphan;
460         gp->softc = gsp;
461         gp->start = g_slice_start;
462         gp->spoiled = g_slice_spoiled;
463         gp->dumpconf = g_slice_dumpconf;
464         if (gp->class->destroy_geom == NULL)
465                 gp->class->destroy_geom = g_slice_destroy_geom;
466         cp = g_new_consumer(gp);
467         error = g_attach(cp, pp);
468         if (error == 0)
469                 error = g_access(cp, 1, 0, 0);
470         if (error) {
471                 g_wither_geom(gp, ENXIO);
472                 return (NULL);
473         }
474         if (extrap != NULL)
475                 *vp = gsp->softc;
476         *cpp = cp;
477         return (gp);
478 }
479
480 static void
481 g_slice_orphan(struct g_consumer *cp)
482 {
483
484         g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name);
485         g_topology_assert();
486         KASSERT(cp->provider->error != 0,
487             ("g_slice_orphan with error == 0"));
488
489         /* XXX: Not good enough we leak the softc and its suballocations */
490         g_slice_free(cp->geom->softc);
491         g_wither_geom(cp->geom, cp->provider->error);
492 }