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