xref: /netbsd-src/sys/fs/v7fs/v7fs_vfsops.c (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1 /*	$NetBSD: v7fs_vfsops.c,v 1.4 2011/07/30 03:53:18 uch Exp $	*/
2 
3 /*-
4  * Copyright (c) 2004, 2011 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: v7fs_vfsops.c,v 1.4 2011/07/30 03:53:18 uch Exp $");
34 #if defined _KERNEL_OPT
35 #include "opt_v7fs.h"
36 #endif
37 
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/pool.h>
42 #include <sys/time.h>
43 #include <sys/ucred.h>
44 #include <sys/mount.h>
45 #include <sys/disklabel.h>
46 #include <sys/fcntl.h>
47 #include <sys/malloc.h>
48 #include <sys/kauth.h>
49 #include <sys/proc.h>
50 
51 /* v-node */
52 #include <sys/namei.h>
53 #include <sys/vnode.h>
54 /* devsw */
55 #include <sys/conf.h>
56 
57 #include "v7fs_extern.h"
58 #include "v7fs.h"
59 #include "v7fs_impl.h"
60 #include "v7fs_inode.h"
61 #include "v7fs_superblock.h"
62 
63 #ifdef V7FS_VFSOPS_DEBUG
64 #define	DPRINTF(fmt, args...)	printf("%s: " fmt, __func__, ##args)
65 #else
66 #define	DPRINTF(arg...)		((void)0)
67 #endif
68 
69 MALLOC_JUSTDEFINE(M_V7FS_VFS, "v7fs vfs", "v7fs vfs structures");
70 
71 struct pool v7fs_node_pool;
72 
73 static int v7fs_mountfs(struct vnode *, struct mount *, int);
74 static int v7fs_openfs(struct vnode *, struct mount *, struct lwp *);
75 static void v7fs_closefs(struct vnode *, struct mount *);
76 static int is_v7fs_partition(struct vnode *);
77 static enum vtype v7fs_mode_to_vtype(v7fs_mode_t mode);
78 int v7fs_vnode_reload(struct mount *, struct vnode *);
79 
80 int
81 v7fs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
82 {
83 	struct lwp *l = curlwp;
84 	struct v7fs_args *args = data;
85 	struct v7fs_mount *v7fsmount = (void *)mp->mnt_data;
86 	struct vnode *devvp = NULL;
87 	int error;
88 	bool update = mp->mnt_flag & MNT_UPDATE;
89 
90 	DPRINTF("mnt_flag=%x %s\n", mp->mnt_flag, update ? "update" : "");
91 
92 	if (*data_len < sizeof(*args))
93 		return EINVAL;
94 
95 	if (mp->mnt_flag & MNT_GETARGS) {
96 		if (!v7fsmount)
97 			return EIO;
98 		args->fspec = NULL;
99 		args->endian = v7fsmount->core->endian;
100 		*data_len = sizeof(*args);
101 		return 0;
102 	}
103 
104 	DPRINTF("args->fspec=%s endian=%d\n", args->fspec, args->endian);
105 	if (args->fspec == NULL) {
106 		/* nothing to do. */
107 		return EINVAL;
108 	}
109 
110 	if (args->fspec != NULL) {
111 		/* Look up the name and verify that it's sane. */
112 		error = namei_simple_user(args->fspec,
113 		    NSM_FOLLOW_NOEMULROOT, &devvp);
114 		if (error != 0)
115 			return (error);
116 		DPRINTF("mount device=%lx\n", (long)devvp->v_rdev);
117 
118 		if (!update) {
119 			/*
120 			 * Be sure this is a valid block device
121 			 */
122 			if (devvp->v_type != VBLK)
123 				error = ENOTBLK;
124 			else if (bdevsw_lookup(devvp->v_rdev) == NULL)
125 				error = ENXIO;
126 		} else {
127 			KDASSERT(v7fsmount);
128 			/*
129 			 * Be sure we're still naming the same device
130 			 * used for our initial mount
131 			 */
132 			if (devvp != v7fsmount->devvp) {
133 				DPRINTF("devvp %p != %p rootvp=%p\n", devvp,
134 				    v7fsmount->devvp, rootvp);
135 				if (rootvp == v7fsmount->devvp) {
136 					vrele(devvp);
137 					devvp = rootvp;
138 					vref(devvp);
139 				} else {
140 					error = EINVAL;
141 				}
142 			}
143 		}
144 	}
145 
146 	/*
147 	 * If mount by non-root, then verify that user has necessary
148 	 * permissions on the device.
149 	 *
150 	 * Permission to update a mount is checked higher, so here we presume
151 	 * updating the mount is okay (for example, as far as securelevel goes)
152 	 * which leaves us with the normal check.
153 	 */
154 	if (error == 0) {
155 		int accessmode = VREAD;
156 		if (update ?
157 		    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
158 		    (mp->mnt_flag & MNT_RDONLY) == 0)
159 			accessmode |= VWRITE;
160 		error = genfs_can_mount(devvp, accessmode, l->l_cred);
161 	}
162 
163 	if (error) {
164 		vrele(devvp);
165 		return error;
166 	}
167 
168 	if (!update) {
169 		if ((error = v7fs_openfs(devvp, mp, l))) {
170 			vrele(devvp);
171 			return error;
172 		}
173 
174 		if ((error = v7fs_mountfs(devvp, mp, args->endian))) {
175 			v7fs_closefs(devvp, mp);
176 			VOP_UNLOCK(devvp);
177 			vrele(devvp);
178 			return error;
179 		}
180 		VOP_UNLOCK(devvp);
181 	} else 	if (mp->mnt_flag & MNT_RDONLY) {
182 		/* XXX: r/w -> read only */
183 	}
184 
185 	return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
186 	    mp->mnt_op->vfs_name, mp, l);
187 }
188 
189 static int
190 is_v7fs_partition(struct vnode *devvp)
191 {
192 	struct partinfo dpart;
193 	int error;
194 
195 	if ((error = VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED)) != 0) {
196 		DPRINTF("VOP_IOCTL=%d\n", error);
197 		return error;
198 	}
199 	DPRINTF("fstype=%d dtype=%d bsize=%d\n", dpart.part->p_fstype,
200 	    dpart.disklab->d_type, dpart.disklab->d_secsize);
201 
202 	return (dpart.part->p_fstype == FS_V7) ? 0 : EINVAL;
203 }
204 
205 static int
206 v7fs_openfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
207 {
208 	kauth_cred_t cred = l->l_cred;
209 	int oflags;
210 	int error;
211 
212 	/* Flush buffer */
213 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
214 	if ((error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0)))
215 		goto unlock_exit;
216 
217 	/* Open block device */
218 	oflags = FREAD;
219 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
220 		oflags |= FWRITE;
221 
222 	if ((error = VOP_OPEN(devvp, oflags, NOCRED)) != 0) {
223 		DPRINTF("VOP_OPEN=%d\n", error);
224 		goto unlock_exit;
225 	}
226 
227 	return 0; /* lock held */
228 
229 unlock_exit:
230 	VOP_UNLOCK(devvp);
231 
232 	return error;
233 }
234 
235 static void
236 v7fs_closefs(struct vnode *devvp, struct mount *mp)
237 {
238 	int oflags = FREAD;
239 
240 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
241 		oflags |= FWRITE;
242 
243 	VOP_CLOSE(devvp, oflags, NOCRED);
244 }
245 
246 static int
247 v7fs_mountfs(struct vnode *devvp, struct mount *mp, int endian)
248 {
249 	struct v7fs_mount *v7fsmount;
250 	int error;
251 	struct v7fs_mount_device mount;
252 
253 	DPRINTF("%d\n",endian);
254 
255 	v7fsmount = malloc(sizeof(*v7fsmount), M_V7FS_VFS, M_WAITOK);
256 	if (v7fsmount == NULL) {
257 		return ENOMEM;
258 	}
259 	v7fsmount->devvp = devvp;
260 	v7fsmount->mountp = mp;
261 
262 	mount.device.vnode = devvp;
263 	mount.endian = endian;
264 
265 	if ((error = v7fs_io_init(&v7fsmount->core, &mount, V7FS_BSIZE))) {
266 		goto err_exit;
267 	}
268 	struct v7fs_self *fs = v7fsmount->core;
269 
270 	if ((error = v7fs_superblock_load(fs))) {
271 		v7fs_io_fini(fs);
272 		goto err_exit;
273 	}
274 
275 	LIST_INIT(&v7fsmount->v7fs_node_head);
276 
277 	mp->mnt_data = v7fsmount;
278 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)devvp->v_rdev;
279 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_V7FS);
280 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
281 	mp->mnt_stat.f_namemax = V7FS_NAME_MAX;
282 	mp->mnt_flag |= MNT_LOCAL;
283 	mp->mnt_dev_bshift = V7FS_BSHIFT;
284 	mp->mnt_fs_bshift = V7FS_BSHIFT;
285 
286 	return 0;
287 
288 err_exit:
289 	free(v7fsmount, M_V7FS_VFS);
290 	return error;
291 }
292 
293 int
294 v7fs_start(struct mount *mp, int flags)
295 {
296 
297 	DPRINTF("\n");
298 	/* Nothing to do. */
299 	return 0;
300 }
301 
302 int
303 v7fs_unmount(struct mount *mp, int mntflags)
304 {
305 	struct v7fs_mount *v7fsmount = (void *)mp->mnt_data;
306 	int error;
307 
308 	DPRINTF("%p\n", v7fsmount);
309 
310 	if ((error = vflush(mp, NULLVP,
311 		    mntflags & MNT_FORCE ? FORCECLOSE : 0)) != 0)
312 		return error;
313 
314 	vn_lock(v7fsmount->devvp, LK_EXCLUSIVE | LK_RETRY);
315 	error = VOP_CLOSE(v7fsmount->devvp, FREAD, NOCRED);
316 	vput(v7fsmount->devvp);
317 
318 	v7fs_io_fini(v7fsmount->core);
319 
320 	free(v7fsmount, M_V7FS_VFS);
321 	mp->mnt_data = NULL;
322 	mp->mnt_flag &= ~MNT_LOCAL;
323 
324 	return 0;
325 }
326 
327 int
328 v7fs_root(struct mount *mp, struct vnode **vpp)
329 {
330 	struct vnode *vp;
331 	int error;
332 
333 	DPRINTF("\n");
334 	if ((error = VFS_VGET(mp, V7FS_ROOT_INODE, &vp)) != 0) {
335 		DPRINTF("error=%d\n", error);
336 		return error;
337 	}
338 	*vpp = vp;
339 	DPRINTF("done.\n");
340 
341 	return 0;
342 }
343 
344 int
345 v7fs_statvfs(struct mount *mp, struct statvfs *f)
346 {
347 	struct v7fs_mount *v7fsmount = mp->mnt_data;
348 	struct v7fs_self *fs = v7fsmount->core;
349 
350 	DPRINTF("scratch remain=%d\n", fs->scratch_remain);
351 
352 	v7fs_superblock_status(fs);
353 
354 	f->f_bsize = V7FS_BSIZE;
355 	f->f_frsize = V7FS_BSIZE;
356 	f->f_iosize = V7FS_BSIZE;
357 	f->f_blocks = fs->stat.total_blocks;
358 	f->f_bfree = fs->stat.free_blocks;
359 	f->f_bavail = fs->stat.free_blocks;
360 	f->f_bresvd = 0;
361 	f->f_files = fs->stat.total_files;
362 	f->f_ffree = fs->stat.free_inode;
363 	f->f_favail = f->f_ffree;
364 	f->f_fresvd = 0;
365 	copy_statvfs_info(f, mp);
366 
367 	return 0;
368 }
369 
370 int
371 v7fs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
372 {
373 	struct v7fs_mount *v7fsmount = mp->mnt_data;
374 	struct v7fs_self *fs = v7fsmount->core;
375 	struct v7fs_node *v7fs_node;
376 	struct v7fs_inode *inode;
377 	struct vnode *v;
378 	int err, error;
379 	int retry_cnt;
380 
381 	DPRINTF("\n");
382 
383 	v7fs_superblock_writeback(fs);
384 	for (retry_cnt = 0; retry_cnt < 2; retry_cnt++) {
385 		error = 0;
386 
387 		mutex_enter(&mntvnode_lock);
388 		for (v7fs_node = LIST_FIRST(&v7fsmount->v7fs_node_head);
389 		    v7fs_node != NULL; v7fs_node = LIST_NEXT(v7fs_node, link)) {
390 			inode = &v7fs_node->inode;
391 			if (!v7fs_inode_allocated(inode)) {
392 				continue;
393 			}
394 			v = v7fs_node->vnode;
395 			mutex_enter(v->v_interlock);
396 			mutex_exit(&mntvnode_lock);
397 			err = vget(v, LK_EXCLUSIVE | LK_NOWAIT);
398 			if (err == 0) {
399 				err = VOP_FSYNC(v, cred, FSYNC_WAIT, 0, 0);
400 				vput(v);
401 			}
402 			if (err != 0)
403 				error = err;
404 			mutex_enter(&mntvnode_lock);
405 		}
406 		mutex_exit(&mntvnode_lock);
407 
408 		if (error == 0)
409 			break;
410 	}
411 
412 	return error;
413 }
414 
415 static enum vtype
416 v7fs_mode_to_vtype (v7fs_mode_t mode)
417 {
418 	enum vtype table[] = { VCHR, VDIR, VBLK, VREG, VLNK, VSOCK };
419 
420 	if ((mode & V7FS_IFMT) == V7FSBSD_IFFIFO)
421 		return VFIFO;
422 
423 	return table[((mode >> 13) & 7) - 1];
424 }
425 
426 int
427 v7fs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
428 {
429 	struct v7fs_mount *v7fsmount = mp->mnt_data;
430 	struct v7fs_self *fs = v7fsmount->core;
431 	struct vnode *vp;
432 	struct v7fs_node *v7fs_node;
433 	struct v7fs_inode inode;
434 	int error;
435 
436 	/* Lookup requested i-node */
437 	if ((error = v7fs_inode_load(fs, &inode, ino))) {
438 		DPRINTF("v7fs_inode_load failed.\n");
439 		return error;
440 	}
441 
442 retry:
443 	mutex_enter(&mntvnode_lock);
444 	for (v7fs_node = LIST_FIRST(&v7fsmount->v7fs_node_head);
445 	    v7fs_node != NULL; v7fs_node = LIST_NEXT(v7fs_node, link)) {
446 		if (v7fs_node->inode.inode_number == ino) {
447 			vp = v7fs_node->vnode;
448 			mutex_enter(vp->v_interlock);
449 			mutex_exit(&mntvnode_lock);
450 			if (vget(vp, LK_EXCLUSIVE) == 0) {
451 				*vpp = vp;
452 				return 0;
453 			} else {
454 				DPRINTF("retry!\n");
455 				goto retry;
456 			}
457 		}
458 	}
459 	mutex_exit(&mntvnode_lock);
460 
461 	/* Allocate v-node. */
462 	if ((error = getnewvnode(VT_V7FS, mp, v7fs_vnodeop_p, NULL, &vp))) {
463 		DPRINTF("getnewvnode error.\n");
464 		return error;
465 	}
466 	/* Lock vnode here */
467 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
468 
469 	/* Allocate i-node */
470 	vp->v_data = pool_get(&v7fs_node_pool, PR_WAITOK);
471 	memset(vp->v_data, 0, sizeof(*v7fs_node));
472 	v7fs_node = vp->v_data;
473 	mutex_enter(&mntvnode_lock);
474 	LIST_INSERT_HEAD(&v7fsmount->v7fs_node_head, v7fs_node, link);
475 	mutex_exit(&mntvnode_lock);
476 	v7fs_node->vnode = vp;
477 	v7fs_node->v7fsmount = v7fsmount;
478 	v7fs_node->inode = inode;/*structure copy */
479 	v7fs_node->lockf = NULL; /* advlock */
480 
481 	genfs_node_init(vp, &v7fs_genfsops);
482 	uvm_vnp_setsize(vp, v7fs_inode_filesize(&inode));
483 
484 	if (ino == V7FS_ROOT_INODE) {
485 		vp->v_type = VDIR;
486 		vp->v_vflag |= VV_ROOT;
487 	} else {
488 		vp->v_type = v7fs_mode_to_vtype(inode.mode);
489 
490 		if (vp->v_type == VBLK || vp->v_type == VCHR) {
491 			dev_t rdev = inode.device;
492 			vp->v_op = v7fs_specop_p;
493 			spec_node_init(vp, rdev);
494 		} else if (vp->v_type == VFIFO) {
495 			vp->v_op = v7fs_fifoop_p;
496 		}
497 	}
498 
499 	*vpp = vp;
500 
501 	return 0;
502 }
503 
504 
505 int
506 v7fs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
507 {
508 
509 	DPRINTF("\n");
510 	/* notyet */
511 	return EOPNOTSUPP;
512 }
513 
514 int
515 v7fs_vptofh(struct vnode *vpp, struct fid *fid, size_t *fh_size)
516 {
517 
518 	DPRINTF("\n");
519 	/* notyet */
520 	return EOPNOTSUPP;
521 }
522 
523 MALLOC_DECLARE(M_V7FS);
524 MALLOC_DECLARE(M_V7FS_VNODE);
525 
526 void
527 v7fs_init(void)
528 {
529 
530 	DPRINTF("\n");
531 	malloc_type_attach(M_V7FS_VFS);
532 	malloc_type_attach(M_V7FS);
533 	malloc_type_attach(M_V7FS_VNODE);
534 	pool_init(&v7fs_node_pool, sizeof(struct v7fs_node), 0, 0, 0,
535 	    "v7fs_node_pool", &pool_allocator_nointr, IPL_NONE);
536 }
537 
538 void
539 v7fs_reinit(void)
540 {
541 
542 	/* Nothing to do. */
543 	DPRINTF("\n");
544 }
545 
546 void
547 v7fs_done(void)
548 {
549 
550 	DPRINTF("\n");
551 	pool_destroy(&v7fs_node_pool);
552 	malloc_type_detach(M_V7FS);
553 	malloc_type_detach(M_V7FS_VFS);
554 	malloc_type_detach(M_V7FS_VNODE);
555 }
556 
557 int
558 v7fs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
559     kauth_cred_t cred)
560 {
561 
562 	DPRINTF("\n");
563 	return 0;
564 }
565 
566 int
567 v7fs_mountroot(void)
568 {
569 	struct mount *mp;
570 	int error;
571 
572 	DPRINTF("");
573 	/* On mountroot, devvp (rootdev) is opened by vfs_mountroot */
574 	if ((error = is_v7fs_partition (rootvp)))
575 		return error;
576 
577 	if ((error = vfs_rootmountalloc(MOUNT_V7FS, "root_device", &mp))) {
578 		DPRINTF("mountalloc error=%d\n", error);
579 		vrele(rootvp);
580 		return error;
581 	}
582 
583 	if ((error = v7fs_mountfs(rootvp, mp, _BYTE_ORDER))) {
584 		DPRINTF("mountfs error=%d\n", error);
585 		vfs_unbusy(mp, false, NULL);
586 		vfs_destroy(mp);
587 		return error;
588 	}
589 
590 	mutex_enter(&mountlist_lock);
591 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
592 	mutex_exit(&mountlist_lock);
593 
594 	vfs_unbusy(mp, false, NULL);
595 
596 	return 0;
597 }
598 
599 /* Reload disk inode information */
600 int
601 v7fs_vnode_reload(struct mount *mp, struct vnode *vp)
602 {
603 	struct v7fs_mount *v7fsmount = mp->mnt_data;
604 	struct v7fs_self *fs = v7fsmount->core;
605 	struct v7fs_node *v7fs_node;
606 	struct v7fs_inode *inode = &((struct v7fs_node *)vp->v_data)->inode;
607 	int target_ino = inode->inode_number;
608 	int error = 0;
609 
610 	DPRINTF("#%d\n", target_ino);
611 	mutex_enter(&mntvnode_lock);
612 	for (v7fs_node = LIST_FIRST(&v7fsmount->v7fs_node_head);
613 	     v7fs_node != NULL; v7fs_node = LIST_NEXT(v7fs_node, link)) {
614 		inode = &v7fs_node->inode;
615 		if (!v7fs_inode_allocated(inode)) {
616 			continue;
617 		}
618 		if (inode->inode_number == target_ino) {
619 			error = v7fs_inode_load(fs, &v7fs_node->inode,
620 			    target_ino);
621 			DPRINTF("sync #%d error=%d\n", target_ino, error);
622 			break;
623 		}
624 	}
625 	mutex_exit(&mntvnode_lock);
626 
627 	return error;
628 }
629