xref: /freebsd/sys/dev/md/md.c (revision 40ea77a036cd80d17111bd00f470fac57c77f07c)
1098ca2bdSWarner Losh /*-
200a6a3c6SPoul-Henning Kamp  * ----------------------------------------------------------------------------
300a6a3c6SPoul-Henning Kamp  * "THE BEER-WARE LICENSE" (Revision 42):
400a6a3c6SPoul-Henning Kamp  * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
500a6a3c6SPoul-Henning Kamp  * can do whatever you want with this stuff. If we meet some day, and you think
600a6a3c6SPoul-Henning Kamp  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
700a6a3c6SPoul-Henning Kamp  * ----------------------------------------------------------------------------
800a6a3c6SPoul-Henning Kamp  *
900a6a3c6SPoul-Henning Kamp  * $FreeBSD$
1000a6a3c6SPoul-Henning Kamp  *
1100a6a3c6SPoul-Henning Kamp  */
1200a6a3c6SPoul-Henning Kamp 
13098ca2bdSWarner Losh /*-
14637f671aSPoul-Henning Kamp  * The following functions are based in the vn(4) driver: mdstart_swap(),
15637f671aSPoul-Henning Kamp  * mdstart_vnode(), mdcreate_swap(), mdcreate_vnode() and mddestroy(),
16637f671aSPoul-Henning Kamp  * and as such under the following copyright:
17637f671aSPoul-Henning Kamp  *
18637f671aSPoul-Henning Kamp  * Copyright (c) 1988 University of Utah.
19637f671aSPoul-Henning Kamp  * Copyright (c) 1990, 1993
20637f671aSPoul-Henning Kamp  *	The Regents of the University of California.  All rights reserved.
2159ec9023SKonstantin Belousov  * Copyright (c) 2013 The FreeBSD Foundation
2259ec9023SKonstantin Belousov  * All rights reserved.
23637f671aSPoul-Henning Kamp  *
24ed010cdfSWarner Losh  * This code is derived from software contributed to Berkeley by
25ed010cdfSWarner Losh  * the Systems Programming Group of the University of Utah Computer
26ed010cdfSWarner Losh  * Science Department.
27ed010cdfSWarner Losh  *
2859ec9023SKonstantin Belousov  * Portions of this software were developed by Konstantin Belousov
2959ec9023SKonstantin Belousov  * under sponsorship from the FreeBSD Foundation.
3059ec9023SKonstantin Belousov  *
31637f671aSPoul-Henning Kamp  * Redistribution and use in source and binary forms, with or without
32637f671aSPoul-Henning Kamp  * modification, are permitted provided that the following conditions
33637f671aSPoul-Henning Kamp  * are met:
34637f671aSPoul-Henning Kamp  * 1. Redistributions of source code must retain the above copyright
35637f671aSPoul-Henning Kamp  *    notice, this list of conditions and the following disclaimer.
36637f671aSPoul-Henning Kamp  * 2. Redistributions in binary form must reproduce the above copyright
37637f671aSPoul-Henning Kamp  *    notice, this list of conditions and the following disclaimer in the
38637f671aSPoul-Henning Kamp  *    documentation and/or other materials provided with the distribution.
39637f671aSPoul-Henning Kamp  * 4. Neither the name of the University nor the names of its contributors
40637f671aSPoul-Henning Kamp  *    may be used to endorse or promote products derived from this software
41637f671aSPoul-Henning Kamp  *    without specific prior written permission.
42637f671aSPoul-Henning Kamp  *
43637f671aSPoul-Henning Kamp  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44637f671aSPoul-Henning Kamp  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45637f671aSPoul-Henning Kamp  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46637f671aSPoul-Henning Kamp  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47637f671aSPoul-Henning Kamp  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48637f671aSPoul-Henning Kamp  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49637f671aSPoul-Henning Kamp  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50637f671aSPoul-Henning Kamp  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51637f671aSPoul-Henning Kamp  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52637f671aSPoul-Henning Kamp  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53637f671aSPoul-Henning Kamp  * SUCH DAMAGE.
54637f671aSPoul-Henning Kamp  *
55637f671aSPoul-Henning Kamp  * from: Utah Hdr: vn.c 1.13 94/04/02
56637f671aSPoul-Henning Kamp  *
57637f671aSPoul-Henning Kamp  *	from: @(#)vn.c	8.6 (Berkeley) 4/1/94
58637f671aSPoul-Henning Kamp  * From: src/sys/dev/vn/vn.c,v 1.122 2000/12/16 16:06:03
59637f671aSPoul-Henning Kamp  */
60637f671aSPoul-Henning Kamp 
616f4f00f1SPoul-Henning Kamp #include "opt_geom.h"
623f54a085SPoul-Henning Kamp #include "opt_md.h"
6371e4fff8SPoul-Henning Kamp 
6400a6a3c6SPoul-Henning Kamp #include <sys/param.h>
6500a6a3c6SPoul-Henning Kamp #include <sys/systm.h>
669626b608SPoul-Henning Kamp #include <sys/bio.h>
6759ec9023SKonstantin Belousov #include <sys/buf.h>
6800a6a3c6SPoul-Henning Kamp #include <sys/conf.h>
69a03be42dSMaxim Sobolev #include <sys/devicestat.h>
708f8def9eSPoul-Henning Kamp #include <sys/fcntl.h>
71fb919e4dSMark Murray #include <sys/kernel.h>
725c97ca54SIan Dowse #include <sys/kthread.h>
7306d425f9SEd Schouten #include <sys/limits.h>
74fb919e4dSMark Murray #include <sys/linker.h>
75fb919e4dSMark Murray #include <sys/lock.h>
76fb919e4dSMark Murray #include <sys/malloc.h>
77fb919e4dSMark Murray #include <sys/mdioctl.h>
78a08d2e7fSJohn Baldwin #include <sys/mount.h>
799dceb26bSJohn Baldwin #include <sys/mutex.h>
809b00ca19SPoul-Henning Kamp #include <sys/sx.h>
81fb919e4dSMark Murray #include <sys/namei.h>
828f8def9eSPoul-Henning Kamp #include <sys/proc.h>
83fb919e4dSMark Murray #include <sys/queue.h>
8489f6b863SAttilio Rao #include <sys/rwlock.h>
85657bd8b1SAndrey V. Elsukov #include <sys/sbuf.h>
8663710c4dSJohn Baldwin #include <sys/sched.h>
877cd53fddSAlan Cox #include <sys/sf_buf.h>
88fb919e4dSMark Murray #include <sys/sysctl.h>
89fb919e4dSMark Murray #include <sys/vnode.h>
90fb919e4dSMark Murray 
916f4f00f1SPoul-Henning Kamp #include <geom/geom.h>
926f4f00f1SPoul-Henning Kamp 
938f8def9eSPoul-Henning Kamp #include <vm/vm.h>
941c771f92SKonstantin Belousov #include <vm/vm_param.h>
958f8def9eSPoul-Henning Kamp #include <vm/vm_object.h>
968f8def9eSPoul-Henning Kamp #include <vm/vm_page.h>
978f8def9eSPoul-Henning Kamp #include <vm/vm_pager.h>
988f8def9eSPoul-Henning Kamp #include <vm/swap_pager.h>
99f43b2bacSPoul-Henning Kamp #include <vm/uma.h>
1003f54a085SPoul-Henning Kamp 
10157e9624eSPoul-Henning Kamp #define MD_MODVER 1
10257e9624eSPoul-Henning Kamp 
1035c97ca54SIan Dowse #define MD_SHUTDOWN	0x10000		/* Tell worker thread to terminate. */
104a08d2e7fSJohn Baldwin #define	MD_EXITING	0x20000		/* Worker thread is exiting. */
1055c97ca54SIan Dowse 
106f2744793SSheldon Hearn #ifndef MD_NSECT
107f2744793SSheldon Hearn #define MD_NSECT (10000 * 2)
10833edfabeSPoul-Henning Kamp #endif
10933edfabeSPoul-Henning Kamp 
1105bb84bc8SRobert Watson static MALLOC_DEFINE(M_MD, "md_disk", "Memory Disk");
1115bb84bc8SRobert Watson static MALLOC_DEFINE(M_MDSECT, "md_sectors", "Memory Disk Sectors");
11200a6a3c6SPoul-Henning Kamp 
11371e4fff8SPoul-Henning Kamp static int md_debug;
1143eb9ab52SEitan Adler SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0,
1153eb9ab52SEitan Adler     "Enable md(4) debug messages");
116c44d423eSKonstantin Belousov static int md_malloc_wait;
1173eb9ab52SEitan Adler SYSCTL_INT(_vm, OID_AUTO, md_malloc_wait, CTLFLAG_RW, &md_malloc_wait, 0,
1183eb9ab52SEitan Adler     "Allow malloc to wait for memory allocations");
11900a6a3c6SPoul-Henning Kamp 
12022ff74b2SMarcel Moolenaar #if defined(MD_ROOT) && !defined(MD_ROOT_FSTYPE)
12122ff74b2SMarcel Moolenaar #define	MD_ROOT_FSTYPE	"ufs"
12222ff74b2SMarcel Moolenaar #endif
12322ff74b2SMarcel Moolenaar 
12471e4fff8SPoul-Henning Kamp #if defined(MD_ROOT) && defined(MD_ROOT_SIZE)
125de64f22aSLuigi Rizzo /*
126de64f22aSLuigi Rizzo  * Preloaded image gets put here.
127de64f22aSLuigi Rizzo  * Applications that patch the object with the image can determine
128de64f22aSLuigi Rizzo  * the size looking at the start and end markers (strings),
129de64f22aSLuigi Rizzo  * so we want them contiguous.
130de64f22aSLuigi Rizzo  */
131de64f22aSLuigi Rizzo static struct {
132de64f22aSLuigi Rizzo 	u_char start[MD_ROOT_SIZE*1024];
133de64f22aSLuigi Rizzo 	u_char end[128];
134de64f22aSLuigi Rizzo } mfs_root = {
135de64f22aSLuigi Rizzo 	.start = "MFS Filesystem goes here",
136de64f22aSLuigi Rizzo 	.end = "MFS Filesystem had better STOP here",
137de64f22aSLuigi Rizzo };
13871e4fff8SPoul-Henning Kamp #endif
13971e4fff8SPoul-Henning Kamp 
14019945697SPoul-Henning Kamp static g_init_t g_md_init;
14119945697SPoul-Henning Kamp static g_fini_t g_md_fini;
14219945697SPoul-Henning Kamp static g_start_t g_md_start;
14319945697SPoul-Henning Kamp static g_access_t g_md_access;
144b42f40b8SJaakko Heinonen static void g_md_dumpconf(struct sbuf *sb, const char *indent,
145b42f40b8SJaakko Heinonen     struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp);
1460eb14309SPoul-Henning Kamp 
14789c9c53dSPoul-Henning Kamp static struct cdev *status_dev = 0;
1489b00ca19SPoul-Henning Kamp static struct sx md_sx;
149f4e7c5a8SJaakko Heinonen static struct unrhdr *md_uh;
15057e9624eSPoul-Henning Kamp 
151a522a159SPoul-Henning Kamp static d_ioctl_t mdctlioctl;
1528f8def9eSPoul-Henning Kamp 
1538f8def9eSPoul-Henning Kamp static struct cdevsw mdctl_cdevsw = {
154dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
1557ac40f5fSPoul-Henning Kamp 	.d_ioctl =	mdctlioctl,
1567ac40f5fSPoul-Henning Kamp 	.d_name =	MD_NAME,
15700a6a3c6SPoul-Henning Kamp };
15800a6a3c6SPoul-Henning Kamp 
15919945697SPoul-Henning Kamp struct g_class g_md_class = {
16019945697SPoul-Henning Kamp 	.name = "MD",
1615721c9c7SPoul-Henning Kamp 	.version = G_VERSION,
16219945697SPoul-Henning Kamp 	.init = g_md_init,
16319945697SPoul-Henning Kamp 	.fini = g_md_fini,
16419945697SPoul-Henning Kamp 	.start = g_md_start,
16519945697SPoul-Henning Kamp 	.access = g_md_access,
166c27a8954SWojciech A. Koszek 	.dumpconf = g_md_dumpconf,
16719945697SPoul-Henning Kamp };
16819945697SPoul-Henning Kamp 
16919945697SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_md_class, g_md);
17019945697SPoul-Henning Kamp 
1710cfaeeeeSPoul-Henning Kamp 
17213e403fdSAntoine Brodin static LIST_HEAD(, md_s) md_softc_list = LIST_HEAD_INITIALIZER(md_softc_list);
1733f54a085SPoul-Henning Kamp 
174c6517568SPoul-Henning Kamp #define NINDIR	(PAGE_SIZE / sizeof(uintptr_t))
175c6517568SPoul-Henning Kamp #define NMASK	(NINDIR-1)
176c6517568SPoul-Henning Kamp static int nshift;
177c6517568SPoul-Henning Kamp 
17859ec9023SKonstantin Belousov static int md_vnode_pbuf_freecnt;
17959ec9023SKonstantin Belousov 
180c6517568SPoul-Henning Kamp struct indir {
181c6517568SPoul-Henning Kamp 	uintptr_t	*array;
1828b149b51SJohn Baldwin 	u_int		total;
1838b149b51SJohn Baldwin 	u_int		used;
1848b149b51SJohn Baldwin 	u_int		shift;
185c6517568SPoul-Henning Kamp };
186c6517568SPoul-Henning Kamp 
18700a6a3c6SPoul-Henning Kamp struct md_s {
18800a6a3c6SPoul-Henning Kamp 	int unit;
1893f54a085SPoul-Henning Kamp 	LIST_ENTRY(md_s) list;
1908177437dSPoul-Henning Kamp 	struct bio_queue_head bio_queue;
1910f8500a5SPoul-Henning Kamp 	struct mtx queue_mtx;
192*40ea77a0SAlexander Motin 	struct mtx stat_mtx;
19389c9c53dSPoul-Henning Kamp 	struct cdev *dev;
1948f8def9eSPoul-Henning Kamp 	enum md_types type;
195b830359bSPawel Jakub Dawidek 	off_t mediasize;
196b830359bSPawel Jakub Dawidek 	unsigned sectorsize;
197fe603109SMaxim Sobolev 	unsigned opencount;
1984e8bfe14SPoul-Henning Kamp 	unsigned fwheads;
1994e8bfe14SPoul-Henning Kamp 	unsigned fwsectors;
2008f8def9eSPoul-Henning Kamp 	unsigned flags;
201f43b2bacSPoul-Henning Kamp 	char name[20];
2025c97ca54SIan Dowse 	struct proc *procp;
2036f4f00f1SPoul-Henning Kamp 	struct g_geom *gp;
2046f4f00f1SPoul-Henning Kamp 	struct g_provider *pp;
2059b00ca19SPoul-Henning Kamp 	int (*start)(struct md_s *sc, struct bio *bp);
206a03be42dSMaxim Sobolev 	struct devstat *devstat;
20795f1a897SPoul-Henning Kamp 
20895f1a897SPoul-Henning Kamp 	/* MD_MALLOC related fields */
209c6517568SPoul-Henning Kamp 	struct indir *indir;
210f43b2bacSPoul-Henning Kamp 	uma_zone_t uma;
21100a6a3c6SPoul-Henning Kamp 
21295f1a897SPoul-Henning Kamp 	/* MD_PRELOAD related fields */
21395f1a897SPoul-Henning Kamp 	u_char *pl_ptr;
214b830359bSPawel Jakub Dawidek 	size_t pl_len;
21500a6a3c6SPoul-Henning Kamp 
2168f8def9eSPoul-Henning Kamp 	/* MD_VNODE related fields */
2178f8def9eSPoul-Henning Kamp 	struct vnode *vnode;
21861a6eb62SPawel Jakub Dawidek 	char file[PATH_MAX];
2198f8def9eSPoul-Henning Kamp 	struct ucred *cred;
2208f8def9eSPoul-Henning Kamp 
221e0cebb40SDima Dorfman 	/* MD_SWAP related fields */
2228f8def9eSPoul-Henning Kamp 	vm_object_t object;
2238f8def9eSPoul-Henning Kamp };
22400a6a3c6SPoul-Henning Kamp 
225c6517568SPoul-Henning Kamp static struct indir *
2268b149b51SJohn Baldwin new_indir(u_int shift)
227c6517568SPoul-Henning Kamp {
228c6517568SPoul-Henning Kamp 	struct indir *ip;
229c6517568SPoul-Henning Kamp 
230c44d423eSKonstantin Belousov 	ip = malloc(sizeof *ip, M_MD, (md_malloc_wait ? M_WAITOK : M_NOWAIT)
231c44d423eSKonstantin Belousov 	    | M_ZERO);
232c6517568SPoul-Henning Kamp 	if (ip == NULL)
233c6517568SPoul-Henning Kamp 		return (NULL);
234c6517568SPoul-Henning Kamp 	ip->array = malloc(sizeof(uintptr_t) * NINDIR,
235c44d423eSKonstantin Belousov 	    M_MDSECT, (md_malloc_wait ? M_WAITOK : M_NOWAIT) | M_ZERO);
236c6517568SPoul-Henning Kamp 	if (ip->array == NULL) {
237c6517568SPoul-Henning Kamp 		free(ip, M_MD);
238c6517568SPoul-Henning Kamp 		return (NULL);
239c6517568SPoul-Henning Kamp 	}
240c6517568SPoul-Henning Kamp 	ip->total = NINDIR;
241c6517568SPoul-Henning Kamp 	ip->shift = shift;
242c6517568SPoul-Henning Kamp 	return (ip);
243c6517568SPoul-Henning Kamp }
244c6517568SPoul-Henning Kamp 
245c6517568SPoul-Henning Kamp static void
246c6517568SPoul-Henning Kamp del_indir(struct indir *ip)
247c6517568SPoul-Henning Kamp {
248c6517568SPoul-Henning Kamp 
249f43b2bacSPoul-Henning Kamp 	free(ip->array, M_MDSECT);
250c6517568SPoul-Henning Kamp 	free(ip, M_MD);
251c6517568SPoul-Henning Kamp }
252c6517568SPoul-Henning Kamp 
253f43b2bacSPoul-Henning Kamp static void
254f43b2bacSPoul-Henning Kamp destroy_indir(struct md_s *sc, struct indir *ip)
255f43b2bacSPoul-Henning Kamp {
256f43b2bacSPoul-Henning Kamp 	int i;
257f43b2bacSPoul-Henning Kamp 
258f43b2bacSPoul-Henning Kamp 	for (i = 0; i < NINDIR; i++) {
259f43b2bacSPoul-Henning Kamp 		if (!ip->array[i])
260f43b2bacSPoul-Henning Kamp 			continue;
261f43b2bacSPoul-Henning Kamp 		if (ip->shift)
262f43b2bacSPoul-Henning Kamp 			destroy_indir(sc, (struct indir*)(ip->array[i]));
263f43b2bacSPoul-Henning Kamp 		else if (ip->array[i] > 255)
264f43b2bacSPoul-Henning Kamp 			uma_zfree(sc->uma, (void *)(ip->array[i]));
265f43b2bacSPoul-Henning Kamp 	}
266f43b2bacSPoul-Henning Kamp 	del_indir(ip);
267f43b2bacSPoul-Henning Kamp }
268f43b2bacSPoul-Henning Kamp 
269c6517568SPoul-Henning Kamp /*
2706c3cd0e2SMaxim Konovalov  * This function does the math and allocates the top level "indir" structure
271c6517568SPoul-Henning Kamp  * for a device of "size" sectors.
272c6517568SPoul-Henning Kamp  */
273c6517568SPoul-Henning Kamp 
274c6517568SPoul-Henning Kamp static struct indir *
275c6517568SPoul-Henning Kamp dimension(off_t size)
276c6517568SPoul-Henning Kamp {
277c6517568SPoul-Henning Kamp 	off_t rcnt;
278c6517568SPoul-Henning Kamp 	struct indir *ip;
279d12fc952SKonstantin Belousov 	int layer;
280c6517568SPoul-Henning Kamp 
281c6517568SPoul-Henning Kamp 	rcnt = size;
282c6517568SPoul-Henning Kamp 	layer = 0;
283c6517568SPoul-Henning Kamp 	while (rcnt > NINDIR) {
284c6517568SPoul-Henning Kamp 		rcnt /= NINDIR;
285c6517568SPoul-Henning Kamp 		layer++;
286c6517568SPoul-Henning Kamp 	}
287c6517568SPoul-Henning Kamp 
288c6517568SPoul-Henning Kamp 	/*
289c6517568SPoul-Henning Kamp 	 * XXX: the top layer is probably not fully populated, so we allocate
29083e13864SPoul-Henning Kamp 	 * too much space for ip->array in here.
291c6517568SPoul-Henning Kamp 	 */
29283e13864SPoul-Henning Kamp 	ip = malloc(sizeof *ip, M_MD, M_WAITOK | M_ZERO);
29383e13864SPoul-Henning Kamp 	ip->array = malloc(sizeof(uintptr_t) * NINDIR,
29483e13864SPoul-Henning Kamp 	    M_MDSECT, M_WAITOK | M_ZERO);
29583e13864SPoul-Henning Kamp 	ip->total = NINDIR;
29683e13864SPoul-Henning Kamp 	ip->shift = layer * nshift;
297c6517568SPoul-Henning Kamp 	return (ip);
298c6517568SPoul-Henning Kamp }
299c6517568SPoul-Henning Kamp 
300c6517568SPoul-Henning Kamp /*
301c6517568SPoul-Henning Kamp  * Read a given sector
302c6517568SPoul-Henning Kamp  */
303c6517568SPoul-Henning Kamp 
304c6517568SPoul-Henning Kamp static uintptr_t
305c6517568SPoul-Henning Kamp s_read(struct indir *ip, off_t offset)
306c6517568SPoul-Henning Kamp {
307c6517568SPoul-Henning Kamp 	struct indir *cip;
308c6517568SPoul-Henning Kamp 	int idx;
309c6517568SPoul-Henning Kamp 	uintptr_t up;
310c6517568SPoul-Henning Kamp 
311c6517568SPoul-Henning Kamp 	if (md_debug > 1)
3126569d6f3SMaxime Henrion 		printf("s_read(%jd)\n", (intmax_t)offset);
313c6517568SPoul-Henning Kamp 	up = 0;
314c6517568SPoul-Henning Kamp 	for (cip = ip; cip != NULL;) {
315c6517568SPoul-Henning Kamp 		if (cip->shift) {
316c6517568SPoul-Henning Kamp 			idx = (offset >> cip->shift) & NMASK;
317c6517568SPoul-Henning Kamp 			up = cip->array[idx];
318c6517568SPoul-Henning Kamp 			cip = (struct indir *)up;
319c6517568SPoul-Henning Kamp 			continue;
320c6517568SPoul-Henning Kamp 		}
321c6517568SPoul-Henning Kamp 		idx = offset & NMASK;
322c6517568SPoul-Henning Kamp 		return (cip->array[idx]);
323c6517568SPoul-Henning Kamp 	}
324c6517568SPoul-Henning Kamp 	return (0);
325c6517568SPoul-Henning Kamp }
326c6517568SPoul-Henning Kamp 
327c6517568SPoul-Henning Kamp /*
328c6517568SPoul-Henning Kamp  * Write a given sector, prune the tree if the value is 0
329c6517568SPoul-Henning Kamp  */
330c6517568SPoul-Henning Kamp 
331c6517568SPoul-Henning Kamp static int
332fde2a2e4SPoul-Henning Kamp s_write(struct indir *ip, off_t offset, uintptr_t ptr)
333c6517568SPoul-Henning Kamp {
334c6517568SPoul-Henning Kamp 	struct indir *cip, *lip[10];
335c6517568SPoul-Henning Kamp 	int idx, li;
336c6517568SPoul-Henning Kamp 	uintptr_t up;
337c6517568SPoul-Henning Kamp 
338c6517568SPoul-Henning Kamp 	if (md_debug > 1)
3396569d6f3SMaxime Henrion 		printf("s_write(%jd, %p)\n", (intmax_t)offset, (void *)ptr);
340c6517568SPoul-Henning Kamp 	up = 0;
341c6517568SPoul-Henning Kamp 	li = 0;
342c6517568SPoul-Henning Kamp 	cip = ip;
343c6517568SPoul-Henning Kamp 	for (;;) {
344c6517568SPoul-Henning Kamp 		lip[li++] = cip;
345c6517568SPoul-Henning Kamp 		if (cip->shift) {
346c6517568SPoul-Henning Kamp 			idx = (offset >> cip->shift) & NMASK;
347c6517568SPoul-Henning Kamp 			up = cip->array[idx];
348c6517568SPoul-Henning Kamp 			if (up != 0) {
349c6517568SPoul-Henning Kamp 				cip = (struct indir *)up;
350c6517568SPoul-Henning Kamp 				continue;
351c6517568SPoul-Henning Kamp 			}
352c6517568SPoul-Henning Kamp 			/* Allocate branch */
353c6517568SPoul-Henning Kamp 			cip->array[idx] =
354c6517568SPoul-Henning Kamp 			    (uintptr_t)new_indir(cip->shift - nshift);
355c6517568SPoul-Henning Kamp 			if (cip->array[idx] == 0)
356975b628fSPoul-Henning Kamp 				return (ENOSPC);
357c6517568SPoul-Henning Kamp 			cip->used++;
358c6517568SPoul-Henning Kamp 			up = cip->array[idx];
359c6517568SPoul-Henning Kamp 			cip = (struct indir *)up;
360c6517568SPoul-Henning Kamp 			continue;
361c6517568SPoul-Henning Kamp 		}
362c6517568SPoul-Henning Kamp 		/* leafnode */
363c6517568SPoul-Henning Kamp 		idx = offset & NMASK;
364c6517568SPoul-Henning Kamp 		up = cip->array[idx];
365c6517568SPoul-Henning Kamp 		if (up != 0)
366c6517568SPoul-Henning Kamp 			cip->used--;
367c6517568SPoul-Henning Kamp 		cip->array[idx] = ptr;
368c6517568SPoul-Henning Kamp 		if (ptr != 0)
369c6517568SPoul-Henning Kamp 			cip->used++;
370c6517568SPoul-Henning Kamp 		break;
371c6517568SPoul-Henning Kamp 	}
372c6517568SPoul-Henning Kamp 	if (cip->used != 0 || li == 1)
373c6517568SPoul-Henning Kamp 		return (0);
374c6517568SPoul-Henning Kamp 	li--;
375c6517568SPoul-Henning Kamp 	while (cip->used == 0 && cip != ip) {
376c6517568SPoul-Henning Kamp 		li--;
377c6517568SPoul-Henning Kamp 		idx = (offset >> lip[li]->shift) & NMASK;
378c6517568SPoul-Henning Kamp 		up = lip[li]->array[idx];
379c6517568SPoul-Henning Kamp 		KASSERT(up == (uintptr_t)cip, ("md screwed up"));
380c6517568SPoul-Henning Kamp 		del_indir(cip);
3814a6a94d8SArchie Cobbs 		lip[li]->array[idx] = 0;
382c6517568SPoul-Henning Kamp 		lip[li]->used--;
383c6517568SPoul-Henning Kamp 		cip = lip[li];
384c6517568SPoul-Henning Kamp 	}
385c6517568SPoul-Henning Kamp 	return (0);
386c6517568SPoul-Henning Kamp }
387c6517568SPoul-Henning Kamp 
3886f4f00f1SPoul-Henning Kamp 
3896f4f00f1SPoul-Henning Kamp static int
3906f4f00f1SPoul-Henning Kamp g_md_access(struct g_provider *pp, int r, int w, int e)
3916f4f00f1SPoul-Henning Kamp {
3926f4f00f1SPoul-Henning Kamp 	struct md_s *sc;
3936f4f00f1SPoul-Henning Kamp 
3946f4f00f1SPoul-Henning Kamp 	sc = pp->geom->softc;
39541c8b468SEdward Tomasz Napierala 	if (sc == NULL) {
39641c8b468SEdward Tomasz Napierala 		if (r <= 0 && w <= 0 && e <= 0)
39741c8b468SEdward Tomasz Napierala 			return (0);
3986b60a2cdSPoul-Henning Kamp 		return (ENXIO);
39941c8b468SEdward Tomasz Napierala 	}
4006f4f00f1SPoul-Henning Kamp 	r += pp->acr;
4016f4f00f1SPoul-Henning Kamp 	w += pp->acw;
4026f4f00f1SPoul-Henning Kamp 	e += pp->ace;
40386776891SChristian S.J. Peron 	if ((sc->flags & MD_READONLY) != 0 && w > 0)
40486776891SChristian S.J. Peron 		return (EROFS);
4056f4f00f1SPoul-Henning Kamp 	if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
4066f4f00f1SPoul-Henning Kamp 		sc->opencount = 1;
4076f4f00f1SPoul-Henning Kamp 	} else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
4086f4f00f1SPoul-Henning Kamp 		sc->opencount = 0;
4096f4f00f1SPoul-Henning Kamp 	}
4106f4f00f1SPoul-Henning Kamp 	return (0);
4116f4f00f1SPoul-Henning Kamp }
4126f4f00f1SPoul-Henning Kamp 
4136f4f00f1SPoul-Henning Kamp static void
4146f4f00f1SPoul-Henning Kamp g_md_start(struct bio *bp)
4156f4f00f1SPoul-Henning Kamp {
4166f4f00f1SPoul-Henning Kamp 	struct md_s *sc;
4176f4f00f1SPoul-Henning Kamp 
4186f4f00f1SPoul-Henning Kamp 	sc = bp->bio_to->geom->softc;
419*40ea77a0SAlexander Motin 	if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE)) {
420*40ea77a0SAlexander Motin 		mtx_lock(&sc->stat_mtx);
421a03be42dSMaxim Sobolev 		devstat_start_transaction_bio(sc->devstat, bp);
422*40ea77a0SAlexander Motin 		mtx_unlock(&sc->stat_mtx);
423*40ea77a0SAlexander Motin 	}
4240f8500a5SPoul-Henning Kamp 	mtx_lock(&sc->queue_mtx);
425891619a6SPoul-Henning Kamp 	bioq_disksort(&sc->bio_queue, bp);
4264b07ede4SPawel Jakub Dawidek 	mtx_unlock(&sc->queue_mtx);
427e4cdd0d4SPawel Jakub Dawidek 	wakeup(sc);
4286f4f00f1SPoul-Henning Kamp }
4296f4f00f1SPoul-Henning Kamp 
43059ec9023SKonstantin Belousov #define	MD_MALLOC_MOVE_ZERO	1
43159ec9023SKonstantin Belousov #define	MD_MALLOC_MOVE_FILL	2
43259ec9023SKonstantin Belousov #define	MD_MALLOC_MOVE_READ	3
43359ec9023SKonstantin Belousov #define	MD_MALLOC_MOVE_WRITE	4
43459ec9023SKonstantin Belousov #define	MD_MALLOC_MOVE_CMP	5
43559ec9023SKonstantin Belousov 
43659ec9023SKonstantin Belousov static int
43759ec9023SKonstantin Belousov md_malloc_move(vm_page_t **mp, int *ma_offs, unsigned sectorsize,
43859ec9023SKonstantin Belousov     void *ptr, u_char fill, int op)
43959ec9023SKonstantin Belousov {
44059ec9023SKonstantin Belousov 	struct sf_buf *sf;
44159ec9023SKonstantin Belousov 	vm_page_t m, *mp1;
44259ec9023SKonstantin Belousov 	char *p, first;
44359ec9023SKonstantin Belousov 	off_t *uc;
44459ec9023SKonstantin Belousov 	unsigned n;
44559ec9023SKonstantin Belousov 	int error, i, ma_offs1, sz, first_read;
44659ec9023SKonstantin Belousov 
44759ec9023SKonstantin Belousov 	m = NULL;
44859ec9023SKonstantin Belousov 	error = 0;
44959ec9023SKonstantin Belousov 	sf = NULL;
45059ec9023SKonstantin Belousov 	/* if (op == MD_MALLOC_MOVE_CMP) { gcc */
45159ec9023SKonstantin Belousov 		first = 0;
45259ec9023SKonstantin Belousov 		first_read = 0;
45359ec9023SKonstantin Belousov 		uc = ptr;
45459ec9023SKonstantin Belousov 		mp1 = *mp;
45559ec9023SKonstantin Belousov 		ma_offs1 = *ma_offs;
45659ec9023SKonstantin Belousov 	/* } */
45759ec9023SKonstantin Belousov 	sched_pin();
45859ec9023SKonstantin Belousov 	for (n = sectorsize; n != 0; n -= sz) {
45959ec9023SKonstantin Belousov 		sz = imin(PAGE_SIZE - *ma_offs, n);
46059ec9023SKonstantin Belousov 		if (m != **mp) {
46159ec9023SKonstantin Belousov 			if (sf != NULL)
46259ec9023SKonstantin Belousov 				sf_buf_free(sf);
46359ec9023SKonstantin Belousov 			m = **mp;
46459ec9023SKonstantin Belousov 			sf = sf_buf_alloc(m, SFB_CPUPRIVATE |
46559ec9023SKonstantin Belousov 			    (md_malloc_wait ? 0 : SFB_NOWAIT));
46659ec9023SKonstantin Belousov 			if (sf == NULL) {
46759ec9023SKonstantin Belousov 				error = ENOMEM;
46859ec9023SKonstantin Belousov 				break;
46959ec9023SKonstantin Belousov 			}
47059ec9023SKonstantin Belousov 		}
47159ec9023SKonstantin Belousov 		p = (char *)sf_buf_kva(sf) + *ma_offs;
47259ec9023SKonstantin Belousov 		switch (op) {
47359ec9023SKonstantin Belousov 		case MD_MALLOC_MOVE_ZERO:
47459ec9023SKonstantin Belousov 			bzero(p, sz);
47559ec9023SKonstantin Belousov 			break;
47659ec9023SKonstantin Belousov 		case MD_MALLOC_MOVE_FILL:
47759ec9023SKonstantin Belousov 			memset(p, fill, sz);
47859ec9023SKonstantin Belousov 			break;
47959ec9023SKonstantin Belousov 		case MD_MALLOC_MOVE_READ:
48059ec9023SKonstantin Belousov 			bcopy(ptr, p, sz);
48159ec9023SKonstantin Belousov 			cpu_flush_dcache(p, sz);
48259ec9023SKonstantin Belousov 			break;
48359ec9023SKonstantin Belousov 		case MD_MALLOC_MOVE_WRITE:
48459ec9023SKonstantin Belousov 			bcopy(p, ptr, sz);
48559ec9023SKonstantin Belousov 			break;
48659ec9023SKonstantin Belousov 		case MD_MALLOC_MOVE_CMP:
48759ec9023SKonstantin Belousov 			for (i = 0; i < sz; i++, p++) {
48859ec9023SKonstantin Belousov 				if (!first_read) {
48959ec9023SKonstantin Belousov 					*uc = (u_char)*p;
49059ec9023SKonstantin Belousov 					first = *p;
49159ec9023SKonstantin Belousov 					first_read = 1;
49259ec9023SKonstantin Belousov 				} else if (*p != first) {
49359ec9023SKonstantin Belousov 					error = EDOOFUS;
49459ec9023SKonstantin Belousov 					break;
49559ec9023SKonstantin Belousov 				}
49659ec9023SKonstantin Belousov 			}
49759ec9023SKonstantin Belousov 			break;
49859ec9023SKonstantin Belousov 		default:
49959ec9023SKonstantin Belousov 			KASSERT(0, ("md_malloc_move unknown op %d\n", op));
50059ec9023SKonstantin Belousov 			break;
50159ec9023SKonstantin Belousov 		}
50259ec9023SKonstantin Belousov 		if (error != 0)
50359ec9023SKonstantin Belousov 			break;
50459ec9023SKonstantin Belousov 		*ma_offs += sz;
50559ec9023SKonstantin Belousov 		*ma_offs %= PAGE_SIZE;
50659ec9023SKonstantin Belousov 		if (*ma_offs == 0)
50759ec9023SKonstantin Belousov 			(*mp)++;
50859ec9023SKonstantin Belousov 		ptr = (char *)ptr + sz;
50959ec9023SKonstantin Belousov 	}
51059ec9023SKonstantin Belousov 
51159ec9023SKonstantin Belousov 	if (sf != NULL)
51259ec9023SKonstantin Belousov 		sf_buf_free(sf);
51359ec9023SKonstantin Belousov 	sched_unpin();
51459ec9023SKonstantin Belousov 	if (op == MD_MALLOC_MOVE_CMP && error != 0) {
51559ec9023SKonstantin Belousov 		*mp = mp1;
51659ec9023SKonstantin Belousov 		*ma_offs = ma_offs1;
51759ec9023SKonstantin Belousov 	}
51859ec9023SKonstantin Belousov 	return (error);
51959ec9023SKonstantin Belousov }
52059ec9023SKonstantin Belousov 
521b4a4f93cSPoul-Henning Kamp static int
522b4a4f93cSPoul-Henning Kamp mdstart_malloc(struct md_s *sc, struct bio *bp)
52300a6a3c6SPoul-Henning Kamp {
524c6517568SPoul-Henning Kamp 	u_char *dst;
52559ec9023SKonstantin Belousov 	vm_page_t *m;
52659ec9023SKonstantin Belousov 	int i, error, error1, ma_offs, notmapped;
527b830359bSPawel Jakub Dawidek 	off_t secno, nsec, uc;
528c6517568SPoul-Henning Kamp 	uintptr_t sp, osp;
52900a6a3c6SPoul-Henning Kamp 
5305541f25eSPawel Jakub Dawidek 	switch (bp->bio_cmd) {
5315541f25eSPawel Jakub Dawidek 	case BIO_READ:
5325541f25eSPawel Jakub Dawidek 	case BIO_WRITE:
5335541f25eSPawel Jakub Dawidek 	case BIO_DELETE:
5345541f25eSPawel Jakub Dawidek 		break;
5355541f25eSPawel Jakub Dawidek 	default:
5365541f25eSPawel Jakub Dawidek 		return (EOPNOTSUPP);
5375541f25eSPawel Jakub Dawidek 	}
5385541f25eSPawel Jakub Dawidek 
53959ec9023SKonstantin Belousov 	notmapped = (bp->bio_flags & BIO_UNMAPPED) != 0;
54059ec9023SKonstantin Belousov 	if (notmapped) {
54159ec9023SKonstantin Belousov 		m = bp->bio_ma;
54259ec9023SKonstantin Belousov 		ma_offs = bp->bio_ma_offset;
54359ec9023SKonstantin Belousov 		dst = NULL;
54459ec9023SKonstantin Belousov 	} else {
54559ec9023SKonstantin Belousov 		dst = bp->bio_data;
54659ec9023SKonstantin Belousov 	}
54759ec9023SKonstantin Belousov 
548b830359bSPawel Jakub Dawidek 	nsec = bp->bio_length / sc->sectorsize;
549b830359bSPawel Jakub Dawidek 	secno = bp->bio_offset / sc->sectorsize;
550c6517568SPoul-Henning Kamp 	error = 0;
55100a6a3c6SPoul-Henning Kamp 	while (nsec--) {
552fde2a2e4SPoul-Henning Kamp 		osp = s_read(sc->indir, secno);
5538177437dSPoul-Henning Kamp 		if (bp->bio_cmd == BIO_DELETE) {
554fde2a2e4SPoul-Henning Kamp 			if (osp != 0)
555fde2a2e4SPoul-Henning Kamp 				error = s_write(sc->indir, secno, 0);
5568177437dSPoul-Henning Kamp 		} else if (bp->bio_cmd == BIO_READ) {
55759ec9023SKonstantin Belousov 			if (osp == 0) {
55859ec9023SKonstantin Belousov 				if (notmapped) {
55959ec9023SKonstantin Belousov 					error = md_malloc_move(&m, &ma_offs,
56059ec9023SKonstantin Belousov 					    sc->sectorsize, NULL, 0,
56159ec9023SKonstantin Belousov 					    MD_MALLOC_MOVE_ZERO);
56259ec9023SKonstantin Belousov 				} else
563b830359bSPawel Jakub Dawidek 					bzero(dst, sc->sectorsize);
56459ec9023SKonstantin Belousov 			} else if (osp <= 255) {
56559ec9023SKonstantin Belousov 				if (notmapped) {
56659ec9023SKonstantin Belousov 					error = md_malloc_move(&m, &ma_offs,
56759ec9023SKonstantin Belousov 					    sc->sectorsize, NULL, osp,
56859ec9023SKonstantin Belousov 					    MD_MALLOC_MOVE_FILL);
56959ec9023SKonstantin Belousov 				} else
570dbb95048SMarcel Moolenaar 					memset(dst, osp, sc->sectorsize);
57159ec9023SKonstantin Belousov 			} else {
57259ec9023SKonstantin Belousov 				if (notmapped) {
57359ec9023SKonstantin Belousov 					error = md_malloc_move(&m, &ma_offs,
57459ec9023SKonstantin Belousov 					    sc->sectorsize, (void *)osp, 0,
57559ec9023SKonstantin Belousov 					    MD_MALLOC_MOVE_READ);
57659ec9023SKonstantin Belousov 				} else {
577b830359bSPawel Jakub Dawidek 					bcopy((void *)osp, dst, sc->sectorsize);
578dbb95048SMarcel Moolenaar 					cpu_flush_dcache(dst, sc->sectorsize);
579dbb95048SMarcel Moolenaar 				}
58059ec9023SKonstantin Belousov 			}
581fde2a2e4SPoul-Henning Kamp 			osp = 0;
582c6517568SPoul-Henning Kamp 		} else if (bp->bio_cmd == BIO_WRITE) {
5838f8def9eSPoul-Henning Kamp 			if (sc->flags & MD_COMPRESS) {
58459ec9023SKonstantin Belousov 				if (notmapped) {
58559ec9023SKonstantin Belousov 					error1 = md_malloc_move(&m, &ma_offs,
58659ec9023SKonstantin Belousov 					    sc->sectorsize, &uc, 0,
58759ec9023SKonstantin Belousov 					    MD_MALLOC_MOVE_CMP);
58859ec9023SKonstantin Belousov 					i = error1 == 0 ? sc->sectorsize : 0;
58959ec9023SKonstantin Belousov 				} else {
59000a6a3c6SPoul-Henning Kamp 					uc = dst[0];
59159ec9023SKonstantin Belousov 					for (i = 1; i < sc->sectorsize; i++) {
59200a6a3c6SPoul-Henning Kamp 						if (dst[i] != uc)
59300a6a3c6SPoul-Henning Kamp 							break;
59459ec9023SKonstantin Belousov 					}
59559ec9023SKonstantin Belousov 				}
5968f8def9eSPoul-Henning Kamp 			} else {
5978f8def9eSPoul-Henning Kamp 				i = 0;
5988f8def9eSPoul-Henning Kamp 				uc = 0;
5998f8def9eSPoul-Henning Kamp 			}
600b830359bSPawel Jakub Dawidek 			if (i == sc->sectorsize) {
601fde2a2e4SPoul-Henning Kamp 				if (osp != uc)
602fde2a2e4SPoul-Henning Kamp 					error = s_write(sc->indir, secno, uc);
60300a6a3c6SPoul-Henning Kamp 			} else {
604fde2a2e4SPoul-Henning Kamp 				if (osp <= 255) {
605b830359bSPawel Jakub Dawidek 					sp = (uintptr_t)uma_zalloc(sc->uma,
606c44d423eSKonstantin Belousov 					    md_malloc_wait ? M_WAITOK :
607b830359bSPawel Jakub Dawidek 					    M_NOWAIT);
608c6517568SPoul-Henning Kamp 					if (sp == 0) {
609fde2a2e4SPoul-Henning Kamp 						error = ENOSPC;
610fde2a2e4SPoul-Henning Kamp 						break;
611fde2a2e4SPoul-Henning Kamp 					}
61259ec9023SKonstantin Belousov 					if (notmapped) {
61359ec9023SKonstantin Belousov 						error = md_malloc_move(&m,
61459ec9023SKonstantin Belousov 						    &ma_offs, sc->sectorsize,
61559ec9023SKonstantin Belousov 						    (void *)sp, 0,
61659ec9023SKonstantin Belousov 						    MD_MALLOC_MOVE_WRITE);
61759ec9023SKonstantin Belousov 					} else {
61859ec9023SKonstantin Belousov 						bcopy(dst, (void *)sp,
61959ec9023SKonstantin Belousov 						    sc->sectorsize);
62059ec9023SKonstantin Belousov 					}
621fde2a2e4SPoul-Henning Kamp 					error = s_write(sc->indir, secno, sp);
622c6517568SPoul-Henning Kamp 				} else {
62359ec9023SKonstantin Belousov 					if (notmapped) {
62459ec9023SKonstantin Belousov 						error = md_malloc_move(&m,
62559ec9023SKonstantin Belousov 						    &ma_offs, sc->sectorsize,
62659ec9023SKonstantin Belousov 						    (void *)osp, 0,
62759ec9023SKonstantin Belousov 						    MD_MALLOC_MOVE_WRITE);
62859ec9023SKonstantin Belousov 					} else {
62959ec9023SKonstantin Belousov 						bcopy(dst, (void *)osp,
63059ec9023SKonstantin Belousov 						    sc->sectorsize);
63159ec9023SKonstantin Belousov 					}
632fde2a2e4SPoul-Henning Kamp 					osp = 0;
63300a6a3c6SPoul-Henning Kamp 				}
63400a6a3c6SPoul-Henning Kamp 			}
635c6517568SPoul-Henning Kamp 		} else {
636c6517568SPoul-Henning Kamp 			error = EOPNOTSUPP;
637c6517568SPoul-Henning Kamp 		}
638c6517568SPoul-Henning Kamp 		if (osp > 255)
639f43b2bacSPoul-Henning Kamp 			uma_zfree(sc->uma, (void*)osp);
640e3ed29a7SPawel Jakub Dawidek 		if (error != 0)
641c6517568SPoul-Henning Kamp 			break;
64200a6a3c6SPoul-Henning Kamp 		secno++;
64359ec9023SKonstantin Belousov 		if (!notmapped)
644b830359bSPawel Jakub Dawidek 			dst += sc->sectorsize;
64500a6a3c6SPoul-Henning Kamp 	}
6468177437dSPoul-Henning Kamp 	bp->bio_resid = 0;
647c6517568SPoul-Henning Kamp 	return (error);
64800a6a3c6SPoul-Henning Kamp }
64900a6a3c6SPoul-Henning Kamp 
650b4a4f93cSPoul-Henning Kamp static int
651b4a4f93cSPoul-Henning Kamp mdstart_preload(struct md_s *sc, struct bio *bp)
65271e4fff8SPoul-Henning Kamp {
65371e4fff8SPoul-Henning Kamp 
654a8a58d03SPawel Jakub Dawidek 	switch (bp->bio_cmd) {
655a8a58d03SPawel Jakub Dawidek 	case BIO_READ:
656a8a58d03SPawel Jakub Dawidek 		bcopy(sc->pl_ptr + bp->bio_offset, bp->bio_data,
657a8a58d03SPawel Jakub Dawidek 		    bp->bio_length);
658dbb95048SMarcel Moolenaar 		cpu_flush_dcache(bp->bio_data, bp->bio_length);
659a8a58d03SPawel Jakub Dawidek 		break;
660a8a58d03SPawel Jakub Dawidek 	case BIO_WRITE:
661a8a58d03SPawel Jakub Dawidek 		bcopy(bp->bio_data, sc->pl_ptr + bp->bio_offset,
662a8a58d03SPawel Jakub Dawidek 		    bp->bio_length);
663a8a58d03SPawel Jakub Dawidek 		break;
66471e4fff8SPoul-Henning Kamp 	}
6658177437dSPoul-Henning Kamp 	bp->bio_resid = 0;
666b4a4f93cSPoul-Henning Kamp 	return (0);
66771e4fff8SPoul-Henning Kamp }
66871e4fff8SPoul-Henning Kamp 
669b4a4f93cSPoul-Henning Kamp static int
670b4a4f93cSPoul-Henning Kamp mdstart_vnode(struct md_s *sc, struct bio *bp)
6718f8def9eSPoul-Henning Kamp {
6725050aa86SKonstantin Belousov 	int error;
6738f8def9eSPoul-Henning Kamp 	struct uio auio;
6748f8def9eSPoul-Henning Kamp 	struct iovec aiov;
6758f8def9eSPoul-Henning Kamp 	struct mount *mp;
6765541f25eSPawel Jakub Dawidek 	struct vnode *vp;
67759ec9023SKonstantin Belousov 	struct buf *pb;
6785541f25eSPawel Jakub Dawidek 	struct thread *td;
6790abd21bdSDag-Erling Smørgrav 	off_t end, zerosize;
6805541f25eSPawel Jakub Dawidek 
6815541f25eSPawel Jakub Dawidek 	switch (bp->bio_cmd) {
6825541f25eSPawel Jakub Dawidek 	case BIO_READ:
6835541f25eSPawel Jakub Dawidek 	case BIO_WRITE:
6840abd21bdSDag-Erling Smørgrav 	case BIO_DELETE:
6855541f25eSPawel Jakub Dawidek 	case BIO_FLUSH:
6865541f25eSPawel Jakub Dawidek 		break;
6875541f25eSPawel Jakub Dawidek 	default:
6885541f25eSPawel Jakub Dawidek 		return (EOPNOTSUPP);
6895541f25eSPawel Jakub Dawidek 	}
6905541f25eSPawel Jakub Dawidek 
6915541f25eSPawel Jakub Dawidek 	td = curthread;
6925541f25eSPawel Jakub Dawidek 	vp = sc->vnode;
6938f8def9eSPoul-Henning Kamp 
6948f8def9eSPoul-Henning Kamp 	/*
6958f8def9eSPoul-Henning Kamp 	 * VNODE I/O
6968f8def9eSPoul-Henning Kamp 	 *
6978f8def9eSPoul-Henning Kamp 	 * If an error occurs, we set BIO_ERROR but we do not set
6988f8def9eSPoul-Henning Kamp 	 * B_INVAL because (for a write anyway), the buffer is
6998f8def9eSPoul-Henning Kamp 	 * still valid.
7008f8def9eSPoul-Henning Kamp 	 */
7018f8def9eSPoul-Henning Kamp 
7025541f25eSPawel Jakub Dawidek 	if (bp->bio_cmd == BIO_FLUSH) {
7035541f25eSPawel Jakub Dawidek 		(void) vn_start_write(vp, &mp, V_WAIT);
704cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
7055541f25eSPawel Jakub Dawidek 		error = VOP_FSYNC(vp, MNT_WAIT, td);
70622db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
7075541f25eSPawel Jakub Dawidek 		vn_finished_write(mp);
7085541f25eSPawel Jakub Dawidek 		return (error);
7095541f25eSPawel Jakub Dawidek 	}
7105541f25eSPawel Jakub Dawidek 
7118f8def9eSPoul-Henning Kamp 	bzero(&auio, sizeof(auio));
7128f8def9eSPoul-Henning Kamp 
7130abd21bdSDag-Erling Smørgrav 	/*
7140abd21bdSDag-Erling Smørgrav 	 * Special case for BIO_DELETE.  On the surface, this is very
7150abd21bdSDag-Erling Smørgrav 	 * similar to BIO_WRITE, except that we write from our own
7160abd21bdSDag-Erling Smørgrav 	 * fixed-length buffer, so we have to loop.  The net result is
7170abd21bdSDag-Erling Smørgrav 	 * that the two cases end up having very little in common.
7180abd21bdSDag-Erling Smørgrav 	 */
7190abd21bdSDag-Erling Smørgrav 	if (bp->bio_cmd == BIO_DELETE) {
72089cb2a19SMatthew D Fleming 		zerosize = ZERO_REGION_SIZE -
72189cb2a19SMatthew D Fleming 		    (ZERO_REGION_SIZE % sc->sectorsize);
7220abd21bdSDag-Erling Smørgrav 		auio.uio_iov = &aiov;
7230abd21bdSDag-Erling Smørgrav 		auio.uio_iovcnt = 1;
7240abd21bdSDag-Erling Smørgrav 		auio.uio_offset = (vm_ooffset_t)bp->bio_offset;
7250abd21bdSDag-Erling Smørgrav 		auio.uio_segflg = UIO_SYSSPACE;
7260abd21bdSDag-Erling Smørgrav 		auio.uio_rw = UIO_WRITE;
7270abd21bdSDag-Erling Smørgrav 		auio.uio_td = td;
7280abd21bdSDag-Erling Smørgrav 		end = bp->bio_offset + bp->bio_length;
7290abd21bdSDag-Erling Smørgrav 		(void) vn_start_write(vp, &mp, V_WAIT);
7300abd21bdSDag-Erling Smørgrav 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
7310abd21bdSDag-Erling Smørgrav 		error = 0;
7320abd21bdSDag-Erling Smørgrav 		while (auio.uio_offset < end) {
73389cb2a19SMatthew D Fleming 			aiov.iov_base = __DECONST(void *, zero_region);
7340abd21bdSDag-Erling Smørgrav 			aiov.iov_len = end - auio.uio_offset;
7350abd21bdSDag-Erling Smørgrav 			if (aiov.iov_len > zerosize)
7360abd21bdSDag-Erling Smørgrav 				aiov.iov_len = zerosize;
7370abd21bdSDag-Erling Smørgrav 			auio.uio_resid = aiov.iov_len;
7380abd21bdSDag-Erling Smørgrav 			error = VOP_WRITE(vp, &auio,
7390abd21bdSDag-Erling Smørgrav 			    sc->flags & MD_ASYNC ? 0 : IO_SYNC, sc->cred);
7400abd21bdSDag-Erling Smørgrav 			if (error != 0)
7410abd21bdSDag-Erling Smørgrav 				break;
7420abd21bdSDag-Erling Smørgrav 		}
7430abd21bdSDag-Erling Smørgrav 		VOP_UNLOCK(vp, 0);
7440abd21bdSDag-Erling Smørgrav 		vn_finished_write(mp);
7450abd21bdSDag-Erling Smørgrav 		bp->bio_resid = end - auio.uio_offset;
7460abd21bdSDag-Erling Smørgrav 		return (error);
7470abd21bdSDag-Erling Smørgrav 	}
7480abd21bdSDag-Erling Smørgrav 
74959ec9023SKonstantin Belousov 	KASSERT(bp->bio_length <= MAXPHYS, ("bio_length %jd",
75059ec9023SKonstantin Belousov 	    (uintmax_t)bp->bio_length));
75159ec9023SKonstantin Belousov 	if ((bp->bio_flags & BIO_UNMAPPED) == 0) {
75259ec9023SKonstantin Belousov 		pb = NULL;
7538f8def9eSPoul-Henning Kamp 		aiov.iov_base = bp->bio_data;
75459ec9023SKonstantin Belousov 	} else {
75559ec9023SKonstantin Belousov 		pb = getpbuf(&md_vnode_pbuf_freecnt);
75659ec9023SKonstantin Belousov 		pmap_qenter((vm_offset_t)pb->b_data, bp->bio_ma, bp->bio_ma_n);
75759ec9023SKonstantin Belousov 		aiov.iov_base = (void *)((vm_offset_t)pb->b_data +
75859ec9023SKonstantin Belousov 		    bp->bio_ma_offset);
75959ec9023SKonstantin Belousov 	}
760a8a58d03SPawel Jakub Dawidek 	aiov.iov_len = bp->bio_length;
7618f8def9eSPoul-Henning Kamp 	auio.uio_iov = &aiov;
7628f8def9eSPoul-Henning Kamp 	auio.uio_iovcnt = 1;
763a8a58d03SPawel Jakub Dawidek 	auio.uio_offset = (vm_ooffset_t)bp->bio_offset;
7648f8def9eSPoul-Henning Kamp 	auio.uio_segflg = UIO_SYSSPACE;
7658f8def9eSPoul-Henning Kamp 	if (bp->bio_cmd == BIO_READ)
7668f8def9eSPoul-Henning Kamp 		auio.uio_rw = UIO_READ;
7678e28326aSPoul-Henning Kamp 	else if (bp->bio_cmd == BIO_WRITE)
7688f8def9eSPoul-Henning Kamp 		auio.uio_rw = UIO_WRITE;
7698e28326aSPoul-Henning Kamp 	else
7708e28326aSPoul-Henning Kamp 		panic("wrong BIO_OP in mdstart_vnode");
771a8a58d03SPawel Jakub Dawidek 	auio.uio_resid = bp->bio_length;
7725541f25eSPawel Jakub Dawidek 	auio.uio_td = td;
7737e76bb56SMatthew Dillon 	/*
7747e76bb56SMatthew Dillon 	 * When reading set IO_DIRECT to try to avoid double-caching
77517a13919SPoul-Henning Kamp 	 * the data.  When writing IO_DIRECT is not optimal.
7767e76bb56SMatthew Dillon 	 */
7778f8def9eSPoul-Henning Kamp 	if (bp->bio_cmd == BIO_READ) {
778cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
7795541f25eSPawel Jakub Dawidek 		error = VOP_READ(vp, &auio, IO_DIRECT, sc->cred);
78022db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
7818f8def9eSPoul-Henning Kamp 	} else {
7825541f25eSPawel Jakub Dawidek 		(void) vn_start_write(vp, &mp, V_WAIT);
783cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
7845541f25eSPawel Jakub Dawidek 		error = VOP_WRITE(vp, &auio, sc->flags & MD_ASYNC ? 0 : IO_SYNC,
7855541f25eSPawel Jakub Dawidek 		    sc->cred);
78622db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
7878f8def9eSPoul-Henning Kamp 		vn_finished_write(mp);
7888f8def9eSPoul-Henning Kamp 	}
78959ec9023SKonstantin Belousov 	if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
79059ec9023SKonstantin Belousov 		pmap_qremove((vm_offset_t)pb->b_data, bp->bio_ma_n);
79159ec9023SKonstantin Belousov 		relpbuf(pb, &md_vnode_pbuf_freecnt);
79259ec9023SKonstantin Belousov 	}
7938f8def9eSPoul-Henning Kamp 	bp->bio_resid = auio.uio_resid;
794b4a4f93cSPoul-Henning Kamp 	return (error);
7958f8def9eSPoul-Henning Kamp }
7968f8def9eSPoul-Henning Kamp 
797b4a4f93cSPoul-Henning Kamp static int
798b4a4f93cSPoul-Henning Kamp mdstart_swap(struct md_s *sc, struct bio *bp)
7998f8def9eSPoul-Henning Kamp {
8008e28326aSPoul-Henning Kamp 	vm_page_t m;
8018e28326aSPoul-Henning Kamp 	u_char *p;
80259ec9023SKonstantin Belousov 	vm_pindex_t i, lastp;
80359ec9023SKonstantin Belousov 	int rv, ma_offs, offs, len, lastend;
8048f8def9eSPoul-Henning Kamp 
8055541f25eSPawel Jakub Dawidek 	switch (bp->bio_cmd) {
8065541f25eSPawel Jakub Dawidek 	case BIO_READ:
8075541f25eSPawel Jakub Dawidek 	case BIO_WRITE:
8085541f25eSPawel Jakub Dawidek 	case BIO_DELETE:
8095541f25eSPawel Jakub Dawidek 		break;
8105541f25eSPawel Jakub Dawidek 	default:
8115541f25eSPawel Jakub Dawidek 		return (EOPNOTSUPP);
8125541f25eSPawel Jakub Dawidek 	}
8135541f25eSPawel Jakub Dawidek 
8148e28326aSPoul-Henning Kamp 	p = bp->bio_data;
81559ec9023SKonstantin Belousov 	ma_offs = (bp->bio_flags & BIO_UNMAPPED) == 0 ? 0 : bp->bio_ma_offset;
816e07113d6SColin Percival 
817e07113d6SColin Percival 	/*
8186c3cd0e2SMaxim Konovalov 	 * offs is the offset at which to start operating on the
819e07113d6SColin Percival 	 * next (ie, first) page.  lastp is the last page on
820e07113d6SColin Percival 	 * which we're going to operate.  lastend is the ending
821e07113d6SColin Percival 	 * position within that last page (ie, PAGE_SIZE if
822e07113d6SColin Percival 	 * we're operating on complete aligned pages).
823e07113d6SColin Percival 	 */
824e07113d6SColin Percival 	offs = bp->bio_offset % PAGE_SIZE;
825e07113d6SColin Percival 	lastp = (bp->bio_offset + bp->bio_length - 1) / PAGE_SIZE;
826e07113d6SColin Percival 	lastend = (bp->bio_offset + bp->bio_length - 1) % PAGE_SIZE + 1;
827e07113d6SColin Percival 
828812851b6SBrian Feldman 	rv = VM_PAGER_OK;
82989f6b863SAttilio Rao 	VM_OBJECT_WLOCK(sc->object);
8308e28326aSPoul-Henning Kamp 	vm_object_pip_add(sc->object, 1);
831e07113d6SColin Percival 	for (i = bp->bio_offset / PAGE_SIZE; i <= lastp; i++) {
832e07113d6SColin Percival 		len = ((i == lastp) ? lastend : PAGE_SIZE) - offs;
8331a42d14aSKonstantin Belousov 		m = vm_page_grab(sc->object, i, VM_ALLOC_SYSTEM);
8348e28326aSPoul-Henning Kamp 		if (bp->bio_cmd == BIO_READ) {
835537cc627SKonstantin Belousov 			if (m->valid == VM_PAGE_BITS_ALL)
836537cc627SKonstantin Belousov 				rv = VM_PAGER_OK;
837537cc627SKonstantin Belousov 			else
83807be617fSAlan Cox 				rv = vm_pager_get_pages(sc->object, &m, 1, 0);
839812851b6SBrian Feldman 			if (rv == VM_PAGER_ERROR) {
840c7aebda8SAttilio Rao 				vm_page_xunbusy(m);
841812851b6SBrian Feldman 				break;
842e9f581baSKonstantin Belousov 			} else if (rv == VM_PAGER_FAIL) {
843e9f581baSKonstantin Belousov 				/*
844e9f581baSKonstantin Belousov 				 * Pager does not have the page.  Zero
845e9f581baSKonstantin Belousov 				 * the allocated page, and mark it as
846e9f581baSKonstantin Belousov 				 * valid. Do not set dirty, the page
847e9f581baSKonstantin Belousov 				 * can be recreated if thrown out.
848e9f581baSKonstantin Belousov 				 */
84959ec9023SKonstantin Belousov 				pmap_zero_page(m);
850e9f581baSKonstantin Belousov 				m->valid = VM_PAGE_BITS_ALL;
851812851b6SBrian Feldman 			}
85259ec9023SKonstantin Belousov 			if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
85359ec9023SKonstantin Belousov 				pmap_copy_pages(&m, offs, bp->bio_ma,
85459ec9023SKonstantin Belousov 				    ma_offs, len);
85559ec9023SKonstantin Belousov 			} else {
85659ec9023SKonstantin Belousov 				physcopyout(VM_PAGE_TO_PHYS(m) + offs, p, len);
857dbb95048SMarcel Moolenaar 				cpu_flush_dcache(p, len);
85859ec9023SKonstantin Belousov 			}
8598e28326aSPoul-Henning Kamp 		} else if (bp->bio_cmd == BIO_WRITE) {
86007be617fSAlan Cox 			if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL)
86107be617fSAlan Cox 				rv = vm_pager_get_pages(sc->object, &m, 1, 0);
862537cc627SKonstantin Belousov 			else
863537cc627SKonstantin Belousov 				rv = VM_PAGER_OK;
864812851b6SBrian Feldman 			if (rv == VM_PAGER_ERROR) {
865c7aebda8SAttilio Rao 				vm_page_xunbusy(m);
866812851b6SBrian Feldman 				break;
867812851b6SBrian Feldman 			}
86859ec9023SKonstantin Belousov 			if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
86959ec9023SKonstantin Belousov 				pmap_copy_pages(bp->bio_ma, ma_offs, &m,
87059ec9023SKonstantin Belousov 				    offs, len);
87159ec9023SKonstantin Belousov 			} else {
87259ec9023SKonstantin Belousov 				physcopyin(p, VM_PAGE_TO_PHYS(m) + offs, len);
87359ec9023SKonstantin Belousov 			}
8748e28326aSPoul-Henning Kamp 			m->valid = VM_PAGE_BITS_ALL;
8758e28326aSPoul-Henning Kamp 		} else if (bp->bio_cmd == BIO_DELETE) {
87607be617fSAlan Cox 			if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL)
87707be617fSAlan Cox 				rv = vm_pager_get_pages(sc->object, &m, 1, 0);
878537cc627SKonstantin Belousov 			else
879537cc627SKonstantin Belousov 				rv = VM_PAGER_OK;
880812851b6SBrian Feldman 			if (rv == VM_PAGER_ERROR) {
881c7aebda8SAttilio Rao 				vm_page_xunbusy(m);
882812851b6SBrian Feldman 				break;
883812851b6SBrian Feldman 			}
8844a13a769SKonstantin Belousov 			if (len != PAGE_SIZE) {
88559ec9023SKonstantin Belousov 				pmap_zero_page_area(m, offs, len);
8864a13a769SKonstantin Belousov 				vm_page_clear_dirty(m, offs, len);
8878e28326aSPoul-Henning Kamp 				m->valid = VM_PAGE_BITS_ALL;
8884a13a769SKonstantin Belousov 			} else
8894a13a769SKonstantin Belousov 				vm_pager_page_unswapped(m);
8908e28326aSPoul-Henning Kamp 		}
891c7aebda8SAttilio Rao 		vm_page_xunbusy(m);
892fc0c3802SKonstantin Belousov 		vm_page_lock(m);
8934a13a769SKonstantin Belousov 		if (bp->bio_cmd == BIO_DELETE && len == PAGE_SIZE)
8944a13a769SKonstantin Belousov 			vm_page_free(m);
8954a13a769SKonstantin Belousov 		else
8968e28326aSPoul-Henning Kamp 			vm_page_activate(m);
897ecd5dd95SAlan Cox 		vm_page_unlock(m);
89807be617fSAlan Cox 		if (bp->bio_cmd == BIO_WRITE)
8998e28326aSPoul-Henning Kamp 			vm_page_dirty(m);
900e07113d6SColin Percival 
901e07113d6SColin Percival 		/* Actions on further pages start at offset 0 */
902e07113d6SColin Percival 		p += PAGE_SIZE - offs;
903e07113d6SColin Percival 		offs = 0;
90459ec9023SKonstantin Belousov 		ma_offs += len;
9058e28326aSPoul-Henning Kamp 	}
9068e28326aSPoul-Henning Kamp 	vm_object_pip_subtract(sc->object, 1);
90789f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(sc->object);
908812851b6SBrian Feldman 	return (rv != VM_PAGER_ERROR ? 0 : ENOSPC);
9098e28326aSPoul-Henning Kamp }
9108f8def9eSPoul-Henning Kamp 
9118f8def9eSPoul-Henning Kamp static void
9125c97ca54SIan Dowse md_kthread(void *arg)
9135c97ca54SIan Dowse {
9145c97ca54SIan Dowse 	struct md_s *sc;
9155c97ca54SIan Dowse 	struct bio *bp;
916a08d2e7fSJohn Baldwin 	int error;
9175c97ca54SIan Dowse 
9185c97ca54SIan Dowse 	sc = arg;
919982d11f8SJeff Roberson 	thread_lock(curthread);
92063710c4dSJohn Baldwin 	sched_prio(curthread, PRIBIO);
921982d11f8SJeff Roberson 	thread_unlock(curthread);
9223b7b5496SKonstantin Belousov 	if (sc->type == MD_VNODE)
9233b7b5496SKonstantin Belousov 		curthread->td_pflags |= TDP_NORUNNINGBUF;
9245c97ca54SIan Dowse 
925b4a4f93cSPoul-Henning Kamp 	for (;;) {
926a08d2e7fSJohn Baldwin 		mtx_lock(&sc->queue_mtx);
9275c97ca54SIan Dowse 		if (sc->flags & MD_SHUTDOWN) {
928a08d2e7fSJohn Baldwin 			sc->flags |= MD_EXITING;
929a08d2e7fSJohn Baldwin 			mtx_unlock(&sc->queue_mtx);
9303745c395SJulian Elischer 			kproc_exit(0);
9315c97ca54SIan Dowse 		}
9329b00ca19SPoul-Henning Kamp 		bp = bioq_takefirst(&sc->bio_queue);
9339b00ca19SPoul-Henning Kamp 		if (!bp) {
9340f8500a5SPoul-Henning Kamp 			msleep(sc, &sc->queue_mtx, PRIBIO | PDROP, "mdwait", 0);
9355c97ca54SIan Dowse 			continue;
9365c97ca54SIan Dowse 		}
9370f8500a5SPoul-Henning Kamp 		mtx_unlock(&sc->queue_mtx);
9384e8bfe14SPoul-Henning Kamp 		if (bp->bio_cmd == BIO_GETATTR) {
939d91e813cSKonstantin Belousov 			if ((sc->fwsectors && sc->fwheads &&
9404e8bfe14SPoul-Henning Kamp 			    (g_handleattr_int(bp, "GEOM::fwsectors",
9414e8bfe14SPoul-Henning Kamp 			    sc->fwsectors) ||
9424e8bfe14SPoul-Henning Kamp 			    g_handleattr_int(bp, "GEOM::fwheads",
943d91e813cSKonstantin Belousov 			    sc->fwheads))) ||
944d91e813cSKonstantin Belousov 			    g_handleattr_int(bp, "GEOM::candelete", 1))
9454e8bfe14SPoul-Henning Kamp 				error = -1;
9464e8bfe14SPoul-Henning Kamp 			else
9474e8bfe14SPoul-Henning Kamp 				error = EOPNOTSUPP;
9484e8bfe14SPoul-Henning Kamp 		} else {
9499b00ca19SPoul-Henning Kamp 			error = sc->start(sc, bp);
9504e8bfe14SPoul-Henning Kamp 		}
951b4a4f93cSPoul-Henning Kamp 
9526f4f00f1SPoul-Henning Kamp 		if (error != -1) {
9536f4f00f1SPoul-Henning Kamp 			bp->bio_completed = bp->bio_length;
954a03be42dSMaxim Sobolev 			if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE))
955a03be42dSMaxim Sobolev 				devstat_end_transaction_bio(sc->devstat, bp);
95696410b95SKonstantin Belousov 			g_io_deliver(bp, error);
957b4a4f93cSPoul-Henning Kamp 		}
9588f8def9eSPoul-Henning Kamp 	}
95926d48b40SPoul-Henning Kamp }
9608f8def9eSPoul-Henning Kamp 
9618f8def9eSPoul-Henning Kamp static struct md_s *
9628f8def9eSPoul-Henning Kamp mdfind(int unit)
9638f8def9eSPoul-Henning Kamp {
9648f8def9eSPoul-Henning Kamp 	struct md_s *sc;
9658f8def9eSPoul-Henning Kamp 
9663f54a085SPoul-Henning Kamp 	LIST_FOREACH(sc, &md_softc_list, list) {
9673f54a085SPoul-Henning Kamp 		if (sc->unit == unit)
9688f8def9eSPoul-Henning Kamp 			break;
9698f8def9eSPoul-Henning Kamp 	}
9708f8def9eSPoul-Henning Kamp 	return (sc);
9718f8def9eSPoul-Henning Kamp }
9728f8def9eSPoul-Henning Kamp 
9738f8def9eSPoul-Henning Kamp static struct md_s *
974947fc8deSPoul-Henning Kamp mdnew(int unit, int *errp, enum md_types type)
9758f8def9eSPoul-Henning Kamp {
976f4e7c5a8SJaakko Heinonen 	struct md_s *sc;
977f4e7c5a8SJaakko Heinonen 	int error;
9788f8def9eSPoul-Henning Kamp 
9799b00ca19SPoul-Henning Kamp 	*errp = 0;
980f4e7c5a8SJaakko Heinonen 	if (unit == -1)
981f4e7c5a8SJaakko Heinonen 		unit = alloc_unr(md_uh);
982f4e7c5a8SJaakko Heinonen 	else
983f4e7c5a8SJaakko Heinonen 		unit = alloc_unr_specific(md_uh, unit);
984f4e7c5a8SJaakko Heinonen 
985f4e7c5a8SJaakko Heinonen 	if (unit == -1) {
9867ee3c044SPawel Jakub Dawidek 		*errp = EBUSY;
9873f54a085SPoul-Henning Kamp 		return (NULL);
9883f54a085SPoul-Henning Kamp 	}
989f4e7c5a8SJaakko Heinonen 
9909b00ca19SPoul-Henning Kamp 	sc = (struct md_s *)malloc(sizeof *sc, M_MD, M_WAITOK | M_ZERO);
991947fc8deSPoul-Henning Kamp 	sc->type = type;
9929b00ca19SPoul-Henning Kamp 	bioq_init(&sc->bio_queue);
9939b00ca19SPoul-Henning Kamp 	mtx_init(&sc->queue_mtx, "md bio queue", NULL, MTX_DEF);
994*40ea77a0SAlexander Motin 	mtx_init(&sc->stat_mtx, "md stat", NULL, MTX_DEF);
9953f54a085SPoul-Henning Kamp 	sc->unit = unit;
996f43b2bacSPoul-Henning Kamp 	sprintf(sc->name, "md%d", unit);
9977ee3c044SPawel Jakub Dawidek 	LIST_INSERT_HEAD(&md_softc_list, sc, list);
9983745c395SJulian Elischer 	error = kproc_create(md_kthread, sc, &sc->procp, 0, 0,"%s", sc->name);
9999b00ca19SPoul-Henning Kamp 	if (error == 0)
10009b00ca19SPoul-Henning Kamp 		return (sc);
10017ee3c044SPawel Jakub Dawidek 	LIST_REMOVE(sc, list);
1002*40ea77a0SAlexander Motin 	mtx_destroy(&sc->stat_mtx);
10037ee3c044SPawel Jakub Dawidek 	mtx_destroy(&sc->queue_mtx);
1004f4e7c5a8SJaakko Heinonen 	free_unr(md_uh, sc->unit);
10055c97ca54SIan Dowse 	free(sc, M_MD);
10067ee3c044SPawel Jakub Dawidek 	*errp = error;
10075c97ca54SIan Dowse 	return (NULL);
10085c97ca54SIan Dowse }
10098f8def9eSPoul-Henning Kamp 
10108f8def9eSPoul-Henning Kamp static void
10118f8def9eSPoul-Henning Kamp mdinit(struct md_s *sc)
10128f8def9eSPoul-Henning Kamp {
10136f4f00f1SPoul-Henning Kamp 	struct g_geom *gp;
10146f4f00f1SPoul-Henning Kamp 	struct g_provider *pp;
10156f4f00f1SPoul-Henning Kamp 
10166f4f00f1SPoul-Henning Kamp 	g_topology_lock();
10176f4f00f1SPoul-Henning Kamp 	gp = g_new_geomf(&g_md_class, "md%d", sc->unit);
10186f4f00f1SPoul-Henning Kamp 	gp->softc = sc;
10196f4f00f1SPoul-Henning Kamp 	pp = g_new_providerf(gp, "md%d", sc->unit);
1020*40ea77a0SAlexander Motin 	pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
1021b830359bSPawel Jakub Dawidek 	pp->mediasize = sc->mediasize;
1022b830359bSPawel Jakub Dawidek 	pp->sectorsize = sc->sectorsize;
10231ef76554SKonstantin Belousov 	switch (sc->type) {
10241ef76554SKonstantin Belousov 	case MD_MALLOC:
10251ef76554SKonstantin Belousov 	case MD_VNODE:
10261ef76554SKonstantin Belousov 	case MD_SWAP:
102759ec9023SKonstantin Belousov 		pp->flags |= G_PF_ACCEPT_UNMAPPED;
10281ef76554SKonstantin Belousov 		break;
10291ef76554SKonstantin Belousov 	case MD_PRELOAD:
10301ef76554SKonstantin Belousov 		break;
10311ef76554SKonstantin Belousov 	}
10326f4f00f1SPoul-Henning Kamp 	sc->gp = gp;
10336f4f00f1SPoul-Henning Kamp 	sc->pp = pp;
10346f4f00f1SPoul-Henning Kamp 	g_error_provider(pp, 0);
10356f4f00f1SPoul-Henning Kamp 	g_topology_unlock();
1036a03be42dSMaxim Sobolev 	sc->devstat = devstat_new_entry("md", sc->unit, sc->sectorsize,
1037a03be42dSMaxim Sobolev 	    DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
10386f4f00f1SPoul-Henning Kamp }
103971e4fff8SPoul-Henning Kamp 
10408f8def9eSPoul-Henning Kamp static int
1041b830359bSPawel Jakub Dawidek mdcreate_malloc(struct md_s *sc, struct md_ioctl *mdio)
104295f1a897SPoul-Henning Kamp {
1043c6517568SPoul-Henning Kamp 	uintptr_t sp;
1044c6517568SPoul-Henning Kamp 	int error;
1045b830359bSPawel Jakub Dawidek 	off_t u;
104695f1a897SPoul-Henning Kamp 
1047c6517568SPoul-Henning Kamp 	error = 0;
10488f8def9eSPoul-Henning Kamp 	if (mdio->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE))
10498f8def9eSPoul-Henning Kamp 		return (EINVAL);
1050b830359bSPawel Jakub Dawidek 	if (mdio->md_sectorsize != 0 && !powerof2(mdio->md_sectorsize))
1051ebe789d6SPoul-Henning Kamp 		return (EINVAL);
10528f8def9eSPoul-Henning Kamp 	/* Compression doesn't make sense if we have reserved space */
10538f8def9eSPoul-Henning Kamp 	if (mdio->md_options & MD_RESERVE)
10548f8def9eSPoul-Henning Kamp 		mdio->md_options &= ~MD_COMPRESS;
10554e8bfe14SPoul-Henning Kamp 	if (mdio->md_fwsectors != 0)
10564e8bfe14SPoul-Henning Kamp 		sc->fwsectors = mdio->md_fwsectors;
10574e8bfe14SPoul-Henning Kamp 	if (mdio->md_fwheads != 0)
10584e8bfe14SPoul-Henning Kamp 		sc->fwheads = mdio->md_fwheads;
105926a0ee75SDima Dorfman 	sc->flags = mdio->md_options & (MD_COMPRESS | MD_FORCE);
1060b830359bSPawel Jakub Dawidek 	sc->indir = dimension(sc->mediasize / sc->sectorsize);
1061b830359bSPawel Jakub Dawidek 	sc->uma = uma_zcreate(sc->name, sc->sectorsize, NULL, NULL, NULL, NULL,
1062b830359bSPawel Jakub Dawidek 	    0x1ff, 0);
106396b6a55fSPoul-Henning Kamp 	if (mdio->md_options & MD_RESERVE) {
1064b830359bSPawel Jakub Dawidek 		off_t nsectors;
1065b830359bSPawel Jakub Dawidek 
1066b830359bSPawel Jakub Dawidek 		nsectors = sc->mediasize / sc->sectorsize;
1067b830359bSPawel Jakub Dawidek 		for (u = 0; u < nsectors; u++) {
1068007777f1SKonstantin Belousov 			sp = (uintptr_t)uma_zalloc(sc->uma, (md_malloc_wait ?
1069007777f1SKonstantin Belousov 			    M_WAITOK : M_NOWAIT) | M_ZERO);
1070c6517568SPoul-Henning Kamp 			if (sp != 0)
1071fde2a2e4SPoul-Henning Kamp 				error = s_write(sc->indir, u, sp);
1072c6517568SPoul-Henning Kamp 			else
1073c6517568SPoul-Henning Kamp 				error = ENOMEM;
1074b830359bSPawel Jakub Dawidek 			if (error != 0)
1075c6517568SPoul-Henning Kamp 				break;
10768f8def9eSPoul-Henning Kamp 		}
1077c6517568SPoul-Henning Kamp 	}
1078c6517568SPoul-Henning Kamp 	return (error);
107900a6a3c6SPoul-Henning Kamp }
108000a6a3c6SPoul-Henning Kamp 
10813f54a085SPoul-Henning Kamp 
10828f8def9eSPoul-Henning Kamp static int
10838f8def9eSPoul-Henning Kamp mdsetcred(struct md_s *sc, struct ucred *cred)
10848f8def9eSPoul-Henning Kamp {
10858f8def9eSPoul-Henning Kamp 	char *tmpbuf;
10868f8def9eSPoul-Henning Kamp 	int error = 0;
10878f8def9eSPoul-Henning Kamp 
10883f54a085SPoul-Henning Kamp 	/*
10898f8def9eSPoul-Henning Kamp 	 * Set credits in our softc
10903f54a085SPoul-Henning Kamp 	 */
10918f8def9eSPoul-Henning Kamp 
10928f8def9eSPoul-Henning Kamp 	if (sc->cred)
10938f8def9eSPoul-Henning Kamp 		crfree(sc->cred);
1094bd78ceceSJohn Baldwin 	sc->cred = crhold(cred);
10958f8def9eSPoul-Henning Kamp 
10968f8def9eSPoul-Henning Kamp 	/*
10978f8def9eSPoul-Henning Kamp 	 * Horrible kludge to establish credentials for NFS  XXX.
10988f8def9eSPoul-Henning Kamp 	 */
10998f8def9eSPoul-Henning Kamp 
11008f8def9eSPoul-Henning Kamp 	if (sc->vnode) {
11018f8def9eSPoul-Henning Kamp 		struct uio auio;
11028f8def9eSPoul-Henning Kamp 		struct iovec aiov;
11038f8def9eSPoul-Henning Kamp 
1104b830359bSPawel Jakub Dawidek 		tmpbuf = malloc(sc->sectorsize, M_TEMP, M_WAITOK);
11058f8def9eSPoul-Henning Kamp 		bzero(&auio, sizeof(auio));
11068f8def9eSPoul-Henning Kamp 
11078f8def9eSPoul-Henning Kamp 		aiov.iov_base = tmpbuf;
1108b830359bSPawel Jakub Dawidek 		aiov.iov_len = sc->sectorsize;
11098f8def9eSPoul-Henning Kamp 		auio.uio_iov = &aiov;
11108f8def9eSPoul-Henning Kamp 		auio.uio_iovcnt = 1;
11118f8def9eSPoul-Henning Kamp 		auio.uio_offset = 0;
11128f8def9eSPoul-Henning Kamp 		auio.uio_rw = UIO_READ;
11138f8def9eSPoul-Henning Kamp 		auio.uio_segflg = UIO_SYSSPACE;
11148f8def9eSPoul-Henning Kamp 		auio.uio_resid = aiov.iov_len;
1115cb05b60aSAttilio Rao 		vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY);
11168f8def9eSPoul-Henning Kamp 		error = VOP_READ(sc->vnode, &auio, 0, sc->cred);
111722db15c0SAttilio Rao 		VOP_UNLOCK(sc->vnode, 0);
11188f8def9eSPoul-Henning Kamp 		free(tmpbuf, M_TEMP);
11198f8def9eSPoul-Henning Kamp 	}
11208f8def9eSPoul-Henning Kamp 	return (error);
11218f8def9eSPoul-Henning Kamp }
11228f8def9eSPoul-Henning Kamp 
11238f8def9eSPoul-Henning Kamp static int
1124b830359bSPawel Jakub Dawidek mdcreate_vnode(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
11258f8def9eSPoul-Henning Kamp {
11268f8def9eSPoul-Henning Kamp 	struct vattr vattr;
11278f8def9eSPoul-Henning Kamp 	struct nameidata nd;
11283d5c947dSMarcel Moolenaar 	char *fname;
11295050aa86SKonstantin Belousov 	int error, flags;
11308f8def9eSPoul-Henning Kamp 
11313d5c947dSMarcel Moolenaar 	/*
11323d5c947dSMarcel Moolenaar 	 * Kernel-originated requests must have the filename appended
11333d5c947dSMarcel Moolenaar 	 * to the mdio structure to protect against malicious software.
11343d5c947dSMarcel Moolenaar 	 */
11353d5c947dSMarcel Moolenaar 	fname = mdio->md_file;
11363d5c947dSMarcel Moolenaar 	if ((void *)fname != (void *)(mdio + 1)) {
11373d5c947dSMarcel Moolenaar 		error = copyinstr(fname, sc->file, sizeof(sc->file), NULL);
113888b5b78dSPawel Jakub Dawidek 		if (error != 0)
113988b5b78dSPawel Jakub Dawidek 			return (error);
11403d5c947dSMarcel Moolenaar 	} else
11413d5c947dSMarcel Moolenaar 		strlcpy(sc->file, fname, sizeof(sc->file));
11423d5c947dSMarcel Moolenaar 
114386776891SChristian S.J. Peron 	/*
11443d5c947dSMarcel Moolenaar 	 * If the user specified that this is a read only device, don't
11453d5c947dSMarcel Moolenaar 	 * set the FWRITE mask before trying to open the backing store.
114686776891SChristian S.J. Peron 	 */
11473d5c947dSMarcel Moolenaar 	flags = FREAD | ((mdio->md_options & MD_READONLY) ? 0 : FWRITE);
11485050aa86SKonstantin Belousov 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, sc->file, td);
11499e223287SKonstantin Belousov 	error = vn_open(&nd, &flags, 0, NULL);
1150e3ed29a7SPawel Jakub Dawidek 	if (error != 0)
115152c6716fSPawel Jakub Dawidek 		return (error);
1152b322d85dSPawel Jakub Dawidek 	NDFREE(&nd, NDF_ONLY_PNBUF);
115333fc3625SJohn Baldwin 	if (nd.ni_vp->v_type != VREG) {
115433fc3625SJohn Baldwin 		error = EINVAL;
115533fc3625SJohn Baldwin 		goto bad;
115633fc3625SJohn Baldwin 	}
115733fc3625SJohn Baldwin 	error = VOP_GETATTR(nd.ni_vp, &vattr, td->td_ucred);
115833fc3625SJohn Baldwin 	if (error != 0)
115933fc3625SJohn Baldwin 		goto bad;
116033fc3625SJohn Baldwin 	if (VOP_ISLOCKED(nd.ni_vp) != LK_EXCLUSIVE) {
116133fc3625SJohn Baldwin 		vn_lock(nd.ni_vp, LK_UPGRADE | LK_RETRY);
116233fc3625SJohn Baldwin 		if (nd.ni_vp->v_iflag & VI_DOOMED) {
116333fc3625SJohn Baldwin 			/* Forced unmount. */
116433fc3625SJohn Baldwin 			error = EBADF;
116533fc3625SJohn Baldwin 			goto bad;
116633fc3625SJohn Baldwin 		}
11678f8def9eSPoul-Henning Kamp 	}
11683b7b5496SKonstantin Belousov 	nd.ni_vp->v_vflag |= VV_MD;
116922db15c0SAttilio Rao 	VOP_UNLOCK(nd.ni_vp, 0);
11709589c256SPoul-Henning Kamp 
1171d5a929dcSPoul-Henning Kamp 	if (mdio->md_fwsectors != 0)
1172d5a929dcSPoul-Henning Kamp 		sc->fwsectors = mdio->md_fwsectors;
1173d5a929dcSPoul-Henning Kamp 	if (mdio->md_fwheads != 0)
1174d5a929dcSPoul-Henning Kamp 		sc->fwheads = mdio->md_fwheads;
11757a6b2b64SPoul-Henning Kamp 	sc->flags = mdio->md_options & (MD_FORCE | MD_ASYNC);
11769589c256SPoul-Henning Kamp 	if (!(flags & FWRITE))
11779589c256SPoul-Henning Kamp 		sc->flags |= MD_READONLY;
11788f8def9eSPoul-Henning Kamp 	sc->vnode = nd.ni_vp;
11798f8def9eSPoul-Henning Kamp 
1180a854ed98SJohn Baldwin 	error = mdsetcred(sc, td->td_ucred);
1181b830359bSPawel Jakub Dawidek 	if (error != 0) {
11823cf74e53SPhilip Paeps 		sc->vnode = NULL;
1183cb05b60aSAttilio Rao 		vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY);
11843b7b5496SKonstantin Belousov 		nd.ni_vp->v_vflag &= ~VV_MD;
118533fc3625SJohn Baldwin 		goto bad;
118633fc3625SJohn Baldwin 	}
118733fc3625SJohn Baldwin 	return (0);
118833fc3625SJohn Baldwin bad:
118922db15c0SAttilio Rao 	VOP_UNLOCK(nd.ni_vp, 0);
119052c6716fSPawel Jakub Dawidek 	(void)vn_close(nd.ni_vp, flags, td->td_ucred, td);
11918f8def9eSPoul-Henning Kamp 	return (error);
11928f8def9eSPoul-Henning Kamp }
11938f8def9eSPoul-Henning Kamp 
11948f8def9eSPoul-Henning Kamp static int
1195b40ce416SJulian Elischer mddestroy(struct md_s *sc, struct thread *td)
11968f8def9eSPoul-Henning Kamp {
11970cddd8f0SMatthew Dillon 
11986f4f00f1SPoul-Henning Kamp 	if (sc->gp) {
11996f4f00f1SPoul-Henning Kamp 		sc->gp->softc = NULL;
12009b00ca19SPoul-Henning Kamp 		g_topology_lock();
12019b00ca19SPoul-Henning Kamp 		g_wither_geom(sc->gp, ENXIO);
12029b00ca19SPoul-Henning Kamp 		g_topology_unlock();
12036b60a2cdSPoul-Henning Kamp 		sc->gp = NULL;
12046b60a2cdSPoul-Henning Kamp 		sc->pp = NULL;
12051f4ee1aaSPoul-Henning Kamp 	}
1206a03be42dSMaxim Sobolev 	if (sc->devstat) {
1207a03be42dSMaxim Sobolev 		devstat_remove_entry(sc->devstat);
1208a03be42dSMaxim Sobolev 		sc->devstat = NULL;
1209a03be42dSMaxim Sobolev 	}
1210a08d2e7fSJohn Baldwin 	mtx_lock(&sc->queue_mtx);
12115c97ca54SIan Dowse 	sc->flags |= MD_SHUTDOWN;
12125c97ca54SIan Dowse 	wakeup(sc);
1213a08d2e7fSJohn Baldwin 	while (!(sc->flags & MD_EXITING))
1214a08d2e7fSJohn Baldwin 		msleep(sc->procp, &sc->queue_mtx, PRIBIO, "mddestroy", hz / 10);
1215a08d2e7fSJohn Baldwin 	mtx_unlock(&sc->queue_mtx);
1216*40ea77a0SAlexander Motin 	mtx_destroy(&sc->stat_mtx);
12179fbea3e3SPoul-Henning Kamp 	mtx_destroy(&sc->queue_mtx);
12189b00ca19SPoul-Henning Kamp 	if (sc->vnode != NULL) {
1219cb05b60aSAttilio Rao 		vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY);
12203b7b5496SKonstantin Belousov 		sc->vnode->v_vflag &= ~VV_MD;
122122db15c0SAttilio Rao 		VOP_UNLOCK(sc->vnode, 0);
12229d4b5945SMaxim Sobolev 		(void)vn_close(sc->vnode, sc->flags & MD_READONLY ?
1223b40ce416SJulian Elischer 		    FREAD : (FREAD|FWRITE), sc->cred, td);
12249b00ca19SPoul-Henning Kamp 	}
12258f8def9eSPoul-Henning Kamp 	if (sc->cred != NULL)
12268f8def9eSPoul-Henning Kamp 		crfree(sc->cred);
12271db17c6dSPawel Jakub Dawidek 	if (sc->object != NULL)
1228f820bc50SAlan Cox 		vm_object_deallocate(sc->object);
1229f43b2bacSPoul-Henning Kamp 	if (sc->indir)
1230f43b2bacSPoul-Henning Kamp 		destroy_indir(sc, sc->indir);
1231f43b2bacSPoul-Henning Kamp 	if (sc->uma)
1232f43b2bacSPoul-Henning Kamp 		uma_zdestroy(sc->uma);
12331f4ee1aaSPoul-Henning Kamp 
12341f4ee1aaSPoul-Henning Kamp 	LIST_REMOVE(sc, list);
1235f4e7c5a8SJaakko Heinonen 	free_unr(md_uh, sc->unit);
1236c6517568SPoul-Henning Kamp 	free(sc, M_MD);
12378f8def9eSPoul-Henning Kamp 	return (0);
12388f8def9eSPoul-Henning Kamp }
12398f8def9eSPoul-Henning Kamp 
12408f8def9eSPoul-Henning Kamp static int
1241dc604f0cSEdward Tomasz Napierala mdresize(struct md_s *sc, struct md_ioctl *mdio)
1242dc604f0cSEdward Tomasz Napierala {
1243dc604f0cSEdward Tomasz Napierala 	int error, res;
1244dc604f0cSEdward Tomasz Napierala 	vm_pindex_t oldpages, newpages;
1245dc604f0cSEdward Tomasz Napierala 
1246dc604f0cSEdward Tomasz Napierala 	switch (sc->type) {
1247dc604f0cSEdward Tomasz Napierala 	case MD_VNODE:
1248dc604f0cSEdward Tomasz Napierala 		break;
1249dc604f0cSEdward Tomasz Napierala 	case MD_SWAP:
12508cb51643SJaakko Heinonen 		if (mdio->md_mediasize <= 0 ||
1251dc604f0cSEdward Tomasz Napierala 		    (mdio->md_mediasize % PAGE_SIZE) != 0)
1252dc604f0cSEdward Tomasz Napierala 			return (EDOM);
1253dc604f0cSEdward Tomasz Napierala 		oldpages = OFF_TO_IDX(round_page(sc->mediasize));
1254dc604f0cSEdward Tomasz Napierala 		newpages = OFF_TO_IDX(round_page(mdio->md_mediasize));
1255dc604f0cSEdward Tomasz Napierala 		if (newpages < oldpages) {
125689f6b863SAttilio Rao 			VM_OBJECT_WLOCK(sc->object);
1257dc604f0cSEdward Tomasz Napierala 			vm_object_page_remove(sc->object, newpages, 0, 0);
1258dc604f0cSEdward Tomasz Napierala 			swap_pager_freespace(sc->object, newpages,
1259dc604f0cSEdward Tomasz Napierala 			    oldpages - newpages);
1260dc604f0cSEdward Tomasz Napierala 			swap_release_by_cred(IDX_TO_OFF(oldpages -
1261dc604f0cSEdward Tomasz Napierala 			    newpages), sc->cred);
1262dc604f0cSEdward Tomasz Napierala 			sc->object->charge = IDX_TO_OFF(newpages);
1263dc604f0cSEdward Tomasz Napierala 			sc->object->size = newpages;
126489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(sc->object);
1265dc604f0cSEdward Tomasz Napierala 		} else if (newpages > oldpages) {
1266dc604f0cSEdward Tomasz Napierala 			res = swap_reserve_by_cred(IDX_TO_OFF(newpages -
1267dc604f0cSEdward Tomasz Napierala 			    oldpages), sc->cred);
1268dc604f0cSEdward Tomasz Napierala 			if (!res)
1269dc604f0cSEdward Tomasz Napierala 				return (ENOMEM);
1270dc604f0cSEdward Tomasz Napierala 			if ((mdio->md_options & MD_RESERVE) ||
1271dc604f0cSEdward Tomasz Napierala 			    (sc->flags & MD_RESERVE)) {
1272dc604f0cSEdward Tomasz Napierala 				error = swap_pager_reserve(sc->object,
1273dc604f0cSEdward Tomasz Napierala 				    oldpages, newpages - oldpages);
1274dc604f0cSEdward Tomasz Napierala 				if (error < 0) {
1275dc604f0cSEdward Tomasz Napierala 					swap_release_by_cred(
1276dc604f0cSEdward Tomasz Napierala 					    IDX_TO_OFF(newpages - oldpages),
1277dc604f0cSEdward Tomasz Napierala 					    sc->cred);
1278dc604f0cSEdward Tomasz Napierala 					return (EDOM);
1279dc604f0cSEdward Tomasz Napierala 				}
1280dc604f0cSEdward Tomasz Napierala 			}
128189f6b863SAttilio Rao 			VM_OBJECT_WLOCK(sc->object);
1282dc604f0cSEdward Tomasz Napierala 			sc->object->charge = IDX_TO_OFF(newpages);
1283dc604f0cSEdward Tomasz Napierala 			sc->object->size = newpages;
128489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(sc->object);
1285dc604f0cSEdward Tomasz Napierala 		}
1286dc604f0cSEdward Tomasz Napierala 		break;
1287dc604f0cSEdward Tomasz Napierala 	default:
1288dc604f0cSEdward Tomasz Napierala 		return (EOPNOTSUPP);
1289dc604f0cSEdward Tomasz Napierala 	}
1290dc604f0cSEdward Tomasz Napierala 
1291dc604f0cSEdward Tomasz Napierala 	sc->mediasize = mdio->md_mediasize;
1292dc604f0cSEdward Tomasz Napierala 	g_topology_lock();
1293dc604f0cSEdward Tomasz Napierala 	g_resize_provider(sc->pp, sc->mediasize);
1294dc604f0cSEdward Tomasz Napierala 	g_topology_unlock();
1295dc604f0cSEdward Tomasz Napierala 	return (0);
1296dc604f0cSEdward Tomasz Napierala }
1297dc604f0cSEdward Tomasz Napierala 
1298dc604f0cSEdward Tomasz Napierala static int
1299b830359bSPawel Jakub Dawidek mdcreate_swap(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
13008f8def9eSPoul-Henning Kamp {
1301fcd57fbeSPawel Jakub Dawidek 	vm_ooffset_t npage;
1302fcd57fbeSPawel Jakub Dawidek 	int error;
13038f8def9eSPoul-Henning Kamp 
13048f8def9eSPoul-Henning Kamp 	/*
13058f8def9eSPoul-Henning Kamp 	 * Range check.  Disallow negative sizes or any size less then the
13068f8def9eSPoul-Henning Kamp 	 * size of a page.  Then round to a page.
13078f8def9eSPoul-Henning Kamp 	 */
13088cb51643SJaakko Heinonen 	if (sc->mediasize <= 0 || (sc->mediasize % PAGE_SIZE) != 0)
13098f8def9eSPoul-Henning Kamp 		return (EDOM);
13108f8def9eSPoul-Henning Kamp 
13118f8def9eSPoul-Henning Kamp 	/*
13128f8def9eSPoul-Henning Kamp 	 * Allocate an OBJT_SWAP object.
13138f8def9eSPoul-Henning Kamp 	 *
13148f8def9eSPoul-Henning Kamp 	 * Note the truncation.
13158f8def9eSPoul-Henning Kamp 	 */
13168f8def9eSPoul-Henning Kamp 
1317b830359bSPawel Jakub Dawidek 	npage = mdio->md_mediasize / PAGE_SIZE;
13189ed40643SPoul-Henning Kamp 	if (mdio->md_fwsectors != 0)
13199ed40643SPoul-Henning Kamp 		sc->fwsectors = mdio->md_fwsectors;
13209ed40643SPoul-Henning Kamp 	if (mdio->md_fwheads != 0)
13219ed40643SPoul-Henning Kamp 		sc->fwheads = mdio->md_fwheads;
1322fcd57fbeSPawel Jakub Dawidek 	sc->object = vm_pager_allocate(OBJT_SWAP, NULL, PAGE_SIZE * npage,
13233364c323SKonstantin Belousov 	    VM_PROT_DEFAULT, 0, td->td_ucred);
1324812851b6SBrian Feldman 	if (sc->object == NULL)
1325812851b6SBrian Feldman 		return (ENOMEM);
1326dc604f0cSEdward Tomasz Napierala 	sc->flags = mdio->md_options & (MD_FORCE | MD_RESERVE);
13278f8def9eSPoul-Henning Kamp 	if (mdio->md_options & MD_RESERVE) {
1328fcd57fbeSPawel Jakub Dawidek 		if (swap_pager_reserve(sc->object, 0, npage) < 0) {
13293364c323SKonstantin Belousov 			error = EDOM;
13303364c323SKonstantin Belousov 			goto finish;
13318f8def9eSPoul-Henning Kamp 		}
13328f8def9eSPoul-Henning Kamp 	}
1333a854ed98SJohn Baldwin 	error = mdsetcred(sc, td->td_ucred);
13343364c323SKonstantin Belousov  finish:
1335e3ed29a7SPawel Jakub Dawidek 	if (error != 0) {
13362eafd8b1SPawel Jakub Dawidek 		vm_object_deallocate(sc->object);
13372eafd8b1SPawel Jakub Dawidek 		sc->object = NULL;
13388f8def9eSPoul-Henning Kamp 	}
1339b830359bSPawel Jakub Dawidek 	return (error);
1340b3b3d1b7SPoul-Henning Kamp }
13418f8def9eSPoul-Henning Kamp 
13429d4b5945SMaxim Sobolev 
13439d4b5945SMaxim Sobolev static int
13449b00ca19SPoul-Henning Kamp xmdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
13458f8def9eSPoul-Henning Kamp {
13468f8def9eSPoul-Henning Kamp 	struct md_ioctl *mdio;
13478f8def9eSPoul-Henning Kamp 	struct md_s *sc;
1348b830359bSPawel Jakub Dawidek 	int error, i;
13498cb51643SJaakko Heinonen 	unsigned sectsize;
13508f8def9eSPoul-Henning Kamp 
13518f8def9eSPoul-Henning Kamp 	if (md_debug)
13528f8def9eSPoul-Henning Kamp 		printf("mdctlioctl(%s %lx %p %x %p)\n",
1353b40ce416SJulian Elischer 			devtoname(dev), cmd, addr, flags, td);
13548f8def9eSPoul-Henning Kamp 
13559b00ca19SPoul-Henning Kamp 	mdio = (struct md_ioctl *)addr;
13569b00ca19SPoul-Henning Kamp 	if (mdio->md_version != MDIOVERSION)
13579b00ca19SPoul-Henning Kamp 		return (EINVAL);
13589b00ca19SPoul-Henning Kamp 
135953d745bcSDima Dorfman 	/*
136053d745bcSDima Dorfman 	 * We assert the version number in the individual ioctl
136153d745bcSDima Dorfman 	 * handlers instead of out here because (a) it is possible we
136253d745bcSDima Dorfman 	 * may add another ioctl in the future which doesn't read an
136353d745bcSDima Dorfman 	 * mdio, and (b) the correct return value for an unknown ioctl
136453d745bcSDima Dorfman 	 * is ENOIOCTL, not EINVAL.
136553d745bcSDima Dorfman 	 */
13669b00ca19SPoul-Henning Kamp 	error = 0;
13678f8def9eSPoul-Henning Kamp 	switch (cmd) {
13688f8def9eSPoul-Henning Kamp 	case MDIOCATTACH:
13698f8def9eSPoul-Henning Kamp 		switch (mdio->md_type) {
13708f8def9eSPoul-Henning Kamp 		case MD_MALLOC:
13718f8def9eSPoul-Henning Kamp 		case MD_PRELOAD:
13728f8def9eSPoul-Henning Kamp 		case MD_VNODE:
13738f8def9eSPoul-Henning Kamp 		case MD_SWAP:
1374b830359bSPawel Jakub Dawidek 			break;
13758f8def9eSPoul-Henning Kamp 		default:
13768f8def9eSPoul-Henning Kamp 			return (EINVAL);
13778f8def9eSPoul-Henning Kamp 		}
13788cb51643SJaakko Heinonen 		if (mdio->md_sectorsize == 0)
13798cb51643SJaakko Heinonen 			sectsize = DEV_BSIZE;
13808cb51643SJaakko Heinonen 		else
13818cb51643SJaakko Heinonen 			sectsize = mdio->md_sectorsize;
13828cb51643SJaakko Heinonen 		if (sectsize > MAXPHYS || mdio->md_mediasize < sectsize)
13838cb51643SJaakko Heinonen 			return (EINVAL);
13847ee3c044SPawel Jakub Dawidek 		if (mdio->md_options & MD_AUTOUNIT)
1385947fc8deSPoul-Henning Kamp 			sc = mdnew(-1, &error, mdio->md_type);
1386f4e7c5a8SJaakko Heinonen 		else {
1387f4e7c5a8SJaakko Heinonen 			if (mdio->md_unit > INT_MAX)
1388f4e7c5a8SJaakko Heinonen 				return (EINVAL);
1389947fc8deSPoul-Henning Kamp 			sc = mdnew(mdio->md_unit, &error, mdio->md_type);
1390f4e7c5a8SJaakko Heinonen 		}
1391b830359bSPawel Jakub Dawidek 		if (sc == NULL)
13927ee3c044SPawel Jakub Dawidek 			return (error);
13937ee3c044SPawel Jakub Dawidek 		if (mdio->md_options & MD_AUTOUNIT)
13947ee3c044SPawel Jakub Dawidek 			mdio->md_unit = sc->unit;
1395b830359bSPawel Jakub Dawidek 		sc->mediasize = mdio->md_mediasize;
13968cb51643SJaakko Heinonen 		sc->sectorsize = sectsize;
1397b830359bSPawel Jakub Dawidek 		error = EDOOFUS;
1398b830359bSPawel Jakub Dawidek 		switch (sc->type) {
1399b830359bSPawel Jakub Dawidek 		case MD_MALLOC:
14009b00ca19SPoul-Henning Kamp 			sc->start = mdstart_malloc;
1401b830359bSPawel Jakub Dawidek 			error = mdcreate_malloc(sc, mdio);
1402b830359bSPawel Jakub Dawidek 			break;
1403b830359bSPawel Jakub Dawidek 		case MD_PRELOAD:
1404734e78dfSJaakko Heinonen 			/*
1405734e78dfSJaakko Heinonen 			 * We disallow attaching preloaded memory disks via
1406734e78dfSJaakko Heinonen 			 * ioctl. Preloaded memory disks are automatically
1407734e78dfSJaakko Heinonen 			 * attached in g_md_init().
1408734e78dfSJaakko Heinonen 			 */
1409734e78dfSJaakko Heinonen 			error = EOPNOTSUPP;
1410b830359bSPawel Jakub Dawidek 			break;
1411b830359bSPawel Jakub Dawidek 		case MD_VNODE:
14129b00ca19SPoul-Henning Kamp 			sc->start = mdstart_vnode;
1413b830359bSPawel Jakub Dawidek 			error = mdcreate_vnode(sc, mdio, td);
1414b830359bSPawel Jakub Dawidek 			break;
1415b830359bSPawel Jakub Dawidek 		case MD_SWAP:
14169b00ca19SPoul-Henning Kamp 			sc->start = mdstart_swap;
1417b830359bSPawel Jakub Dawidek 			error = mdcreate_swap(sc, mdio, td);
1418b830359bSPawel Jakub Dawidek 			break;
1419b830359bSPawel Jakub Dawidek 		}
1420b830359bSPawel Jakub Dawidek 		if (error != 0) {
1421b830359bSPawel Jakub Dawidek 			mddestroy(sc, td);
1422b830359bSPawel Jakub Dawidek 			return (error);
1423b830359bSPawel Jakub Dawidek 		}
14249b00ca19SPoul-Henning Kamp 
14259b00ca19SPoul-Henning Kamp 		/* Prune off any residual fractional sector */
14269b00ca19SPoul-Henning Kamp 		i = sc->mediasize % sc->sectorsize;
14279b00ca19SPoul-Henning Kamp 		sc->mediasize -= i;
14289b00ca19SPoul-Henning Kamp 
1429b830359bSPawel Jakub Dawidek 		mdinit(sc);
1430b830359bSPawel Jakub Dawidek 		return (0);
14318f8def9eSPoul-Henning Kamp 	case MDIOCDETACH:
1432a9ebb311SEdward Tomasz Napierala 		if (mdio->md_mediasize != 0 ||
1433a9ebb311SEdward Tomasz Napierala 		    (mdio->md_options & ~MD_FORCE) != 0)
14348f8def9eSPoul-Henning Kamp 			return (EINVAL);
14359b00ca19SPoul-Henning Kamp 
14369b00ca19SPoul-Henning Kamp 		sc = mdfind(mdio->md_unit);
14379b00ca19SPoul-Henning Kamp 		if (sc == NULL)
14389b00ca19SPoul-Henning Kamp 			return (ENOENT);
1439a9ebb311SEdward Tomasz Napierala 		if (sc->opencount != 0 && !(sc->flags & MD_FORCE) &&
1440a9ebb311SEdward Tomasz Napierala 		    !(mdio->md_options & MD_FORCE))
14419b00ca19SPoul-Henning Kamp 			return (EBUSY);
14429b00ca19SPoul-Henning Kamp 		return (mddestroy(sc, td));
1443dc604f0cSEdward Tomasz Napierala 	case MDIOCRESIZE:
1444dc604f0cSEdward Tomasz Napierala 		if ((mdio->md_options & ~(MD_FORCE | MD_RESERVE)) != 0)
1445dc604f0cSEdward Tomasz Napierala 			return (EINVAL);
1446dc604f0cSEdward Tomasz Napierala 
1447dc604f0cSEdward Tomasz Napierala 		sc = mdfind(mdio->md_unit);
1448dc604f0cSEdward Tomasz Napierala 		if (sc == NULL)
1449dc604f0cSEdward Tomasz Napierala 			return (ENOENT);
14508cb51643SJaakko Heinonen 		if (mdio->md_mediasize < sc->sectorsize)
14518cb51643SJaakko Heinonen 			return (EINVAL);
1452dc604f0cSEdward Tomasz Napierala 		if (mdio->md_mediasize < sc->mediasize &&
1453dc604f0cSEdward Tomasz Napierala 		    !(sc->flags & MD_FORCE) &&
1454dc604f0cSEdward Tomasz Napierala 		    !(mdio->md_options & MD_FORCE))
1455dc604f0cSEdward Tomasz Napierala 			return (EBUSY);
1456dc604f0cSEdward Tomasz Napierala 		return (mdresize(sc, mdio));
1457174b5e9aSPoul-Henning Kamp 	case MDIOCQUERY:
1458174b5e9aSPoul-Henning Kamp 		sc = mdfind(mdio->md_unit);
1459174b5e9aSPoul-Henning Kamp 		if (sc == NULL)
1460174b5e9aSPoul-Henning Kamp 			return (ENOENT);
1461174b5e9aSPoul-Henning Kamp 		mdio->md_type = sc->type;
1462174b5e9aSPoul-Henning Kamp 		mdio->md_options = sc->flags;
1463b830359bSPawel Jakub Dawidek 		mdio->md_mediasize = sc->mediasize;
1464b830359bSPawel Jakub Dawidek 		mdio->md_sectorsize = sc->sectorsize;
14659b00ca19SPoul-Henning Kamp 		if (sc->type == MD_VNODE)
146688b5b78dSPawel Jakub Dawidek 			error = copyout(sc->file, mdio->md_file,
146788b5b78dSPawel Jakub Dawidek 			    strlen(sc->file) + 1);
146888b5b78dSPawel Jakub Dawidek 		return (error);
146916bcbe8cSPoul-Henning Kamp 	case MDIOCLIST:
147016bcbe8cSPoul-Henning Kamp 		i = 1;
147116bcbe8cSPoul-Henning Kamp 		LIST_FOREACH(sc, &md_softc_list, list) {
147216bcbe8cSPoul-Henning Kamp 			if (i == MDNPAD - 1)
147316bcbe8cSPoul-Henning Kamp 				mdio->md_pad[i] = -1;
147416bcbe8cSPoul-Henning Kamp 			else
147516bcbe8cSPoul-Henning Kamp 				mdio->md_pad[i++] = sc->unit;
147616bcbe8cSPoul-Henning Kamp 		}
147716bcbe8cSPoul-Henning Kamp 		mdio->md_pad[0] = i - 1;
147816bcbe8cSPoul-Henning Kamp 		return (0);
14798f8def9eSPoul-Henning Kamp 	default:
14808f8def9eSPoul-Henning Kamp 		return (ENOIOCTL);
14818f8def9eSPoul-Henning Kamp 	};
14829b00ca19SPoul-Henning Kamp }
14839b00ca19SPoul-Henning Kamp 
14849b00ca19SPoul-Henning Kamp static int
14859b00ca19SPoul-Henning Kamp mdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
14869b00ca19SPoul-Henning Kamp {
14879b00ca19SPoul-Henning Kamp 	int error;
14889b00ca19SPoul-Henning Kamp 
14899b00ca19SPoul-Henning Kamp 	sx_xlock(&md_sx);
14909b00ca19SPoul-Henning Kamp 	error = xmdctlioctl(dev, cmd, addr, flags, td);
14919b00ca19SPoul-Henning Kamp 	sx_xunlock(&md_sx);
14929b00ca19SPoul-Henning Kamp 	return (error);
14933f54a085SPoul-Henning Kamp }
14943f54a085SPoul-Henning Kamp 
149500a6a3c6SPoul-Henning Kamp static void
1496341b240dSJaakko Heinonen md_preloaded(u_char *image, size_t length, const char *name)
1497637f671aSPoul-Henning Kamp {
1498637f671aSPoul-Henning Kamp 	struct md_s *sc;
14999b00ca19SPoul-Henning Kamp 	int error;
1500637f671aSPoul-Henning Kamp 
1501947fc8deSPoul-Henning Kamp 	sc = mdnew(-1, &error, MD_PRELOAD);
1502637f671aSPoul-Henning Kamp 	if (sc == NULL)
1503637f671aSPoul-Henning Kamp 		return;
1504b830359bSPawel Jakub Dawidek 	sc->mediasize = length;
1505b830359bSPawel Jakub Dawidek 	sc->sectorsize = DEV_BSIZE;
1506637f671aSPoul-Henning Kamp 	sc->pl_ptr = image;
1507637f671aSPoul-Henning Kamp 	sc->pl_len = length;
15089b00ca19SPoul-Henning Kamp 	sc->start = mdstart_preload;
15095d4ca75eSLuigi Rizzo #ifdef MD_ROOT
1510637f671aSPoul-Henning Kamp 	if (sc->unit == 0)
151122ff74b2SMarcel Moolenaar 		rootdevnames[0] = MD_ROOT_FSTYPE ":/dev/md0";
15125d4ca75eSLuigi Rizzo #endif
1513637f671aSPoul-Henning Kamp 	mdinit(sc);
1514341b240dSJaakko Heinonen 	if (name != NULL) {
1515341b240dSJaakko Heinonen 		printf("%s%d: Preloaded image <%s> %zd bytes at %p\n",
1516341b240dSJaakko Heinonen 		    MD_NAME, sc->unit, name, length, image);
1517341b240dSJaakko Heinonen 	}
1518637f671aSPoul-Henning Kamp }
1519637f671aSPoul-Henning Kamp 
1520637f671aSPoul-Henning Kamp static void
152119945697SPoul-Henning Kamp g_md_init(struct g_class *mp __unused)
152200a6a3c6SPoul-Henning Kamp {
152395f1a897SPoul-Henning Kamp 	caddr_t mod;
152495f1a897SPoul-Henning Kamp 	u_char *ptr, *name, *type;
152595f1a897SPoul-Henning Kamp 	unsigned len;
1526d12fc952SKonstantin Belousov 	int i;
1527d12fc952SKonstantin Belousov 
1528d12fc952SKonstantin Belousov 	/* figure out log2(NINDIR) */
1529d12fc952SKonstantin Belousov 	for (i = NINDIR, nshift = -1; i; nshift++)
1530d12fc952SKonstantin Belousov 		i >>= 1;
153195f1a897SPoul-Henning Kamp 
15320a937206SPoul-Henning Kamp 	mod = NULL;
15339b00ca19SPoul-Henning Kamp 	sx_init(&md_sx, "MD config lock");
15340a937206SPoul-Henning Kamp 	g_topology_unlock();
1535f4e7c5a8SJaakko Heinonen 	md_uh = new_unrhdr(0, INT_MAX, NULL);
153671e4fff8SPoul-Henning Kamp #ifdef MD_ROOT_SIZE
15379b00ca19SPoul-Henning Kamp 	sx_xlock(&md_sx);
1538341b240dSJaakko Heinonen 	md_preloaded(mfs_root.start, sizeof(mfs_root.start), NULL);
15399b00ca19SPoul-Henning Kamp 	sx_xunlock(&md_sx);
154071e4fff8SPoul-Henning Kamp #endif
15419b00ca19SPoul-Henning Kamp 	/* XXX: are preload_* static or do they need Giant ? */
154295f1a897SPoul-Henning Kamp 	while ((mod = preload_search_next_name(mod)) != NULL) {
154395f1a897SPoul-Henning Kamp 		name = (char *)preload_search_info(mod, MODINFO_NAME);
154495f1a897SPoul-Henning Kamp 		if (name == NULL)
154595f1a897SPoul-Henning Kamp 			continue;
15469b00ca19SPoul-Henning Kamp 		type = (char *)preload_search_info(mod, MODINFO_TYPE);
154795f1a897SPoul-Henning Kamp 		if (type == NULL)
154895f1a897SPoul-Henning Kamp 			continue;
154971e4fff8SPoul-Henning Kamp 		if (strcmp(type, "md_image") && strcmp(type, "mfs_root"))
155095f1a897SPoul-Henning Kamp 			continue;
15518d5ac6c3SMarcel Moolenaar 		ptr = preload_fetch_addr(mod);
15528d5ac6c3SMarcel Moolenaar 		len = preload_fetch_size(mod);
15538d5ac6c3SMarcel Moolenaar 		if (ptr != NULL && len != 0) {
15549b00ca19SPoul-Henning Kamp 			sx_xlock(&md_sx);
1555341b240dSJaakko Heinonen 			md_preloaded(ptr, len, name);
15569b00ca19SPoul-Henning Kamp 			sx_xunlock(&md_sx);
155795f1a897SPoul-Henning Kamp 		}
15588d5ac6c3SMarcel Moolenaar 	}
155959ec9023SKonstantin Belousov 	md_vnode_pbuf_freecnt = nswbuf / 10;
156006d425f9SEd Schouten 	status_dev = make_dev(&mdctl_cdevsw, INT_MAX, UID_ROOT, GID_WHEEL,
156110b0e058SDima Dorfman 	    0600, MDCTL_NAME);
15620eb14309SPoul-Henning Kamp 	g_topology_lock();
156300a6a3c6SPoul-Henning Kamp }
156400a6a3c6SPoul-Henning Kamp 
156519945697SPoul-Henning Kamp static void
1566c27a8954SWojciech A. Koszek g_md_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1567c27a8954SWojciech A. Koszek     struct g_consumer *cp __unused, struct g_provider *pp)
1568c27a8954SWojciech A. Koszek {
1569c27a8954SWojciech A. Koszek 	struct md_s *mp;
1570c27a8954SWojciech A. Koszek 	char *type;
1571c27a8954SWojciech A. Koszek 
1572c27a8954SWojciech A. Koszek 	mp = gp->softc;
1573c27a8954SWojciech A. Koszek 	if (mp == NULL)
1574c27a8954SWojciech A. Koszek 		return;
1575c27a8954SWojciech A. Koszek 
1576c27a8954SWojciech A. Koszek 	switch (mp->type) {
1577c27a8954SWojciech A. Koszek 	case MD_MALLOC:
1578c27a8954SWojciech A. Koszek 		type = "malloc";
1579c27a8954SWojciech A. Koszek 		break;
1580c27a8954SWojciech A. Koszek 	case MD_PRELOAD:
1581c27a8954SWojciech A. Koszek 		type = "preload";
1582c27a8954SWojciech A. Koszek 		break;
1583c27a8954SWojciech A. Koszek 	case MD_VNODE:
1584c27a8954SWojciech A. Koszek 		type = "vnode";
1585c27a8954SWojciech A. Koszek 		break;
1586c27a8954SWojciech A. Koszek 	case MD_SWAP:
1587c27a8954SWojciech A. Koszek 		type = "swap";
1588c27a8954SWojciech A. Koszek 		break;
1589c27a8954SWojciech A. Koszek 	default:
1590c27a8954SWojciech A. Koszek 		type = "unknown";
1591c27a8954SWojciech A. Koszek 		break;
1592c27a8954SWojciech A. Koszek 	}
1593c27a8954SWojciech A. Koszek 
1594c27a8954SWojciech A. Koszek 	if (pp != NULL) {
1595c27a8954SWojciech A. Koszek 		if (indent == NULL) {
1596c27a8954SWojciech A. Koszek 			sbuf_printf(sb, " u %d", mp->unit);
1597c27a8954SWojciech A. Koszek 			sbuf_printf(sb, " s %ju", (uintmax_t) mp->sectorsize);
1598c27a8954SWojciech A. Koszek 			sbuf_printf(sb, " f %ju", (uintmax_t) mp->fwheads);
1599c27a8954SWojciech A. Koszek 			sbuf_printf(sb, " fs %ju", (uintmax_t) mp->fwsectors);
1600c27a8954SWojciech A. Koszek 			sbuf_printf(sb, " l %ju", (uintmax_t) mp->mediasize);
1601c27a8954SWojciech A. Koszek 			sbuf_printf(sb, " t %s", type);
1602c27a8954SWojciech A. Koszek 			if (mp->type == MD_VNODE && mp->vnode != NULL)
1603c27a8954SWojciech A. Koszek 				sbuf_printf(sb, " file %s", mp->file);
1604c27a8954SWojciech A. Koszek 		} else {
1605c27a8954SWojciech A. Koszek 			sbuf_printf(sb, "%s<unit>%d</unit>\n", indent,
1606c27a8954SWojciech A. Koszek 			    mp->unit);
1607c27a8954SWojciech A. Koszek 			sbuf_printf(sb, "%s<sectorsize>%ju</sectorsize>\n",
1608c27a8954SWojciech A. Koszek 			    indent, (uintmax_t) mp->sectorsize);
1609c27a8954SWojciech A. Koszek 			sbuf_printf(sb, "%s<fwheads>%ju</fwheads>\n",
1610c27a8954SWojciech A. Koszek 			    indent, (uintmax_t) mp->fwheads);
1611c27a8954SWojciech A. Koszek 			sbuf_printf(sb, "%s<fwsectors>%ju</fwsectors>\n",
1612c27a8954SWojciech A. Koszek 			    indent, (uintmax_t) mp->fwsectors);
1613c27a8954SWojciech A. Koszek 			sbuf_printf(sb, "%s<length>%ju</length>\n",
1614c27a8954SWojciech A. Koszek 			    indent, (uintmax_t) mp->mediasize);
16151f192809SAndrey V. Elsukov 			sbuf_printf(sb, "%s<compression>%s</compression>\n", indent,
16161f192809SAndrey V. Elsukov 			    (mp->flags & MD_COMPRESS) == 0 ? "off": "on");
16171f192809SAndrey V. Elsukov 			sbuf_printf(sb, "%s<access>%s</access>\n", indent,
16181f192809SAndrey V. Elsukov 			    (mp->flags & MD_READONLY) == 0 ? "read-write":
16191f192809SAndrey V. Elsukov 			    "read-only");
1620c27a8954SWojciech A. Koszek 			sbuf_printf(sb, "%s<type>%s</type>\n", indent,
1621c27a8954SWojciech A. Koszek 			    type);
1622c27a8954SWojciech A. Koszek 			if (mp->type == MD_VNODE && mp->vnode != NULL)
1623c27a8954SWojciech A. Koszek 				sbuf_printf(sb, "%s<file>%s</file>\n",
1624c27a8954SWojciech A. Koszek 				    indent, mp->file);
1625c27a8954SWojciech A. Koszek 		}
1626c27a8954SWojciech A. Koszek 	}
1627c27a8954SWojciech A. Koszek }
1628c27a8954SWojciech A. Koszek 
1629c27a8954SWojciech A. Koszek static void
163019945697SPoul-Henning Kamp g_md_fini(struct g_class *mp __unused)
163157e9624eSPoul-Henning Kamp {
16329d4b5945SMaxim Sobolev 
16339b00ca19SPoul-Henning Kamp 	sx_destroy(&md_sx);
163419945697SPoul-Henning Kamp 	if (status_dev != NULL)
163557e9624eSPoul-Henning Kamp 		destroy_dev(status_dev);
1636f4e7c5a8SJaakko Heinonen 	delete_unrhdr(md_uh);
163757e9624eSPoul-Henning Kamp }
1638