Home | History | Annotate | Line # | Download | only in filecorefs
filecore_vnops.c revision 1.1.2.2
      1 /*	$NetBSD: filecore_vnops.c,v 1.1.2.2 2002/12/29 19:55:53 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 Andrew McMurry
      5  * Copyright (c) 1994 The Regents of the University of California.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	filecore_vnops.c	1.2	1998/8/18
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: filecore_vnops.c,v 1.1.2.2 2002/12/29 19:55:53 thorpej Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/namei.h>
     45 #include <sys/resourcevar.h>
     46 #include <sys/kernel.h>
     47 #include <sys/file.h>
     48 #include <sys/stat.h>
     49 #include <sys/buf.h>
     50 #include <sys/proc.h>
     51 #include <sys/conf.h>
     52 #include <sys/mount.h>
     53 #include <sys/vnode.h>
     54 #include <sys/malloc.h>
     55 #include <sys/dirent.h>
     56 
     57 #include <miscfs/genfs/genfs.h>
     58 #include <miscfs/specfs/specdev.h>
     59 
     60 #include <fs/filecorefs/filecore.h>
     61 #include <fs/filecorefs/filecore_extern.h>
     62 #include <fs/filecorefs/filecore_node.h>
     63 
     64 /*
     65  * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
     66  * The mode is shifted to select the owner/group/other fields. The
     67  * super user is granted all permissions.
     68  */
     69 int
     70 filecore_access(v)
     71 	void *v;
     72 {
     73 	struct vop_access_args /* {
     74 		struct vnode *a_vp;
     75 		int  a_mode;
     76 		struct ucred *a_cred;
     77 		struct proc *a_p;
     78 	} */ *ap = v;
     79 	struct vnode *vp = ap->a_vp;
     80 	struct filecore_node *ip = VTOI(vp);
     81 	struct filecore_mnt *fcmp = ip->i_mnt;
     82 
     83 	/*
     84 	 * Disallow write attempts unless the file is a socket,
     85 	 * fifo, or a block or character device resident on the
     86 	 * file system.
     87 	 */
     88 	if (ap->a_mode & VWRITE) {
     89 		switch (vp->v_type) {
     90 		case VDIR:
     91 		case VLNK:
     92 		case VREG:
     93 			return (EROFS);
     94 		default:
     95 			break;
     96 		}
     97 	}
     98 
     99 	return (vaccess(vp->v_type, filecore_mode(ip),
    100 	    fcmp->fc_uid, fcmp->fc_gid, ap->a_mode, ap->a_cred));
    101 }
    102 
    103 int
    104 filecore_getattr(v)
    105 	void *v;
    106 {
    107 	struct vop_getattr_args /* {
    108 		struct vnode *a_vp;
    109 		struct vattr *a_vap;
    110 		struct ucred *a_cred;
    111 		struct proc *a_p;
    112 	} */ *ap = v;
    113 	struct vnode *vp = ap->a_vp;
    114 	struct filecore_node *ip = VTOI(vp);
    115 	struct vattr *vap = ap->a_vap;
    116 	struct filecore_mnt *fcmp = ip->i_mnt;
    117 
    118 	vap->va_fsid	= ip->i_dev;
    119 	vap->va_fileid	= ip->i_number;
    120 
    121 	vap->va_mode	= filecore_mode(ip);
    122 	vap->va_nlink	= 1;
    123 	vap->va_uid	= fcmp->fc_uid;
    124 	vap->va_gid	= fcmp->fc_gid;
    125 	vap->va_atime	= filecore_time(ip);
    126 	vap->va_mtime	= filecore_time(ip);
    127 	vap->va_ctime	= filecore_time(ip);
    128 	vap->va_rdev	= 0;  /* We don't support specials */
    129 
    130 	vap->va_size	= (u_quad_t) ip->i_size;
    131 	vap->va_flags	= 0;
    132 	vap->va_gen	= 1;
    133 	vap->va_blocksize = fcmp->blksize;
    134 	vap->va_bytes	= vap->va_size;
    135 	vap->va_type	= vp->v_type;
    136 	return (0);
    137 }
    138 
    139 /*
    140  * Vnode op for reading.
    141  */
    142 int
    143 filecore_read(v)
    144 	void *v;
    145 {
    146 	struct vop_read_args /* {
    147 		struct vnode *a_vp;
    148 		struct uio *a_uio;
    149 		int a_ioflag;
    150 		struct ucred *a_cred;
    151 	} */ *ap = v;
    152 	struct vnode *vp = ap->a_vp;
    153 	struct uio *uio = ap->a_uio;
    154 	struct filecore_node *ip = VTOI(vp);
    155 	struct filecore_mnt *fcmp;
    156 	struct buf *bp;
    157 	daddr_t lbn, rablock;
    158 	off_t diff;
    159 	int error = 0;
    160 	long size, n, on;
    161 
    162 	if (uio->uio_resid == 0)
    163 		return (0);
    164 	if (uio->uio_offset < 0)
    165 		return (EINVAL);
    166 	if (uio->uio_offset >= ip->i_size)
    167 		return (0);
    168 	ip->i_flag |= IN_ACCESS;
    169 	fcmp = ip->i_mnt;
    170 
    171 	if (vp->v_type == VREG) {
    172 		error = 0;
    173 		while (uio->uio_resid > 0) {
    174 			void *win;
    175 			vsize_t bytelen = MIN(ip->i_size - uio->uio_offset,
    176 					      uio->uio_resid);
    177 
    178 			if (bytelen == 0) {
    179 				break;
    180 			}
    181 			win = ubc_alloc(&vp->v_uobj, uio->uio_offset,
    182 					&bytelen, UBC_READ);
    183 			error = uiomove(win, bytelen, uio);
    184 			ubc_release(win, 0);
    185 			if (error) {
    186 				break;
    187 			}
    188 		}
    189 		goto out;
    190 	}
    191 
    192 	do {
    193 		lbn = lblkno(fcmp, uio->uio_offset);
    194 		on = blkoff(fcmp, uio->uio_offset);
    195 		n = MIN(blksize(fcmp, ip, lbn) - on, uio->uio_resid);
    196 		diff = (off_t)ip->i_size - uio->uio_offset;
    197 		if (diff <= 0)
    198 			return (0);
    199 		if (diff < n)
    200 			n = diff;
    201 		size = blksize(fcmp, ip, lbn);
    202 		rablock = lbn + 1;
    203 		if (ip->i_dirent.attr & FILECORE_ATTR_DIR) {
    204 			error = filecore_dbread(ip, &bp);
    205 			on = uio->uio_offset;
    206 			n = MIN(FILECORE_DIR_SIZE - on, uio->uio_resid);
    207 			size = FILECORE_DIR_SIZE;
    208 		} else {
    209 			error = bread(vp, lbn, size, NOCRED, &bp);
    210 #ifdef FILECORE_DEBUG_BR
    211 			printf("bread(%p, %x, %ld, CRED, %p)=%d\n",
    212 			    vp, lbn, size, bp, error);
    213 #endif
    214 		}
    215 		n = MIN(n, size - bp->b_resid);
    216 		if (error) {
    217 #ifdef FILECORE_DEBUG_BR
    218 			printf("brelse(%p) vn1\n", bp);
    219 #endif
    220 			brelse(bp);
    221 			return (error);
    222 		}
    223 
    224 		error = uiomove(bp->b_data + on, (int)n, uio);
    225 #ifdef FILECORE_DEBUG_BR
    226 		printf("brelse(%p) vn2\n", bp);
    227 #endif
    228 		brelse(bp);
    229 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
    230 
    231 out:
    232 	return (error);
    233 }
    234 
    235 /*
    236  * Vnode op for readdir
    237  */
    238 int
    239 filecore_readdir(v)
    240 	void *v;
    241 {
    242 	struct vop_readdir_args /* {
    243 		struct vnode *a_vp;
    244 		struct uio *a_uio;
    245 		struct ucred *a_cred;
    246 		int *a_eofflag;
    247 		off_t **a_cookies;
    248 		int *a_ncookies;
    249 	} */ *ap = v;
    250 	struct uio *uio = ap->a_uio;
    251 	struct vnode *vdp = ap->a_vp;
    252 	struct filecore_node *dp;
    253 	struct filecore_mnt *fcmp;
    254 	struct buf *bp = NULL;
    255 	struct dirent de;
    256 	struct filecore_direntry *dep = NULL;
    257 	int error = 0;
    258 	off_t *cookies = NULL;
    259 	int ncookies = 0;
    260 	int i;
    261 	off_t uiooff;
    262 
    263 	dp = VTOI(vdp);
    264 
    265 	if ((dp->i_dirent.attr & FILECORE_ATTR_DIR) == 0)
    266 		return (ENOTDIR);
    267 
    268 	if (uio->uio_offset % FILECORE_DIRENT_SIZE != 0)
    269 		return (EINVAL);
    270 	i = uio->uio_offset / FILECORE_DIRENT_SIZE;
    271 	uiooff = uio->uio_offset;
    272 
    273 	*ap->a_eofflag = 0;
    274 	fcmp = dp->i_mnt;
    275 
    276 	error = filecore_dbread(dp, &bp);
    277 	if (error) {
    278 		brelse(bp);
    279 		return error;
    280 	}
    281 
    282 	if (ap->a_ncookies == NULL)
    283 		cookies = NULL;
    284 	else {
    285 		*ap->a_ncookies = 0;
    286 		ncookies = uio->uio_resid/16;
    287 		cookies = malloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
    288 	}
    289 
    290 	for (; ; i++) {
    291 		switch (i) {
    292 		case 0:
    293 			/* Fake the '.' entry */
    294 			de.d_fileno = dp->i_number;
    295 			de.d_type = DT_DIR;
    296 			de.d_namlen = 1;
    297 			strcpy(de.d_name, ".");
    298 			break;
    299 		case 1:
    300 			/* Fake the '..' entry */
    301 			de.d_fileno = filecore_getparent(dp);
    302 			de.d_type = DT_DIR;
    303 			de.d_namlen = 2;
    304 			strcpy(de.d_name, "..");
    305 			break;
    306 		default:
    307 			de.d_fileno = dp->i_dirent.addr +
    308 					((i - 2) << FILECORE_INO_INDEX);
    309 			dep = fcdirentry(bp->b_data, i - 2);
    310 			if (dep->attr & FILECORE_ATTR_DIR)
    311 				de.d_type = DT_DIR;
    312 			else
    313 				de.d_type = DT_REG;
    314 			if (filecore_fn2unix(dep->name, de.d_name,
    315 			    &de.d_namlen)) {
    316 				*ap->a_eofflag = 1;
    317 				goto out;
    318 			}
    319 			break;
    320 		}
    321 		de.d_reclen = DIRENT_SIZE(&de);
    322 		if (uio->uio_resid < de.d_reclen)
    323 			goto out;
    324 		error = uiomove((caddr_t) &de, de.d_reclen, uio);
    325 		if (error)
    326 			goto out;
    327 		uiooff += FILECORE_DIRENT_SIZE;
    328 
    329 		if (cookies) {
    330 			*cookies++ = i*FILECORE_DIRENT_SIZE;
    331 			(*ap->a_ncookies)++;
    332 			if (--ncookies == 0) goto out;
    333 		}
    334 	}
    335 out:
    336 	if (cookies) {
    337 		*ap->a_cookies = cookies;
    338 		if (error) {
    339 			free(cookies, M_TEMP);
    340 			*ap->a_ncookies = 0;
    341 			*ap->a_cookies = NULL;
    342 		}
    343 	}
    344 	uio->uio_offset = uiooff;
    345 
    346 #ifdef FILECORE_DEBUG_BR
    347 	printf("brelse(%p) vn3\n", bp);
    348 #endif
    349 	brelse (bp);
    350 
    351 	return (error);
    352 }
    353 
    354 /*
    355  * Return target name of a symbolic link
    356  * Shouldn't we get the parent vnode and read the data from there?
    357  * This could eventually result in deadlocks in filecore_lookup.
    358  * But otherwise the block read here is in the block buffer two times.
    359  */
    360 int
    361 filecore_readlink(v)
    362 	void *v;
    363 {
    364 #if 0
    365 	struct vop_readlink_args /* {
    366 		struct vnode *a_vp;
    367 		struct uio *a_uio;
    368 		struct ucred *a_cred;
    369 	} */ *ap = v;
    370 #endif
    371 
    372 	return (EINVAL);
    373 }
    374 
    375 int
    376 filecore_link(v)
    377 	void *v;
    378 {
    379 	struct vop_link_args /* {
    380 		struct vnode *a_dvp;
    381 		struct vnode *a_vp;
    382 		struct componentname *a_cnp;
    383 	} */ *ap = v;
    384 
    385 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
    386 	vput(ap->a_dvp);
    387 	return (EROFS);
    388 }
    389 
    390 int
    391 filecore_symlink(v)
    392 	void *v;
    393 {
    394 	struct vop_symlink_args /* {
    395 		struct vnode *a_dvp;
    396 		struct vnode **a_vpp;
    397 		struct componentname *a_cnp;
    398 		struct vattr *a_vap;
    399 		char *a_target;
    400 	} */ *ap = v;
    401 
    402 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
    403 	vput(ap->a_dvp);
    404 	return (EROFS);
    405 }
    406 
    407 /*
    408  * Calculate the logical to physical mapping if not done already,
    409  * then call the device strategy routine.
    410  */
    411 int
    412 filecore_strategy(v)
    413 	void *v;
    414 {
    415 	struct vop_strategy_args /* {
    416 		struct buf *a_bp;
    417 	} */ *ap = v;
    418 	struct buf *bp = ap->a_bp;
    419 	struct vnode *vp = bp->b_vp;
    420 	struct filecore_node *ip;
    421 	int error;
    422 
    423 	ip = VTOI(vp);
    424 	if (bp->b_blkno == bp->b_lblkno) {
    425 		error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL);
    426 		if (error) {
    427 			bp->b_error = error;
    428 			bp->b_flags |= B_ERROR;
    429 			biodone(bp);
    430 			return (error);
    431 		}
    432 		if ((long)bp->b_blkno == -1)
    433 			clrbuf(bp);
    434 	}
    435 	if ((long)bp->b_blkno == -1) {
    436 		biodone(bp);
    437 		return (0);
    438 	}
    439 	vp = ip->i_devvp;
    440 	bp->b_dev = vp->v_rdev;
    441 	VOCALL (vp->v_op, VOFFSET(vop_strategy), ap);
    442 	return (0);
    443 }
    444 
    445 /*
    446  * Print out the contents of an inode.
    447  */
    448 /*ARGSUSED*/
    449 int
    450 filecore_print(v)
    451 	void *v;
    452 {
    453 
    454 	printf("tag VT_FILECORE, filecore vnode\n");
    455 	return (0);
    456 }
    457 
    458 /*
    459  * Return POSIX pathconf information applicable to filecore filesystems.
    460  */
    461 int
    462 filecore_pathconf(v)
    463 	void *v;
    464 {
    465 	struct vop_pathconf_args /* {
    466 		struct vnode *a_vp;
    467 		int a_name;
    468 		register_t *a_retval;
    469 	} */ *ap = v;
    470 	switch (ap->a_name) {
    471 	case _PC_LINK_MAX:
    472 		*ap->a_retval = 1;
    473 		return (0);
    474 	case _PC_NAME_MAX:
    475 		*ap->a_retval = 10;
    476 		return (0);
    477 	case _PC_PATH_MAX:
    478 		*ap->a_retval = 256;
    479 		return (0);
    480 	case _PC_CHOWN_RESTRICTED:
    481 		*ap->a_retval = 1;
    482 		return (0);
    483 	case _PC_NO_TRUNC:
    484 		*ap->a_retval = 1;
    485 		return (0);
    486 	case _PC_SYNC_IO:
    487 		*ap->a_retval = 1;
    488 		return (0);
    489 	case _PC_FILESIZEBITS:
    490 		*ap->a_retval = 32;
    491 		return (0);
    492 	default:
    493 		return (EINVAL);
    494 	}
    495 	/* NOTREACHED */
    496 }
    497 
    498 /*
    499  * Global vfs data structures for isofs
    500  */
    501 #define	filecore_create	genfs_eopnotsupp
    502 #define	filecore_mknod	genfs_eopnotsupp
    503 #define	filecore_write	genfs_eopnotsupp
    504 #define	filecore_setattr	genfs_eopnotsupp
    505 #ifdef	NFSSERVER
    506 int	lease_check	__P((void *));
    507 #define	filecore_lease_check	lease_check
    508 #else
    509 #define	filecore_lease_check	genfs_nullop
    510 #endif
    511 #define	filecore_fcntl	genfs_fcntl
    512 #define	filecore_ioctl	genfs_enoioctl
    513 #define	filecore_fsync	genfs_nullop
    514 #define	filecore_remove	genfs_eopnotsupp
    515 #define	filecore_rename	genfs_eopnotsupp
    516 #define	filecore_mkdir	genfs_eopnotsupp
    517 #define	filecore_rmdir	genfs_eopnotsupp
    518 #define	filecore_advlock	genfs_eopnotsupp
    519 #define	filecore_valloc	genfs_eopnotsupp
    520 #define	filecore_vfree	genfs_nullop
    521 #define	filecore_truncate	genfs_eopnotsupp
    522 #define	filecore_update	genfs_nullop
    523 #define	filecore_bwrite	genfs_eopnotsupp
    524 #define filecore_revoke	genfs_revoke
    525 #define filecore_blkatoff	genfs_eopnotsupp
    526 
    527 /*
    528  * Global vfs data structures for filecore
    529  */
    530 int (**filecore_vnodeop_p) __P((void *));
    531 const struct vnodeopv_entry_desc filecore_vnodeop_entries[] = {
    532 	{ &vop_default_desc, vn_default_error },
    533 	{ &vop_lookup_desc, filecore_lookup },		/* lookup */
    534 	{ &vop_create_desc, filecore_create },		/* create */
    535 	{ &vop_mknod_desc, filecore_mknod },		/* mknod */
    536 	{ &vop_open_desc, filecore_open },		/* open */
    537 	{ &vop_close_desc, filecore_close },		/* close */
    538 	{ &vop_access_desc, filecore_access },		/* access */
    539 	{ &vop_getattr_desc, filecore_getattr },	/* getattr */
    540 	{ &vop_setattr_desc, filecore_setattr },	/* setattr */
    541 	{ &vop_read_desc, filecore_read },		/* read */
    542 	{ &vop_write_desc, filecore_write },		/* write */
    543 	{ &vop_lease_desc, filecore_lease_check },	/* lease */
    544 	{ &vop_fcntl_desc, filecore_fcntl },		/* fcntl */
    545 	{ &vop_ioctl_desc, filecore_ioctl },		/* ioctl */
    546 	{ &vop_poll_desc, filecore_poll },		/* poll */
    547 	{ &vop_kqfilter_desc, genfs_kqfilter },		/* kqfilter */
    548 	{ &vop_revoke_desc, filecore_revoke },		/* revoke */
    549 	{ &vop_mmap_desc, filecore_mmap },		/* mmap */
    550 	{ &vop_fsync_desc, filecore_fsync },		/* fsync */
    551 	{ &vop_seek_desc, filecore_seek },		/* seek */
    552 	{ &vop_remove_desc, filecore_remove },		/* remove */
    553 	{ &vop_link_desc, filecore_link },		/* link */
    554 	{ &vop_rename_desc, filecore_rename },		/* rename */
    555 	{ &vop_mkdir_desc, filecore_mkdir },		/* mkdir */
    556 	{ &vop_rmdir_desc, filecore_rmdir },		/* rmdir */
    557 	{ &vop_symlink_desc, filecore_symlink },	/* symlink */
    558 	{ &vop_readdir_desc, filecore_readdir },      	/* readdir */
    559 	{ &vop_readlink_desc, filecore_readlink },	/* readlink */
    560 	{ &vop_abortop_desc, filecore_abortop },       	/* abortop */
    561 	{ &vop_inactive_desc, filecore_inactive },	/* inactive */
    562 	{ &vop_reclaim_desc, filecore_reclaim },       	/* reclaim */
    563 	{ &vop_lock_desc, genfs_lock },			/* lock */
    564 	{ &vop_unlock_desc, genfs_unlock },		/* unlock */
    565 	{ &vop_bmap_desc, filecore_bmap },		/* bmap */
    566 	{ &vop_strategy_desc, filecore_strategy },	/* strategy */
    567 	{ &vop_print_desc, filecore_print },		/* print */
    568 	{ &vop_islocked_desc, genfs_islocked },		/* islocked */
    569 	{ &vop_pathconf_desc, filecore_pathconf },	/* pathconf */
    570 	{ &vop_advlock_desc, filecore_advlock },       	/* advlock */
    571 	{ &vop_blkatoff_desc, filecore_blkatoff },	/* blkatoff */
    572 	{ &vop_valloc_desc, filecore_valloc },		/* valloc */
    573 	{ &vop_vfree_desc, filecore_vfree },		/* vfree */
    574 	{ &vop_truncate_desc, filecore_truncate },	/* truncate */
    575 	{ &vop_update_desc, filecore_update },		/* update */
    576 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
    577 	{ &vop_getpages_desc, genfs_getpages },		/* getpages */
    578 	{ &vop_putpages_desc, genfs_putpages },		/* putpages */
    579 	{ NULL, NULL }
    580 };
    581 const struct vnodeopv_desc filecore_vnodeop_opv_desc =
    582 	{ &filecore_vnodeop_p, filecore_vnodeop_entries };
    583