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