]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/geom/geom_disk.c
- Check flag with the bitwise operator, not the logical operator.
[FreeBSD/FreeBSD.git] / sys / geom / geom_disk.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 "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>
46 #include <sys/conf.h>
47 #include <sys/ctype.h>
48 #include <sys/fcntl.h>
49 #include <sys/malloc.h>
50 #include <sys/sysctl.h>
51 #include <sys/devicestat.h>
52 #include <machine/md_var.h>
53
54 #include <sys/lock.h>
55 #include <sys/mutex.h>
56 #include <geom/geom.h>
57 #include <geom/geom_disk.h>
58 #include <geom/geom_int.h>
59
60 static struct mtx g_disk_done_mtx;
61
62 static g_access_t g_disk_access;
63 static g_init_t g_disk_init;
64 static g_fini_t g_disk_fini;
65 static g_start_t g_disk_start;
66 static g_ioctl_t g_disk_ioctl;
67 static g_dumpconf_t g_disk_dumpconf;
68
69 static struct g_class g_disk_class = {
70         .name = "DISK",
71         .version = G_VERSION,
72         .init = g_disk_init,
73         .fini = g_disk_fini,
74         .start = g_disk_start,
75         .access = g_disk_access,
76         .ioctl = g_disk_ioctl,
77         .dumpconf = g_disk_dumpconf,
78 };
79
80 static void
81 g_disk_init(struct g_class *mp __unused)
82 {
83
84         mtx_init(&g_disk_done_mtx, "g_disk_done", NULL, MTX_DEF);
85 }
86
87 static void
88 g_disk_fini(struct g_class *mp __unused)
89 {
90
91         mtx_destroy(&g_disk_done_mtx);
92 }
93
94 DECLARE_GEOM_CLASS(g_disk_class, g_disk);
95
96 static void __inline
97 g_disk_lock_giant(struct disk *dp)
98 {
99         if (dp->d_flags & DISKFLAG_NEEDSGIANT)
100                 mtx_lock(&Giant);
101 }
102
103 static void __inline
104 g_disk_unlock_giant(struct disk *dp)
105 {
106         if (dp->d_flags & DISKFLAG_NEEDSGIANT)
107                 mtx_unlock(&Giant);
108 }
109
110 static int
111 g_disk_access(struct g_provider *pp, int r, int w, int e)
112 {
113         struct disk *dp;
114         int error;
115
116         g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
117             pp->name, r, w, e);
118         g_topology_assert();
119         dp = pp->geom->softc;
120         if (dp == NULL || dp->d_destroyed) {
121                 /*
122                  * Allow decreasing access count even if disk is not
123                  * avaliable anymore.
124                  */
125                 if (r <= 0 && w <= 0 && e <= 0)
126                         return (0);
127                 return (ENXIO);
128         }
129         r += pp->acr;
130         w += pp->acw;
131         e += pp->ace;
132         error = 0;
133         if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
134                 if (dp->d_open != NULL) {
135                         g_disk_lock_giant(dp);
136                         error = dp->d_open(dp);
137                         if (bootverbose && error != 0)
138                                 printf("Opened disk %s -> %d\n",
139                                     pp->name, error);
140                         g_disk_unlock_giant(dp);
141                 }
142                 pp->mediasize = dp->d_mediasize;
143                 pp->sectorsize = dp->d_sectorsize;
144                 dp->d_flags |= DISKFLAG_OPEN;
145                 if (dp->d_maxsize == 0) {
146                         printf("WARNING: Disk drive %s%d has no d_maxsize\n",
147                             dp->d_name, dp->d_unit);
148                         dp->d_maxsize = DFLTPHYS;
149                 }
150         } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
151                 if (dp->d_close != NULL) {
152                         g_disk_lock_giant(dp);
153                         error = dp->d_close(dp);
154                         if (error != 0)
155                                 printf("Closed disk %s -> %d\n",
156                                     pp->name, error);
157                         g_disk_unlock_giant(dp);
158                 }
159                 dp->d_flags &= ~DISKFLAG_OPEN;
160         }
161         return (error);
162 }
163
164 static void
165 g_disk_kerneldump(struct bio *bp, struct disk *dp)
166
167         int error;
168         struct g_kerneldump *gkd;
169         struct dumperinfo di;
170         struct g_geom *gp;
171
172         gkd = (struct g_kerneldump*)bp->bio_data;
173         gp = bp->bio_to->geom;
174         g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)",
175                 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
176         if (dp->d_dump == NULL) {
177                 g_io_deliver(bp, ENODEV);
178                 return;
179         }
180         di.dumper = dp->d_dump;
181         di.priv = dp;
182         di.blocksize = dp->d_sectorsize;
183         di.maxiosize = dp->d_maxsize;
184         di.mediaoffset = gkd->offset;
185         if ((gkd->offset + gkd->length) > dp->d_mediasize)
186                 gkd->length = dp->d_mediasize - gkd->offset;
187         di.mediasize = gkd->length;
188         error = set_dumper(&di);
189         g_io_deliver(bp, error);
190 }
191
192 static void
193 g_disk_done(struct bio *bp)
194 {
195         struct bio *bp2;
196         struct disk *dp;
197
198         /* See "notes" for why we need a mutex here */
199         /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
200         mtx_lock(&g_disk_done_mtx);
201         bp->bio_completed = bp->bio_length - bp->bio_resid;
202
203         bp2 = bp->bio_parent;
204         if (bp2->bio_error == 0)
205                 bp2->bio_error = bp->bio_error;
206         bp2->bio_completed += bp->bio_completed;
207         if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) &&
208             (dp = bp2->bio_to->geom->softc)) {
209                 devstat_end_transaction_bio(dp->d_devstat, bp);
210         }
211         g_destroy_bio(bp);
212         bp2->bio_inbed++;
213         if (bp2->bio_children == bp2->bio_inbed) {
214                 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
215                 g_io_deliver(bp2, bp2->bio_error);
216         }
217         mtx_unlock(&g_disk_done_mtx);
218 }
219
220 static int
221 g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td)
222 {
223         struct g_geom *gp;
224         struct disk *dp;
225         int error;
226
227         gp = pp->geom;
228         dp = gp->softc;
229
230         if (dp->d_ioctl == NULL)
231                 return (ENOIOCTL);
232         g_disk_lock_giant(dp);
233         error = dp->d_ioctl(dp, cmd, data, fflag, td);
234         g_disk_unlock_giant(dp);
235         return(error);
236 }
237
238 static void
239 g_disk_start(struct bio *bp)
240 {
241         struct bio *bp2, *bp3;
242         struct disk *dp;
243         int error;
244         off_t off;
245
246         dp = bp->bio_to->geom->softc;
247         if (dp == NULL || dp->d_destroyed) {
248                 g_io_deliver(bp, ENXIO);
249                 return;
250         }
251         error = EJUSTRETURN;
252         switch(bp->bio_cmd) {
253         case BIO_DELETE:
254                 if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
255                         error = 0;
256                         break;
257                 }
258                 /* fall-through */
259         case BIO_READ:
260         case BIO_WRITE:
261                 off = 0;
262                 bp3 = NULL;
263                 bp2 = g_clone_bio(bp);
264                 if (bp2 == NULL) {
265                         error = ENOMEM;
266                         break;
267                 }
268                 do {
269                         bp2->bio_offset += off;
270                         bp2->bio_length -= off;
271                         bp2->bio_data += off;
272                         if (bp2->bio_length > dp->d_maxsize) {
273                                 /*
274                                  * XXX: If we have a stripesize we should really
275                                  * use it here.
276                                  */
277                                 bp2->bio_length = dp->d_maxsize;
278                                 off += dp->d_maxsize;
279                                 /*
280                                  * To avoid a race, we need to grab the next bio
281                                  * before we schedule this one.  See "notes".
282                                  */
283                                 bp3 = g_clone_bio(bp);
284                                 if (bp3 == NULL)
285                                         bp->bio_error = ENOMEM;
286                         }
287                         bp2->bio_done = g_disk_done;
288                         bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
289                         bp2->bio_bcount = bp2->bio_length;
290                         bp2->bio_disk = dp;
291                         devstat_start_transaction_bio(dp->d_devstat, bp2);
292                         g_disk_lock_giant(dp);
293                         dp->d_strategy(bp2);
294                         g_disk_unlock_giant(dp);
295                         bp2 = bp3;
296                         bp3 = NULL;
297                 } while (bp2 != NULL);
298                 break;
299         case BIO_GETATTR:
300                 if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors))
301                         break;
302                 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
303                         break;
304                 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
305                         break;
306                 else if (g_handleattr_str(bp, "GEOM::ident", dp->d_ident))
307                         break;
308                 else if (g_handleattr(bp, "GEOM::hba_vendor",
309                     &dp->d_hba_vendor, 2))
310                         break;
311                 else if (g_handleattr(bp, "GEOM::hba_device",
312                     &dp->d_hba_device, 2))
313                         break;
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);
330                         return;
331                 }
332                 bp2 = g_clone_bio(bp);
333                 if (bp2 == NULL) {
334                         g_io_deliver(bp, ENOMEM);
335                         return;
336                 }
337                 bp2->bio_done = g_disk_done;
338                 bp2->bio_disk = dp;
339                 g_disk_lock_giant(dp);
340                 dp->d_strategy(bp2);
341                 g_disk_unlock_giant(dp);
342                 break;
343         default:
344                 error = EOPNOTSUPP;
345                 break;
346         }
347         if (error != EJUSTRETURN)
348                 g_io_deliver(bp, error);
349         return;
350 }
351
352 static void
353 g_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",
367                     indent, dp->d_fwheads);
368                 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
369                     indent, dp->d_fwsectors);
370         }
371 }
372
373 static void
374 g_disk_create(void *arg, int flag)
375 {
376         struct g_geom *gp;
377         struct g_provider *pp;
378         struct disk *dp;
379
380         if (flag == EV_CANCEL)
381                 return;
382         g_topology_assert();
383         dp = arg;
384         gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
385         gp->softc = dp;
386         pp = g_new_providerf(gp, "%s", gp->name);
387         pp->mediasize = dp->d_mediasize;
388         pp->sectorsize = dp->d_sectorsize;
389         if (dp->d_flags & DISKFLAG_CANDELETE)
390                 pp->flags |= G_PF_CANDELETE;
391         pp->stripeoffset = dp->d_stripeoffset;
392         pp->stripesize = dp->d_stripesize;
393         if (bootverbose)
394                 printf("GEOM: new disk %s\n", gp->name);
395         dp->d_geom = gp;
396         g_error_provider(pp, 0);
397 }
398
399 static void
400 g_disk_destroy(void *ptr, int flag)
401 {
402         struct disk *dp;
403         struct g_geom *gp;
404
405         g_topology_assert();
406         dp = ptr;
407         gp = dp->d_geom;
408         if (gp != NULL) {
409                 gp->softc = NULL;
410                 g_wither_geom(gp, ENXIO);
411         }
412         g_free(dp);
413 }
414
415 /*
416  * We only allow printable characters in disk ident,
417  * the rest is converted to 'x<HH>'.
418  */
419 static void
420 g_disk_ident_adjust(char *ident, size_t size)
421 {
422         char *p, tmp[4], newid[DISK_IDENT_SIZE];
423
424         newid[0] = '\0';
425         for (p = ident; *p != '\0'; p++) {
426                 if (isprint(*p)) {
427                         tmp[0] = *p;
428                         tmp[1] = '\0';
429                 } else {
430                         snprintf(tmp, sizeof(tmp), "x%02hhx",
431                             *(unsigned char *)p);
432                 }
433                 if (strlcat(newid, tmp, sizeof(newid)) >= sizeof(newid))
434                         break;
435         }
436         bzero(ident, size);
437         strlcpy(ident, newid, size);
438 }
439
440 struct disk *
441 disk_alloc()
442 {
443         struct disk *dp;
444
445         dp = g_malloc(sizeof *dp, M_WAITOK | M_ZERO);
446         return (dp);
447 }
448
449 void
450 disk_create(struct disk *dp, int version)
451 {
452         if (version != DISK_VERSION_00 && version != DISK_VERSION_01) {
453                 printf("WARNING: Attempt to add disk %s%d %s",
454                     dp->d_name, dp->d_unit,
455                     " using incompatible ABI version of disk(9)\n");
456                 printf("WARNING: Ignoring disk %s%d\n",
457                     dp->d_name, dp->d_unit);
458                 return;
459         }
460         KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
461         KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
462         KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
463         KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
464         if (dp->d_devstat == NULL)
465                 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
466                     dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
467                     DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
468         dp->d_geom = NULL;
469         g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident));
470         g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
471 }
472
473 void
474 disk_destroy(struct disk *dp)
475 {
476
477         g_cancel_event(dp);
478         dp->d_destroyed = 1;
479         if (dp->d_devstat != NULL)
480                 devstat_remove_entry(dp->d_devstat);
481         g_post_event(g_disk_destroy, dp, M_WAITOK, NULL);
482 }
483
484 void
485 disk_gone(struct disk *dp)
486 {
487         struct g_geom *gp;
488         struct g_provider *pp;
489
490         gp = dp->d_geom;
491         if (gp != NULL)
492                 LIST_FOREACH(pp, &gp->provider, provider)
493                         g_wither_provider(pp, ENXIO);
494 }
495
496 static void
497 g_kern_disks(void *p, int flag __unused)
498 {
499         struct sbuf *sb;
500         struct g_geom *gp;
501         char *sp;
502
503         sb = p;
504         sp = "";
505         g_topology_assert();
506         LIST_FOREACH(gp, &g_disk_class.geom, geom) {
507                 sbuf_printf(sb, "%s%s", sp, gp->name);
508                 sp = " ";
509         }
510         sbuf_finish(sb);
511 }
512
513 static int
514 sysctl_disks(SYSCTL_HANDLER_ARGS)
515 {
516         int error;
517         struct sbuf *sb;
518
519         sb = sbuf_new_auto();
520         g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
521         error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
522         sbuf_delete(sb);
523         return error;
524 }
525  
526 SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0, 
527     sysctl_disks, "A", "names of available disks");
528