xref: /netbsd-src/sys/fs/sysvbfs/sysvbfs_vfsops.c (revision aad9773e38ed2370a628a6416e098f9008fc10a7)
1 /*	$NetBSD: sysvbfs_vfsops.c,v 1.43 2014/04/16 18:55:19 maxv 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.43 2014/04/16 18:55:19 maxv 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/fcntl.h>
43 #include <sys/malloc.h>
44 #include <sys/kauth.h>
45 #include <sys/proc.h>
46 
47 /* v-node */
48 #include <sys/namei.h>
49 #include <sys/vnode.h>
50 /* devsw */
51 #include <sys/conf.h>
52 
53 #include <fs/sysvbfs/sysvbfs.h>	/* external interface */
54 #include <fs/sysvbfs/bfs.h>	/* internal interface */
55 
56 #ifdef SYSVBFS_VNOPS_DEBUG
57 #define	DPRINTF(fmt, args...)	printf(fmt, ##args)
58 #else
59 #define	DPRINTF(arg...)		((void)0)
60 #endif
61 
62 MALLOC_JUSTDEFINE(M_SYSVBFS_VFS, "sysvbfs vfs", "sysvbfs vfs structures");
63 
64 struct pool sysvbfs_node_pool;
65 
66 int sysvbfs_mountfs(struct vnode *, struct mount *, struct lwp *);
67 
68 int
69 sysvbfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
70 {
71 	struct lwp *l = curlwp;
72 	struct sysvbfs_args *args = data;
73 	struct sysvbfs_mount *bmp = NULL;
74 	struct vnode *devvp = NULL;
75 	int error = 0;
76 	bool update;
77 
78 	DPRINTF("%s: mnt_flag=%x\n", __func__, mp->mnt_flag);
79 
80 	if (args == NULL)
81 		return EINVAL;
82 	if (*data_len < sizeof *args)
83 		return EINVAL;
84 
85 	if (mp->mnt_flag & MNT_GETARGS) {
86 		if ((bmp = (struct sysvbfs_mount *)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 		error = namei_simple_user(args->fspec,
104 					NSM_FOLLOW_NOEMULROOT, &devvp);
105 		if (error != 0)
106 			return (error);
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 			bmp = (struct sysvbfs_mount *)mp->mnt_data;
122 			if (devvp != bmp->devvp)
123 				error = EINVAL;
124 		}
125 	}
126 
127 	/*
128 	 * If mount by non-root, then verify that user has necessary
129 	 * permissions on the device.
130 	 *
131 	 * Permission to update a mount is checked higher, so here we presume
132 	 * updating the mount is okay (for example, as far as securelevel goes)
133 	 * which leaves us with the normal check.
134 	 */
135 	if (error == 0) {
136 		int accessmode = VREAD;
137 		if (update ?
138 		    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
139 		    (mp->mnt_flag & MNT_RDONLY) == 0)
140 			accessmode |= VWRITE;
141 
142 		error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
143 		    KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp,
144 		    KAUTH_ARG(accessmode));
145 	}
146 
147 	if (error) {
148 		vrele(devvp);
149 		return error;
150 	}
151 
152 	if (!update) {
153 		if ((error = sysvbfs_mountfs(devvp, mp, l)) != 0) {
154 			vrele(devvp);
155 			return error;
156 		}
157 	} else 	if (mp->mnt_flag & MNT_RDONLY) {
158 		/* XXX: r/w -> read only */
159 	}
160 
161 	return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
162 	    mp->mnt_op->vfs_name, mp, l);
163 }
164 
165 int
166 sysvbfs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
167 {
168 	kauth_cred_t cred = l->l_cred;
169 	struct sysvbfs_mount *bmp;
170 	int error, oflags;
171 	bool devopen = false;
172 
173 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
174 	error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
175 	if (error)
176 		goto out;
177 
178 	/* Open block device */
179 	oflags = FREAD;
180 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
181 		oflags |= FWRITE;
182 	if ((error = VOP_OPEN(devvp, oflags, NOCRED)) != 0)
183 		goto out;
184 	devopen = true;
185 
186 	bmp = malloc(sizeof(*bmp), M_SYSVBFS_VFS, M_WAITOK | M_ZERO);
187 	bmp->devvp = devvp;
188 	bmp->mountp = mp;
189 	if ((error = sysvbfs_bfs_init(&bmp->bfs, devvp)) != 0) {
190 		free(bmp, M_SYSVBFS_VFS);
191 		goto out;
192 	}
193 	LIST_INIT(&bmp->bnode_head);
194 
195 	mp->mnt_data = bmp;
196 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)devvp->v_rdev;
197 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_SYSVBFS);
198 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
199 	mp->mnt_stat.f_namemax = BFS_FILENAME_MAXLEN;
200 	mp->mnt_flag |= MNT_LOCAL;
201 	mp->mnt_dev_bshift = BFS_BSHIFT;
202 	mp->mnt_fs_bshift = BFS_BSHIFT;
203 
204  out:
205 	if (devopen && error)
206 		VOP_CLOSE(devvp, oflags, NOCRED);
207 	VOP_UNLOCK(devvp);
208 	return error;
209 }
210 
211 int
212 sysvbfs_start(struct mount *mp, int flags)
213 {
214 
215 	DPRINTF("%s:\n", __func__);
216 	/* Nothing to do. */
217 	return 0;
218 }
219 
220 int
221 sysvbfs_unmount(struct mount *mp, int mntflags)
222 {
223 	struct sysvbfs_mount *bmp = (void *)mp->mnt_data;
224 	int error;
225 
226 	DPRINTF("%s: %p\n", __func__, bmp);
227 
228 	if ((error = vflush(mp, NULLVP,
229 	    mntflags & MNT_FORCE ? FORCECLOSE : 0)) != 0)
230 		return error;
231 
232 	vn_lock(bmp->devvp, LK_EXCLUSIVE | LK_RETRY);
233 	error = VOP_CLOSE(bmp->devvp, FREAD, NOCRED);
234 	vput(bmp->devvp);
235 
236 	sysvbfs_bfs_fini(bmp->bfs);
237 
238 	free(bmp, M_SYSVBFS_VFS);
239 	mp->mnt_data = NULL;
240 	mp->mnt_flag &= ~MNT_LOCAL;
241 
242 	return 0;
243 }
244 
245 int
246 sysvbfs_root(struct mount *mp, struct vnode **vpp)
247 {
248 	struct vnode *vp;
249 	int error;
250 
251 	DPRINTF("%s:\n", __func__);
252 	if ((error = VFS_VGET(mp, BFS_ROOT_INODE, &vp)) != 0)
253 		return error;
254 	*vpp = vp;
255 
256 	return 0;
257 }
258 
259 int
260 sysvbfs_statvfs(struct mount *mp, struct statvfs *f)
261 {
262 	struct sysvbfs_mount *bmp = mp->mnt_data;
263 	struct bfs *bfs = bmp->bfs;
264 	int free_block;
265 	size_t data_block;
266 
267 	data_block = (bfs->data_end - bfs->data_start) >> BFS_BSHIFT;
268 	if (bfs_inode_alloc(bfs, 0, 0, &free_block) != 0)
269 		free_block = 0;
270 	else
271 		free_block = (bfs->data_end >> BFS_BSHIFT) - free_block;
272 
273 	DPRINTF("%s: %d %d %d\n", __func__, bfs->data_start,
274 	    bfs->data_end, free_block);
275 
276 	f->f_bsize = BFS_BSIZE;
277 	f->f_frsize = BFS_BSIZE;
278 	f->f_iosize = BFS_BSIZE;
279 	f->f_blocks = data_block;
280 	f->f_bfree = free_block;
281 	f->f_bavail = f->f_bfree;
282 	f->f_bresvd = 0;
283 	f->f_files = bfs->max_inode;
284 	f->f_ffree = bfs->max_inode - bfs->n_inode;
285 	f->f_favail = f->f_ffree;
286 	f->f_fresvd = 0;
287 	copy_statvfs_info(f, mp);
288 
289 	return 0;
290 }
291 
292 int
293 sysvbfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
294 {
295 	struct sysvbfs_mount *bmp = mp->mnt_data;
296 	struct sysvbfs_node *bnode;
297 	struct vnode *v;
298 	int err, error;
299 
300 	DPRINTF("%s:\n", __func__);
301 	error = 0;
302 	mutex_enter(&mntvnode_lock);
303 	for (bnode = LIST_FIRST(&bmp->bnode_head); bnode != NULL;
304 	    bnode = LIST_NEXT(bnode, link)) {
305 		v = bnode->vnode;
306 	    	mutex_enter(v->v_interlock);
307 		mutex_exit(&mntvnode_lock);
308 		err = vget(v, LK_EXCLUSIVE | LK_NOWAIT);
309 		if (err == 0) {
310 			err = VOP_FSYNC(v, cred, FSYNC_WAIT, 0, 0);
311 			vput(v);
312 		}
313 		if (err != 0)
314 			error = err;
315 		mutex_enter(&mntvnode_lock);
316 	}
317 	mutex_exit(&mntvnode_lock);
318 
319 	return error;
320 }
321 
322 int
323 sysvbfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
324 {
325 	struct sysvbfs_mount *bmp = mp->mnt_data;
326 	struct bfs *bfs = bmp->bfs;
327 	struct vnode *vp;
328 	struct sysvbfs_node *bnode;
329 	struct bfs_inode *inode;
330 	int error;
331 
332 	DPRINTF("%s: i-node=%lld\n", __func__, (long long)ino);
333 	/* Lookup requested i-node */
334 	if (!bfs_inode_lookup(bfs, ino, &inode)) {
335 		DPRINTF("%s: bfs_inode_lookup failed.\n", __func__);
336 		return ENOENT;
337 	}
338 
339  retry:
340 	mutex_enter(&mntvnode_lock);
341 	for (bnode = LIST_FIRST(&bmp->bnode_head); bnode != NULL;
342 	    bnode = LIST_NEXT(bnode, link)) {
343 		if (bnode->inode->number == ino) {
344 			vp = bnode->vnode;
345 			mutex_enter(vp->v_interlock);
346 			mutex_exit(&mntvnode_lock);
347 			if (vget(vp, LK_EXCLUSIVE) == 0) {
348 				*vpp = vp;
349 				return 0;
350 			} else {
351 				goto retry;
352 			}
353 		}
354 	}
355 	mutex_exit(&mntvnode_lock);
356 
357 	/* Allocate v-node. */
358 	error = getnewvnode(VT_SYSVBFS, mp, sysvbfs_vnodeop_p, NULL, &vp);
359 	if (error) {
360 		DPRINTF("%s: getnewvnode error.\n", __func__);
361 		return error;
362 	}
363 	/* Lock vnode here */
364 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
365 
366 	/* Allocate i-node */
367 	vp->v_data = pool_get(&sysvbfs_node_pool, PR_WAITOK);
368 	memset(vp->v_data, 0, sizeof(struct sysvbfs_node));
369 	bnode = vp->v_data;
370 	mutex_enter(&mntvnode_lock);
371 	LIST_INSERT_HEAD(&bmp->bnode_head, bnode, link);
372 	mutex_exit(&mntvnode_lock);
373 	bnode->vnode = vp;
374 	bnode->bmp = bmp;
375 	bnode->inode = inode;
376 	bnode->lockf = NULL; /* advlock */
377 
378 	if (ino == BFS_ROOT_INODE) {	/* BFS is flat filesystem */
379 		vp->v_type = VDIR;
380 		vp->v_vflag |= VV_ROOT;
381 	} else {
382 		vp->v_type = VREG;
383 	}
384 
385 	genfs_node_init(vp, &sysvbfs_genfsops);
386 	uvm_vnp_setsize(vp, bfs_file_size(inode));
387 	*vpp = vp;
388 
389 	return 0;
390 }
391 
392 int
393 sysvbfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
394 {
395 
396 	DPRINTF("%s:\n", __func__);
397 	/* notyet */
398 	return EOPNOTSUPP;
399 }
400 
401 int
402 sysvbfs_vptofh(struct vnode *vpp, struct fid *fid, size_t *fh_size)
403 {
404 
405 	DPRINTF("%s:\n", __func__);
406 	/* notyet */
407 	return EOPNOTSUPP;
408 }
409 
410 MALLOC_DECLARE(M_BFS);
411 MALLOC_DECLARE(M_SYSVBFS_VNODE);
412 
413 void
414 sysvbfs_init(void)
415 {
416 
417 	DPRINTF("%s:\n", __func__);
418 	malloc_type_attach(M_SYSVBFS_VFS);
419 	malloc_type_attach(M_BFS);
420 	malloc_type_attach(M_SYSVBFS_VNODE);
421 	pool_init(&sysvbfs_node_pool, sizeof(struct sysvbfs_node), 0, 0, 0,
422 	    "sysvbfs_node_pool", &pool_allocator_nointr, IPL_NONE);
423 }
424 
425 void
426 sysvbfs_reinit(void)
427 {
428 
429 	/* Nothing to do. */
430 	DPRINTF("%s:\n", __func__);
431 }
432 
433 void
434 sysvbfs_done(void)
435 {
436 
437 	DPRINTF("%s:\n", __func__);
438 	pool_destroy(&sysvbfs_node_pool);
439 	malloc_type_detach(M_BFS);
440 	malloc_type_detach(M_SYSVBFS_VFS);
441 	malloc_type_detach(M_SYSVBFS_VNODE);
442 }
443 
444 int
445 sysvbfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
446     kauth_cred_t cred)
447 {
448 
449 	return 0;
450 }
451