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