xref: /netbsd-src/sys/fs/sysvbfs/sysvbfs_vfsops.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /*	$NetBSD: sysvbfs_vfsops.c,v 1.17 2007/10/10 20:42:25 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.17 2007/10/10 20:42:25 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     struct lwp *l)
79 {
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", __FUNCTION__, 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", __FUNCTION__, 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, l);
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_specinfo->si_rdev) ==
122 			    NULL)
123 				error = ENXIO;
124 		} else {
125 			/*
126 			 * Be sure we're still naming the same device
127 			 * used for our initial mount
128 			 */
129 			if (devvp != bmp->devvp)
130 				error = EINVAL;
131 		}
132 	}
133 
134 	/*
135 	 * If mount by non-root, then verify that user has necessary
136 	 * permissions on the device.
137 	 */
138 	if (error == 0 && kauth_authorize_generic(l->l_cred,
139 	    KAUTH_GENERIC_ISSUSER, NULL)) {
140 		int accessmode = VREAD;
141 		if (update ?
142 		    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
143 		    (mp->mnt_flag & MNT_RDONLY) == 0)
144 			accessmode |= VWRITE;
145 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
146 		error = VOP_ACCESS(devvp, accessmode, l->l_cred, l);
147 		VOP_UNLOCK(devvp, 0);
148 	}
149 
150 	if (error) {
151 		vrele(devvp);
152 		return error;
153 	}
154 
155 	if (!update) {
156 		if ((error = sysvbfs_mountfs(devvp, mp, l)) != 0) {
157 			vrele(devvp);
158 			return error;
159 		}
160 	} else 	if (mp->mnt_flag & MNT_RDONLY) {
161 		/* XXX: r/w -> read only */
162 	}
163 
164 	return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
165 	    mp->mnt_op->vfs_name, mp, l);
166 }
167 
168 int
169 sysvbfs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
170 {
171 	kauth_cred_t cred = l->l_cred;
172 	struct sysvbfs_mount *bmp;
173 	struct partinfo dpart;
174 	int error;
175 
176 	if ((error = vfs_mountedon(devvp)) != 0)
177 		return error;	/* Already mounted */
178 	if (vcount(devvp) > 1)
179 		return EBUSY;	/* Opened by other */
180 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
181 	error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
182 	VOP_UNLOCK(devvp, 0);
183 	if (error)
184 		return error;
185 
186 	/* Open block device */
187 	if ((error = VOP_OPEN(devvp, FREAD, NOCRED, l)) != 0)
188 		return error;
189 
190 	/* Get partition information */
191 	if ((error = VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, l)) != 0)
192 		return error;
193 
194 	bmp = malloc(sizeof(struct sysvbfs_mount), M_SYSVBFS_VFS, M_WAITOK);
195 	if (bmp == NULL)
196 		return ENOMEM;
197 	bmp->devvp = devvp;
198 	bmp->mountp = mp;
199 	if ((error = sysvbfs_bfs_init(&bmp->bfs, devvp)) != 0) {
200 		free(bmp, M_SYSVBFS_VFS);
201 		return error;
202 	}
203 	LIST_INIT(&bmp->bnode_head);
204 
205 	mp->mnt_data = bmp;
206 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)devvp->v_rdev;
207 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_SYSVBFS);
208 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
209 	mp->mnt_flag |= MNT_LOCAL;
210 	mp->mnt_dev_bshift = BFS_BSHIFT;
211 	mp->mnt_fs_bshift = BFS_BSHIFT;
212 
213 	DPRINTF("fstype=%d dtype=%d bsize=%d\n", dpart.part->p_fstype,
214 	    dpart.disklab->d_type, dpart.disklab->d_secsize);
215 
216 	return 0;
217 }
218 
219 int
220 sysvbfs_start(struct mount *mp, int flags, struct lwp *l)
221 {
222 
223 	DPRINTF("%s:\n", __FUNCTION__);
224 	/* Nothing to do. */
225 	return 0;
226 }
227 
228 int
229 sysvbfs_unmount(struct mount *mp, int mntflags, struct lwp *l)
230 {
231 	struct sysvbfs_mount *bmp = (void *)mp->mnt_data;
232 	int error;
233 
234 	DPRINTF("%s: %p\n", __FUNCTION__, bmp);
235 
236 	if ((error = vflush(mp, NULLVP,
237 	    mntflags & MNT_FORCE ? FORCECLOSE : 0)) != 0)
238 		return error;
239 
240 	vn_lock(bmp->devvp, LK_EXCLUSIVE | LK_RETRY);
241 	error = VOP_CLOSE(bmp->devvp, FREAD, NOCRED, l);
242 	vput(bmp->devvp);
243 
244 	sysvbfs_bfs_fini(bmp->bfs);
245 
246 	free(bmp, M_SYSVBFS_VFS);
247 	mp->mnt_data = NULL;
248 	mp->mnt_flag &= ~MNT_LOCAL;
249 
250 	return 0;
251 }
252 
253 int
254 sysvbfs_root(struct mount *mp, struct vnode **vpp)
255 {
256 	struct vnode *vp;
257 	int error;
258 
259 	DPRINTF("%s:\n", __FUNCTION__);
260 	if ((error = VFS_VGET(mp, BFS_ROOT_INODE, &vp)) != 0)
261 		return error;
262 	*vpp = vp;
263 
264 	return 0;
265 }
266 
267 int
268 sysvbfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg,
269     struct lwp *l)
270 {
271 
272 	DPRINTF("%s:\n", __FUNCTION__);
273 	/* Don't support. */
274 	return 0;
275 }
276 
277 int
278 sysvbfs_statvfs(struct mount *mp, struct statvfs *f, struct lwp *l)
279 {
280 	struct sysvbfs_mount *bmp = mp->mnt_data;
281 	struct bfs *bfs = bmp->bfs;
282 	int free_block;
283 	size_t data_block;
284 
285 	data_block = (bfs->data_end - bfs->data_start) >> BFS_BSHIFT;
286 	if (bfs_inode_alloc(bfs, 0, 0, &free_block) != 0)
287 		free_block = 0;
288 	else
289 		free_block = (bfs->data_end >> BFS_BSHIFT) - free_block;
290 
291 	DPRINTF("%s: %d %d %d\n", __FUNCTION__, bfs->data_start,
292 	    bfs->data_end, free_block);
293 
294 	f->f_bsize = BFS_BSIZE;
295 	f->f_frsize = BFS_BSIZE;
296 	f->f_iosize = BFS_BSIZE;
297 	f->f_blocks = data_block;
298 	f->f_bfree = free_block;
299 	f->f_bavail = f->f_bfree;
300 	f->f_bresvd = 0;
301 	f->f_files = bfs->n_inode;
302 	f->f_ffree = bfs->max_inode - f->f_files;
303 	f->f_favail = f->f_ffree;
304 	f->f_fresvd = 0;
305 	copy_statvfs_info(f, mp);
306 
307 	return 0;
308 }
309 
310 int
311 sysvbfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred,
312     struct lwp *l)
313 {
314 	struct sysvbfs_mount *bmp = mp->mnt_data;
315 	struct sysvbfs_node *bnode;
316 	struct vnode *v;
317 	int err, error;
318 
319 	DPRINTF("%s:\n", __FUNCTION__);
320 	error = 0;
321 	simple_lock(&mntvnode_slock);
322 	for (bnode = LIST_FIRST(&bmp->bnode_head); bnode != NULL;
323 	    bnode = LIST_NEXT(bnode, link)) {
324 		simple_unlock(&mntvnode_slock);
325 		v = bnode->vnode;
326 		err = vget(v, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
327 		if (err == 0) {
328 			err = VOP_FSYNC(v, cred, FSYNC_WAIT, 0, 0, l);
329 			vput(v);
330 		}
331 		if (err != 0)
332 			error = err;
333 		simple_lock(&mntvnode_slock);
334 	}
335 	simple_unlock(&mntvnode_slock);
336 
337 	return error;
338 }
339 
340 int
341 sysvbfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
342 {
343 	struct sysvbfs_mount *bmp = mp->mnt_data;
344 	struct bfs *bfs = bmp->bfs;
345 	struct vnode *vp;
346 	struct sysvbfs_node *bnode;
347 	struct bfs_inode *inode;
348 	int error;
349 
350 	DPRINTF("%s: i-node=%d\n", __FUNCTION__, ino);
351 	/* Lookup requested i-node */
352 	if (!bfs_inode_lookup(bfs, ino, &inode)) {
353 		DPRINTF("bfs_inode_lookup failed.\n");
354 		return ENOENT;
355 	}
356 	for (bnode = LIST_FIRST(&bmp->bnode_head); bnode != NULL;
357 	    bnode = LIST_NEXT(bnode, link)) {
358 		if (bnode->inode->number == ino) {
359 			*vpp = bnode->vnode;
360 		}
361 	}
362 
363 	/* Allocate v-node. */
364 	if ((error = getnewvnode(VT_SYSVBFS, mp, sysvbfs_vnodeop_p, &vp)) !=
365 	    0) {
366 		DPRINTF("%s: getnewvnode error.\n", __FUNCTION__);
367 		return error;
368 	}
369 	/* Lock vnode here */
370 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
371 
372 	/* Allocate i-node */
373 	vp->v_data = pool_get(&sysvbfs_node_pool, PR_WAITOK);
374 	memset(vp->v_data, 0, sizeof(struct sysvbfs_node));
375 	bnode = vp->v_data;
376 	simple_lock(&mntvnode_slock);
377 	LIST_INSERT_HEAD(&bmp->bnode_head, bnode, link);
378 	simple_unlock(&mntvnode_slock);
379 	bnode->vnode = vp;
380 	bnode->bmp = bmp;
381 	bnode->inode = inode;
382 	bnode->lockf = NULL; /* advlock */
383 
384 	if (ino == BFS_ROOT_INODE) {	/* BFS is flat filesystem */
385 		vp->v_type = VDIR;
386 		vp->v_vflag |= VV_ROOT;
387 	} else {
388 		vp->v_type = VREG;
389 	}
390 
391 	genfs_node_init(vp, &sysvbfs_genfsops);
392 	uvm_vnp_setsize(vp, bfs_file_size(inode));
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