xref: /freebsd/sys/dev/md/md.c (revision 341b240dc7d5f40f873ece04264bc29c85444b5b)
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 
11322ff74b2SMarcel Moolenaar #if defined(MD_ROOT) && !defined(MD_ROOT_FSTYPE)
11422ff74b2SMarcel Moolenaar #define	MD_ROOT_FSTYPE	"ufs"
11522ff74b2SMarcel Moolenaar #endif
11622ff74b2SMarcel Moolenaar 
11771e4fff8SPoul-Henning Kamp #if defined(MD_ROOT) && defined(MD_ROOT_SIZE)
118de64f22aSLuigi Rizzo /*
119de64f22aSLuigi Rizzo  * Preloaded image gets put here.
120de64f22aSLuigi Rizzo  * Applications that patch the object with the image can determine
121de64f22aSLuigi Rizzo  * the size looking at the start and end markers (strings),
122de64f22aSLuigi Rizzo  * so we want them contiguous.
123de64f22aSLuigi Rizzo  */
124de64f22aSLuigi Rizzo static struct {
125de64f22aSLuigi Rizzo 	u_char start[MD_ROOT_SIZE*1024];
126de64f22aSLuigi Rizzo 	u_char end[128];
127de64f22aSLuigi Rizzo } mfs_root = {
128de64f22aSLuigi Rizzo 	.start = "MFS Filesystem goes here",
129de64f22aSLuigi Rizzo 	.end = "MFS Filesystem had better STOP here",
130de64f22aSLuigi Rizzo };
13171e4fff8SPoul-Henning Kamp #endif
13271e4fff8SPoul-Henning Kamp 
13319945697SPoul-Henning Kamp static g_init_t g_md_init;
13419945697SPoul-Henning Kamp static g_fini_t g_md_fini;
13519945697SPoul-Henning Kamp static g_start_t g_md_start;
13619945697SPoul-Henning Kamp static g_access_t g_md_access;
137b42f40b8SJaakko Heinonen static void g_md_dumpconf(struct sbuf *sb, const char *indent,
138b42f40b8SJaakko Heinonen     struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp);
1390eb14309SPoul-Henning Kamp 
14089c9c53dSPoul-Henning Kamp static struct cdev *status_dev = 0;
1419b00ca19SPoul-Henning Kamp static struct sx md_sx;
142f4e7c5a8SJaakko Heinonen static struct unrhdr *md_uh;
14357e9624eSPoul-Henning Kamp 
144a522a159SPoul-Henning Kamp static d_ioctl_t mdctlioctl;
1458f8def9eSPoul-Henning Kamp 
1468f8def9eSPoul-Henning Kamp static struct cdevsw mdctl_cdevsw = {
147dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
1487ac40f5fSPoul-Henning Kamp 	.d_ioctl =	mdctlioctl,
1497ac40f5fSPoul-Henning Kamp 	.d_name =	MD_NAME,
15000a6a3c6SPoul-Henning Kamp };
15100a6a3c6SPoul-Henning Kamp 
15219945697SPoul-Henning Kamp struct g_class g_md_class = {
15319945697SPoul-Henning Kamp 	.name = "MD",
1545721c9c7SPoul-Henning Kamp 	.version = G_VERSION,
15519945697SPoul-Henning Kamp 	.init = g_md_init,
15619945697SPoul-Henning Kamp 	.fini = g_md_fini,
15719945697SPoul-Henning Kamp 	.start = g_md_start,
15819945697SPoul-Henning Kamp 	.access = g_md_access,
159c27a8954SWojciech A. Koszek 	.dumpconf = g_md_dumpconf,
16019945697SPoul-Henning Kamp };
16119945697SPoul-Henning Kamp 
16219945697SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_md_class, g_md);
16319945697SPoul-Henning Kamp 
1640cfaeeeeSPoul-Henning Kamp 
16513e403fdSAntoine Brodin static LIST_HEAD(, md_s) md_softc_list = LIST_HEAD_INITIALIZER(md_softc_list);
1663f54a085SPoul-Henning Kamp 
167c6517568SPoul-Henning Kamp #define NINDIR	(PAGE_SIZE / sizeof(uintptr_t))
168c6517568SPoul-Henning Kamp #define NMASK	(NINDIR-1)
169c6517568SPoul-Henning Kamp static int nshift;
170c6517568SPoul-Henning Kamp 
171c6517568SPoul-Henning Kamp struct indir {
172c6517568SPoul-Henning Kamp 	uintptr_t	*array;
1738b149b51SJohn Baldwin 	u_int		total;
1748b149b51SJohn Baldwin 	u_int		used;
1758b149b51SJohn Baldwin 	u_int		shift;
176c6517568SPoul-Henning Kamp };
177c6517568SPoul-Henning Kamp 
17800a6a3c6SPoul-Henning Kamp struct md_s {
17900a6a3c6SPoul-Henning Kamp 	int unit;
1803f54a085SPoul-Henning Kamp 	LIST_ENTRY(md_s) list;
1818177437dSPoul-Henning Kamp 	struct bio_queue_head bio_queue;
1820f8500a5SPoul-Henning Kamp 	struct mtx queue_mtx;
18389c9c53dSPoul-Henning Kamp 	struct cdev *dev;
1848f8def9eSPoul-Henning Kamp 	enum md_types type;
185b830359bSPawel Jakub Dawidek 	off_t mediasize;
186b830359bSPawel Jakub Dawidek 	unsigned sectorsize;
187fe603109SMaxim Sobolev 	unsigned opencount;
1884e8bfe14SPoul-Henning Kamp 	unsigned fwheads;
1894e8bfe14SPoul-Henning Kamp 	unsigned fwsectors;
1908f8def9eSPoul-Henning Kamp 	unsigned flags;
191f43b2bacSPoul-Henning Kamp 	char name[20];
1925c97ca54SIan Dowse 	struct proc *procp;
1936f4f00f1SPoul-Henning Kamp 	struct g_geom *gp;
1946f4f00f1SPoul-Henning Kamp 	struct g_provider *pp;
1959b00ca19SPoul-Henning Kamp 	int (*start)(struct md_s *sc, struct bio *bp);
196a03be42dSMaxim Sobolev 	struct devstat *devstat;
19795f1a897SPoul-Henning Kamp 
19895f1a897SPoul-Henning Kamp 	/* MD_MALLOC related fields */
199c6517568SPoul-Henning Kamp 	struct indir *indir;
200f43b2bacSPoul-Henning Kamp 	uma_zone_t uma;
20100a6a3c6SPoul-Henning Kamp 
20295f1a897SPoul-Henning Kamp 	/* MD_PRELOAD related fields */
20395f1a897SPoul-Henning Kamp 	u_char *pl_ptr;
204b830359bSPawel Jakub Dawidek 	size_t pl_len;
20500a6a3c6SPoul-Henning Kamp 
2068f8def9eSPoul-Henning Kamp 	/* MD_VNODE related fields */
2078f8def9eSPoul-Henning Kamp 	struct vnode *vnode;
20861a6eb62SPawel Jakub Dawidek 	char file[PATH_MAX];
2098f8def9eSPoul-Henning Kamp 	struct ucred *cred;
2108f8def9eSPoul-Henning Kamp 
211e0cebb40SDima Dorfman 	/* MD_SWAP related fields */
2128f8def9eSPoul-Henning Kamp 	vm_object_t object;
2138f8def9eSPoul-Henning Kamp };
21400a6a3c6SPoul-Henning Kamp 
215c6517568SPoul-Henning Kamp static struct indir *
2168b149b51SJohn Baldwin new_indir(u_int shift)
217c6517568SPoul-Henning Kamp {
218c6517568SPoul-Henning Kamp 	struct indir *ip;
219c6517568SPoul-Henning Kamp 
220c44d423eSKonstantin Belousov 	ip = malloc(sizeof *ip, M_MD, (md_malloc_wait ? M_WAITOK : M_NOWAIT)
221c44d423eSKonstantin Belousov 	    | M_ZERO);
222c6517568SPoul-Henning Kamp 	if (ip == NULL)
223c6517568SPoul-Henning Kamp 		return (NULL);
224c6517568SPoul-Henning Kamp 	ip->array = malloc(sizeof(uintptr_t) * NINDIR,
225c44d423eSKonstantin Belousov 	    M_MDSECT, (md_malloc_wait ? M_WAITOK : M_NOWAIT) | M_ZERO);
226c6517568SPoul-Henning Kamp 	if (ip->array == NULL) {
227c6517568SPoul-Henning Kamp 		free(ip, M_MD);
228c6517568SPoul-Henning Kamp 		return (NULL);
229c6517568SPoul-Henning Kamp 	}
230c6517568SPoul-Henning Kamp 	ip->total = NINDIR;
231c6517568SPoul-Henning Kamp 	ip->shift = shift;
232c6517568SPoul-Henning Kamp 	return (ip);
233c6517568SPoul-Henning Kamp }
234c6517568SPoul-Henning Kamp 
235c6517568SPoul-Henning Kamp static void
236c6517568SPoul-Henning Kamp del_indir(struct indir *ip)
237c6517568SPoul-Henning Kamp {
238c6517568SPoul-Henning Kamp 
239f43b2bacSPoul-Henning Kamp 	free(ip->array, M_MDSECT);
240c6517568SPoul-Henning Kamp 	free(ip, M_MD);
241c6517568SPoul-Henning Kamp }
242c6517568SPoul-Henning Kamp 
243f43b2bacSPoul-Henning Kamp static void
244f43b2bacSPoul-Henning Kamp destroy_indir(struct md_s *sc, struct indir *ip)
245f43b2bacSPoul-Henning Kamp {
246f43b2bacSPoul-Henning Kamp 	int i;
247f43b2bacSPoul-Henning Kamp 
248f43b2bacSPoul-Henning Kamp 	for (i = 0; i < NINDIR; i++) {
249f43b2bacSPoul-Henning Kamp 		if (!ip->array[i])
250f43b2bacSPoul-Henning Kamp 			continue;
251f43b2bacSPoul-Henning Kamp 		if (ip->shift)
252f43b2bacSPoul-Henning Kamp 			destroy_indir(sc, (struct indir*)(ip->array[i]));
253f43b2bacSPoul-Henning Kamp 		else if (ip->array[i] > 255)
254f43b2bacSPoul-Henning Kamp 			uma_zfree(sc->uma, (void *)(ip->array[i]));
255f43b2bacSPoul-Henning Kamp 	}
256f43b2bacSPoul-Henning Kamp 	del_indir(ip);
257f43b2bacSPoul-Henning Kamp }
258f43b2bacSPoul-Henning Kamp 
259c6517568SPoul-Henning Kamp /*
2606c3cd0e2SMaxim Konovalov  * This function does the math and allocates the top level "indir" structure
261c6517568SPoul-Henning Kamp  * for a device of "size" sectors.
262c6517568SPoul-Henning Kamp  */
263c6517568SPoul-Henning Kamp 
264c6517568SPoul-Henning Kamp static struct indir *
265c6517568SPoul-Henning Kamp dimension(off_t size)
266c6517568SPoul-Henning Kamp {
267c6517568SPoul-Henning Kamp 	off_t rcnt;
268c6517568SPoul-Henning Kamp 	struct indir *ip;
269d12fc952SKonstantin Belousov 	int layer;
270c6517568SPoul-Henning Kamp 
271c6517568SPoul-Henning Kamp 	rcnt = size;
272c6517568SPoul-Henning Kamp 	layer = 0;
273c6517568SPoul-Henning Kamp 	while (rcnt > NINDIR) {
274c6517568SPoul-Henning Kamp 		rcnt /= NINDIR;
275c6517568SPoul-Henning Kamp 		layer++;
276c6517568SPoul-Henning Kamp 	}
277c6517568SPoul-Henning Kamp 
278c6517568SPoul-Henning Kamp 	/*
279c6517568SPoul-Henning Kamp 	 * XXX: the top layer is probably not fully populated, so we allocate
28083e13864SPoul-Henning Kamp 	 * too much space for ip->array in here.
281c6517568SPoul-Henning Kamp 	 */
28283e13864SPoul-Henning Kamp 	ip = malloc(sizeof *ip, M_MD, M_WAITOK | M_ZERO);
28383e13864SPoul-Henning Kamp 	ip->array = malloc(sizeof(uintptr_t) * NINDIR,
28483e13864SPoul-Henning Kamp 	    M_MDSECT, M_WAITOK | M_ZERO);
28583e13864SPoul-Henning Kamp 	ip->total = NINDIR;
28683e13864SPoul-Henning Kamp 	ip->shift = layer * nshift;
287c6517568SPoul-Henning Kamp 	return (ip);
288c6517568SPoul-Henning Kamp }
289c6517568SPoul-Henning Kamp 
290c6517568SPoul-Henning Kamp /*
291c6517568SPoul-Henning Kamp  * Read a given sector
292c6517568SPoul-Henning Kamp  */
293c6517568SPoul-Henning Kamp 
294c6517568SPoul-Henning Kamp static uintptr_t
295c6517568SPoul-Henning Kamp s_read(struct indir *ip, off_t offset)
296c6517568SPoul-Henning Kamp {
297c6517568SPoul-Henning Kamp 	struct indir *cip;
298c6517568SPoul-Henning Kamp 	int idx;
299c6517568SPoul-Henning Kamp 	uintptr_t up;
300c6517568SPoul-Henning Kamp 
301c6517568SPoul-Henning Kamp 	if (md_debug > 1)
3026569d6f3SMaxime Henrion 		printf("s_read(%jd)\n", (intmax_t)offset);
303c6517568SPoul-Henning Kamp 	up = 0;
304c6517568SPoul-Henning Kamp 	for (cip = ip; cip != NULL;) {
305c6517568SPoul-Henning Kamp 		if (cip->shift) {
306c6517568SPoul-Henning Kamp 			idx = (offset >> cip->shift) & NMASK;
307c6517568SPoul-Henning Kamp 			up = cip->array[idx];
308c6517568SPoul-Henning Kamp 			cip = (struct indir *)up;
309c6517568SPoul-Henning Kamp 			continue;
310c6517568SPoul-Henning Kamp 		}
311c6517568SPoul-Henning Kamp 		idx = offset & NMASK;
312c6517568SPoul-Henning Kamp 		return (cip->array[idx]);
313c6517568SPoul-Henning Kamp 	}
314c6517568SPoul-Henning Kamp 	return (0);
315c6517568SPoul-Henning Kamp }
316c6517568SPoul-Henning Kamp 
317c6517568SPoul-Henning Kamp /*
318c6517568SPoul-Henning Kamp  * Write a given sector, prune the tree if the value is 0
319c6517568SPoul-Henning Kamp  */
320c6517568SPoul-Henning Kamp 
321c6517568SPoul-Henning Kamp static int
322fde2a2e4SPoul-Henning Kamp s_write(struct indir *ip, off_t offset, uintptr_t ptr)
323c6517568SPoul-Henning Kamp {
324c6517568SPoul-Henning Kamp 	struct indir *cip, *lip[10];
325c6517568SPoul-Henning Kamp 	int idx, li;
326c6517568SPoul-Henning Kamp 	uintptr_t up;
327c6517568SPoul-Henning Kamp 
328c6517568SPoul-Henning Kamp 	if (md_debug > 1)
3296569d6f3SMaxime Henrion 		printf("s_write(%jd, %p)\n", (intmax_t)offset, (void *)ptr);
330c6517568SPoul-Henning Kamp 	up = 0;
331c6517568SPoul-Henning Kamp 	li = 0;
332c6517568SPoul-Henning Kamp 	cip = ip;
333c6517568SPoul-Henning Kamp 	for (;;) {
334c6517568SPoul-Henning Kamp 		lip[li++] = cip;
335c6517568SPoul-Henning Kamp 		if (cip->shift) {
336c6517568SPoul-Henning Kamp 			idx = (offset >> cip->shift) & NMASK;
337c6517568SPoul-Henning Kamp 			up = cip->array[idx];
338c6517568SPoul-Henning Kamp 			if (up != 0) {
339c6517568SPoul-Henning Kamp 				cip = (struct indir *)up;
340c6517568SPoul-Henning Kamp 				continue;
341c6517568SPoul-Henning Kamp 			}
342c6517568SPoul-Henning Kamp 			/* Allocate branch */
343c6517568SPoul-Henning Kamp 			cip->array[idx] =
344c6517568SPoul-Henning Kamp 			    (uintptr_t)new_indir(cip->shift - nshift);
345c6517568SPoul-Henning Kamp 			if (cip->array[idx] == 0)
346975b628fSPoul-Henning Kamp 				return (ENOSPC);
347c6517568SPoul-Henning Kamp 			cip->used++;
348c6517568SPoul-Henning Kamp 			up = cip->array[idx];
349c6517568SPoul-Henning Kamp 			cip = (struct indir *)up;
350c6517568SPoul-Henning Kamp 			continue;
351c6517568SPoul-Henning Kamp 		}
352c6517568SPoul-Henning Kamp 		/* leafnode */
353c6517568SPoul-Henning Kamp 		idx = offset & NMASK;
354c6517568SPoul-Henning Kamp 		up = cip->array[idx];
355c6517568SPoul-Henning Kamp 		if (up != 0)
356c6517568SPoul-Henning Kamp 			cip->used--;
357c6517568SPoul-Henning Kamp 		cip->array[idx] = ptr;
358c6517568SPoul-Henning Kamp 		if (ptr != 0)
359c6517568SPoul-Henning Kamp 			cip->used++;
360c6517568SPoul-Henning Kamp 		break;
361c6517568SPoul-Henning Kamp 	}
362c6517568SPoul-Henning Kamp 	if (cip->used != 0 || li == 1)
363c6517568SPoul-Henning Kamp 		return (0);
364c6517568SPoul-Henning Kamp 	li--;
365c6517568SPoul-Henning Kamp 	while (cip->used == 0 && cip != ip) {
366c6517568SPoul-Henning Kamp 		li--;
367c6517568SPoul-Henning Kamp 		idx = (offset >> lip[li]->shift) & NMASK;
368c6517568SPoul-Henning Kamp 		up = lip[li]->array[idx];
369c6517568SPoul-Henning Kamp 		KASSERT(up == (uintptr_t)cip, ("md screwed up"));
370c6517568SPoul-Henning Kamp 		del_indir(cip);
3714a6a94d8SArchie Cobbs 		lip[li]->array[idx] = 0;
372c6517568SPoul-Henning Kamp 		lip[li]->used--;
373c6517568SPoul-Henning Kamp 		cip = lip[li];
374c6517568SPoul-Henning Kamp 	}
375c6517568SPoul-Henning Kamp 	return (0);
376c6517568SPoul-Henning Kamp }
377c6517568SPoul-Henning Kamp 
3786f4f00f1SPoul-Henning Kamp 
3796f4f00f1SPoul-Henning Kamp static int
3806f4f00f1SPoul-Henning Kamp g_md_access(struct g_provider *pp, int r, int w, int e)
3816f4f00f1SPoul-Henning Kamp {
3826f4f00f1SPoul-Henning Kamp 	struct md_s *sc;
3836f4f00f1SPoul-Henning Kamp 
3846f4f00f1SPoul-Henning Kamp 	sc = pp->geom->softc;
38541c8b468SEdward Tomasz Napierala 	if (sc == NULL) {
38641c8b468SEdward Tomasz Napierala 		if (r <= 0 && w <= 0 && e <= 0)
38741c8b468SEdward Tomasz Napierala 			return (0);
3886b60a2cdSPoul-Henning Kamp 		return (ENXIO);
38941c8b468SEdward Tomasz Napierala 	}
3906f4f00f1SPoul-Henning Kamp 	r += pp->acr;
3916f4f00f1SPoul-Henning Kamp 	w += pp->acw;
3926f4f00f1SPoul-Henning Kamp 	e += pp->ace;
39386776891SChristian S.J. Peron 	if ((sc->flags & MD_READONLY) != 0 && w > 0)
39486776891SChristian S.J. Peron 		return (EROFS);
3956f4f00f1SPoul-Henning Kamp 	if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
3966f4f00f1SPoul-Henning Kamp 		sc->opencount = 1;
3976f4f00f1SPoul-Henning Kamp 	} else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
3986f4f00f1SPoul-Henning Kamp 		sc->opencount = 0;
3996f4f00f1SPoul-Henning Kamp 	}
4006f4f00f1SPoul-Henning Kamp 	return (0);
4016f4f00f1SPoul-Henning Kamp }
4026f4f00f1SPoul-Henning Kamp 
4036f4f00f1SPoul-Henning Kamp static void
4046f4f00f1SPoul-Henning Kamp g_md_start(struct bio *bp)
4056f4f00f1SPoul-Henning Kamp {
4066f4f00f1SPoul-Henning Kamp 	struct md_s *sc;
4076f4f00f1SPoul-Henning Kamp 
4086f4f00f1SPoul-Henning Kamp 	sc = bp->bio_to->geom->softc;
409a03be42dSMaxim Sobolev 	if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE))
410a03be42dSMaxim Sobolev 		devstat_start_transaction_bio(sc->devstat, bp);
4110f8500a5SPoul-Henning Kamp 	mtx_lock(&sc->queue_mtx);
412891619a6SPoul-Henning Kamp 	bioq_disksort(&sc->bio_queue, bp);
4134b07ede4SPawel Jakub Dawidek 	mtx_unlock(&sc->queue_mtx);
414e4cdd0d4SPawel Jakub Dawidek 	wakeup(sc);
4156f4f00f1SPoul-Henning Kamp }
4166f4f00f1SPoul-Henning Kamp 
417b4a4f93cSPoul-Henning Kamp static int
418b4a4f93cSPoul-Henning Kamp mdstart_malloc(struct md_s *sc, struct bio *bp)
41900a6a3c6SPoul-Henning Kamp {
420c6517568SPoul-Henning Kamp 	int i, error;
421c6517568SPoul-Henning Kamp 	u_char *dst;
422b830359bSPawel Jakub Dawidek 	off_t secno, nsec, uc;
423c6517568SPoul-Henning Kamp 	uintptr_t sp, osp;
42400a6a3c6SPoul-Henning Kamp 
4255541f25eSPawel Jakub Dawidek 	switch (bp->bio_cmd) {
4265541f25eSPawel Jakub Dawidek 	case BIO_READ:
4275541f25eSPawel Jakub Dawidek 	case BIO_WRITE:
4285541f25eSPawel Jakub Dawidek 	case BIO_DELETE:
4295541f25eSPawel Jakub Dawidek 		break;
4305541f25eSPawel Jakub Dawidek 	default:
4315541f25eSPawel Jakub Dawidek 		return (EOPNOTSUPP);
4325541f25eSPawel Jakub Dawidek 	}
4335541f25eSPawel Jakub Dawidek 
434b830359bSPawel Jakub Dawidek 	nsec = bp->bio_length / sc->sectorsize;
435b830359bSPawel Jakub Dawidek 	secno = bp->bio_offset / sc->sectorsize;
4368177437dSPoul-Henning Kamp 	dst = bp->bio_data;
437c6517568SPoul-Henning Kamp 	error = 0;
43800a6a3c6SPoul-Henning Kamp 	while (nsec--) {
439fde2a2e4SPoul-Henning Kamp 		osp = s_read(sc->indir, secno);
4408177437dSPoul-Henning Kamp 		if (bp->bio_cmd == BIO_DELETE) {
441fde2a2e4SPoul-Henning Kamp 			if (osp != 0)
442fde2a2e4SPoul-Henning Kamp 				error = s_write(sc->indir, secno, 0);
4438177437dSPoul-Henning Kamp 		} else if (bp->bio_cmd == BIO_READ) {
444fde2a2e4SPoul-Henning Kamp 			if (osp == 0)
445b830359bSPawel Jakub Dawidek 				bzero(dst, sc->sectorsize);
446fde2a2e4SPoul-Henning Kamp 			else if (osp <= 255)
447dbb95048SMarcel Moolenaar 				memset(dst, osp, sc->sectorsize);
448dbb95048SMarcel Moolenaar 			else {
449b830359bSPawel Jakub Dawidek 				bcopy((void *)osp, dst, sc->sectorsize);
450dbb95048SMarcel Moolenaar 				cpu_flush_dcache(dst, sc->sectorsize);
451dbb95048SMarcel Moolenaar 			}
452fde2a2e4SPoul-Henning Kamp 			osp = 0;
453c6517568SPoul-Henning Kamp 		} else if (bp->bio_cmd == BIO_WRITE) {
4548f8def9eSPoul-Henning Kamp 			if (sc->flags & MD_COMPRESS) {
45500a6a3c6SPoul-Henning Kamp 				uc = dst[0];
456b830359bSPawel Jakub Dawidek 				for (i = 1; i < sc->sectorsize; i++)
45700a6a3c6SPoul-Henning Kamp 					if (dst[i] != uc)
45800a6a3c6SPoul-Henning Kamp 						break;
4598f8def9eSPoul-Henning Kamp 			} else {
4608f8def9eSPoul-Henning Kamp 				i = 0;
4618f8def9eSPoul-Henning Kamp 				uc = 0;
4628f8def9eSPoul-Henning Kamp 			}
463b830359bSPawel Jakub Dawidek 			if (i == sc->sectorsize) {
464fde2a2e4SPoul-Henning Kamp 				if (osp != uc)
465fde2a2e4SPoul-Henning Kamp 					error = s_write(sc->indir, secno, uc);
46600a6a3c6SPoul-Henning Kamp 			} else {
467fde2a2e4SPoul-Henning Kamp 				if (osp <= 255) {
468b830359bSPawel Jakub Dawidek 					sp = (uintptr_t)uma_zalloc(sc->uma,
469c44d423eSKonstantin Belousov 					    md_malloc_wait ? M_WAITOK :
470b830359bSPawel Jakub Dawidek 					    M_NOWAIT);
471c6517568SPoul-Henning Kamp 					if (sp == 0) {
472fde2a2e4SPoul-Henning Kamp 						error = ENOSPC;
473fde2a2e4SPoul-Henning Kamp 						break;
474fde2a2e4SPoul-Henning Kamp 					}
475b830359bSPawel Jakub Dawidek 					bcopy(dst, (void *)sp, sc->sectorsize);
476fde2a2e4SPoul-Henning Kamp 					error = s_write(sc->indir, secno, sp);
477c6517568SPoul-Henning Kamp 				} else {
478b830359bSPawel Jakub Dawidek 					bcopy(dst, (void *)osp, sc->sectorsize);
479fde2a2e4SPoul-Henning Kamp 					osp = 0;
48000a6a3c6SPoul-Henning Kamp 				}
48100a6a3c6SPoul-Henning Kamp 			}
482c6517568SPoul-Henning Kamp 		} else {
483c6517568SPoul-Henning Kamp 			error = EOPNOTSUPP;
484c6517568SPoul-Henning Kamp 		}
485c6517568SPoul-Henning Kamp 		if (osp > 255)
486f43b2bacSPoul-Henning Kamp 			uma_zfree(sc->uma, (void*)osp);
487e3ed29a7SPawel Jakub Dawidek 		if (error != 0)
488c6517568SPoul-Henning Kamp 			break;
48900a6a3c6SPoul-Henning Kamp 		secno++;
490b830359bSPawel Jakub Dawidek 		dst += sc->sectorsize;
49100a6a3c6SPoul-Henning Kamp 	}
4928177437dSPoul-Henning Kamp 	bp->bio_resid = 0;
493c6517568SPoul-Henning Kamp 	return (error);
49400a6a3c6SPoul-Henning Kamp }
49500a6a3c6SPoul-Henning Kamp 
496b4a4f93cSPoul-Henning Kamp static int
497b4a4f93cSPoul-Henning Kamp mdstart_preload(struct md_s *sc, struct bio *bp)
49871e4fff8SPoul-Henning Kamp {
49971e4fff8SPoul-Henning Kamp 
500a8a58d03SPawel Jakub Dawidek 	switch (bp->bio_cmd) {
501a8a58d03SPawel Jakub Dawidek 	case BIO_READ:
502a8a58d03SPawel Jakub Dawidek 		bcopy(sc->pl_ptr + bp->bio_offset, bp->bio_data,
503a8a58d03SPawel Jakub Dawidek 		    bp->bio_length);
504dbb95048SMarcel Moolenaar 		cpu_flush_dcache(bp->bio_data, bp->bio_length);
505a8a58d03SPawel Jakub Dawidek 		break;
506a8a58d03SPawel Jakub Dawidek 	case BIO_WRITE:
507a8a58d03SPawel Jakub Dawidek 		bcopy(bp->bio_data, sc->pl_ptr + bp->bio_offset,
508a8a58d03SPawel Jakub Dawidek 		    bp->bio_length);
509a8a58d03SPawel Jakub Dawidek 		break;
51071e4fff8SPoul-Henning Kamp 	}
5118177437dSPoul-Henning Kamp 	bp->bio_resid = 0;
512b4a4f93cSPoul-Henning Kamp 	return (0);
51371e4fff8SPoul-Henning Kamp }
51471e4fff8SPoul-Henning Kamp 
515b4a4f93cSPoul-Henning Kamp static int
516b4a4f93cSPoul-Henning Kamp mdstart_vnode(struct md_s *sc, struct bio *bp)
5178f8def9eSPoul-Henning Kamp {
5185050aa86SKonstantin Belousov 	int error;
5198f8def9eSPoul-Henning Kamp 	struct uio auio;
5208f8def9eSPoul-Henning Kamp 	struct iovec aiov;
5218f8def9eSPoul-Henning Kamp 	struct mount *mp;
5225541f25eSPawel Jakub Dawidek 	struct vnode *vp;
5235541f25eSPawel Jakub Dawidek 	struct thread *td;
5240abd21bdSDag-Erling Smørgrav 	off_t end, zerosize;
5255541f25eSPawel Jakub Dawidek 
5265541f25eSPawel Jakub Dawidek 	switch (bp->bio_cmd) {
5275541f25eSPawel Jakub Dawidek 	case BIO_READ:
5285541f25eSPawel Jakub Dawidek 	case BIO_WRITE:
5290abd21bdSDag-Erling Smørgrav 	case BIO_DELETE:
5305541f25eSPawel Jakub Dawidek 	case BIO_FLUSH:
5315541f25eSPawel Jakub Dawidek 		break;
5325541f25eSPawel Jakub Dawidek 	default:
5335541f25eSPawel Jakub Dawidek 		return (EOPNOTSUPP);
5345541f25eSPawel Jakub Dawidek 	}
5355541f25eSPawel Jakub Dawidek 
5365541f25eSPawel Jakub Dawidek 	td = curthread;
5375541f25eSPawel Jakub Dawidek 	vp = sc->vnode;
5388f8def9eSPoul-Henning Kamp 
5398f8def9eSPoul-Henning Kamp 	/*
5408f8def9eSPoul-Henning Kamp 	 * VNODE I/O
5418f8def9eSPoul-Henning Kamp 	 *
5428f8def9eSPoul-Henning Kamp 	 * If an error occurs, we set BIO_ERROR but we do not set
5438f8def9eSPoul-Henning Kamp 	 * B_INVAL because (for a write anyway), the buffer is
5448f8def9eSPoul-Henning Kamp 	 * still valid.
5458f8def9eSPoul-Henning Kamp 	 */
5468f8def9eSPoul-Henning Kamp 
5475541f25eSPawel Jakub Dawidek 	if (bp->bio_cmd == BIO_FLUSH) {
5485541f25eSPawel Jakub Dawidek 		(void) vn_start_write(vp, &mp, V_WAIT);
549cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5505541f25eSPawel Jakub Dawidek 		error = VOP_FSYNC(vp, MNT_WAIT, td);
55122db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
5525541f25eSPawel Jakub Dawidek 		vn_finished_write(mp);
5535541f25eSPawel Jakub Dawidek 		return (error);
5545541f25eSPawel Jakub Dawidek 	}
5555541f25eSPawel Jakub Dawidek 
5568f8def9eSPoul-Henning Kamp 	bzero(&auio, sizeof(auio));
5578f8def9eSPoul-Henning Kamp 
5580abd21bdSDag-Erling Smørgrav 	/*
5590abd21bdSDag-Erling Smørgrav 	 * Special case for BIO_DELETE.  On the surface, this is very
5600abd21bdSDag-Erling Smørgrav 	 * similar to BIO_WRITE, except that we write from our own
5610abd21bdSDag-Erling Smørgrav 	 * fixed-length buffer, so we have to loop.  The net result is
5620abd21bdSDag-Erling Smørgrav 	 * that the two cases end up having very little in common.
5630abd21bdSDag-Erling Smørgrav 	 */
5640abd21bdSDag-Erling Smørgrav 	if (bp->bio_cmd == BIO_DELETE) {
56589cb2a19SMatthew D Fleming 		zerosize = ZERO_REGION_SIZE -
56689cb2a19SMatthew D Fleming 		    (ZERO_REGION_SIZE % sc->sectorsize);
5670abd21bdSDag-Erling Smørgrav 		auio.uio_iov = &aiov;
5680abd21bdSDag-Erling Smørgrav 		auio.uio_iovcnt = 1;
5690abd21bdSDag-Erling Smørgrav 		auio.uio_offset = (vm_ooffset_t)bp->bio_offset;
5700abd21bdSDag-Erling Smørgrav 		auio.uio_segflg = UIO_SYSSPACE;
5710abd21bdSDag-Erling Smørgrav 		auio.uio_rw = UIO_WRITE;
5720abd21bdSDag-Erling Smørgrav 		auio.uio_td = td;
5730abd21bdSDag-Erling Smørgrav 		end = bp->bio_offset + bp->bio_length;
5740abd21bdSDag-Erling Smørgrav 		(void) vn_start_write(vp, &mp, V_WAIT);
5750abd21bdSDag-Erling Smørgrav 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5760abd21bdSDag-Erling Smørgrav 		error = 0;
5770abd21bdSDag-Erling Smørgrav 		while (auio.uio_offset < end) {
57889cb2a19SMatthew D Fleming 			aiov.iov_base = __DECONST(void *, zero_region);
5790abd21bdSDag-Erling Smørgrav 			aiov.iov_len = end - auio.uio_offset;
5800abd21bdSDag-Erling Smørgrav 			if (aiov.iov_len > zerosize)
5810abd21bdSDag-Erling Smørgrav 				aiov.iov_len = zerosize;
5820abd21bdSDag-Erling Smørgrav 			auio.uio_resid = aiov.iov_len;
5830abd21bdSDag-Erling Smørgrav 			error = VOP_WRITE(vp, &auio,
5840abd21bdSDag-Erling Smørgrav 			    sc->flags & MD_ASYNC ? 0 : IO_SYNC, sc->cred);
5850abd21bdSDag-Erling Smørgrav 			if (error != 0)
5860abd21bdSDag-Erling Smørgrav 				break;
5870abd21bdSDag-Erling Smørgrav 		}
5880abd21bdSDag-Erling Smørgrav 		VOP_UNLOCK(vp, 0);
5890abd21bdSDag-Erling Smørgrav 		vn_finished_write(mp);
5900abd21bdSDag-Erling Smørgrav 		bp->bio_resid = end - auio.uio_offset;
5910abd21bdSDag-Erling Smørgrav 		return (error);
5920abd21bdSDag-Erling Smørgrav 	}
5930abd21bdSDag-Erling Smørgrav 
5948f8def9eSPoul-Henning Kamp 	aiov.iov_base = bp->bio_data;
595a8a58d03SPawel Jakub Dawidek 	aiov.iov_len = bp->bio_length;
5968f8def9eSPoul-Henning Kamp 	auio.uio_iov = &aiov;
5978f8def9eSPoul-Henning Kamp 	auio.uio_iovcnt = 1;
598a8a58d03SPawel Jakub Dawidek 	auio.uio_offset = (vm_ooffset_t)bp->bio_offset;
5998f8def9eSPoul-Henning Kamp 	auio.uio_segflg = UIO_SYSSPACE;
6008f8def9eSPoul-Henning Kamp 	if (bp->bio_cmd == BIO_READ)
6018f8def9eSPoul-Henning Kamp 		auio.uio_rw = UIO_READ;
6028e28326aSPoul-Henning Kamp 	else if (bp->bio_cmd == BIO_WRITE)
6038f8def9eSPoul-Henning Kamp 		auio.uio_rw = UIO_WRITE;
6048e28326aSPoul-Henning Kamp 	else
6058e28326aSPoul-Henning Kamp 		panic("wrong BIO_OP in mdstart_vnode");
606a8a58d03SPawel Jakub Dawidek 	auio.uio_resid = bp->bio_length;
6075541f25eSPawel Jakub Dawidek 	auio.uio_td = td;
6087e76bb56SMatthew Dillon 	/*
6097e76bb56SMatthew Dillon 	 * When reading set IO_DIRECT to try to avoid double-caching
61017a13919SPoul-Henning Kamp 	 * the data.  When writing IO_DIRECT is not optimal.
6117e76bb56SMatthew Dillon 	 */
6128f8def9eSPoul-Henning Kamp 	if (bp->bio_cmd == BIO_READ) {
613cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
6145541f25eSPawel Jakub Dawidek 		error = VOP_READ(vp, &auio, IO_DIRECT, sc->cred);
61522db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
6168f8def9eSPoul-Henning Kamp 	} else {
6175541f25eSPawel Jakub Dawidek 		(void) vn_start_write(vp, &mp, V_WAIT);
618cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
6195541f25eSPawel Jakub Dawidek 		error = VOP_WRITE(vp, &auio, sc->flags & MD_ASYNC ? 0 : IO_SYNC,
6205541f25eSPawel Jakub Dawidek 		    sc->cred);
62122db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
6228f8def9eSPoul-Henning Kamp 		vn_finished_write(mp);
6238f8def9eSPoul-Henning Kamp 	}
6248f8def9eSPoul-Henning Kamp 	bp->bio_resid = auio.uio_resid;
625b4a4f93cSPoul-Henning Kamp 	return (error);
6268f8def9eSPoul-Henning Kamp }
6278f8def9eSPoul-Henning Kamp 
628b4a4f93cSPoul-Henning Kamp static int
629b4a4f93cSPoul-Henning Kamp mdstart_swap(struct md_s *sc, struct bio *bp)
6308f8def9eSPoul-Henning Kamp {
6317cd53fddSAlan Cox 	struct sf_buf *sf;
6326ab0a0aeSPawel Jakub Dawidek 	int rv, offs, len, lastend;
6336ab0a0aeSPawel Jakub Dawidek 	vm_pindex_t i, lastp;
6348e28326aSPoul-Henning Kamp 	vm_page_t m;
6358e28326aSPoul-Henning Kamp 	u_char *p;
6368f8def9eSPoul-Henning Kamp 
6375541f25eSPawel Jakub Dawidek 	switch (bp->bio_cmd) {
6385541f25eSPawel Jakub Dawidek 	case BIO_READ:
6395541f25eSPawel Jakub Dawidek 	case BIO_WRITE:
6405541f25eSPawel Jakub Dawidek 	case BIO_DELETE:
6415541f25eSPawel Jakub Dawidek 		break;
6425541f25eSPawel Jakub Dawidek 	default:
6435541f25eSPawel Jakub Dawidek 		return (EOPNOTSUPP);
6445541f25eSPawel Jakub Dawidek 	}
6455541f25eSPawel Jakub Dawidek 
6468e28326aSPoul-Henning Kamp 	p = bp->bio_data;
647e07113d6SColin Percival 
648e07113d6SColin Percival 	/*
6496c3cd0e2SMaxim Konovalov 	 * offs is the offset at which to start operating on the
650e07113d6SColin Percival 	 * next (ie, first) page.  lastp is the last page on
651e07113d6SColin Percival 	 * which we're going to operate.  lastend is the ending
652e07113d6SColin Percival 	 * position within that last page (ie, PAGE_SIZE if
653e07113d6SColin Percival 	 * we're operating on complete aligned pages).
654e07113d6SColin Percival 	 */
655e07113d6SColin Percival 	offs = bp->bio_offset % PAGE_SIZE;
656e07113d6SColin Percival 	lastp = (bp->bio_offset + bp->bio_length - 1) / PAGE_SIZE;
657e07113d6SColin Percival 	lastend = (bp->bio_offset + bp->bio_length - 1) % PAGE_SIZE + 1;
658e07113d6SColin Percival 
659812851b6SBrian Feldman 	rv = VM_PAGER_OK;
6608e28326aSPoul-Henning Kamp 	VM_OBJECT_LOCK(sc->object);
6618e28326aSPoul-Henning Kamp 	vm_object_pip_add(sc->object, 1);
662e07113d6SColin Percival 	for (i = bp->bio_offset / PAGE_SIZE; i <= lastp; i++) {
663e07113d6SColin Percival 		len = ((i == lastp) ? lastend : PAGE_SIZE) - offs;
664e07113d6SColin Percival 
665e07113d6SColin Percival 		m = vm_page_grab(sc->object, i,
6668e28326aSPoul-Henning Kamp 		    VM_ALLOC_NORMAL|VM_ALLOC_RETRY);
6677cd53fddSAlan Cox 		VM_OBJECT_UNLOCK(sc->object);
668e340fc60SAlan Cox 		sched_pin();
669e340fc60SAlan Cox 		sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
6707cd53fddSAlan Cox 		VM_OBJECT_LOCK(sc->object);
6718e28326aSPoul-Henning Kamp 		if (bp->bio_cmd == BIO_READ) {
67207be617fSAlan Cox 			if (m->valid != VM_PAGE_BITS_ALL)
67307be617fSAlan Cox 				rv = vm_pager_get_pages(sc->object, &m, 1, 0);
674812851b6SBrian Feldman 			if (rv == VM_PAGER_ERROR) {
675812851b6SBrian Feldman 				sf_buf_free(sf);
676e340fc60SAlan Cox 				sched_unpin();
677812851b6SBrian Feldman 				vm_page_wakeup(m);
678812851b6SBrian Feldman 				break;
679e9f581baSKonstantin Belousov 			} else if (rv == VM_PAGER_FAIL) {
680e9f581baSKonstantin Belousov 				/*
681e9f581baSKonstantin Belousov 				 * Pager does not have the page.  Zero
682e9f581baSKonstantin Belousov 				 * the allocated page, and mark it as
683e9f581baSKonstantin Belousov 				 * valid. Do not set dirty, the page
684e9f581baSKonstantin Belousov 				 * can be recreated if thrown out.
685e9f581baSKonstantin Belousov 				 */
686e9f581baSKonstantin Belousov 				bzero((void *)sf_buf_kva(sf), PAGE_SIZE);
687e9f581baSKonstantin Belousov 				m->valid = VM_PAGE_BITS_ALL;
688812851b6SBrian Feldman 			}
6897cd53fddSAlan Cox 			bcopy((void *)(sf_buf_kva(sf) + offs), p, len);
690dbb95048SMarcel Moolenaar 			cpu_flush_dcache(p, len);
6918e28326aSPoul-Henning Kamp 		} else if (bp->bio_cmd == BIO_WRITE) {
69207be617fSAlan Cox 			if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL)
69307be617fSAlan Cox 				rv = vm_pager_get_pages(sc->object, &m, 1, 0);
694812851b6SBrian Feldman 			if (rv == VM_PAGER_ERROR) {
695812851b6SBrian Feldman 				sf_buf_free(sf);
696e340fc60SAlan Cox 				sched_unpin();
697812851b6SBrian Feldman 				vm_page_wakeup(m);
698812851b6SBrian Feldman 				break;
699812851b6SBrian Feldman 			}
7007cd53fddSAlan Cox 			bcopy(p, (void *)(sf_buf_kva(sf) + offs), len);
7018e28326aSPoul-Henning Kamp 			m->valid = VM_PAGE_BITS_ALL;
7028e28326aSPoul-Henning Kamp 		} else if (bp->bio_cmd == BIO_DELETE) {
70307be617fSAlan Cox 			if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL)
70407be617fSAlan Cox 				rv = vm_pager_get_pages(sc->object, &m, 1, 0);
705812851b6SBrian Feldman 			if (rv == VM_PAGER_ERROR) {
706812851b6SBrian Feldman 				sf_buf_free(sf);
707e340fc60SAlan Cox 				sched_unpin();
708812851b6SBrian Feldman 				vm_page_wakeup(m);
709812851b6SBrian Feldman 				break;
710812851b6SBrian Feldman 			}
7114a13a769SKonstantin Belousov 			if (len != PAGE_SIZE) {
7127cd53fddSAlan Cox 				bzero((void *)(sf_buf_kva(sf) + offs), len);
7134a13a769SKonstantin Belousov 				vm_page_clear_dirty(m, offs, len);
7148e28326aSPoul-Henning Kamp 				m->valid = VM_PAGE_BITS_ALL;
7154a13a769SKonstantin Belousov 			} else
7164a13a769SKonstantin Belousov 				vm_pager_page_unswapped(m);
7178e28326aSPoul-Henning Kamp 		}
7187cd53fddSAlan Cox 		sf_buf_free(sf);
719e340fc60SAlan Cox 		sched_unpin();
7208e28326aSPoul-Henning Kamp 		vm_page_wakeup(m);
721fc0c3802SKonstantin Belousov 		vm_page_lock(m);
7224a13a769SKonstantin Belousov 		if (bp->bio_cmd == BIO_DELETE && len == PAGE_SIZE)
7234a13a769SKonstantin Belousov 			vm_page_free(m);
7244a13a769SKonstantin Belousov 		else
7258e28326aSPoul-Henning Kamp 			vm_page_activate(m);
726ecd5dd95SAlan Cox 		vm_page_unlock(m);
72707be617fSAlan Cox 		if (bp->bio_cmd == BIO_WRITE)
7288e28326aSPoul-Henning Kamp 			vm_page_dirty(m);
729e07113d6SColin Percival 
730e07113d6SColin Percival 		/* Actions on further pages start at offset 0 */
731e07113d6SColin Percival 		p += PAGE_SIZE - offs;
732e07113d6SColin Percival 		offs = 0;
7338e28326aSPoul-Henning Kamp 	}
7348e28326aSPoul-Henning Kamp 	vm_object_pip_subtract(sc->object, 1);
7358e28326aSPoul-Henning Kamp 	VM_OBJECT_UNLOCK(sc->object);
736812851b6SBrian Feldman 	return (rv != VM_PAGER_ERROR ? 0 : ENOSPC);
7378e28326aSPoul-Henning Kamp }
7388f8def9eSPoul-Henning Kamp 
7398f8def9eSPoul-Henning Kamp static void
7405c97ca54SIan Dowse md_kthread(void *arg)
7415c97ca54SIan Dowse {
7425c97ca54SIan Dowse 	struct md_s *sc;
7435c97ca54SIan Dowse 	struct bio *bp;
744a08d2e7fSJohn Baldwin 	int error;
7455c97ca54SIan Dowse 
7465c97ca54SIan Dowse 	sc = arg;
747982d11f8SJeff Roberson 	thread_lock(curthread);
74863710c4dSJohn Baldwin 	sched_prio(curthread, PRIBIO);
749982d11f8SJeff Roberson 	thread_unlock(curthread);
7503b7b5496SKonstantin Belousov 	if (sc->type == MD_VNODE)
7513b7b5496SKonstantin Belousov 		curthread->td_pflags |= TDP_NORUNNINGBUF;
7525c97ca54SIan Dowse 
753b4a4f93cSPoul-Henning Kamp 	for (;;) {
754a08d2e7fSJohn Baldwin 		mtx_lock(&sc->queue_mtx);
7555c97ca54SIan Dowse 		if (sc->flags & MD_SHUTDOWN) {
756a08d2e7fSJohn Baldwin 			sc->flags |= MD_EXITING;
757a08d2e7fSJohn Baldwin 			mtx_unlock(&sc->queue_mtx);
7583745c395SJulian Elischer 			kproc_exit(0);
7595c97ca54SIan Dowse 		}
7609b00ca19SPoul-Henning Kamp 		bp = bioq_takefirst(&sc->bio_queue);
7619b00ca19SPoul-Henning Kamp 		if (!bp) {
7620f8500a5SPoul-Henning Kamp 			msleep(sc, &sc->queue_mtx, PRIBIO | PDROP, "mdwait", 0);
7635c97ca54SIan Dowse 			continue;
7645c97ca54SIan Dowse 		}
7650f8500a5SPoul-Henning Kamp 		mtx_unlock(&sc->queue_mtx);
7664e8bfe14SPoul-Henning Kamp 		if (bp->bio_cmd == BIO_GETATTR) {
767d91e813cSKonstantin Belousov 			if ((sc->fwsectors && sc->fwheads &&
7684e8bfe14SPoul-Henning Kamp 			    (g_handleattr_int(bp, "GEOM::fwsectors",
7694e8bfe14SPoul-Henning Kamp 			    sc->fwsectors) ||
7704e8bfe14SPoul-Henning Kamp 			    g_handleattr_int(bp, "GEOM::fwheads",
771d91e813cSKonstantin Belousov 			    sc->fwheads))) ||
772d91e813cSKonstantin Belousov 			    g_handleattr_int(bp, "GEOM::candelete", 1))
7734e8bfe14SPoul-Henning Kamp 				error = -1;
7744e8bfe14SPoul-Henning Kamp 			else
7754e8bfe14SPoul-Henning Kamp 				error = EOPNOTSUPP;
7764e8bfe14SPoul-Henning Kamp 		} else {
7779b00ca19SPoul-Henning Kamp 			error = sc->start(sc, bp);
7784e8bfe14SPoul-Henning Kamp 		}
779b4a4f93cSPoul-Henning Kamp 
7806f4f00f1SPoul-Henning Kamp 		if (error != -1) {
7816f4f00f1SPoul-Henning Kamp 			bp->bio_completed = bp->bio_length;
782a03be42dSMaxim Sobolev 			if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE))
783a03be42dSMaxim Sobolev 				devstat_end_transaction_bio(sc->devstat, bp);
78496410b95SKonstantin Belousov 			g_io_deliver(bp, error);
785b4a4f93cSPoul-Henning Kamp 		}
7868f8def9eSPoul-Henning Kamp 	}
78726d48b40SPoul-Henning Kamp }
7888f8def9eSPoul-Henning Kamp 
7898f8def9eSPoul-Henning Kamp static struct md_s *
7908f8def9eSPoul-Henning Kamp mdfind(int unit)
7918f8def9eSPoul-Henning Kamp {
7928f8def9eSPoul-Henning Kamp 	struct md_s *sc;
7938f8def9eSPoul-Henning Kamp 
7943f54a085SPoul-Henning Kamp 	LIST_FOREACH(sc, &md_softc_list, list) {
7953f54a085SPoul-Henning Kamp 		if (sc->unit == unit)
7968f8def9eSPoul-Henning Kamp 			break;
7978f8def9eSPoul-Henning Kamp 	}
7988f8def9eSPoul-Henning Kamp 	return (sc);
7998f8def9eSPoul-Henning Kamp }
8008f8def9eSPoul-Henning Kamp 
8018f8def9eSPoul-Henning Kamp static struct md_s *
802947fc8deSPoul-Henning Kamp mdnew(int unit, int *errp, enum md_types type)
8038f8def9eSPoul-Henning Kamp {
804f4e7c5a8SJaakko Heinonen 	struct md_s *sc;
805f4e7c5a8SJaakko Heinonen 	int error;
8068f8def9eSPoul-Henning Kamp 
8079b00ca19SPoul-Henning Kamp 	*errp = 0;
808f4e7c5a8SJaakko Heinonen 	if (unit == -1)
809f4e7c5a8SJaakko Heinonen 		unit = alloc_unr(md_uh);
810f4e7c5a8SJaakko Heinonen 	else
811f4e7c5a8SJaakko Heinonen 		unit = alloc_unr_specific(md_uh, unit);
812f4e7c5a8SJaakko Heinonen 
813f4e7c5a8SJaakko Heinonen 	if (unit == -1) {
8147ee3c044SPawel Jakub Dawidek 		*errp = EBUSY;
8153f54a085SPoul-Henning Kamp 		return (NULL);
8163f54a085SPoul-Henning Kamp 	}
817f4e7c5a8SJaakko Heinonen 
8189b00ca19SPoul-Henning Kamp 	sc = (struct md_s *)malloc(sizeof *sc, M_MD, M_WAITOK | M_ZERO);
819947fc8deSPoul-Henning Kamp 	sc->type = type;
8209b00ca19SPoul-Henning Kamp 	bioq_init(&sc->bio_queue);
8219b00ca19SPoul-Henning Kamp 	mtx_init(&sc->queue_mtx, "md bio queue", NULL, MTX_DEF);
8223f54a085SPoul-Henning Kamp 	sc->unit = unit;
823f43b2bacSPoul-Henning Kamp 	sprintf(sc->name, "md%d", unit);
8247ee3c044SPawel Jakub Dawidek 	LIST_INSERT_HEAD(&md_softc_list, sc, list);
8253745c395SJulian Elischer 	error = kproc_create(md_kthread, sc, &sc->procp, 0, 0,"%s", sc->name);
8269b00ca19SPoul-Henning Kamp 	if (error == 0)
8279b00ca19SPoul-Henning Kamp 		return (sc);
8287ee3c044SPawel Jakub Dawidek 	LIST_REMOVE(sc, list);
8297ee3c044SPawel Jakub Dawidek 	mtx_destroy(&sc->queue_mtx);
830f4e7c5a8SJaakko Heinonen 	free_unr(md_uh, sc->unit);
8315c97ca54SIan Dowse 	free(sc, M_MD);
8327ee3c044SPawel Jakub Dawidek 	*errp = error;
8335c97ca54SIan Dowse 	return (NULL);
8345c97ca54SIan Dowse }
8358f8def9eSPoul-Henning Kamp 
8368f8def9eSPoul-Henning Kamp static void
8378f8def9eSPoul-Henning Kamp mdinit(struct md_s *sc)
8388f8def9eSPoul-Henning Kamp {
8396f4f00f1SPoul-Henning Kamp 	struct g_geom *gp;
8406f4f00f1SPoul-Henning Kamp 	struct g_provider *pp;
8416f4f00f1SPoul-Henning Kamp 
8426f4f00f1SPoul-Henning Kamp 	g_topology_lock();
8436f4f00f1SPoul-Henning Kamp 	gp = g_new_geomf(&g_md_class, "md%d", sc->unit);
8446f4f00f1SPoul-Henning Kamp 	gp->softc = sc;
8456f4f00f1SPoul-Henning Kamp 	pp = g_new_providerf(gp, "md%d", sc->unit);
846b830359bSPawel Jakub Dawidek 	pp->mediasize = sc->mediasize;
847b830359bSPawel Jakub Dawidek 	pp->sectorsize = sc->sectorsize;
8486f4f00f1SPoul-Henning Kamp 	sc->gp = gp;
8496f4f00f1SPoul-Henning Kamp 	sc->pp = pp;
8506f4f00f1SPoul-Henning Kamp 	g_error_provider(pp, 0);
8516f4f00f1SPoul-Henning Kamp 	g_topology_unlock();
852a03be42dSMaxim Sobolev 	sc->devstat = devstat_new_entry("md", sc->unit, sc->sectorsize,
853a03be42dSMaxim Sobolev 	    DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
8546f4f00f1SPoul-Henning Kamp }
85571e4fff8SPoul-Henning Kamp 
8568f8def9eSPoul-Henning Kamp static int
857b830359bSPawel Jakub Dawidek mdcreate_malloc(struct md_s *sc, struct md_ioctl *mdio)
85895f1a897SPoul-Henning Kamp {
859c6517568SPoul-Henning Kamp 	uintptr_t sp;
860c6517568SPoul-Henning Kamp 	int error;
861b830359bSPawel Jakub Dawidek 	off_t u;
86295f1a897SPoul-Henning Kamp 
863c6517568SPoul-Henning Kamp 	error = 0;
8648f8def9eSPoul-Henning Kamp 	if (mdio->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE))
8658f8def9eSPoul-Henning Kamp 		return (EINVAL);
866b830359bSPawel Jakub Dawidek 	if (mdio->md_sectorsize != 0 && !powerof2(mdio->md_sectorsize))
867ebe789d6SPoul-Henning Kamp 		return (EINVAL);
8688f8def9eSPoul-Henning Kamp 	/* Compression doesn't make sense if we have reserved space */
8698f8def9eSPoul-Henning Kamp 	if (mdio->md_options & MD_RESERVE)
8708f8def9eSPoul-Henning Kamp 		mdio->md_options &= ~MD_COMPRESS;
8714e8bfe14SPoul-Henning Kamp 	if (mdio->md_fwsectors != 0)
8724e8bfe14SPoul-Henning Kamp 		sc->fwsectors = mdio->md_fwsectors;
8734e8bfe14SPoul-Henning Kamp 	if (mdio->md_fwheads != 0)
8744e8bfe14SPoul-Henning Kamp 		sc->fwheads = mdio->md_fwheads;
87526a0ee75SDima Dorfman 	sc->flags = mdio->md_options & (MD_COMPRESS | MD_FORCE);
876b830359bSPawel Jakub Dawidek 	sc->indir = dimension(sc->mediasize / sc->sectorsize);
877b830359bSPawel Jakub Dawidek 	sc->uma = uma_zcreate(sc->name, sc->sectorsize, NULL, NULL, NULL, NULL,
878b830359bSPawel Jakub Dawidek 	    0x1ff, 0);
87996b6a55fSPoul-Henning Kamp 	if (mdio->md_options & MD_RESERVE) {
880b830359bSPawel Jakub Dawidek 		off_t nsectors;
881b830359bSPawel Jakub Dawidek 
882b830359bSPawel Jakub Dawidek 		nsectors = sc->mediasize / sc->sectorsize;
883b830359bSPawel Jakub Dawidek 		for (u = 0; u < nsectors; u++) {
884007777f1SKonstantin Belousov 			sp = (uintptr_t)uma_zalloc(sc->uma, (md_malloc_wait ?
885007777f1SKonstantin Belousov 			    M_WAITOK : M_NOWAIT) | M_ZERO);
886c6517568SPoul-Henning Kamp 			if (sp != 0)
887fde2a2e4SPoul-Henning Kamp 				error = s_write(sc->indir, u, sp);
888c6517568SPoul-Henning Kamp 			else
889c6517568SPoul-Henning Kamp 				error = ENOMEM;
890b830359bSPawel Jakub Dawidek 			if (error != 0)
891c6517568SPoul-Henning Kamp 				break;
8928f8def9eSPoul-Henning Kamp 		}
893c6517568SPoul-Henning Kamp 	}
894c6517568SPoul-Henning Kamp 	return (error);
89500a6a3c6SPoul-Henning Kamp }
89600a6a3c6SPoul-Henning Kamp 
8973f54a085SPoul-Henning Kamp 
8988f8def9eSPoul-Henning Kamp static int
8998f8def9eSPoul-Henning Kamp mdsetcred(struct md_s *sc, struct ucred *cred)
9008f8def9eSPoul-Henning Kamp {
9018f8def9eSPoul-Henning Kamp 	char *tmpbuf;
9028f8def9eSPoul-Henning Kamp 	int error = 0;
9038f8def9eSPoul-Henning Kamp 
9043f54a085SPoul-Henning Kamp 	/*
9058f8def9eSPoul-Henning Kamp 	 * Set credits in our softc
9063f54a085SPoul-Henning Kamp 	 */
9078f8def9eSPoul-Henning Kamp 
9088f8def9eSPoul-Henning Kamp 	if (sc->cred)
9098f8def9eSPoul-Henning Kamp 		crfree(sc->cred);
910bd78ceceSJohn Baldwin 	sc->cred = crhold(cred);
9118f8def9eSPoul-Henning Kamp 
9128f8def9eSPoul-Henning Kamp 	/*
9138f8def9eSPoul-Henning Kamp 	 * Horrible kludge to establish credentials for NFS  XXX.
9148f8def9eSPoul-Henning Kamp 	 */
9158f8def9eSPoul-Henning Kamp 
9168f8def9eSPoul-Henning Kamp 	if (sc->vnode) {
9178f8def9eSPoul-Henning Kamp 		struct uio auio;
9188f8def9eSPoul-Henning Kamp 		struct iovec aiov;
9198f8def9eSPoul-Henning Kamp 
920b830359bSPawel Jakub Dawidek 		tmpbuf = malloc(sc->sectorsize, M_TEMP, M_WAITOK);
9218f8def9eSPoul-Henning Kamp 		bzero(&auio, sizeof(auio));
9228f8def9eSPoul-Henning Kamp 
9238f8def9eSPoul-Henning Kamp 		aiov.iov_base = tmpbuf;
924b830359bSPawel Jakub Dawidek 		aiov.iov_len = sc->sectorsize;
9258f8def9eSPoul-Henning Kamp 		auio.uio_iov = &aiov;
9268f8def9eSPoul-Henning Kamp 		auio.uio_iovcnt = 1;
9278f8def9eSPoul-Henning Kamp 		auio.uio_offset = 0;
9288f8def9eSPoul-Henning Kamp 		auio.uio_rw = UIO_READ;
9298f8def9eSPoul-Henning Kamp 		auio.uio_segflg = UIO_SYSSPACE;
9308f8def9eSPoul-Henning Kamp 		auio.uio_resid = aiov.iov_len;
931cb05b60aSAttilio Rao 		vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY);
9328f8def9eSPoul-Henning Kamp 		error = VOP_READ(sc->vnode, &auio, 0, sc->cred);
93322db15c0SAttilio Rao 		VOP_UNLOCK(sc->vnode, 0);
9348f8def9eSPoul-Henning Kamp 		free(tmpbuf, M_TEMP);
9358f8def9eSPoul-Henning Kamp 	}
9368f8def9eSPoul-Henning Kamp 	return (error);
9378f8def9eSPoul-Henning Kamp }
9388f8def9eSPoul-Henning Kamp 
9398f8def9eSPoul-Henning Kamp static int
940b830359bSPawel Jakub Dawidek mdcreate_vnode(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
9418f8def9eSPoul-Henning Kamp {
9428f8def9eSPoul-Henning Kamp 	struct vattr vattr;
9438f8def9eSPoul-Henning Kamp 	struct nameidata nd;
9443d5c947dSMarcel Moolenaar 	char *fname;
9455050aa86SKonstantin Belousov 	int error, flags;
9468f8def9eSPoul-Henning Kamp 
9473d5c947dSMarcel Moolenaar 	/*
9483d5c947dSMarcel Moolenaar 	 * Kernel-originated requests must have the filename appended
9493d5c947dSMarcel Moolenaar 	 * to the mdio structure to protect against malicious software.
9503d5c947dSMarcel Moolenaar 	 */
9513d5c947dSMarcel Moolenaar 	fname = mdio->md_file;
9523d5c947dSMarcel Moolenaar 	if ((void *)fname != (void *)(mdio + 1)) {
9533d5c947dSMarcel Moolenaar 		error = copyinstr(fname, sc->file, sizeof(sc->file), NULL);
95488b5b78dSPawel Jakub Dawidek 		if (error != 0)
95588b5b78dSPawel Jakub Dawidek 			return (error);
9563d5c947dSMarcel Moolenaar 	} else
9573d5c947dSMarcel Moolenaar 		strlcpy(sc->file, fname, sizeof(sc->file));
9583d5c947dSMarcel Moolenaar 
95986776891SChristian S.J. Peron 	/*
9603d5c947dSMarcel Moolenaar 	 * If the user specified that this is a read only device, don't
9613d5c947dSMarcel Moolenaar 	 * set the FWRITE mask before trying to open the backing store.
96286776891SChristian S.J. Peron 	 */
9633d5c947dSMarcel Moolenaar 	flags = FREAD | ((mdio->md_options & MD_READONLY) ? 0 : FWRITE);
9645050aa86SKonstantin Belousov 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, sc->file, td);
9659e223287SKonstantin Belousov 	error = vn_open(&nd, &flags, 0, NULL);
966e3ed29a7SPawel Jakub Dawidek 	if (error != 0)
96752c6716fSPawel Jakub Dawidek 		return (error);
968b322d85dSPawel Jakub Dawidek 	NDFREE(&nd, NDF_ONLY_PNBUF);
96933fc3625SJohn Baldwin 	if (nd.ni_vp->v_type != VREG) {
97033fc3625SJohn Baldwin 		error = EINVAL;
97133fc3625SJohn Baldwin 		goto bad;
97233fc3625SJohn Baldwin 	}
97333fc3625SJohn Baldwin 	error = VOP_GETATTR(nd.ni_vp, &vattr, td->td_ucred);
97433fc3625SJohn Baldwin 	if (error != 0)
97533fc3625SJohn Baldwin 		goto bad;
97633fc3625SJohn Baldwin 	if (VOP_ISLOCKED(nd.ni_vp) != LK_EXCLUSIVE) {
97733fc3625SJohn Baldwin 		vn_lock(nd.ni_vp, LK_UPGRADE | LK_RETRY);
97833fc3625SJohn Baldwin 		if (nd.ni_vp->v_iflag & VI_DOOMED) {
97933fc3625SJohn Baldwin 			/* Forced unmount. */
98033fc3625SJohn Baldwin 			error = EBADF;
98133fc3625SJohn Baldwin 			goto bad;
98233fc3625SJohn Baldwin 		}
9838f8def9eSPoul-Henning Kamp 	}
9843b7b5496SKonstantin Belousov 	nd.ni_vp->v_vflag |= VV_MD;
98522db15c0SAttilio Rao 	VOP_UNLOCK(nd.ni_vp, 0);
9869589c256SPoul-Henning Kamp 
987d5a929dcSPoul-Henning Kamp 	if (mdio->md_fwsectors != 0)
988d5a929dcSPoul-Henning Kamp 		sc->fwsectors = mdio->md_fwsectors;
989d5a929dcSPoul-Henning Kamp 	if (mdio->md_fwheads != 0)
990d5a929dcSPoul-Henning Kamp 		sc->fwheads = mdio->md_fwheads;
9917a6b2b64SPoul-Henning Kamp 	sc->flags = mdio->md_options & (MD_FORCE | MD_ASYNC);
9929589c256SPoul-Henning Kamp 	if (!(flags & FWRITE))
9939589c256SPoul-Henning Kamp 		sc->flags |= MD_READONLY;
9948f8def9eSPoul-Henning Kamp 	sc->vnode = nd.ni_vp;
9958f8def9eSPoul-Henning Kamp 
996a854ed98SJohn Baldwin 	error = mdsetcred(sc, td->td_ucred);
997b830359bSPawel Jakub Dawidek 	if (error != 0) {
9983cf74e53SPhilip Paeps 		sc->vnode = NULL;
999cb05b60aSAttilio Rao 		vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY);
10003b7b5496SKonstantin Belousov 		nd.ni_vp->v_vflag &= ~VV_MD;
100133fc3625SJohn Baldwin 		goto bad;
100233fc3625SJohn Baldwin 	}
100333fc3625SJohn Baldwin 	return (0);
100433fc3625SJohn Baldwin bad:
100522db15c0SAttilio Rao 	VOP_UNLOCK(nd.ni_vp, 0);
100652c6716fSPawel Jakub Dawidek 	(void)vn_close(nd.ni_vp, flags, td->td_ucred, td);
10078f8def9eSPoul-Henning Kamp 	return (error);
10088f8def9eSPoul-Henning Kamp }
10098f8def9eSPoul-Henning Kamp 
10108f8def9eSPoul-Henning Kamp static int
1011b40ce416SJulian Elischer mddestroy(struct md_s *sc, struct thread *td)
10128f8def9eSPoul-Henning Kamp {
10130cddd8f0SMatthew Dillon 
10146f4f00f1SPoul-Henning Kamp 	if (sc->gp) {
10156f4f00f1SPoul-Henning Kamp 		sc->gp->softc = NULL;
10169b00ca19SPoul-Henning Kamp 		g_topology_lock();
10179b00ca19SPoul-Henning Kamp 		g_wither_geom(sc->gp, ENXIO);
10189b00ca19SPoul-Henning Kamp 		g_topology_unlock();
10196b60a2cdSPoul-Henning Kamp 		sc->gp = NULL;
10206b60a2cdSPoul-Henning Kamp 		sc->pp = NULL;
10211f4ee1aaSPoul-Henning Kamp 	}
1022a03be42dSMaxim Sobolev 	if (sc->devstat) {
1023a03be42dSMaxim Sobolev 		devstat_remove_entry(sc->devstat);
1024a03be42dSMaxim Sobolev 		sc->devstat = NULL;
1025a03be42dSMaxim Sobolev 	}
1026a08d2e7fSJohn Baldwin 	mtx_lock(&sc->queue_mtx);
10275c97ca54SIan Dowse 	sc->flags |= MD_SHUTDOWN;
10285c97ca54SIan Dowse 	wakeup(sc);
1029a08d2e7fSJohn Baldwin 	while (!(sc->flags & MD_EXITING))
1030a08d2e7fSJohn Baldwin 		msleep(sc->procp, &sc->queue_mtx, PRIBIO, "mddestroy", hz / 10);
1031a08d2e7fSJohn Baldwin 	mtx_unlock(&sc->queue_mtx);
10329fbea3e3SPoul-Henning Kamp 	mtx_destroy(&sc->queue_mtx);
10339b00ca19SPoul-Henning Kamp 	if (sc->vnode != NULL) {
1034cb05b60aSAttilio Rao 		vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY);
10353b7b5496SKonstantin Belousov 		sc->vnode->v_vflag &= ~VV_MD;
103622db15c0SAttilio Rao 		VOP_UNLOCK(sc->vnode, 0);
10379d4b5945SMaxim Sobolev 		(void)vn_close(sc->vnode, sc->flags & MD_READONLY ?
1038b40ce416SJulian Elischer 		    FREAD : (FREAD|FWRITE), sc->cred, td);
10399b00ca19SPoul-Henning Kamp 	}
10408f8def9eSPoul-Henning Kamp 	if (sc->cred != NULL)
10418f8def9eSPoul-Henning Kamp 		crfree(sc->cred);
10421db17c6dSPawel Jakub Dawidek 	if (sc->object != NULL)
1043f820bc50SAlan Cox 		vm_object_deallocate(sc->object);
1044f43b2bacSPoul-Henning Kamp 	if (sc->indir)
1045f43b2bacSPoul-Henning Kamp 		destroy_indir(sc, sc->indir);
1046f43b2bacSPoul-Henning Kamp 	if (sc->uma)
1047f43b2bacSPoul-Henning Kamp 		uma_zdestroy(sc->uma);
10481f4ee1aaSPoul-Henning Kamp 
10491f4ee1aaSPoul-Henning Kamp 	LIST_REMOVE(sc, list);
1050f4e7c5a8SJaakko Heinonen 	free_unr(md_uh, sc->unit);
1051c6517568SPoul-Henning Kamp 	free(sc, M_MD);
10528f8def9eSPoul-Henning Kamp 	return (0);
10538f8def9eSPoul-Henning Kamp }
10548f8def9eSPoul-Henning Kamp 
10558f8def9eSPoul-Henning Kamp static int
1056dc604f0cSEdward Tomasz Napierala mdresize(struct md_s *sc, struct md_ioctl *mdio)
1057dc604f0cSEdward Tomasz Napierala {
1058dc604f0cSEdward Tomasz Napierala 	int error, res;
1059dc604f0cSEdward Tomasz Napierala 	vm_pindex_t oldpages, newpages;
1060dc604f0cSEdward Tomasz Napierala 
1061dc604f0cSEdward Tomasz Napierala 	switch (sc->type) {
1062dc604f0cSEdward Tomasz Napierala 	case MD_VNODE:
1063dc604f0cSEdward Tomasz Napierala 		break;
1064dc604f0cSEdward Tomasz Napierala 	case MD_SWAP:
10658cb51643SJaakko Heinonen 		if (mdio->md_mediasize <= 0 ||
1066dc604f0cSEdward Tomasz Napierala 		    (mdio->md_mediasize % PAGE_SIZE) != 0)
1067dc604f0cSEdward Tomasz Napierala 			return (EDOM);
1068dc604f0cSEdward Tomasz Napierala 		oldpages = OFF_TO_IDX(round_page(sc->mediasize));
1069dc604f0cSEdward Tomasz Napierala 		newpages = OFF_TO_IDX(round_page(mdio->md_mediasize));
1070dc604f0cSEdward Tomasz Napierala 		if (newpages < oldpages) {
1071dc604f0cSEdward Tomasz Napierala 			VM_OBJECT_LOCK(sc->object);
1072dc604f0cSEdward Tomasz Napierala 			vm_object_page_remove(sc->object, newpages, 0, 0);
1073dc604f0cSEdward Tomasz Napierala 			swap_pager_freespace(sc->object, newpages,
1074dc604f0cSEdward Tomasz Napierala 			    oldpages - newpages);
1075dc604f0cSEdward Tomasz Napierala 			swap_release_by_cred(IDX_TO_OFF(oldpages -
1076dc604f0cSEdward Tomasz Napierala 			    newpages), sc->cred);
1077dc604f0cSEdward Tomasz Napierala 			sc->object->charge = IDX_TO_OFF(newpages);
1078dc604f0cSEdward Tomasz Napierala 			sc->object->size = newpages;
1079dc604f0cSEdward Tomasz Napierala 			VM_OBJECT_UNLOCK(sc->object);
1080dc604f0cSEdward Tomasz Napierala 		} else if (newpages > oldpages) {
1081dc604f0cSEdward Tomasz Napierala 			res = swap_reserve_by_cred(IDX_TO_OFF(newpages -
1082dc604f0cSEdward Tomasz Napierala 			    oldpages), sc->cred);
1083dc604f0cSEdward Tomasz Napierala 			if (!res)
1084dc604f0cSEdward Tomasz Napierala 				return (ENOMEM);
1085dc604f0cSEdward Tomasz Napierala 			if ((mdio->md_options & MD_RESERVE) ||
1086dc604f0cSEdward Tomasz Napierala 			    (sc->flags & MD_RESERVE)) {
1087dc604f0cSEdward Tomasz Napierala 				error = swap_pager_reserve(sc->object,
1088dc604f0cSEdward Tomasz Napierala 				    oldpages, newpages - oldpages);
1089dc604f0cSEdward Tomasz Napierala 				if (error < 0) {
1090dc604f0cSEdward Tomasz Napierala 					swap_release_by_cred(
1091dc604f0cSEdward Tomasz Napierala 					    IDX_TO_OFF(newpages - oldpages),
1092dc604f0cSEdward Tomasz Napierala 					    sc->cred);
1093dc604f0cSEdward Tomasz Napierala 					return (EDOM);
1094dc604f0cSEdward Tomasz Napierala 				}
1095dc604f0cSEdward Tomasz Napierala 			}
1096dc604f0cSEdward Tomasz Napierala 			VM_OBJECT_LOCK(sc->object);
1097dc604f0cSEdward Tomasz Napierala 			sc->object->charge = IDX_TO_OFF(newpages);
1098dc604f0cSEdward Tomasz Napierala 			sc->object->size = newpages;
1099dc604f0cSEdward Tomasz Napierala 			VM_OBJECT_UNLOCK(sc->object);
1100dc604f0cSEdward Tomasz Napierala 		}
1101dc604f0cSEdward Tomasz Napierala 		break;
1102dc604f0cSEdward Tomasz Napierala 	default:
1103dc604f0cSEdward Tomasz Napierala 		return (EOPNOTSUPP);
1104dc604f0cSEdward Tomasz Napierala 	}
1105dc604f0cSEdward Tomasz Napierala 
1106dc604f0cSEdward Tomasz Napierala 	sc->mediasize = mdio->md_mediasize;
1107dc604f0cSEdward Tomasz Napierala 	g_topology_lock();
1108dc604f0cSEdward Tomasz Napierala 	g_resize_provider(sc->pp, sc->mediasize);
1109dc604f0cSEdward Tomasz Napierala 	g_topology_unlock();
1110dc604f0cSEdward Tomasz Napierala 	return (0);
1111dc604f0cSEdward Tomasz Napierala }
1112dc604f0cSEdward Tomasz Napierala 
1113dc604f0cSEdward Tomasz Napierala static int
1114b830359bSPawel Jakub Dawidek mdcreate_swap(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
11158f8def9eSPoul-Henning Kamp {
1116fcd57fbeSPawel Jakub Dawidek 	vm_ooffset_t npage;
1117fcd57fbeSPawel Jakub Dawidek 	int error;
11188f8def9eSPoul-Henning Kamp 
11198f8def9eSPoul-Henning Kamp 	/*
11208f8def9eSPoul-Henning Kamp 	 * Range check.  Disallow negative sizes or any size less then the
11218f8def9eSPoul-Henning Kamp 	 * size of a page.  Then round to a page.
11228f8def9eSPoul-Henning Kamp 	 */
11238cb51643SJaakko Heinonen 	if (sc->mediasize <= 0 || (sc->mediasize % PAGE_SIZE) != 0)
11248f8def9eSPoul-Henning Kamp 		return (EDOM);
11258f8def9eSPoul-Henning Kamp 
11268f8def9eSPoul-Henning Kamp 	/*
11278f8def9eSPoul-Henning Kamp 	 * Allocate an OBJT_SWAP object.
11288f8def9eSPoul-Henning Kamp 	 *
11298f8def9eSPoul-Henning Kamp 	 * Note the truncation.
11308f8def9eSPoul-Henning Kamp 	 */
11318f8def9eSPoul-Henning Kamp 
1132b830359bSPawel Jakub Dawidek 	npage = mdio->md_mediasize / PAGE_SIZE;
11339ed40643SPoul-Henning Kamp 	if (mdio->md_fwsectors != 0)
11349ed40643SPoul-Henning Kamp 		sc->fwsectors = mdio->md_fwsectors;
11359ed40643SPoul-Henning Kamp 	if (mdio->md_fwheads != 0)
11369ed40643SPoul-Henning Kamp 		sc->fwheads = mdio->md_fwheads;
1137fcd57fbeSPawel Jakub Dawidek 	sc->object = vm_pager_allocate(OBJT_SWAP, NULL, PAGE_SIZE * npage,
11383364c323SKonstantin Belousov 	    VM_PROT_DEFAULT, 0, td->td_ucred);
1139812851b6SBrian Feldman 	if (sc->object == NULL)
1140812851b6SBrian Feldman 		return (ENOMEM);
1141dc604f0cSEdward Tomasz Napierala 	sc->flags = mdio->md_options & (MD_FORCE | MD_RESERVE);
11428f8def9eSPoul-Henning Kamp 	if (mdio->md_options & MD_RESERVE) {
1143fcd57fbeSPawel Jakub Dawidek 		if (swap_pager_reserve(sc->object, 0, npage) < 0) {
11443364c323SKonstantin Belousov 			error = EDOM;
11453364c323SKonstantin Belousov 			goto finish;
11468f8def9eSPoul-Henning Kamp 		}
11478f8def9eSPoul-Henning Kamp 	}
1148a854ed98SJohn Baldwin 	error = mdsetcred(sc, td->td_ucred);
11493364c323SKonstantin Belousov  finish:
1150e3ed29a7SPawel Jakub Dawidek 	if (error != 0) {
11512eafd8b1SPawel Jakub Dawidek 		vm_object_deallocate(sc->object);
11522eafd8b1SPawel Jakub Dawidek 		sc->object = NULL;
11538f8def9eSPoul-Henning Kamp 	}
1154b830359bSPawel Jakub Dawidek 	return (error);
1155b3b3d1b7SPoul-Henning Kamp }
11568f8def9eSPoul-Henning Kamp 
11579d4b5945SMaxim Sobolev 
11589d4b5945SMaxim Sobolev static int
11599b00ca19SPoul-Henning Kamp xmdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
11608f8def9eSPoul-Henning Kamp {
11618f8def9eSPoul-Henning Kamp 	struct md_ioctl *mdio;
11628f8def9eSPoul-Henning Kamp 	struct md_s *sc;
1163b830359bSPawel Jakub Dawidek 	int error, i;
11648cb51643SJaakko Heinonen 	unsigned sectsize;
11658f8def9eSPoul-Henning Kamp 
11668f8def9eSPoul-Henning Kamp 	if (md_debug)
11678f8def9eSPoul-Henning Kamp 		printf("mdctlioctl(%s %lx %p %x %p)\n",
1168b40ce416SJulian Elischer 			devtoname(dev), cmd, addr, flags, td);
11698f8def9eSPoul-Henning Kamp 
11709b00ca19SPoul-Henning Kamp 	mdio = (struct md_ioctl *)addr;
11719b00ca19SPoul-Henning Kamp 	if (mdio->md_version != MDIOVERSION)
11729b00ca19SPoul-Henning Kamp 		return (EINVAL);
11739b00ca19SPoul-Henning Kamp 
117453d745bcSDima Dorfman 	/*
117553d745bcSDima Dorfman 	 * We assert the version number in the individual ioctl
117653d745bcSDima Dorfman 	 * handlers instead of out here because (a) it is possible we
117753d745bcSDima Dorfman 	 * may add another ioctl in the future which doesn't read an
117853d745bcSDima Dorfman 	 * mdio, and (b) the correct return value for an unknown ioctl
117953d745bcSDima Dorfman 	 * is ENOIOCTL, not EINVAL.
118053d745bcSDima Dorfman 	 */
11819b00ca19SPoul-Henning Kamp 	error = 0;
11828f8def9eSPoul-Henning Kamp 	switch (cmd) {
11838f8def9eSPoul-Henning Kamp 	case MDIOCATTACH:
11848f8def9eSPoul-Henning Kamp 		switch (mdio->md_type) {
11858f8def9eSPoul-Henning Kamp 		case MD_MALLOC:
11868f8def9eSPoul-Henning Kamp 		case MD_PRELOAD:
11878f8def9eSPoul-Henning Kamp 		case MD_VNODE:
11888f8def9eSPoul-Henning Kamp 		case MD_SWAP:
1189b830359bSPawel Jakub Dawidek 			break;
11908f8def9eSPoul-Henning Kamp 		default:
11918f8def9eSPoul-Henning Kamp 			return (EINVAL);
11928f8def9eSPoul-Henning Kamp 		}
11938cb51643SJaakko Heinonen 		if (mdio->md_sectorsize == 0)
11948cb51643SJaakko Heinonen 			sectsize = DEV_BSIZE;
11958cb51643SJaakko Heinonen 		else
11968cb51643SJaakko Heinonen 			sectsize = mdio->md_sectorsize;
11978cb51643SJaakko Heinonen 		if (sectsize > MAXPHYS || mdio->md_mediasize < sectsize)
11988cb51643SJaakko Heinonen 			return (EINVAL);
11997ee3c044SPawel Jakub Dawidek 		if (mdio->md_options & MD_AUTOUNIT)
1200947fc8deSPoul-Henning Kamp 			sc = mdnew(-1, &error, mdio->md_type);
1201f4e7c5a8SJaakko Heinonen 		else {
1202f4e7c5a8SJaakko Heinonen 			if (mdio->md_unit > INT_MAX)
1203f4e7c5a8SJaakko Heinonen 				return (EINVAL);
1204947fc8deSPoul-Henning Kamp 			sc = mdnew(mdio->md_unit, &error, mdio->md_type);
1205f4e7c5a8SJaakko Heinonen 		}
1206b830359bSPawel Jakub Dawidek 		if (sc == NULL)
12077ee3c044SPawel Jakub Dawidek 			return (error);
12087ee3c044SPawel Jakub Dawidek 		if (mdio->md_options & MD_AUTOUNIT)
12097ee3c044SPawel Jakub Dawidek 			mdio->md_unit = sc->unit;
1210b830359bSPawel Jakub Dawidek 		sc->mediasize = mdio->md_mediasize;
12118cb51643SJaakko Heinonen 		sc->sectorsize = sectsize;
1212b830359bSPawel Jakub Dawidek 		error = EDOOFUS;
1213b830359bSPawel Jakub Dawidek 		switch (sc->type) {
1214b830359bSPawel Jakub Dawidek 		case MD_MALLOC:
12159b00ca19SPoul-Henning Kamp 			sc->start = mdstart_malloc;
1216b830359bSPawel Jakub Dawidek 			error = mdcreate_malloc(sc, mdio);
1217b830359bSPawel Jakub Dawidek 			break;
1218b830359bSPawel Jakub Dawidek 		case MD_PRELOAD:
1219734e78dfSJaakko Heinonen 			/*
1220734e78dfSJaakko Heinonen 			 * We disallow attaching preloaded memory disks via
1221734e78dfSJaakko Heinonen 			 * ioctl. Preloaded memory disks are automatically
1222734e78dfSJaakko Heinonen 			 * attached in g_md_init().
1223734e78dfSJaakko Heinonen 			 */
1224734e78dfSJaakko Heinonen 			error = EOPNOTSUPP;
1225b830359bSPawel Jakub Dawidek 			break;
1226b830359bSPawel Jakub Dawidek 		case MD_VNODE:
12279b00ca19SPoul-Henning Kamp 			sc->start = mdstart_vnode;
1228b830359bSPawel Jakub Dawidek 			error = mdcreate_vnode(sc, mdio, td);
1229b830359bSPawel Jakub Dawidek 			break;
1230b830359bSPawel Jakub Dawidek 		case MD_SWAP:
12319b00ca19SPoul-Henning Kamp 			sc->start = mdstart_swap;
1232b830359bSPawel Jakub Dawidek 			error = mdcreate_swap(sc, mdio, td);
1233b830359bSPawel Jakub Dawidek 			break;
1234b830359bSPawel Jakub Dawidek 		}
1235b830359bSPawel Jakub Dawidek 		if (error != 0) {
1236b830359bSPawel Jakub Dawidek 			mddestroy(sc, td);
1237b830359bSPawel Jakub Dawidek 			return (error);
1238b830359bSPawel Jakub Dawidek 		}
12399b00ca19SPoul-Henning Kamp 
12409b00ca19SPoul-Henning Kamp 		/* Prune off any residual fractional sector */
12419b00ca19SPoul-Henning Kamp 		i = sc->mediasize % sc->sectorsize;
12429b00ca19SPoul-Henning Kamp 		sc->mediasize -= i;
12439b00ca19SPoul-Henning Kamp 
1244b830359bSPawel Jakub Dawidek 		mdinit(sc);
1245b830359bSPawel Jakub Dawidek 		return (0);
12468f8def9eSPoul-Henning Kamp 	case MDIOCDETACH:
1247a9ebb311SEdward Tomasz Napierala 		if (mdio->md_mediasize != 0 ||
1248a9ebb311SEdward Tomasz Napierala 		    (mdio->md_options & ~MD_FORCE) != 0)
12498f8def9eSPoul-Henning Kamp 			return (EINVAL);
12509b00ca19SPoul-Henning Kamp 
12519b00ca19SPoul-Henning Kamp 		sc = mdfind(mdio->md_unit);
12529b00ca19SPoul-Henning Kamp 		if (sc == NULL)
12539b00ca19SPoul-Henning Kamp 			return (ENOENT);
1254a9ebb311SEdward Tomasz Napierala 		if (sc->opencount != 0 && !(sc->flags & MD_FORCE) &&
1255a9ebb311SEdward Tomasz Napierala 		    !(mdio->md_options & MD_FORCE))
12569b00ca19SPoul-Henning Kamp 			return (EBUSY);
12579b00ca19SPoul-Henning Kamp 		return (mddestroy(sc, td));
1258dc604f0cSEdward Tomasz Napierala 	case MDIOCRESIZE:
1259dc604f0cSEdward Tomasz Napierala 		if ((mdio->md_options & ~(MD_FORCE | MD_RESERVE)) != 0)
1260dc604f0cSEdward Tomasz Napierala 			return (EINVAL);
1261dc604f0cSEdward Tomasz Napierala 
1262dc604f0cSEdward Tomasz Napierala 		sc = mdfind(mdio->md_unit);
1263dc604f0cSEdward Tomasz Napierala 		if (sc == NULL)
1264dc604f0cSEdward Tomasz Napierala 			return (ENOENT);
12658cb51643SJaakko Heinonen 		if (mdio->md_mediasize < sc->sectorsize)
12668cb51643SJaakko Heinonen 			return (EINVAL);
1267dc604f0cSEdward Tomasz Napierala 		if (mdio->md_mediasize < sc->mediasize &&
1268dc604f0cSEdward Tomasz Napierala 		    !(sc->flags & MD_FORCE) &&
1269dc604f0cSEdward Tomasz Napierala 		    !(mdio->md_options & MD_FORCE))
1270dc604f0cSEdward Tomasz Napierala 			return (EBUSY);
1271dc604f0cSEdward Tomasz Napierala 		return (mdresize(sc, mdio));
1272174b5e9aSPoul-Henning Kamp 	case MDIOCQUERY:
1273174b5e9aSPoul-Henning Kamp 		sc = mdfind(mdio->md_unit);
1274174b5e9aSPoul-Henning Kamp 		if (sc == NULL)
1275174b5e9aSPoul-Henning Kamp 			return (ENOENT);
1276174b5e9aSPoul-Henning Kamp 		mdio->md_type = sc->type;
1277174b5e9aSPoul-Henning Kamp 		mdio->md_options = sc->flags;
1278b830359bSPawel Jakub Dawidek 		mdio->md_mediasize = sc->mediasize;
1279b830359bSPawel Jakub Dawidek 		mdio->md_sectorsize = sc->sectorsize;
12809b00ca19SPoul-Henning Kamp 		if (sc->type == MD_VNODE)
128188b5b78dSPawel Jakub Dawidek 			error = copyout(sc->file, mdio->md_file,
128288b5b78dSPawel Jakub Dawidek 			    strlen(sc->file) + 1);
128388b5b78dSPawel Jakub Dawidek 		return (error);
128416bcbe8cSPoul-Henning Kamp 	case MDIOCLIST:
128516bcbe8cSPoul-Henning Kamp 		i = 1;
128616bcbe8cSPoul-Henning Kamp 		LIST_FOREACH(sc, &md_softc_list, list) {
128716bcbe8cSPoul-Henning Kamp 			if (i == MDNPAD - 1)
128816bcbe8cSPoul-Henning Kamp 				mdio->md_pad[i] = -1;
128916bcbe8cSPoul-Henning Kamp 			else
129016bcbe8cSPoul-Henning Kamp 				mdio->md_pad[i++] = sc->unit;
129116bcbe8cSPoul-Henning Kamp 		}
129216bcbe8cSPoul-Henning Kamp 		mdio->md_pad[0] = i - 1;
129316bcbe8cSPoul-Henning Kamp 		return (0);
12948f8def9eSPoul-Henning Kamp 	default:
12958f8def9eSPoul-Henning Kamp 		return (ENOIOCTL);
12968f8def9eSPoul-Henning Kamp 	};
12979b00ca19SPoul-Henning Kamp }
12989b00ca19SPoul-Henning Kamp 
12999b00ca19SPoul-Henning Kamp static int
13009b00ca19SPoul-Henning Kamp mdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
13019b00ca19SPoul-Henning Kamp {
13029b00ca19SPoul-Henning Kamp 	int error;
13039b00ca19SPoul-Henning Kamp 
13049b00ca19SPoul-Henning Kamp 	sx_xlock(&md_sx);
13059b00ca19SPoul-Henning Kamp 	error = xmdctlioctl(dev, cmd, addr, flags, td);
13069b00ca19SPoul-Henning Kamp 	sx_xunlock(&md_sx);
13079b00ca19SPoul-Henning Kamp 	return (error);
13083f54a085SPoul-Henning Kamp }
13093f54a085SPoul-Henning Kamp 
131000a6a3c6SPoul-Henning Kamp static void
1311*341b240dSJaakko Heinonen md_preloaded(u_char *image, size_t length, const char *name)
1312637f671aSPoul-Henning Kamp {
1313637f671aSPoul-Henning Kamp 	struct md_s *sc;
13149b00ca19SPoul-Henning Kamp 	int error;
1315637f671aSPoul-Henning Kamp 
1316947fc8deSPoul-Henning Kamp 	sc = mdnew(-1, &error, MD_PRELOAD);
1317637f671aSPoul-Henning Kamp 	if (sc == NULL)
1318637f671aSPoul-Henning Kamp 		return;
1319b830359bSPawel Jakub Dawidek 	sc->mediasize = length;
1320b830359bSPawel Jakub Dawidek 	sc->sectorsize = DEV_BSIZE;
1321637f671aSPoul-Henning Kamp 	sc->pl_ptr = image;
1322637f671aSPoul-Henning Kamp 	sc->pl_len = length;
13239b00ca19SPoul-Henning Kamp 	sc->start = mdstart_preload;
13245d4ca75eSLuigi Rizzo #ifdef MD_ROOT
1325637f671aSPoul-Henning Kamp 	if (sc->unit == 0)
132622ff74b2SMarcel Moolenaar 		rootdevnames[0] = MD_ROOT_FSTYPE ":/dev/md0";
13275d4ca75eSLuigi Rizzo #endif
1328637f671aSPoul-Henning Kamp 	mdinit(sc);
1329*341b240dSJaakko Heinonen 	if (name != NULL) {
1330*341b240dSJaakko Heinonen 		printf("%s%d: Preloaded image <%s> %zd bytes at %p\n",
1331*341b240dSJaakko Heinonen 		    MD_NAME, sc->unit, name, length, image);
1332*341b240dSJaakko Heinonen 	}
1333637f671aSPoul-Henning Kamp }
1334637f671aSPoul-Henning Kamp 
1335637f671aSPoul-Henning Kamp static void
133619945697SPoul-Henning Kamp g_md_init(struct g_class *mp __unused)
133700a6a3c6SPoul-Henning Kamp {
133895f1a897SPoul-Henning Kamp 	caddr_t mod;
133995f1a897SPoul-Henning Kamp 	u_char *ptr, *name, *type;
134095f1a897SPoul-Henning Kamp 	unsigned len;
1341d12fc952SKonstantin Belousov 	int i;
1342d12fc952SKonstantin Belousov 
1343d12fc952SKonstantin Belousov 	/* figure out log2(NINDIR) */
1344d12fc952SKonstantin Belousov 	for (i = NINDIR, nshift = -1; i; nshift++)
1345d12fc952SKonstantin Belousov 		i >>= 1;
134695f1a897SPoul-Henning Kamp 
13470a937206SPoul-Henning Kamp 	mod = NULL;
13489b00ca19SPoul-Henning Kamp 	sx_init(&md_sx, "MD config lock");
13490a937206SPoul-Henning Kamp 	g_topology_unlock();
1350f4e7c5a8SJaakko Heinonen 	md_uh = new_unrhdr(0, INT_MAX, NULL);
135171e4fff8SPoul-Henning Kamp #ifdef MD_ROOT_SIZE
13529b00ca19SPoul-Henning Kamp 	sx_xlock(&md_sx);
1353*341b240dSJaakko Heinonen 	md_preloaded(mfs_root.start, sizeof(mfs_root.start), NULL);
13549b00ca19SPoul-Henning Kamp 	sx_xunlock(&md_sx);
135571e4fff8SPoul-Henning Kamp #endif
13569b00ca19SPoul-Henning Kamp 	/* XXX: are preload_* static or do they need Giant ? */
135795f1a897SPoul-Henning Kamp 	while ((mod = preload_search_next_name(mod)) != NULL) {
135895f1a897SPoul-Henning Kamp 		name = (char *)preload_search_info(mod, MODINFO_NAME);
135995f1a897SPoul-Henning Kamp 		if (name == NULL)
136095f1a897SPoul-Henning Kamp 			continue;
13619b00ca19SPoul-Henning Kamp 		type = (char *)preload_search_info(mod, MODINFO_TYPE);
136295f1a897SPoul-Henning Kamp 		if (type == NULL)
136395f1a897SPoul-Henning Kamp 			continue;
136471e4fff8SPoul-Henning Kamp 		if (strcmp(type, "md_image") && strcmp(type, "mfs_root"))
136595f1a897SPoul-Henning Kamp 			continue;
13668d5ac6c3SMarcel Moolenaar 		ptr = preload_fetch_addr(mod);
13678d5ac6c3SMarcel Moolenaar 		len = preload_fetch_size(mod);
13688d5ac6c3SMarcel Moolenaar 		if (ptr != NULL && len != 0) {
13699b00ca19SPoul-Henning Kamp 			sx_xlock(&md_sx);
1370*341b240dSJaakko Heinonen 			md_preloaded(ptr, len, name);
13719b00ca19SPoul-Henning Kamp 			sx_xunlock(&md_sx);
137295f1a897SPoul-Henning Kamp 		}
13738d5ac6c3SMarcel Moolenaar 	}
137406d425f9SEd Schouten 	status_dev = make_dev(&mdctl_cdevsw, INT_MAX, UID_ROOT, GID_WHEEL,
137510b0e058SDima Dorfman 	    0600, MDCTL_NAME);
13760eb14309SPoul-Henning Kamp 	g_topology_lock();
137700a6a3c6SPoul-Henning Kamp }
137800a6a3c6SPoul-Henning Kamp 
137919945697SPoul-Henning Kamp static void
1380c27a8954SWojciech A. Koszek g_md_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1381c27a8954SWojciech A. Koszek     struct g_consumer *cp __unused, struct g_provider *pp)
1382c27a8954SWojciech A. Koszek {
1383c27a8954SWojciech A. Koszek 	struct md_s *mp;
1384c27a8954SWojciech A. Koszek 	char *type;
1385c27a8954SWojciech A. Koszek 
1386c27a8954SWojciech A. Koszek 	mp = gp->softc;
1387c27a8954SWojciech A. Koszek 	if (mp == NULL)
1388c27a8954SWojciech A. Koszek 		return;
1389c27a8954SWojciech A. Koszek 
1390c27a8954SWojciech A. Koszek 	switch (mp->type) {
1391c27a8954SWojciech A. Koszek 	case MD_MALLOC:
1392c27a8954SWojciech A. Koszek 		type = "malloc";
1393c27a8954SWojciech A. Koszek 		break;
1394c27a8954SWojciech A. Koszek 	case MD_PRELOAD:
1395c27a8954SWojciech A. Koszek 		type = "preload";
1396c27a8954SWojciech A. Koszek 		break;
1397c27a8954SWojciech A. Koszek 	case MD_VNODE:
1398c27a8954SWojciech A. Koszek 		type = "vnode";
1399c27a8954SWojciech A. Koszek 		break;
1400c27a8954SWojciech A. Koszek 	case MD_SWAP:
1401c27a8954SWojciech A. Koszek 		type = "swap";
1402c27a8954SWojciech A. Koszek 		break;
1403c27a8954SWojciech A. Koszek 	default:
1404c27a8954SWojciech A. Koszek 		type = "unknown";
1405c27a8954SWojciech A. Koszek 		break;
1406c27a8954SWojciech A. Koszek 	}
1407c27a8954SWojciech A. Koszek 
1408c27a8954SWojciech A. Koszek 	if (pp != NULL) {
1409c27a8954SWojciech A. Koszek 		if (indent == NULL) {
1410c27a8954SWojciech A. Koszek 			sbuf_printf(sb, " u %d", mp->unit);
1411c27a8954SWojciech A. Koszek 			sbuf_printf(sb, " s %ju", (uintmax_t) mp->sectorsize);
1412c27a8954SWojciech A. Koszek 			sbuf_printf(sb, " f %ju", (uintmax_t) mp->fwheads);
1413c27a8954SWojciech A. Koszek 			sbuf_printf(sb, " fs %ju", (uintmax_t) mp->fwsectors);
1414c27a8954SWojciech A. Koszek 			sbuf_printf(sb, " l %ju", (uintmax_t) mp->mediasize);
1415c27a8954SWojciech A. Koszek 			sbuf_printf(sb, " t %s", type);
1416c27a8954SWojciech A. Koszek 			if (mp->type == MD_VNODE && mp->vnode != NULL)
1417c27a8954SWojciech A. Koszek 				sbuf_printf(sb, " file %s", mp->file);
1418c27a8954SWojciech A. Koszek 		} else {
1419c27a8954SWojciech A. Koszek 			sbuf_printf(sb, "%s<unit>%d</unit>\n", indent,
1420c27a8954SWojciech A. Koszek 			    mp->unit);
1421c27a8954SWojciech A. Koszek 			sbuf_printf(sb, "%s<sectorsize>%ju</sectorsize>\n",
1422c27a8954SWojciech A. Koszek 			    indent, (uintmax_t) mp->sectorsize);
1423c27a8954SWojciech A. Koszek 			sbuf_printf(sb, "%s<fwheads>%ju</fwheads>\n",
1424c27a8954SWojciech A. Koszek 			    indent, (uintmax_t) mp->fwheads);
1425c27a8954SWojciech A. Koszek 			sbuf_printf(sb, "%s<fwsectors>%ju</fwsectors>\n",
1426c27a8954SWojciech A. Koszek 			    indent, (uintmax_t) mp->fwsectors);
1427c27a8954SWojciech A. Koszek 			sbuf_printf(sb, "%s<length>%ju</length>\n",
1428c27a8954SWojciech A. Koszek 			    indent, (uintmax_t) mp->mediasize);
14291f192809SAndrey V. Elsukov 			sbuf_printf(sb, "%s<compression>%s</compression>\n", indent,
14301f192809SAndrey V. Elsukov 			    (mp->flags & MD_COMPRESS) == 0 ? "off": "on");
14311f192809SAndrey V. Elsukov 			sbuf_printf(sb, "%s<access>%s</access>\n", indent,
14321f192809SAndrey V. Elsukov 			    (mp->flags & MD_READONLY) == 0 ? "read-write":
14331f192809SAndrey V. Elsukov 			    "read-only");
1434c27a8954SWojciech A. Koszek 			sbuf_printf(sb, "%s<type>%s</type>\n", indent,
1435c27a8954SWojciech A. Koszek 			    type);
1436c27a8954SWojciech A. Koszek 			if (mp->type == MD_VNODE && mp->vnode != NULL)
1437c27a8954SWojciech A. Koszek 				sbuf_printf(sb, "%s<file>%s</file>\n",
1438c27a8954SWojciech A. Koszek 				    indent, mp->file);
1439c27a8954SWojciech A. Koszek 		}
1440c27a8954SWojciech A. Koszek 	}
1441c27a8954SWojciech A. Koszek }
1442c27a8954SWojciech A. Koszek 
1443c27a8954SWojciech A. Koszek static void
144419945697SPoul-Henning Kamp g_md_fini(struct g_class *mp __unused)
144557e9624eSPoul-Henning Kamp {
14469d4b5945SMaxim Sobolev 
14479b00ca19SPoul-Henning Kamp 	sx_destroy(&md_sx);
144819945697SPoul-Henning Kamp 	if (status_dev != NULL)
144957e9624eSPoul-Henning Kamp 		destroy_dev(status_dev);
1450f4e7c5a8SJaakko Heinonen 	delete_unrhdr(md_uh);
145157e9624eSPoul-Henning Kamp }
1452