xref: /freebsd/sys/geom/geom_vfs.c (revision d056fa046c6a91b90cd98165face0e42a33a5173)
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 #include <sys/mount.h>	/* XXX Temporary for VFS_LOCK_GIANT */
37 
38 #include <geom/geom.h>
39 #include <geom/geom_vfs.h>
40 
41 /*
42  * subroutines for use by filesystems.
43  *
44  * XXX: should maybe live somewhere else ?
45  */
46 #include <sys/buf.h>
47 
48 static struct buf_ops __g_vfs_bufops = {
49 	.bop_name =	"GEOM_VFS",
50 	.bop_write =	bufwrite,
51 	.bop_strategy =	g_vfs_strategy,
52 	.bop_sync =	bufsync,
53 };
54 
55 struct buf_ops *g_vfs_bufops = &__g_vfs_bufops;
56 
57 static g_orphan_t g_vfs_orphan;
58 
59 static struct g_class g_vfs_class = {
60 	.name =		"VFS",
61 	.version =	G_VERSION,
62 	.orphan =	g_vfs_orphan,
63 };
64 
65 DECLARE_GEOM_CLASS(g_vfs_class, g_vfs);
66 
67 static void
68 g_vfs_done(struct bio *bip)
69 {
70 	struct buf *bp;
71 	int vfslocked;
72 
73 	if (bip->bio_error) {
74 		printf("g_vfs_done():");
75 		g_print_bio(bip);
76 		printf("error = %d\n", bip->bio_error);
77 	}
78 	bp = bip->bio_caller2;
79 	bp->b_error = bip->bio_error;
80 	bp->b_ioflags = bip->bio_flags;
81 	if (bip->bio_error)
82 		bp->b_ioflags |= BIO_ERROR;
83 	bp->b_resid = bp->b_bcount - bip->bio_completed;
84 	g_destroy_bio(bip);
85 	vfslocked = VFS_LOCK_GIANT(((struct mount *)NULL));
86 	bufdone(bp);
87 	VFS_UNLOCK_GIANT(vfslocked);
88 }
89 
90 void
91 g_vfs_strategy(struct bufobj *bo, struct buf *bp)
92 {
93 	struct g_consumer *cp;
94 	struct bio *bip;
95 
96 	cp = bo->bo_private;
97 	G_VALID_CONSUMER(cp);
98 
99 	bip = g_alloc_bio();
100 	bip->bio_cmd = bp->b_iocmd;
101 	bip->bio_offset = bp->b_iooffset;
102 	bip->bio_data = bp->b_data;
103 	bip->bio_done = g_vfs_done;
104 	bip->bio_caller2 = bp;
105 	bip->bio_length = bp->b_bcount;
106 	g_io_request(bip, cp);
107 }
108 
109 static void
110 g_vfs_orphan(struct g_consumer *cp)
111 {
112 
113 	/*
114 	 * Don't do anything here yet.
115 	 *
116 	 * Ideally we should detach the consumer already now, but that
117 	 * leads to a locking requirement in the I/O path to see if we have
118 	 * a consumer or not.  Considering how ugly things are going to get
119 	 * anyway as none of our filesystems are graceful about i/o errors,
120 	 * this is not important right now.
121 	 *
122 	 * Down the road, this is the place where we could give the user
123 	 * a "Abort, Retry or Ignore" option to replace the media again.
124 	 */
125 }
126 
127 int
128 g_vfs_open(struct vnode *vp, struct g_consumer **cpp, const char *fsname, int wr)
129 {
130 	struct g_geom *gp;
131 	struct g_provider *pp;
132 	struct g_consumer *cp;
133 	struct bufobj *bo;
134 	int vfslocked;
135 	int error;
136 
137 	g_topology_assert();
138 
139 	*cpp = NULL;
140 	pp = g_dev_getprovider(vp->v_rdev);
141 	if (pp == NULL)
142 		return (ENOENT);
143 	gp = g_new_geomf(&g_vfs_class, "%s.%s", fsname, pp->name);
144 	cp = g_new_consumer(gp);
145 	g_attach(cp, pp);
146 	error = g_access(cp, 1, wr, 1);
147 	if (error) {
148 		g_wither_geom(gp, ENXIO);
149 		return (error);
150 	}
151 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
152 	vnode_create_vobject(vp, pp->mediasize, curthread);
153 	VFS_UNLOCK_GIANT(vfslocked);
154 	*cpp = cp;
155 	bo = &vp->v_bufobj;
156 	bo->bo_ops = g_vfs_bufops;
157 	bo->bo_private = cp;
158 	bo->bo_bsize = pp->sectorsize;
159 	gp->softc = bo;
160 
161 	return (error);
162 }
163 
164 void
165 g_vfs_close(struct g_consumer *cp, struct thread *td)
166 {
167 	struct g_geom *gp;
168 	struct bufobj *bo;
169 
170 	g_topology_assert();
171 
172 	gp = cp->geom;
173 	bo = gp->softc;
174 	bufobj_invalbuf(bo, V_SAVE, td, 0, 0);
175 	g_wither_geom_close(gp, ENXIO);
176 }
177