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