762598aa45319f85dbfa2ee6fc100ce04182979e
[dragonfly.git] / sys / dev / disk / md / md.c
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 $
10  *
11  */
12
13 #include "opt_md.h"             /* We have adopted some tasks from MFS */
14
15 #include <sys/param.h>
16 #include <sys/systm.h>
17 #include <sys/buf.h>
18 #include <sys/conf.h>
19 #include <sys/devicestat.h>
20 #include <sys/disk.h>
21 #include <sys/kernel.h>
22 #include <sys/malloc.h>
23 #include <sys/sysctl.h>
24 #include <sys/linker.h>
25 #include <sys/proc.h>
26 #include <sys/buf2.h>
27 #include <sys/thread2.h>
28 #include <sys/queue.h>
29 #include <sys/udev.h>
30
31 #ifndef MD_NSECT
32 #define MD_NSECT (10000 * 2)
33 #endif
34
35 MALLOC_DEFINE(M_MD, "MD disk", "Memory Disk");
36 MALLOC_DEFINE(M_MDSECT, "MD sectors", "Memory Disk Sectors");
37
38 static int md_debug;
39 SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0, "");
40
41 #if defined(MD_ROOT) && defined(MD_ROOT_SIZE)
42 /* Image gets put here: */
43 static u_char mfs_root[MD_ROOT_SIZE*1024] = "MFS Filesystem goes here";
44 static u_char end_mfs_root[] __unused = "MFS Filesystem had better STOP here";
45 #endif
46
47 static int mdrootready;
48
49 static d_strategy_t mdstrategy;
50 static d_strategy_t mdstrategy_preload;
51 static d_strategy_t mdstrategy_malloc;
52 static d_open_t mdopen;
53 static d_close_t mdclose;
54 static d_ioctl_t mdioctl;
55
56 static struct dev_ops md_ops = {
57         { "md", 0, D_DISK | D_CANFREE | D_MEMDISK | D_TRACKCLOSE},
58         .d_open =       mdopen,
59         .d_close =      mdclose,
60         .d_read =       physread,
61         .d_write =      physwrite,
62         .d_ioctl =      mdioctl,
63         .d_strategy =   mdstrategy,
64 };
65
66 struct md_s {
67         int unit;
68         struct devstat stats;
69         struct bio_queue_head bio_queue;
70         struct disk disk;
71         cdev_t dev;
72         int busy;
73         enum {                  /* Memory disk type */
74                 MD_MALLOC,
75                 MD_PRELOAD
76         } type;
77         unsigned nsect;
78
79         /* MD_MALLOC related fields */
80         unsigned nsecp;
81         u_char **secp;
82
83         /* MD_PRELOAD related fields */
84         u_char *pl_ptr;
85         unsigned pl_len;
86         TAILQ_ENTRY(md_s) link;
87 };
88 TAILQ_HEAD(mdshead, md_s) mdlist = TAILQ_HEAD_INITIALIZER(mdlist);
89
90 static int mdunits;
91 static int refcnt;
92
93 static struct md_s *mdcreate(unsigned);
94 static void mdcreate_malloc(void);
95 static int mdinit(module_t, int, void *);
96 static void md_drvinit(void *);
97 static int md_drvcleanup(void);
98
99 static int
100 mdinit(module_t mod, int cmd, void *arg)
101 {
102     int ret = 0;
103
104     switch(cmd) {
105         case MOD_LOAD:
106                 TAILQ_INIT(&mdlist);
107                 md_drvinit(NULL);
108                 break;
109         case MOD_UNLOAD:
110                 ret = md_drvcleanup();
111                 break;
112         default:
113                 ret = EINVAL;
114                 break;
115     }
116
117     return (ret);
118 }
119
120 static int
121 mdopen(struct dev_open_args *ap)
122 {
123         cdev_t dev = ap->a_head.a_dev;
124         struct md_s *sc;
125
126         if (md_debug)
127                 kprintf("mdopen(%s %x %x)\n",
128                         devtoname(dev), ap->a_oflags, ap->a_devtype);
129
130         sc = dev->si_drv1;
131         if (sc->unit + 1 == mdunits)
132                 mdcreate_malloc();
133
134         atomic_add_int(&refcnt, 1);
135         return (0);
136 }
137
138 static int
139 mdclose(struct dev_close_args *ap)
140 {
141         cdev_t dev = ap->a_head.a_dev;
142         struct md_s *sc;
143
144         if (md_debug)
145                 kprintf("mdclose(%s %x %x)\n",
146                         devtoname(dev), ap->a_fflag, ap->a_devtype);
147         sc = dev->si_drv1;
148         atomic_add_int(&refcnt, -1);
149
150         return (0);
151 }
152
153 static int
154 mdioctl(struct dev_ioctl_args *ap)
155 {
156         cdev_t dev = ap->a_head.a_dev;
157
158         if (md_debug)
159                 kprintf("mdioctl(%s %lx %p %x)\n",
160                         devtoname(dev), ap->a_cmd, ap->a_data, ap->a_fflag);
161
162         return (ENOIOCTL);
163 }
164
165 static int
166 mdstrategy(struct dev_strategy_args *ap)
167 {
168         cdev_t dev = ap->a_head.a_dev;
169         struct bio *bio = ap->a_bio;
170         struct buf *bp = bio->bio_buf;
171         struct md_s *sc;
172
173         if (md_debug > 1) {
174                 kprintf("mdstrategy(%p) %s %08x, %lld, %d, %p)\n",
175                     bp, devtoname(dev), bp->b_flags,
176                     (long long)bio->bio_offset,
177                     bp->b_bcount, bp->b_data);
178         }
179         bio->bio_driver_info = dev;
180         sc = dev->si_drv1;
181         if (sc->type == MD_MALLOC) {
182                 mdstrategy_malloc(ap);
183         } else {
184                 mdstrategy_preload(ap);
185         }
186         return(0);
187 }
188
189
190 static int
191 mdstrategy_malloc(struct dev_strategy_args *ap)
192 {
193         cdev_t dev = ap->a_head.a_dev;
194         struct bio *bio = ap->a_bio;
195         struct buf *bp = bio->bio_buf;
196         unsigned secno, nsec, secval, uc;
197         u_char *secp, **secpp, *dst;
198         struct md_s *sc;
199         int i;
200
201         if (md_debug > 1)
202                 kprintf("mdstrategy_malloc(%p) %s %08xx, %lld, %d, %p)\n",
203                     bp, devtoname(dev), bp->b_flags,
204                     (long long)bio->bio_offset,
205                     bp->b_bcount, bp->b_data);
206
207         sc = dev->si_drv1;
208
209         crit_enter();
210
211         bioqdisksort(&sc->bio_queue, bio);
212
213         if (sc->busy) {
214                 crit_exit();
215                 return(0);
216         }
217
218         sc->busy++;
219         
220         while (1) {
221                 bio = bioq_first(&sc->bio_queue);
222                 if (bio == NULL) {
223                         crit_exit();
224                         break;
225                 }
226                 crit_exit();
227                 bioq_remove(&sc->bio_queue, bio);
228                 bp = bio->bio_buf;
229
230                 devstat_start_transaction(&sc->stats);
231
232                 switch (bp->b_cmd) {
233                 case BUF_CMD_FREEBLKS:
234                 case BUF_CMD_READ:
235                 case BUF_CMD_WRITE:
236                         break;
237                 default:
238                         panic("md: bad b_cmd %d", bp->b_cmd);
239                 }
240
241                 nsec = bp->b_bcount >> DEV_BSHIFT;
242                 secno = (unsigned)(bio->bio_offset >> DEV_BSHIFT);
243                 dst = bp->b_data;
244                 while (nsec--) {
245                         if (secno < sc->nsecp) {
246                                 secpp = &sc->secp[secno];
247                                 if ((u_int)(uintptr_t)*secpp > 255) {
248                                         secp = *secpp;
249                                         secval = 0;
250                                 } else {
251                                         secp = 0;
252                                         secval = (u_int)(uintptr_t)*secpp;
253                                 }
254                         } else {
255                                 secpp = 0;
256                                 secp = 0;
257                                 secval = 0;
258                         }
259                         if (md_debug > 2)
260                                 kprintf("%08x %p %p %d\n", bp->b_flags, secpp, secp, secval);
261
262                         switch (bp->b_cmd) {
263                         case BUF_CMD_FREEBLKS:
264                                 if (secpp) {
265                                         if (secp)
266                                                 FREE(secp, M_MDSECT);
267                                         *secpp = 0;
268                                 }
269                                 break;
270                         case BUF_CMD_READ:
271                                 if (secp) {
272                                         bcopy(secp, dst, DEV_BSIZE);
273                                 } else if (secval) {
274                                         for (i = 0; i < DEV_BSIZE; i++)
275                                                 dst[i] = secval;
276                                 } else {
277                                         bzero(dst, DEV_BSIZE);
278                                 }
279                                 break;
280                         case BUF_CMD_WRITE:
281                                 uc = dst[0];
282                                 for (i = 1; i < DEV_BSIZE; i++) 
283                                         if (dst[i] != uc)
284                                                 break;
285                                 if (i == DEV_BSIZE && !uc) {
286                                         if (secp)
287                                                 FREE(secp, M_MDSECT);
288                                         if (secpp)
289                                                 *secpp = (u_char *)(uintptr_t)uc;
290                                 } else {
291                                         if (!secpp) {
292                                                 MALLOC(secpp, u_char **, (secno + nsec + 1) * sizeof(u_char *), M_MD, M_WAITOK | M_ZERO);
293                                                 bcopy(sc->secp, secpp, sc->nsecp * sizeof(u_char *));
294                                                 FREE(sc->secp, M_MD);
295                                                 sc->secp = secpp;
296                                                 sc->nsecp = secno + nsec + 1;
297                                                 secpp = &sc->secp[secno];
298                                         }
299                                         if (i == DEV_BSIZE) {
300                                                 if (secp)
301                                                         FREE(secp, M_MDSECT);
302                                                 *secpp = (u_char *)(uintptr_t)uc;
303                                         } else {
304                                                 if (!secp) 
305                                                         MALLOC(secp, u_char *, DEV_BSIZE, M_MDSECT, M_WAITOK);
306                                                 bcopy(dst, secp, DEV_BSIZE);
307
308                                                 *secpp = secp;
309                                         }
310                                 }
311                                 break;
312                         default:
313                                 panic("md: bad b_cmd %d", bp->b_cmd);
314
315                         }
316                         secno++;
317                         dst += DEV_BSIZE;
318                 }
319                 bp->b_resid = 0;
320                 devstat_end_transaction_buf(&sc->stats, bp);
321                 biodone(bio);
322                 crit_enter();
323         }
324         sc->busy = 0;
325         return(0);
326 }
327
328
329 static int
330 mdstrategy_preload(struct dev_strategy_args *ap)
331 {
332         cdev_t dev = ap->a_head.a_dev;
333         struct bio *bio = ap->a_bio;
334         struct buf *bp = bio->bio_buf;
335         struct md_s *sc;
336
337         if (md_debug > 1)
338                 kprintf("mdstrategy_preload(%p) %s %08x, %lld, %d, %p)\n",
339                     bp, devtoname(dev), bp->b_flags,
340                     (long long)bio->bio_offset,
341                     bp->b_bcount, bp->b_data);
342
343         sc = dev->si_drv1;
344
345         crit_enter();
346
347         bioqdisksort(&sc->bio_queue, bio);
348
349         if (sc->busy) {
350                 crit_exit();
351                 return(0);
352         }
353
354         sc->busy++;
355         
356         while (1) {
357                 bio = bioq_first(&sc->bio_queue);
358                 if (bio)
359                         bioq_remove(&sc->bio_queue, bio);
360                 crit_exit();
361                 if (bio == NULL)
362                         break;
363
364                 devstat_start_transaction(&sc->stats);
365
366                 switch (bp->b_cmd) {
367                 case BUF_CMD_FREEBLKS:
368                         break;
369                 case BUF_CMD_READ:
370                         bcopy(sc->pl_ptr + bio->bio_offset, 
371                                bp->b_data, bp->b_bcount);
372                         break;
373                 case BUF_CMD_WRITE:
374                         bcopy(bp->b_data, sc->pl_ptr + bio->bio_offset,
375                               bp->b_bcount);
376                         break;
377                 default:
378                         panic("md: bad cmd %d\n", bp->b_cmd);
379                 }
380                 bp->b_resid = 0;
381                 devstat_end_transaction_buf(&sc->stats, bp);
382                 biodone(bio);
383                 crit_enter();
384         }
385         sc->busy = 0;
386         return(0);
387 }
388
389 static struct md_s *
390 mdcreate(unsigned length)
391 {
392         struct md_s *sc;
393         struct disk_info info;
394
395         MALLOC(sc, struct md_s *,sizeof(*sc), M_MD, M_WAITOK | M_ZERO);
396         sc->unit = mdunits++;
397         bioq_init(&sc->bio_queue);
398         devstat_add_entry(&sc->stats, "md", sc->unit, DEV_BSIZE,
399                 DEVSTAT_NO_ORDERED_TAGS, 
400                 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER,
401                 DEVSTAT_PRIORITY_OTHER);
402         sc->dev = disk_create(sc->unit, &sc->disk, &md_ops);
403         sc->dev->si_drv1 = sc;
404         sc->dev->si_iosize_max = DFLTPHYS;
405         disk_setdisktype(&sc->disk, "memory");
406
407         bzero(&info, sizeof(info));
408         info.d_media_blksize = DEV_BSIZE;       /* mandatory */
409         info.d_media_blocks = length / DEV_BSIZE;
410
411         info.d_secpertrack = 1024;              /* optional */
412         info.d_nheads = 1;
413         info.d_secpercyl = info.d_secpertrack * info.d_nheads;
414         info.d_ncylinders = (u_int)(info.d_media_blocks / info.d_secpercyl);
415         disk_setdiskinfo(&sc->disk, &info);
416         TAILQ_INSERT_HEAD(&mdlist, sc, link);
417
418         return (sc);
419 }
420
421
422 static void
423 mdcreate_preload(u_char *image, unsigned length)
424 {
425         struct md_s *sc;
426
427         sc = mdcreate(length);
428         sc->type = MD_PRELOAD;
429         sc->nsect = length / DEV_BSIZE;
430         sc->pl_ptr = image;
431         sc->pl_len = length;
432
433         if (sc->unit == 0) 
434                 mdrootready = 1;
435 }
436
437 static void
438 mdcreate_malloc(void)
439 {
440         struct md_s *sc;
441
442         sc = mdcreate(MD_NSECT*DEV_BSIZE);
443         sc->type = MD_MALLOC;
444
445         sc->nsect = MD_NSECT;   /* for now */
446         MALLOC(sc->secp, u_char **, sizeof(u_char *), M_MD, M_WAITOK | M_ZERO);
447         sc->nsecp = 1;
448         kprintf("md%d: Malloc disk\n", sc->unit);
449 }
450
451 static int
452 md_drvcleanup(void)
453 {
454
455         int secno;
456         struct md_s *sc, *sc_temp;
457
458         if (atomic_fetchadd_int(&refcnt, 0) != 0)
459                 return EBUSY;
460
461         /*
462          * Go through all the md devices, freeing up all the
463          * memory allocated for sectors, and the md_s struct
464          * itself.
465          */
466         TAILQ_FOREACH_MUTABLE(sc, &mdlist, link, sc_temp) {
467                 for (secno = 0; secno < sc->nsecp; secno++) {
468                         if ((u_int)(uintptr_t)sc->secp[secno] > 255)
469                                 FREE(sc->secp[secno], M_MDSECT);
470                 }
471
472                 if (sc->dev != NULL)
473                         disk_destroy(&sc->disk);
474
475                 devstat_remove_entry(&sc->stats);
476                 TAILQ_REMOVE(&mdlist, sc, link);
477
478                 FREE(sc->secp, M_MD);
479                 FREE(sc, M_MD);
480         }
481
482         return 0;
483
484 }
485
486 static void
487 md_drvinit(void *unused)
488 {
489
490         caddr_t mod;
491         caddr_t c;
492         u_char *ptr, *name, *type;
493         unsigned len;
494
495 #ifdef MD_ROOT_SIZE
496         mdcreate_preload(mfs_root, MD_ROOT_SIZE*1024);
497 #endif
498         mod = NULL;
499         while ((mod = preload_search_next_name(mod)) != NULL) {
500                 name = (char *)preload_search_info(mod, MODINFO_NAME);
501                 type = (char *)preload_search_info(mod, MODINFO_TYPE);
502                 if (name == NULL)
503                         continue;
504                 if (type == NULL)
505                         continue;
506                 if (strcmp(type, "md_image") && strcmp(type, "mfs_root"))
507                         continue;
508                 c = preload_search_info(mod, MODINFO_ADDR);
509                 ptr = *(u_char **)c;
510                 c = preload_search_info(mod, MODINFO_SIZE);
511                 len = *(unsigned *)c;
512                 kprintf("md%d: Preloaded image <%s> %d bytes at %p\n",
513                    mdunits, name, len, ptr);
514                 mdcreate_preload(ptr, len);
515         } 
516         mdcreate_malloc();
517 }
518
519 DEV_MODULE(md, mdinit, NULL);
520
521 #ifdef MD_ROOT
522 static void
523 md_takeroot(void *junk)
524 {
525         if (mdrootready)
526                 rootdevnames[0] = "ufs:/dev/md0s0";
527 }
528
529 SYSINIT(md_root, SI_SUB_MOUNT_ROOT, SI_ORDER_FIRST, md_takeroot, NULL);
530 #endif