Home | History | Annotate | Line # | Download | only in sysvbfs
sysvbfs_vfsops.c revision 1.2.2.2
      1 /*	$NetBSD: sysvbfs_vfsops.c,v 1.2.2.2 2006/08/11 15:45:34 yamt 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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: sysvbfs_vfsops.c,v 1.2.2.2 2006/08/11 15:45:34 yamt Exp $");
     41 
     42 #include <sys/types.h>
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/pool.h>
     46 
     47 #include <sys/time.h>
     48 #include <sys/ucred.h>
     49 #include <sys/mount.h>
     50 #include <sys/disklabel.h>
     51 #include <sys/fcntl.h>
     52 #include <sys/malloc.h>
     53 #include <sys/kauth.h>
     54 
     55 /* v-node */
     56 #include <sys/namei.h>
     57 #include <sys/vnode.h>
     58 /* devsw */
     59 #include <sys/conf.h>
     60 
     61 #include <fs/sysvbfs/sysvbfs.h>	/* external interface */
     62 #include <fs/sysvbfs/bfs.h>	/* internal interface */
     63 
     64 #ifdef SYSVBFS_VNOPS_DEBUG
     65 #define	DPRINTF(fmt, args...)	printf(fmt, ##args)
     66 #else
     67 #define	DPRINTF(arg...)		((void)0)
     68 #endif
     69 
     70 MALLOC_DEFINE(M_SYSVBFS_VFS, "sysvbfs vfs", "sysvbfs vfs structures");
     71 
     72 POOL_INIT(sysvbfs_node_pool, sizeof(struct sysvbfs_node), 0, 0, 0,
     73     "sysvbfs_node_pool", &pool_allocator_nointr);
     74 
     75 int sysvbfs_mountfs(struct vnode *, struct mount *, struct lwp *);
     76 
     77 int
     78 sysvbfs_mount(struct mount *mp, const char *path, void *data,
     79     struct nameidata *ndp, struct lwp *l)
     80 {
     81 	struct sysvbfs_args args;
     82 	struct sysvbfs_mount *bmp = NULL;
     83 	struct vnode *devvp = NULL;
     84 	int error;
     85 	boolean_t update;
     86 
     87 	DPRINTF("%s: mnt_flag=%x\n", __FUNCTION__, mp->mnt_flag);
     88 
     89 	if (mp->mnt_flag & MNT_GETARGS) {
     90 		if ((bmp = (void *)mp->mnt_data) == NULL)
     91 			return EIO;
     92 		args.fspec = NULL;
     93 		return copyout(&args, data, sizeof(args));
     94 	}
     95 
     96 	if ((error = copyin(data, &args, sizeof(args))) != 0)
     97 		return error;
     98 
     99 	DPRINTF("%s: args.fspec=%s\n", __FUNCTION__, args.fspec);
    100 	update = mp->mnt_flag & MNT_UPDATE;
    101 	if (args.fspec == NULL) {
    102 		/* nothing to do. */
    103 		return EINVAL;
    104 	}
    105 
    106 	if (args.fspec != NULL) {
    107 		/* Look up the name and verify that it's sane. */
    108 		NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, l);
    109 		if ((error = namei(ndp)) != 0)
    110 			return (error);
    111 		devvp = ndp->ni_vp;
    112 
    113 		if (!update) {
    114 			/*
    115 			 * Be sure this is a valid block device
    116 			 */
    117 			if (devvp->v_type != VBLK)
    118 				error = ENOTBLK;
    119 			else if (bdevsw_lookup(devvp->v_specinfo->si_rdev) ==
    120 			    NULL)
    121 				error = ENXIO;
    122 		} else {
    123 			/*
    124 			 * Be sure we're still naming the same device
    125 			 * used for our initial mount
    126 			 */
    127 			if (devvp != bmp->devvp)
    128 				error = EINVAL;
    129 		}
    130 	}
    131 
    132 	/*
    133 	 * If mount by non-root, then verify that user has necessary
    134 	 * permissions on the device.
    135 	 */
    136 	if (error == 0 && kauth_cred_geteuid(l->l_cred) != 0) {
    137 		int accessmode = VREAD;
    138 		if (update ?
    139 		    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
    140 		    (mp->mnt_flag & MNT_RDONLY) == 0)
    141 			accessmode |= VWRITE;
    142 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    143 		error = VOP_ACCESS(devvp, accessmode, l->l_cred, l);
    144 		VOP_UNLOCK(devvp, 0);
    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, 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 	struct partinfo dpart;
    171 	int error;
    172 
    173 	if ((error = vfs_mountedon(devvp)) != 0)
    174 		return error;	/* Already mounted */
    175 	if (vcount(devvp) > 1)
    176 		return EBUSY;	/* Opened by other */
    177 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    178 	error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
    179 	VOP_UNLOCK(devvp, 0);
    180 	if (error)
    181 		return error;
    182 
    183 	/* Open block device */
    184 	if ((error = VOP_OPEN(devvp, FREAD, NOCRED, l)) != 0)
    185 		return error;
    186 
    187 	/* Get partition information */
    188 	if ((error = VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, l)) != 0)
    189 		return error;
    190 
    191 	bmp = malloc(sizeof(struct sysvbfs_mount), M_SYSVBFS_VFS, M_WAITOK);
    192 	if (bmp == NULL)
    193 		return ENOMEM;
    194 	bmp->devvp = devvp;
    195 	bmp->mountp = mp;
    196 	if ((error = sysvbfs_bfs_init(&bmp->bfs, devvp)) != 0) {
    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_BSIZE;
    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, struct lwp *l)
    218 {
    219 
    220 	DPRINTF("%s:\n", __FUNCTION__);
    221 	/* Nothing to do. */
    222 	return 0;
    223 }
    224 
    225 int
    226 sysvbfs_unmount(struct mount *mp, int mntflags, struct lwp *l)
    227 {
    228 	struct sysvbfs_mount *bmp = (void *)mp->mnt_data;
    229 	int error;
    230 
    231 	DPRINTF("%s: %p\n", __FUNCTION__, 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, l);
    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", __FUNCTION__);
    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_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg,
    266     struct lwp *l)
    267 {
    268 
    269 	DPRINTF("%s:\n", __FUNCTION__);
    270 	/* Don't support. */
    271 	return 0;
    272 }
    273 
    274 int
    275 sysvbfs_statvfs(struct mount *mp, struct statvfs *f, struct lwp *l)
    276 {
    277 	struct sysvbfs_mount *bmp = mp->mnt_data;
    278 	struct bfs *bfs = bmp->bfs;
    279 	int free_block;
    280 	size_t data_block;
    281 
    282 	data_block = (bfs->data_end - bfs->data_start) >> BFS_BSHIFT;
    283 	if (bfs_inode_alloc(bfs, 0, 0, &free_block) != 0)
    284 		free_block = 0;
    285 	else
    286 		free_block = (bfs->data_end >> BFS_BSHIFT) - free_block;
    287 
    288 	DPRINTF("%s: %d %d %d\n", __FUNCTION__, bfs->data_start,
    289 	    bfs->data_end, free_block);
    290 
    291 	f->f_bsize = BFS_BSIZE;
    292 	f->f_frsize = BFS_BSIZE;
    293 	f->f_iosize = BFS_BSIZE;
    294 	f->f_blocks = data_block;
    295 	f->f_bfree = free_block;
    296 	f->f_bavail = f->f_bfree;
    297 	f->f_bresvd = 0;
    298 	f->f_files = bfs->n_inode;
    299 	f->f_ffree = bfs->max_inode - f->f_files;
    300 	f->f_favail = f->f_ffree;
    301 	f->f_fresvd = 0;
    302 	copy_statvfs_info(f, mp);
    303 
    304 	return 0;
    305 }
    306 
    307 int
    308 sysvbfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred,
    309     struct lwp *l)
    310 {
    311 	struct sysvbfs_mount *bmp = mp->mnt_data;
    312 	struct sysvbfs_node *bnode;
    313 	struct vnode *v;
    314 	int err, error;
    315 
    316 	DPRINTF("%s:\n", __FUNCTION__);
    317 	error = 0;
    318 	simple_lock(&mntvnode_slock);
    319 	for (bnode = LIST_FIRST(&bmp->bnode_head); bnode != NULL;
    320 	    bnode = LIST_NEXT(bnode, link)) {
    321 		simple_unlock(&mntvnode_slock);
    322 		v = bnode->vnode;
    323 		err = vget(v, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
    324 		if (err == 0) {
    325 			err = VOP_FSYNC(v, cred, FSYNC_WAIT, 0, 0, l);
    326 			vput(v);
    327 		}
    328 		if (err != 0)
    329 			error = err;
    330 		simple_lock(&mntvnode_slock);
    331 	}
    332 	simple_unlock(&mntvnode_slock);
    333 
    334 	return error;
    335 }
    336 
    337 int
    338 sysvbfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
    339 {
    340 	struct sysvbfs_mount *bmp = mp->mnt_data;
    341 	struct bfs *bfs = bmp->bfs;
    342 	struct vnode *vp;
    343 	struct sysvbfs_node *bnode;
    344 	struct bfs_inode *inode;
    345 	int error;
    346 
    347 	DPRINTF("%s: i-node=%d\n", __FUNCTION__, ino);
    348 	/* Lookup requested i-node */
    349 	if (!bfs_inode_lookup(bfs, ino, &inode)) {
    350 		DPRINTF("bfs_inode_lookup failed.\n");
    351 		return ENOENT;
    352 	}
    353 	for (bnode = LIST_FIRST(&bmp->bnode_head); bnode != NULL;
    354 	    bnode = LIST_NEXT(bnode, link)) {
    355 		if (bnode->inode->number == ino) {
    356 			*vpp = bnode->vnode;
    357 		}
    358 	}
    359 
    360 	/* Allocate v-node. */
    361 	if ((error = getnewvnode(VT_SYSVBFS, mp, sysvbfs_vnodeop_p, &vp)) !=
    362 	    0) {
    363 		DPRINTF("%s: getnewvnode error.\n", __FUNCTION__);
    364 		return error;
    365 	}
    366 	/* Lock vnode here */
    367 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    368 
    369 	/* Allocate i-node */
    370 	vp->v_data = pool_get(&sysvbfs_node_pool, PR_WAITOK);
    371 	memset(vp->v_data, 0, sizeof(struct sysvbfs_node));
    372 	bnode = vp->v_data;
    373 	simple_lock(&mntvnode_slock);
    374 	LIST_INSERT_HEAD(&bmp->bnode_head, bnode, link);
    375 	simple_unlock(&mntvnode_slock);
    376 	bnode->vnode = vp;
    377 	bnode->bmp = bmp;
    378 	bnode->inode = inode;
    379 	bnode->lockf = NULL; /* advlock */
    380 
    381 	if (ino == BFS_ROOT_INODE) {	/* BFS is flat filesystem */
    382 		vp->v_type = VDIR;
    383 		vp->v_flag |= VROOT;
    384 	} else {
    385 		vp->v_type = VREG;
    386 	}
    387 	vp->v_size = bfs_file_size(inode);
    388 
    389 	genfs_node_init(vp, &sysvbfs_genfsops);
    390 	uvm_vnp_setsize(vp, vp->v_size);
    391 	*vpp = vp;
    392 
    393 	return 0;
    394 }
    395 
    396 int
    397 sysvbfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
    398 {
    399 
    400 	DPRINTF("%s:\n", __FUNCTION__);
    401 	/* notyet */
    402 	return EOPNOTSUPP;
    403 }
    404 
    405 int
    406 sysvbfs_vptofh(struct vnode *vpp, struct fid *fid, size_t *fh_size)
    407 {
    408 
    409 	DPRINTF("%s:\n", __FUNCTION__);
    410 	/* notyet */
    411 	return EOPNOTSUPP;
    412 }
    413 
    414 void
    415 sysvbfs_init(void)
    416 {
    417 
    418 	/* Nothing to do. */
    419 	DPRINTF("%s:\n", __FUNCTION__);
    420 }
    421 
    422 void
    423 sysvbfs_reinit(void)
    424 {
    425 
    426 	/* Nothing to do. */
    427 	DPRINTF("%s:\n", __FUNCTION__);
    428 }
    429 
    430 void
    431 sysvbfs_done(void)
    432 {
    433 
    434 	DPRINTF("%s:\n", __FUNCTION__);
    435 	pool_destroy(&sysvbfs_node_pool);
    436 }
    437 
    438 int
    439 sysvbfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
    440     kauth_cred_t cred)
    441 {
    442 
    443 	return 0;
    444 }
    445