xref: /dragonfly/sys/dev/disk/md/md.c (revision fcce2b94)
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.12 2006/05/11 08:23:20 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 
30 #ifndef MD_NSECT
31 #define MD_NSECT (10000 * 2)
32 #endif
33 
34 MALLOC_DEFINE(M_MD, "MD disk", "Memory Disk");
35 MALLOC_DEFINE(M_MDSECT, "MD sectors", "Memory Disk Sectors");
36 
37 static int md_debug;
38 SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0, "");
39 
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
45 
46 static int mdrootready;
47 
48 static void mdcreate_malloc(void);
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_ioctl_t mdioctl;
57 
58 static struct cdevsw md_cdevsw = {
59         /* name */      "md",
60         /* maj */       CDEV_MAJOR,
61         /* flags */     D_DISK | D_CANFREE | D_MEMDISK,
62 	/* port */	NULL,
63 	/* clone */	NULL,
64 
65         /* open */      mdopen,
66         /* close */     nullclose,
67         /* read */      physread,
68         /* write */     physwrite,
69         /* ioctl */     mdioctl,
70         /* poll */      nopoll,
71         /* mmap */      nommap,
72         /* strategy */  mdstrategy,
73         /* dump */      nodump,
74         /* psize */     nopsize,
75 };
76 
77 struct md_s {
78 	int unit;
79 	struct devstat stats;
80 	struct bio_queue_head bio_queue;
81 	struct disk disk;
82 	dev_t dev;
83 	int busy;
84 	enum {MD_MALLOC, MD_PRELOAD} type;
85 	unsigned nsect;
86 
87 	/* MD_MALLOC related fields */
88 	unsigned nsecp;
89 	u_char **secp;
90 
91 	/* MD_PRELOAD related fields */
92 	u_char *pl_ptr;
93 	unsigned pl_len;
94 };
95 
96 static int mdunits;
97 
98 static int
99 mdopen(dev_t dev, int flag, int fmt, struct thread *td)
100 {
101 	struct md_s *sc;
102 	struct disklabel *dl;
103 
104 	if (md_debug)
105 		printf("mdopen(%s %x %x %p)\n",
106 			devtoname(dev), flag, fmt, td);
107 
108 	sc = dev->si_drv1;
109 	if (sc->unit + 1 == mdunits)
110 		mdcreate_malloc();
111 
112 	dl = &sc->disk.d_label;
113 	bzero(dl, sizeof(*dl));
114 	dl->d_secsize = DEV_BSIZE;
115 	dl->d_nsectors = 1024;
116 	dl->d_ntracks = 1;
117 	dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks;
118 	dl->d_secperunit = sc->nsect;
119 	dl->d_ncylinders = dl->d_secperunit / dl->d_secpercyl;
120 	return (0);
121 }
122 
123 static int
124 mdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
125 {
126 
127 	if (md_debug)
128 		printf("mdioctl(%s %lx %p %x %p)\n",
129 			devtoname(dev), cmd, addr, flags, td);
130 
131 	return (ENOIOCTL);
132 }
133 
134 static void
135 mdstrategy(dev_t dev, struct bio *bio)
136 {
137 	struct buf *bp = bio->bio_buf;
138 	struct md_s *sc;
139 
140 	if (md_debug > 1) {
141 		printf("mdstrategy(%p) %s %08x, %lld, %d, %p)\n",
142 		    bp, devtoname(dev), bp->b_flags, bio->bio_offset,
143 		    bp->b_bcount, bp->b_data);
144 	}
145 	bio->bio_driver_info = dev;
146 	sc = dev->si_drv1;
147 	if (sc->type == MD_MALLOC) {
148 		mdstrategy_malloc(dev, bio);
149 	} else {
150 		mdstrategy_preload(dev, bio);
151 	}
152 }
153 
154 
155 static void
156 mdstrategy_malloc(dev_t dev, struct bio *bio)
157 {
158 	struct buf *bp = bio->bio_buf;
159 	unsigned secno, nsec, secval, uc;
160 	u_char *secp, **secpp, *dst;
161 	devstat_trans_flags dop;
162 	struct md_s *sc;
163 	int i;
164 
165 	if (md_debug > 1)
166 		printf("mdstrategy_malloc(%p) %s %08xx, %lld, %d, %p)\n",
167 		    bp, devtoname(dev), bp->b_flags, bio->bio_offset,
168 		    bp->b_bcount, bp->b_data);
169 
170 	sc = dev->si_drv1;
171 
172 	crit_enter();
173 
174 	bioqdisksort(&sc->bio_queue, bio);
175 
176 	if (sc->busy) {
177 		crit_exit();
178 		return;
179 	}
180 
181 	sc->busy++;
182 
183 	while (1) {
184 		bio = bioq_first(&sc->bio_queue);
185 		if (bp)
186 			bioq_remove(&sc->bio_queue, bio);
187 		crit_exit();
188 		if (bio == NULL)
189 			break;
190 
191 		devstat_start_transaction(&sc->stats);
192 
193 		switch(bp->b_cmd) {
194 		case BUF_CMD_FREEBLKS:
195 			dop = DEVSTAT_NO_DATA;
196 			break;
197 		case BUF_CMD_READ:
198 			dop = DEVSTAT_READ;
199 			break;
200 		case BUF_CMD_WRITE:
201 			dop = DEVSTAT_WRITE;
202 			break;
203 		default:
204 			panic("md: bad b_cmd %d", bp->b_cmd);
205 		}
206 
207 		nsec = bp->b_bcount >> DEV_BSHIFT;
208 		secno = (unsigned)(bio->bio_offset >> DEV_BSHIFT);
209 		dst = bp->b_data;
210 		while (nsec--) {
211 			if (secno < sc->nsecp) {
212 				secpp = &sc->secp[secno];
213 				if ((u_int)*secpp > 255) {
214 					secp = *secpp;
215 					secval = 0;
216 				} else {
217 					secp = 0;
218 					secval = (u_int) *secpp;
219 				}
220 			} else {
221 				secpp = 0;
222 				secp = 0;
223 				secval = 0;
224 			}
225 			if (md_debug > 2)
226 				printf("%08x %p %p %d\n", bp->b_flags, secpp, secp, secval);
227 
228 			switch(bp->b_cmd) {
229 			case BUF_CMD_FREEBLKS:
230 				if (secpp) {
231 					if (secp)
232 						FREE(secp, M_MDSECT);
233 					*secpp = 0;
234 				}
235 				break;
236 			case BUF_CMD_READ:
237 				if (secp) {
238 					bcopy(secp, dst, DEV_BSIZE);
239 				} else if (secval) {
240 					for (i = 0; i < DEV_BSIZE; i++)
241 						dst[i] = secval;
242 				} else {
243 					bzero(dst, DEV_BSIZE);
244 				}
245 				break;
246 			case BUF_CMD_WRITE:
247 				uc = dst[0];
248 				for (i = 1; i < DEV_BSIZE; i++)
249 					if (dst[i] != uc)
250 						break;
251 				if (i == DEV_BSIZE && !uc) {
252 					if (secp)
253 						FREE(secp, M_MDSECT);
254 					if (secpp)
255 						*secpp = (u_char *)uc;
256 				} else {
257 					if (!secpp) {
258 						MALLOC(secpp, u_char **, (secno + nsec + 1) * sizeof(u_char *), M_MD, M_WAITOK);
259 						bzero(secpp, (secno + nsec + 1) * sizeof(u_char *));
260 						bcopy(sc->secp, secpp, sc->nsecp * sizeof(u_char *));
261 						FREE(sc->secp, M_MD);
262 						sc->secp = secpp;
263 						sc->nsecp = secno + nsec + 1;
264 						secpp = &sc->secp[secno];
265 					}
266 					if (i == DEV_BSIZE) {
267 						if (secp)
268 							FREE(secp, M_MDSECT);
269 						*secpp = (u_char *)uc;
270 					} else {
271 						if (!secp)
272 							MALLOC(secp, u_char *, DEV_BSIZE, M_MDSECT, M_WAITOK);
273 						bcopy(dst, secp, DEV_BSIZE);
274 
275 						*secpp = secp;
276 					}
277 				}
278 				break;
279 			default:
280 				panic("md: bad b_cmd %d", bp->b_cmd);
281 
282 			}
283 			secno++;
284 			dst += DEV_BSIZE;
285 		}
286 		bp->b_resid = 0;
287 		devstat_end_transaction_buf(&sc->stats, bp);
288 		biodone(bio);
289 		crit_enter();
290 	}
291 	sc->busy = 0;
292 }
293 
294 
295 static void
296 mdstrategy_preload(dev_t dev, struct bio *bio)
297 {
298 	struct buf *bp = bio->bio_buf;
299 	devstat_trans_flags dop;
300 	struct md_s *sc;
301 
302 	if (md_debug > 1)
303 		printf("mdstrategy_preload(%p) %s %08x, %lld, %d, %p)\n",
304 		    bp, devtoname(dev), bp->b_flags, bio->bio_offset,
305 		    bp->b_bcount, bp->b_data);
306 
307 	sc = dev->si_drv1;
308 
309 	crit_enter();
310 
311 	bioqdisksort(&sc->bio_queue, bio);
312 
313 	if (sc->busy) {
314 		crit_exit();
315 		return;
316 	}
317 
318 	sc->busy++;
319 
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;
327 
328 		devstat_start_transaction(&sc->stats);
329 
330 		switch(bp->b_cmd) {
331 		case BUF_CMD_FREEBLKS:
332 			dop = DEVSTAT_NO_DATA;
333 			break;
334 		case BUF_CMD_READ:
335 			dop = DEVSTAT_READ;
336 			bcopy(sc->pl_ptr + bio->bio_offset,
337 			       bp->b_data, bp->b_bcount);
338 			break;
339 		case BUF_CMD_WRITE:
340 			dop = DEVSTAT_WRITE;
341 			bcopy(bp->b_data, sc->pl_ptr + bio->bio_offset,
342 			      bp->b_bcount);
343 			break;
344 		default:
345 			panic("md: bad cmd %d\n", bp->b_cmd);
346 		}
347 		bp->b_resid = 0;
348 		devstat_end_transaction_buf(&sc->stats, bp);
349 		biodone(bio);
350 		crit_enter();
351 	}
352 	sc->busy = 0;
353 }
354 
355 static struct md_s *
356 mdcreate(void)
357 {
358 	struct md_s *sc;
359 
360 	MALLOC(sc, struct md_s *,sizeof(*sc), M_MD, M_WAITOK);
361 	bzero(sc, sizeof(*sc));
362 	sc->unit = mdunits++;
363 	bioq_init(&sc->bio_queue);
364 	devstat_add_entry(&sc->stats, "md", sc->unit, DEV_BSIZE,
365 		DEVSTAT_NO_ORDERED_TAGS,
366 		DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER,
367 		DEVSTAT_PRIORITY_OTHER);
368 	sc->dev = disk_create(sc->unit, &sc->disk, 0, &md_cdevsw);
369 	sc->dev->si_drv1 = sc;
370 	return (sc);
371 }
372 
373 static void
374 mdcreate_preload(u_char *image, unsigned length)
375 {
376 	struct md_s *sc;
377 
378 	sc = mdcreate();
379 	sc->type = MD_PRELOAD;
380 	sc->nsect = length / DEV_BSIZE;
381 	sc->pl_ptr = image;
382 	sc->pl_len = length;
383 
384 	if (sc->unit == 0)
385 		mdrootready = 1;
386 }
387 
388 static void
389 mdcreate_malloc(void)
390 {
391 	struct md_s *sc;
392 
393 	sc = mdcreate();
394 	sc->type = MD_MALLOC;
395 
396 	sc->nsect = MD_NSECT;	/* for now */
397 	MALLOC(sc->secp, u_char **, sizeof(u_char *), M_MD, M_WAITOK);
398 	bzero(sc->secp, sizeof(u_char *));
399 	sc->nsecp = 1;
400 	printf("md%d: Malloc disk\n", sc->unit);
401 }
402 
403 static void
404 md_drvinit(void *unused)
405 {
406 
407 	caddr_t mod;
408 	caddr_t c;
409 	u_char *ptr, *name, *type;
410 	unsigned len;
411 
412 #ifdef MD_ROOT_SIZE
413 	mdcreate_preload(mfs_root, MD_ROOT_SIZE*1024);
414 #endif
415 	mod = NULL;
416 	while ((mod = preload_search_next_name(mod)) != NULL) {
417 		name = (char *)preload_search_info(mod, MODINFO_NAME);
418 		type = (char *)preload_search_info(mod, MODINFO_TYPE);
419 		if (name == NULL)
420 			continue;
421 		if (type == NULL)
422 			continue;
423 		if (strcmp(type, "md_image") && strcmp(type, "mfs_root"))
424 			continue;
425 		c = preload_search_info(mod, MODINFO_ADDR);
426 		ptr = *(u_char **)c;
427 		c = preload_search_info(mod, MODINFO_SIZE);
428 		len = *(unsigned *)c;
429 		printf("md%d: Preloaded image <%s> %d bytes at %p\n",
430 		   mdunits, name, len, ptr);
431 		mdcreate_preload(ptr, len);
432 	}
433 	mdcreate_malloc();
434 }
435 
436 SYSINIT(mddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR, md_drvinit,NULL)
437 
438 #ifdef MD_ROOT
439 static void
440 md_takeroot(void *junk)
441 {
442 	if (mdrootready)
443 		rootdevnames[0] = "ufs:/dev/md0c";
444 }
445 
446 SYSINIT(md_root, SI_SUB_MOUNT_ROOT, SI_ORDER_FIRST, md_takeroot, NULL);
447 #endif
448