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