Home | History | Annotate | Line # | Download | only in sysvbfs
sysvbfs_vfsops.c revision 1.30
      1 /*	$NetBSD: sysvbfs_vfsops.c,v 1.30 2009/06/29 05:08:17 dholland 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.30 2009/06/29 05:08:17 dholland 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 sysvbfs_args *args = data;
     74 	struct sysvbfs_mount *bmp = NULL;
     75 	struct vnode *devvp = NULL;
     76 	int error;
     77 	bool update;
     78 
     79 	DPRINTF("%s: mnt_flag=%x\n", __func__, mp->mnt_flag);
     80 
     81 	if (*data_len < sizeof *args)
     82 		return EINVAL;
     83 
     84 	if (mp->mnt_flag & MNT_GETARGS) {
     85 		if ((bmp = (void *)mp->mnt_data) == NULL)
     86 			return EIO;
     87 		args->fspec = NULL;
     88 		*data_len = sizeof *args;
     89 		return 0;
     90 	}
     91 
     92 
     93 	DPRINTF("%s: args->fspec=%s\n", __func__, args->fspec);
     94 	update = mp->mnt_flag & MNT_UPDATE;
     95 	if (args->fspec == NULL) {
     96 		/* nothing to do. */
     97 		return EINVAL;
     98 	}
     99 
    100 	if (args->fspec != NULL) {
    101 		/* Look up the name and verify that it's sane. */
    102 		error = namei_simple_user(args->fspec,
    103 					NSM_FOLLOW_NOEMULROOT, &devvp);
    104 		if (error != 0)
    105 			return (error);
    106 
    107 		if (!update) {
    108 			/*
    109 			 * Be sure this is a valid block device
    110 			 */
    111 			if (devvp->v_type != VBLK)
    112 				error = ENOTBLK;
    113 			else if (bdevsw_lookup(devvp->v_rdev) == NULL)
    114 				error = ENXIO;
    115 		} else {
    116 			/*
    117 			 * Be sure we're still naming the same device
    118 			 * used for our initial mount
    119 			 */
    120 			if (devvp != bmp->devvp)
    121 				error = EINVAL;
    122 		}
    123 	}
    124 
    125 	/*
    126 	 * If mount by non-root, then verify that user has necessary
    127 	 * permissions on the device.
    128 	 *
    129 	 * Permission to update a mount is checked higher, so here we presume
    130 	 * updating the mount is okay (for example, as far as securelevel goes)
    131 	 * which leaves us with the normal check.
    132 	 */
    133 	if (error == 0) {
    134 		int accessmode = VREAD;
    135 		if (update ?
    136 		    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
    137 		    (mp->mnt_flag & MNT_RDONLY) == 0)
    138 			accessmode |= VWRITE;
    139 
    140 		error = genfs_can_mount(devvp, accessmode, l->l_cred);
    141 	}
    142 
    143 	if (error) {
    144 		vrele(devvp);
    145 		return error;
    146 	}
    147 
    148 	if (!update) {
    149 		if ((error = sysvbfs_mountfs(devvp, mp, l)) != 0) {
    150 			vrele(devvp);
    151 			return error;
    152 		}
    153 	} else 	if (mp->mnt_flag & MNT_RDONLY) {
    154 		/* XXX: r/w -> read only */
    155 	}
    156 
    157 	return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
    158 	    mp->mnt_op->vfs_name, mp, l);
    159 }
    160 
    161 int
    162 sysvbfs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
    163 {
    164 	kauth_cred_t cred = l->l_cred;
    165 	struct sysvbfs_mount *bmp;
    166 	struct partinfo dpart;
    167 	int error, oflags;
    168 
    169 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    170 	error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
    171 	VOP_UNLOCK(devvp, 0);
    172 	if (error)
    173 		return error;
    174 
    175 	/* Open block device */
    176 	oflags = FREAD;
    177 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
    178 		oflags |= FWRITE;
    179 	if ((error = VOP_OPEN(devvp, oflags, NOCRED)) != 0)
    180 		return error;
    181 
    182 	/* Get partition information */
    183 	if ((error = VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred)) != 0) {
    184 		VOP_CLOSE(devvp, oflags, NOCRED);
    185 		return error;
    186 	}
    187 
    188 	bmp = malloc(sizeof(struct sysvbfs_mount), M_SYSVBFS_VFS, M_WAITOK);
    189 	if (bmp == NULL) {
    190 		VOP_CLOSE(devvp, oflags, NOCRED);
    191 		return ENOMEM;
    192 	}
    193 	bmp->devvp = devvp;
    194 	bmp->mountp = mp;
    195 	if ((error = sysvbfs_bfs_init(&bmp->bfs, devvp)) != 0) {
    196 		VOP_CLOSE(devvp, oflags, NOCRED);
    197 		free(bmp, M_SYSVBFS_VFS);
    198 		return error;
    199 	}
    200 	LIST_INIT(&bmp->bnode_head);
    201 
    202 	mp->mnt_data = bmp;
    203 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)devvp->v_rdev;
    204 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_SYSVBFS);
    205 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
    206 	mp->mnt_flag |= MNT_LOCAL;
    207 	mp->mnt_dev_bshift = BFS_BSHIFT;
    208 	mp->mnt_fs_bshift = BFS_BSHIFT;
    209 
    210 	DPRINTF("fstype=%d dtype=%d bsize=%d\n", dpart.part->p_fstype,
    211 	    dpart.disklab->d_type, dpart.disklab->d_secsize);
    212 
    213 	return 0;
    214 }
    215 
    216 int
    217 sysvbfs_start(struct mount *mp, int flags)
    218 {
    219 
    220 	DPRINTF("%s:\n", __func__);
    221 	/* Nothing to do. */
    222 	return 0;
    223 }
    224 
    225 int
    226 sysvbfs_unmount(struct mount *mp, int mntflags)
    227 {
    228 	struct sysvbfs_mount *bmp = (void *)mp->mnt_data;
    229 	int error;
    230 
    231 	DPRINTF("%s: %p\n", __func__, bmp);
    232 
    233 	if ((error = vflush(mp, NULLVP,
    234 	    mntflags & MNT_FORCE ? FORCECLOSE : 0)) != 0)
    235 		return error;
    236 
    237 	vn_lock(bmp->devvp, LK_EXCLUSIVE | LK_RETRY);
    238 	error = VOP_CLOSE(bmp->devvp, FREAD, NOCRED);
    239 	vput(bmp->devvp);
    240 
    241 	sysvbfs_bfs_fini(bmp->bfs);
    242 
    243 	free(bmp, M_SYSVBFS_VFS);
    244 	mp->mnt_data = NULL;
    245 	mp->mnt_flag &= ~MNT_LOCAL;
    246 
    247 	return 0;
    248 }
    249 
    250 int
    251 sysvbfs_root(struct mount *mp, struct vnode **vpp)
    252 {
    253 	struct vnode *vp;
    254 	int error;
    255 
    256 	DPRINTF("%s:\n", __func__);
    257 	if ((error = VFS_VGET(mp, BFS_ROOT_INODE, &vp)) != 0)
    258 		return error;
    259 	*vpp = vp;
    260 
    261 	return 0;
    262 }
    263 
    264 int
    265 sysvbfs_statvfs(struct mount *mp, struct statvfs *f)
    266 {
    267 	struct sysvbfs_mount *bmp = mp->mnt_data;
    268 	struct bfs *bfs = bmp->bfs;
    269 	int free_block;
    270 	size_t data_block;
    271 
    272 	data_block = (bfs->data_end - bfs->data_start) >> BFS_BSHIFT;
    273 	if (bfs_inode_alloc(bfs, 0, 0, &free_block) != 0)
    274 		free_block = 0;
    275 	else
    276 		free_block = (bfs->data_end >> BFS_BSHIFT) - free_block;
    277 
    278 	DPRINTF("%s: %d %d %d\n", __func__, bfs->data_start,
    279 	    bfs->data_end, free_block);
    280 
    281 	f->f_bsize = BFS_BSIZE;
    282 	f->f_frsize = BFS_BSIZE;
    283 	f->f_iosize = BFS_BSIZE;
    284 	f->f_blocks = data_block;
    285 	f->f_bfree = free_block;
    286 	f->f_bavail = f->f_bfree;
    287 	f->f_bresvd = 0;
    288 	f->f_files = bfs->n_inode;
    289 	f->f_ffree = bfs->max_inode - f->f_files;
    290 	f->f_favail = f->f_ffree;
    291 	f->f_fresvd = 0;
    292 	copy_statvfs_info(f, mp);
    293 
    294 	return 0;
    295 }
    296 
    297 int
    298 sysvbfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
    299 {
    300 	struct sysvbfs_mount *bmp = mp->mnt_data;
    301 	struct sysvbfs_node *bnode;
    302 	struct vnode *v;
    303 	int err, error;
    304 
    305 	DPRINTF("%s:\n", __func__);
    306 	error = 0;
    307 	mutex_enter(&mntvnode_lock);
    308 	for (bnode = LIST_FIRST(&bmp->bnode_head); bnode != NULL;
    309 	    bnode = LIST_NEXT(bnode, link)) {
    310 		v = bnode->vnode;
    311 	    	mutex_enter(&v->v_interlock);
    312 		mutex_exit(&mntvnode_lock);
    313 		err = vget(v, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
    314 		if (err == 0) {
    315 			err = VOP_FSYNC(v, cred, FSYNC_WAIT, 0, 0);
    316 			vput(v);
    317 		}
    318 		if (err != 0)
    319 			error = err;
    320 		mutex_enter(&mntvnode_lock);
    321 	}
    322 	mutex_exit(&mntvnode_lock);
    323 
    324 	return error;
    325 }
    326 
    327 int
    328 sysvbfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
    329 {
    330 	struct sysvbfs_mount *bmp = mp->mnt_data;
    331 	struct bfs *bfs = bmp->bfs;
    332 	struct vnode *vp;
    333 	struct sysvbfs_node *bnode;
    334 	struct bfs_inode *inode;
    335 	int error;
    336 
    337 	DPRINTF("%s: i-node=%lld\n", __func__, (long long)ino);
    338 	/* Lookup requested i-node */
    339 	if (!bfs_inode_lookup(bfs, ino, &inode)) {
    340 		DPRINTF("bfs_inode_lookup failed.\n");
    341 		return ENOENT;
    342 	}
    343 
    344  retry:
    345 	mutex_enter(&mntvnode_lock);
    346 	for (bnode = LIST_FIRST(&bmp->bnode_head); bnode != NULL;
    347 	    bnode = LIST_NEXT(bnode, link)) {
    348 		if (bnode->inode->number == ino) {
    349 			vp = bnode->vnode;
    350 			mutex_enter(&vp->v_interlock);
    351 			mutex_exit(&mntvnode_lock);
    352 			if (vget(vp, LK_EXCLUSIVE|LK_RETRY|LK_INTERLOCK) == 0) {
    353 				*vpp = vp;
    354 				return 0;
    355 			} else {
    356 				goto retry;
    357 			}
    358 		}
    359 	}
    360 	mutex_exit(&mntvnode_lock);
    361 
    362 	/* Allocate v-node. */
    363 	if ((error = getnewvnode(VT_SYSVBFS, mp, sysvbfs_vnodeop_p, &vp)) !=
    364 	    0) {
    365 		DPRINTF("%s: getnewvnode error.\n", __func__);
    366 		return error;
    367 	}
    368 	/* Lock vnode here */
    369 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    370 
    371 	/* Allocate i-node */
    372 	vp->v_data = pool_get(&sysvbfs_node_pool, PR_WAITOK);
    373 	memset(vp->v_data, 0, sizeof(struct sysvbfs_node));
    374 	bnode = vp->v_data;
    375 	mutex_enter(&mntvnode_lock);
    376 	LIST_INSERT_HEAD(&bmp->bnode_head, bnode, link);
    377 	mutex_exit(&mntvnode_lock);
    378 	bnode->vnode = vp;
    379 	bnode->bmp = bmp;
    380 	bnode->inode = inode;
    381 	bnode->lockf = NULL; /* advlock */
    382 
    383 	if (ino == BFS_ROOT_INODE) {	/* BFS is flat filesystem */
    384 		vp->v_type = VDIR;
    385 		vp->v_vflag |= VV_ROOT;
    386 	} else {
    387 		vp->v_type = VREG;
    388 	}
    389 
    390 	genfs_node_init(vp, &sysvbfs_genfsops);
    391 	uvm_vnp_setsize(vp, bfs_file_size(inode));
    392 	*vpp = vp;
    393 
    394 	return 0;
    395 }
    396 
    397 int
    398 sysvbfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
    399 {
    400 
    401 	DPRINTF("%s:\n", __func__);
    402 	/* notyet */
    403 	return EOPNOTSUPP;
    404 }
    405 
    406 int
    407 sysvbfs_vptofh(struct vnode *vpp, struct fid *fid, size_t *fh_size)
    408 {
    409 
    410 	DPRINTF("%s:\n", __func__);
    411 	/* notyet */
    412 	return EOPNOTSUPP;
    413 }
    414 
    415 MALLOC_DECLARE(M_BFS);
    416 MALLOC_DECLARE(M_SYSVBFS_VNODE);
    417 
    418 void
    419 sysvbfs_init(void)
    420 {
    421 
    422 	DPRINTF("%s:\n", __func__);
    423 	malloc_type_attach(M_SYSVBFS_VFS);
    424 	malloc_type_attach(M_BFS);
    425 	malloc_type_attach(M_SYSVBFS_VNODE);
    426 	pool_init(&sysvbfs_node_pool, sizeof(struct sysvbfs_node), 0, 0, 0,
    427 	    "sysvbfs_node_pool", &pool_allocator_nointr, IPL_NONE);
    428 }
    429 
    430 void
    431 sysvbfs_reinit(void)
    432 {
    433 
    434 	/* Nothing to do. */
    435 	DPRINTF("%s:\n", __func__);
    436 }
    437 
    438 void
    439 sysvbfs_done(void)
    440 {
    441 
    442 	DPRINTF("%s:\n", __func__);
    443 	pool_destroy(&sysvbfs_node_pool);
    444 	malloc_type_detach(M_BFS);
    445 	malloc_type_detach(M_SYSVBFS_VFS);
    446 	malloc_type_detach(M_SYSVBFS_VNODE);
    447 }
    448 
    449 int
    450 sysvbfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
    451     kauth_cred_t cred)
    452 {
    453 
    454 	return 0;
    455 }
    456