AMD64 - Fix many compile-time warnings. int/ptr type mismatches, %llx, etc.
[dragonfly.git] / sys / dev / disk / md / md.c
CommitLineData
984263bc
MD
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 * ----------------------------------------------------------------------------
8 *
9 * $FreeBSD: src/sys/dev/md/md.c,v 1.8.2.2 2002/08/19 17:43:34 jdp Exp $
8b6f36f5 10 * $DragonFly: src/sys/dev/disk/md/md.c,v 1.20 2008/09/07 08:09:39 swildner Exp $
984263bc
MD
11 *
12 */
13
984263bc
MD
14#include "opt_md.h" /* We have adopted some tasks from MFS */
15
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>
3020e3be
MD
26#include <sys/proc.h>
27#include <sys/buf2.h>
fbe276c7 28#include <sys/thread2.h>
984263bc
MD
29
30#ifndef MD_NSECT
31#define MD_NSECT (10000 * 2)
32#endif
33
34MALLOC_DEFINE(M_MD, "MD disk", "Memory Disk");
35MALLOC_DEFINE(M_MDSECT, "MD sectors", "Memory Disk Sectors");
36
37static int md_debug;
38SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0, "");
39
984263bc
MD
40#if defined(MD_ROOT) && defined(MD_ROOT_SIZE)
41/* Image gets put here: */
42static u_char mfs_root[MD_ROOT_SIZE*1024] = "MFS Filesystem goes here";
43static u_char end_mfs_root[] __unused = "MFS Filesystem had better STOP here";
44#endif
45
46static int mdrootready;
47
48static void mdcreate_malloc(void);
49
50#define CDEV_MAJOR 95
984263bc
MD
51
52static d_strategy_t mdstrategy;
53static d_strategy_t mdstrategy_preload;
54static d_strategy_t mdstrategy_malloc;
55static d_open_t mdopen;
56static d_ioctl_t mdioctl;
57
fef8985e
MD
58static 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,
984263bc
MD
66};
67
984263bc
MD
68struct md_s {
69 int unit;
70 struct devstat stats;
81b5c339 71 struct bio_queue_head bio_queue;
984263bc 72 struct disk disk;
b13267a5 73 cdev_t dev;
984263bc
MD
74 int busy;
75 enum {MD_MALLOC, MD_PRELOAD} type;
76 unsigned nsect;
77
78 /* MD_MALLOC related fields */
79 unsigned nsecp;
80 u_char **secp;
81
82 /* MD_PRELOAD related fields */
83 u_char *pl_ptr;
84 unsigned pl_len;
85};
86
87static int mdunits;
88
89static int
fef8985e 90mdopen(struct dev_open_args *ap)
984263bc 91{
b13267a5 92 cdev_t dev = ap->a_head.a_dev;
984263bc 93 struct md_s *sc;
a688b15c 94 struct disk_info info;
984263bc
MD
95
96 if (md_debug)
e3869ec7 97 kprintf("mdopen(%s %x %x)\n",
fef8985e 98 devtoname(dev), ap->a_oflags, ap->a_devtype);
984263bc
MD
99
100 sc = dev->si_drv1;
101 if (sc->unit + 1 == mdunits)
102 mdcreate_malloc();
103
a688b15c
MD
104 bzero(&info, sizeof(info));
105 info.d_media_blksize = DEV_BSIZE; /* mandatory */
106 info.d_media_blocks = sc->nsect;
107
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);
113
984263bc
MD
114 return (0);
115}
116
117static int
fef8985e 118mdioctl(struct dev_ioctl_args *ap)
984263bc 119{
b13267a5 120 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
121
122 if (md_debug)
e3869ec7 123 kprintf("mdioctl(%s %lx %p %x)\n",
fef8985e 124 devtoname(dev), ap->a_cmd, ap->a_data, ap->a_fflag);
984263bc
MD
125
126 return (ENOIOCTL);
127}
128
fef8985e
MD
129static int
130mdstrategy(struct dev_strategy_args *ap)
984263bc 131{
b13267a5 132 cdev_t dev = ap->a_head.a_dev;
fef8985e 133 struct bio *bio = ap->a_bio;
81b5c339 134 struct buf *bp = bio->bio_buf;
984263bc
MD
135 struct md_s *sc;
136
81b5c339 137 if (md_debug > 1) {
e3869ec7 138 kprintf("mdstrategy(%p) %s %08x, %lld, %d, %p)\n",
973c11b9
MD
139 bp, devtoname(dev), bp->b_flags,
140 (long long)bio->bio_offset,
54078292 141 bp->b_bcount, bp->b_data);
81b5c339
MD
142 }
143 bio->bio_driver_info = dev;
144 sc = dev->si_drv1;
984263bc 145 if (sc->type == MD_MALLOC) {
fef8985e 146 mdstrategy_malloc(ap);
984263bc 147 } else {
fef8985e 148 mdstrategy_preload(ap);
984263bc 149 }
fef8985e 150 return(0);
984263bc
MD
151}
152
153
fef8985e
MD
154static int
155mdstrategy_malloc(struct dev_strategy_args *ap)
984263bc 156{
b13267a5 157 cdev_t dev = ap->a_head.a_dev;
fef8985e 158 struct bio *bio = ap->a_bio;
81b5c339 159 struct buf *bp = bio->bio_buf;
984263bc 160 unsigned secno, nsec, secval, uc;
81b5c339 161 u_char *secp, **secpp, *dst;
81b5c339
MD
162 struct md_s *sc;
163 int i;
984263bc
MD
164
165 if (md_debug > 1)
e3869ec7 166 kprintf("mdstrategy_malloc(%p) %s %08xx, %lld, %d, %p)\n",
973c11b9
MD
167 bp, devtoname(dev), bp->b_flags,
168 (long long)bio->bio_offset,
54078292 169 bp->b_bcount, bp->b_data);
984263bc 170
81b5c339 171 sc = dev->si_drv1;
984263bc 172
fbe276c7 173 crit_enter();
984263bc 174
81b5c339 175 bioqdisksort(&sc->bio_queue, bio);
984263bc
MD
176
177 if (sc->busy) {
fbe276c7 178 crit_exit();
fef8985e 179 return(0);
984263bc
MD
180 }
181
182 sc->busy++;
183
184 while (1) {
81b5c339 185 bio = bioq_first(&sc->bio_queue);
31a163d2
MD
186 if (bio == NULL) {
187 crit_exit();
984263bc 188 break;
31a163d2
MD
189 }
190 crit_exit();
191 bioq_remove(&sc->bio_queue, bio);
192 bp = bio->bio_buf;
984263bc
MD
193
194 devstat_start_transaction(&sc->stats);
195
8b6f36f5 196 switch (bp->b_cmd) {
10f3fee5 197 case BUF_CMD_FREEBLKS:
10f3fee5 198 case BUF_CMD_READ:
10f3fee5 199 case BUF_CMD_WRITE:
10f3fee5
MD
200 break;
201 default:
202 panic("md: bad b_cmd %d", bp->b_cmd);
203 }
984263bc 204
54078292
MD
205 nsec = bp->b_bcount >> DEV_BSHIFT;
206 secno = (unsigned)(bio->bio_offset >> DEV_BSHIFT);
984263bc
MD
207 dst = bp->b_data;
208 while (nsec--) {
984263bc
MD
209 if (secno < sc->nsecp) {
210 secpp = &sc->secp[secno];
973c11b9 211 if ((u_int)(uintptr_t)*secpp > 255) {
984263bc
MD
212 secp = *secpp;
213 secval = 0;
214 } else {
215 secp = 0;
973c11b9 216 secval = (u_int)(uintptr_t)*secpp;
984263bc
MD
217 }
218 } else {
219 secpp = 0;
220 secp = 0;
221 secval = 0;
222 }
223 if (md_debug > 2)
e3869ec7 224 kprintf("%08x %p %p %d\n", bp->b_flags, secpp, secp, secval);
984263bc 225
8b6f36f5 226 switch (bp->b_cmd) {
10f3fee5 227 case BUF_CMD_FREEBLKS:
984263bc
MD
228 if (secpp) {
229 if (secp)
230 FREE(secp, M_MDSECT);
231 *secpp = 0;
232 }
10f3fee5
MD
233 break;
234 case BUF_CMD_READ:
984263bc
MD
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);
242 }
10f3fee5
MD
243 break;
244 case BUF_CMD_WRITE:
984263bc
MD
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)
973c11b9 253 *secpp = (u_char *)(uintptr_t)uc;
984263bc
MD
254 } else {
255 if (!secpp) {
e7b4468c 256 MALLOC(secpp, u_char **, (secno + nsec + 1) * sizeof(u_char *), M_MD, M_WAITOK | M_ZERO);
984263bc
MD
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];
262 }
263 if (i == DEV_BSIZE) {
264 if (secp)
265 FREE(secp, M_MDSECT);
973c11b9 266 *secpp = (u_char *)(uintptr_t)uc;
984263bc
MD
267 } else {
268 if (!secp)
269 MALLOC(secp, u_char *, DEV_BSIZE, M_MDSECT, M_WAITOK);
270 bcopy(dst, secp, DEV_BSIZE);
271
272 *secpp = secp;
273 }
274 }
10f3fee5
MD
275 break;
276 default:
277 panic("md: bad b_cmd %d", bp->b_cmd);
278
984263bc
MD
279 }
280 secno++;
281 dst += DEV_BSIZE;
282 }
283 bp->b_resid = 0;
284 devstat_end_transaction_buf(&sc->stats, bp);
81b5c339 285 biodone(bio);
fbe276c7 286 crit_enter();
984263bc
MD
287 }
288 sc->busy = 0;
fef8985e 289 return(0);
984263bc
MD
290}
291
292
fef8985e
MD
293static int
294mdstrategy_preload(struct dev_strategy_args *ap)
984263bc 295{
b13267a5 296 cdev_t dev = ap->a_head.a_dev;
fef8985e 297 struct bio *bio = ap->a_bio;
81b5c339 298 struct buf *bp = bio->bio_buf;
81b5c339 299 struct md_s *sc;
984263bc
MD
300
301 if (md_debug > 1)
e3869ec7 302 kprintf("mdstrategy_preload(%p) %s %08x, %lld, %d, %p)\n",
973c11b9
MD
303 bp, devtoname(dev), bp->b_flags,
304 (long long)bio->bio_offset,
54078292 305 bp->b_bcount, bp->b_data);
984263bc 306
81b5c339 307 sc = dev->si_drv1;
984263bc 308
fbe276c7 309 crit_enter();
984263bc 310
81b5c339 311 bioqdisksort(&sc->bio_queue, bio);
984263bc
MD
312
313 if (sc->busy) {
fbe276c7 314 crit_exit();
fef8985e 315 return(0);
984263bc
MD
316 }
317
318 sc->busy++;
319
320 while (1) {
81b5c339
MD
321 bio = bioq_first(&sc->bio_queue);
322 if (bio)
323 bioq_remove(&sc->bio_queue, bio);
fbe276c7 324 crit_exit();
81b5c339 325 if (bio == NULL)
984263bc
MD
326 break;
327
328 devstat_start_transaction(&sc->stats);
329
8b6f36f5 330 switch (bp->b_cmd) {
10f3fee5 331 case BUF_CMD_FREEBLKS:
10f3fee5
MD
332 break;
333 case BUF_CMD_READ:
54078292
MD
334 bcopy(sc->pl_ptr + bio->bio_offset,
335 bp->b_data, bp->b_bcount);
10f3fee5
MD
336 break;
337 case BUF_CMD_WRITE:
54078292
MD
338 bcopy(bp->b_data, sc->pl_ptr + bio->bio_offset,
339 bp->b_bcount);
10f3fee5
MD
340 break;
341 default:
342 panic("md: bad cmd %d\n", bp->b_cmd);
984263bc
MD
343 }
344 bp->b_resid = 0;
345 devstat_end_transaction_buf(&sc->stats, bp);
81b5c339 346 biodone(bio);
fbe276c7 347 crit_enter();
984263bc
MD
348 }
349 sc->busy = 0;
fef8985e 350 return(0);
984263bc
MD
351}
352
353static struct md_s *
354mdcreate(void)
355{
356 struct md_s *sc;
357
e7b4468c 358 MALLOC(sc, struct md_s *,sizeof(*sc), M_MD, M_WAITOK | M_ZERO);
984263bc 359 sc->unit = mdunits++;
81b5c339 360 bioq_init(&sc->bio_queue);
984263bc
MD
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);
a688b15c 365 sc->dev = disk_create(sc->unit, &sc->disk, &md_ops);
984263bc 366 sc->dev->si_drv1 = sc;
a260c869
MD
367 sc->dev->si_iosize_max = DFLTPHYS;
368
984263bc
MD
369 return (sc);
370}
371
372static void
373mdcreate_preload(u_char *image, unsigned length)
374{
375 struct md_s *sc;
376
377 sc = mdcreate();
378 sc->type = MD_PRELOAD;
379 sc->nsect = length / DEV_BSIZE;
380 sc->pl_ptr = image;
381 sc->pl_len = length;
382
383 if (sc->unit == 0)
384 mdrootready = 1;
385}
386
387static void
388mdcreate_malloc(void)
389{
390 struct md_s *sc;
391
392 sc = mdcreate();
393 sc->type = MD_MALLOC;
394
395 sc->nsect = MD_NSECT; /* for now */
e7b4468c 396 MALLOC(sc->secp, u_char **, sizeof(u_char *), M_MD, M_WAITOK | M_ZERO);
984263bc 397 sc->nsecp = 1;
e3869ec7 398 kprintf("md%d: Malloc disk\n", sc->unit);
984263bc
MD
399}
400
401static void
402md_drvinit(void *unused)
403{
404
405 caddr_t mod;
406 caddr_t c;
407 u_char *ptr, *name, *type;
408 unsigned len;
409
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;
e3869ec7 427 kprintf("md%d: Preloaded image <%s> %d bytes at %p\n",
984263bc
MD
428 mdunits, name, len, ptr);
429 mdcreate_preload(ptr, len);
430 }
431 mdcreate_malloc();
432}
433
434SYSINIT(mddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR, md_drvinit,NULL)
435
436#ifdef MD_ROOT
437static void
438md_takeroot(void *junk)
439{
440 if (mdrootready)
441 rootdevnames[0] = "ufs:/dev/md0c";
442}
443
444SYSINIT(md_root, SI_SUB_MOUNT_ROOT, SI_ORDER_FIRST, md_takeroot, NULL);
445#endif