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