xref: /freebsd/sys/geom/geom_vfs.c (revision 7562eaabc01a48e6b11d5b558c41e3b92dae5c2d)
1 /*-
2  * Copyright (c) 2004 Poul-Henning Kamp
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bio.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/vnode.h>
36 
37 #include <geom/geom.h>
38 #include <geom/geom_vfs.h>
39 
40 /*
41  * subroutines for use by filesystems.
42  *
43  * XXX: should maybe live somewhere else ?
44  */
45 #include <sys/buf.h>
46 
47 static struct buf_ops __g_vfs_bufops = {
48 	.bop_name =	"GEOM_VFS",
49 	.bop_write =	bufwrite,
50 	.bop_strategy =	g_vfs_strategy,
51 };
52 
53 struct buf_ops *g_vfs_bufops = &__g_vfs_bufops;
54 
55 static struct g_class g_vfs_class = {
56 	.name =		"VFS",
57 	.version =	G_VERSION,
58 	.orphan =	g_vfs_orphan,
59 };
60 
61 DECLARE_GEOM_CLASS(g_vfs_class, g_vfs);
62 
63 static void
64 g_vfs_done(struct bio *bip)
65 {
66 	struct buf *bp;
67 
68 	if (bip->bio_error) {
69 		printf("g_vfs_done():");
70 		g_print_bio(bip);
71 		printf("error = %d\n", bip->bio_error);
72 	}
73 	bp = bip->bio_caller2;
74 	bp->b_error = bip->bio_error;
75 	bp->b_ioflags = bip->bio_flags;
76 	if (bip->bio_error)
77 		bp->b_ioflags |= BIO_ERROR;
78 	bp->b_resid = bp->b_bcount - bip->bio_completed;
79 	g_destroy_bio(bip);
80 	mtx_lock(&Giant);
81 	bufdone(bp);
82 	mtx_unlock(&Giant);
83 }
84 
85 void
86 g_vfs_strategy(struct bufobj *bo, struct buf *bp)
87 {
88 	struct g_consumer *cp;
89 	struct bio *bip;
90 
91 	cp = bo->bo_private;
92 	G_VALID_CONSUMER(cp);
93 
94 	bip = g_alloc_bio();
95 	bip->bio_cmd = bp->b_iocmd;
96 	bip->bio_offset = bp->b_iooffset;
97 	bip->bio_data = bp->b_data;
98 	bip->bio_done = g_vfs_done;
99 	bip->bio_caller2 = bp;
100 	bip->bio_length = bp->b_bcount;
101 	g_io_request(bip, cp);
102 }
103 
104 void
105 g_vfs_orphan(struct g_consumer *cp)
106 {
107 
108 	/*
109 	 * Don't do anything here yet.
110 	 *
111 	 * Ideally we should detach the consumer already now, but that
112 	 * leads to a locking requirement in the I/O path to see if we have
113 	 * a consumer or not.  Considering how ugly things are going to get
114 	 * anyway as none of our filesystems are graceful about i/o errors,
115 	 * this is not important right now.
116 	 *
117 	 * Down the road, this is the place where we could give the user
118 	 * a "Abort, Retry or Ignore" option to replace the media again.
119 	 */
120 }
121 
122 int
123 g_vfs_open(struct vnode *vp, struct g_consumer **cpp, const char *fsname, int wr)
124 {
125 	struct g_geom *gp;
126 	struct g_provider *pp;
127 	struct g_consumer *cp;
128 	struct bufobj *bo;
129 	int error;
130 
131 	g_topology_assert();
132 
133 	*cpp = NULL;
134 	pp = g_dev_getprovider(vp->v_rdev);
135 	if (pp == NULL)
136 		return (ENOENT);
137 	gp = g_new_geomf(&g_vfs_class, "%s.%s", fsname, pp->name);
138 	cp = g_new_consumer(gp);
139 	g_attach(cp, pp);
140 	error = g_access(cp, 1, wr, 1);
141 	if (error) {
142 		g_wither_geom(gp, ENXIO);
143 		return (error);
144 	}
145 	*cpp = cp;
146 	bo = &vp->v_bufobj;
147 	bo->bo_ops = g_vfs_bufops;
148 	bo->bo_private = cp;
149 	bo->bo_bsize = pp->sectorsize;
150 
151 	return (error);
152 }
153