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
--- 20 unchanged lines hidden (view full) ---
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: head/sys/geom/geom_disk.c 219950 2011-03-24 08:37:48Z mav $");
38
39#include "opt_geom.h"
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/kernel.h>
44#include <sys/sysctl.h>
45#include <sys/bio.h>
--- 5 unchanged lines hidden (view full) ---
51#include <machine/md_var.h>
52
53#include <sys/lock.h>
54#include <sys/mutex.h>
55#include <geom/geom.h>
56#include <geom/geom_disk.h>
57#include <geom/geom_int.h>
58
59static struct mtx g_disk_done_mtx;
60
61static g_access_t g_disk_access;
62static g_init_t g_disk_init;
63static g_fini_t g_disk_fini;
64static g_start_t g_disk_start;
65static g_ioctl_t g_disk_ioctl;
66static g_dumpconf_t g_disk_dumpconf;
--- 4 unchanged lines hidden (view full) ---
71 .init = g_disk_init,
72 .fini = g_disk_fini,
73 .start = g_disk_start,
74 .access = g_disk_access,
75 .ioctl = g_disk_ioctl,
76 .dumpconf = g_disk_dumpconf,
77};
78
79static void
80g_disk_init(struct g_class *mp __unused)
81{
82
83 mtx_init(&g_disk_done_mtx, "g_disk_done", NULL, MTX_DEF);
84}
85
86static void
--- 18 unchanged lines hidden (view full) ---
105 if (dp->d_flags & DISKFLAG_NEEDSGIANT)
106 mtx_unlock(&Giant);
107}
108
109static int
110g_disk_access(struct g_provider *pp, int r, int w, int e)
111{
112 struct disk *dp;
113 int error;
114
115 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
116 pp->name, r, w, e);
117 g_topology_assert();
118 dp = pp->geom->softc;
119 if (dp == NULL || dp->d_destroyed) {
120 /*
121 * Allow decreasing access count even if disk is not
122 * avaliable anymore.
123 */
124 if (r <= 0 && w <= 0 && e <= 0)
125 return (0);
126 return (ENXIO);
127 }
--- 22 unchanged lines hidden (view full) ---
150 if (dp->d_close != NULL) {
151 g_disk_lock_giant(dp);
152 error = dp->d_close(dp);
153 if (error != 0)
154 printf("Closed disk %s -> %d\n",
155 pp->name, error);
156 g_disk_unlock_giant(dp);
157 }
158 dp->d_flags &= ~DISKFLAG_OPEN;
159 }
160 return (error);
161}
162
163static void
164g_disk_kerneldump(struct bio *bp, struct disk *dp)
165{
--- 15 unchanged lines hidden (view full) ---
181 gkd->di.mediaoffset = gkd->offset;
182 if ((gkd->offset + gkd->length) > dp->d_mediasize)
183 gkd->length = dp->d_mediasize - gkd->offset;
184 gkd->di.mediasize = gkd->length;
185 g_io_deliver(bp, 0);
186}
187
188static void
189g_disk_done(struct bio *bp)
190{
191 struct bio *bp2;
192 struct disk *dp;
193
194 /* See "notes" for why we need a mutex here */
195 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
196 mtx_lock(&g_disk_done_mtx);
197 bp->bio_completed = bp->bio_length - bp->bio_resid;
198
199 bp2 = bp->bio_parent;
200 if (bp2->bio_error == 0)
201 bp2->bio_error = bp->bio_error;
202 bp2->bio_completed += bp->bio_completed;
203 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) &&
204 (dp = bp2->bio_to->geom->softc)) {
205 devstat_end_transaction_bio(dp->d_devstat, bp);
206 }
207 g_destroy_bio(bp);
208 bp2->bio_inbed++;
209 if (bp2->bio_children == bp2->bio_inbed) {
210 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
211 g_io_deliver(bp2, bp2->bio_error);
212 }
213 mtx_unlock(&g_disk_done_mtx);
214}
215
216static int
217g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td)
218{
219 struct g_geom *gp;
220 struct disk *dp;
221 int error;
222
223 gp = pp->geom;
224 dp = gp->softc;
225
226 if (dp->d_ioctl == NULL)
227 return (ENOIOCTL);
228 g_disk_lock_giant(dp);
229 error = dp->d_ioctl(dp, cmd, data, fflag, td);
230 g_disk_unlock_giant(dp);
231 return(error);
232}
233
234static void
235g_disk_start(struct bio *bp)
236{
237 struct bio *bp2, *bp3;
238 struct disk *dp;
239 int error;
240 off_t off;
241
242 dp = bp->bio_to->geom->softc;
243 if (dp == NULL || dp->d_destroyed) {
244 g_io_deliver(bp, ENXIO);
245 return;
246 }
247 error = EJUSTRETURN;
248 switch(bp->bio_cmd) {
249 case BIO_DELETE:
250 if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
251 error = 0;
--- 62 unchanged lines hidden (view full) ---
314 else if (g_handleattr(bp, "GEOM::hba_subvendor",
315 &dp->d_hba_subvendor, 2))
316 break;
317 else if (g_handleattr(bp, "GEOM::hba_subdevice",
318 &dp->d_hba_subdevice, 2))
319 break;
320 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
321 g_disk_kerneldump(bp, dp);
322 else
323 error = ENOIOCTL;
324 break;
325 case BIO_FLUSH:
326 g_trace(G_T_TOPOLOGY, "g_disk_flushcache(%s)",
327 bp->bio_to->name);
328 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) {
329 g_io_deliver(bp, ENODEV);
--- 18 unchanged lines hidden (view full) ---
348 g_io_deliver(bp, error);
349 return;
350}
351
352static void
353g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
354{
355 struct disk *dp;
356
357 dp = gp->softc;
358 if (dp == NULL)
359 return;
360 if (indent == NULL) {
361 sbuf_printf(sb, " hd %u", dp->d_fwheads);
362 sbuf_printf(sb, " sc %u", dp->d_fwsectors);
363 return;
364 }
365 if (pp != NULL) {
366 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
--- 6 unchanged lines hidden (view full) ---
373}
374
375static void
376g_disk_create(void *arg, int flag)
377{
378 struct g_geom *gp;
379 struct g_provider *pp;
380 struct disk *dp;
381
382 if (flag == EV_CANCEL)
383 return;
384 g_topology_assert();
385 dp = arg;
386 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
387 gp->softc = dp;
388 pp = g_new_providerf(gp, "%s", gp->name);
389 pp->mediasize = dp->d_mediasize;
390 pp->sectorsize = dp->d_sectorsize;
391 if (dp->d_flags & DISKFLAG_CANDELETE)
392 pp->flags |= G_PF_CANDELETE;
393 pp->stripeoffset = dp->d_stripeoffset;
394 pp->stripesize = dp->d_stripesize;
395 if (bootverbose)
396 printf("GEOM: new disk %s\n", gp->name);
397 dp->d_geom = gp;
398 g_error_provider(pp, 0);
399}
400
401static void
402g_disk_destroy(void *ptr, int flag)
403{
404 struct disk *dp;
405 struct g_geom *gp;
406
407 g_topology_assert();
408 dp = ptr;
409 gp = dp->d_geom;
410 if (gp != NULL) {
411 gp->softc = NULL;
412 g_wither_geom(gp, ENXIO);
413 }
414 g_free(dp);
415}
416
417/*
418 * We only allow printable characters in disk ident,
--- 113 unchanged lines hidden ---
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
--- 20 unchanged lines hidden (view full) ---
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: head/sys/geom/geom_disk.c 219950 2011-03-24 08:37:48Z mav $");
38
39#include "opt_geom.h"
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/kernel.h>
44#include <sys/sysctl.h>
45#include <sys/bio.h>
--- 5 unchanged lines hidden (view full) ---
51#include <machine/md_var.h>
52
53#include <sys/lock.h>
54#include <sys/mutex.h>
55#include <geom/geom.h>
56#include <geom/geom_disk.h>
57#include <geom/geom_int.h>
58
59static struct mtx g_disk_done_mtx;
60
61static g_access_t g_disk_access;
62static g_init_t g_disk_init;
63static g_fini_t g_disk_fini;
64static g_start_t g_disk_start;
65static g_ioctl_t g_disk_ioctl;
66static g_dumpconf_t g_disk_dumpconf;
--- 4 unchanged lines hidden (view full) ---
71 .init = g_disk_init,
72 .fini = g_disk_fini,
73 .start = g_disk_start,
74 .access = g_disk_access,
75 .ioctl = g_disk_ioctl,
76 .dumpconf = g_disk_dumpconf,
77};
78
79static void
80g_disk_init(struct g_class *mp __unused)
81{
82
83 mtx_init(&g_disk_done_mtx, "g_disk_done", NULL, MTX_DEF);
84}
85
86static void
--- 18 unchanged lines hidden (view full) ---
105 if (dp->d_flags & DISKFLAG_NEEDSGIANT)
106 mtx_unlock(&Giant);
107}
108
109static int
110g_disk_access(struct g_provider *pp, int r, int w, int e)
111{
112 struct disk *dp;
113 int error;
114
115 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
116 pp->name, r, w, e);
117 g_topology_assert();
118 dp = pp->geom->softc;
119 if (dp == NULL || dp->d_destroyed) {
120 /*
121 * Allow decreasing access count even if disk is not
122 * avaliable anymore.
123 */
124 if (r <= 0 && w <= 0 && e <= 0)
125 return (0);
126 return (ENXIO);
127 }
--- 22 unchanged lines hidden (view full) ---
150 if (dp->d_close != NULL) {
151 g_disk_lock_giant(dp);
152 error = dp->d_close(dp);
153 if (error != 0)
154 printf("Closed disk %s -> %d\n",
155 pp->name, error);
156 g_disk_unlock_giant(dp);
157 }
158 dp->d_flags &= ~DISKFLAG_OPEN;
159 }
160 return (error);
161}
162
163static void
164g_disk_kerneldump(struct bio *bp, struct disk *dp)
165{
--- 15 unchanged lines hidden (view full) ---
181 gkd->di.mediaoffset = gkd->offset;
182 if ((gkd->offset + gkd->length) > dp->d_mediasize)
183 gkd->length = dp->d_mediasize - gkd->offset;
184 gkd->di.mediasize = gkd->length;
185 g_io_deliver(bp, 0);
186}
187
188static void
189g_disk_done(struct bio *bp)
190{
191 struct bio *bp2;
192 struct disk *dp;
193
194 /* See "notes" for why we need a mutex here */
195 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
196 mtx_lock(&g_disk_done_mtx);
197 bp->bio_completed = bp->bio_length - bp->bio_resid;
198
199 bp2 = bp->bio_parent;
200 if (bp2->bio_error == 0)
201 bp2->bio_error = bp->bio_error;
202 bp2->bio_completed += bp->bio_completed;
203 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) &&
204 (dp = bp2->bio_to->geom->softc)) {
205 devstat_end_transaction_bio(dp->d_devstat, bp);
206 }
207 g_destroy_bio(bp);
208 bp2->bio_inbed++;
209 if (bp2->bio_children == bp2->bio_inbed) {
210 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
211 g_io_deliver(bp2, bp2->bio_error);
212 }
213 mtx_unlock(&g_disk_done_mtx);
214}
215
216static int
217g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td)
218{
219 struct g_geom *gp;
220 struct disk *dp;
221 int error;
222
223 gp = pp->geom;
224 dp = gp->softc;
225
226 if (dp->d_ioctl == NULL)
227 return (ENOIOCTL);
228 g_disk_lock_giant(dp);
229 error = dp->d_ioctl(dp, cmd, data, fflag, td);
230 g_disk_unlock_giant(dp);
231 return(error);
232}
233
234static void
235g_disk_start(struct bio *bp)
236{
237 struct bio *bp2, *bp3;
238 struct disk *dp;
239 int error;
240 off_t off;
241
242 dp = bp->bio_to->geom->softc;
243 if (dp == NULL || dp->d_destroyed) {
244 g_io_deliver(bp, ENXIO);
245 return;
246 }
247 error = EJUSTRETURN;
248 switch(bp->bio_cmd) {
249 case BIO_DELETE:
250 if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
251 error = 0;
--- 62 unchanged lines hidden (view full) ---
314 else if (g_handleattr(bp, "GEOM::hba_subvendor",
315 &dp->d_hba_subvendor, 2))
316 break;
317 else if (g_handleattr(bp, "GEOM::hba_subdevice",
318 &dp->d_hba_subdevice, 2))
319 break;
320 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
321 g_disk_kerneldump(bp, dp);
322 else
323 error = ENOIOCTL;
324 break;
325 case BIO_FLUSH:
326 g_trace(G_T_TOPOLOGY, "g_disk_flushcache(%s)",
327 bp->bio_to->name);
328 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) {
329 g_io_deliver(bp, ENODEV);
--- 18 unchanged lines hidden (view full) ---
348 g_io_deliver(bp, error);
349 return;
350}
351
352static void
353g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
354{
355 struct disk *dp;
356
357 dp = gp->softc;
358 if (dp == NULL)
359 return;
360 if (indent == NULL) {
361 sbuf_printf(sb, " hd %u", dp->d_fwheads);
362 sbuf_printf(sb, " sc %u", dp->d_fwsectors);
363 return;
364 }
365 if (pp != NULL) {
366 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
--- 6 unchanged lines hidden (view full) ---
373}
374
375static void
376g_disk_create(void *arg, int flag)
377{
378 struct g_geom *gp;
379 struct g_provider *pp;
380 struct disk *dp;
381
382 if (flag == EV_CANCEL)
383 return;
384 g_topology_assert();
385 dp = arg;
386 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
387 gp->softc = dp;
388 pp = g_new_providerf(gp, "%s", gp->name);
389 pp->mediasize = dp->d_mediasize;
390 pp->sectorsize = dp->d_sectorsize;
391 if (dp->d_flags & DISKFLAG_CANDELETE)
392 pp->flags |= G_PF_CANDELETE;
393 pp->stripeoffset = dp->d_stripeoffset;
394 pp->stripesize = dp->d_stripesize;
395 if (bootverbose)
396 printf("GEOM: new disk %s\n", gp->name);
397 dp->d_geom = gp;
398 g_error_provider(pp, 0);
399}
400
401static void
402g_disk_destroy(void *ptr, int flag)
403{
404 struct disk *dp;
405 struct g_geom *gp;
406
407 g_topology_assert();
408 dp = ptr;
409 gp = dp->d_geom;
410 if (gp != NULL) {
411 gp->softc = NULL;
412 g_wither_geom(gp, ENXIO);
413 }
414 g_free(dp);
415}
416
417/*
418 * We only allow printable characters in disk ident,
--- 113 unchanged lines hidden ---