AMD64 - Fix many compile-time warnings. int/ptr type mismatches, %llx, etc.
[dragonfly.git] / sys / dev / disk / md / md.c
blob103a4865eb0089ba2308b9fc8a50adf7086d6c42
1 /*
2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
9 * $FreeBSD: src/sys/dev/md/md.c,v 1.8.2.2 2002/08/19 17:43:34 jdp Exp $
10 * $DragonFly: src/sys/dev/disk/md/md.c,v 1.20 2008/09/07 08:09:39 swildner Exp $
14 #include "opt_md.h" /* We have adopted some tasks from MFS */
16 #include <sys/param.h>
17 #include <sys/systm.h>
18 #include <sys/buf.h>
19 #include <sys/conf.h>
20 #include <sys/devicestat.h>
21 #include <sys/disk.h>
22 #include <sys/kernel.h>
23 #include <sys/malloc.h>
24 #include <sys/sysctl.h>
25 #include <sys/linker.h>
26 #include <sys/proc.h>
27 #include <sys/buf2.h>
28 #include <sys/thread2.h>
30 #ifndef MD_NSECT
31 #define MD_NSECT (10000 * 2)
32 #endif
34 MALLOC_DEFINE(M_MD, "MD disk", "Memory Disk");
35 MALLOC_DEFINE(M_MDSECT, "MD sectors", "Memory Disk Sectors");
37 static int md_debug;
38 SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0, "");
40 #if defined(MD_ROOT) && defined(MD_ROOT_SIZE)
41 /* Image gets put here: */
42 static u_char mfs_root[MD_ROOT_SIZE*1024] = "MFS Filesystem goes here";
43 static u_char end_mfs_root[] __unused = "MFS Filesystem had better STOP here";
44 #endif
46 static int mdrootready;
48 static void mdcreate_malloc(void);
50 #define CDEV_MAJOR 95
52 static d_strategy_t mdstrategy;
53 static d_strategy_t mdstrategy_preload;
54 static d_strategy_t mdstrategy_malloc;
55 static d_open_t mdopen;
56 static d_ioctl_t mdioctl;
58 static struct dev_ops md_ops = {
59 { "md", CDEV_MAJOR, D_DISK | D_CANFREE | D_MEMDISK },
60 .d_open = mdopen,
61 .d_close = nullclose,
62 .d_read = physread,
63 .d_write = physwrite,
64 .d_ioctl = mdioctl,
65 .d_strategy = mdstrategy,
68 struct md_s {
69 int unit;
70 struct devstat stats;
71 struct bio_queue_head bio_queue;
72 struct disk disk;
73 cdev_t dev;
74 int busy;
75 enum {MD_MALLOC, MD_PRELOAD} type;
76 unsigned nsect;
78 /* MD_MALLOC related fields */
79 unsigned nsecp;
80 u_char **secp;
82 /* MD_PRELOAD related fields */
83 u_char *pl_ptr;
84 unsigned pl_len;
87 static int mdunits;
89 static int
90 mdopen(struct dev_open_args *ap)
92 cdev_t dev = ap->a_head.a_dev;
93 struct md_s *sc;
94 struct disk_info info;
96 if (md_debug)
97 kprintf("mdopen(%s %x %x)\n",
98 devtoname(dev), ap->a_oflags, ap->a_devtype);
100 sc = dev->si_drv1;
101 if (sc->unit + 1 == mdunits)
102 mdcreate_malloc();
104 bzero(&info, sizeof(info));
105 info.d_media_blksize = DEV_BSIZE; /* mandatory */
106 info.d_media_blocks = sc->nsect;
108 info.d_secpertrack = 1024; /* optional */
109 info.d_nheads = 1;
110 info.d_secpercyl = info.d_secpertrack * info.d_nheads;
111 info.d_ncylinders = (u_int)(info.d_media_blocks / info.d_secpercyl);
112 disk_setdiskinfo(&sc->disk, &info);
114 return (0);
117 static int
118 mdioctl(struct dev_ioctl_args *ap)
120 cdev_t dev = ap->a_head.a_dev;
122 if (md_debug)
123 kprintf("mdioctl(%s %lx %p %x)\n",
124 devtoname(dev), ap->a_cmd, ap->a_data, ap->a_fflag);
126 return (ENOIOCTL);
129 static int
130 mdstrategy(struct dev_strategy_args *ap)
132 cdev_t dev = ap->a_head.a_dev;
133 struct bio *bio = ap->a_bio;
134 struct buf *bp = bio->bio_buf;
135 struct md_s *sc;
137 if (md_debug > 1) {
138 kprintf("mdstrategy(%p) %s %08x, %lld, %d, %p)\n",
139 bp, devtoname(dev), bp->b_flags,
140 (long long)bio->bio_offset,
141 bp->b_bcount, bp->b_data);
143 bio->bio_driver_info = dev;
144 sc = dev->si_drv1;
145 if (sc->type == MD_MALLOC) {
146 mdstrategy_malloc(ap);
147 } else {
148 mdstrategy_preload(ap);
150 return(0);
154 static int
155 mdstrategy_malloc(struct dev_strategy_args *ap)
157 cdev_t dev = ap->a_head.a_dev;
158 struct bio *bio = ap->a_bio;
159 struct buf *bp = bio->bio_buf;
160 unsigned secno, nsec, secval, uc;
161 u_char *secp, **secpp, *dst;
162 struct md_s *sc;
163 int i;
165 if (md_debug > 1)
166 kprintf("mdstrategy_malloc(%p) %s %08xx, %lld, %d, %p)\n",
167 bp, devtoname(dev), bp->b_flags,
168 (long long)bio->bio_offset,
169 bp->b_bcount, bp->b_data);
171 sc = dev->si_drv1;
173 crit_enter();
175 bioqdisksort(&sc->bio_queue, bio);
177 if (sc->busy) {
178 crit_exit();
179 return(0);
182 sc->busy++;
184 while (1) {
185 bio = bioq_first(&sc->bio_queue);
186 if (bio == NULL) {
187 crit_exit();
188 break;
190 crit_exit();
191 bioq_remove(&sc->bio_queue, bio);
192 bp = bio->bio_buf;
194 devstat_start_transaction(&sc->stats);
196 switch (bp->b_cmd) {
197 case BUF_CMD_FREEBLKS:
198 case BUF_CMD_READ:
199 case BUF_CMD_WRITE:
200 break;
201 default:
202 panic("md: bad b_cmd %d", bp->b_cmd);
205 nsec = bp->b_bcount >> DEV_BSHIFT;
206 secno = (unsigned)(bio->bio_offset >> DEV_BSHIFT);
207 dst = bp->b_data;
208 while (nsec--) {
209 if (secno < sc->nsecp) {
210 secpp = &sc->secp[secno];
211 if ((u_int)(uintptr_t)*secpp > 255) {
212 secp = *secpp;
213 secval = 0;
214 } else {
215 secp = 0;
216 secval = (u_int)(uintptr_t)*secpp;
218 } else {
219 secpp = 0;
220 secp = 0;
221 secval = 0;
223 if (md_debug > 2)
224 kprintf("%08x %p %p %d\n", bp->b_flags, secpp, secp, secval);
226 switch (bp->b_cmd) {
227 case BUF_CMD_FREEBLKS:
228 if (secpp) {
229 if (secp)
230 FREE(secp, M_MDSECT);
231 *secpp = 0;
233 break;
234 case BUF_CMD_READ:
235 if (secp) {
236 bcopy(secp, dst, DEV_BSIZE);
237 } else if (secval) {
238 for (i = 0; i < DEV_BSIZE; i++)
239 dst[i] = secval;
240 } else {
241 bzero(dst, DEV_BSIZE);
243 break;
244 case BUF_CMD_WRITE:
245 uc = dst[0];
246 for (i = 1; i < DEV_BSIZE; i++)
247 if (dst[i] != uc)
248 break;
249 if (i == DEV_BSIZE && !uc) {
250 if (secp)
251 FREE(secp, M_MDSECT);
252 if (secpp)
253 *secpp = (u_char *)(uintptr_t)uc;
254 } else {
255 if (!secpp) {
256 MALLOC(secpp, u_char **, (secno + nsec + 1) * sizeof(u_char *), M_MD, M_WAITOK | M_ZERO);
257 bcopy(sc->secp, secpp, sc->nsecp * sizeof(u_char *));
258 FREE(sc->secp, M_MD);
259 sc->secp = secpp;
260 sc->nsecp = secno + nsec + 1;
261 secpp = &sc->secp[secno];
263 if (i == DEV_BSIZE) {
264 if (secp)
265 FREE(secp, M_MDSECT);
266 *secpp = (u_char *)(uintptr_t)uc;
267 } else {
268 if (!secp)
269 MALLOC(secp, u_char *, DEV_BSIZE, M_MDSECT, M_WAITOK);
270 bcopy(dst, secp, DEV_BSIZE);
272 *secpp = secp;
275 break;
276 default:
277 panic("md: bad b_cmd %d", bp->b_cmd);
280 secno++;
281 dst += DEV_BSIZE;
283 bp->b_resid = 0;
284 devstat_end_transaction_buf(&sc->stats, bp);
285 biodone(bio);
286 crit_enter();
288 sc->busy = 0;
289 return(0);
293 static int
294 mdstrategy_preload(struct dev_strategy_args *ap)
296 cdev_t dev = ap->a_head.a_dev;
297 struct bio *bio = ap->a_bio;
298 struct buf *bp = bio->bio_buf;
299 struct md_s *sc;
301 if (md_debug > 1)
302 kprintf("mdstrategy_preload(%p) %s %08x, %lld, %d, %p)\n",
303 bp, devtoname(dev), bp->b_flags,
304 (long long)bio->bio_offset,
305 bp->b_bcount, bp->b_data);
307 sc = dev->si_drv1;
309 crit_enter();
311 bioqdisksort(&sc->bio_queue, bio);
313 if (sc->busy) {
314 crit_exit();
315 return(0);
318 sc->busy++;
320 while (1) {
321 bio = bioq_first(&sc->bio_queue);
322 if (bio)
323 bioq_remove(&sc->bio_queue, bio);
324 crit_exit();
325 if (bio == NULL)
326 break;
328 devstat_start_transaction(&sc->stats);
330 switch (bp->b_cmd) {
331 case BUF_CMD_FREEBLKS:
332 break;
333 case BUF_CMD_READ:
334 bcopy(sc->pl_ptr + bio->bio_offset,
335 bp->b_data, bp->b_bcount);
336 break;
337 case BUF_CMD_WRITE:
338 bcopy(bp->b_data, sc->pl_ptr + bio->bio_offset,
339 bp->b_bcount);
340 break;
341 default:
342 panic("md: bad cmd %d\n", bp->b_cmd);
344 bp->b_resid = 0;
345 devstat_end_transaction_buf(&sc->stats, bp);
346 biodone(bio);
347 crit_enter();
349 sc->busy = 0;
350 return(0);
353 static struct md_s *
354 mdcreate(void)
356 struct md_s *sc;
358 MALLOC(sc, struct md_s *,sizeof(*sc), M_MD, M_WAITOK | M_ZERO);
359 sc->unit = mdunits++;
360 bioq_init(&sc->bio_queue);
361 devstat_add_entry(&sc->stats, "md", sc->unit, DEV_BSIZE,
362 DEVSTAT_NO_ORDERED_TAGS,
363 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER,
364 DEVSTAT_PRIORITY_OTHER);
365 sc->dev = disk_create(sc->unit, &sc->disk, &md_ops);
366 sc->dev->si_drv1 = sc;
367 sc->dev->si_iosize_max = DFLTPHYS;
369 return (sc);
372 static void
373 mdcreate_preload(u_char *image, unsigned length)
375 struct md_s *sc;
377 sc = mdcreate();
378 sc->type = MD_PRELOAD;
379 sc->nsect = length / DEV_BSIZE;
380 sc->pl_ptr = image;
381 sc->pl_len = length;
383 if (sc->unit == 0)
384 mdrootready = 1;
387 static void
388 mdcreate_malloc(void)
390 struct md_s *sc;
392 sc = mdcreate();
393 sc->type = MD_MALLOC;
395 sc->nsect = MD_NSECT; /* for now */
396 MALLOC(sc->secp, u_char **, sizeof(u_char *), M_MD, M_WAITOK | M_ZERO);
397 sc->nsecp = 1;
398 kprintf("md%d: Malloc disk\n", sc->unit);
401 static void
402 md_drvinit(void *unused)
405 caddr_t mod;
406 caddr_t c;
407 u_char *ptr, *name, *type;
408 unsigned len;
410 #ifdef MD_ROOT_SIZE
411 mdcreate_preload(mfs_root, MD_ROOT_SIZE*1024);
412 #endif
413 mod = NULL;
414 while ((mod = preload_search_next_name(mod)) != NULL) {
415 name = (char *)preload_search_info(mod, MODINFO_NAME);
416 type = (char *)preload_search_info(mod, MODINFO_TYPE);
417 if (name == NULL)
418 continue;
419 if (type == NULL)
420 continue;
421 if (strcmp(type, "md_image") && strcmp(type, "mfs_root"))
422 continue;
423 c = preload_search_info(mod, MODINFO_ADDR);
424 ptr = *(u_char **)c;
425 c = preload_search_info(mod, MODINFO_SIZE);
426 len = *(unsigned *)c;
427 kprintf("md%d: Preloaded image <%s> %d bytes at %p\n",
428 mdunits, name, len, ptr);
429 mdcreate_preload(ptr, len);
431 mdcreate_malloc();
434 SYSINIT(mddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR, md_drvinit,NULL)
436 #ifdef MD_ROOT
437 static void
438 md_takeroot(void *junk)
440 if (mdrootready)
441 rootdevnames[0] = "ufs:/dev/md0c";
444 SYSINIT(md_root, SI_SUB_MOUNT_ROOT, SI_ORDER_FIRST, md_takeroot, NULL);
445 #endif