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