sysvbfs_vfsops.c revision 1.22 1 /* $NetBSD: sysvbfs_vfsops.c,v 1.22 2008/01/24 17:32:54 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: sysvbfs_vfsops.c,v 1.22 2008/01/24 17:32:54 ad Exp $");
41
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/pool.h>
46 #include <sys/time.h>
47 #include <sys/ucred.h>
48 #include <sys/mount.h>
49 #include <sys/disklabel.h>
50 #include <sys/fcntl.h>
51 #include <sys/malloc.h>
52 #include <sys/kauth.h>
53 #include <sys/proc.h>
54
55 /* v-node */
56 #include <sys/namei.h>
57 #include <sys/vnode.h>
58 /* devsw */
59 #include <sys/conf.h>
60
61 #include <fs/sysvbfs/sysvbfs.h> /* external interface */
62 #include <fs/sysvbfs/bfs.h> /* internal interface */
63
64 #ifdef SYSVBFS_VNOPS_DEBUG
65 #define DPRINTF(fmt, args...) printf(fmt, ##args)
66 #else
67 #define DPRINTF(arg...) ((void)0)
68 #endif
69
70 MALLOC_JUSTDEFINE(M_SYSVBFS_VFS, "sysvbfs vfs", "sysvbfs vfs structures");
71
72 struct pool sysvbfs_node_pool;
73
74 int sysvbfs_mountfs(struct vnode *, struct mount *, struct lwp *);
75
76 int
77 sysvbfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
78 {
79 struct lwp *l = curlwp;
80 struct nameidata nd;
81 struct sysvbfs_args *args = data;
82 struct sysvbfs_mount *bmp = NULL;
83 struct vnode *devvp = NULL;
84 int error;
85 bool update;
86
87 DPRINTF("%s: mnt_flag=%x\n", __func__, mp->mnt_flag);
88
89 if (*data_len < sizeof *args)
90 return EINVAL;
91
92 if (mp->mnt_flag & MNT_GETARGS) {
93 if ((bmp = (void *)mp->mnt_data) == NULL)
94 return EIO;
95 args->fspec = NULL;
96 *data_len = sizeof *args;
97 return 0;
98 }
99
100
101 DPRINTF("%s: args->fspec=%s\n", __func__, args->fspec);
102 update = mp->mnt_flag & MNT_UPDATE;
103 if (args->fspec == NULL) {
104 /* nothing to do. */
105 return EINVAL;
106 }
107
108 if (args->fspec != NULL) {
109 /* Look up the name and verify that it's sane. */
110 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec);
111 if ((error = namei(&nd)) != 0)
112 return (error);
113 devvp = nd.ni_vp;
114
115 if (!update) {
116 /*
117 * Be sure this is a valid block device
118 */
119 if (devvp->v_type != VBLK)
120 error = ENOTBLK;
121 else if (bdevsw_lookup(devvp->v_rdev) == NULL)
122 error = ENXIO;
123 } else {
124 /*
125 * Be sure we're still naming the same device
126 * used for our initial mount
127 */
128 if (devvp != bmp->devvp)
129 error = EINVAL;
130 }
131 }
132
133 /*
134 * If mount by non-root, then verify that user has necessary
135 * permissions on the device.
136 */
137 if (error == 0 && kauth_authorize_generic(l->l_cred,
138 KAUTH_GENERIC_ISSUSER, NULL)) {
139 int accessmode = VREAD;
140 if (update ?
141 (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
142 (mp->mnt_flag & MNT_RDONLY) == 0)
143 accessmode |= VWRITE;
144 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
145 error = VOP_ACCESS(devvp, accessmode, l->l_cred);
146 VOP_UNLOCK(devvp, 0);
147 }
148
149 if (error) {
150 vrele(devvp);
151 return error;
152 }
153
154 if (!update) {
155 if ((error = sysvbfs_mountfs(devvp, mp, l)) != 0) {
156 vrele(devvp);
157 return error;
158 }
159 } else if (mp->mnt_flag & MNT_RDONLY) {
160 /* XXX: r/w -> read only */
161 }
162
163 return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
164 mp->mnt_op->vfs_name, mp, l);
165 }
166
167 int
168 sysvbfs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
169 {
170 kauth_cred_t cred = l->l_cred;
171 struct sysvbfs_mount *bmp;
172 struct partinfo dpart;
173 int error;
174
175 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
176 error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
177 VOP_UNLOCK(devvp, 0);
178 if (error)
179 return error;
180
181 /* Open block device */
182 if ((error = VOP_OPEN(devvp, FREAD, NOCRED)) != 0)
183 return error;
184
185 /* Get partition information */
186 if ((error = VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred)) != 0)
187 return error;
188
189 bmp = malloc(sizeof(struct sysvbfs_mount), M_SYSVBFS_VFS, M_WAITOK);
190 if (bmp == NULL)
191 return ENOMEM;
192 bmp->devvp = devvp;
193 bmp->mountp = mp;
194 if ((error = sysvbfs_bfs_init(&bmp->bfs, devvp)) != 0) {
195 free(bmp, M_SYSVBFS_VFS);
196 return error;
197 }
198 LIST_INIT(&bmp->bnode_head);
199
200 mp->mnt_data = bmp;
201 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)devvp->v_rdev;
202 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_SYSVBFS);
203 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
204 mp->mnt_flag |= MNT_LOCAL;
205 mp->mnt_dev_bshift = BFS_BSHIFT;
206 mp->mnt_fs_bshift = BFS_BSHIFT;
207
208 DPRINTF("fstype=%d dtype=%d bsize=%d\n", dpart.part->p_fstype,
209 dpart.disklab->d_type, dpart.disklab->d_secsize);
210
211 return 0;
212 }
213
214 int
215 sysvbfs_start(struct mount *mp, int flags)
216 {
217
218 DPRINTF("%s:\n", __func__);
219 /* Nothing to do. */
220 return 0;
221 }
222
223 int
224 sysvbfs_unmount(struct mount *mp, int mntflags)
225 {
226 struct sysvbfs_mount *bmp = (void *)mp->mnt_data;
227 int error;
228
229 DPRINTF("%s: %p\n", __func__, bmp);
230
231 if ((error = vflush(mp, NULLVP,
232 mntflags & MNT_FORCE ? FORCECLOSE : 0)) != 0)
233 return error;
234
235 vn_lock(bmp->devvp, LK_EXCLUSIVE | LK_RETRY);
236 error = VOP_CLOSE(bmp->devvp, FREAD, NOCRED);
237 vput(bmp->devvp);
238
239 sysvbfs_bfs_fini(bmp->bfs);
240
241 free(bmp, M_SYSVBFS_VFS);
242 mp->mnt_data = NULL;
243 mp->mnt_flag &= ~MNT_LOCAL;
244
245 return 0;
246 }
247
248 int
249 sysvbfs_root(struct mount *mp, struct vnode **vpp)
250 {
251 struct vnode *vp;
252 int error;
253
254 DPRINTF("%s:\n", __func__);
255 if ((error = VFS_VGET(mp, BFS_ROOT_INODE, &vp)) != 0)
256 return error;
257 *vpp = vp;
258
259 return 0;
260 }
261
262 int
263 sysvbfs_statvfs(struct mount *mp, struct statvfs *f)
264 {
265 struct sysvbfs_mount *bmp = mp->mnt_data;
266 struct bfs *bfs = bmp->bfs;
267 int free_block;
268 size_t data_block;
269
270 data_block = (bfs->data_end - bfs->data_start) >> BFS_BSHIFT;
271 if (bfs_inode_alloc(bfs, 0, 0, &free_block) != 0)
272 free_block = 0;
273 else
274 free_block = (bfs->data_end >> BFS_BSHIFT) - free_block;
275
276 DPRINTF("%s: %d %d %d\n", __func__, bfs->data_start,
277 bfs->data_end, free_block);
278
279 f->f_bsize = BFS_BSIZE;
280 f->f_frsize = BFS_BSIZE;
281 f->f_iosize = BFS_BSIZE;
282 f->f_blocks = data_block;
283 f->f_bfree = free_block;
284 f->f_bavail = f->f_bfree;
285 f->f_bresvd = 0;
286 f->f_files = bfs->n_inode;
287 f->f_ffree = bfs->max_inode - f->f_files;
288 f->f_favail = f->f_ffree;
289 f->f_fresvd = 0;
290 copy_statvfs_info(f, mp);
291
292 return 0;
293 }
294
295 int
296 sysvbfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
297 {
298 struct sysvbfs_mount *bmp = mp->mnt_data;
299 struct sysvbfs_node *bnode;
300 struct vnode *v;
301 int err, error;
302
303 DPRINTF("%s:\n", __func__);
304 error = 0;
305 mutex_enter(&mntvnode_lock);
306 for (bnode = LIST_FIRST(&bmp->bnode_head); bnode != NULL;
307 bnode = LIST_NEXT(bnode, link)) {
308 v = bnode->vnode;
309 mutex_enter(&v->v_interlock);
310 mutex_exit(&mntvnode_lock);
311 err = vget(v, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
312 if (err == 0) {
313 err = VOP_FSYNC(v, cred, FSYNC_WAIT, 0, 0);
314 vput(v);
315 }
316 if (err != 0)
317 error = err;
318 mutex_enter(&mntvnode_lock);
319 }
320 mutex_exit(&mntvnode_lock);
321
322 return error;
323 }
324
325 int
326 sysvbfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
327 {
328 struct sysvbfs_mount *bmp = mp->mnt_data;
329 struct bfs *bfs = bmp->bfs;
330 struct vnode *vp;
331 struct sysvbfs_node *bnode;
332 struct bfs_inode *inode;
333 int error;
334
335 DPRINTF("%s: i-node=%d\n", __func__, ino);
336 /* Lookup requested i-node */
337 if (!bfs_inode_lookup(bfs, ino, &inode)) {
338 DPRINTF("bfs_inode_lookup failed.\n");
339 return ENOENT;
340 }
341 for (bnode = LIST_FIRST(&bmp->bnode_head); bnode != NULL;
342 bnode = LIST_NEXT(bnode, link)) {
343 if (bnode->inode->number == ino) {
344 *vpp = bnode->vnode;
345 }
346 }
347
348 /* Allocate v-node. */
349 if ((error = getnewvnode(VT_SYSVBFS, mp, sysvbfs_vnodeop_p, &vp)) !=
350 0) {
351 DPRINTF("%s: getnewvnode error.\n", __func__);
352 return error;
353 }
354 /* Lock vnode here */
355 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
356
357 /* Allocate i-node */
358 vp->v_data = pool_get(&sysvbfs_node_pool, PR_WAITOK);
359 memset(vp->v_data, 0, sizeof(struct sysvbfs_node));
360 bnode = vp->v_data;
361 mutex_enter(&mntvnode_lock);
362 LIST_INSERT_HEAD(&bmp->bnode_head, bnode, link);
363 mutex_exit(&mntvnode_lock);
364 bnode->vnode = vp;
365 bnode->bmp = bmp;
366 bnode->inode = inode;
367 bnode->lockf = NULL; /* advlock */
368
369 if (ino == BFS_ROOT_INODE) { /* BFS is flat filesystem */
370 vp->v_type = VDIR;
371 vp->v_vflag |= VV_ROOT;
372 } else {
373 vp->v_type = VREG;
374 }
375
376 genfs_node_init(vp, &sysvbfs_genfsops);
377 uvm_vnp_setsize(vp, bfs_file_size(inode));
378 *vpp = vp;
379
380 return 0;
381 }
382
383 int
384 sysvbfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
385 {
386
387 DPRINTF("%s:\n", __func__);
388 /* notyet */
389 return EOPNOTSUPP;
390 }
391
392 int
393 sysvbfs_vptofh(struct vnode *vpp, struct fid *fid, size_t *fh_size)
394 {
395
396 DPRINTF("%s:\n", __func__);
397 /* notyet */
398 return EOPNOTSUPP;
399 }
400
401 MALLOC_DECLARE(M_BFS);
402 MALLOC_DECLARE(M_SYSVBFS_VNODE);
403
404 void
405 sysvbfs_init(void)
406 {
407
408 DPRINTF("%s:\n", __func__);
409 malloc_type_attach(M_SYSVBFS_VFS);
410 malloc_type_attach(M_BFS);
411 malloc_type_attach(M_SYSVBFS_VNODE);
412 pool_init(&sysvbfs_node_pool, sizeof(struct sysvbfs_node), 0, 0, 0,
413 "sysvbfs_node_pool", &pool_allocator_nointr, IPL_NONE);
414 }
415
416 void
417 sysvbfs_reinit(void)
418 {
419
420 /* Nothing to do. */
421 DPRINTF("%s:\n", __func__);
422 }
423
424 void
425 sysvbfs_done(void)
426 {
427
428 DPRINTF("%s:\n", __func__);
429 pool_destroy(&sysvbfs_node_pool);
430 malloc_type_detach(M_BFS);
431 malloc_type_detach(M_SYSVBFS_VFS);
432 malloc_type_detach(M_SYSVBFS_VNODE);
433 }
434
435 int
436 sysvbfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
437 kauth_cred_t cred)
438 {
439
440 return 0;
441 }
442