xref: /netbsd-src/sys/fs/sysvbfs/sysvbfs_vfsops.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: sysvbfs_vfsops.c,v 1.4 2006/05/15 11:16:16 yamt 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.4 2006/05/15 11:16:16 yamt 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_DEFINE(M_SYSVBFS_VFS, "sysvbfs vfs", "sysvbfs vfs structures");
71 
72 POOL_INIT(sysvbfs_node_pool, sizeof(struct sysvbfs_node), 0, 0, 0,
73     "sysvbfs_node_pool", &pool_allocator_nointr);
74 
75 int sysvbfs_mountfs(struct vnode *, struct mount *, struct lwp *);
76 
77 int
78 sysvbfs_mount(struct mount *mp, const char *path, void *data,
79     struct nameidata *ndp, struct lwp *l)
80 {
81 	struct sysvbfs_args args;
82 	struct sysvbfs_mount *bmp = NULL;
83 	struct vnode *devvp = NULL;
84 	struct proc *p = l->l_proc;
85 	int error;
86 	boolean_t update;
87 
88 	DPRINTF("%s: mnt_flag=%x\n", __FUNCTION__, mp->mnt_flag);
89 
90 	if (mp->mnt_flag & MNT_GETARGS) {
91 		if ((bmp = (void *)mp->mnt_data) == NULL)
92 			return EIO;
93 		args.fspec = NULL;
94 		return copyout(&args, data, sizeof(args));
95 	}
96 
97 	if ((error = copyin(data, &args, sizeof(args))) != 0)
98 		return error;
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_cred_geteuid(p->p_cred) != 0) {
138 		int accessmode = VREAD;
139 		if (update ?
140 		    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
141 		    (mp->mnt_flag & MNT_RDONLY) == 0)
142 			accessmode |= VWRITE;
143 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
144 		error = VOP_ACCESS(devvp, accessmode, p->p_cred, l);
145 		VOP_UNLOCK(devvp, 0);
146 	}
147 
148 	if (error) {
149 		vrele(devvp);
150 		return error;
151 	}
152 
153 	if (!update) {
154 		if ((error = sysvbfs_mountfs(devvp, mp, l)) != 0) {
155 			vrele(devvp);
156 			return error;
157 		}
158 	} else 	if (mp->mnt_flag & MNT_RDONLY) {
159 		/* XXX: r/w -> read only */
160 	}
161 
162 	return set_statvfs_info(path, UIO_USERSPACE, args.fspec, UIO_USERSPACE,
163 	    mp, l);
164 }
165 
166 int
167 sysvbfs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
168 {
169 	kauth_cred_t cred = l->l_proc->p_cred;
170 	struct sysvbfs_mount *bmp;
171 	struct partinfo dpart;
172 	int error;
173 
174 	if ((error = vfs_mountedon(devvp)) != 0)
175 		return error;	/* Already mounted */
176 	if (vcount(devvp) > 1)
177 		return EBUSY;	/* Opened by other */
178 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
179 	error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
180 	VOP_UNLOCK(devvp, 0);
181 	if (error)
182 		return error;
183 
184 	/* Open block device */
185 	if ((error = VOP_OPEN(devvp, FREAD, NOCRED, l)) != 0)
186 		return error;
187 
188 	/* Get partition information */
189 	if ((error = VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, l)) != 0)
190 		return error;
191 
192 	bmp = malloc(sizeof(struct sysvbfs_mount), M_SYSVBFS_VFS, M_WAITOK);
193 	if (bmp == NULL)
194 		return ENOMEM;
195 	bmp->devvp = devvp;
196 	bmp->mountp = mp;
197 	if ((error = sysvbfs_bfs_init(&bmp->bfs, devvp)) != 0) {
198 		free(bmp, M_SYSVBFS_VFS);
199 		return error;
200 	}
201 	LIST_INIT(&bmp->bnode_head);
202 
203 	mp->mnt_data = bmp;
204 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)devvp->v_rdev;
205 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_SYSVBFS);
206 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
207 	mp->mnt_flag |= MNT_LOCAL;
208 	mp->mnt_dev_bshift = BFS_BSIZE;
209 	mp->mnt_fs_bshift = BFS_BSHIFT;
210 
211 	DPRINTF("fstype=%d dtype=%d bsize=%d\n", dpart.part->p_fstype,
212 	    dpart.disklab->d_type, dpart.disklab->d_secsize);
213 
214 	return 0;
215 }
216 
217 int
218 sysvbfs_start(struct mount *mp, int flags, struct lwp *l)
219 {
220 
221 	DPRINTF("%s:\n", __FUNCTION__);
222 	/* Nothing to do. */
223 	return 0;
224 }
225 
226 int
227 sysvbfs_unmount(struct mount *mp, int mntflags, struct lwp *l)
228 {
229 	struct sysvbfs_mount *bmp = (void *)mp->mnt_data;
230 	int error;
231 
232 	DPRINTF("%s: %p\n", __FUNCTION__, bmp);
233 
234 	if ((error = vflush(mp, NULLVP,
235 	    mntflags & MNT_FORCE ? FORCECLOSE : 0)) != 0)
236 		return error;
237 
238 	vn_lock(bmp->devvp, LK_EXCLUSIVE | LK_RETRY);
239 	error = VOP_CLOSE(bmp->devvp, FREAD, NOCRED, l);
240 	vput(bmp->devvp);
241 
242 	sysvbfs_bfs_fini(bmp->bfs);
243 
244 	free(bmp, M_SYSVBFS_VFS);
245 	mp->mnt_data = NULL;
246 	mp->mnt_flag &= ~MNT_LOCAL;
247 
248 	return 0;
249 }
250 
251 int
252 sysvbfs_root(struct mount *mp, struct vnode **vpp)
253 {
254 	struct vnode *vp;
255 	int error;
256 
257 	DPRINTF("%s:\n", __FUNCTION__);
258 	if ((error = VFS_VGET(mp, BFS_ROOT_INODE, &vp)) != 0)
259 		return error;
260 	*vpp = vp;
261 
262 	return 0;
263 }
264 
265 int
266 sysvbfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg,
267     struct lwp *l)
268 {
269 
270 	DPRINTF("%s:\n", __FUNCTION__);
271 	/* Don't support. */
272 	return 0;
273 }
274 
275 int
276 sysvbfs_statvfs(struct mount *mp, struct statvfs *f, struct lwp *l)
277 {
278 	struct sysvbfs_mount *bmp = mp->mnt_data;
279 	struct bfs *bfs = bmp->bfs;
280 	int free_block;
281 	size_t data_block;
282 
283 	data_block = (bfs->data_end - bfs->data_start) >> BFS_BSHIFT;
284 	if (bfs_inode_alloc(bfs, 0, 0, &free_block) != 0)
285 		free_block = 0;
286 	else
287 		free_block = (bfs->data_end >> BFS_BSHIFT) - free_block;
288 
289 	DPRINTF("%s: %d %d %d\n", __FUNCTION__, bfs->data_start,
290 	    bfs->data_end, free_block);
291 
292 	f->f_bsize = BFS_BSIZE;
293 	f->f_frsize = BFS_BSIZE;
294 	f->f_iosize = BFS_BSIZE;
295 	f->f_blocks = data_block;
296 	f->f_bfree = free_block;
297 	f->f_bavail = f->f_bfree;
298 	f->f_bresvd = 0;
299 	f->f_files = bfs->n_inode;
300 	f->f_ffree = bfs->max_inode - f->f_files;
301 	f->f_favail = f->f_ffree;
302 	f->f_fresvd = 0;
303 	copy_statvfs_info(f, mp);
304 
305 	return 0;
306 }
307 
308 int
309 sysvbfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred,
310     struct lwp *l)
311 {
312 	struct sysvbfs_mount *bmp = mp->mnt_data;
313 	struct sysvbfs_node *bnode;
314 	struct vnode *v;
315 	int err, error;
316 
317 	DPRINTF("%s:\n", __FUNCTION__);
318 	error = 0;
319 	simple_lock(&mntvnode_slock);
320 	for (bnode = LIST_FIRST(&bmp->bnode_head); bnode != NULL;
321 	    bnode = LIST_NEXT(bnode, link)) {
322 		simple_unlock(&mntvnode_slock);
323 		v = bnode->vnode;
324 		err = vget(v, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
325 		if (err == 0) {
326 			err = VOP_FSYNC(v, cred, FSYNC_WAIT, 0, 0, l);
327 			vput(v);
328 		}
329 		if (err != 0)
330 			error = err;
331 		simple_lock(&mntvnode_slock);
332 	}
333 	simple_unlock(&mntvnode_slock);
334 
335 	return error;
336 }
337 
338 int
339 sysvbfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
340 {
341 	struct sysvbfs_mount *bmp = mp->mnt_data;
342 	struct bfs *bfs = bmp->bfs;
343 	struct vnode *vp;
344 	struct sysvbfs_node *bnode;
345 	struct bfs_inode *inode;
346 	int error;
347 
348 	DPRINTF("%s: i-node=%d\n", __FUNCTION__, ino);
349 	/* Lookup requested i-node */
350 	if (!bfs_inode_lookup(bfs, ino, &inode)) {
351 		DPRINTF("bfs_inode_lookup failed.\n");
352 		return ENOENT;
353 	}
354 	for (bnode = LIST_FIRST(&bmp->bnode_head); bnode != NULL;
355 	    bnode = LIST_NEXT(bnode, link)) {
356 		if (bnode->inode->number == ino) {
357 			*vpp = bnode->vnode;
358 		}
359 	}
360 
361 	/* Allocate v-node. */
362 	if ((error = getnewvnode(VT_SYSVBFS, mp, sysvbfs_vnodeop_p, &vp)) !=
363 	    0) {
364 		DPRINTF("%s: getnewvnode error.\n", __FUNCTION__);
365 		return error;
366 	}
367 	/* Lock vnode here */
368 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
369 
370 	/* Allocate i-node */
371 	vp->v_data = pool_get(&sysvbfs_node_pool, PR_WAITOK);
372 	memset(vp->v_data, 0, sizeof(struct sysvbfs_node));
373 	bnode = vp->v_data;
374 	simple_lock(&mntvnode_slock);
375 	LIST_INSERT_HEAD(&bmp->bnode_head, bnode, link);
376 	simple_unlock(&mntvnode_slock);
377 	bnode->vnode = vp;
378 	bnode->bmp = bmp;
379 	bnode->inode = inode;
380 	bnode->lockf = NULL; /* advlock */
381 
382 	if (ino == BFS_ROOT_INODE) {	/* BFS is flat filesystem */
383 		vp->v_type = VDIR;
384 		vp->v_flag |= VROOT;
385 	} else {
386 		vp->v_type = VREG;
387 	}
388 	vp->v_size = bfs_file_size(inode);
389 
390 	genfs_node_init(vp, &sysvbfs_genfsops);
391 	uvm_vnp_setsize(vp, vp->v_size);
392 	*vpp = vp;
393 
394 	return 0;
395 }
396 
397 int
398 sysvbfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
399 {
400 
401 	DPRINTF("%s:\n", __FUNCTION__);
402 	/* notyet */
403 	return EOPNOTSUPP;
404 }
405 
406 int
407 sysvbfs_vptofh(struct vnode *vpp, struct fid *fid)
408 {
409 
410 	DPRINTF("%s:\n", __FUNCTION__);
411 	/* notyet */
412 	return EOPNOTSUPP;
413 }
414 
415 void
416 sysvbfs_init(void)
417 {
418 
419 	/* Nothing to do. */
420 	DPRINTF("%s:\n", __FUNCTION__);
421 }
422 
423 void
424 sysvbfs_reinit(void)
425 {
426 
427 	/* Nothing to do. */
428 	DPRINTF("%s:\n", __FUNCTION__);
429 }
430 
431 void
432 sysvbfs_done(void)
433 {
434 
435 	DPRINTF("%s:\n", __FUNCTION__);
436 	pool_destroy(&sysvbfs_node_pool);
437 }
438 
439 int
440 sysvbfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
441     kauth_cred_t cred)
442 {
443 
444 	return 0;
445 }
446