xref: /freebsd/sys/dev/md/md.c (revision b0cd20172d854584c67cd47461a77e98b43cbcd8)
1098ca2bdSWarner Losh /*-
200a6a3c6SPoul-Henning Kamp  * ----------------------------------------------------------------------------
300a6a3c6SPoul-Henning Kamp  * "THE BEER-WARE LICENSE" (Revision 42):
400a6a3c6SPoul-Henning Kamp  * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
500a6a3c6SPoul-Henning Kamp  * can do whatever you want with this stuff. If we meet some day, and you think
600a6a3c6SPoul-Henning Kamp  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
700a6a3c6SPoul-Henning Kamp  * ----------------------------------------------------------------------------
800a6a3c6SPoul-Henning Kamp  *
900a6a3c6SPoul-Henning Kamp  * $FreeBSD$
1000a6a3c6SPoul-Henning Kamp  *
1100a6a3c6SPoul-Henning Kamp  */
1200a6a3c6SPoul-Henning Kamp 
13098ca2bdSWarner Losh /*-
14637f671aSPoul-Henning Kamp  * The following functions are based in the vn(4) driver: mdstart_swap(),
15637f671aSPoul-Henning Kamp  * mdstart_vnode(), mdcreate_swap(), mdcreate_vnode() and mddestroy(),
16637f671aSPoul-Henning Kamp  * and as such under the following copyright:
17637f671aSPoul-Henning Kamp  *
18637f671aSPoul-Henning Kamp  * Copyright (c) 1988 University of Utah.
19637f671aSPoul-Henning Kamp  * Copyright (c) 1990, 1993
20637f671aSPoul-Henning Kamp  *	The Regents of the University of California.  All rights reserved.
2159ec9023SKonstantin Belousov  * Copyright (c) 2013 The FreeBSD Foundation
2259ec9023SKonstantin Belousov  * All rights reserved.
23637f671aSPoul-Henning Kamp  *
24ed010cdfSWarner Losh  * This code is derived from software contributed to Berkeley by
25ed010cdfSWarner Losh  * the Systems Programming Group of the University of Utah Computer
26ed010cdfSWarner Losh  * Science Department.
27ed010cdfSWarner Losh  *
2859ec9023SKonstantin Belousov  * Portions of this software were developed by Konstantin Belousov
2959ec9023SKonstantin Belousov  * under sponsorship from the FreeBSD Foundation.
3059ec9023SKonstantin Belousov  *
31637f671aSPoul-Henning Kamp  * Redistribution and use in source and binary forms, with or without
32637f671aSPoul-Henning Kamp  * modification, are permitted provided that the following conditions
33637f671aSPoul-Henning Kamp  * are met:
34637f671aSPoul-Henning Kamp  * 1. Redistributions of source code must retain the above copyright
35637f671aSPoul-Henning Kamp  *    notice, this list of conditions and the following disclaimer.
36637f671aSPoul-Henning Kamp  * 2. Redistributions in binary form must reproduce the above copyright
37637f671aSPoul-Henning Kamp  *    notice, this list of conditions and the following disclaimer in the
38637f671aSPoul-Henning Kamp  *    documentation and/or other materials provided with the distribution.
39637f671aSPoul-Henning Kamp  * 4. Neither the name of the University nor the names of its contributors
40637f671aSPoul-Henning Kamp  *    may be used to endorse or promote products derived from this software
41637f671aSPoul-Henning Kamp  *    without specific prior written permission.
42637f671aSPoul-Henning Kamp  *
43637f671aSPoul-Henning Kamp  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44637f671aSPoul-Henning Kamp  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45637f671aSPoul-Henning Kamp  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46637f671aSPoul-Henning Kamp  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47637f671aSPoul-Henning Kamp  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48637f671aSPoul-Henning Kamp  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49637f671aSPoul-Henning Kamp  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50637f671aSPoul-Henning Kamp  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51637f671aSPoul-Henning Kamp  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52637f671aSPoul-Henning Kamp  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53637f671aSPoul-Henning Kamp  * SUCH DAMAGE.
54637f671aSPoul-Henning Kamp  *
55637f671aSPoul-Henning Kamp  * from: Utah Hdr: vn.c 1.13 94/04/02
56637f671aSPoul-Henning Kamp  *
57637f671aSPoul-Henning Kamp  *	from: @(#)vn.c	8.6 (Berkeley) 4/1/94
58637f671aSPoul-Henning Kamp  * From: src/sys/dev/vn/vn.c,v 1.122 2000/12/16 16:06:03
59637f671aSPoul-Henning Kamp  */
60637f671aSPoul-Henning Kamp 
616f4f00f1SPoul-Henning Kamp #include "opt_geom.h"
623f54a085SPoul-Henning Kamp #include "opt_md.h"
6371e4fff8SPoul-Henning Kamp 
6400a6a3c6SPoul-Henning Kamp #include <sys/param.h>
6500a6a3c6SPoul-Henning Kamp #include <sys/systm.h>
669626b608SPoul-Henning Kamp #include <sys/bio.h>
6759ec9023SKonstantin Belousov #include <sys/buf.h>
6800a6a3c6SPoul-Henning Kamp #include <sys/conf.h>
69a03be42dSMaxim Sobolev #include <sys/devicestat.h>
708f8def9eSPoul-Henning Kamp #include <sys/fcntl.h>
71fb919e4dSMark Murray #include <sys/kernel.h>
725c97ca54SIan Dowse #include <sys/kthread.h>
7306d425f9SEd Schouten #include <sys/limits.h>
74fb919e4dSMark Murray #include <sys/linker.h>
75fb919e4dSMark Murray #include <sys/lock.h>
76fb919e4dSMark Murray #include <sys/malloc.h>
77fb919e4dSMark Murray #include <sys/mdioctl.h>
78a08d2e7fSJohn Baldwin #include <sys/mount.h>
799dceb26bSJohn Baldwin #include <sys/mutex.h>
809b00ca19SPoul-Henning Kamp #include <sys/sx.h>
81fb919e4dSMark Murray #include <sys/namei.h>
828f8def9eSPoul-Henning Kamp #include <sys/proc.h>
83fb919e4dSMark Murray #include <sys/queue.h>
8489f6b863SAttilio Rao #include <sys/rwlock.h>
85657bd8b1SAndrey V. Elsukov #include <sys/sbuf.h>
8663710c4dSJohn Baldwin #include <sys/sched.h>
877cd53fddSAlan Cox #include <sys/sf_buf.h>
88fb919e4dSMark Murray #include <sys/sysctl.h>
89fb919e4dSMark Murray #include <sys/vnode.h>
90fb919e4dSMark Murray 
916f4f00f1SPoul-Henning Kamp #include <geom/geom.h>
92ec170744SAndrey V. Elsukov #include <geom/geom_int.h>
936f4f00f1SPoul-Henning Kamp 
948f8def9eSPoul-Henning Kamp #include <vm/vm.h>
951c771f92SKonstantin Belousov #include <vm/vm_param.h>
968f8def9eSPoul-Henning Kamp #include <vm/vm_object.h>
978f8def9eSPoul-Henning Kamp #include <vm/vm_page.h>
988f8def9eSPoul-Henning Kamp #include <vm/vm_pager.h>
998f8def9eSPoul-Henning Kamp #include <vm/swap_pager.h>
100f43b2bacSPoul-Henning Kamp #include <vm/uma.h>
1013f54a085SPoul-Henning Kamp 
102a9934668SKenneth D. Merry #include <machine/bus.h>
103a9934668SKenneth D. Merry 
10457e9624eSPoul-Henning Kamp #define MD_MODVER 1
10557e9624eSPoul-Henning Kamp 
1065c97ca54SIan Dowse #define MD_SHUTDOWN	0x10000		/* Tell worker thread to terminate. */
107a08d2e7fSJohn Baldwin #define	MD_EXITING	0x20000		/* Worker thread is exiting. */
1085c97ca54SIan Dowse 
109f2744793SSheldon Hearn #ifndef MD_NSECT
110f2744793SSheldon Hearn #define MD_NSECT (10000 * 2)
11133edfabeSPoul-Henning Kamp #endif
11233edfabeSPoul-Henning Kamp 
1135bb84bc8SRobert Watson static MALLOC_DEFINE(M_MD, "md_disk", "Memory Disk");
1145bb84bc8SRobert Watson static MALLOC_DEFINE(M_MDSECT, "md_sectors", "Memory Disk Sectors");
11500a6a3c6SPoul-Henning Kamp 
11671e4fff8SPoul-Henning Kamp static int md_debug;
1173eb9ab52SEitan Adler SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0,
1183eb9ab52SEitan Adler     "Enable md(4) debug messages");
119c44d423eSKonstantin Belousov static int md_malloc_wait;
1203eb9ab52SEitan Adler SYSCTL_INT(_vm, OID_AUTO, md_malloc_wait, CTLFLAG_RW, &md_malloc_wait, 0,
1213eb9ab52SEitan Adler     "Allow malloc to wait for memory allocations");
12200a6a3c6SPoul-Henning Kamp 
12322ff74b2SMarcel Moolenaar #if defined(MD_ROOT) && !defined(MD_ROOT_FSTYPE)
12422ff74b2SMarcel Moolenaar #define	MD_ROOT_FSTYPE	"ufs"
12522ff74b2SMarcel Moolenaar #endif
12622ff74b2SMarcel Moolenaar 
127cc787e3dSMarcel Moolenaar #if defined(MD_ROOT)
128de64f22aSLuigi Rizzo /*
129de64f22aSLuigi Rizzo  * Preloaded image gets put here.
130cc787e3dSMarcel Moolenaar  */
131cc787e3dSMarcel Moolenaar #if defined(MD_ROOT_SIZE)
132cc787e3dSMarcel Moolenaar /*
133de64f22aSLuigi Rizzo  * Applications that patch the object with the image can determine
134de64f22aSLuigi Rizzo  * the size looking at the start and end markers (strings),
135de64f22aSLuigi Rizzo  * so we want them contiguous.
136de64f22aSLuigi Rizzo  */
137de64f22aSLuigi Rizzo static struct {
138de64f22aSLuigi Rizzo 	u_char start[MD_ROOT_SIZE*1024];
139de64f22aSLuigi Rizzo 	u_char end[128];
140de64f22aSLuigi Rizzo } mfs_root = {
141de64f22aSLuigi Rizzo 	.start = "MFS Filesystem goes here",
142de64f22aSLuigi Rizzo 	.end = "MFS Filesystem had better STOP here",
143de64f22aSLuigi Rizzo };
144cc787e3dSMarcel Moolenaar const int mfs_root_size = sizeof(mfs_root.start);
145cc787e3dSMarcel Moolenaar #else
146cc787e3dSMarcel Moolenaar extern volatile u_char __weak_symbol mfs_root;
147cc787e3dSMarcel Moolenaar extern volatile u_char __weak_symbol mfs_root_end;
148cc787e3dSMarcel Moolenaar __GLOBL(mfs_root);
149cc787e3dSMarcel Moolenaar __GLOBL(mfs_root_end);
150cc787e3dSMarcel Moolenaar #define mfs_root_size ((uintptr_t)(&mfs_root_end - &mfs_root))
151cc787e3dSMarcel Moolenaar #endif
15271e4fff8SPoul-Henning Kamp #endif
15371e4fff8SPoul-Henning Kamp 
15419945697SPoul-Henning Kamp static g_init_t g_md_init;
15519945697SPoul-Henning Kamp static g_fini_t g_md_fini;
15619945697SPoul-Henning Kamp static g_start_t g_md_start;
15719945697SPoul-Henning Kamp static g_access_t g_md_access;
158b42f40b8SJaakko Heinonen static void g_md_dumpconf(struct sbuf *sb, const char *indent,
159b42f40b8SJaakko Heinonen     struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp);
1600eb14309SPoul-Henning Kamp 
16189c9c53dSPoul-Henning Kamp static struct cdev *status_dev = 0;
1629b00ca19SPoul-Henning Kamp static struct sx md_sx;
163f4e7c5a8SJaakko Heinonen static struct unrhdr *md_uh;
16457e9624eSPoul-Henning Kamp 
165a522a159SPoul-Henning Kamp static d_ioctl_t mdctlioctl;
1668f8def9eSPoul-Henning Kamp 
1678f8def9eSPoul-Henning Kamp static struct cdevsw mdctl_cdevsw = {
168dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
1697ac40f5fSPoul-Henning Kamp 	.d_ioctl =	mdctlioctl,
1707ac40f5fSPoul-Henning Kamp 	.d_name =	MD_NAME,
17100a6a3c6SPoul-Henning Kamp };
17200a6a3c6SPoul-Henning Kamp 
17319945697SPoul-Henning Kamp struct g_class g_md_class = {
17419945697SPoul-Henning Kamp 	.name = "MD",
1755721c9c7SPoul-Henning Kamp 	.version = G_VERSION,
17619945697SPoul-Henning Kamp 	.init = g_md_init,
17719945697SPoul-Henning Kamp 	.fini = g_md_fini,
17819945697SPoul-Henning Kamp 	.start = g_md_start,
17919945697SPoul-Henning Kamp 	.access = g_md_access,
180c27a8954SWojciech A. Koszek 	.dumpconf = g_md_dumpconf,
18119945697SPoul-Henning Kamp };
18219945697SPoul-Henning Kamp 
18319945697SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_md_class, g_md);
18419945697SPoul-Henning Kamp 
1850cfaeeeeSPoul-Henning Kamp 
18613e403fdSAntoine Brodin static LIST_HEAD(, md_s) md_softc_list = LIST_HEAD_INITIALIZER(md_softc_list);
1873f54a085SPoul-Henning Kamp 
188c6517568SPoul-Henning Kamp #define NINDIR	(PAGE_SIZE / sizeof(uintptr_t))
189c6517568SPoul-Henning Kamp #define NMASK	(NINDIR-1)
190c6517568SPoul-Henning Kamp static int nshift;
191c6517568SPoul-Henning Kamp 
19259ec9023SKonstantin Belousov static int md_vnode_pbuf_freecnt;
19359ec9023SKonstantin Belousov 
194c6517568SPoul-Henning Kamp struct indir {
195c6517568SPoul-Henning Kamp 	uintptr_t	*array;
1968b149b51SJohn Baldwin 	u_int		total;
1978b149b51SJohn Baldwin 	u_int		used;
1988b149b51SJohn Baldwin 	u_int		shift;
199c6517568SPoul-Henning Kamp };
200c6517568SPoul-Henning Kamp 
20100a6a3c6SPoul-Henning Kamp struct md_s {
20200a6a3c6SPoul-Henning Kamp 	int unit;
2033f54a085SPoul-Henning Kamp 	LIST_ENTRY(md_s) list;
2048177437dSPoul-Henning Kamp 	struct bio_queue_head bio_queue;
2050f8500a5SPoul-Henning Kamp 	struct mtx queue_mtx;
20640ea77a0SAlexander Motin 	struct mtx stat_mtx;
20789c9c53dSPoul-Henning Kamp 	struct cdev *dev;
2088f8def9eSPoul-Henning Kamp 	enum md_types type;
209b830359bSPawel Jakub Dawidek 	off_t mediasize;
210b830359bSPawel Jakub Dawidek 	unsigned sectorsize;
211fe603109SMaxim Sobolev 	unsigned opencount;
2124e8bfe14SPoul-Henning Kamp 	unsigned fwheads;
2134e8bfe14SPoul-Henning Kamp 	unsigned fwsectors;
2148f8def9eSPoul-Henning Kamp 	unsigned flags;
215f43b2bacSPoul-Henning Kamp 	char name[20];
2165c97ca54SIan Dowse 	struct proc *procp;
2176f4f00f1SPoul-Henning Kamp 	struct g_geom *gp;
2186f4f00f1SPoul-Henning Kamp 	struct g_provider *pp;
2199b00ca19SPoul-Henning Kamp 	int (*start)(struct md_s *sc, struct bio *bp);
220a03be42dSMaxim Sobolev 	struct devstat *devstat;
22195f1a897SPoul-Henning Kamp 
22295f1a897SPoul-Henning Kamp 	/* MD_MALLOC related fields */
223c6517568SPoul-Henning Kamp 	struct indir *indir;
224f43b2bacSPoul-Henning Kamp 	uma_zone_t uma;
22500a6a3c6SPoul-Henning Kamp 
22695f1a897SPoul-Henning Kamp 	/* MD_PRELOAD related fields */
22795f1a897SPoul-Henning Kamp 	u_char *pl_ptr;
228b830359bSPawel Jakub Dawidek 	size_t pl_len;
22900a6a3c6SPoul-Henning Kamp 
2308f8def9eSPoul-Henning Kamp 	/* MD_VNODE related fields */
2318f8def9eSPoul-Henning Kamp 	struct vnode *vnode;
23261a6eb62SPawel Jakub Dawidek 	char file[PATH_MAX];
2338f8def9eSPoul-Henning Kamp 	struct ucred *cred;
2348f8def9eSPoul-Henning Kamp 
235e0cebb40SDima Dorfman 	/* MD_SWAP related fields */
2368f8def9eSPoul-Henning Kamp 	vm_object_t object;
2378f8def9eSPoul-Henning Kamp };
23800a6a3c6SPoul-Henning Kamp 
239c6517568SPoul-Henning Kamp static struct indir *
2408b149b51SJohn Baldwin new_indir(u_int shift)
241c6517568SPoul-Henning Kamp {
242c6517568SPoul-Henning Kamp 	struct indir *ip;
243c6517568SPoul-Henning Kamp 
244c44d423eSKonstantin Belousov 	ip = malloc(sizeof *ip, M_MD, (md_malloc_wait ? M_WAITOK : M_NOWAIT)
245c44d423eSKonstantin Belousov 	    | M_ZERO);
246c6517568SPoul-Henning Kamp 	if (ip == NULL)
247c6517568SPoul-Henning Kamp 		return (NULL);
248c6517568SPoul-Henning Kamp 	ip->array = malloc(sizeof(uintptr_t) * NINDIR,
249c44d423eSKonstantin Belousov 	    M_MDSECT, (md_malloc_wait ? M_WAITOK : M_NOWAIT) | M_ZERO);
250c6517568SPoul-Henning Kamp 	if (ip->array == NULL) {
251c6517568SPoul-Henning Kamp 		free(ip, M_MD);
252c6517568SPoul-Henning Kamp 		return (NULL);
253c6517568SPoul-Henning Kamp 	}
254c6517568SPoul-Henning Kamp 	ip->total = NINDIR;
255c6517568SPoul-Henning Kamp 	ip->shift = shift;
256c6517568SPoul-Henning Kamp 	return (ip);
257c6517568SPoul-Henning Kamp }
258c6517568SPoul-Henning Kamp 
259c6517568SPoul-Henning Kamp static void
260c6517568SPoul-Henning Kamp del_indir(struct indir *ip)
261c6517568SPoul-Henning Kamp {
262c6517568SPoul-Henning Kamp 
263f43b2bacSPoul-Henning Kamp 	free(ip->array, M_MDSECT);
264c6517568SPoul-Henning Kamp 	free(ip, M_MD);
265c6517568SPoul-Henning Kamp }
266c6517568SPoul-Henning Kamp 
267f43b2bacSPoul-Henning Kamp static void
268f43b2bacSPoul-Henning Kamp destroy_indir(struct md_s *sc, struct indir *ip)
269f43b2bacSPoul-Henning Kamp {
270f43b2bacSPoul-Henning Kamp 	int i;
271f43b2bacSPoul-Henning Kamp 
272f43b2bacSPoul-Henning Kamp 	for (i = 0; i < NINDIR; i++) {
273f43b2bacSPoul-Henning Kamp 		if (!ip->array[i])
274f43b2bacSPoul-Henning Kamp 			continue;
275f43b2bacSPoul-Henning Kamp 		if (ip->shift)
276f43b2bacSPoul-Henning Kamp 			destroy_indir(sc, (struct indir*)(ip->array[i]));
277f43b2bacSPoul-Henning Kamp 		else if (ip->array[i] > 255)
278f43b2bacSPoul-Henning Kamp 			uma_zfree(sc->uma, (void *)(ip->array[i]));
279f43b2bacSPoul-Henning Kamp 	}
280f43b2bacSPoul-Henning Kamp 	del_indir(ip);
281f43b2bacSPoul-Henning Kamp }
282f43b2bacSPoul-Henning Kamp 
283c6517568SPoul-Henning Kamp /*
2846c3cd0e2SMaxim Konovalov  * This function does the math and allocates the top level "indir" structure
285c6517568SPoul-Henning Kamp  * for a device of "size" sectors.
286c6517568SPoul-Henning Kamp  */
287c6517568SPoul-Henning Kamp 
288c6517568SPoul-Henning Kamp static struct indir *
289c6517568SPoul-Henning Kamp dimension(off_t size)
290c6517568SPoul-Henning Kamp {
291c6517568SPoul-Henning Kamp 	off_t rcnt;
292c6517568SPoul-Henning Kamp 	struct indir *ip;
293d12fc952SKonstantin Belousov 	int layer;
294c6517568SPoul-Henning Kamp 
295c6517568SPoul-Henning Kamp 	rcnt = size;
296c6517568SPoul-Henning Kamp 	layer = 0;
297c6517568SPoul-Henning Kamp 	while (rcnt > NINDIR) {
298c6517568SPoul-Henning Kamp 		rcnt /= NINDIR;
299c6517568SPoul-Henning Kamp 		layer++;
300c6517568SPoul-Henning Kamp 	}
301c6517568SPoul-Henning Kamp 
302c6517568SPoul-Henning Kamp 	/*
303c6517568SPoul-Henning Kamp 	 * XXX: the top layer is probably not fully populated, so we allocate
30483e13864SPoul-Henning Kamp 	 * too much space for ip->array in here.
305c6517568SPoul-Henning Kamp 	 */
30683e13864SPoul-Henning Kamp 	ip = malloc(sizeof *ip, M_MD, M_WAITOK | M_ZERO);
30783e13864SPoul-Henning Kamp 	ip->array = malloc(sizeof(uintptr_t) * NINDIR,
30883e13864SPoul-Henning Kamp 	    M_MDSECT, M_WAITOK | M_ZERO);
30983e13864SPoul-Henning Kamp 	ip->total = NINDIR;
31083e13864SPoul-Henning Kamp 	ip->shift = layer * nshift;
311c6517568SPoul-Henning Kamp 	return (ip);
312c6517568SPoul-Henning Kamp }
313c6517568SPoul-Henning Kamp 
314c6517568SPoul-Henning Kamp /*
315c6517568SPoul-Henning Kamp  * Read a given sector
316c6517568SPoul-Henning Kamp  */
317c6517568SPoul-Henning Kamp 
318c6517568SPoul-Henning Kamp static uintptr_t
319c6517568SPoul-Henning Kamp s_read(struct indir *ip, off_t offset)
320c6517568SPoul-Henning Kamp {
321c6517568SPoul-Henning Kamp 	struct indir *cip;
322c6517568SPoul-Henning Kamp 	int idx;
323c6517568SPoul-Henning Kamp 	uintptr_t up;
324c6517568SPoul-Henning Kamp 
325c6517568SPoul-Henning Kamp 	if (md_debug > 1)
3266569d6f3SMaxime Henrion 		printf("s_read(%jd)\n", (intmax_t)offset);
327c6517568SPoul-Henning Kamp 	up = 0;
328c6517568SPoul-Henning Kamp 	for (cip = ip; cip != NULL;) {
329c6517568SPoul-Henning Kamp 		if (cip->shift) {
330c6517568SPoul-Henning Kamp 			idx = (offset >> cip->shift) & NMASK;
331c6517568SPoul-Henning Kamp 			up = cip->array[idx];
332c6517568SPoul-Henning Kamp 			cip = (struct indir *)up;
333c6517568SPoul-Henning Kamp 			continue;
334c6517568SPoul-Henning Kamp 		}
335c6517568SPoul-Henning Kamp 		idx = offset & NMASK;
336c6517568SPoul-Henning Kamp 		return (cip->array[idx]);
337c6517568SPoul-Henning Kamp 	}
338c6517568SPoul-Henning Kamp 	return (0);
339c6517568SPoul-Henning Kamp }
340c6517568SPoul-Henning Kamp 
341c6517568SPoul-Henning Kamp /*
342c6517568SPoul-Henning Kamp  * Write a given sector, prune the tree if the value is 0
343c6517568SPoul-Henning Kamp  */
344c6517568SPoul-Henning Kamp 
345c6517568SPoul-Henning Kamp static int
346fde2a2e4SPoul-Henning Kamp s_write(struct indir *ip, off_t offset, uintptr_t ptr)
347c6517568SPoul-Henning Kamp {
348c6517568SPoul-Henning Kamp 	struct indir *cip, *lip[10];
349c6517568SPoul-Henning Kamp 	int idx, li;
350c6517568SPoul-Henning Kamp 	uintptr_t up;
351c6517568SPoul-Henning Kamp 
352c6517568SPoul-Henning Kamp 	if (md_debug > 1)
3536569d6f3SMaxime Henrion 		printf("s_write(%jd, %p)\n", (intmax_t)offset, (void *)ptr);
354c6517568SPoul-Henning Kamp 	up = 0;
355c6517568SPoul-Henning Kamp 	li = 0;
356c6517568SPoul-Henning Kamp 	cip = ip;
357c6517568SPoul-Henning Kamp 	for (;;) {
358c6517568SPoul-Henning Kamp 		lip[li++] = cip;
359c6517568SPoul-Henning Kamp 		if (cip->shift) {
360c6517568SPoul-Henning Kamp 			idx = (offset >> cip->shift) & NMASK;
361c6517568SPoul-Henning Kamp 			up = cip->array[idx];
362c6517568SPoul-Henning Kamp 			if (up != 0) {
363c6517568SPoul-Henning Kamp 				cip = (struct indir *)up;
364c6517568SPoul-Henning Kamp 				continue;
365c6517568SPoul-Henning Kamp 			}
366c6517568SPoul-Henning Kamp 			/* Allocate branch */
367c6517568SPoul-Henning Kamp 			cip->array[idx] =
368c6517568SPoul-Henning Kamp 			    (uintptr_t)new_indir(cip->shift - nshift);
369c6517568SPoul-Henning Kamp 			if (cip->array[idx] == 0)
370975b628fSPoul-Henning Kamp 				return (ENOSPC);
371c6517568SPoul-Henning Kamp 			cip->used++;
372c6517568SPoul-Henning Kamp 			up = cip->array[idx];
373c6517568SPoul-Henning Kamp 			cip = (struct indir *)up;
374c6517568SPoul-Henning Kamp 			continue;
375c6517568SPoul-Henning Kamp 		}
376c6517568SPoul-Henning Kamp 		/* leafnode */
377c6517568SPoul-Henning Kamp 		idx = offset & NMASK;
378c6517568SPoul-Henning Kamp 		up = cip->array[idx];
379c6517568SPoul-Henning Kamp 		if (up != 0)
380c6517568SPoul-Henning Kamp 			cip->used--;
381c6517568SPoul-Henning Kamp 		cip->array[idx] = ptr;
382c6517568SPoul-Henning Kamp 		if (ptr != 0)
383c6517568SPoul-Henning Kamp 			cip->used++;
384c6517568SPoul-Henning Kamp 		break;
385c6517568SPoul-Henning Kamp 	}
386c6517568SPoul-Henning Kamp 	if (cip->used != 0 || li == 1)
387c6517568SPoul-Henning Kamp 		return (0);
388c6517568SPoul-Henning Kamp 	li--;
389c6517568SPoul-Henning Kamp 	while (cip->used == 0 && cip != ip) {
390c6517568SPoul-Henning Kamp 		li--;
391c6517568SPoul-Henning Kamp 		idx = (offset >> lip[li]->shift) & NMASK;
392c6517568SPoul-Henning Kamp 		up = lip[li]->array[idx];
393c6517568SPoul-Henning Kamp 		KASSERT(up == (uintptr_t)cip, ("md screwed up"));
394c6517568SPoul-Henning Kamp 		del_indir(cip);
3954a6a94d8SArchie Cobbs 		lip[li]->array[idx] = 0;
396c6517568SPoul-Henning Kamp 		lip[li]->used--;
397c6517568SPoul-Henning Kamp 		cip = lip[li];
398c6517568SPoul-Henning Kamp 	}
399c6517568SPoul-Henning Kamp 	return (0);
400c6517568SPoul-Henning Kamp }
401c6517568SPoul-Henning Kamp 
4026f4f00f1SPoul-Henning Kamp 
4036f4f00f1SPoul-Henning Kamp static int
4046f4f00f1SPoul-Henning Kamp g_md_access(struct g_provider *pp, int r, int w, int e)
4056f4f00f1SPoul-Henning Kamp {
4066f4f00f1SPoul-Henning Kamp 	struct md_s *sc;
4076f4f00f1SPoul-Henning Kamp 
4086f4f00f1SPoul-Henning Kamp 	sc = pp->geom->softc;
40941c8b468SEdward Tomasz Napierala 	if (sc == NULL) {
41041c8b468SEdward Tomasz Napierala 		if (r <= 0 && w <= 0 && e <= 0)
41141c8b468SEdward Tomasz Napierala 			return (0);
4126b60a2cdSPoul-Henning Kamp 		return (ENXIO);
41341c8b468SEdward Tomasz Napierala 	}
4146f4f00f1SPoul-Henning Kamp 	r += pp->acr;
4156f4f00f1SPoul-Henning Kamp 	w += pp->acw;
4166f4f00f1SPoul-Henning Kamp 	e += pp->ace;
41786776891SChristian S.J. Peron 	if ((sc->flags & MD_READONLY) != 0 && w > 0)
41886776891SChristian S.J. Peron 		return (EROFS);
4196f4f00f1SPoul-Henning Kamp 	if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
4206f4f00f1SPoul-Henning Kamp 		sc->opencount = 1;
4216f4f00f1SPoul-Henning Kamp 	} else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
4226f4f00f1SPoul-Henning Kamp 		sc->opencount = 0;
4236f4f00f1SPoul-Henning Kamp 	}
4246f4f00f1SPoul-Henning Kamp 	return (0);
4256f4f00f1SPoul-Henning Kamp }
4266f4f00f1SPoul-Henning Kamp 
4276f4f00f1SPoul-Henning Kamp static void
4286f4f00f1SPoul-Henning Kamp g_md_start(struct bio *bp)
4296f4f00f1SPoul-Henning Kamp {
4306f4f00f1SPoul-Henning Kamp 	struct md_s *sc;
4316f4f00f1SPoul-Henning Kamp 
4326f4f00f1SPoul-Henning Kamp 	sc = bp->bio_to->geom->softc;
43340ea77a0SAlexander Motin 	if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE)) {
43440ea77a0SAlexander Motin 		mtx_lock(&sc->stat_mtx);
435a03be42dSMaxim Sobolev 		devstat_start_transaction_bio(sc->devstat, bp);
43640ea77a0SAlexander Motin 		mtx_unlock(&sc->stat_mtx);
43740ea77a0SAlexander Motin 	}
4380f8500a5SPoul-Henning Kamp 	mtx_lock(&sc->queue_mtx);
439891619a6SPoul-Henning Kamp 	bioq_disksort(&sc->bio_queue, bp);
4404b07ede4SPawel Jakub Dawidek 	mtx_unlock(&sc->queue_mtx);
441e4cdd0d4SPawel Jakub Dawidek 	wakeup(sc);
4426f4f00f1SPoul-Henning Kamp }
4436f4f00f1SPoul-Henning Kamp 
44459ec9023SKonstantin Belousov #define	MD_MALLOC_MOVE_ZERO	1
44559ec9023SKonstantin Belousov #define	MD_MALLOC_MOVE_FILL	2
44659ec9023SKonstantin Belousov #define	MD_MALLOC_MOVE_READ	3
44759ec9023SKonstantin Belousov #define	MD_MALLOC_MOVE_WRITE	4
44859ec9023SKonstantin Belousov #define	MD_MALLOC_MOVE_CMP	5
44959ec9023SKonstantin Belousov 
45059ec9023SKonstantin Belousov static int
451a9934668SKenneth D. Merry md_malloc_move_ma(vm_page_t **mp, int *ma_offs, unsigned sectorsize,
45259ec9023SKonstantin Belousov     void *ptr, u_char fill, int op)
45359ec9023SKonstantin Belousov {
45459ec9023SKonstantin Belousov 	struct sf_buf *sf;
45559ec9023SKonstantin Belousov 	vm_page_t m, *mp1;
45659ec9023SKonstantin Belousov 	char *p, first;
45759ec9023SKonstantin Belousov 	off_t *uc;
45859ec9023SKonstantin Belousov 	unsigned n;
45959ec9023SKonstantin Belousov 	int error, i, ma_offs1, sz, first_read;
46059ec9023SKonstantin Belousov 
46159ec9023SKonstantin Belousov 	m = NULL;
46259ec9023SKonstantin Belousov 	error = 0;
46359ec9023SKonstantin Belousov 	sf = NULL;
46459ec9023SKonstantin Belousov 	/* if (op == MD_MALLOC_MOVE_CMP) { gcc */
46559ec9023SKonstantin Belousov 		first = 0;
46659ec9023SKonstantin Belousov 		first_read = 0;
46759ec9023SKonstantin Belousov 		uc = ptr;
46859ec9023SKonstantin Belousov 		mp1 = *mp;
46959ec9023SKonstantin Belousov 		ma_offs1 = *ma_offs;
47059ec9023SKonstantin Belousov 	/* } */
47159ec9023SKonstantin Belousov 	sched_pin();
47259ec9023SKonstantin Belousov 	for (n = sectorsize; n != 0; n -= sz) {
47359ec9023SKonstantin Belousov 		sz = imin(PAGE_SIZE - *ma_offs, n);
47459ec9023SKonstantin Belousov 		if (m != **mp) {
47559ec9023SKonstantin Belousov 			if (sf != NULL)
47659ec9023SKonstantin Belousov 				sf_buf_free(sf);
47759ec9023SKonstantin Belousov 			m = **mp;
47859ec9023SKonstantin Belousov 			sf = sf_buf_alloc(m, SFB_CPUPRIVATE |
47959ec9023SKonstantin Belousov 			    (md_malloc_wait ? 0 : SFB_NOWAIT));
48059ec9023SKonstantin Belousov 			if (sf == NULL) {
48159ec9023SKonstantin Belousov 				error = ENOMEM;
48259ec9023SKonstantin Belousov 				break;
48359ec9023SKonstantin Belousov 			}
48459ec9023SKonstantin Belousov 		}
48559ec9023SKonstantin Belousov 		p = (char *)sf_buf_kva(sf) + *ma_offs;
48659ec9023SKonstantin Belousov 		switch (op) {
48759ec9023SKonstantin Belousov 		case MD_MALLOC_MOVE_ZERO:
48859ec9023SKonstantin Belousov 			bzero(p, sz);
48959ec9023SKonstantin Belousov 			break;
49059ec9023SKonstantin Belousov 		case MD_MALLOC_MOVE_FILL:
49159ec9023SKonstantin Belousov 			memset(p, fill, sz);
49259ec9023SKonstantin Belousov 			break;
49359ec9023SKonstantin Belousov 		case MD_MALLOC_MOVE_READ:
49459ec9023SKonstantin Belousov 			bcopy(ptr, p, sz);
49559ec9023SKonstantin Belousov 			cpu_flush_dcache(p, sz);
49659ec9023SKonstantin Belousov 			break;
49759ec9023SKonstantin Belousov 		case MD_MALLOC_MOVE_WRITE:
49859ec9023SKonstantin Belousov 			bcopy(p, ptr, sz);
49959ec9023SKonstantin Belousov 			break;
50059ec9023SKonstantin Belousov 		case MD_MALLOC_MOVE_CMP:
50159ec9023SKonstantin Belousov 			for (i = 0; i < sz; i++, p++) {
50259ec9023SKonstantin Belousov 				if (!first_read) {
50359ec9023SKonstantin Belousov 					*uc = (u_char)*p;
50459ec9023SKonstantin Belousov 					first = *p;
50559ec9023SKonstantin Belousov 					first_read = 1;
50659ec9023SKonstantin Belousov 				} else if (*p != first) {
50759ec9023SKonstantin Belousov 					error = EDOOFUS;
50859ec9023SKonstantin Belousov 					break;
50959ec9023SKonstantin Belousov 				}
51059ec9023SKonstantin Belousov 			}
51159ec9023SKonstantin Belousov 			break;
51259ec9023SKonstantin Belousov 		default:
513a9934668SKenneth D. Merry 			KASSERT(0, ("md_malloc_move_ma unknown op %d\n", op));
51459ec9023SKonstantin Belousov 			break;
51559ec9023SKonstantin Belousov 		}
51659ec9023SKonstantin Belousov 		if (error != 0)
51759ec9023SKonstantin Belousov 			break;
51859ec9023SKonstantin Belousov 		*ma_offs += sz;
51959ec9023SKonstantin Belousov 		*ma_offs %= PAGE_SIZE;
52059ec9023SKonstantin Belousov 		if (*ma_offs == 0)
52159ec9023SKonstantin Belousov 			(*mp)++;
52259ec9023SKonstantin Belousov 		ptr = (char *)ptr + sz;
52359ec9023SKonstantin Belousov 	}
52459ec9023SKonstantin Belousov 
52559ec9023SKonstantin Belousov 	if (sf != NULL)
52659ec9023SKonstantin Belousov 		sf_buf_free(sf);
52759ec9023SKonstantin Belousov 	sched_unpin();
52859ec9023SKonstantin Belousov 	if (op == MD_MALLOC_MOVE_CMP && error != 0) {
52959ec9023SKonstantin Belousov 		*mp = mp1;
53059ec9023SKonstantin Belousov 		*ma_offs = ma_offs1;
53159ec9023SKonstantin Belousov 	}
53259ec9023SKonstantin Belousov 	return (error);
53359ec9023SKonstantin Belousov }
53459ec9023SKonstantin Belousov 
535b4a4f93cSPoul-Henning Kamp static int
536a9934668SKenneth D. Merry md_malloc_move_vlist(bus_dma_segment_t **pvlist, int *pma_offs,
537a9934668SKenneth D. Merry     unsigned len, void *ptr, u_char fill, int op)
538a9934668SKenneth D. Merry {
539a9934668SKenneth D. Merry 	bus_dma_segment_t *vlist;
540a9934668SKenneth D. Merry 	uint8_t *p, *end, first;
541a9934668SKenneth D. Merry 	off_t *uc;
542a9934668SKenneth D. Merry 	int ma_offs, seg_len;
543a9934668SKenneth D. Merry 
544a9934668SKenneth D. Merry 	vlist = *pvlist;
545a9934668SKenneth D. Merry 	ma_offs = *pma_offs;
546a9934668SKenneth D. Merry 	uc = ptr;
547a9934668SKenneth D. Merry 
548a9934668SKenneth D. Merry 	for (; len != 0; len -= seg_len) {
549a9934668SKenneth D. Merry 		seg_len = imin(vlist->ds_len - ma_offs, len);
550a9934668SKenneth D. Merry 		p = (uint8_t *)(uintptr_t)vlist->ds_addr + ma_offs;
551a9934668SKenneth D. Merry 		switch (op) {
552a9934668SKenneth D. Merry 		case MD_MALLOC_MOVE_ZERO:
553a9934668SKenneth D. Merry 			bzero(p, seg_len);
554a9934668SKenneth D. Merry 			break;
555a9934668SKenneth D. Merry 		case MD_MALLOC_MOVE_FILL:
556a9934668SKenneth D. Merry 			memset(p, fill, seg_len);
557a9934668SKenneth D. Merry 			break;
558a9934668SKenneth D. Merry 		case MD_MALLOC_MOVE_READ:
559a9934668SKenneth D. Merry 			bcopy(ptr, p, seg_len);
560a9934668SKenneth D. Merry 			cpu_flush_dcache(p, seg_len);
561a9934668SKenneth D. Merry 			break;
562a9934668SKenneth D. Merry 		case MD_MALLOC_MOVE_WRITE:
563a9934668SKenneth D. Merry 			bcopy(p, ptr, seg_len);
564a9934668SKenneth D. Merry 			break;
565a9934668SKenneth D. Merry 		case MD_MALLOC_MOVE_CMP:
566a9934668SKenneth D. Merry 			end = p + seg_len;
567a9934668SKenneth D. Merry 			first = *uc = *p;
568a9934668SKenneth D. Merry 			/* Confirm all following bytes match the first */
569a9934668SKenneth D. Merry 			while (++p < end) {
570a9934668SKenneth D. Merry 				if (*p != first)
571a9934668SKenneth D. Merry 					return (EDOOFUS);
572a9934668SKenneth D. Merry 			}
573a9934668SKenneth D. Merry 			break;
574a9934668SKenneth D. Merry 		default:
575a9934668SKenneth D. Merry 			KASSERT(0, ("md_malloc_move_vlist unknown op %d\n", op));
576a9934668SKenneth D. Merry 			break;
577a9934668SKenneth D. Merry 		}
578a9934668SKenneth D. Merry 
579a9934668SKenneth D. Merry 		ma_offs += seg_len;
580a9934668SKenneth D. Merry 		if (ma_offs == vlist->ds_len) {
581a9934668SKenneth D. Merry 			ma_offs = 0;
582a9934668SKenneth D. Merry 			vlist++;
583a9934668SKenneth D. Merry 		}
584a9934668SKenneth D. Merry 		ptr = (uint8_t *)ptr + seg_len;
585a9934668SKenneth D. Merry 	}
586a9934668SKenneth D. Merry 	*pvlist = vlist;
587a9934668SKenneth D. Merry 	*pma_offs = ma_offs;
588a9934668SKenneth D. Merry 
589a9934668SKenneth D. Merry 	return (0);
590a9934668SKenneth D. Merry }
591a9934668SKenneth D. Merry 
592a9934668SKenneth D. Merry static int
593b4a4f93cSPoul-Henning Kamp mdstart_malloc(struct md_s *sc, struct bio *bp)
59400a6a3c6SPoul-Henning Kamp {
595c6517568SPoul-Henning Kamp 	u_char *dst;
59659ec9023SKonstantin Belousov 	vm_page_t *m;
597a9934668SKenneth D. Merry 	bus_dma_segment_t *vlist;
59859ec9023SKonstantin Belousov 	int i, error, error1, ma_offs, notmapped;
599b830359bSPawel Jakub Dawidek 	off_t secno, nsec, uc;
600c6517568SPoul-Henning Kamp 	uintptr_t sp, osp;
60100a6a3c6SPoul-Henning Kamp 
6025541f25eSPawel Jakub Dawidek 	switch (bp->bio_cmd) {
6035541f25eSPawel Jakub Dawidek 	case BIO_READ:
6045541f25eSPawel Jakub Dawidek 	case BIO_WRITE:
6055541f25eSPawel Jakub Dawidek 	case BIO_DELETE:
6065541f25eSPawel Jakub Dawidek 		break;
6075541f25eSPawel Jakub Dawidek 	default:
6085541f25eSPawel Jakub Dawidek 		return (EOPNOTSUPP);
6095541f25eSPawel Jakub Dawidek 	}
6105541f25eSPawel Jakub Dawidek 
61159ec9023SKonstantin Belousov 	notmapped = (bp->bio_flags & BIO_UNMAPPED) != 0;
612a9934668SKenneth D. Merry 	vlist = (bp->bio_flags & BIO_VLIST) != 0 ?
613a9934668SKenneth D. Merry 	    (bus_dma_segment_t *)bp->bio_data : NULL;
61459ec9023SKonstantin Belousov 	if (notmapped) {
61559ec9023SKonstantin Belousov 		m = bp->bio_ma;
61659ec9023SKonstantin Belousov 		ma_offs = bp->bio_ma_offset;
61759ec9023SKonstantin Belousov 		dst = NULL;
618a9934668SKenneth D. Merry 		KASSERT(vlist == NULL, ("vlists cannot be unmapped"));
619a9934668SKenneth D. Merry 	} else if (vlist != NULL) {
620a9934668SKenneth D. Merry 		ma_offs = bp->bio_ma_offset;
621a9934668SKenneth D. Merry 		dst = NULL;
62259ec9023SKonstantin Belousov 	} else {
62359ec9023SKonstantin Belousov 		dst = bp->bio_data;
62459ec9023SKonstantin Belousov 	}
62559ec9023SKonstantin Belousov 
626b830359bSPawel Jakub Dawidek 	nsec = bp->bio_length / sc->sectorsize;
627b830359bSPawel Jakub Dawidek 	secno = bp->bio_offset / sc->sectorsize;
628c6517568SPoul-Henning Kamp 	error = 0;
62900a6a3c6SPoul-Henning Kamp 	while (nsec--) {
630fde2a2e4SPoul-Henning Kamp 		osp = s_read(sc->indir, secno);
6318177437dSPoul-Henning Kamp 		if (bp->bio_cmd == BIO_DELETE) {
632fde2a2e4SPoul-Henning Kamp 			if (osp != 0)
633fde2a2e4SPoul-Henning Kamp 				error = s_write(sc->indir, secno, 0);
6348177437dSPoul-Henning Kamp 		} else if (bp->bio_cmd == BIO_READ) {
63559ec9023SKonstantin Belousov 			if (osp == 0) {
63659ec9023SKonstantin Belousov 				if (notmapped) {
637a9934668SKenneth D. Merry 					error = md_malloc_move_ma(&m, &ma_offs,
63859ec9023SKonstantin Belousov 					    sc->sectorsize, NULL, 0,
63959ec9023SKonstantin Belousov 					    MD_MALLOC_MOVE_ZERO);
640a9934668SKenneth D. Merry 				} else if (vlist != NULL) {
641a9934668SKenneth D. Merry 					error = md_malloc_move_vlist(&vlist,
642a9934668SKenneth D. Merry 					    &ma_offs, sc->sectorsize, NULL, 0,
643a9934668SKenneth D. Merry 					    MD_MALLOC_MOVE_ZERO);
64459ec9023SKonstantin Belousov 				} else
645b830359bSPawel Jakub Dawidek 					bzero(dst, sc->sectorsize);
64659ec9023SKonstantin Belousov 			} else if (osp <= 255) {
64759ec9023SKonstantin Belousov 				if (notmapped) {
648a9934668SKenneth D. Merry 					error = md_malloc_move_ma(&m, &ma_offs,
64959ec9023SKonstantin Belousov 					    sc->sectorsize, NULL, osp,
65059ec9023SKonstantin Belousov 					    MD_MALLOC_MOVE_FILL);
651a9934668SKenneth D. Merry 				} else if (vlist != NULL) {
652a9934668SKenneth D. Merry 					error = md_malloc_move_vlist(&vlist,
653a9934668SKenneth D. Merry 					    &ma_offs, sc->sectorsize, NULL, osp,
654a9934668SKenneth D. Merry 					    MD_MALLOC_MOVE_FILL);
65559ec9023SKonstantin Belousov 				} else
656dbb95048SMarcel Moolenaar 					memset(dst, osp, sc->sectorsize);
65759ec9023SKonstantin Belousov 			} else {
65859ec9023SKonstantin Belousov 				if (notmapped) {
659a9934668SKenneth D. Merry 					error = md_malloc_move_ma(&m, &ma_offs,
66059ec9023SKonstantin Belousov 					    sc->sectorsize, (void *)osp, 0,
66159ec9023SKonstantin Belousov 					    MD_MALLOC_MOVE_READ);
662a9934668SKenneth D. Merry 				} else if (vlist != NULL) {
663a9934668SKenneth D. Merry 					error = md_malloc_move_vlist(&vlist,
664a9934668SKenneth D. Merry 					    &ma_offs, sc->sectorsize,
665a9934668SKenneth D. Merry 					    (void *)osp, 0,
666a9934668SKenneth D. Merry 					    MD_MALLOC_MOVE_READ);
66759ec9023SKonstantin Belousov 				} else {
668b830359bSPawel Jakub Dawidek 					bcopy((void *)osp, dst, sc->sectorsize);
669dbb95048SMarcel Moolenaar 					cpu_flush_dcache(dst, sc->sectorsize);
670dbb95048SMarcel Moolenaar 				}
67159ec9023SKonstantin Belousov 			}
672fde2a2e4SPoul-Henning Kamp 			osp = 0;
673c6517568SPoul-Henning Kamp 		} else if (bp->bio_cmd == BIO_WRITE) {
6748f8def9eSPoul-Henning Kamp 			if (sc->flags & MD_COMPRESS) {
67559ec9023SKonstantin Belousov 				if (notmapped) {
676a9934668SKenneth D. Merry 					error1 = md_malloc_move_ma(&m, &ma_offs,
67759ec9023SKonstantin Belousov 					    sc->sectorsize, &uc, 0,
67859ec9023SKonstantin Belousov 					    MD_MALLOC_MOVE_CMP);
67959ec9023SKonstantin Belousov 					i = error1 == 0 ? sc->sectorsize : 0;
680a9934668SKenneth D. Merry 				} else if (vlist != NULL) {
681a9934668SKenneth D. Merry 					error1 = md_malloc_move_vlist(&vlist,
682a9934668SKenneth D. Merry 					    &ma_offs, sc->sectorsize, &uc, 0,
683a9934668SKenneth D. Merry 					    MD_MALLOC_MOVE_CMP);
684a9934668SKenneth D. Merry 					i = error1 == 0 ? sc->sectorsize : 0;
68559ec9023SKonstantin Belousov 				} else {
68600a6a3c6SPoul-Henning Kamp 					uc = dst[0];
68759ec9023SKonstantin Belousov 					for (i = 1; i < sc->sectorsize; i++) {
68800a6a3c6SPoul-Henning Kamp 						if (dst[i] != uc)
68900a6a3c6SPoul-Henning Kamp 							break;
69059ec9023SKonstantin Belousov 					}
69159ec9023SKonstantin Belousov 				}
6928f8def9eSPoul-Henning Kamp 			} else {
6938f8def9eSPoul-Henning Kamp 				i = 0;
6948f8def9eSPoul-Henning Kamp 				uc = 0;
6958f8def9eSPoul-Henning Kamp 			}
696b830359bSPawel Jakub Dawidek 			if (i == sc->sectorsize) {
697fde2a2e4SPoul-Henning Kamp 				if (osp != uc)
698fde2a2e4SPoul-Henning Kamp 					error = s_write(sc->indir, secno, uc);
69900a6a3c6SPoul-Henning Kamp 			} else {
700fde2a2e4SPoul-Henning Kamp 				if (osp <= 255) {
701b830359bSPawel Jakub Dawidek 					sp = (uintptr_t)uma_zalloc(sc->uma,
702c44d423eSKonstantin Belousov 					    md_malloc_wait ? M_WAITOK :
703b830359bSPawel Jakub Dawidek 					    M_NOWAIT);
704c6517568SPoul-Henning Kamp 					if (sp == 0) {
705fde2a2e4SPoul-Henning Kamp 						error = ENOSPC;
706fde2a2e4SPoul-Henning Kamp 						break;
707fde2a2e4SPoul-Henning Kamp 					}
70859ec9023SKonstantin Belousov 					if (notmapped) {
709a9934668SKenneth D. Merry 						error = md_malloc_move_ma(&m,
71059ec9023SKonstantin Belousov 						    &ma_offs, sc->sectorsize,
71159ec9023SKonstantin Belousov 						    (void *)sp, 0,
71259ec9023SKonstantin Belousov 						    MD_MALLOC_MOVE_WRITE);
713a9934668SKenneth D. Merry 					} else if (vlist != NULL) {
714a9934668SKenneth D. Merry 						error = md_malloc_move_vlist(
715a9934668SKenneth D. Merry 						    &vlist, &ma_offs,
716a9934668SKenneth D. Merry 						    sc->sectorsize, (void *)sp,
717a9934668SKenneth D. Merry 						    0, MD_MALLOC_MOVE_WRITE);
71859ec9023SKonstantin Belousov 					} else {
71959ec9023SKonstantin Belousov 						bcopy(dst, (void *)sp,
72059ec9023SKonstantin Belousov 						    sc->sectorsize);
72159ec9023SKonstantin Belousov 					}
722fde2a2e4SPoul-Henning Kamp 					error = s_write(sc->indir, secno, sp);
723c6517568SPoul-Henning Kamp 				} else {
72459ec9023SKonstantin Belousov 					if (notmapped) {
725a9934668SKenneth D. Merry 						error = md_malloc_move_ma(&m,
72659ec9023SKonstantin Belousov 						    &ma_offs, sc->sectorsize,
72759ec9023SKonstantin Belousov 						    (void *)osp, 0,
72859ec9023SKonstantin Belousov 						    MD_MALLOC_MOVE_WRITE);
729a9934668SKenneth D. Merry 					} else if (vlist != NULL) {
730a9934668SKenneth D. Merry 						error = md_malloc_move_vlist(
731a9934668SKenneth D. Merry 						    &vlist, &ma_offs,
732a9934668SKenneth D. Merry 						    sc->sectorsize, (void *)osp,
733a9934668SKenneth D. Merry 						    0, MD_MALLOC_MOVE_WRITE);
73459ec9023SKonstantin Belousov 					} else {
73559ec9023SKonstantin Belousov 						bcopy(dst, (void *)osp,
73659ec9023SKonstantin Belousov 						    sc->sectorsize);
73759ec9023SKonstantin Belousov 					}
738fde2a2e4SPoul-Henning Kamp 					osp = 0;
73900a6a3c6SPoul-Henning Kamp 				}
74000a6a3c6SPoul-Henning Kamp 			}
741c6517568SPoul-Henning Kamp 		} else {
742c6517568SPoul-Henning Kamp 			error = EOPNOTSUPP;
743c6517568SPoul-Henning Kamp 		}
744c6517568SPoul-Henning Kamp 		if (osp > 255)
745f43b2bacSPoul-Henning Kamp 			uma_zfree(sc->uma, (void*)osp);
746e3ed29a7SPawel Jakub Dawidek 		if (error != 0)
747c6517568SPoul-Henning Kamp 			break;
74800a6a3c6SPoul-Henning Kamp 		secno++;
749a9934668SKenneth D. Merry 		if (!notmapped && vlist == NULL)
750b830359bSPawel Jakub Dawidek 			dst += sc->sectorsize;
75100a6a3c6SPoul-Henning Kamp 	}
7528177437dSPoul-Henning Kamp 	bp->bio_resid = 0;
753c6517568SPoul-Henning Kamp 	return (error);
75400a6a3c6SPoul-Henning Kamp }
75500a6a3c6SPoul-Henning Kamp 
756a9934668SKenneth D. Merry static void
757a9934668SKenneth D. Merry mdcopyto_vlist(void *src, bus_dma_segment_t *vlist, off_t offset, off_t len)
758a9934668SKenneth D. Merry {
759a9934668SKenneth D. Merry 	off_t seg_len;
760a9934668SKenneth D. Merry 
761a9934668SKenneth D. Merry 	while (offset >= vlist->ds_len) {
762a9934668SKenneth D. Merry 		offset -= vlist->ds_len;
763a9934668SKenneth D. Merry 		vlist++;
764a9934668SKenneth D. Merry 	}
765a9934668SKenneth D. Merry 
766a9934668SKenneth D. Merry 	while (len != 0) {
767a9934668SKenneth D. Merry 		seg_len = omin(len, vlist->ds_len - offset);
768a9934668SKenneth D. Merry 		bcopy(src, (void *)(uintptr_t)(vlist->ds_addr + offset),
769a9934668SKenneth D. Merry 		    seg_len);
770a9934668SKenneth D. Merry 		offset = 0;
771a9934668SKenneth D. Merry 		src = (uint8_t *)src + seg_len;
772a9934668SKenneth D. Merry 		len -= seg_len;
773a9934668SKenneth D. Merry 		vlist++;
774a9934668SKenneth D. Merry 	}
775a9934668SKenneth D. Merry }
776a9934668SKenneth D. Merry 
777a9934668SKenneth D. Merry static void
778a9934668SKenneth D. Merry mdcopyfrom_vlist(bus_dma_segment_t *vlist, off_t offset, void *dst, off_t len)
779a9934668SKenneth D. Merry {
780a9934668SKenneth D. Merry 	off_t seg_len;
781a9934668SKenneth D. Merry 
782a9934668SKenneth D. Merry 	while (offset >= vlist->ds_len) {
783a9934668SKenneth D. Merry 		offset -= vlist->ds_len;
784a9934668SKenneth D. Merry 		vlist++;
785a9934668SKenneth D. Merry 	}
786a9934668SKenneth D. Merry 
787a9934668SKenneth D. Merry 	while (len != 0) {
788a9934668SKenneth D. Merry 		seg_len = omin(len, vlist->ds_len - offset);
789a9934668SKenneth D. Merry 		bcopy((void *)(uintptr_t)(vlist->ds_addr + offset), dst,
790a9934668SKenneth D. Merry 		    seg_len);
791a9934668SKenneth D. Merry 		offset = 0;
792a9934668SKenneth D. Merry 		dst = (uint8_t *)dst + seg_len;
793a9934668SKenneth D. Merry 		len -= seg_len;
794a9934668SKenneth D. Merry 		vlist++;
795a9934668SKenneth D. Merry 	}
796a9934668SKenneth D. Merry }
797a9934668SKenneth D. Merry 
798b4a4f93cSPoul-Henning Kamp static int
799b4a4f93cSPoul-Henning Kamp mdstart_preload(struct md_s *sc, struct bio *bp)
80071e4fff8SPoul-Henning Kamp {
801a9934668SKenneth D. Merry 	uint8_t *p;
80271e4fff8SPoul-Henning Kamp 
803a9934668SKenneth D. Merry 	p = sc->pl_ptr + bp->bio_offset;
804a8a58d03SPawel Jakub Dawidek 	switch (bp->bio_cmd) {
805a8a58d03SPawel Jakub Dawidek 	case BIO_READ:
806a9934668SKenneth D. Merry 		if ((bp->bio_flags & BIO_VLIST) != 0) {
807a9934668SKenneth D. Merry 			mdcopyto_vlist(p, (bus_dma_segment_t *)bp->bio_data,
808a9934668SKenneth D. Merry 			    bp->bio_ma_offset, bp->bio_length);
809a9934668SKenneth D. Merry 		} else {
810a9934668SKenneth D. Merry 			bcopy(p, bp->bio_data, bp->bio_length);
811a9934668SKenneth D. Merry 		}
812dbb95048SMarcel Moolenaar 		cpu_flush_dcache(bp->bio_data, bp->bio_length);
813a8a58d03SPawel Jakub Dawidek 		break;
814a8a58d03SPawel Jakub Dawidek 	case BIO_WRITE:
815a9934668SKenneth D. Merry 		if ((bp->bio_flags & BIO_VLIST) != 0) {
816a9934668SKenneth D. Merry 			mdcopyfrom_vlist((bus_dma_segment_t *)bp->bio_data,
817a9934668SKenneth D. Merry 			    bp->bio_ma_offset, p, bp->bio_length);
818a9934668SKenneth D. Merry 		} else {
819a9934668SKenneth D. Merry 			bcopy(bp->bio_data, p, bp->bio_length);
820a9934668SKenneth D. Merry 		}
821a8a58d03SPawel Jakub Dawidek 		break;
82271e4fff8SPoul-Henning Kamp 	}
8238177437dSPoul-Henning Kamp 	bp->bio_resid = 0;
824b4a4f93cSPoul-Henning Kamp 	return (0);
82571e4fff8SPoul-Henning Kamp }
82671e4fff8SPoul-Henning Kamp 
827b4a4f93cSPoul-Henning Kamp static int
828b4a4f93cSPoul-Henning Kamp mdstart_vnode(struct md_s *sc, struct bio *bp)
8298f8def9eSPoul-Henning Kamp {
8305050aa86SKonstantin Belousov 	int error;
8318f8def9eSPoul-Henning Kamp 	struct uio auio;
8328f8def9eSPoul-Henning Kamp 	struct iovec aiov;
833a9934668SKenneth D. Merry 	struct iovec *piov;
8348f8def9eSPoul-Henning Kamp 	struct mount *mp;
8355541f25eSPawel Jakub Dawidek 	struct vnode *vp;
83659ec9023SKonstantin Belousov 	struct buf *pb;
837a9934668SKenneth D. Merry 	bus_dma_segment_t *vlist;
8385541f25eSPawel Jakub Dawidek 	struct thread *td;
839d5f998baSKonstantin Belousov 	off_t iolen, len, zerosize;
840d5f998baSKonstantin Belousov 	int ma_offs, npages;
8415541f25eSPawel Jakub Dawidek 
8425541f25eSPawel Jakub Dawidek 	switch (bp->bio_cmd) {
8435541f25eSPawel Jakub Dawidek 	case BIO_READ:
844a9934668SKenneth D. Merry 		auio.uio_rw = UIO_READ;
845a9934668SKenneth D. Merry 		break;
8465541f25eSPawel Jakub Dawidek 	case BIO_WRITE:
8470abd21bdSDag-Erling Smørgrav 	case BIO_DELETE:
848a9934668SKenneth D. Merry 		auio.uio_rw = UIO_WRITE;
849a9934668SKenneth D. Merry 		break;
8505541f25eSPawel Jakub Dawidek 	case BIO_FLUSH:
8515541f25eSPawel Jakub Dawidek 		break;
8525541f25eSPawel Jakub Dawidek 	default:
8535541f25eSPawel Jakub Dawidek 		return (EOPNOTSUPP);
8545541f25eSPawel Jakub Dawidek 	}
8555541f25eSPawel Jakub Dawidek 
8565541f25eSPawel Jakub Dawidek 	td = curthread;
8575541f25eSPawel Jakub Dawidek 	vp = sc->vnode;
858a9934668SKenneth D. Merry 	pb = NULL;
859a9934668SKenneth D. Merry 	piov = NULL;
860a9934668SKenneth D. Merry 	ma_offs = bp->bio_ma_offset;
861d5f998baSKonstantin Belousov 	len = bp->bio_length;
8628f8def9eSPoul-Henning Kamp 
8638f8def9eSPoul-Henning Kamp 	/*
8648f8def9eSPoul-Henning Kamp 	 * VNODE I/O
8658f8def9eSPoul-Henning Kamp 	 *
8668f8def9eSPoul-Henning Kamp 	 * If an error occurs, we set BIO_ERROR but we do not set
8678f8def9eSPoul-Henning Kamp 	 * B_INVAL because (for a write anyway), the buffer is
8688f8def9eSPoul-Henning Kamp 	 * still valid.
8698f8def9eSPoul-Henning Kamp 	 */
8708f8def9eSPoul-Henning Kamp 
8715541f25eSPawel Jakub Dawidek 	if (bp->bio_cmd == BIO_FLUSH) {
8725541f25eSPawel Jakub Dawidek 		(void) vn_start_write(vp, &mp, V_WAIT);
873cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
8745541f25eSPawel Jakub Dawidek 		error = VOP_FSYNC(vp, MNT_WAIT, td);
87522db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
8765541f25eSPawel Jakub Dawidek 		vn_finished_write(mp);
8775541f25eSPawel Jakub Dawidek 		return (error);
8785541f25eSPawel Jakub Dawidek 	}
8795541f25eSPawel Jakub Dawidek 
880a9934668SKenneth D. Merry 	auio.uio_offset = (vm_ooffset_t)bp->bio_offset;
881a9934668SKenneth D. Merry 	auio.uio_resid = bp->bio_length;
882a9934668SKenneth D. Merry 	auio.uio_segflg = UIO_SYSSPACE;
883a9934668SKenneth D. Merry 	auio.uio_td = td;
8848f8def9eSPoul-Henning Kamp 
8850abd21bdSDag-Erling Smørgrav 	if (bp->bio_cmd == BIO_DELETE) {
886a9934668SKenneth D. Merry 		/*
887a9934668SKenneth D. Merry 		 * Emulate BIO_DELETE by writing zeros.
888a9934668SKenneth D. Merry 		 */
88989cb2a19SMatthew D Fleming 		zerosize = ZERO_REGION_SIZE -
89089cb2a19SMatthew D Fleming 		    (ZERO_REGION_SIZE % sc->sectorsize);
891a9934668SKenneth D. Merry 		auio.uio_iovcnt = howmany(bp->bio_length, zerosize);
892a9934668SKenneth D. Merry 		piov = malloc(sizeof(*piov) * auio.uio_iovcnt, M_MD, M_WAITOK);
893a9934668SKenneth D. Merry 		auio.uio_iov = piov;
894a9934668SKenneth D. Merry 		while (len > 0) {
895a9934668SKenneth D. Merry 			piov->iov_base = __DECONST(void *, zero_region);
896a9934668SKenneth D. Merry 			piov->iov_len = len;
897a9934668SKenneth D. Merry 			if (len > zerosize)
898a9934668SKenneth D. Merry 				piov->iov_len = zerosize;
899a9934668SKenneth D. Merry 			len -= piov->iov_len;
900a9934668SKenneth D. Merry 			piov++;
9010abd21bdSDag-Erling Smørgrav 		}
902a9934668SKenneth D. Merry 		piov = auio.uio_iov;
903a9934668SKenneth D. Merry 	} else if ((bp->bio_flags & BIO_VLIST) != 0) {
904a9934668SKenneth D. Merry 		piov = malloc(sizeof(*piov) * bp->bio_ma_n, M_MD, M_WAITOK);
905a9934668SKenneth D. Merry 		auio.uio_iov = piov;
906a9934668SKenneth D. Merry 		vlist = (bus_dma_segment_t *)bp->bio_data;
907a9934668SKenneth D. Merry 		while (len > 0) {
908a9934668SKenneth D. Merry 			piov->iov_base = (void *)(uintptr_t)(vlist->ds_addr +
909a9934668SKenneth D. Merry 			    ma_offs);
910a9934668SKenneth D. Merry 			piov->iov_len = vlist->ds_len - ma_offs;
911a9934668SKenneth D. Merry 			if (piov->iov_len > len)
912a9934668SKenneth D. Merry 				piov->iov_len = len;
913a9934668SKenneth D. Merry 			len -= piov->iov_len;
914a9934668SKenneth D. Merry 			ma_offs = 0;
915a9934668SKenneth D. Merry 			vlist++;
916a9934668SKenneth D. Merry 			piov++;
9170abd21bdSDag-Erling Smørgrav 		}
918a9934668SKenneth D. Merry 		auio.uio_iovcnt = piov - auio.uio_iov;
919a9934668SKenneth D. Merry 		piov = auio.uio_iov;
920a9934668SKenneth D. Merry 	} else if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
92159ec9023SKonstantin Belousov 		pb = getpbuf(&md_vnode_pbuf_freecnt);
922d5f998baSKonstantin Belousov 		bp->bio_resid = len;
923d5f998baSKonstantin Belousov unmapped_step:
924d5f998baSKonstantin Belousov 		npages = atop(min(MAXPHYS, round_page(len + (ma_offs &
925d5f998baSKonstantin Belousov 		    PAGE_MASK))));
926d5f998baSKonstantin Belousov 		iolen = min(ptoa(npages) - (ma_offs & PAGE_MASK), len);
927d5f998baSKonstantin Belousov 		KASSERT(iolen > 0, ("zero iolen"));
928d5f998baSKonstantin Belousov 		pmap_qenter((vm_offset_t)pb->b_data,
929d5f998baSKonstantin Belousov 		    &bp->bio_ma[atop(ma_offs)], npages);
930d5f998baSKonstantin Belousov 		aiov.iov_base = (void *)((vm_offset_t)pb->b_data +
931d5f998baSKonstantin Belousov 		    (ma_offs & PAGE_MASK));
932d5f998baSKonstantin Belousov 		aiov.iov_len = iolen;
9338f8def9eSPoul-Henning Kamp 		auio.uio_iov = &aiov;
9348f8def9eSPoul-Henning Kamp 		auio.uio_iovcnt = 1;
935d5f998baSKonstantin Belousov 		auio.uio_resid = iolen;
936a9934668SKenneth D. Merry 	} else {
937a9934668SKenneth D. Merry 		aiov.iov_base = bp->bio_data;
938a9934668SKenneth D. Merry 		aiov.iov_len = bp->bio_length;
939a9934668SKenneth D. Merry 		auio.uio_iov = &aiov;
940a9934668SKenneth D. Merry 		auio.uio_iovcnt = 1;
941a9934668SKenneth D. Merry 	}
9427e76bb56SMatthew Dillon 	/*
9437e76bb56SMatthew Dillon 	 * When reading set IO_DIRECT to try to avoid double-caching
94417a13919SPoul-Henning Kamp 	 * the data.  When writing IO_DIRECT is not optimal.
9457e76bb56SMatthew Dillon 	 */
946a9934668SKenneth D. Merry 	if (auio.uio_rw == UIO_READ) {
947cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
9485541f25eSPawel Jakub Dawidek 		error = VOP_READ(vp, &auio, IO_DIRECT, sc->cred);
94922db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
9508f8def9eSPoul-Henning Kamp 	} else {
9515541f25eSPawel Jakub Dawidek 		(void) vn_start_write(vp, &mp, V_WAIT);
952cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
9535541f25eSPawel Jakub Dawidek 		error = VOP_WRITE(vp, &auio, sc->flags & MD_ASYNC ? 0 : IO_SYNC,
9545541f25eSPawel Jakub Dawidek 		    sc->cred);
95522db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
9568f8def9eSPoul-Henning Kamp 		vn_finished_write(mp);
9578f8def9eSPoul-Henning Kamp 	}
958a9934668SKenneth D. Merry 
959d5f998baSKonstantin Belousov 	if (pb != NULL) {
960d5f998baSKonstantin Belousov 		pmap_qremove((vm_offset_t)pb->b_data, npages);
961d5f998baSKonstantin Belousov 		if (error == 0) {
962d5f998baSKonstantin Belousov 			len -= iolen;
963d5f998baSKonstantin Belousov 			bp->bio_resid -= iolen;
964d5f998baSKonstantin Belousov 			ma_offs += iolen;
965d5f998baSKonstantin Belousov 			if (len > 0)
966d5f998baSKonstantin Belousov 				goto unmapped_step;
967d5f998baSKonstantin Belousov 		}
96859ec9023SKonstantin Belousov 		relpbuf(pb, &md_vnode_pbuf_freecnt);
96959ec9023SKonstantin Belousov 	}
970a9934668SKenneth D. Merry 
971a9934668SKenneth D. Merry 	free(piov, M_MD);
972d5f998baSKonstantin Belousov 	if (pb == NULL)
9738f8def9eSPoul-Henning Kamp 		bp->bio_resid = auio.uio_resid;
974b4a4f93cSPoul-Henning Kamp 	return (error);
9758f8def9eSPoul-Henning Kamp }
9768f8def9eSPoul-Henning Kamp 
977b4a4f93cSPoul-Henning Kamp static int
978b4a4f93cSPoul-Henning Kamp mdstart_swap(struct md_s *sc, struct bio *bp)
9798f8def9eSPoul-Henning Kamp {
9808e28326aSPoul-Henning Kamp 	vm_page_t m;
9818e28326aSPoul-Henning Kamp 	u_char *p;
98259ec9023SKonstantin Belousov 	vm_pindex_t i, lastp;
983a9934668SKenneth D. Merry 	bus_dma_segment_t *vlist;
98459ec9023SKonstantin Belousov 	int rv, ma_offs, offs, len, lastend;
9858f8def9eSPoul-Henning Kamp 
9865541f25eSPawel Jakub Dawidek 	switch (bp->bio_cmd) {
9875541f25eSPawel Jakub Dawidek 	case BIO_READ:
9885541f25eSPawel Jakub Dawidek 	case BIO_WRITE:
9895541f25eSPawel Jakub Dawidek 	case BIO_DELETE:
9905541f25eSPawel Jakub Dawidek 		break;
9915541f25eSPawel Jakub Dawidek 	default:
9925541f25eSPawel Jakub Dawidek 		return (EOPNOTSUPP);
9935541f25eSPawel Jakub Dawidek 	}
9945541f25eSPawel Jakub Dawidek 
9958e28326aSPoul-Henning Kamp 	p = bp->bio_data;
996a9934668SKenneth D. Merry 	ma_offs = (bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0 ?
997a9934668SKenneth D. Merry 	    bp->bio_ma_offset : 0;
998a9934668SKenneth D. Merry 	vlist = (bp->bio_flags & BIO_VLIST) != 0 ?
999a9934668SKenneth D. Merry 	    (bus_dma_segment_t *)bp->bio_data : NULL;
1000e07113d6SColin Percival 
1001e07113d6SColin Percival 	/*
10026c3cd0e2SMaxim Konovalov 	 * offs is the offset at which to start operating on the
1003e07113d6SColin Percival 	 * next (ie, first) page.  lastp is the last page on
1004e07113d6SColin Percival 	 * which we're going to operate.  lastend is the ending
1005e07113d6SColin Percival 	 * position within that last page (ie, PAGE_SIZE if
1006e07113d6SColin Percival 	 * we're operating on complete aligned pages).
1007e07113d6SColin Percival 	 */
1008e07113d6SColin Percival 	offs = bp->bio_offset % PAGE_SIZE;
1009e07113d6SColin Percival 	lastp = (bp->bio_offset + bp->bio_length - 1) / PAGE_SIZE;
1010e07113d6SColin Percival 	lastend = (bp->bio_offset + bp->bio_length - 1) % PAGE_SIZE + 1;
1011e07113d6SColin Percival 
1012812851b6SBrian Feldman 	rv = VM_PAGER_OK;
101389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(sc->object);
10148e28326aSPoul-Henning Kamp 	vm_object_pip_add(sc->object, 1);
1015e07113d6SColin Percival 	for (i = bp->bio_offset / PAGE_SIZE; i <= lastp; i++) {
1016e07113d6SColin Percival 		len = ((i == lastp) ? lastend : PAGE_SIZE) - offs;
10171a42d14aSKonstantin Belousov 		m = vm_page_grab(sc->object, i, VM_ALLOC_SYSTEM);
10188e28326aSPoul-Henning Kamp 		if (bp->bio_cmd == BIO_READ) {
1019537cc627SKonstantin Belousov 			if (m->valid == VM_PAGE_BITS_ALL)
1020537cc627SKonstantin Belousov 				rv = VM_PAGER_OK;
1021537cc627SKonstantin Belousov 			else
1022*b0cd2017SGleb Smirnoff 				rv = vm_pager_get_pages(sc->object, &m, 1,
1023*b0cd2017SGleb Smirnoff 				    NULL, NULL);
1024812851b6SBrian Feldman 			if (rv == VM_PAGER_ERROR) {
1025c7aebda8SAttilio Rao 				vm_page_xunbusy(m);
1026812851b6SBrian Feldman 				break;
1027e9f581baSKonstantin Belousov 			} else if (rv == VM_PAGER_FAIL) {
1028e9f581baSKonstantin Belousov 				/*
1029e9f581baSKonstantin Belousov 				 * Pager does not have the page.  Zero
1030e9f581baSKonstantin Belousov 				 * the allocated page, and mark it as
1031e9f581baSKonstantin Belousov 				 * valid. Do not set dirty, the page
1032e9f581baSKonstantin Belousov 				 * can be recreated if thrown out.
1033e9f581baSKonstantin Belousov 				 */
103459ec9023SKonstantin Belousov 				pmap_zero_page(m);
1035e9f581baSKonstantin Belousov 				m->valid = VM_PAGE_BITS_ALL;
1036812851b6SBrian Feldman 			}
103759ec9023SKonstantin Belousov 			if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
103859ec9023SKonstantin Belousov 				pmap_copy_pages(&m, offs, bp->bio_ma,
103959ec9023SKonstantin Belousov 				    ma_offs, len);
1040a9934668SKenneth D. Merry 			} else if ((bp->bio_flags & BIO_VLIST) != 0) {
1041a9934668SKenneth D. Merry 				physcopyout_vlist(VM_PAGE_TO_PHYS(m) + offs,
1042a9934668SKenneth D. Merry 				    vlist, ma_offs, len);
1043a9934668SKenneth D. Merry 				cpu_flush_dcache(p, len);
104459ec9023SKonstantin Belousov 			} else {
104559ec9023SKonstantin Belousov 				physcopyout(VM_PAGE_TO_PHYS(m) + offs, p, len);
1046dbb95048SMarcel Moolenaar 				cpu_flush_dcache(p, len);
104759ec9023SKonstantin Belousov 			}
10488e28326aSPoul-Henning Kamp 		} else if (bp->bio_cmd == BIO_WRITE) {
104907be617fSAlan Cox 			if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL)
1050*b0cd2017SGleb Smirnoff 				rv = vm_pager_get_pages(sc->object, &m, 1,
1051*b0cd2017SGleb Smirnoff 				    NULL, NULL);
1052537cc627SKonstantin Belousov 			else
1053537cc627SKonstantin Belousov 				rv = VM_PAGER_OK;
1054812851b6SBrian Feldman 			if (rv == VM_PAGER_ERROR) {
1055c7aebda8SAttilio Rao 				vm_page_xunbusy(m);
1056812851b6SBrian Feldman 				break;
1057812851b6SBrian Feldman 			}
105859ec9023SKonstantin Belousov 			if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
105959ec9023SKonstantin Belousov 				pmap_copy_pages(bp->bio_ma, ma_offs, &m,
106059ec9023SKonstantin Belousov 				    offs, len);
1061a9934668SKenneth D. Merry 			} else if ((bp->bio_flags & BIO_VLIST) != 0) {
1062a9934668SKenneth D. Merry 				physcopyin_vlist(vlist, ma_offs,
1063a9934668SKenneth D. Merry 				    VM_PAGE_TO_PHYS(m) + offs, len);
106459ec9023SKonstantin Belousov 			} else {
106559ec9023SKonstantin Belousov 				physcopyin(p, VM_PAGE_TO_PHYS(m) + offs, len);
106659ec9023SKonstantin Belousov 			}
10678e28326aSPoul-Henning Kamp 			m->valid = VM_PAGE_BITS_ALL;
10688e28326aSPoul-Henning Kamp 		} else if (bp->bio_cmd == BIO_DELETE) {
106907be617fSAlan Cox 			if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL)
1070*b0cd2017SGleb Smirnoff 				rv = vm_pager_get_pages(sc->object, &m, 1,
1071*b0cd2017SGleb Smirnoff 				    NULL, NULL);
1072537cc627SKonstantin Belousov 			else
1073537cc627SKonstantin Belousov 				rv = VM_PAGER_OK;
1074812851b6SBrian Feldman 			if (rv == VM_PAGER_ERROR) {
1075c7aebda8SAttilio Rao 				vm_page_xunbusy(m);
1076812851b6SBrian Feldman 				break;
1077812851b6SBrian Feldman 			}
10784a13a769SKonstantin Belousov 			if (len != PAGE_SIZE) {
107959ec9023SKonstantin Belousov 				pmap_zero_page_area(m, offs, len);
10804a13a769SKonstantin Belousov 				vm_page_clear_dirty(m, offs, len);
10818e28326aSPoul-Henning Kamp 				m->valid = VM_PAGE_BITS_ALL;
10824a13a769SKonstantin Belousov 			} else
10834a13a769SKonstantin Belousov 				vm_pager_page_unswapped(m);
10848e28326aSPoul-Henning Kamp 		}
1085c7aebda8SAttilio Rao 		vm_page_xunbusy(m);
1086fc0c3802SKonstantin Belousov 		vm_page_lock(m);
10874a13a769SKonstantin Belousov 		if (bp->bio_cmd == BIO_DELETE && len == PAGE_SIZE)
10884a13a769SKonstantin Belousov 			vm_page_free(m);
10894a13a769SKonstantin Belousov 		else
10908e28326aSPoul-Henning Kamp 			vm_page_activate(m);
1091ecd5dd95SAlan Cox 		vm_page_unlock(m);
10925d9b4508SKonstantin Belousov 		if (bp->bio_cmd == BIO_WRITE) {
10938e28326aSPoul-Henning Kamp 			vm_page_dirty(m);
10945d9b4508SKonstantin Belousov 			vm_pager_page_unswapped(m);
10955d9b4508SKonstantin Belousov 		}
1096e07113d6SColin Percival 
1097e07113d6SColin Percival 		/* Actions on further pages start at offset 0 */
1098e07113d6SColin Percival 		p += PAGE_SIZE - offs;
1099e07113d6SColin Percival 		offs = 0;
110059ec9023SKonstantin Belousov 		ma_offs += len;
11018e28326aSPoul-Henning Kamp 	}
11020d8243ccSAttilio Rao 	vm_object_pip_wakeup(sc->object);
110389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(sc->object);
1104812851b6SBrian Feldman 	return (rv != VM_PAGER_ERROR ? 0 : ENOSPC);
11058e28326aSPoul-Henning Kamp }
11068f8def9eSPoul-Henning Kamp 
11070efd9bfdSEdward Tomasz Napierala static int
11080efd9bfdSEdward Tomasz Napierala mdstart_null(struct md_s *sc, struct bio *bp)
11090efd9bfdSEdward Tomasz Napierala {
11100efd9bfdSEdward Tomasz Napierala 
11110efd9bfdSEdward Tomasz Napierala 	switch (bp->bio_cmd) {
11120efd9bfdSEdward Tomasz Napierala 	case BIO_READ:
11130efd9bfdSEdward Tomasz Napierala 		bzero(bp->bio_data, bp->bio_length);
11140efd9bfdSEdward Tomasz Napierala 		cpu_flush_dcache(bp->bio_data, bp->bio_length);
11150efd9bfdSEdward Tomasz Napierala 		break;
11160efd9bfdSEdward Tomasz Napierala 	case BIO_WRITE:
11170efd9bfdSEdward Tomasz Napierala 		break;
11180efd9bfdSEdward Tomasz Napierala 	}
11190efd9bfdSEdward Tomasz Napierala 	bp->bio_resid = 0;
11200efd9bfdSEdward Tomasz Napierala 	return (0);
11210efd9bfdSEdward Tomasz Napierala }
11220efd9bfdSEdward Tomasz Napierala 
11238f8def9eSPoul-Henning Kamp static void
11245c97ca54SIan Dowse md_kthread(void *arg)
11255c97ca54SIan Dowse {
11265c97ca54SIan Dowse 	struct md_s *sc;
11275c97ca54SIan Dowse 	struct bio *bp;
1128a08d2e7fSJohn Baldwin 	int error;
11295c97ca54SIan Dowse 
11305c97ca54SIan Dowse 	sc = arg;
1131982d11f8SJeff Roberson 	thread_lock(curthread);
113263710c4dSJohn Baldwin 	sched_prio(curthread, PRIBIO);
1133982d11f8SJeff Roberson 	thread_unlock(curthread);
11343b7b5496SKonstantin Belousov 	if (sc->type == MD_VNODE)
11353b7b5496SKonstantin Belousov 		curthread->td_pflags |= TDP_NORUNNINGBUF;
11365c97ca54SIan Dowse 
1137b4a4f93cSPoul-Henning Kamp 	for (;;) {
1138a08d2e7fSJohn Baldwin 		mtx_lock(&sc->queue_mtx);
11395c97ca54SIan Dowse 		if (sc->flags & MD_SHUTDOWN) {
1140a08d2e7fSJohn Baldwin 			sc->flags |= MD_EXITING;
1141a08d2e7fSJohn Baldwin 			mtx_unlock(&sc->queue_mtx);
11423745c395SJulian Elischer 			kproc_exit(0);
11435c97ca54SIan Dowse 		}
11449b00ca19SPoul-Henning Kamp 		bp = bioq_takefirst(&sc->bio_queue);
11459b00ca19SPoul-Henning Kamp 		if (!bp) {
11460f8500a5SPoul-Henning Kamp 			msleep(sc, &sc->queue_mtx, PRIBIO | PDROP, "mdwait", 0);
11475c97ca54SIan Dowse 			continue;
11485c97ca54SIan Dowse 		}
11490f8500a5SPoul-Henning Kamp 		mtx_unlock(&sc->queue_mtx);
11504e8bfe14SPoul-Henning Kamp 		if (bp->bio_cmd == BIO_GETATTR) {
1151d91e813cSKonstantin Belousov 			if ((sc->fwsectors && sc->fwheads &&
11524e8bfe14SPoul-Henning Kamp 			    (g_handleattr_int(bp, "GEOM::fwsectors",
11534e8bfe14SPoul-Henning Kamp 			    sc->fwsectors) ||
11544e8bfe14SPoul-Henning Kamp 			    g_handleattr_int(bp, "GEOM::fwheads",
1155d91e813cSKonstantin Belousov 			    sc->fwheads))) ||
1156d91e813cSKonstantin Belousov 			    g_handleattr_int(bp, "GEOM::candelete", 1))
11574e8bfe14SPoul-Henning Kamp 				error = -1;
11584e8bfe14SPoul-Henning Kamp 			else
11594e8bfe14SPoul-Henning Kamp 				error = EOPNOTSUPP;
11604e8bfe14SPoul-Henning Kamp 		} else {
11619b00ca19SPoul-Henning Kamp 			error = sc->start(sc, bp);
11624e8bfe14SPoul-Henning Kamp 		}
1163b4a4f93cSPoul-Henning Kamp 
11646f4f00f1SPoul-Henning Kamp 		if (error != -1) {
11656f4f00f1SPoul-Henning Kamp 			bp->bio_completed = bp->bio_length;
1166a03be42dSMaxim Sobolev 			if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE))
1167a03be42dSMaxim Sobolev 				devstat_end_transaction_bio(sc->devstat, bp);
116896410b95SKonstantin Belousov 			g_io_deliver(bp, error);
1169b4a4f93cSPoul-Henning Kamp 		}
11708f8def9eSPoul-Henning Kamp 	}
117126d48b40SPoul-Henning Kamp }
11728f8def9eSPoul-Henning Kamp 
11738f8def9eSPoul-Henning Kamp static struct md_s *
11748f8def9eSPoul-Henning Kamp mdfind(int unit)
11758f8def9eSPoul-Henning Kamp {
11768f8def9eSPoul-Henning Kamp 	struct md_s *sc;
11778f8def9eSPoul-Henning Kamp 
11783f54a085SPoul-Henning Kamp 	LIST_FOREACH(sc, &md_softc_list, list) {
11793f54a085SPoul-Henning Kamp 		if (sc->unit == unit)
11808f8def9eSPoul-Henning Kamp 			break;
11818f8def9eSPoul-Henning Kamp 	}
11828f8def9eSPoul-Henning Kamp 	return (sc);
11838f8def9eSPoul-Henning Kamp }
11848f8def9eSPoul-Henning Kamp 
11858f8def9eSPoul-Henning Kamp static struct md_s *
1186947fc8deSPoul-Henning Kamp mdnew(int unit, int *errp, enum md_types type)
11878f8def9eSPoul-Henning Kamp {
1188f4e7c5a8SJaakko Heinonen 	struct md_s *sc;
1189f4e7c5a8SJaakko Heinonen 	int error;
11908f8def9eSPoul-Henning Kamp 
11919b00ca19SPoul-Henning Kamp 	*errp = 0;
1192f4e7c5a8SJaakko Heinonen 	if (unit == -1)
1193f4e7c5a8SJaakko Heinonen 		unit = alloc_unr(md_uh);
1194f4e7c5a8SJaakko Heinonen 	else
1195f4e7c5a8SJaakko Heinonen 		unit = alloc_unr_specific(md_uh, unit);
1196f4e7c5a8SJaakko Heinonen 
1197f4e7c5a8SJaakko Heinonen 	if (unit == -1) {
11987ee3c044SPawel Jakub Dawidek 		*errp = EBUSY;
11993f54a085SPoul-Henning Kamp 		return (NULL);
12003f54a085SPoul-Henning Kamp 	}
1201f4e7c5a8SJaakko Heinonen 
12029b00ca19SPoul-Henning Kamp 	sc = (struct md_s *)malloc(sizeof *sc, M_MD, M_WAITOK | M_ZERO);
1203947fc8deSPoul-Henning Kamp 	sc->type = type;
12049b00ca19SPoul-Henning Kamp 	bioq_init(&sc->bio_queue);
12059b00ca19SPoul-Henning Kamp 	mtx_init(&sc->queue_mtx, "md bio queue", NULL, MTX_DEF);
120640ea77a0SAlexander Motin 	mtx_init(&sc->stat_mtx, "md stat", NULL, MTX_DEF);
12073f54a085SPoul-Henning Kamp 	sc->unit = unit;
1208f43b2bacSPoul-Henning Kamp 	sprintf(sc->name, "md%d", unit);
12097ee3c044SPawel Jakub Dawidek 	LIST_INSERT_HEAD(&md_softc_list, sc, list);
12103745c395SJulian Elischer 	error = kproc_create(md_kthread, sc, &sc->procp, 0, 0,"%s", sc->name);
12119b00ca19SPoul-Henning Kamp 	if (error == 0)
12129b00ca19SPoul-Henning Kamp 		return (sc);
12137ee3c044SPawel Jakub Dawidek 	LIST_REMOVE(sc, list);
121440ea77a0SAlexander Motin 	mtx_destroy(&sc->stat_mtx);
12157ee3c044SPawel Jakub Dawidek 	mtx_destroy(&sc->queue_mtx);
1216f4e7c5a8SJaakko Heinonen 	free_unr(md_uh, sc->unit);
12175c97ca54SIan Dowse 	free(sc, M_MD);
12187ee3c044SPawel Jakub Dawidek 	*errp = error;
12195c97ca54SIan Dowse 	return (NULL);
12205c97ca54SIan Dowse }
12218f8def9eSPoul-Henning Kamp 
12228f8def9eSPoul-Henning Kamp static void
12238f8def9eSPoul-Henning Kamp mdinit(struct md_s *sc)
12248f8def9eSPoul-Henning Kamp {
12256f4f00f1SPoul-Henning Kamp 	struct g_geom *gp;
12266f4f00f1SPoul-Henning Kamp 	struct g_provider *pp;
12276f4f00f1SPoul-Henning Kamp 
12286f4f00f1SPoul-Henning Kamp 	g_topology_lock();
12296f4f00f1SPoul-Henning Kamp 	gp = g_new_geomf(&g_md_class, "md%d", sc->unit);
12306f4f00f1SPoul-Henning Kamp 	gp->softc = sc;
12316f4f00f1SPoul-Henning Kamp 	pp = g_new_providerf(gp, "md%d", sc->unit);
123240ea77a0SAlexander Motin 	pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
1233b830359bSPawel Jakub Dawidek 	pp->mediasize = sc->mediasize;
1234b830359bSPawel Jakub Dawidek 	pp->sectorsize = sc->sectorsize;
12351ef76554SKonstantin Belousov 	switch (sc->type) {
12361ef76554SKonstantin Belousov 	case MD_MALLOC:
12371ef76554SKonstantin Belousov 	case MD_VNODE:
12381ef76554SKonstantin Belousov 	case MD_SWAP:
123959ec9023SKonstantin Belousov 		pp->flags |= G_PF_ACCEPT_UNMAPPED;
12401ef76554SKonstantin Belousov 		break;
12411ef76554SKonstantin Belousov 	case MD_PRELOAD:
12420efd9bfdSEdward Tomasz Napierala 	case MD_NULL:
12431ef76554SKonstantin Belousov 		break;
12441ef76554SKonstantin Belousov 	}
12456f4f00f1SPoul-Henning Kamp 	sc->gp = gp;
12466f4f00f1SPoul-Henning Kamp 	sc->pp = pp;
12476f4f00f1SPoul-Henning Kamp 	g_error_provider(pp, 0);
12486f4f00f1SPoul-Henning Kamp 	g_topology_unlock();
1249a03be42dSMaxim Sobolev 	sc->devstat = devstat_new_entry("md", sc->unit, sc->sectorsize,
1250a03be42dSMaxim Sobolev 	    DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
12516f4f00f1SPoul-Henning Kamp }
125271e4fff8SPoul-Henning Kamp 
12538f8def9eSPoul-Henning Kamp static int
1254b830359bSPawel Jakub Dawidek mdcreate_malloc(struct md_s *sc, struct md_ioctl *mdio)
125595f1a897SPoul-Henning Kamp {
1256c6517568SPoul-Henning Kamp 	uintptr_t sp;
1257c6517568SPoul-Henning Kamp 	int error;
1258b830359bSPawel Jakub Dawidek 	off_t u;
125995f1a897SPoul-Henning Kamp 
1260c6517568SPoul-Henning Kamp 	error = 0;
12618f8def9eSPoul-Henning Kamp 	if (mdio->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE))
12628f8def9eSPoul-Henning Kamp 		return (EINVAL);
1263b830359bSPawel Jakub Dawidek 	if (mdio->md_sectorsize != 0 && !powerof2(mdio->md_sectorsize))
1264ebe789d6SPoul-Henning Kamp 		return (EINVAL);
12658f8def9eSPoul-Henning Kamp 	/* Compression doesn't make sense if we have reserved space */
12668f8def9eSPoul-Henning Kamp 	if (mdio->md_options & MD_RESERVE)
12678f8def9eSPoul-Henning Kamp 		mdio->md_options &= ~MD_COMPRESS;
12684e8bfe14SPoul-Henning Kamp 	if (mdio->md_fwsectors != 0)
12694e8bfe14SPoul-Henning Kamp 		sc->fwsectors = mdio->md_fwsectors;
12704e8bfe14SPoul-Henning Kamp 	if (mdio->md_fwheads != 0)
12714e8bfe14SPoul-Henning Kamp 		sc->fwheads = mdio->md_fwheads;
127226a0ee75SDima Dorfman 	sc->flags = mdio->md_options & (MD_COMPRESS | MD_FORCE);
1273b830359bSPawel Jakub Dawidek 	sc->indir = dimension(sc->mediasize / sc->sectorsize);
1274b830359bSPawel Jakub Dawidek 	sc->uma = uma_zcreate(sc->name, sc->sectorsize, NULL, NULL, NULL, NULL,
1275b830359bSPawel Jakub Dawidek 	    0x1ff, 0);
127696b6a55fSPoul-Henning Kamp 	if (mdio->md_options & MD_RESERVE) {
1277b830359bSPawel Jakub Dawidek 		off_t nsectors;
1278b830359bSPawel Jakub Dawidek 
1279b830359bSPawel Jakub Dawidek 		nsectors = sc->mediasize / sc->sectorsize;
1280b830359bSPawel Jakub Dawidek 		for (u = 0; u < nsectors; u++) {
1281007777f1SKonstantin Belousov 			sp = (uintptr_t)uma_zalloc(sc->uma, (md_malloc_wait ?
1282007777f1SKonstantin Belousov 			    M_WAITOK : M_NOWAIT) | M_ZERO);
1283c6517568SPoul-Henning Kamp 			if (sp != 0)
1284fde2a2e4SPoul-Henning Kamp 				error = s_write(sc->indir, u, sp);
1285c6517568SPoul-Henning Kamp 			else
1286c6517568SPoul-Henning Kamp 				error = ENOMEM;
1287b830359bSPawel Jakub Dawidek 			if (error != 0)
1288c6517568SPoul-Henning Kamp 				break;
12898f8def9eSPoul-Henning Kamp 		}
1290c6517568SPoul-Henning Kamp 	}
1291c6517568SPoul-Henning Kamp 	return (error);
129200a6a3c6SPoul-Henning Kamp }
129300a6a3c6SPoul-Henning Kamp 
12943f54a085SPoul-Henning Kamp 
12958f8def9eSPoul-Henning Kamp static int
12968f8def9eSPoul-Henning Kamp mdsetcred(struct md_s *sc, struct ucred *cred)
12978f8def9eSPoul-Henning Kamp {
12988f8def9eSPoul-Henning Kamp 	char *tmpbuf;
12998f8def9eSPoul-Henning Kamp 	int error = 0;
13008f8def9eSPoul-Henning Kamp 
13013f54a085SPoul-Henning Kamp 	/*
13028f8def9eSPoul-Henning Kamp 	 * Set credits in our softc
13033f54a085SPoul-Henning Kamp 	 */
13048f8def9eSPoul-Henning Kamp 
13058f8def9eSPoul-Henning Kamp 	if (sc->cred)
13068f8def9eSPoul-Henning Kamp 		crfree(sc->cred);
1307bd78ceceSJohn Baldwin 	sc->cred = crhold(cred);
13088f8def9eSPoul-Henning Kamp 
13098f8def9eSPoul-Henning Kamp 	/*
13108f8def9eSPoul-Henning Kamp 	 * Horrible kludge to establish credentials for NFS  XXX.
13118f8def9eSPoul-Henning Kamp 	 */
13128f8def9eSPoul-Henning Kamp 
13138f8def9eSPoul-Henning Kamp 	if (sc->vnode) {
13148f8def9eSPoul-Henning Kamp 		struct uio auio;
13158f8def9eSPoul-Henning Kamp 		struct iovec aiov;
13168f8def9eSPoul-Henning Kamp 
1317b830359bSPawel Jakub Dawidek 		tmpbuf = malloc(sc->sectorsize, M_TEMP, M_WAITOK);
13188f8def9eSPoul-Henning Kamp 		bzero(&auio, sizeof(auio));
13198f8def9eSPoul-Henning Kamp 
13208f8def9eSPoul-Henning Kamp 		aiov.iov_base = tmpbuf;
1321b830359bSPawel Jakub Dawidek 		aiov.iov_len = sc->sectorsize;
13228f8def9eSPoul-Henning Kamp 		auio.uio_iov = &aiov;
13238f8def9eSPoul-Henning Kamp 		auio.uio_iovcnt = 1;
13248f8def9eSPoul-Henning Kamp 		auio.uio_offset = 0;
13258f8def9eSPoul-Henning Kamp 		auio.uio_rw = UIO_READ;
13268f8def9eSPoul-Henning Kamp 		auio.uio_segflg = UIO_SYSSPACE;
13278f8def9eSPoul-Henning Kamp 		auio.uio_resid = aiov.iov_len;
1328cb05b60aSAttilio Rao 		vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY);
13298f8def9eSPoul-Henning Kamp 		error = VOP_READ(sc->vnode, &auio, 0, sc->cred);
133022db15c0SAttilio Rao 		VOP_UNLOCK(sc->vnode, 0);
13318f8def9eSPoul-Henning Kamp 		free(tmpbuf, M_TEMP);
13328f8def9eSPoul-Henning Kamp 	}
13338f8def9eSPoul-Henning Kamp 	return (error);
13348f8def9eSPoul-Henning Kamp }
13358f8def9eSPoul-Henning Kamp 
13368f8def9eSPoul-Henning Kamp static int
1337b830359bSPawel Jakub Dawidek mdcreate_vnode(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
13388f8def9eSPoul-Henning Kamp {
13398f8def9eSPoul-Henning Kamp 	struct vattr vattr;
13408f8def9eSPoul-Henning Kamp 	struct nameidata nd;
13413d5c947dSMarcel Moolenaar 	char *fname;
13425050aa86SKonstantin Belousov 	int error, flags;
13438f8def9eSPoul-Henning Kamp 
13443d5c947dSMarcel Moolenaar 	/*
13453d5c947dSMarcel Moolenaar 	 * Kernel-originated requests must have the filename appended
13463d5c947dSMarcel Moolenaar 	 * to the mdio structure to protect against malicious software.
13473d5c947dSMarcel Moolenaar 	 */
13483d5c947dSMarcel Moolenaar 	fname = mdio->md_file;
13493d5c947dSMarcel Moolenaar 	if ((void *)fname != (void *)(mdio + 1)) {
13503d5c947dSMarcel Moolenaar 		error = copyinstr(fname, sc->file, sizeof(sc->file), NULL);
135188b5b78dSPawel Jakub Dawidek 		if (error != 0)
135288b5b78dSPawel Jakub Dawidek 			return (error);
13533d5c947dSMarcel Moolenaar 	} else
13543d5c947dSMarcel Moolenaar 		strlcpy(sc->file, fname, sizeof(sc->file));
13553d5c947dSMarcel Moolenaar 
135686776891SChristian S.J. Peron 	/*
13573d5c947dSMarcel Moolenaar 	 * If the user specified that this is a read only device, don't
13583d5c947dSMarcel Moolenaar 	 * set the FWRITE mask before trying to open the backing store.
135986776891SChristian S.J. Peron 	 */
13603d5c947dSMarcel Moolenaar 	flags = FREAD | ((mdio->md_options & MD_READONLY) ? 0 : FWRITE);
13615050aa86SKonstantin Belousov 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, sc->file, td);
13629e223287SKonstantin Belousov 	error = vn_open(&nd, &flags, 0, NULL);
1363e3ed29a7SPawel Jakub Dawidek 	if (error != 0)
136452c6716fSPawel Jakub Dawidek 		return (error);
1365b322d85dSPawel Jakub Dawidek 	NDFREE(&nd, NDF_ONLY_PNBUF);
136633fc3625SJohn Baldwin 	if (nd.ni_vp->v_type != VREG) {
136733fc3625SJohn Baldwin 		error = EINVAL;
136833fc3625SJohn Baldwin 		goto bad;
136933fc3625SJohn Baldwin 	}
137033fc3625SJohn Baldwin 	error = VOP_GETATTR(nd.ni_vp, &vattr, td->td_ucred);
137133fc3625SJohn Baldwin 	if (error != 0)
137233fc3625SJohn Baldwin 		goto bad;
137333fc3625SJohn Baldwin 	if (VOP_ISLOCKED(nd.ni_vp) != LK_EXCLUSIVE) {
137433fc3625SJohn Baldwin 		vn_lock(nd.ni_vp, LK_UPGRADE | LK_RETRY);
137533fc3625SJohn Baldwin 		if (nd.ni_vp->v_iflag & VI_DOOMED) {
137633fc3625SJohn Baldwin 			/* Forced unmount. */
137733fc3625SJohn Baldwin 			error = EBADF;
137833fc3625SJohn Baldwin 			goto bad;
137933fc3625SJohn Baldwin 		}
13808f8def9eSPoul-Henning Kamp 	}
13813b7b5496SKonstantin Belousov 	nd.ni_vp->v_vflag |= VV_MD;
138222db15c0SAttilio Rao 	VOP_UNLOCK(nd.ni_vp, 0);
13839589c256SPoul-Henning Kamp 
1384d5a929dcSPoul-Henning Kamp 	if (mdio->md_fwsectors != 0)
1385d5a929dcSPoul-Henning Kamp 		sc->fwsectors = mdio->md_fwsectors;
1386d5a929dcSPoul-Henning Kamp 	if (mdio->md_fwheads != 0)
1387d5a929dcSPoul-Henning Kamp 		sc->fwheads = mdio->md_fwheads;
13887a6b2b64SPoul-Henning Kamp 	sc->flags = mdio->md_options & (MD_FORCE | MD_ASYNC);
13899589c256SPoul-Henning Kamp 	if (!(flags & FWRITE))
13909589c256SPoul-Henning Kamp 		sc->flags |= MD_READONLY;
13918f8def9eSPoul-Henning Kamp 	sc->vnode = nd.ni_vp;
13928f8def9eSPoul-Henning Kamp 
1393a854ed98SJohn Baldwin 	error = mdsetcred(sc, td->td_ucred);
1394b830359bSPawel Jakub Dawidek 	if (error != 0) {
13953cf74e53SPhilip Paeps 		sc->vnode = NULL;
1396cb05b60aSAttilio Rao 		vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY);
13973b7b5496SKonstantin Belousov 		nd.ni_vp->v_vflag &= ~VV_MD;
139833fc3625SJohn Baldwin 		goto bad;
139933fc3625SJohn Baldwin 	}
140033fc3625SJohn Baldwin 	return (0);
140133fc3625SJohn Baldwin bad:
140222db15c0SAttilio Rao 	VOP_UNLOCK(nd.ni_vp, 0);
140352c6716fSPawel Jakub Dawidek 	(void)vn_close(nd.ni_vp, flags, td->td_ucred, td);
14048f8def9eSPoul-Henning Kamp 	return (error);
14058f8def9eSPoul-Henning Kamp }
14068f8def9eSPoul-Henning Kamp 
14078f8def9eSPoul-Henning Kamp static int
1408b40ce416SJulian Elischer mddestroy(struct md_s *sc, struct thread *td)
14098f8def9eSPoul-Henning Kamp {
14100cddd8f0SMatthew Dillon 
14116f4f00f1SPoul-Henning Kamp 	if (sc->gp) {
14126f4f00f1SPoul-Henning Kamp 		sc->gp->softc = NULL;
14139b00ca19SPoul-Henning Kamp 		g_topology_lock();
14149b00ca19SPoul-Henning Kamp 		g_wither_geom(sc->gp, ENXIO);
14159b00ca19SPoul-Henning Kamp 		g_topology_unlock();
14166b60a2cdSPoul-Henning Kamp 		sc->gp = NULL;
14176b60a2cdSPoul-Henning Kamp 		sc->pp = NULL;
14181f4ee1aaSPoul-Henning Kamp 	}
1419a03be42dSMaxim Sobolev 	if (sc->devstat) {
1420a03be42dSMaxim Sobolev 		devstat_remove_entry(sc->devstat);
1421a03be42dSMaxim Sobolev 		sc->devstat = NULL;
1422a03be42dSMaxim Sobolev 	}
1423a08d2e7fSJohn Baldwin 	mtx_lock(&sc->queue_mtx);
14245c97ca54SIan Dowse 	sc->flags |= MD_SHUTDOWN;
14255c97ca54SIan Dowse 	wakeup(sc);
1426a08d2e7fSJohn Baldwin 	while (!(sc->flags & MD_EXITING))
1427a08d2e7fSJohn Baldwin 		msleep(sc->procp, &sc->queue_mtx, PRIBIO, "mddestroy", hz / 10);
1428a08d2e7fSJohn Baldwin 	mtx_unlock(&sc->queue_mtx);
142940ea77a0SAlexander Motin 	mtx_destroy(&sc->stat_mtx);
14309fbea3e3SPoul-Henning Kamp 	mtx_destroy(&sc->queue_mtx);
14319b00ca19SPoul-Henning Kamp 	if (sc->vnode != NULL) {
1432cb05b60aSAttilio Rao 		vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY);
14333b7b5496SKonstantin Belousov 		sc->vnode->v_vflag &= ~VV_MD;
143422db15c0SAttilio Rao 		VOP_UNLOCK(sc->vnode, 0);
14359d4b5945SMaxim Sobolev 		(void)vn_close(sc->vnode, sc->flags & MD_READONLY ?
1436b40ce416SJulian Elischer 		    FREAD : (FREAD|FWRITE), sc->cred, td);
14379b00ca19SPoul-Henning Kamp 	}
14388f8def9eSPoul-Henning Kamp 	if (sc->cred != NULL)
14398f8def9eSPoul-Henning Kamp 		crfree(sc->cred);
14401db17c6dSPawel Jakub Dawidek 	if (sc->object != NULL)
1441f820bc50SAlan Cox 		vm_object_deallocate(sc->object);
1442f43b2bacSPoul-Henning Kamp 	if (sc->indir)
1443f43b2bacSPoul-Henning Kamp 		destroy_indir(sc, sc->indir);
1444f43b2bacSPoul-Henning Kamp 	if (sc->uma)
1445f43b2bacSPoul-Henning Kamp 		uma_zdestroy(sc->uma);
14461f4ee1aaSPoul-Henning Kamp 
14471f4ee1aaSPoul-Henning Kamp 	LIST_REMOVE(sc, list);
1448f4e7c5a8SJaakko Heinonen 	free_unr(md_uh, sc->unit);
1449c6517568SPoul-Henning Kamp 	free(sc, M_MD);
14508f8def9eSPoul-Henning Kamp 	return (0);
14518f8def9eSPoul-Henning Kamp }
14528f8def9eSPoul-Henning Kamp 
14538f8def9eSPoul-Henning Kamp static int
1454dc604f0cSEdward Tomasz Napierala mdresize(struct md_s *sc, struct md_ioctl *mdio)
1455dc604f0cSEdward Tomasz Napierala {
1456dc604f0cSEdward Tomasz Napierala 	int error, res;
1457dc604f0cSEdward Tomasz Napierala 	vm_pindex_t oldpages, newpages;
1458dc604f0cSEdward Tomasz Napierala 
1459dc604f0cSEdward Tomasz Napierala 	switch (sc->type) {
1460dc604f0cSEdward Tomasz Napierala 	case MD_VNODE:
14610efd9bfdSEdward Tomasz Napierala 	case MD_NULL:
1462dc604f0cSEdward Tomasz Napierala 		break;
1463dc604f0cSEdward Tomasz Napierala 	case MD_SWAP:
14648cb51643SJaakko Heinonen 		if (mdio->md_mediasize <= 0 ||
1465dc604f0cSEdward Tomasz Napierala 		    (mdio->md_mediasize % PAGE_SIZE) != 0)
1466dc604f0cSEdward Tomasz Napierala 			return (EDOM);
1467dc604f0cSEdward Tomasz Napierala 		oldpages = OFF_TO_IDX(round_page(sc->mediasize));
1468dc604f0cSEdward Tomasz Napierala 		newpages = OFF_TO_IDX(round_page(mdio->md_mediasize));
1469dc604f0cSEdward Tomasz Napierala 		if (newpages < oldpages) {
147089f6b863SAttilio Rao 			VM_OBJECT_WLOCK(sc->object);
1471dc604f0cSEdward Tomasz Napierala 			vm_object_page_remove(sc->object, newpages, 0, 0);
1472dc604f0cSEdward Tomasz Napierala 			swap_pager_freespace(sc->object, newpages,
1473dc604f0cSEdward Tomasz Napierala 			    oldpages - newpages);
1474dc604f0cSEdward Tomasz Napierala 			swap_release_by_cred(IDX_TO_OFF(oldpages -
1475dc604f0cSEdward Tomasz Napierala 			    newpages), sc->cred);
1476dc604f0cSEdward Tomasz Napierala 			sc->object->charge = IDX_TO_OFF(newpages);
1477dc604f0cSEdward Tomasz Napierala 			sc->object->size = newpages;
147889f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(sc->object);
1479dc604f0cSEdward Tomasz Napierala 		} else if (newpages > oldpages) {
1480dc604f0cSEdward Tomasz Napierala 			res = swap_reserve_by_cred(IDX_TO_OFF(newpages -
1481dc604f0cSEdward Tomasz Napierala 			    oldpages), sc->cred);
1482dc604f0cSEdward Tomasz Napierala 			if (!res)
1483dc604f0cSEdward Tomasz Napierala 				return (ENOMEM);
1484dc604f0cSEdward Tomasz Napierala 			if ((mdio->md_options & MD_RESERVE) ||
1485dc604f0cSEdward Tomasz Napierala 			    (sc->flags & MD_RESERVE)) {
1486dc604f0cSEdward Tomasz Napierala 				error = swap_pager_reserve(sc->object,
1487dc604f0cSEdward Tomasz Napierala 				    oldpages, newpages - oldpages);
1488dc604f0cSEdward Tomasz Napierala 				if (error < 0) {
1489dc604f0cSEdward Tomasz Napierala 					swap_release_by_cred(
1490dc604f0cSEdward Tomasz Napierala 					    IDX_TO_OFF(newpages - oldpages),
1491dc604f0cSEdward Tomasz Napierala 					    sc->cred);
1492dc604f0cSEdward Tomasz Napierala 					return (EDOM);
1493dc604f0cSEdward Tomasz Napierala 				}
1494dc604f0cSEdward Tomasz Napierala 			}
149589f6b863SAttilio Rao 			VM_OBJECT_WLOCK(sc->object);
1496dc604f0cSEdward Tomasz Napierala 			sc->object->charge = IDX_TO_OFF(newpages);
1497dc604f0cSEdward Tomasz Napierala 			sc->object->size = newpages;
149889f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(sc->object);
1499dc604f0cSEdward Tomasz Napierala 		}
1500dc604f0cSEdward Tomasz Napierala 		break;
1501dc604f0cSEdward Tomasz Napierala 	default:
1502dc604f0cSEdward Tomasz Napierala 		return (EOPNOTSUPP);
1503dc604f0cSEdward Tomasz Napierala 	}
1504dc604f0cSEdward Tomasz Napierala 
1505dc604f0cSEdward Tomasz Napierala 	sc->mediasize = mdio->md_mediasize;
1506dc604f0cSEdward Tomasz Napierala 	g_topology_lock();
1507dc604f0cSEdward Tomasz Napierala 	g_resize_provider(sc->pp, sc->mediasize);
1508dc604f0cSEdward Tomasz Napierala 	g_topology_unlock();
1509dc604f0cSEdward Tomasz Napierala 	return (0);
1510dc604f0cSEdward Tomasz Napierala }
1511dc604f0cSEdward Tomasz Napierala 
1512dc604f0cSEdward Tomasz Napierala static int
1513b830359bSPawel Jakub Dawidek mdcreate_swap(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
15148f8def9eSPoul-Henning Kamp {
1515fcd57fbeSPawel Jakub Dawidek 	vm_ooffset_t npage;
1516fcd57fbeSPawel Jakub Dawidek 	int error;
15178f8def9eSPoul-Henning Kamp 
15188f8def9eSPoul-Henning Kamp 	/*
1519bc2308d4SEdward Tomasz Napierala 	 * Range check.  Disallow negative sizes and sizes not being
1520bc2308d4SEdward Tomasz Napierala 	 * multiple of page size.
15218f8def9eSPoul-Henning Kamp 	 */
15228cb51643SJaakko Heinonen 	if (sc->mediasize <= 0 || (sc->mediasize % PAGE_SIZE) != 0)
15238f8def9eSPoul-Henning Kamp 		return (EDOM);
15248f8def9eSPoul-Henning Kamp 
15258f8def9eSPoul-Henning Kamp 	/*
15268f8def9eSPoul-Henning Kamp 	 * Allocate an OBJT_SWAP object.
15278f8def9eSPoul-Henning Kamp 	 *
15288f8def9eSPoul-Henning Kamp 	 * Note the truncation.
15298f8def9eSPoul-Henning Kamp 	 */
15308f8def9eSPoul-Henning Kamp 
1531b830359bSPawel Jakub Dawidek 	npage = mdio->md_mediasize / PAGE_SIZE;
15329ed40643SPoul-Henning Kamp 	if (mdio->md_fwsectors != 0)
15339ed40643SPoul-Henning Kamp 		sc->fwsectors = mdio->md_fwsectors;
15349ed40643SPoul-Henning Kamp 	if (mdio->md_fwheads != 0)
15359ed40643SPoul-Henning Kamp 		sc->fwheads = mdio->md_fwheads;
1536fcd57fbeSPawel Jakub Dawidek 	sc->object = vm_pager_allocate(OBJT_SWAP, NULL, PAGE_SIZE * npage,
15373364c323SKonstantin Belousov 	    VM_PROT_DEFAULT, 0, td->td_ucred);
1538812851b6SBrian Feldman 	if (sc->object == NULL)
1539812851b6SBrian Feldman 		return (ENOMEM);
1540dc604f0cSEdward Tomasz Napierala 	sc->flags = mdio->md_options & (MD_FORCE | MD_RESERVE);
15418f8def9eSPoul-Henning Kamp 	if (mdio->md_options & MD_RESERVE) {
1542fcd57fbeSPawel Jakub Dawidek 		if (swap_pager_reserve(sc->object, 0, npage) < 0) {
15433364c323SKonstantin Belousov 			error = EDOM;
15443364c323SKonstantin Belousov 			goto finish;
15458f8def9eSPoul-Henning Kamp 		}
15468f8def9eSPoul-Henning Kamp 	}
1547a854ed98SJohn Baldwin 	error = mdsetcred(sc, td->td_ucred);
15483364c323SKonstantin Belousov  finish:
1549e3ed29a7SPawel Jakub Dawidek 	if (error != 0) {
15502eafd8b1SPawel Jakub Dawidek 		vm_object_deallocate(sc->object);
15512eafd8b1SPawel Jakub Dawidek 		sc->object = NULL;
15528f8def9eSPoul-Henning Kamp 	}
1553b830359bSPawel Jakub Dawidek 	return (error);
1554b3b3d1b7SPoul-Henning Kamp }
15558f8def9eSPoul-Henning Kamp 
15560efd9bfdSEdward Tomasz Napierala static int
15570efd9bfdSEdward Tomasz Napierala mdcreate_null(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
15580efd9bfdSEdward Tomasz Napierala {
15590efd9bfdSEdward Tomasz Napierala 
15600efd9bfdSEdward Tomasz Napierala 	/*
1561bc2308d4SEdward Tomasz Napierala 	 * Range check.  Disallow negative sizes and sizes not being
1562bc2308d4SEdward Tomasz Napierala 	 * multiple of page size.
15630efd9bfdSEdward Tomasz Napierala 	 */
15640efd9bfdSEdward Tomasz Napierala 	if (sc->mediasize <= 0 || (sc->mediasize % PAGE_SIZE) != 0)
15650efd9bfdSEdward Tomasz Napierala 		return (EDOM);
15660efd9bfdSEdward Tomasz Napierala 
15670efd9bfdSEdward Tomasz Napierala 	return (0);
15680efd9bfdSEdward Tomasz Napierala }
15699d4b5945SMaxim Sobolev 
15709d4b5945SMaxim Sobolev static int
15719b00ca19SPoul-Henning Kamp xmdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
15728f8def9eSPoul-Henning Kamp {
15738f8def9eSPoul-Henning Kamp 	struct md_ioctl *mdio;
15748f8def9eSPoul-Henning Kamp 	struct md_s *sc;
1575b830359bSPawel Jakub Dawidek 	int error, i;
15768cb51643SJaakko Heinonen 	unsigned sectsize;
15778f8def9eSPoul-Henning Kamp 
15788f8def9eSPoul-Henning Kamp 	if (md_debug)
15798f8def9eSPoul-Henning Kamp 		printf("mdctlioctl(%s %lx %p %x %p)\n",
1580b40ce416SJulian Elischer 			devtoname(dev), cmd, addr, flags, td);
15818f8def9eSPoul-Henning Kamp 
15829b00ca19SPoul-Henning Kamp 	mdio = (struct md_ioctl *)addr;
15839b00ca19SPoul-Henning Kamp 	if (mdio->md_version != MDIOVERSION)
15849b00ca19SPoul-Henning Kamp 		return (EINVAL);
15859b00ca19SPoul-Henning Kamp 
158653d745bcSDima Dorfman 	/*
158753d745bcSDima Dorfman 	 * We assert the version number in the individual ioctl
158853d745bcSDima Dorfman 	 * handlers instead of out here because (a) it is possible we
158953d745bcSDima Dorfman 	 * may add another ioctl in the future which doesn't read an
159053d745bcSDima Dorfman 	 * mdio, and (b) the correct return value for an unknown ioctl
159153d745bcSDima Dorfman 	 * is ENOIOCTL, not EINVAL.
159253d745bcSDima Dorfman 	 */
15939b00ca19SPoul-Henning Kamp 	error = 0;
15948f8def9eSPoul-Henning Kamp 	switch (cmd) {
15958f8def9eSPoul-Henning Kamp 	case MDIOCATTACH:
15968f8def9eSPoul-Henning Kamp 		switch (mdio->md_type) {
15978f8def9eSPoul-Henning Kamp 		case MD_MALLOC:
15988f8def9eSPoul-Henning Kamp 		case MD_PRELOAD:
15998f8def9eSPoul-Henning Kamp 		case MD_VNODE:
16008f8def9eSPoul-Henning Kamp 		case MD_SWAP:
16010efd9bfdSEdward Tomasz Napierala 		case MD_NULL:
1602b830359bSPawel Jakub Dawidek 			break;
16038f8def9eSPoul-Henning Kamp 		default:
16048f8def9eSPoul-Henning Kamp 			return (EINVAL);
16058f8def9eSPoul-Henning Kamp 		}
16068cb51643SJaakko Heinonen 		if (mdio->md_sectorsize == 0)
16078cb51643SJaakko Heinonen 			sectsize = DEV_BSIZE;
16088cb51643SJaakko Heinonen 		else
16098cb51643SJaakko Heinonen 			sectsize = mdio->md_sectorsize;
16108cb51643SJaakko Heinonen 		if (sectsize > MAXPHYS || mdio->md_mediasize < sectsize)
16118cb51643SJaakko Heinonen 			return (EINVAL);
16127ee3c044SPawel Jakub Dawidek 		if (mdio->md_options & MD_AUTOUNIT)
1613947fc8deSPoul-Henning Kamp 			sc = mdnew(-1, &error, mdio->md_type);
1614f4e7c5a8SJaakko Heinonen 		else {
1615f4e7c5a8SJaakko Heinonen 			if (mdio->md_unit > INT_MAX)
1616f4e7c5a8SJaakko Heinonen 				return (EINVAL);
1617947fc8deSPoul-Henning Kamp 			sc = mdnew(mdio->md_unit, &error, mdio->md_type);
1618f4e7c5a8SJaakko Heinonen 		}
1619b830359bSPawel Jakub Dawidek 		if (sc == NULL)
16207ee3c044SPawel Jakub Dawidek 			return (error);
16217ee3c044SPawel Jakub Dawidek 		if (mdio->md_options & MD_AUTOUNIT)
16227ee3c044SPawel Jakub Dawidek 			mdio->md_unit = sc->unit;
1623b830359bSPawel Jakub Dawidek 		sc->mediasize = mdio->md_mediasize;
16248cb51643SJaakko Heinonen 		sc->sectorsize = sectsize;
1625b830359bSPawel Jakub Dawidek 		error = EDOOFUS;
1626b830359bSPawel Jakub Dawidek 		switch (sc->type) {
1627b830359bSPawel Jakub Dawidek 		case MD_MALLOC:
16289b00ca19SPoul-Henning Kamp 			sc->start = mdstart_malloc;
1629b830359bSPawel Jakub Dawidek 			error = mdcreate_malloc(sc, mdio);
1630b830359bSPawel Jakub Dawidek 			break;
1631b830359bSPawel Jakub Dawidek 		case MD_PRELOAD:
1632734e78dfSJaakko Heinonen 			/*
1633734e78dfSJaakko Heinonen 			 * We disallow attaching preloaded memory disks via
1634734e78dfSJaakko Heinonen 			 * ioctl. Preloaded memory disks are automatically
1635734e78dfSJaakko Heinonen 			 * attached in g_md_init().
1636734e78dfSJaakko Heinonen 			 */
1637734e78dfSJaakko Heinonen 			error = EOPNOTSUPP;
1638b830359bSPawel Jakub Dawidek 			break;
1639b830359bSPawel Jakub Dawidek 		case MD_VNODE:
16409b00ca19SPoul-Henning Kamp 			sc->start = mdstart_vnode;
1641b830359bSPawel Jakub Dawidek 			error = mdcreate_vnode(sc, mdio, td);
1642b830359bSPawel Jakub Dawidek 			break;
1643b830359bSPawel Jakub Dawidek 		case MD_SWAP:
16449b00ca19SPoul-Henning Kamp 			sc->start = mdstart_swap;
1645b830359bSPawel Jakub Dawidek 			error = mdcreate_swap(sc, mdio, td);
1646b830359bSPawel Jakub Dawidek 			break;
16470efd9bfdSEdward Tomasz Napierala 		case MD_NULL:
16480efd9bfdSEdward Tomasz Napierala 			sc->start = mdstart_null;
16490efd9bfdSEdward Tomasz Napierala 			error = mdcreate_null(sc, mdio, td);
16500efd9bfdSEdward Tomasz Napierala 			break;
1651b830359bSPawel Jakub Dawidek 		}
1652b830359bSPawel Jakub Dawidek 		if (error != 0) {
1653b830359bSPawel Jakub Dawidek 			mddestroy(sc, td);
1654b830359bSPawel Jakub Dawidek 			return (error);
1655b830359bSPawel Jakub Dawidek 		}
16569b00ca19SPoul-Henning Kamp 
16579b00ca19SPoul-Henning Kamp 		/* Prune off any residual fractional sector */
16589b00ca19SPoul-Henning Kamp 		i = sc->mediasize % sc->sectorsize;
16599b00ca19SPoul-Henning Kamp 		sc->mediasize -= i;
16609b00ca19SPoul-Henning Kamp 
1661b830359bSPawel Jakub Dawidek 		mdinit(sc);
1662b830359bSPawel Jakub Dawidek 		return (0);
16638f8def9eSPoul-Henning Kamp 	case MDIOCDETACH:
1664a9ebb311SEdward Tomasz Napierala 		if (mdio->md_mediasize != 0 ||
1665a9ebb311SEdward Tomasz Napierala 		    (mdio->md_options & ~MD_FORCE) != 0)
16668f8def9eSPoul-Henning Kamp 			return (EINVAL);
16679b00ca19SPoul-Henning Kamp 
16689b00ca19SPoul-Henning Kamp 		sc = mdfind(mdio->md_unit);
16699b00ca19SPoul-Henning Kamp 		if (sc == NULL)
16709b00ca19SPoul-Henning Kamp 			return (ENOENT);
1671a9ebb311SEdward Tomasz Napierala 		if (sc->opencount != 0 && !(sc->flags & MD_FORCE) &&
1672a9ebb311SEdward Tomasz Napierala 		    !(mdio->md_options & MD_FORCE))
16739b00ca19SPoul-Henning Kamp 			return (EBUSY);
16749b00ca19SPoul-Henning Kamp 		return (mddestroy(sc, td));
1675dc604f0cSEdward Tomasz Napierala 	case MDIOCRESIZE:
1676dc604f0cSEdward Tomasz Napierala 		if ((mdio->md_options & ~(MD_FORCE | MD_RESERVE)) != 0)
1677dc604f0cSEdward Tomasz Napierala 			return (EINVAL);
1678dc604f0cSEdward Tomasz Napierala 
1679dc604f0cSEdward Tomasz Napierala 		sc = mdfind(mdio->md_unit);
1680dc604f0cSEdward Tomasz Napierala 		if (sc == NULL)
1681dc604f0cSEdward Tomasz Napierala 			return (ENOENT);
16828cb51643SJaakko Heinonen 		if (mdio->md_mediasize < sc->sectorsize)
16838cb51643SJaakko Heinonen 			return (EINVAL);
1684dc604f0cSEdward Tomasz Napierala 		if (mdio->md_mediasize < sc->mediasize &&
1685dc604f0cSEdward Tomasz Napierala 		    !(sc->flags & MD_FORCE) &&
1686dc604f0cSEdward Tomasz Napierala 		    !(mdio->md_options & MD_FORCE))
1687dc604f0cSEdward Tomasz Napierala 			return (EBUSY);
1688dc604f0cSEdward Tomasz Napierala 		return (mdresize(sc, mdio));
1689174b5e9aSPoul-Henning Kamp 	case MDIOCQUERY:
1690174b5e9aSPoul-Henning Kamp 		sc = mdfind(mdio->md_unit);
1691174b5e9aSPoul-Henning Kamp 		if (sc == NULL)
1692174b5e9aSPoul-Henning Kamp 			return (ENOENT);
1693174b5e9aSPoul-Henning Kamp 		mdio->md_type = sc->type;
1694174b5e9aSPoul-Henning Kamp 		mdio->md_options = sc->flags;
1695b830359bSPawel Jakub Dawidek 		mdio->md_mediasize = sc->mediasize;
1696b830359bSPawel Jakub Dawidek 		mdio->md_sectorsize = sc->sectorsize;
16979b00ca19SPoul-Henning Kamp 		if (sc->type == MD_VNODE)
169888b5b78dSPawel Jakub Dawidek 			error = copyout(sc->file, mdio->md_file,
169988b5b78dSPawel Jakub Dawidek 			    strlen(sc->file) + 1);
170088b5b78dSPawel Jakub Dawidek 		return (error);
170116bcbe8cSPoul-Henning Kamp 	case MDIOCLIST:
170216bcbe8cSPoul-Henning Kamp 		i = 1;
170316bcbe8cSPoul-Henning Kamp 		LIST_FOREACH(sc, &md_softc_list, list) {
170416bcbe8cSPoul-Henning Kamp 			if (i == MDNPAD - 1)
170516bcbe8cSPoul-Henning Kamp 				mdio->md_pad[i] = -1;
170616bcbe8cSPoul-Henning Kamp 			else
170716bcbe8cSPoul-Henning Kamp 				mdio->md_pad[i++] = sc->unit;
170816bcbe8cSPoul-Henning Kamp 		}
170916bcbe8cSPoul-Henning Kamp 		mdio->md_pad[0] = i - 1;
171016bcbe8cSPoul-Henning Kamp 		return (0);
17118f8def9eSPoul-Henning Kamp 	default:
17128f8def9eSPoul-Henning Kamp 		return (ENOIOCTL);
17138f8def9eSPoul-Henning Kamp 	};
17149b00ca19SPoul-Henning Kamp }
17159b00ca19SPoul-Henning Kamp 
17169b00ca19SPoul-Henning Kamp static int
17179b00ca19SPoul-Henning Kamp mdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
17189b00ca19SPoul-Henning Kamp {
17199b00ca19SPoul-Henning Kamp 	int error;
17209b00ca19SPoul-Henning Kamp 
17219b00ca19SPoul-Henning Kamp 	sx_xlock(&md_sx);
17229b00ca19SPoul-Henning Kamp 	error = xmdctlioctl(dev, cmd, addr, flags, td);
17239b00ca19SPoul-Henning Kamp 	sx_xunlock(&md_sx);
17249b00ca19SPoul-Henning Kamp 	return (error);
17253f54a085SPoul-Henning Kamp }
17263f54a085SPoul-Henning Kamp 
172700a6a3c6SPoul-Henning Kamp static void
1728341b240dSJaakko Heinonen md_preloaded(u_char *image, size_t length, const char *name)
1729637f671aSPoul-Henning Kamp {
1730637f671aSPoul-Henning Kamp 	struct md_s *sc;
17319b00ca19SPoul-Henning Kamp 	int error;
1732637f671aSPoul-Henning Kamp 
1733947fc8deSPoul-Henning Kamp 	sc = mdnew(-1, &error, MD_PRELOAD);
1734637f671aSPoul-Henning Kamp 	if (sc == NULL)
1735637f671aSPoul-Henning Kamp 		return;
1736b830359bSPawel Jakub Dawidek 	sc->mediasize = length;
1737b830359bSPawel Jakub Dawidek 	sc->sectorsize = DEV_BSIZE;
1738637f671aSPoul-Henning Kamp 	sc->pl_ptr = image;
1739637f671aSPoul-Henning Kamp 	sc->pl_len = length;
17409b00ca19SPoul-Henning Kamp 	sc->start = mdstart_preload;
17415d4ca75eSLuigi Rizzo #ifdef MD_ROOT
1742637f671aSPoul-Henning Kamp 	if (sc->unit == 0)
174322ff74b2SMarcel Moolenaar 		rootdevnames[0] = MD_ROOT_FSTYPE ":/dev/md0";
17445d4ca75eSLuigi Rizzo #endif
1745637f671aSPoul-Henning Kamp 	mdinit(sc);
1746341b240dSJaakko Heinonen 	if (name != NULL) {
1747341b240dSJaakko Heinonen 		printf("%s%d: Preloaded image <%s> %zd bytes at %p\n",
1748341b240dSJaakko Heinonen 		    MD_NAME, sc->unit, name, length, image);
1749cc787e3dSMarcel Moolenaar 	} else {
1750c68ea8a6SMarcel Moolenaar 		printf("%s%d: Embedded image %zd bytes at %p\n",
1751cc787e3dSMarcel Moolenaar 		    MD_NAME, sc->unit, length, image);
1752341b240dSJaakko Heinonen 	}
1753637f671aSPoul-Henning Kamp }
1754637f671aSPoul-Henning Kamp 
1755637f671aSPoul-Henning Kamp static void
175619945697SPoul-Henning Kamp g_md_init(struct g_class *mp __unused)
175700a6a3c6SPoul-Henning Kamp {
175895f1a897SPoul-Henning Kamp 	caddr_t mod;
175995f1a897SPoul-Henning Kamp 	u_char *ptr, *name, *type;
176095f1a897SPoul-Henning Kamp 	unsigned len;
1761d12fc952SKonstantin Belousov 	int i;
1762d12fc952SKonstantin Belousov 
1763d12fc952SKonstantin Belousov 	/* figure out log2(NINDIR) */
1764d12fc952SKonstantin Belousov 	for (i = NINDIR, nshift = -1; i; nshift++)
1765d12fc952SKonstantin Belousov 		i >>= 1;
176695f1a897SPoul-Henning Kamp 
17670a937206SPoul-Henning Kamp 	mod = NULL;
17689b00ca19SPoul-Henning Kamp 	sx_init(&md_sx, "MD config lock");
17690a937206SPoul-Henning Kamp 	g_topology_unlock();
1770f4e7c5a8SJaakko Heinonen 	md_uh = new_unrhdr(0, INT_MAX, NULL);
1771cc787e3dSMarcel Moolenaar #ifdef MD_ROOT
1772cc787e3dSMarcel Moolenaar 	if (mfs_root_size != 0) {
17739b00ca19SPoul-Henning Kamp 		sx_xlock(&md_sx);
1774cc787e3dSMarcel Moolenaar 		md_preloaded(__DEVOLATILE(u_char *, &mfs_root), mfs_root_size,
1775cc787e3dSMarcel Moolenaar 		    NULL);
17769b00ca19SPoul-Henning Kamp 		sx_xunlock(&md_sx);
1777cc787e3dSMarcel Moolenaar 	}
177871e4fff8SPoul-Henning Kamp #endif
17799b00ca19SPoul-Henning Kamp 	/* XXX: are preload_* static or do they need Giant ? */
178095f1a897SPoul-Henning Kamp 	while ((mod = preload_search_next_name(mod)) != NULL) {
178195f1a897SPoul-Henning Kamp 		name = (char *)preload_search_info(mod, MODINFO_NAME);
178295f1a897SPoul-Henning Kamp 		if (name == NULL)
178395f1a897SPoul-Henning Kamp 			continue;
17849b00ca19SPoul-Henning Kamp 		type = (char *)preload_search_info(mod, MODINFO_TYPE);
178595f1a897SPoul-Henning Kamp 		if (type == NULL)
178695f1a897SPoul-Henning Kamp 			continue;
178771e4fff8SPoul-Henning Kamp 		if (strcmp(type, "md_image") && strcmp(type, "mfs_root"))
178895f1a897SPoul-Henning Kamp 			continue;
17898d5ac6c3SMarcel Moolenaar 		ptr = preload_fetch_addr(mod);
17908d5ac6c3SMarcel Moolenaar 		len = preload_fetch_size(mod);
17918d5ac6c3SMarcel Moolenaar 		if (ptr != NULL && len != 0) {
17929b00ca19SPoul-Henning Kamp 			sx_xlock(&md_sx);
1793341b240dSJaakko Heinonen 			md_preloaded(ptr, len, name);
17949b00ca19SPoul-Henning Kamp 			sx_xunlock(&md_sx);
179595f1a897SPoul-Henning Kamp 		}
17968d5ac6c3SMarcel Moolenaar 	}
179759ec9023SKonstantin Belousov 	md_vnode_pbuf_freecnt = nswbuf / 10;
179806d425f9SEd Schouten 	status_dev = make_dev(&mdctl_cdevsw, INT_MAX, UID_ROOT, GID_WHEEL,
179910b0e058SDima Dorfman 	    0600, MDCTL_NAME);
18000eb14309SPoul-Henning Kamp 	g_topology_lock();
180100a6a3c6SPoul-Henning Kamp }
180200a6a3c6SPoul-Henning Kamp 
180319945697SPoul-Henning Kamp static void
1804c27a8954SWojciech A. Koszek g_md_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1805c27a8954SWojciech A. Koszek     struct g_consumer *cp __unused, struct g_provider *pp)
1806c27a8954SWojciech A. Koszek {
1807c27a8954SWojciech A. Koszek 	struct md_s *mp;
1808c27a8954SWojciech A. Koszek 	char *type;
1809c27a8954SWojciech A. Koszek 
1810c27a8954SWojciech A. Koszek 	mp = gp->softc;
1811c27a8954SWojciech A. Koszek 	if (mp == NULL)
1812c27a8954SWojciech A. Koszek 		return;
1813c27a8954SWojciech A. Koszek 
1814c27a8954SWojciech A. Koszek 	switch (mp->type) {
1815c27a8954SWojciech A. Koszek 	case MD_MALLOC:
1816c27a8954SWojciech A. Koszek 		type = "malloc";
1817c27a8954SWojciech A. Koszek 		break;
1818c27a8954SWojciech A. Koszek 	case MD_PRELOAD:
1819c27a8954SWojciech A. Koszek 		type = "preload";
1820c27a8954SWojciech A. Koszek 		break;
1821c27a8954SWojciech A. Koszek 	case MD_VNODE:
1822c27a8954SWojciech A. Koszek 		type = "vnode";
1823c27a8954SWojciech A. Koszek 		break;
1824c27a8954SWojciech A. Koszek 	case MD_SWAP:
1825c27a8954SWojciech A. Koszek 		type = "swap";
1826c27a8954SWojciech A. Koszek 		break;
18270efd9bfdSEdward Tomasz Napierala 	case MD_NULL:
18280efd9bfdSEdward Tomasz Napierala 		type = "null";
18290efd9bfdSEdward Tomasz Napierala 		break;
1830c27a8954SWojciech A. Koszek 	default:
1831c27a8954SWojciech A. Koszek 		type = "unknown";
1832c27a8954SWojciech A. Koszek 		break;
1833c27a8954SWojciech A. Koszek 	}
1834c27a8954SWojciech A. Koszek 
1835c27a8954SWojciech A. Koszek 	if (pp != NULL) {
1836c27a8954SWojciech A. Koszek 		if (indent == NULL) {
1837c27a8954SWojciech A. Koszek 			sbuf_printf(sb, " u %d", mp->unit);
1838c27a8954SWojciech A. Koszek 			sbuf_printf(sb, " s %ju", (uintmax_t) mp->sectorsize);
1839c27a8954SWojciech A. Koszek 			sbuf_printf(sb, " f %ju", (uintmax_t) mp->fwheads);
1840c27a8954SWojciech A. Koszek 			sbuf_printf(sb, " fs %ju", (uintmax_t) mp->fwsectors);
1841c27a8954SWojciech A. Koszek 			sbuf_printf(sb, " l %ju", (uintmax_t) mp->mediasize);
1842c27a8954SWojciech A. Koszek 			sbuf_printf(sb, " t %s", type);
1843c27a8954SWojciech A. Koszek 			if (mp->type == MD_VNODE && mp->vnode != NULL)
1844c27a8954SWojciech A. Koszek 				sbuf_printf(sb, " file %s", mp->file);
1845c27a8954SWojciech A. Koszek 		} else {
1846c27a8954SWojciech A. Koszek 			sbuf_printf(sb, "%s<unit>%d</unit>\n", indent,
1847c27a8954SWojciech A. Koszek 			    mp->unit);
1848c27a8954SWojciech A. Koszek 			sbuf_printf(sb, "%s<sectorsize>%ju</sectorsize>\n",
1849c27a8954SWojciech A. Koszek 			    indent, (uintmax_t) mp->sectorsize);
1850c27a8954SWojciech A. Koszek 			sbuf_printf(sb, "%s<fwheads>%ju</fwheads>\n",
1851c27a8954SWojciech A. Koszek 			    indent, (uintmax_t) mp->fwheads);
1852c27a8954SWojciech A. Koszek 			sbuf_printf(sb, "%s<fwsectors>%ju</fwsectors>\n",
1853c27a8954SWojciech A. Koszek 			    indent, (uintmax_t) mp->fwsectors);
1854c27a8954SWojciech A. Koszek 			sbuf_printf(sb, "%s<length>%ju</length>\n",
1855c27a8954SWojciech A. Koszek 			    indent, (uintmax_t) mp->mediasize);
18561f192809SAndrey V. Elsukov 			sbuf_printf(sb, "%s<compression>%s</compression>\n", indent,
18571f192809SAndrey V. Elsukov 			    (mp->flags & MD_COMPRESS) == 0 ? "off": "on");
18581f192809SAndrey V. Elsukov 			sbuf_printf(sb, "%s<access>%s</access>\n", indent,
18591f192809SAndrey V. Elsukov 			    (mp->flags & MD_READONLY) == 0 ? "read-write":
18601f192809SAndrey V. Elsukov 			    "read-only");
1861c27a8954SWojciech A. Koszek 			sbuf_printf(sb, "%s<type>%s</type>\n", indent,
1862c27a8954SWojciech A. Koszek 			    type);
1863ec170744SAndrey V. Elsukov 			if (mp->type == MD_VNODE && mp->vnode != NULL) {
1864ec170744SAndrey V. Elsukov 				sbuf_printf(sb, "%s<file>", indent);
1865ec170744SAndrey V. Elsukov 				g_conf_printf_escaped(sb, "%s", mp->file);
1866ec170744SAndrey V. Elsukov 				sbuf_printf(sb, "</file>\n");
1867ec170744SAndrey V. Elsukov 			}
1868c27a8954SWojciech A. Koszek 		}
1869c27a8954SWojciech A. Koszek 	}
1870c27a8954SWojciech A. Koszek }
1871c27a8954SWojciech A. Koszek 
1872c27a8954SWojciech A. Koszek static void
187319945697SPoul-Henning Kamp g_md_fini(struct g_class *mp __unused)
187457e9624eSPoul-Henning Kamp {
18759d4b5945SMaxim Sobolev 
18769b00ca19SPoul-Henning Kamp 	sx_destroy(&md_sx);
187719945697SPoul-Henning Kamp 	if (status_dev != NULL)
187857e9624eSPoul-Henning Kamp 		destroy_dev(status_dev);
1879f4e7c5a8SJaakko Heinonen 	delete_unrhdr(md_uh);
188057e9624eSPoul-Henning Kamp }
1881