sysvbfs_vfsops.c revision 1.13 1 /* $NetBSD: sysvbfs_vfsops.c,v 1.13 2007/07/17 11:19:33 pooka 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.13 2007/07/17 11:19:33 pooka Exp $");
41
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/pool.h>
46
47 #include <sys/time.h>
48 #include <sys/ucred.h>
49 #include <sys/mount.h>
50 #include <sys/disklabel.h>
51 #include <sys/fcntl.h>
52 #include <sys/malloc.h>
53 #include <sys/kauth.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 struct nameidata *ndp, struct lwp *l)
79 {
80 struct sysvbfs_args *args = data;
81 struct sysvbfs_mount *bmp = NULL;
82 struct vnode *devvp = NULL;
83 int error;
84 bool update;
85
86 DPRINTF("%s: mnt_flag=%x\n", __FUNCTION__, mp->mnt_flag);
87
88 if (*data_len < sizeof *args)
89 return EINVAL;
90
91 if (mp->mnt_flag & MNT_GETARGS) {
92 if ((bmp = (void *)mp->mnt_data) == NULL)
93 return EIO;
94 args->fspec = NULL;
95 *data_len = sizeof *args;
96 return 0;
97 }
98
99
100 DPRINTF("%s: args->fspec=%s\n", __FUNCTION__, args->fspec);
101 update = mp->mnt_flag & MNT_UPDATE;
102 if (args->fspec == NULL) {
103 /* nothing to do. */
104 return EINVAL;
105 }
106
107 if (args->fspec != NULL) {
108 /* Look up the name and verify that it's sane. */
109 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec, l);
110 if ((error = namei(ndp)) != 0)
111 return (error);
112 devvp = ndp->ni_vp;
113
114 if (!update) {
115 /*
116 * Be sure this is a valid block device
117 */
118 if (devvp->v_type != VBLK)
119 error = ENOTBLK;
120 else if (bdevsw_lookup(devvp->v_specinfo->si_rdev) ==
121 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, l);
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 if ((error = vfs_mountedon(devvp)) != 0)
176 return error; /* Already mounted */
177 if (vcount(devvp) > 1)
178 return EBUSY; /* Opened by other */
179 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
180 error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
181 VOP_UNLOCK(devvp, 0);
182 if (error)
183 return error;
184
185 /* Open block device */
186 if ((error = VOP_OPEN(devvp, FREAD, NOCRED, l)) != 0)
187 return error;
188
189 /* Get partition information */
190 if ((error = VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, l)) != 0)
191 return error;
192
193 bmp = malloc(sizeof(struct sysvbfs_mount), M_SYSVBFS_VFS, M_WAITOK);
194 if (bmp == NULL)
195 return ENOMEM;
196 bmp->devvp = devvp;
197 bmp->mountp = mp;
198 if ((error = sysvbfs_bfs_init(&bmp->bfs, devvp)) != 0) {
199 free(bmp, M_SYSVBFS_VFS);
200 return error;
201 }
202 LIST_INIT(&bmp->bnode_head);
203
204 mp->mnt_data = bmp;
205 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)devvp->v_rdev;
206 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_SYSVBFS);
207 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
208 mp->mnt_flag |= MNT_LOCAL;
209 mp->mnt_dev_bshift = BFS_BSHIFT;
210 mp->mnt_fs_bshift = BFS_BSHIFT;
211
212 DPRINTF("fstype=%d dtype=%d bsize=%d\n", dpart.part->p_fstype,
213 dpart.disklab->d_type, dpart.disklab->d_secsize);
214
215 return 0;
216 }
217
218 int
219 sysvbfs_start(struct mount *mp, int flags, struct lwp *l)
220 {
221
222 DPRINTF("%s:\n", __FUNCTION__);
223 /* Nothing to do. */
224 return 0;
225 }
226
227 int
228 sysvbfs_unmount(struct mount *mp, int mntflags, struct lwp *l)
229 {
230 struct sysvbfs_mount *bmp = (void *)mp->mnt_data;
231 int error;
232
233 DPRINTF("%s: %p\n", __FUNCTION__, bmp);
234
235 if ((error = vflush(mp, NULLVP,
236 mntflags & MNT_FORCE ? FORCECLOSE : 0)) != 0)
237 return error;
238
239 vn_lock(bmp->devvp, LK_EXCLUSIVE | LK_RETRY);
240 error = VOP_CLOSE(bmp->devvp, FREAD, NOCRED, l);
241 vput(bmp->devvp);
242
243 sysvbfs_bfs_fini(bmp->bfs);
244
245 free(bmp, M_SYSVBFS_VFS);
246 mp->mnt_data = NULL;
247 mp->mnt_flag &= ~MNT_LOCAL;
248
249 return 0;
250 }
251
252 int
253 sysvbfs_root(struct mount *mp, struct vnode **vpp)
254 {
255 struct vnode *vp;
256 int error;
257
258 DPRINTF("%s:\n", __FUNCTION__);
259 if ((error = VFS_VGET(mp, BFS_ROOT_INODE, &vp)) != 0)
260 return error;
261 *vpp = vp;
262
263 return 0;
264 }
265
266 int
267 sysvbfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg,
268 struct lwp *l)
269 {
270
271 DPRINTF("%s:\n", __FUNCTION__);
272 /* Don't support. */
273 return 0;
274 }
275
276 int
277 sysvbfs_statvfs(struct mount *mp, struct statvfs *f, struct lwp *l)
278 {
279 struct sysvbfs_mount *bmp = mp->mnt_data;
280 struct bfs *bfs = bmp->bfs;
281 int free_block;
282 size_t data_block;
283
284 data_block = (bfs->data_end - bfs->data_start) >> BFS_BSHIFT;
285 if (bfs_inode_alloc(bfs, 0, 0, &free_block) != 0)
286 free_block = 0;
287 else
288 free_block = (bfs->data_end >> BFS_BSHIFT) - free_block;
289
290 DPRINTF("%s: %d %d %d\n", __FUNCTION__, bfs->data_start,
291 bfs->data_end, free_block);
292
293 f->f_bsize = BFS_BSIZE;
294 f->f_frsize = BFS_BSIZE;
295 f->f_iosize = BFS_BSIZE;
296 f->f_blocks = data_block;
297 f->f_bfree = free_block;
298 f->f_bavail = f->f_bfree;
299 f->f_bresvd = 0;
300 f->f_files = bfs->n_inode;
301 f->f_ffree = bfs->max_inode - f->f_files;
302 f->f_favail = f->f_ffree;
303 f->f_fresvd = 0;
304 copy_statvfs_info(f, mp);
305
306 return 0;
307 }
308
309 int
310 sysvbfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred,
311 struct lwp *l)
312 {
313 struct sysvbfs_mount *bmp = mp->mnt_data;
314 struct sysvbfs_node *bnode;
315 struct vnode *v;
316 int err, error;
317
318 DPRINTF("%s:\n", __FUNCTION__);
319 error = 0;
320 simple_lock(&mntvnode_slock);
321 for (bnode = LIST_FIRST(&bmp->bnode_head); bnode != NULL;
322 bnode = LIST_NEXT(bnode, link)) {
323 simple_unlock(&mntvnode_slock);
324 v = bnode->vnode;
325 err = vget(v, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
326 if (err == 0) {
327 err = VOP_FSYNC(v, cred, FSYNC_WAIT, 0, 0, l);
328 vput(v);
329 }
330 if (err != 0)
331 error = err;
332 simple_lock(&mntvnode_slock);
333 }
334 simple_unlock(&mntvnode_slock);
335
336 return error;
337 }
338
339 int
340 sysvbfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
341 {
342 struct sysvbfs_mount *bmp = mp->mnt_data;
343 struct bfs *bfs = bmp->bfs;
344 struct vnode *vp;
345 struct sysvbfs_node *bnode;
346 struct bfs_inode *inode;
347 int error;
348
349 DPRINTF("%s: i-node=%d\n", __FUNCTION__, ino);
350 /* Lookup requested i-node */
351 if (!bfs_inode_lookup(bfs, ino, &inode)) {
352 DPRINTF("bfs_inode_lookup failed.\n");
353 return ENOENT;
354 }
355 for (bnode = LIST_FIRST(&bmp->bnode_head); bnode != NULL;
356 bnode = LIST_NEXT(bnode, link)) {
357 if (bnode->inode->number == ino) {
358 *vpp = bnode->vnode;
359 }
360 }
361
362 /* Allocate v-node. */
363 if ((error = getnewvnode(VT_SYSVBFS, mp, sysvbfs_vnodeop_p, &vp)) !=
364 0) {
365 DPRINTF("%s: getnewvnode error.\n", __FUNCTION__);
366 return error;
367 }
368 /* Lock vnode here */
369 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
370
371 /* Allocate i-node */
372 vp->v_data = pool_get(&sysvbfs_node_pool, PR_WAITOK);
373 memset(vp->v_data, 0, sizeof(struct sysvbfs_node));
374 bnode = vp->v_data;
375 simple_lock(&mntvnode_slock);
376 LIST_INSERT_HEAD(&bmp->bnode_head, bnode, link);
377 simple_unlock(&mntvnode_slock);
378 bnode->vnode = vp;
379 bnode->bmp = bmp;
380 bnode->inode = inode;
381 bnode->lockf = NULL; /* advlock */
382
383 if (ino == BFS_ROOT_INODE) { /* BFS is flat filesystem */
384 vp->v_type = VDIR;
385 vp->v_flag |= VROOT;
386 } else {
387 vp->v_type = VREG;
388 }
389 vp->v_size = bfs_file_size(inode);
390
391 genfs_node_init(vp, &sysvbfs_genfsops);
392 uvm_vnp_setsize(vp, vp->v_size);
393 *vpp = vp;
394
395 return 0;
396 }
397
398 int
399 sysvbfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
400 {
401
402 DPRINTF("%s:\n", __FUNCTION__);
403 /* notyet */
404 return EOPNOTSUPP;
405 }
406
407 int
408 sysvbfs_vptofh(struct vnode *vpp, struct fid *fid, size_t *fh_size)
409 {
410
411 DPRINTF("%s:\n", __FUNCTION__);
412 /* notyet */
413 return EOPNOTSUPP;
414 }
415
416 MALLOC_DECLARE(M_BFS);
417 MALLOC_DECLARE(M_SYSVBFS_VNODE);
418
419 void
420 sysvbfs_init(void)
421 {
422
423 DPRINTF("%s:\n", __FUNCTION__);
424 malloc_type_attach(M_SYSVBFS_VFS);
425 malloc_type_attach(M_BFS);
426 malloc_type_attach(M_SYSVBFS_VNODE);
427 pool_init(&sysvbfs_node_pool, sizeof(struct sysvbfs_node), 0, 0, 0,
428 "sysvbfs_node_pool", &pool_allocator_nointr, IPL_NONE);
429 }
430
431 void
432 sysvbfs_reinit(void)
433 {
434
435 /* Nothing to do. */
436 DPRINTF("%s:\n", __FUNCTION__);
437 }
438
439 void
440 sysvbfs_done(void)
441 {
442
443 DPRINTF("%s:\n", __FUNCTION__);
444 pool_destroy(&sysvbfs_node_pool);
445 malloc_type_detach(M_BFS);
446 malloc_type_detach(M_SYSVBFS_VFS);
447 malloc_type_detach(M_SYSVBFS_VNODE);
448 }
449
450 int
451 sysvbfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
452 kauth_cred_t cred)
453 {
454
455 return 0;
456 }
457