xref: /freebsd/sys/dev/md/md.c (revision 5050aa86cff105784877fb886a7b1d25bca5813b)
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.
21637f671aSPoul-Henning Kamp  *
22ed010cdfSWarner Losh  * This code is derived from software contributed to Berkeley by
23ed010cdfSWarner Losh  * the Systems Programming Group of the University of Utah Computer
24ed010cdfSWarner Losh  * Science Department.
25ed010cdfSWarner Losh  *
26637f671aSPoul-Henning Kamp  * Redistribution and use in source and binary forms, with or without
27637f671aSPoul-Henning Kamp  * modification, are permitted provided that the following conditions
28637f671aSPoul-Henning Kamp  * are met:
29637f671aSPoul-Henning Kamp  * 1. Redistributions of source code must retain the above copyright
30637f671aSPoul-Henning Kamp  *    notice, this list of conditions and the following disclaimer.
31637f671aSPoul-Henning Kamp  * 2. Redistributions in binary form must reproduce the above copyright
32637f671aSPoul-Henning Kamp  *    notice, this list of conditions and the following disclaimer in the
33637f671aSPoul-Henning Kamp  *    documentation and/or other materials provided with the distribution.
34637f671aSPoul-Henning Kamp  * 4. Neither the name of the University nor the names of its contributors
35637f671aSPoul-Henning Kamp  *    may be used to endorse or promote products derived from this software
36637f671aSPoul-Henning Kamp  *    without specific prior written permission.
37637f671aSPoul-Henning Kamp  *
38637f671aSPoul-Henning Kamp  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
39637f671aSPoul-Henning Kamp  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40637f671aSPoul-Henning Kamp  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41637f671aSPoul-Henning Kamp  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
42637f671aSPoul-Henning Kamp  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43637f671aSPoul-Henning Kamp  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44637f671aSPoul-Henning Kamp  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45637f671aSPoul-Henning Kamp  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46637f671aSPoul-Henning Kamp  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47637f671aSPoul-Henning Kamp  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48637f671aSPoul-Henning Kamp  * SUCH DAMAGE.
49637f671aSPoul-Henning Kamp  *
50637f671aSPoul-Henning Kamp  * from: Utah Hdr: vn.c 1.13 94/04/02
51637f671aSPoul-Henning Kamp  *
52637f671aSPoul-Henning Kamp  *	from: @(#)vn.c	8.6 (Berkeley) 4/1/94
53637f671aSPoul-Henning Kamp  * From: src/sys/dev/vn/vn.c,v 1.122 2000/12/16 16:06:03
54637f671aSPoul-Henning Kamp  */
55637f671aSPoul-Henning Kamp 
566f4f00f1SPoul-Henning Kamp #include "opt_geom.h"
573f54a085SPoul-Henning Kamp #include "opt_md.h"
5871e4fff8SPoul-Henning Kamp 
5900a6a3c6SPoul-Henning Kamp #include <sys/param.h>
6000a6a3c6SPoul-Henning Kamp #include <sys/systm.h>
619626b608SPoul-Henning Kamp #include <sys/bio.h>
6200a6a3c6SPoul-Henning Kamp #include <sys/conf.h>
63a03be42dSMaxim Sobolev #include <sys/devicestat.h>
648f8def9eSPoul-Henning Kamp #include <sys/fcntl.h>
65fb919e4dSMark Murray #include <sys/kernel.h>
665c97ca54SIan Dowse #include <sys/kthread.h>
6706d425f9SEd Schouten #include <sys/limits.h>
68fb919e4dSMark Murray #include <sys/linker.h>
69fb919e4dSMark Murray #include <sys/lock.h>
70fb919e4dSMark Murray #include <sys/malloc.h>
71fb919e4dSMark Murray #include <sys/mdioctl.h>
72a08d2e7fSJohn Baldwin #include <sys/mount.h>
739dceb26bSJohn Baldwin #include <sys/mutex.h>
749b00ca19SPoul-Henning Kamp #include <sys/sx.h>
75fb919e4dSMark Murray #include <sys/namei.h>
768f8def9eSPoul-Henning Kamp #include <sys/proc.h>
77fb919e4dSMark Murray #include <sys/queue.h>
78657bd8b1SAndrey V. Elsukov #include <sys/sbuf.h>
7963710c4dSJohn Baldwin #include <sys/sched.h>
807cd53fddSAlan Cox #include <sys/sf_buf.h>
81fb919e4dSMark Murray #include <sys/sysctl.h>
82fb919e4dSMark Murray #include <sys/vnode.h>
83fb919e4dSMark Murray 
846f4f00f1SPoul-Henning Kamp #include <geom/geom.h>
856f4f00f1SPoul-Henning Kamp 
868f8def9eSPoul-Henning Kamp #include <vm/vm.h>
871c771f92SKonstantin Belousov #include <vm/vm_param.h>
888f8def9eSPoul-Henning Kamp #include <vm/vm_object.h>
898f8def9eSPoul-Henning Kamp #include <vm/vm_page.h>
908f8def9eSPoul-Henning Kamp #include <vm/vm_pager.h>
918f8def9eSPoul-Henning Kamp #include <vm/swap_pager.h>
92f43b2bacSPoul-Henning Kamp #include <vm/uma.h>
933f54a085SPoul-Henning Kamp 
9457e9624eSPoul-Henning Kamp #define MD_MODVER 1
9557e9624eSPoul-Henning Kamp 
965c97ca54SIan Dowse #define MD_SHUTDOWN	0x10000		/* Tell worker thread to terminate. */
97a08d2e7fSJohn Baldwin #define	MD_EXITING	0x20000		/* Worker thread is exiting. */
985c97ca54SIan Dowse 
99f2744793SSheldon Hearn #ifndef MD_NSECT
100f2744793SSheldon Hearn #define MD_NSECT (10000 * 2)
10133edfabeSPoul-Henning Kamp #endif
10233edfabeSPoul-Henning Kamp 
1035bb84bc8SRobert Watson static MALLOC_DEFINE(M_MD, "md_disk", "Memory Disk");
1045bb84bc8SRobert Watson static MALLOC_DEFINE(M_MDSECT, "md_sectors", "Memory Disk Sectors");
10500a6a3c6SPoul-Henning Kamp 
10671e4fff8SPoul-Henning Kamp static int md_debug;
1073eb9ab52SEitan Adler SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0,
1083eb9ab52SEitan Adler     "Enable md(4) debug messages");
109c44d423eSKonstantin Belousov static int md_malloc_wait;
1103eb9ab52SEitan Adler SYSCTL_INT(_vm, OID_AUTO, md_malloc_wait, CTLFLAG_RW, &md_malloc_wait, 0,
1113eb9ab52SEitan Adler     "Allow malloc to wait for memory allocations");
11200a6a3c6SPoul-Henning Kamp 
11371e4fff8SPoul-Henning Kamp #if defined(MD_ROOT) && defined(MD_ROOT_SIZE)
114de64f22aSLuigi Rizzo /*
115de64f22aSLuigi Rizzo  * Preloaded image gets put here.
116de64f22aSLuigi Rizzo  * Applications that patch the object with the image can determine
117de64f22aSLuigi Rizzo  * the size looking at the start and end markers (strings),
118de64f22aSLuigi Rizzo  * so we want them contiguous.
119de64f22aSLuigi Rizzo  */
120de64f22aSLuigi Rizzo static struct {
121de64f22aSLuigi Rizzo 	u_char start[MD_ROOT_SIZE*1024];
122de64f22aSLuigi Rizzo 	u_char end[128];
123de64f22aSLuigi Rizzo } mfs_root = {
124de64f22aSLuigi Rizzo 	.start = "MFS Filesystem goes here",
125de64f22aSLuigi Rizzo 	.end = "MFS Filesystem had better STOP here",
126de64f22aSLuigi Rizzo };
12771e4fff8SPoul-Henning Kamp #endif
12871e4fff8SPoul-Henning Kamp 
12919945697SPoul-Henning Kamp static g_init_t g_md_init;
13019945697SPoul-Henning Kamp static g_fini_t g_md_fini;
13119945697SPoul-Henning Kamp static g_start_t g_md_start;
13219945697SPoul-Henning Kamp static g_access_t g_md_access;
133b42f40b8SJaakko Heinonen static void g_md_dumpconf(struct sbuf *sb, const char *indent,
134b42f40b8SJaakko Heinonen     struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp);
1350eb14309SPoul-Henning Kamp 
1368f8def9eSPoul-Henning Kamp static int mdunits;
13789c9c53dSPoul-Henning Kamp static struct cdev *status_dev = 0;
1389b00ca19SPoul-Henning Kamp static struct sx md_sx;
139f4e7c5a8SJaakko Heinonen static struct unrhdr *md_uh;
14057e9624eSPoul-Henning Kamp 
141a522a159SPoul-Henning Kamp static d_ioctl_t mdctlioctl;
1428f8def9eSPoul-Henning Kamp 
1438f8def9eSPoul-Henning Kamp static struct cdevsw mdctl_cdevsw = {
144dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
1457ac40f5fSPoul-Henning Kamp 	.d_ioctl =	mdctlioctl,
1467ac40f5fSPoul-Henning Kamp 	.d_name =	MD_NAME,
14700a6a3c6SPoul-Henning Kamp };
14800a6a3c6SPoul-Henning Kamp 
14919945697SPoul-Henning Kamp struct g_class g_md_class = {
15019945697SPoul-Henning Kamp 	.name = "MD",
1515721c9c7SPoul-Henning Kamp 	.version = G_VERSION,
15219945697SPoul-Henning Kamp 	.init = g_md_init,
15319945697SPoul-Henning Kamp 	.fini = g_md_fini,
15419945697SPoul-Henning Kamp 	.start = g_md_start,
15519945697SPoul-Henning Kamp 	.access = g_md_access,
156c27a8954SWojciech A. Koszek 	.dumpconf = g_md_dumpconf,
15719945697SPoul-Henning Kamp };
15819945697SPoul-Henning Kamp 
15919945697SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_md_class, g_md);
16019945697SPoul-Henning Kamp 
1610cfaeeeeSPoul-Henning Kamp 
16213e403fdSAntoine Brodin static LIST_HEAD(, md_s) md_softc_list = LIST_HEAD_INITIALIZER(md_softc_list);
1633f54a085SPoul-Henning Kamp 
164c6517568SPoul-Henning Kamp #define NINDIR	(PAGE_SIZE / sizeof(uintptr_t))
165c6517568SPoul-Henning Kamp #define NMASK	(NINDIR-1)
166c6517568SPoul-Henning Kamp static int nshift;
167c6517568SPoul-Henning Kamp 
168c6517568SPoul-Henning Kamp struct indir {
169c6517568SPoul-Henning Kamp 	uintptr_t	*array;
1708b149b51SJohn Baldwin 	u_int		total;
1718b149b51SJohn Baldwin 	u_int		used;
1728b149b51SJohn Baldwin 	u_int		shift;
173c6517568SPoul-Henning Kamp };
174c6517568SPoul-Henning Kamp 
17500a6a3c6SPoul-Henning Kamp struct md_s {
17600a6a3c6SPoul-Henning Kamp 	int unit;
1773f54a085SPoul-Henning Kamp 	LIST_ENTRY(md_s) list;
1788177437dSPoul-Henning Kamp 	struct bio_queue_head bio_queue;
1790f8500a5SPoul-Henning Kamp 	struct mtx queue_mtx;
18089c9c53dSPoul-Henning Kamp 	struct cdev *dev;
1818f8def9eSPoul-Henning Kamp 	enum md_types type;
182b830359bSPawel Jakub Dawidek 	off_t mediasize;
183b830359bSPawel Jakub Dawidek 	unsigned sectorsize;
184fe603109SMaxim Sobolev 	unsigned opencount;
1854e8bfe14SPoul-Henning Kamp 	unsigned fwheads;
1864e8bfe14SPoul-Henning Kamp 	unsigned fwsectors;
1878f8def9eSPoul-Henning Kamp 	unsigned flags;
188f43b2bacSPoul-Henning Kamp 	char name[20];
1895c97ca54SIan Dowse 	struct proc *procp;
1906f4f00f1SPoul-Henning Kamp 	struct g_geom *gp;
1916f4f00f1SPoul-Henning Kamp 	struct g_provider *pp;
1929b00ca19SPoul-Henning Kamp 	int (*start)(struct md_s *sc, struct bio *bp);
193a03be42dSMaxim Sobolev 	struct devstat *devstat;
19495f1a897SPoul-Henning Kamp 
19595f1a897SPoul-Henning Kamp 	/* MD_MALLOC related fields */
196c6517568SPoul-Henning Kamp 	struct indir *indir;
197f43b2bacSPoul-Henning Kamp 	uma_zone_t uma;
19800a6a3c6SPoul-Henning Kamp 
19995f1a897SPoul-Henning Kamp 	/* MD_PRELOAD related fields */
20095f1a897SPoul-Henning Kamp 	u_char *pl_ptr;
201b830359bSPawel Jakub Dawidek 	size_t pl_len;
20200a6a3c6SPoul-Henning Kamp 
2038f8def9eSPoul-Henning Kamp 	/* MD_VNODE related fields */
2048f8def9eSPoul-Henning Kamp 	struct vnode *vnode;
20561a6eb62SPawel Jakub Dawidek 	char file[PATH_MAX];
2068f8def9eSPoul-Henning Kamp 	struct ucred *cred;
2078f8def9eSPoul-Henning Kamp 
208e0cebb40SDima Dorfman 	/* MD_SWAP related fields */
2098f8def9eSPoul-Henning Kamp 	vm_object_t object;
2108f8def9eSPoul-Henning Kamp };
21100a6a3c6SPoul-Henning Kamp 
212c6517568SPoul-Henning Kamp static struct indir *
2138b149b51SJohn Baldwin new_indir(u_int shift)
214c6517568SPoul-Henning Kamp {
215c6517568SPoul-Henning Kamp 	struct indir *ip;
216c6517568SPoul-Henning Kamp 
217c44d423eSKonstantin Belousov 	ip = malloc(sizeof *ip, M_MD, (md_malloc_wait ? M_WAITOK : M_NOWAIT)
218c44d423eSKonstantin Belousov 	    | M_ZERO);
219c6517568SPoul-Henning Kamp 	if (ip == NULL)
220c6517568SPoul-Henning Kamp 		return (NULL);
221c6517568SPoul-Henning Kamp 	ip->array = malloc(sizeof(uintptr_t) * NINDIR,
222c44d423eSKonstantin Belousov 	    M_MDSECT, (md_malloc_wait ? M_WAITOK : M_NOWAIT) | M_ZERO);
223c6517568SPoul-Henning Kamp 	if (ip->array == NULL) {
224c6517568SPoul-Henning Kamp 		free(ip, M_MD);
225c6517568SPoul-Henning Kamp 		return (NULL);
226c6517568SPoul-Henning Kamp 	}
227c6517568SPoul-Henning Kamp 	ip->total = NINDIR;
228c6517568SPoul-Henning Kamp 	ip->shift = shift;
229c6517568SPoul-Henning Kamp 	return (ip);
230c6517568SPoul-Henning Kamp }
231c6517568SPoul-Henning Kamp 
232c6517568SPoul-Henning Kamp static void
233c6517568SPoul-Henning Kamp del_indir(struct indir *ip)
234c6517568SPoul-Henning Kamp {
235c6517568SPoul-Henning Kamp 
236f43b2bacSPoul-Henning Kamp 	free(ip->array, M_MDSECT);
237c6517568SPoul-Henning Kamp 	free(ip, M_MD);
238c6517568SPoul-Henning Kamp }
239c6517568SPoul-Henning Kamp 
240f43b2bacSPoul-Henning Kamp static void
241f43b2bacSPoul-Henning Kamp destroy_indir(struct md_s *sc, struct indir *ip)
242f43b2bacSPoul-Henning Kamp {
243f43b2bacSPoul-Henning Kamp 	int i;
244f43b2bacSPoul-Henning Kamp 
245f43b2bacSPoul-Henning Kamp 	for (i = 0; i < NINDIR; i++) {
246f43b2bacSPoul-Henning Kamp 		if (!ip->array[i])
247f43b2bacSPoul-Henning Kamp 			continue;
248f43b2bacSPoul-Henning Kamp 		if (ip->shift)
249f43b2bacSPoul-Henning Kamp 			destroy_indir(sc, (struct indir*)(ip->array[i]));
250f43b2bacSPoul-Henning Kamp 		else if (ip->array[i] > 255)
251f43b2bacSPoul-Henning Kamp 			uma_zfree(sc->uma, (void *)(ip->array[i]));
252f43b2bacSPoul-Henning Kamp 	}
253f43b2bacSPoul-Henning Kamp 	del_indir(ip);
254f43b2bacSPoul-Henning Kamp }
255f43b2bacSPoul-Henning Kamp 
256c6517568SPoul-Henning Kamp /*
2576c3cd0e2SMaxim Konovalov  * This function does the math and allocates the top level "indir" structure
258c6517568SPoul-Henning Kamp  * for a device of "size" sectors.
259c6517568SPoul-Henning Kamp  */
260c6517568SPoul-Henning Kamp 
261c6517568SPoul-Henning Kamp static struct indir *
262c6517568SPoul-Henning Kamp dimension(off_t size)
263c6517568SPoul-Henning Kamp {
264c6517568SPoul-Henning Kamp 	off_t rcnt;
265c6517568SPoul-Henning Kamp 	struct indir *ip;
266d12fc952SKonstantin Belousov 	int layer;
267c6517568SPoul-Henning Kamp 
268c6517568SPoul-Henning Kamp 	rcnt = size;
269c6517568SPoul-Henning Kamp 	layer = 0;
270c6517568SPoul-Henning Kamp 	while (rcnt > NINDIR) {
271c6517568SPoul-Henning Kamp 		rcnt /= NINDIR;
272c6517568SPoul-Henning Kamp 		layer++;
273c6517568SPoul-Henning Kamp 	}
274c6517568SPoul-Henning Kamp 
275c6517568SPoul-Henning Kamp 	/*
276c6517568SPoul-Henning Kamp 	 * XXX: the top layer is probably not fully populated, so we allocate
27783e13864SPoul-Henning Kamp 	 * too much space for ip->array in here.
278c6517568SPoul-Henning Kamp 	 */
27983e13864SPoul-Henning Kamp 	ip = malloc(sizeof *ip, M_MD, M_WAITOK | M_ZERO);
28083e13864SPoul-Henning Kamp 	ip->array = malloc(sizeof(uintptr_t) * NINDIR,
28183e13864SPoul-Henning Kamp 	    M_MDSECT, M_WAITOK | M_ZERO);
28283e13864SPoul-Henning Kamp 	ip->total = NINDIR;
28383e13864SPoul-Henning Kamp 	ip->shift = layer * nshift;
284c6517568SPoul-Henning Kamp 	return (ip);
285c6517568SPoul-Henning Kamp }
286c6517568SPoul-Henning Kamp 
287c6517568SPoul-Henning Kamp /*
288c6517568SPoul-Henning Kamp  * Read a given sector
289c6517568SPoul-Henning Kamp  */
290c6517568SPoul-Henning Kamp 
291c6517568SPoul-Henning Kamp static uintptr_t
292c6517568SPoul-Henning Kamp s_read(struct indir *ip, off_t offset)
293c6517568SPoul-Henning Kamp {
294c6517568SPoul-Henning Kamp 	struct indir *cip;
295c6517568SPoul-Henning Kamp 	int idx;
296c6517568SPoul-Henning Kamp 	uintptr_t up;
297c6517568SPoul-Henning Kamp 
298c6517568SPoul-Henning Kamp 	if (md_debug > 1)
2996569d6f3SMaxime Henrion 		printf("s_read(%jd)\n", (intmax_t)offset);
300c6517568SPoul-Henning Kamp 	up = 0;
301c6517568SPoul-Henning Kamp 	for (cip = ip; cip != NULL;) {
302c6517568SPoul-Henning Kamp 		if (cip->shift) {
303c6517568SPoul-Henning Kamp 			idx = (offset >> cip->shift) & NMASK;
304c6517568SPoul-Henning Kamp 			up = cip->array[idx];
305c6517568SPoul-Henning Kamp 			cip = (struct indir *)up;
306c6517568SPoul-Henning Kamp 			continue;
307c6517568SPoul-Henning Kamp 		}
308c6517568SPoul-Henning Kamp 		idx = offset & NMASK;
309c6517568SPoul-Henning Kamp 		return (cip->array[idx]);
310c6517568SPoul-Henning Kamp 	}
311c6517568SPoul-Henning Kamp 	return (0);
312c6517568SPoul-Henning Kamp }
313c6517568SPoul-Henning Kamp 
314c6517568SPoul-Henning Kamp /*
315c6517568SPoul-Henning Kamp  * Write a given sector, prune the tree if the value is 0
316c6517568SPoul-Henning Kamp  */
317c6517568SPoul-Henning Kamp 
318c6517568SPoul-Henning Kamp static int
319fde2a2e4SPoul-Henning Kamp s_write(struct indir *ip, off_t offset, uintptr_t ptr)
320c6517568SPoul-Henning Kamp {
321c6517568SPoul-Henning Kamp 	struct indir *cip, *lip[10];
322c6517568SPoul-Henning Kamp 	int idx, li;
323c6517568SPoul-Henning Kamp 	uintptr_t up;
324c6517568SPoul-Henning Kamp 
325c6517568SPoul-Henning Kamp 	if (md_debug > 1)
3266569d6f3SMaxime Henrion 		printf("s_write(%jd, %p)\n", (intmax_t)offset, (void *)ptr);
327c6517568SPoul-Henning Kamp 	up = 0;
328c6517568SPoul-Henning Kamp 	li = 0;
329c6517568SPoul-Henning Kamp 	cip = ip;
330c6517568SPoul-Henning Kamp 	for (;;) {
331c6517568SPoul-Henning Kamp 		lip[li++] = cip;
332c6517568SPoul-Henning Kamp 		if (cip->shift) {
333c6517568SPoul-Henning Kamp 			idx = (offset >> cip->shift) & NMASK;
334c6517568SPoul-Henning Kamp 			up = cip->array[idx];
335c6517568SPoul-Henning Kamp 			if (up != 0) {
336c6517568SPoul-Henning Kamp 				cip = (struct indir *)up;
337c6517568SPoul-Henning Kamp 				continue;
338c6517568SPoul-Henning Kamp 			}
339c6517568SPoul-Henning Kamp 			/* Allocate branch */
340c6517568SPoul-Henning Kamp 			cip->array[idx] =
341c6517568SPoul-Henning Kamp 			    (uintptr_t)new_indir(cip->shift - nshift);
342c6517568SPoul-Henning Kamp 			if (cip->array[idx] == 0)
343975b628fSPoul-Henning Kamp 				return (ENOSPC);
344c6517568SPoul-Henning Kamp 			cip->used++;
345c6517568SPoul-Henning Kamp 			up = cip->array[idx];
346c6517568SPoul-Henning Kamp 			cip = (struct indir *)up;
347c6517568SPoul-Henning Kamp 			continue;
348c6517568SPoul-Henning Kamp 		}
349c6517568SPoul-Henning Kamp 		/* leafnode */
350c6517568SPoul-Henning Kamp 		idx = offset & NMASK;
351c6517568SPoul-Henning Kamp 		up = cip->array[idx];
352c6517568SPoul-Henning Kamp 		if (up != 0)
353c6517568SPoul-Henning Kamp 			cip->used--;
354c6517568SPoul-Henning Kamp 		cip->array[idx] = ptr;
355c6517568SPoul-Henning Kamp 		if (ptr != 0)
356c6517568SPoul-Henning Kamp 			cip->used++;
357c6517568SPoul-Henning Kamp 		break;
358c6517568SPoul-Henning Kamp 	}
359c6517568SPoul-Henning Kamp 	if (cip->used != 0 || li == 1)
360c6517568SPoul-Henning Kamp 		return (0);
361c6517568SPoul-Henning Kamp 	li--;
362c6517568SPoul-Henning Kamp 	while (cip->used == 0 && cip != ip) {
363c6517568SPoul-Henning Kamp 		li--;
364c6517568SPoul-Henning Kamp 		idx = (offset >> lip[li]->shift) & NMASK;
365c6517568SPoul-Henning Kamp 		up = lip[li]->array[idx];
366c6517568SPoul-Henning Kamp 		KASSERT(up == (uintptr_t)cip, ("md screwed up"));
367c6517568SPoul-Henning Kamp 		del_indir(cip);
3684a6a94d8SArchie Cobbs 		lip[li]->array[idx] = 0;
369c6517568SPoul-Henning Kamp 		lip[li]->used--;
370c6517568SPoul-Henning Kamp 		cip = lip[li];
371c6517568SPoul-Henning Kamp 	}
372c6517568SPoul-Henning Kamp 	return (0);
373c6517568SPoul-Henning Kamp }
374c6517568SPoul-Henning Kamp 
3756f4f00f1SPoul-Henning Kamp 
3766f4f00f1SPoul-Henning Kamp static int
3776f4f00f1SPoul-Henning Kamp g_md_access(struct g_provider *pp, int r, int w, int e)
3786f4f00f1SPoul-Henning Kamp {
3796f4f00f1SPoul-Henning Kamp 	struct md_s *sc;
3806f4f00f1SPoul-Henning Kamp 
3816f4f00f1SPoul-Henning Kamp 	sc = pp->geom->softc;
38241c8b468SEdward Tomasz Napierala 	if (sc == NULL) {
38341c8b468SEdward Tomasz Napierala 		if (r <= 0 && w <= 0 && e <= 0)
38441c8b468SEdward Tomasz Napierala 			return (0);
3856b60a2cdSPoul-Henning Kamp 		return (ENXIO);
38641c8b468SEdward Tomasz Napierala 	}
3876f4f00f1SPoul-Henning Kamp 	r += pp->acr;
3886f4f00f1SPoul-Henning Kamp 	w += pp->acw;
3896f4f00f1SPoul-Henning Kamp 	e += pp->ace;
39086776891SChristian S.J. Peron 	if ((sc->flags & MD_READONLY) != 0 && w > 0)
39186776891SChristian S.J. Peron 		return (EROFS);
3926f4f00f1SPoul-Henning Kamp 	if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
3936f4f00f1SPoul-Henning Kamp 		sc->opencount = 1;
3946f4f00f1SPoul-Henning Kamp 	} else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
3956f4f00f1SPoul-Henning Kamp 		sc->opencount = 0;
3966f4f00f1SPoul-Henning Kamp 	}
3976f4f00f1SPoul-Henning Kamp 	return (0);
3986f4f00f1SPoul-Henning Kamp }
3996f4f00f1SPoul-Henning Kamp 
4006f4f00f1SPoul-Henning Kamp static void
4016f4f00f1SPoul-Henning Kamp g_md_start(struct bio *bp)
4026f4f00f1SPoul-Henning Kamp {
4036f4f00f1SPoul-Henning Kamp 	struct md_s *sc;
4046f4f00f1SPoul-Henning Kamp 
4056f4f00f1SPoul-Henning Kamp 	sc = bp->bio_to->geom->softc;
406a03be42dSMaxim Sobolev 	if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE))
407a03be42dSMaxim Sobolev 		devstat_start_transaction_bio(sc->devstat, bp);
4080f8500a5SPoul-Henning Kamp 	mtx_lock(&sc->queue_mtx);
409891619a6SPoul-Henning Kamp 	bioq_disksort(&sc->bio_queue, bp);
4104b07ede4SPawel Jakub Dawidek 	mtx_unlock(&sc->queue_mtx);
411e4cdd0d4SPawel Jakub Dawidek 	wakeup(sc);
4126f4f00f1SPoul-Henning Kamp }
4136f4f00f1SPoul-Henning Kamp 
414b4a4f93cSPoul-Henning Kamp static int
415b4a4f93cSPoul-Henning Kamp mdstart_malloc(struct md_s *sc, struct bio *bp)
41600a6a3c6SPoul-Henning Kamp {
417c6517568SPoul-Henning Kamp 	int i, error;
418c6517568SPoul-Henning Kamp 	u_char *dst;
419b830359bSPawel Jakub Dawidek 	off_t secno, nsec, uc;
420c6517568SPoul-Henning Kamp 	uintptr_t sp, osp;
42100a6a3c6SPoul-Henning Kamp 
4225541f25eSPawel Jakub Dawidek 	switch (bp->bio_cmd) {
4235541f25eSPawel Jakub Dawidek 	case BIO_READ:
4245541f25eSPawel Jakub Dawidek 	case BIO_WRITE:
4255541f25eSPawel Jakub Dawidek 	case BIO_DELETE:
4265541f25eSPawel Jakub Dawidek 		break;
4275541f25eSPawel Jakub Dawidek 	default:
4285541f25eSPawel Jakub Dawidek 		return (EOPNOTSUPP);
4295541f25eSPawel Jakub Dawidek 	}
4305541f25eSPawel Jakub Dawidek 
431b830359bSPawel Jakub Dawidek 	nsec = bp->bio_length / sc->sectorsize;
432b830359bSPawel Jakub Dawidek 	secno = bp->bio_offset / sc->sectorsize;
4338177437dSPoul-Henning Kamp 	dst = bp->bio_data;
434c6517568SPoul-Henning Kamp 	error = 0;
43500a6a3c6SPoul-Henning Kamp 	while (nsec--) {
436fde2a2e4SPoul-Henning Kamp 		osp = s_read(sc->indir, secno);
4378177437dSPoul-Henning Kamp 		if (bp->bio_cmd == BIO_DELETE) {
438fde2a2e4SPoul-Henning Kamp 			if (osp != 0)
439fde2a2e4SPoul-Henning Kamp 				error = s_write(sc->indir, secno, 0);
4408177437dSPoul-Henning Kamp 		} else if (bp->bio_cmd == BIO_READ) {
441fde2a2e4SPoul-Henning Kamp 			if (osp == 0)
442b830359bSPawel Jakub Dawidek 				bzero(dst, sc->sectorsize);
443fde2a2e4SPoul-Henning Kamp 			else if (osp <= 255)
444dbb95048SMarcel Moolenaar 				memset(dst, osp, sc->sectorsize);
445dbb95048SMarcel Moolenaar 			else {
446b830359bSPawel Jakub Dawidek 				bcopy((void *)osp, dst, sc->sectorsize);
447dbb95048SMarcel Moolenaar 				cpu_flush_dcache(dst, sc->sectorsize);
448dbb95048SMarcel Moolenaar 			}
449fde2a2e4SPoul-Henning Kamp 			osp = 0;
450c6517568SPoul-Henning Kamp 		} else if (bp->bio_cmd == BIO_WRITE) {
4518f8def9eSPoul-Henning Kamp 			if (sc->flags & MD_COMPRESS) {
45200a6a3c6SPoul-Henning Kamp 				uc = dst[0];
453b830359bSPawel Jakub Dawidek 				for (i = 1; i < sc->sectorsize; i++)
45400a6a3c6SPoul-Henning Kamp 					if (dst[i] != uc)
45500a6a3c6SPoul-Henning Kamp 						break;
4568f8def9eSPoul-Henning Kamp 			} else {
4578f8def9eSPoul-Henning Kamp 				i = 0;
4588f8def9eSPoul-Henning Kamp 				uc = 0;
4598f8def9eSPoul-Henning Kamp 			}
460b830359bSPawel Jakub Dawidek 			if (i == sc->sectorsize) {
461fde2a2e4SPoul-Henning Kamp 				if (osp != uc)
462fde2a2e4SPoul-Henning Kamp 					error = s_write(sc->indir, secno, uc);
46300a6a3c6SPoul-Henning Kamp 			} else {
464fde2a2e4SPoul-Henning Kamp 				if (osp <= 255) {
465b830359bSPawel Jakub Dawidek 					sp = (uintptr_t)uma_zalloc(sc->uma,
466c44d423eSKonstantin Belousov 					    md_malloc_wait ? M_WAITOK :
467b830359bSPawel Jakub Dawidek 					    M_NOWAIT);
468c6517568SPoul-Henning Kamp 					if (sp == 0) {
469fde2a2e4SPoul-Henning Kamp 						error = ENOSPC;
470fde2a2e4SPoul-Henning Kamp 						break;
471fde2a2e4SPoul-Henning Kamp 					}
472b830359bSPawel Jakub Dawidek 					bcopy(dst, (void *)sp, sc->sectorsize);
473fde2a2e4SPoul-Henning Kamp 					error = s_write(sc->indir, secno, sp);
474c6517568SPoul-Henning Kamp 				} else {
475b830359bSPawel Jakub Dawidek 					bcopy(dst, (void *)osp, sc->sectorsize);
476fde2a2e4SPoul-Henning Kamp 					osp = 0;
47700a6a3c6SPoul-Henning Kamp 				}
47800a6a3c6SPoul-Henning Kamp 			}
479c6517568SPoul-Henning Kamp 		} else {
480c6517568SPoul-Henning Kamp 			error = EOPNOTSUPP;
481c6517568SPoul-Henning Kamp 		}
482c6517568SPoul-Henning Kamp 		if (osp > 255)
483f43b2bacSPoul-Henning Kamp 			uma_zfree(sc->uma, (void*)osp);
484e3ed29a7SPawel Jakub Dawidek 		if (error != 0)
485c6517568SPoul-Henning Kamp 			break;
48600a6a3c6SPoul-Henning Kamp 		secno++;
487b830359bSPawel Jakub Dawidek 		dst += sc->sectorsize;
48800a6a3c6SPoul-Henning Kamp 	}
4898177437dSPoul-Henning Kamp 	bp->bio_resid = 0;
490c6517568SPoul-Henning Kamp 	return (error);
49100a6a3c6SPoul-Henning Kamp }
49200a6a3c6SPoul-Henning Kamp 
493b4a4f93cSPoul-Henning Kamp static int
494b4a4f93cSPoul-Henning Kamp mdstart_preload(struct md_s *sc, struct bio *bp)
49571e4fff8SPoul-Henning Kamp {
49671e4fff8SPoul-Henning Kamp 
497a8a58d03SPawel Jakub Dawidek 	switch (bp->bio_cmd) {
498a8a58d03SPawel Jakub Dawidek 	case BIO_READ:
499a8a58d03SPawel Jakub Dawidek 		bcopy(sc->pl_ptr + bp->bio_offset, bp->bio_data,
500a8a58d03SPawel Jakub Dawidek 		    bp->bio_length);
501dbb95048SMarcel Moolenaar 		cpu_flush_dcache(bp->bio_data, bp->bio_length);
502a8a58d03SPawel Jakub Dawidek 		break;
503a8a58d03SPawel Jakub Dawidek 	case BIO_WRITE:
504a8a58d03SPawel Jakub Dawidek 		bcopy(bp->bio_data, sc->pl_ptr + bp->bio_offset,
505a8a58d03SPawel Jakub Dawidek 		    bp->bio_length);
506a8a58d03SPawel Jakub Dawidek 		break;
50771e4fff8SPoul-Henning Kamp 	}
5088177437dSPoul-Henning Kamp 	bp->bio_resid = 0;
509b4a4f93cSPoul-Henning Kamp 	return (0);
51071e4fff8SPoul-Henning Kamp }
51171e4fff8SPoul-Henning Kamp 
512b4a4f93cSPoul-Henning Kamp static int
513b4a4f93cSPoul-Henning Kamp mdstart_vnode(struct md_s *sc, struct bio *bp)
5148f8def9eSPoul-Henning Kamp {
515*5050aa86SKonstantin Belousov 	int error;
5168f8def9eSPoul-Henning Kamp 	struct uio auio;
5178f8def9eSPoul-Henning Kamp 	struct iovec aiov;
5188f8def9eSPoul-Henning Kamp 	struct mount *mp;
5195541f25eSPawel Jakub Dawidek 	struct vnode *vp;
5205541f25eSPawel Jakub Dawidek 	struct thread *td;
5210abd21bdSDag-Erling Smørgrav 	off_t end, zerosize;
5225541f25eSPawel Jakub Dawidek 
5235541f25eSPawel Jakub Dawidek 	switch (bp->bio_cmd) {
5245541f25eSPawel Jakub Dawidek 	case BIO_READ:
5255541f25eSPawel Jakub Dawidek 	case BIO_WRITE:
5260abd21bdSDag-Erling Smørgrav 	case BIO_DELETE:
5275541f25eSPawel Jakub Dawidek 	case BIO_FLUSH:
5285541f25eSPawel Jakub Dawidek 		break;
5295541f25eSPawel Jakub Dawidek 	default:
5305541f25eSPawel Jakub Dawidek 		return (EOPNOTSUPP);
5315541f25eSPawel Jakub Dawidek 	}
5325541f25eSPawel Jakub Dawidek 
5335541f25eSPawel Jakub Dawidek 	td = curthread;
5345541f25eSPawel Jakub Dawidek 	vp = sc->vnode;
5358f8def9eSPoul-Henning Kamp 
5368f8def9eSPoul-Henning Kamp 	/*
5378f8def9eSPoul-Henning Kamp 	 * VNODE I/O
5388f8def9eSPoul-Henning Kamp 	 *
5398f8def9eSPoul-Henning Kamp 	 * If an error occurs, we set BIO_ERROR but we do not set
5408f8def9eSPoul-Henning Kamp 	 * B_INVAL because (for a write anyway), the buffer is
5418f8def9eSPoul-Henning Kamp 	 * still valid.
5428f8def9eSPoul-Henning Kamp 	 */
5438f8def9eSPoul-Henning Kamp 
5445541f25eSPawel Jakub Dawidek 	if (bp->bio_cmd == BIO_FLUSH) {
5455541f25eSPawel Jakub Dawidek 		(void) vn_start_write(vp, &mp, V_WAIT);
546cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5475541f25eSPawel Jakub Dawidek 		error = VOP_FSYNC(vp, MNT_WAIT, td);
54822db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
5495541f25eSPawel Jakub Dawidek 		vn_finished_write(mp);
5505541f25eSPawel Jakub Dawidek 		return (error);
5515541f25eSPawel Jakub Dawidek 	}
5525541f25eSPawel Jakub Dawidek 
5538f8def9eSPoul-Henning Kamp 	bzero(&auio, sizeof(auio));
5548f8def9eSPoul-Henning Kamp 
5550abd21bdSDag-Erling Smørgrav 	/*
5560abd21bdSDag-Erling Smørgrav 	 * Special case for BIO_DELETE.  On the surface, this is very
5570abd21bdSDag-Erling Smørgrav 	 * similar to BIO_WRITE, except that we write from our own
5580abd21bdSDag-Erling Smørgrav 	 * fixed-length buffer, so we have to loop.  The net result is
5590abd21bdSDag-Erling Smørgrav 	 * that the two cases end up having very little in common.
5600abd21bdSDag-Erling Smørgrav 	 */
5610abd21bdSDag-Erling Smørgrav 	if (bp->bio_cmd == BIO_DELETE) {
56289cb2a19SMatthew D Fleming 		zerosize = ZERO_REGION_SIZE -
56389cb2a19SMatthew D Fleming 		    (ZERO_REGION_SIZE % sc->sectorsize);
5640abd21bdSDag-Erling Smørgrav 		auio.uio_iov = &aiov;
5650abd21bdSDag-Erling Smørgrav 		auio.uio_iovcnt = 1;
5660abd21bdSDag-Erling Smørgrav 		auio.uio_offset = (vm_ooffset_t)bp->bio_offset;
5670abd21bdSDag-Erling Smørgrav 		auio.uio_segflg = UIO_SYSSPACE;
5680abd21bdSDag-Erling Smørgrav 		auio.uio_rw = UIO_WRITE;
5690abd21bdSDag-Erling Smørgrav 		auio.uio_td = td;
5700abd21bdSDag-Erling Smørgrav 		end = bp->bio_offset + bp->bio_length;
5710abd21bdSDag-Erling Smørgrav 		(void) vn_start_write(vp, &mp, V_WAIT);
5720abd21bdSDag-Erling Smørgrav 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5730abd21bdSDag-Erling Smørgrav 		error = 0;
5740abd21bdSDag-Erling Smørgrav 		while (auio.uio_offset < end) {
57589cb2a19SMatthew D Fleming 			aiov.iov_base = __DECONST(void *, zero_region);
5760abd21bdSDag-Erling Smørgrav 			aiov.iov_len = end - auio.uio_offset;
5770abd21bdSDag-Erling Smørgrav 			if (aiov.iov_len > zerosize)
5780abd21bdSDag-Erling Smørgrav 				aiov.iov_len = zerosize;
5790abd21bdSDag-Erling Smørgrav 			auio.uio_resid = aiov.iov_len;
5800abd21bdSDag-Erling Smørgrav 			error = VOP_WRITE(vp, &auio,
5810abd21bdSDag-Erling Smørgrav 			    sc->flags & MD_ASYNC ? 0 : IO_SYNC, sc->cred);
5820abd21bdSDag-Erling Smørgrav 			if (error != 0)
5830abd21bdSDag-Erling Smørgrav 				break;
5840abd21bdSDag-Erling Smørgrav 		}
5850abd21bdSDag-Erling Smørgrav 		VOP_UNLOCK(vp, 0);
5860abd21bdSDag-Erling Smørgrav 		vn_finished_write(mp);
5870abd21bdSDag-Erling Smørgrav 		bp->bio_resid = end - auio.uio_offset;
5880abd21bdSDag-Erling Smørgrav 		return (error);
5890abd21bdSDag-Erling Smørgrav 	}
5900abd21bdSDag-Erling Smørgrav 
5918f8def9eSPoul-Henning Kamp 	aiov.iov_base = bp->bio_data;
592a8a58d03SPawel Jakub Dawidek 	aiov.iov_len = bp->bio_length;
5938f8def9eSPoul-Henning Kamp 	auio.uio_iov = &aiov;
5948f8def9eSPoul-Henning Kamp 	auio.uio_iovcnt = 1;
595a8a58d03SPawel Jakub Dawidek 	auio.uio_offset = (vm_ooffset_t)bp->bio_offset;
5968f8def9eSPoul-Henning Kamp 	auio.uio_segflg = UIO_SYSSPACE;
5978f8def9eSPoul-Henning Kamp 	if (bp->bio_cmd == BIO_READ)
5988f8def9eSPoul-Henning Kamp 		auio.uio_rw = UIO_READ;
5998e28326aSPoul-Henning Kamp 	else if (bp->bio_cmd == BIO_WRITE)
6008f8def9eSPoul-Henning Kamp 		auio.uio_rw = UIO_WRITE;
6018e28326aSPoul-Henning Kamp 	else
6028e28326aSPoul-Henning Kamp 		panic("wrong BIO_OP in mdstart_vnode");
603a8a58d03SPawel Jakub Dawidek 	auio.uio_resid = bp->bio_length;
6045541f25eSPawel Jakub Dawidek 	auio.uio_td = td;
6057e76bb56SMatthew Dillon 	/*
6067e76bb56SMatthew Dillon 	 * When reading set IO_DIRECT to try to avoid double-caching
60717a13919SPoul-Henning Kamp 	 * the data.  When writing IO_DIRECT is not optimal.
6087e76bb56SMatthew Dillon 	 */
6098f8def9eSPoul-Henning Kamp 	if (bp->bio_cmd == BIO_READ) {
610cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
6115541f25eSPawel Jakub Dawidek 		error = VOP_READ(vp, &auio, IO_DIRECT, sc->cred);
61222db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
6138f8def9eSPoul-Henning Kamp 	} else {
6145541f25eSPawel Jakub Dawidek 		(void) vn_start_write(vp, &mp, V_WAIT);
615cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
6165541f25eSPawel Jakub Dawidek 		error = VOP_WRITE(vp, &auio, sc->flags & MD_ASYNC ? 0 : IO_SYNC,
6175541f25eSPawel Jakub Dawidek 		    sc->cred);
61822db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
6198f8def9eSPoul-Henning Kamp 		vn_finished_write(mp);
6208f8def9eSPoul-Henning Kamp 	}
6218f8def9eSPoul-Henning Kamp 	bp->bio_resid = auio.uio_resid;
622b4a4f93cSPoul-Henning Kamp 	return (error);
6238f8def9eSPoul-Henning Kamp }
6248f8def9eSPoul-Henning Kamp 
625b4a4f93cSPoul-Henning Kamp static int
626b4a4f93cSPoul-Henning Kamp mdstart_swap(struct md_s *sc, struct bio *bp)
6278f8def9eSPoul-Henning Kamp {
6287cd53fddSAlan Cox 	struct sf_buf *sf;
6296ab0a0aeSPawel Jakub Dawidek 	int rv, offs, len, lastend;
6306ab0a0aeSPawel Jakub Dawidek 	vm_pindex_t i, lastp;
6318e28326aSPoul-Henning Kamp 	vm_page_t m;
6328e28326aSPoul-Henning Kamp 	u_char *p;
6338f8def9eSPoul-Henning Kamp 
6345541f25eSPawel Jakub Dawidek 	switch (bp->bio_cmd) {
6355541f25eSPawel Jakub Dawidek 	case BIO_READ:
6365541f25eSPawel Jakub Dawidek 	case BIO_WRITE:
6375541f25eSPawel Jakub Dawidek 	case BIO_DELETE:
6385541f25eSPawel Jakub Dawidek 		break;
6395541f25eSPawel Jakub Dawidek 	default:
6405541f25eSPawel Jakub Dawidek 		return (EOPNOTSUPP);
6415541f25eSPawel Jakub Dawidek 	}
6425541f25eSPawel Jakub Dawidek 
6438e28326aSPoul-Henning Kamp 	p = bp->bio_data;
644e07113d6SColin Percival 
645e07113d6SColin Percival 	/*
6466c3cd0e2SMaxim Konovalov 	 * offs is the offset at which to start operating on the
647e07113d6SColin Percival 	 * next (ie, first) page.  lastp is the last page on
648e07113d6SColin Percival 	 * which we're going to operate.  lastend is the ending
649e07113d6SColin Percival 	 * position within that last page (ie, PAGE_SIZE if
650e07113d6SColin Percival 	 * we're operating on complete aligned pages).
651e07113d6SColin Percival 	 */
652e07113d6SColin Percival 	offs = bp->bio_offset % PAGE_SIZE;
653e07113d6SColin Percival 	lastp = (bp->bio_offset + bp->bio_length - 1) / PAGE_SIZE;
654e07113d6SColin Percival 	lastend = (bp->bio_offset + bp->bio_length - 1) % PAGE_SIZE + 1;
655e07113d6SColin Percival 
656812851b6SBrian Feldman 	rv = VM_PAGER_OK;
6578e28326aSPoul-Henning Kamp 	VM_OBJECT_LOCK(sc->object);
6588e28326aSPoul-Henning Kamp 	vm_object_pip_add(sc->object, 1);
659e07113d6SColin Percival 	for (i = bp->bio_offset / PAGE_SIZE; i <= lastp; i++) {
660e07113d6SColin Percival 		len = ((i == lastp) ? lastend : PAGE_SIZE) - offs;
661e07113d6SColin Percival 
662e07113d6SColin Percival 		m = vm_page_grab(sc->object, i,
6638e28326aSPoul-Henning Kamp 		    VM_ALLOC_NORMAL|VM_ALLOC_RETRY);
6647cd53fddSAlan Cox 		VM_OBJECT_UNLOCK(sc->object);
665e340fc60SAlan Cox 		sched_pin();
666e340fc60SAlan Cox 		sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
6677cd53fddSAlan Cox 		VM_OBJECT_LOCK(sc->object);
6688e28326aSPoul-Henning Kamp 		if (bp->bio_cmd == BIO_READ) {
66907be617fSAlan Cox 			if (m->valid != VM_PAGE_BITS_ALL)
67007be617fSAlan Cox 				rv = vm_pager_get_pages(sc->object, &m, 1, 0);
671812851b6SBrian Feldman 			if (rv == VM_PAGER_ERROR) {
672812851b6SBrian Feldman 				sf_buf_free(sf);
673e340fc60SAlan Cox 				sched_unpin();
674812851b6SBrian Feldman 				vm_page_wakeup(m);
675812851b6SBrian Feldman 				break;
676812851b6SBrian Feldman 			}
6777cd53fddSAlan Cox 			bcopy((void *)(sf_buf_kva(sf) + offs), p, len);
678dbb95048SMarcel Moolenaar 			cpu_flush_dcache(p, len);
6798e28326aSPoul-Henning Kamp 		} else if (bp->bio_cmd == BIO_WRITE) {
68007be617fSAlan Cox 			if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL)
68107be617fSAlan Cox 				rv = vm_pager_get_pages(sc->object, &m, 1, 0);
682812851b6SBrian Feldman 			if (rv == VM_PAGER_ERROR) {
683812851b6SBrian Feldman 				sf_buf_free(sf);
684e340fc60SAlan Cox 				sched_unpin();
685812851b6SBrian Feldman 				vm_page_wakeup(m);
686812851b6SBrian Feldman 				break;
687812851b6SBrian Feldman 			}
6887cd53fddSAlan Cox 			bcopy(p, (void *)(sf_buf_kva(sf) + offs), len);
6898e28326aSPoul-Henning Kamp 			m->valid = VM_PAGE_BITS_ALL;
6908e28326aSPoul-Henning Kamp 		} else if (bp->bio_cmd == BIO_DELETE) {
69107be617fSAlan Cox 			if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL)
69207be617fSAlan Cox 				rv = vm_pager_get_pages(sc->object, &m, 1, 0);
693812851b6SBrian Feldman 			if (rv == VM_PAGER_ERROR) {
694812851b6SBrian Feldman 				sf_buf_free(sf);
695e340fc60SAlan Cox 				sched_unpin();
696812851b6SBrian Feldman 				vm_page_wakeup(m);
697812851b6SBrian Feldman 				break;
698812851b6SBrian Feldman 			}
6994a13a769SKonstantin Belousov 			if (len != PAGE_SIZE) {
7007cd53fddSAlan Cox 				bzero((void *)(sf_buf_kva(sf) + offs), len);
7014a13a769SKonstantin Belousov 				vm_page_clear_dirty(m, offs, len);
7028e28326aSPoul-Henning Kamp 				m->valid = VM_PAGE_BITS_ALL;
7034a13a769SKonstantin Belousov 			} else
7044a13a769SKonstantin Belousov 				vm_pager_page_unswapped(m);
7058e28326aSPoul-Henning Kamp 		}
7067cd53fddSAlan Cox 		sf_buf_free(sf);
707e340fc60SAlan Cox 		sched_unpin();
7088e28326aSPoul-Henning Kamp 		vm_page_wakeup(m);
709fc0c3802SKonstantin Belousov 		vm_page_lock(m);
7104a13a769SKonstantin Belousov 		if (bp->bio_cmd == BIO_DELETE && len == PAGE_SIZE)
7114a13a769SKonstantin Belousov 			vm_page_free(m);
7124a13a769SKonstantin Belousov 		else
7138e28326aSPoul-Henning Kamp 			vm_page_activate(m);
714ecd5dd95SAlan Cox 		vm_page_unlock(m);
71507be617fSAlan Cox 		if (bp->bio_cmd == BIO_WRITE)
7168e28326aSPoul-Henning Kamp 			vm_page_dirty(m);
717e07113d6SColin Percival 
718e07113d6SColin Percival 		/* Actions on further pages start at offset 0 */
719e07113d6SColin Percival 		p += PAGE_SIZE - offs;
720e07113d6SColin Percival 		offs = 0;
7218e28326aSPoul-Henning Kamp 	}
7228e28326aSPoul-Henning Kamp 	vm_object_pip_subtract(sc->object, 1);
7238e28326aSPoul-Henning Kamp 	VM_OBJECT_UNLOCK(sc->object);
724812851b6SBrian Feldman 	return (rv != VM_PAGER_ERROR ? 0 : ENOSPC);
7258e28326aSPoul-Henning Kamp }
7268f8def9eSPoul-Henning Kamp 
7278f8def9eSPoul-Henning Kamp static void
7285c97ca54SIan Dowse md_kthread(void *arg)
7295c97ca54SIan Dowse {
7305c97ca54SIan Dowse 	struct md_s *sc;
7315c97ca54SIan Dowse 	struct bio *bp;
732a08d2e7fSJohn Baldwin 	int error;
7335c97ca54SIan Dowse 
7345c97ca54SIan Dowse 	sc = arg;
735982d11f8SJeff Roberson 	thread_lock(curthread);
73663710c4dSJohn Baldwin 	sched_prio(curthread, PRIBIO);
737982d11f8SJeff Roberson 	thread_unlock(curthread);
7383b7b5496SKonstantin Belousov 	if (sc->type == MD_VNODE)
7393b7b5496SKonstantin Belousov 		curthread->td_pflags |= TDP_NORUNNINGBUF;
7405c97ca54SIan Dowse 
741b4a4f93cSPoul-Henning Kamp 	for (;;) {
742a08d2e7fSJohn Baldwin 		mtx_lock(&sc->queue_mtx);
7435c97ca54SIan Dowse 		if (sc->flags & MD_SHUTDOWN) {
744a08d2e7fSJohn Baldwin 			sc->flags |= MD_EXITING;
745a08d2e7fSJohn Baldwin 			mtx_unlock(&sc->queue_mtx);
7463745c395SJulian Elischer 			kproc_exit(0);
7475c97ca54SIan Dowse 		}
7489b00ca19SPoul-Henning Kamp 		bp = bioq_takefirst(&sc->bio_queue);
7499b00ca19SPoul-Henning Kamp 		if (!bp) {
7500f8500a5SPoul-Henning Kamp 			msleep(sc, &sc->queue_mtx, PRIBIO | PDROP, "mdwait", 0);
7515c97ca54SIan Dowse 			continue;
7525c97ca54SIan Dowse 		}
7530f8500a5SPoul-Henning Kamp 		mtx_unlock(&sc->queue_mtx);
7544e8bfe14SPoul-Henning Kamp 		if (bp->bio_cmd == BIO_GETATTR) {
755d91e813cSKonstantin Belousov 			if ((sc->fwsectors && sc->fwheads &&
7564e8bfe14SPoul-Henning Kamp 			    (g_handleattr_int(bp, "GEOM::fwsectors",
7574e8bfe14SPoul-Henning Kamp 			    sc->fwsectors) ||
7584e8bfe14SPoul-Henning Kamp 			    g_handleattr_int(bp, "GEOM::fwheads",
759d91e813cSKonstantin Belousov 			    sc->fwheads))) ||
760d91e813cSKonstantin Belousov 			    g_handleattr_int(bp, "GEOM::candelete", 1))
7614e8bfe14SPoul-Henning Kamp 				error = -1;
7624e8bfe14SPoul-Henning Kamp 			else
7634e8bfe14SPoul-Henning Kamp 				error = EOPNOTSUPP;
7644e8bfe14SPoul-Henning Kamp 		} else {
7659b00ca19SPoul-Henning Kamp 			error = sc->start(sc, bp);
7664e8bfe14SPoul-Henning Kamp 		}
767b4a4f93cSPoul-Henning Kamp 
7686f4f00f1SPoul-Henning Kamp 		if (error != -1) {
7696f4f00f1SPoul-Henning Kamp 			bp->bio_completed = bp->bio_length;
770a03be42dSMaxim Sobolev 			if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE))
771a03be42dSMaxim Sobolev 				devstat_end_transaction_bio(sc->devstat, bp);
77296410b95SKonstantin Belousov 			g_io_deliver(bp, error);
773b4a4f93cSPoul-Henning Kamp 		}
7748f8def9eSPoul-Henning Kamp 	}
77526d48b40SPoul-Henning Kamp }
7768f8def9eSPoul-Henning Kamp 
7778f8def9eSPoul-Henning Kamp static struct md_s *
7788f8def9eSPoul-Henning Kamp mdfind(int unit)
7798f8def9eSPoul-Henning Kamp {
7808f8def9eSPoul-Henning Kamp 	struct md_s *sc;
7818f8def9eSPoul-Henning Kamp 
7823f54a085SPoul-Henning Kamp 	LIST_FOREACH(sc, &md_softc_list, list) {
7833f54a085SPoul-Henning Kamp 		if (sc->unit == unit)
7848f8def9eSPoul-Henning Kamp 			break;
7858f8def9eSPoul-Henning Kamp 	}
7868f8def9eSPoul-Henning Kamp 	return (sc);
7878f8def9eSPoul-Henning Kamp }
7888f8def9eSPoul-Henning Kamp 
7898f8def9eSPoul-Henning Kamp static struct md_s *
790947fc8deSPoul-Henning Kamp mdnew(int unit, int *errp, enum md_types type)
7918f8def9eSPoul-Henning Kamp {
792f4e7c5a8SJaakko Heinonen 	struct md_s *sc;
793f4e7c5a8SJaakko Heinonen 	int error;
7948f8def9eSPoul-Henning Kamp 
7959b00ca19SPoul-Henning Kamp 	*errp = 0;
796f4e7c5a8SJaakko Heinonen 	if (unit == -1)
797f4e7c5a8SJaakko Heinonen 		unit = alloc_unr(md_uh);
798f4e7c5a8SJaakko Heinonen 	else
799f4e7c5a8SJaakko Heinonen 		unit = alloc_unr_specific(md_uh, unit);
800f4e7c5a8SJaakko Heinonen 
801f4e7c5a8SJaakko Heinonen 	if (unit == -1) {
8027ee3c044SPawel Jakub Dawidek 		*errp = EBUSY;
8033f54a085SPoul-Henning Kamp 		return (NULL);
8043f54a085SPoul-Henning Kamp 	}
805f4e7c5a8SJaakko Heinonen 
8069b00ca19SPoul-Henning Kamp 	sc = (struct md_s *)malloc(sizeof *sc, M_MD, M_WAITOK | M_ZERO);
807947fc8deSPoul-Henning Kamp 	sc->type = type;
8089b00ca19SPoul-Henning Kamp 	bioq_init(&sc->bio_queue);
8099b00ca19SPoul-Henning Kamp 	mtx_init(&sc->queue_mtx, "md bio queue", NULL, MTX_DEF);
8103f54a085SPoul-Henning Kamp 	sc->unit = unit;
811f43b2bacSPoul-Henning Kamp 	sprintf(sc->name, "md%d", unit);
8127ee3c044SPawel Jakub Dawidek 	LIST_INSERT_HEAD(&md_softc_list, sc, list);
8133745c395SJulian Elischer 	error = kproc_create(md_kthread, sc, &sc->procp, 0, 0,"%s", sc->name);
8149b00ca19SPoul-Henning Kamp 	if (error == 0)
8159b00ca19SPoul-Henning Kamp 		return (sc);
8167ee3c044SPawel Jakub Dawidek 	LIST_REMOVE(sc, list);
8177ee3c044SPawel Jakub Dawidek 	mtx_destroy(&sc->queue_mtx);
818f4e7c5a8SJaakko Heinonen 	free_unr(md_uh, sc->unit);
8195c97ca54SIan Dowse 	free(sc, M_MD);
8207ee3c044SPawel Jakub Dawidek 	*errp = error;
8215c97ca54SIan Dowse 	return (NULL);
8225c97ca54SIan Dowse }
8238f8def9eSPoul-Henning Kamp 
8248f8def9eSPoul-Henning Kamp static void
8258f8def9eSPoul-Henning Kamp mdinit(struct md_s *sc)
8268f8def9eSPoul-Henning Kamp {
8276f4f00f1SPoul-Henning Kamp 	struct g_geom *gp;
8286f4f00f1SPoul-Henning Kamp 	struct g_provider *pp;
8296f4f00f1SPoul-Henning Kamp 
8306f4f00f1SPoul-Henning Kamp 	g_topology_lock();
8316f4f00f1SPoul-Henning Kamp 	gp = g_new_geomf(&g_md_class, "md%d", sc->unit);
8326f4f00f1SPoul-Henning Kamp 	gp->softc = sc;
8336f4f00f1SPoul-Henning Kamp 	pp = g_new_providerf(gp, "md%d", sc->unit);
834b830359bSPawel Jakub Dawidek 	pp->mediasize = sc->mediasize;
835b830359bSPawel Jakub Dawidek 	pp->sectorsize = sc->sectorsize;
8366f4f00f1SPoul-Henning Kamp 	sc->gp = gp;
8376f4f00f1SPoul-Henning Kamp 	sc->pp = pp;
8386f4f00f1SPoul-Henning Kamp 	g_error_provider(pp, 0);
8396f4f00f1SPoul-Henning Kamp 	g_topology_unlock();
840a03be42dSMaxim Sobolev 	sc->devstat = devstat_new_entry("md", sc->unit, sc->sectorsize,
841a03be42dSMaxim Sobolev 	    DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
8426f4f00f1SPoul-Henning Kamp }
84371e4fff8SPoul-Henning Kamp 
84496b6a55fSPoul-Henning Kamp /*
84596b6a55fSPoul-Henning Kamp  * XXX: we should check that the range they feed us is mapped.
84696b6a55fSPoul-Henning Kamp  * XXX: we should implement read-only.
84796b6a55fSPoul-Henning Kamp  */
84896b6a55fSPoul-Henning Kamp 
849637f671aSPoul-Henning Kamp static int
850b830359bSPawel Jakub Dawidek mdcreate_preload(struct md_s *sc, struct md_ioctl *mdio)
85171e4fff8SPoul-Henning Kamp {
85271e4fff8SPoul-Henning Kamp 
853b830359bSPawel Jakub Dawidek 	if (mdio->md_options & ~(MD_AUTOUNIT | MD_FORCE))
854637f671aSPoul-Henning Kamp 		return (EINVAL);
8555ed1eb2bSEdward Tomasz Napierala 	if (mdio->md_base == 0)
8565ed1eb2bSEdward Tomasz Napierala 		return (EINVAL);
85726a0ee75SDima Dorfman 	sc->flags = mdio->md_options & MD_FORCE;
85896b6a55fSPoul-Henning Kamp 	/* Cast to pointer size, then to pointer to avoid warning */
859dc57d7c6SPeter Wemm 	sc->pl_ptr = (u_char *)(uintptr_t)mdio->md_base;
860b830359bSPawel Jakub Dawidek 	sc->pl_len = (size_t)sc->mediasize;
861637f671aSPoul-Henning Kamp 	return (0);
86295f1a897SPoul-Henning Kamp }
86395f1a897SPoul-Henning Kamp 
864637f671aSPoul-Henning Kamp 
8658f8def9eSPoul-Henning Kamp static int
866b830359bSPawel Jakub Dawidek mdcreate_malloc(struct md_s *sc, struct md_ioctl *mdio)
86795f1a897SPoul-Henning Kamp {
868c6517568SPoul-Henning Kamp 	uintptr_t sp;
869c6517568SPoul-Henning Kamp 	int error;
870b830359bSPawel Jakub Dawidek 	off_t u;
87195f1a897SPoul-Henning Kamp 
872c6517568SPoul-Henning Kamp 	error = 0;
8738f8def9eSPoul-Henning Kamp 	if (mdio->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE))
8748f8def9eSPoul-Henning Kamp 		return (EINVAL);
875b830359bSPawel Jakub Dawidek 	if (mdio->md_sectorsize != 0 && !powerof2(mdio->md_sectorsize))
876ebe789d6SPoul-Henning Kamp 		return (EINVAL);
8778f8def9eSPoul-Henning Kamp 	/* Compression doesn't make sense if we have reserved space */
8788f8def9eSPoul-Henning Kamp 	if (mdio->md_options & MD_RESERVE)
8798f8def9eSPoul-Henning Kamp 		mdio->md_options &= ~MD_COMPRESS;
8804e8bfe14SPoul-Henning Kamp 	if (mdio->md_fwsectors != 0)
8814e8bfe14SPoul-Henning Kamp 		sc->fwsectors = mdio->md_fwsectors;
8824e8bfe14SPoul-Henning Kamp 	if (mdio->md_fwheads != 0)
8834e8bfe14SPoul-Henning Kamp 		sc->fwheads = mdio->md_fwheads;
88426a0ee75SDima Dorfman 	sc->flags = mdio->md_options & (MD_COMPRESS | MD_FORCE);
885b830359bSPawel Jakub Dawidek 	sc->indir = dimension(sc->mediasize / sc->sectorsize);
886b830359bSPawel Jakub Dawidek 	sc->uma = uma_zcreate(sc->name, sc->sectorsize, NULL, NULL, NULL, NULL,
887b830359bSPawel Jakub Dawidek 	    0x1ff, 0);
88896b6a55fSPoul-Henning Kamp 	if (mdio->md_options & MD_RESERVE) {
889b830359bSPawel Jakub Dawidek 		off_t nsectors;
890b830359bSPawel Jakub Dawidek 
891b830359bSPawel Jakub Dawidek 		nsectors = sc->mediasize / sc->sectorsize;
892b830359bSPawel Jakub Dawidek 		for (u = 0; u < nsectors; u++) {
893007777f1SKonstantin Belousov 			sp = (uintptr_t)uma_zalloc(sc->uma, (md_malloc_wait ?
894007777f1SKonstantin Belousov 			    M_WAITOK : M_NOWAIT) | M_ZERO);
895c6517568SPoul-Henning Kamp 			if (sp != 0)
896fde2a2e4SPoul-Henning Kamp 				error = s_write(sc->indir, u, sp);
897c6517568SPoul-Henning Kamp 			else
898c6517568SPoul-Henning Kamp 				error = ENOMEM;
899b830359bSPawel Jakub Dawidek 			if (error != 0)
900c6517568SPoul-Henning Kamp 				break;
9018f8def9eSPoul-Henning Kamp 		}
902c6517568SPoul-Henning Kamp 	}
903c6517568SPoul-Henning Kamp 	return (error);
90400a6a3c6SPoul-Henning Kamp }
90500a6a3c6SPoul-Henning Kamp 
9063f54a085SPoul-Henning Kamp 
9078f8def9eSPoul-Henning Kamp static int
9088f8def9eSPoul-Henning Kamp mdsetcred(struct md_s *sc, struct ucred *cred)
9098f8def9eSPoul-Henning Kamp {
9108f8def9eSPoul-Henning Kamp 	char *tmpbuf;
9118f8def9eSPoul-Henning Kamp 	int error = 0;
9128f8def9eSPoul-Henning Kamp 
9133f54a085SPoul-Henning Kamp 	/*
9148f8def9eSPoul-Henning Kamp 	 * Set credits in our softc
9153f54a085SPoul-Henning Kamp 	 */
9168f8def9eSPoul-Henning Kamp 
9178f8def9eSPoul-Henning Kamp 	if (sc->cred)
9188f8def9eSPoul-Henning Kamp 		crfree(sc->cred);
919bd78ceceSJohn Baldwin 	sc->cred = crhold(cred);
9208f8def9eSPoul-Henning Kamp 
9218f8def9eSPoul-Henning Kamp 	/*
9228f8def9eSPoul-Henning Kamp 	 * Horrible kludge to establish credentials for NFS  XXX.
9238f8def9eSPoul-Henning Kamp 	 */
9248f8def9eSPoul-Henning Kamp 
9258f8def9eSPoul-Henning Kamp 	if (sc->vnode) {
9268f8def9eSPoul-Henning Kamp 		struct uio auio;
9278f8def9eSPoul-Henning Kamp 		struct iovec aiov;
9288f8def9eSPoul-Henning Kamp 
929b830359bSPawel Jakub Dawidek 		tmpbuf = malloc(sc->sectorsize, M_TEMP, M_WAITOK);
9308f8def9eSPoul-Henning Kamp 		bzero(&auio, sizeof(auio));
9318f8def9eSPoul-Henning Kamp 
9328f8def9eSPoul-Henning Kamp 		aiov.iov_base = tmpbuf;
933b830359bSPawel Jakub Dawidek 		aiov.iov_len = sc->sectorsize;
9348f8def9eSPoul-Henning Kamp 		auio.uio_iov = &aiov;
9358f8def9eSPoul-Henning Kamp 		auio.uio_iovcnt = 1;
9368f8def9eSPoul-Henning Kamp 		auio.uio_offset = 0;
9378f8def9eSPoul-Henning Kamp 		auio.uio_rw = UIO_READ;
9388f8def9eSPoul-Henning Kamp 		auio.uio_segflg = UIO_SYSSPACE;
9398f8def9eSPoul-Henning Kamp 		auio.uio_resid = aiov.iov_len;
940cb05b60aSAttilio Rao 		vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY);
9418f8def9eSPoul-Henning Kamp 		error = VOP_READ(sc->vnode, &auio, 0, sc->cred);
94222db15c0SAttilio Rao 		VOP_UNLOCK(sc->vnode, 0);
9438f8def9eSPoul-Henning Kamp 		free(tmpbuf, M_TEMP);
9448f8def9eSPoul-Henning Kamp 	}
9458f8def9eSPoul-Henning Kamp 	return (error);
9468f8def9eSPoul-Henning Kamp }
9478f8def9eSPoul-Henning Kamp 
9488f8def9eSPoul-Henning Kamp static int
949b830359bSPawel Jakub Dawidek mdcreate_vnode(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
9508f8def9eSPoul-Henning Kamp {
9518f8def9eSPoul-Henning Kamp 	struct vattr vattr;
9528f8def9eSPoul-Henning Kamp 	struct nameidata nd;
9533d5c947dSMarcel Moolenaar 	char *fname;
954*5050aa86SKonstantin Belousov 	int error, flags;
9558f8def9eSPoul-Henning Kamp 
9563d5c947dSMarcel Moolenaar 	/*
9573d5c947dSMarcel Moolenaar 	 * Kernel-originated requests must have the filename appended
9583d5c947dSMarcel Moolenaar 	 * to the mdio structure to protect against malicious software.
9593d5c947dSMarcel Moolenaar 	 */
9603d5c947dSMarcel Moolenaar 	fname = mdio->md_file;
9613d5c947dSMarcel Moolenaar 	if ((void *)fname != (void *)(mdio + 1)) {
9623d5c947dSMarcel Moolenaar 		error = copyinstr(fname, sc->file, sizeof(sc->file), NULL);
96388b5b78dSPawel Jakub Dawidek 		if (error != 0)
96488b5b78dSPawel Jakub Dawidek 			return (error);
9653d5c947dSMarcel Moolenaar 	} else
9663d5c947dSMarcel Moolenaar 		strlcpy(sc->file, fname, sizeof(sc->file));
9673d5c947dSMarcel Moolenaar 
96886776891SChristian S.J. Peron 	/*
9693d5c947dSMarcel Moolenaar 	 * If the user specified that this is a read only device, don't
9703d5c947dSMarcel Moolenaar 	 * set the FWRITE mask before trying to open the backing store.
97186776891SChristian S.J. Peron 	 */
9723d5c947dSMarcel Moolenaar 	flags = FREAD | ((mdio->md_options & MD_READONLY) ? 0 : FWRITE);
973*5050aa86SKonstantin Belousov 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, sc->file, td);
9749e223287SKonstantin Belousov 	error = vn_open(&nd, &flags, 0, NULL);
975e3ed29a7SPawel Jakub Dawidek 	if (error != 0)
97652c6716fSPawel Jakub Dawidek 		return (error);
977b322d85dSPawel Jakub Dawidek 	NDFREE(&nd, NDF_ONLY_PNBUF);
97833fc3625SJohn Baldwin 	if (nd.ni_vp->v_type != VREG) {
97933fc3625SJohn Baldwin 		error = EINVAL;
98033fc3625SJohn Baldwin 		goto bad;
98133fc3625SJohn Baldwin 	}
98233fc3625SJohn Baldwin 	error = VOP_GETATTR(nd.ni_vp, &vattr, td->td_ucred);
98333fc3625SJohn Baldwin 	if (error != 0)
98433fc3625SJohn Baldwin 		goto bad;
98533fc3625SJohn Baldwin 	if (VOP_ISLOCKED(nd.ni_vp) != LK_EXCLUSIVE) {
98633fc3625SJohn Baldwin 		vn_lock(nd.ni_vp, LK_UPGRADE | LK_RETRY);
98733fc3625SJohn Baldwin 		if (nd.ni_vp->v_iflag & VI_DOOMED) {
98833fc3625SJohn Baldwin 			/* Forced unmount. */
98933fc3625SJohn Baldwin 			error = EBADF;
99033fc3625SJohn Baldwin 			goto bad;
99133fc3625SJohn Baldwin 		}
9928f8def9eSPoul-Henning Kamp 	}
9933b7b5496SKonstantin Belousov 	nd.ni_vp->v_vflag |= VV_MD;
99422db15c0SAttilio Rao 	VOP_UNLOCK(nd.ni_vp, 0);
9959589c256SPoul-Henning Kamp 
996d5a929dcSPoul-Henning Kamp 	if (mdio->md_fwsectors != 0)
997d5a929dcSPoul-Henning Kamp 		sc->fwsectors = mdio->md_fwsectors;
998d5a929dcSPoul-Henning Kamp 	if (mdio->md_fwheads != 0)
999d5a929dcSPoul-Henning Kamp 		sc->fwheads = mdio->md_fwheads;
10007a6b2b64SPoul-Henning Kamp 	sc->flags = mdio->md_options & (MD_FORCE | MD_ASYNC);
10019589c256SPoul-Henning Kamp 	if (!(flags & FWRITE))
10029589c256SPoul-Henning Kamp 		sc->flags |= MD_READONLY;
10038f8def9eSPoul-Henning Kamp 	sc->vnode = nd.ni_vp;
10048f8def9eSPoul-Henning Kamp 
1005a854ed98SJohn Baldwin 	error = mdsetcred(sc, td->td_ucred);
1006b830359bSPawel Jakub Dawidek 	if (error != 0) {
10073cf74e53SPhilip Paeps 		sc->vnode = NULL;
1008cb05b60aSAttilio Rao 		vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY);
10093b7b5496SKonstantin Belousov 		nd.ni_vp->v_vflag &= ~VV_MD;
101033fc3625SJohn Baldwin 		goto bad;
101133fc3625SJohn Baldwin 	}
101233fc3625SJohn Baldwin 	return (0);
101333fc3625SJohn Baldwin bad:
101422db15c0SAttilio Rao 	VOP_UNLOCK(nd.ni_vp, 0);
101552c6716fSPawel Jakub Dawidek 	(void)vn_close(nd.ni_vp, flags, td->td_ucred, td);
10168f8def9eSPoul-Henning Kamp 	return (error);
10178f8def9eSPoul-Henning Kamp }
10188f8def9eSPoul-Henning Kamp 
10198f8def9eSPoul-Henning Kamp static int
1020b40ce416SJulian Elischer mddestroy(struct md_s *sc, struct thread *td)
10218f8def9eSPoul-Henning Kamp {
10220cddd8f0SMatthew Dillon 
10236f4f00f1SPoul-Henning Kamp 	if (sc->gp) {
10246f4f00f1SPoul-Henning Kamp 		sc->gp->softc = NULL;
10259b00ca19SPoul-Henning Kamp 		g_topology_lock();
10269b00ca19SPoul-Henning Kamp 		g_wither_geom(sc->gp, ENXIO);
10279b00ca19SPoul-Henning Kamp 		g_topology_unlock();
10286b60a2cdSPoul-Henning Kamp 		sc->gp = NULL;
10296b60a2cdSPoul-Henning Kamp 		sc->pp = NULL;
10301f4ee1aaSPoul-Henning Kamp 	}
1031a03be42dSMaxim Sobolev 	if (sc->devstat) {
1032a03be42dSMaxim Sobolev 		devstat_remove_entry(sc->devstat);
1033a03be42dSMaxim Sobolev 		sc->devstat = NULL;
1034a03be42dSMaxim Sobolev 	}
1035a08d2e7fSJohn Baldwin 	mtx_lock(&sc->queue_mtx);
10365c97ca54SIan Dowse 	sc->flags |= MD_SHUTDOWN;
10375c97ca54SIan Dowse 	wakeup(sc);
1038a08d2e7fSJohn Baldwin 	while (!(sc->flags & MD_EXITING))
1039a08d2e7fSJohn Baldwin 		msleep(sc->procp, &sc->queue_mtx, PRIBIO, "mddestroy", hz / 10);
1040a08d2e7fSJohn Baldwin 	mtx_unlock(&sc->queue_mtx);
10419fbea3e3SPoul-Henning Kamp 	mtx_destroy(&sc->queue_mtx);
10429b00ca19SPoul-Henning Kamp 	if (sc->vnode != NULL) {
1043cb05b60aSAttilio Rao 		vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY);
10443b7b5496SKonstantin Belousov 		sc->vnode->v_vflag &= ~VV_MD;
104522db15c0SAttilio Rao 		VOP_UNLOCK(sc->vnode, 0);
10469d4b5945SMaxim Sobolev 		(void)vn_close(sc->vnode, sc->flags & MD_READONLY ?
1047b40ce416SJulian Elischer 		    FREAD : (FREAD|FWRITE), sc->cred, td);
10489b00ca19SPoul-Henning Kamp 	}
10498f8def9eSPoul-Henning Kamp 	if (sc->cred != NULL)
10508f8def9eSPoul-Henning Kamp 		crfree(sc->cred);
10511db17c6dSPawel Jakub Dawidek 	if (sc->object != NULL)
1052f820bc50SAlan Cox 		vm_object_deallocate(sc->object);
1053f43b2bacSPoul-Henning Kamp 	if (sc->indir)
1054f43b2bacSPoul-Henning Kamp 		destroy_indir(sc, sc->indir);
1055f43b2bacSPoul-Henning Kamp 	if (sc->uma)
1056f43b2bacSPoul-Henning Kamp 		uma_zdestroy(sc->uma);
10571f4ee1aaSPoul-Henning Kamp 
10581f4ee1aaSPoul-Henning Kamp 	LIST_REMOVE(sc, list);
1059f4e7c5a8SJaakko Heinonen 	free_unr(md_uh, sc->unit);
1060c6517568SPoul-Henning Kamp 	free(sc, M_MD);
10618f8def9eSPoul-Henning Kamp 	return (0);
10628f8def9eSPoul-Henning Kamp }
10638f8def9eSPoul-Henning Kamp 
10648f8def9eSPoul-Henning Kamp static int
1065dc604f0cSEdward Tomasz Napierala mdresize(struct md_s *sc, struct md_ioctl *mdio)
1066dc604f0cSEdward Tomasz Napierala {
1067dc604f0cSEdward Tomasz Napierala 	int error, res;
1068dc604f0cSEdward Tomasz Napierala 	vm_pindex_t oldpages, newpages;
1069dc604f0cSEdward Tomasz Napierala 
1070dc604f0cSEdward Tomasz Napierala 	switch (sc->type) {
1071dc604f0cSEdward Tomasz Napierala 	case MD_VNODE:
1072dc604f0cSEdward Tomasz Napierala 		break;
1073dc604f0cSEdward Tomasz Napierala 	case MD_SWAP:
10748cb51643SJaakko Heinonen 		if (mdio->md_mediasize <= 0 ||
1075dc604f0cSEdward Tomasz Napierala 		    (mdio->md_mediasize % PAGE_SIZE) != 0)
1076dc604f0cSEdward Tomasz Napierala 			return (EDOM);
1077dc604f0cSEdward Tomasz Napierala 		oldpages = OFF_TO_IDX(round_page(sc->mediasize));
1078dc604f0cSEdward Tomasz Napierala 		newpages = OFF_TO_IDX(round_page(mdio->md_mediasize));
1079dc604f0cSEdward Tomasz Napierala 		if (newpages < oldpages) {
1080dc604f0cSEdward Tomasz Napierala 			VM_OBJECT_LOCK(sc->object);
1081dc604f0cSEdward Tomasz Napierala 			vm_object_page_remove(sc->object, newpages, 0, 0);
1082dc604f0cSEdward Tomasz Napierala 			swap_pager_freespace(sc->object, newpages,
1083dc604f0cSEdward Tomasz Napierala 			    oldpages - newpages);
1084dc604f0cSEdward Tomasz Napierala 			swap_release_by_cred(IDX_TO_OFF(oldpages -
1085dc604f0cSEdward Tomasz Napierala 			    newpages), sc->cred);
1086dc604f0cSEdward Tomasz Napierala 			sc->object->charge = IDX_TO_OFF(newpages);
1087dc604f0cSEdward Tomasz Napierala 			sc->object->size = newpages;
1088dc604f0cSEdward Tomasz Napierala 			VM_OBJECT_UNLOCK(sc->object);
1089dc604f0cSEdward Tomasz Napierala 		} else if (newpages > oldpages) {
1090dc604f0cSEdward Tomasz Napierala 			res = swap_reserve_by_cred(IDX_TO_OFF(newpages -
1091dc604f0cSEdward Tomasz Napierala 			    oldpages), sc->cred);
1092dc604f0cSEdward Tomasz Napierala 			if (!res)
1093dc604f0cSEdward Tomasz Napierala 				return (ENOMEM);
1094dc604f0cSEdward Tomasz Napierala 			if ((mdio->md_options & MD_RESERVE) ||
1095dc604f0cSEdward Tomasz Napierala 			    (sc->flags & MD_RESERVE)) {
1096dc604f0cSEdward Tomasz Napierala 				error = swap_pager_reserve(sc->object,
1097dc604f0cSEdward Tomasz Napierala 				    oldpages, newpages - oldpages);
1098dc604f0cSEdward Tomasz Napierala 				if (error < 0) {
1099dc604f0cSEdward Tomasz Napierala 					swap_release_by_cred(
1100dc604f0cSEdward Tomasz Napierala 					    IDX_TO_OFF(newpages - oldpages),
1101dc604f0cSEdward Tomasz Napierala 					    sc->cred);
1102dc604f0cSEdward Tomasz Napierala 					return (EDOM);
1103dc604f0cSEdward Tomasz Napierala 				}
1104dc604f0cSEdward Tomasz Napierala 			}
1105dc604f0cSEdward Tomasz Napierala 			VM_OBJECT_LOCK(sc->object);
1106dc604f0cSEdward Tomasz Napierala 			sc->object->charge = IDX_TO_OFF(newpages);
1107dc604f0cSEdward Tomasz Napierala 			sc->object->size = newpages;
1108dc604f0cSEdward Tomasz Napierala 			VM_OBJECT_UNLOCK(sc->object);
1109dc604f0cSEdward Tomasz Napierala 		}
1110dc604f0cSEdward Tomasz Napierala 		break;
1111dc604f0cSEdward Tomasz Napierala 	default:
1112dc604f0cSEdward Tomasz Napierala 		return (EOPNOTSUPP);
1113dc604f0cSEdward Tomasz Napierala 	}
1114dc604f0cSEdward Tomasz Napierala 
1115dc604f0cSEdward Tomasz Napierala 	sc->mediasize = mdio->md_mediasize;
1116dc604f0cSEdward Tomasz Napierala 	g_topology_lock();
1117dc604f0cSEdward Tomasz Napierala 	g_resize_provider(sc->pp, sc->mediasize);
1118dc604f0cSEdward Tomasz Napierala 	g_topology_unlock();
1119dc604f0cSEdward Tomasz Napierala 	return (0);
1120dc604f0cSEdward Tomasz Napierala }
1121dc604f0cSEdward Tomasz Napierala 
1122dc604f0cSEdward Tomasz Napierala static int
1123b830359bSPawel Jakub Dawidek mdcreate_swap(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
11248f8def9eSPoul-Henning Kamp {
1125fcd57fbeSPawel Jakub Dawidek 	vm_ooffset_t npage;
1126fcd57fbeSPawel Jakub Dawidek 	int error;
11278f8def9eSPoul-Henning Kamp 
11288f8def9eSPoul-Henning Kamp 	/*
11298f8def9eSPoul-Henning Kamp 	 * Range check.  Disallow negative sizes or any size less then the
11308f8def9eSPoul-Henning Kamp 	 * size of a page.  Then round to a page.
11318f8def9eSPoul-Henning Kamp 	 */
11328cb51643SJaakko Heinonen 	if (sc->mediasize <= 0 || (sc->mediasize % PAGE_SIZE) != 0)
11338f8def9eSPoul-Henning Kamp 		return (EDOM);
11348f8def9eSPoul-Henning Kamp 
11358f8def9eSPoul-Henning Kamp 	/*
11368f8def9eSPoul-Henning Kamp 	 * Allocate an OBJT_SWAP object.
11378f8def9eSPoul-Henning Kamp 	 *
11388f8def9eSPoul-Henning Kamp 	 * Note the truncation.
11398f8def9eSPoul-Henning Kamp 	 */
11408f8def9eSPoul-Henning Kamp 
1141b830359bSPawel Jakub Dawidek 	npage = mdio->md_mediasize / PAGE_SIZE;
11429ed40643SPoul-Henning Kamp 	if (mdio->md_fwsectors != 0)
11439ed40643SPoul-Henning Kamp 		sc->fwsectors = mdio->md_fwsectors;
11449ed40643SPoul-Henning Kamp 	if (mdio->md_fwheads != 0)
11459ed40643SPoul-Henning Kamp 		sc->fwheads = mdio->md_fwheads;
1146fcd57fbeSPawel Jakub Dawidek 	sc->object = vm_pager_allocate(OBJT_SWAP, NULL, PAGE_SIZE * npage,
11473364c323SKonstantin Belousov 	    VM_PROT_DEFAULT, 0, td->td_ucred);
1148812851b6SBrian Feldman 	if (sc->object == NULL)
1149812851b6SBrian Feldman 		return (ENOMEM);
1150dc604f0cSEdward Tomasz Napierala 	sc->flags = mdio->md_options & (MD_FORCE | MD_RESERVE);
11518f8def9eSPoul-Henning Kamp 	if (mdio->md_options & MD_RESERVE) {
1152fcd57fbeSPawel Jakub Dawidek 		if (swap_pager_reserve(sc->object, 0, npage) < 0) {
11533364c323SKonstantin Belousov 			error = EDOM;
11543364c323SKonstantin Belousov 			goto finish;
11558f8def9eSPoul-Henning Kamp 		}
11568f8def9eSPoul-Henning Kamp 	}
1157a854ed98SJohn Baldwin 	error = mdsetcred(sc, td->td_ucred);
11583364c323SKonstantin Belousov  finish:
1159e3ed29a7SPawel Jakub Dawidek 	if (error != 0) {
11602eafd8b1SPawel Jakub Dawidek 		vm_object_deallocate(sc->object);
11612eafd8b1SPawel Jakub Dawidek 		sc->object = NULL;
11628f8def9eSPoul-Henning Kamp 	}
1163b830359bSPawel Jakub Dawidek 	return (error);
1164b3b3d1b7SPoul-Henning Kamp }
11658f8def9eSPoul-Henning Kamp 
11669d4b5945SMaxim Sobolev 
11679d4b5945SMaxim Sobolev static int
11689b00ca19SPoul-Henning Kamp xmdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
11698f8def9eSPoul-Henning Kamp {
11708f8def9eSPoul-Henning Kamp 	struct md_ioctl *mdio;
11718f8def9eSPoul-Henning Kamp 	struct md_s *sc;
1172b830359bSPawel Jakub Dawidek 	int error, i;
11738cb51643SJaakko Heinonen 	unsigned sectsize;
11748f8def9eSPoul-Henning Kamp 
11758f8def9eSPoul-Henning Kamp 	if (md_debug)
11768f8def9eSPoul-Henning Kamp 		printf("mdctlioctl(%s %lx %p %x %p)\n",
1177b40ce416SJulian Elischer 			devtoname(dev), cmd, addr, flags, td);
11788f8def9eSPoul-Henning Kamp 
11799b00ca19SPoul-Henning Kamp 	mdio = (struct md_ioctl *)addr;
11809b00ca19SPoul-Henning Kamp 	if (mdio->md_version != MDIOVERSION)
11819b00ca19SPoul-Henning Kamp 		return (EINVAL);
11829b00ca19SPoul-Henning Kamp 
118353d745bcSDima Dorfman 	/*
118453d745bcSDima Dorfman 	 * We assert the version number in the individual ioctl
118553d745bcSDima Dorfman 	 * handlers instead of out here because (a) it is possible we
118653d745bcSDima Dorfman 	 * may add another ioctl in the future which doesn't read an
118753d745bcSDima Dorfman 	 * mdio, and (b) the correct return value for an unknown ioctl
118853d745bcSDima Dorfman 	 * is ENOIOCTL, not EINVAL.
118953d745bcSDima Dorfman 	 */
11909b00ca19SPoul-Henning Kamp 	error = 0;
11918f8def9eSPoul-Henning Kamp 	switch (cmd) {
11928f8def9eSPoul-Henning Kamp 	case MDIOCATTACH:
11938f8def9eSPoul-Henning Kamp 		switch (mdio->md_type) {
11948f8def9eSPoul-Henning Kamp 		case MD_MALLOC:
11958f8def9eSPoul-Henning Kamp 		case MD_PRELOAD:
11968f8def9eSPoul-Henning Kamp 		case MD_VNODE:
11978f8def9eSPoul-Henning Kamp 		case MD_SWAP:
1198b830359bSPawel Jakub Dawidek 			break;
11998f8def9eSPoul-Henning Kamp 		default:
12008f8def9eSPoul-Henning Kamp 			return (EINVAL);
12018f8def9eSPoul-Henning Kamp 		}
12028cb51643SJaakko Heinonen 		if (mdio->md_sectorsize == 0)
12038cb51643SJaakko Heinonen 			sectsize = DEV_BSIZE;
12048cb51643SJaakko Heinonen 		else
12058cb51643SJaakko Heinonen 			sectsize = mdio->md_sectorsize;
12068cb51643SJaakko Heinonen 		if (sectsize > MAXPHYS || mdio->md_mediasize < sectsize)
12078cb51643SJaakko Heinonen 			return (EINVAL);
12087ee3c044SPawel Jakub Dawidek 		if (mdio->md_options & MD_AUTOUNIT)
1209947fc8deSPoul-Henning Kamp 			sc = mdnew(-1, &error, mdio->md_type);
1210f4e7c5a8SJaakko Heinonen 		else {
1211f4e7c5a8SJaakko Heinonen 			if (mdio->md_unit > INT_MAX)
1212f4e7c5a8SJaakko Heinonen 				return (EINVAL);
1213947fc8deSPoul-Henning Kamp 			sc = mdnew(mdio->md_unit, &error, mdio->md_type);
1214f4e7c5a8SJaakko Heinonen 		}
1215b830359bSPawel Jakub Dawidek 		if (sc == NULL)
12167ee3c044SPawel Jakub Dawidek 			return (error);
12177ee3c044SPawel Jakub Dawidek 		if (mdio->md_options & MD_AUTOUNIT)
12187ee3c044SPawel Jakub Dawidek 			mdio->md_unit = sc->unit;
1219b830359bSPawel Jakub Dawidek 		sc->mediasize = mdio->md_mediasize;
12208cb51643SJaakko Heinonen 		sc->sectorsize = sectsize;
1221b830359bSPawel Jakub Dawidek 		error = EDOOFUS;
1222b830359bSPawel Jakub Dawidek 		switch (sc->type) {
1223b830359bSPawel Jakub Dawidek 		case MD_MALLOC:
12249b00ca19SPoul-Henning Kamp 			sc->start = mdstart_malloc;
1225b830359bSPawel Jakub Dawidek 			error = mdcreate_malloc(sc, mdio);
1226b830359bSPawel Jakub Dawidek 			break;
1227b830359bSPawel Jakub Dawidek 		case MD_PRELOAD:
12289b00ca19SPoul-Henning Kamp 			sc->start = mdstart_preload;
1229b830359bSPawel Jakub Dawidek 			error = mdcreate_preload(sc, mdio);
1230b830359bSPawel Jakub Dawidek 			break;
1231b830359bSPawel Jakub Dawidek 		case MD_VNODE:
12329b00ca19SPoul-Henning Kamp 			sc->start = mdstart_vnode;
1233b830359bSPawel Jakub Dawidek 			error = mdcreate_vnode(sc, mdio, td);
1234b830359bSPawel Jakub Dawidek 			break;
1235b830359bSPawel Jakub Dawidek 		case MD_SWAP:
12369b00ca19SPoul-Henning Kamp 			sc->start = mdstart_swap;
1237b830359bSPawel Jakub Dawidek 			error = mdcreate_swap(sc, mdio, td);
1238b830359bSPawel Jakub Dawidek 			break;
1239b830359bSPawel Jakub Dawidek 		}
1240b830359bSPawel Jakub Dawidek 		if (error != 0) {
1241b830359bSPawel Jakub Dawidek 			mddestroy(sc, td);
1242b830359bSPawel Jakub Dawidek 			return (error);
1243b830359bSPawel Jakub Dawidek 		}
12449b00ca19SPoul-Henning Kamp 
12459b00ca19SPoul-Henning Kamp 		/* Prune off any residual fractional sector */
12469b00ca19SPoul-Henning Kamp 		i = sc->mediasize % sc->sectorsize;
12479b00ca19SPoul-Henning Kamp 		sc->mediasize -= i;
12489b00ca19SPoul-Henning Kamp 
1249b830359bSPawel Jakub Dawidek 		mdinit(sc);
1250b830359bSPawel Jakub Dawidek 		return (0);
12518f8def9eSPoul-Henning Kamp 	case MDIOCDETACH:
1252a9ebb311SEdward Tomasz Napierala 		if (mdio->md_mediasize != 0 ||
1253a9ebb311SEdward Tomasz Napierala 		    (mdio->md_options & ~MD_FORCE) != 0)
12548f8def9eSPoul-Henning Kamp 			return (EINVAL);
12559b00ca19SPoul-Henning Kamp 
12569b00ca19SPoul-Henning Kamp 		sc = mdfind(mdio->md_unit);
12579b00ca19SPoul-Henning Kamp 		if (sc == NULL)
12589b00ca19SPoul-Henning Kamp 			return (ENOENT);
1259a9ebb311SEdward Tomasz Napierala 		if (sc->opencount != 0 && !(sc->flags & MD_FORCE) &&
1260a9ebb311SEdward Tomasz Napierala 		    !(mdio->md_options & MD_FORCE))
12619b00ca19SPoul-Henning Kamp 			return (EBUSY);
12629b00ca19SPoul-Henning Kamp 		return (mddestroy(sc, td));
1263dc604f0cSEdward Tomasz Napierala 	case MDIOCRESIZE:
1264dc604f0cSEdward Tomasz Napierala 		if ((mdio->md_options & ~(MD_FORCE | MD_RESERVE)) != 0)
1265dc604f0cSEdward Tomasz Napierala 			return (EINVAL);
1266dc604f0cSEdward Tomasz Napierala 
1267dc604f0cSEdward Tomasz Napierala 		sc = mdfind(mdio->md_unit);
1268dc604f0cSEdward Tomasz Napierala 		if (sc == NULL)
1269dc604f0cSEdward Tomasz Napierala 			return (ENOENT);
12708cb51643SJaakko Heinonen 		if (mdio->md_mediasize < sc->sectorsize)
12718cb51643SJaakko Heinonen 			return (EINVAL);
1272dc604f0cSEdward Tomasz Napierala 		if (mdio->md_mediasize < sc->mediasize &&
1273dc604f0cSEdward Tomasz Napierala 		    !(sc->flags & MD_FORCE) &&
1274dc604f0cSEdward Tomasz Napierala 		    !(mdio->md_options & MD_FORCE))
1275dc604f0cSEdward Tomasz Napierala 			return (EBUSY);
1276dc604f0cSEdward Tomasz Napierala 		return (mdresize(sc, mdio));
1277174b5e9aSPoul-Henning Kamp 	case MDIOCQUERY:
1278174b5e9aSPoul-Henning Kamp 		sc = mdfind(mdio->md_unit);
1279174b5e9aSPoul-Henning Kamp 		if (sc == NULL)
1280174b5e9aSPoul-Henning Kamp 			return (ENOENT);
1281174b5e9aSPoul-Henning Kamp 		mdio->md_type = sc->type;
1282174b5e9aSPoul-Henning Kamp 		mdio->md_options = sc->flags;
1283b830359bSPawel Jakub Dawidek 		mdio->md_mediasize = sc->mediasize;
1284b830359bSPawel Jakub Dawidek 		mdio->md_sectorsize = sc->sectorsize;
12859b00ca19SPoul-Henning Kamp 		if (sc->type == MD_VNODE)
128688b5b78dSPawel Jakub Dawidek 			error = copyout(sc->file, mdio->md_file,
128788b5b78dSPawel Jakub Dawidek 			    strlen(sc->file) + 1);
128888b5b78dSPawel Jakub Dawidek 		return (error);
128916bcbe8cSPoul-Henning Kamp 	case MDIOCLIST:
129016bcbe8cSPoul-Henning Kamp 		i = 1;
129116bcbe8cSPoul-Henning Kamp 		LIST_FOREACH(sc, &md_softc_list, list) {
129216bcbe8cSPoul-Henning Kamp 			if (i == MDNPAD - 1)
129316bcbe8cSPoul-Henning Kamp 				mdio->md_pad[i] = -1;
129416bcbe8cSPoul-Henning Kamp 			else
129516bcbe8cSPoul-Henning Kamp 				mdio->md_pad[i++] = sc->unit;
129616bcbe8cSPoul-Henning Kamp 		}
129716bcbe8cSPoul-Henning Kamp 		mdio->md_pad[0] = i - 1;
129816bcbe8cSPoul-Henning Kamp 		return (0);
12998f8def9eSPoul-Henning Kamp 	default:
13008f8def9eSPoul-Henning Kamp 		return (ENOIOCTL);
13018f8def9eSPoul-Henning Kamp 	};
13029b00ca19SPoul-Henning Kamp }
13039b00ca19SPoul-Henning Kamp 
13049b00ca19SPoul-Henning Kamp static int
13059b00ca19SPoul-Henning Kamp mdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
13069b00ca19SPoul-Henning Kamp {
13079b00ca19SPoul-Henning Kamp 	int error;
13089b00ca19SPoul-Henning Kamp 
13099b00ca19SPoul-Henning Kamp 	sx_xlock(&md_sx);
13109b00ca19SPoul-Henning Kamp 	error = xmdctlioctl(dev, cmd, addr, flags, td);
13119b00ca19SPoul-Henning Kamp 	sx_xunlock(&md_sx);
13129b00ca19SPoul-Henning Kamp 	return (error);
13133f54a085SPoul-Henning Kamp }
13143f54a085SPoul-Henning Kamp 
131500a6a3c6SPoul-Henning Kamp static void
1316b830359bSPawel Jakub Dawidek md_preloaded(u_char *image, size_t length)
1317637f671aSPoul-Henning Kamp {
1318637f671aSPoul-Henning Kamp 	struct md_s *sc;
13199b00ca19SPoul-Henning Kamp 	int error;
1320637f671aSPoul-Henning Kamp 
1321947fc8deSPoul-Henning Kamp 	sc = mdnew(-1, &error, MD_PRELOAD);
1322637f671aSPoul-Henning Kamp 	if (sc == NULL)
1323637f671aSPoul-Henning Kamp 		return;
1324b830359bSPawel Jakub Dawidek 	sc->mediasize = length;
1325b830359bSPawel Jakub Dawidek 	sc->sectorsize = DEV_BSIZE;
1326637f671aSPoul-Henning Kamp 	sc->pl_ptr = image;
1327637f671aSPoul-Henning Kamp 	sc->pl_len = length;
13289b00ca19SPoul-Henning Kamp 	sc->start = mdstart_preload;
13295d4ca75eSLuigi Rizzo #ifdef MD_ROOT
1330637f671aSPoul-Henning Kamp 	if (sc->unit == 0)
13315d4ca75eSLuigi Rizzo 		rootdevnames[0] = "ufs:/dev/md0";
13325d4ca75eSLuigi Rizzo #endif
1333637f671aSPoul-Henning Kamp 	mdinit(sc);
1334637f671aSPoul-Henning Kamp }
1335637f671aSPoul-Henning Kamp 
1336637f671aSPoul-Henning Kamp static void
133719945697SPoul-Henning Kamp g_md_init(struct g_class *mp __unused)
133800a6a3c6SPoul-Henning Kamp {
133995f1a897SPoul-Henning Kamp 	caddr_t mod;
134095f1a897SPoul-Henning Kamp 	u_char *ptr, *name, *type;
134195f1a897SPoul-Henning Kamp 	unsigned len;
1342d12fc952SKonstantin Belousov 	int i;
1343d12fc952SKonstantin Belousov 
1344d12fc952SKonstantin Belousov 	/* figure out log2(NINDIR) */
1345d12fc952SKonstantin Belousov 	for (i = NINDIR, nshift = -1; i; nshift++)
1346d12fc952SKonstantin Belousov 		i >>= 1;
134795f1a897SPoul-Henning Kamp 
13480a937206SPoul-Henning Kamp 	mod = NULL;
13499b00ca19SPoul-Henning Kamp 	sx_init(&md_sx, "MD config lock");
13500a937206SPoul-Henning Kamp 	g_topology_unlock();
1351f4e7c5a8SJaakko Heinonen 	md_uh = new_unrhdr(0, INT_MAX, NULL);
135271e4fff8SPoul-Henning Kamp #ifdef MD_ROOT_SIZE
13539b00ca19SPoul-Henning Kamp 	sx_xlock(&md_sx);
1354de64f22aSLuigi Rizzo 	md_preloaded(mfs_root.start, sizeof(mfs_root.start));
13559b00ca19SPoul-Henning Kamp 	sx_xunlock(&md_sx);
135671e4fff8SPoul-Henning Kamp #endif
13579b00ca19SPoul-Henning Kamp 	/* XXX: are preload_* static or do they need Giant ? */
135895f1a897SPoul-Henning Kamp 	while ((mod = preload_search_next_name(mod)) != NULL) {
135995f1a897SPoul-Henning Kamp 		name = (char *)preload_search_info(mod, MODINFO_NAME);
136095f1a897SPoul-Henning Kamp 		if (name == NULL)
136195f1a897SPoul-Henning Kamp 			continue;
13629b00ca19SPoul-Henning Kamp 		type = (char *)preload_search_info(mod, MODINFO_TYPE);
136395f1a897SPoul-Henning Kamp 		if (type == NULL)
136495f1a897SPoul-Henning Kamp 			continue;
136571e4fff8SPoul-Henning Kamp 		if (strcmp(type, "md_image") && strcmp(type, "mfs_root"))
136695f1a897SPoul-Henning Kamp 			continue;
13678d5ac6c3SMarcel Moolenaar 		ptr = preload_fetch_addr(mod);
13688d5ac6c3SMarcel Moolenaar 		len = preload_fetch_size(mod);
13698d5ac6c3SMarcel Moolenaar 		if (ptr != NULL && len != 0) {
1370fe603109SMaxim Sobolev 			printf("%s%d: Preloaded image <%s> %d bytes at %p\n",
1371fe603109SMaxim Sobolev 			    MD_NAME, mdunits, name, len, ptr);
13729b00ca19SPoul-Henning Kamp 			sx_xlock(&md_sx);
1373637f671aSPoul-Henning Kamp 			md_preloaded(ptr, len);
13749b00ca19SPoul-Henning Kamp 			sx_xunlock(&md_sx);
137595f1a897SPoul-Henning Kamp 		}
13768d5ac6c3SMarcel Moolenaar 	}
137706d425f9SEd Schouten 	status_dev = make_dev(&mdctl_cdevsw, INT_MAX, UID_ROOT, GID_WHEEL,
137810b0e058SDima Dorfman 	    0600, MDCTL_NAME);
13790eb14309SPoul-Henning Kamp 	g_topology_lock();
138000a6a3c6SPoul-Henning Kamp }
138100a6a3c6SPoul-Henning Kamp 
138219945697SPoul-Henning Kamp static void
1383c27a8954SWojciech A. Koszek g_md_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1384c27a8954SWojciech A. Koszek     struct g_consumer *cp __unused, struct g_provider *pp)
1385c27a8954SWojciech A. Koszek {
1386c27a8954SWojciech A. Koszek 	struct md_s *mp;
1387c27a8954SWojciech A. Koszek 	char *type;
1388c27a8954SWojciech A. Koszek 
1389c27a8954SWojciech A. Koszek 	mp = gp->softc;
1390c27a8954SWojciech A. Koszek 	if (mp == NULL)
1391c27a8954SWojciech A. Koszek 		return;
1392c27a8954SWojciech A. Koszek 
1393c27a8954SWojciech A. Koszek 	switch (mp->type) {
1394c27a8954SWojciech A. Koszek 	case MD_MALLOC:
1395c27a8954SWojciech A. Koszek 		type = "malloc";
1396c27a8954SWojciech A. Koszek 		break;
1397c27a8954SWojciech A. Koszek 	case MD_PRELOAD:
1398c27a8954SWojciech A. Koszek 		type = "preload";
1399c27a8954SWojciech A. Koszek 		break;
1400c27a8954SWojciech A. Koszek 	case MD_VNODE:
1401c27a8954SWojciech A. Koszek 		type = "vnode";
1402c27a8954SWojciech A. Koszek 		break;
1403c27a8954SWojciech A. Koszek 	case MD_SWAP:
1404c27a8954SWojciech A. Koszek 		type = "swap";
1405c27a8954SWojciech A. Koszek 		break;
1406c27a8954SWojciech A. Koszek 	default:
1407c27a8954SWojciech A. Koszek 		type = "unknown";
1408c27a8954SWojciech A. Koszek 		break;
1409c27a8954SWojciech A. Koszek 	}
1410c27a8954SWojciech A. Koszek 
1411c27a8954SWojciech A. Koszek 	if (pp != NULL) {
1412c27a8954SWojciech A. Koszek 		if (indent == NULL) {
1413c27a8954SWojciech A. Koszek 			sbuf_printf(sb, " u %d", mp->unit);
1414c27a8954SWojciech A. Koszek 			sbuf_printf(sb, " s %ju", (uintmax_t) mp->sectorsize);
1415c27a8954SWojciech A. Koszek 			sbuf_printf(sb, " f %ju", (uintmax_t) mp->fwheads);
1416c27a8954SWojciech A. Koszek 			sbuf_printf(sb, " fs %ju", (uintmax_t) mp->fwsectors);
1417c27a8954SWojciech A. Koszek 			sbuf_printf(sb, " l %ju", (uintmax_t) mp->mediasize);
1418c27a8954SWojciech A. Koszek 			sbuf_printf(sb, " t %s", type);
1419c27a8954SWojciech A. Koszek 			if (mp->type == MD_VNODE && mp->vnode != NULL)
1420c27a8954SWojciech A. Koszek 				sbuf_printf(sb, " file %s", mp->file);
1421c27a8954SWojciech A. Koszek 		} else {
1422c27a8954SWojciech A. Koszek 			sbuf_printf(sb, "%s<unit>%d</unit>\n", indent,
1423c27a8954SWojciech A. Koszek 			    mp->unit);
1424c27a8954SWojciech A. Koszek 			sbuf_printf(sb, "%s<sectorsize>%ju</sectorsize>\n",
1425c27a8954SWojciech A. Koszek 			    indent, (uintmax_t) mp->sectorsize);
1426c27a8954SWojciech A. Koszek 			sbuf_printf(sb, "%s<fwheads>%ju</fwheads>\n",
1427c27a8954SWojciech A. Koszek 			    indent, (uintmax_t) mp->fwheads);
1428c27a8954SWojciech A. Koszek 			sbuf_printf(sb, "%s<fwsectors>%ju</fwsectors>\n",
1429c27a8954SWojciech A. Koszek 			    indent, (uintmax_t) mp->fwsectors);
1430c27a8954SWojciech A. Koszek 			sbuf_printf(sb, "%s<length>%ju</length>\n",
1431c27a8954SWojciech A. Koszek 			    indent, (uintmax_t) mp->mediasize);
14321f192809SAndrey V. Elsukov 			sbuf_printf(sb, "%s<compression>%s</compression>\n", indent,
14331f192809SAndrey V. Elsukov 			    (mp->flags & MD_COMPRESS) == 0 ? "off": "on");
14341f192809SAndrey V. Elsukov 			sbuf_printf(sb, "%s<access>%s</access>\n", indent,
14351f192809SAndrey V. Elsukov 			    (mp->flags & MD_READONLY) == 0 ? "read-write":
14361f192809SAndrey V. Elsukov 			    "read-only");
1437c27a8954SWojciech A. Koszek 			sbuf_printf(sb, "%s<type>%s</type>\n", indent,
1438c27a8954SWojciech A. Koszek 			    type);
1439c27a8954SWojciech A. Koszek 			if (mp->type == MD_VNODE && mp->vnode != NULL)
1440c27a8954SWojciech A. Koszek 				sbuf_printf(sb, "%s<file>%s</file>\n",
1441c27a8954SWojciech A. Koszek 				    indent, mp->file);
1442c27a8954SWojciech A. Koszek 		}
1443c27a8954SWojciech A. Koszek 	}
1444c27a8954SWojciech A. Koszek }
1445c27a8954SWojciech A. Koszek 
1446c27a8954SWojciech A. Koszek static void
144719945697SPoul-Henning Kamp g_md_fini(struct g_class *mp __unused)
144857e9624eSPoul-Henning Kamp {
14499d4b5945SMaxim Sobolev 
14509b00ca19SPoul-Henning Kamp 	sx_destroy(&md_sx);
145119945697SPoul-Henning Kamp 	if (status_dev != NULL)
145257e9624eSPoul-Henning Kamp 		destroy_dev(status_dev);
1453f4e7c5a8SJaakko Heinonen 	delete_unrhdr(md_uh);
145457e9624eSPoul-Henning Kamp }
1455