Deleted Added
full compact
geom_disk.c (92403) geom_disk.c (93248)
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 *
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_disk.c 92403 2002-03-16 09:24:19Z phk $
35 * $FreeBSD: head/sys/geom/geom_disk.c 93248 2002-03-26 21:40:06Z phk $
36 */
37
38#include "opt_geom.h"
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/sysctl.h>
44#include <sys/bio.h>
45#include <sys/conf.h>
46#include <sys/disk.h>
47#include <sys/malloc.h>
48#include <sys/sysctl.h>
49#include <machine/md_var.h>
50#include <sys/ctype.h>
51
52
53#include <sys/lock.h>
54#include <sys/mutex.h>
55#include <geom/geom.h>
56
57static g_access_t g_disk_access;
58
36 */
37
38#include "opt_geom.h"
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/sysctl.h>
44#include <sys/bio.h>
45#include <sys/conf.h>
46#include <sys/disk.h>
47#include <sys/malloc.h>
48#include <sys/sysctl.h>
49#include <machine/md_var.h>
50#include <sys/ctype.h>
51
52
53#include <sys/lock.h>
54#include <sys/mutex.h>
55#include <geom/geom.h>
56
57static g_access_t g_disk_access;
58
59struct g_method g_disk_method = {
60 "DISK-method",
59struct g_class g_disk_class = {
60 "DISK-class",
61 NULL,
62 g_disk_access,
63 NULL,
64 NULL,
65 { 0, 0 },
66 { 0 }
67};
68
69static int
70g_disk_access(struct g_provider *pp, int r, int w, int e)
71{
72 struct disk *dp;
73 dev_t dev;
74 int error;
75
76 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
77 pp->name, r, w, e);
78 g_topology_assert();
79 r += pp->acr;
80 w += pp->acw;
81 e += pp->ace;
82 dp = pp->geom->softc;
83 dev = dp->d_dev;
84 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
85 mtx_lock(&Giant);
86 error = devsw(dev)->d_open(dev, 3, 0, NULL);
87 if (error != 0)
88 printf("Opened disk %s -> %d\n", pp->name, error);
89 mtx_unlock(&Giant);
90 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
91 mtx_lock(&Giant);
92 error = devsw(dev)->d_close(dev, 3, 0, NULL);
93 if (error != 0)
94 printf("Closed disk %s -> %d\n", pp->name, error);
95 mtx_unlock(&Giant);
96 } else {
97 error = 0;
98 }
99 return (error);
100}
101
102static void
103g_disk_done(struct bio *bp)
104{
105
106 mtx_unlock(&Giant);
107 bp->bio_completed = bp->bio_length - bp->bio_resid;
108 g_std_done(bp);
109 mtx_lock(&Giant);
110}
111
112static void
113g_disk_start(struct bio *bp)
114{
115 struct bio *bp2;
116 dev_t dev;
117 struct disk *dp;
118 struct g_ioctl *gio;
119 int error;
120
121 dp = bp->bio_to->geom->softc;
122 dev = dp->d_dev;
123 error = 0;
124 switch(bp->bio_cmd) {
125 case BIO_READ:
126 case BIO_WRITE:
127 bp2 = g_clone_bio(bp);
128 bp2->bio_done = g_disk_done;
129 bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT;
130 bp2->bio_pblkno = bp2->bio_blkno;
131 bp2->bio_bcount = bp2->bio_length;
132 bp2->bio_dev = dev;
133 mtx_lock(&Giant);
134 devsw(dev)->d_strategy(bp2);
135 mtx_unlock(&Giant);
136 break;
137 case BIO_GETATTR:
138 if (g_haveattr_int(bp, "GEOM::sectorsize",
139 dp->d_label.d_secsize))
140 break;
141 else if (g_haveattr_int(bp, "GEOM::fwsectors",
142 dp->d_label.d_nsectors))
143 break;
144 else if (g_haveattr_int(bp, "GEOM::fwheads",
145 dp->d_label.d_ntracks))
146 break;
147 else if (g_haveattr_int(bp, "GEOM::fwcylinders",
148 dp->d_label.d_ncylinders))
149 break;
150 else if (g_haveattr_off_t(bp, "GEOM::mediasize",
151 dp->d_label.d_secsize * (off_t)dp->d_label.d_secperunit))
152 break;
153 else if (!strcmp(bp->bio_attribute, "GEOM::ioctl") &&
154 bp->bio_length == sizeof *gio) {
155 gio = (struct g_ioctl *)bp->bio_data;
156 mtx_lock(&Giant);
157 error = devsw(dev)->d_ioctl(dev, gio->cmd,
158 gio->data, gio->fflag, gio->td);
159 mtx_unlock(&Giant);
160 } else
161 error = ENOIOCTL;
162 break;
163 default:
164 error = EOPNOTSUPP;
165 break;
166 }
167 if (error) {
168 bp->bio_error = error;
169 g_io_deliver(bp);
170 }
171 return;
172}
173
174dev_t
175disk_create(int unit, struct disk *dp, int flags, struct cdevsw *cdevsw, struct cdevsw *proto)
176{
177 static int once;
178 struct g_geom *gp;
179 struct g_provider *pp;
180 dev_t dev;
181
182 mtx_unlock(&Giant);
183 if (!once) {
61 NULL,
62 g_disk_access,
63 NULL,
64 NULL,
65 { 0, 0 },
66 { 0 }
67};
68
69static int
70g_disk_access(struct g_provider *pp, int r, int w, int e)
71{
72 struct disk *dp;
73 dev_t dev;
74 int error;
75
76 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
77 pp->name, r, w, e);
78 g_topology_assert();
79 r += pp->acr;
80 w += pp->acw;
81 e += pp->ace;
82 dp = pp->geom->softc;
83 dev = dp->d_dev;
84 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
85 mtx_lock(&Giant);
86 error = devsw(dev)->d_open(dev, 3, 0, NULL);
87 if (error != 0)
88 printf("Opened disk %s -> %d\n", pp->name, error);
89 mtx_unlock(&Giant);
90 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
91 mtx_lock(&Giant);
92 error = devsw(dev)->d_close(dev, 3, 0, NULL);
93 if (error != 0)
94 printf("Closed disk %s -> %d\n", pp->name, error);
95 mtx_unlock(&Giant);
96 } else {
97 error = 0;
98 }
99 return (error);
100}
101
102static void
103g_disk_done(struct bio *bp)
104{
105
106 mtx_unlock(&Giant);
107 bp->bio_completed = bp->bio_length - bp->bio_resid;
108 g_std_done(bp);
109 mtx_lock(&Giant);
110}
111
112static void
113g_disk_start(struct bio *bp)
114{
115 struct bio *bp2;
116 dev_t dev;
117 struct disk *dp;
118 struct g_ioctl *gio;
119 int error;
120
121 dp = bp->bio_to->geom->softc;
122 dev = dp->d_dev;
123 error = 0;
124 switch(bp->bio_cmd) {
125 case BIO_READ:
126 case BIO_WRITE:
127 bp2 = g_clone_bio(bp);
128 bp2->bio_done = g_disk_done;
129 bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT;
130 bp2->bio_pblkno = bp2->bio_blkno;
131 bp2->bio_bcount = bp2->bio_length;
132 bp2->bio_dev = dev;
133 mtx_lock(&Giant);
134 devsw(dev)->d_strategy(bp2);
135 mtx_unlock(&Giant);
136 break;
137 case BIO_GETATTR:
138 if (g_haveattr_int(bp, "GEOM::sectorsize",
139 dp->d_label.d_secsize))
140 break;
141 else if (g_haveattr_int(bp, "GEOM::fwsectors",
142 dp->d_label.d_nsectors))
143 break;
144 else if (g_haveattr_int(bp, "GEOM::fwheads",
145 dp->d_label.d_ntracks))
146 break;
147 else if (g_haveattr_int(bp, "GEOM::fwcylinders",
148 dp->d_label.d_ncylinders))
149 break;
150 else if (g_haveattr_off_t(bp, "GEOM::mediasize",
151 dp->d_label.d_secsize * (off_t)dp->d_label.d_secperunit))
152 break;
153 else if (!strcmp(bp->bio_attribute, "GEOM::ioctl") &&
154 bp->bio_length == sizeof *gio) {
155 gio = (struct g_ioctl *)bp->bio_data;
156 mtx_lock(&Giant);
157 error = devsw(dev)->d_ioctl(dev, gio->cmd,
158 gio->data, gio->fflag, gio->td);
159 mtx_unlock(&Giant);
160 } else
161 error = ENOIOCTL;
162 break;
163 default:
164 error = EOPNOTSUPP;
165 break;
166 }
167 if (error) {
168 bp->bio_error = error;
169 g_io_deliver(bp);
170 }
171 return;
172}
173
174dev_t
175disk_create(int unit, struct disk *dp, int flags, struct cdevsw *cdevsw, struct cdevsw *proto)
176{
177 static int once;
178 struct g_geom *gp;
179 struct g_provider *pp;
180 dev_t dev;
181
182 mtx_unlock(&Giant);
183 if (!once) {
184 g_add_method(&g_disk_method);
184 g_add_class(&g_disk_class);
185 once++;
186 }
187 dev = g_malloc(sizeof *dev, M_WAITOK | M_ZERO);
188 dp->d_dev = dev;
189 dev->si_devsw = cdevsw;
190 dev->si_disk = dp;
191 dev->si_udev = dkmakeminor(unit, WHOLE_DISK_SLICE, RAW_PART);
192 g_topology_lock();
185 once++;
186 }
187 dev = g_malloc(sizeof *dev, M_WAITOK | M_ZERO);
188 dp->d_dev = dev;
189 dev->si_devsw = cdevsw;
190 dev->si_disk = dp;
191 dev->si_udev = dkmakeminor(unit, WHOLE_DISK_SLICE, RAW_PART);
192 g_topology_lock();
193 gp = g_new_geomf(&g_disk_method, "%s%d", cdevsw->d_name, unit);
193 gp = g_new_geomf(&g_disk_class, "%s%d", cdevsw->d_name, unit);
194 gp->start = g_disk_start;
195 gp->softc = dp;
196 dp->d_softc = gp;
197 pp = g_new_providerf(gp, "%s", gp->name);
198 g_error_provider(pp, 0);
199 g_topology_unlock();
200 mtx_lock(&Giant);
201 return (dev);
202}
203
204void disk_dev_synth(dev_t dev);
205
206void
207disk_dev_synth(dev_t dev)
208{
209
210 return;
211}
212
213void
214disk_destroy(dev_t dev)
215{
216 struct disk *dp;
217 struct g_geom *gp;
218
219 dp = dev->si_disk;
220 gp = dp->d_softc;
221 g_free(dev);
222 gp->flags |= G_GEOM_WITHER;
223 g_orphan_provider(LIST_FIRST(&gp->provider), ENXIO);
224}
225
226void
227disk_invalidate (struct disk *disk)
228{
229}
230
231int
232disk_dumpcheck(dev_t dev, u_int *count, u_int *blkno, u_int *secsize)
233{
234
235 return (ENXIO);
236}
237
238
239SYSCTL_INT(_debug_sizeof, OID_AUTO, disklabel, CTLFLAG_RD,
240 0, sizeof(struct disklabel), "sizeof(struct disklabel)");
241
242SYSCTL_INT(_debug_sizeof, OID_AUTO, diskslices, CTLFLAG_RD,
243 0, sizeof(struct diskslices), "sizeof(struct diskslices)");
244
245SYSCTL_INT(_debug_sizeof, OID_AUTO, disk, CTLFLAG_RD,
246 0, sizeof(struct disk), "sizeof(struct disk)");
194 gp->start = g_disk_start;
195 gp->softc = dp;
196 dp->d_softc = gp;
197 pp = g_new_providerf(gp, "%s", gp->name);
198 g_error_provider(pp, 0);
199 g_topology_unlock();
200 mtx_lock(&Giant);
201 return (dev);
202}
203
204void disk_dev_synth(dev_t dev);
205
206void
207disk_dev_synth(dev_t dev)
208{
209
210 return;
211}
212
213void
214disk_destroy(dev_t dev)
215{
216 struct disk *dp;
217 struct g_geom *gp;
218
219 dp = dev->si_disk;
220 gp = dp->d_softc;
221 g_free(dev);
222 gp->flags |= G_GEOM_WITHER;
223 g_orphan_provider(LIST_FIRST(&gp->provider), ENXIO);
224}
225
226void
227disk_invalidate (struct disk *disk)
228{
229}
230
231int
232disk_dumpcheck(dev_t dev, u_int *count, u_int *blkno, u_int *secsize)
233{
234
235 return (ENXIO);
236}
237
238
239SYSCTL_INT(_debug_sizeof, OID_AUTO, disklabel, CTLFLAG_RD,
240 0, sizeof(struct disklabel), "sizeof(struct disklabel)");
241
242SYSCTL_INT(_debug_sizeof, OID_AUTO, diskslices, CTLFLAG_RD,
243 0, sizeof(struct diskslices), "sizeof(struct diskslices)");
244
245SYSCTL_INT(_debug_sizeof, OID_AUTO, disk, CTLFLAG_RD,
246 0, sizeof(struct disk), "sizeof(struct disk)");