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