Home | History | Annotate | Line # | Download | only in coda
coda_vfsops.c revision 1.7.8.1
      1 /*	$NetBSD: coda_vfsops.c,v 1.7.8.1 1999/12/27 18:34:22 wrstuden Exp $	*/
      2 
      3 /*
      4  *
      5  *             Coda: an Experimental Distributed File System
      6  *                              Release 3.1
      7  *
      8  *           Copyright (c) 1987-1998 Carnegie Mellon University
      9  *                          All Rights Reserved
     10  *
     11  * Permission  to  use, copy, modify and distribute this software and its
     12  * documentation is hereby granted,  provided  that  both  the  copyright
     13  * notice  and  this  permission  notice  appear  in  all  copies  of the
     14  * software, derivative works or  modified  versions,  and  any  portions
     15  * thereof, and that both notices appear in supporting documentation, and
     16  * that credit is given to Carnegie Mellon University  in  all  documents
     17  * and publicity pertaining to direct or indirect use of this code or its
     18  * derivatives.
     19  *
     20  * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS  KNOWN  TO  HAVE  BUGS,
     21  * SOME  OF  WHICH MAY HAVE SERIOUS CONSEQUENCES.  CARNEGIE MELLON ALLOWS
     22  * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.   CARNEGIE  MELLON
     23  * DISCLAIMS  ANY  LIABILITY  OF  ANY  KIND  FOR  ANY  DAMAGES WHATSOEVER
     24  * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE  OR  OF
     25  * ANY DERIVATIVE WORK.
     26  *
     27  * Carnegie  Mellon  encourages  users  of  this  software  to return any
     28  * improvements or extensions that  they  make,  and  to  grant  Carnegie
     29  * Mellon the rights to redistribute these changes without encumbrance.
     30  *
     31  * 	@(#) cfs/coda_vfsops.c,v 1.1.1.1 1998/08/29 21:26:45 rvb Exp $
     32  */
     33 
     34 /*
     35  * Mach Operating System
     36  * Copyright (c) 1989 Carnegie-Mellon University
     37  * All rights reserved.  The CMU software License Agreement specifies
     38  * the terms and conditions for use and redistribution.
     39  */
     40 
     41 /*
     42  * This code was written for the Coda file system at Carnegie Mellon
     43  * University.  Contributers include David Steere, James Kistler, and
     44  * M. Satyanarayanan.
     45  */
     46 
     47 #ifdef	_LKM
     48 #define	NVCODA 4
     49 #else
     50 #include <vcoda.h>
     51 #endif
     52 
     53 #include <sys/param.h>
     54 #include <sys/systm.h>
     55 #include <sys/malloc.h>
     56 #include <sys/conf.h>
     57 #include <sys/namei.h>
     58 #include <sys/mount.h>
     59 #include <sys/proc.h>
     60 #include <sys/select.h>
     61 
     62 #include <coda/coda.h>
     63 #include <coda/cnode.h>
     64 #include <coda/coda_vfsops.h>
     65 #include <coda/coda_venus.h>
     66 #include <coda/coda_subr.h>
     67 #include <coda/coda_opstats.h>
     68 /* for VN_RDEV */
     69 #include <miscfs/specfs/specdev.h>
     70 
     71 int codadebug = 0;
     72 
     73 int coda_vfsop_print_entry = 0;
     74 #define ENTRY if(coda_vfsop_print_entry) myprintf(("Entered %s\n",__FUNCTION__))
     75 
     76 struct vnode *coda_ctlvp;
     77 struct coda_mntinfo coda_mnttbl[NVCODA]; /* indexed by minor device number */
     78 
     79 /* structure to keep statistics of internally generated/satisfied calls */
     80 
     81 struct coda_op_stats coda_vfsopstats[CODA_VFSOPS_SIZE];
     82 
     83 #define MARK_ENTRY(op) (coda_vfsopstats[op].entries++)
     84 #define MARK_INT_SAT(op) (coda_vfsopstats[op].sat_intrn++)
     85 #define MARK_INT_FAIL(op) (coda_vfsopstats[op].unsat_intrn++)
     86 #define MRAK_INT_GEN(op) (coda_vfsopstats[op].gen_intrn++)
     87 
     88 extern int coda_nc_initialized;     /* Set if cache has been initialized */
     89 extern int vc_nb_open __P((dev_t, int, int, struct proc *));
     90 extern struct cdevsw cdevsw[];    /* For sanity check in coda_mount */
     91 extern struct vnodeopv_desc coda_vnodeop_opv_desc;
     92 
     93 struct vnodeopv_desc *coda_vnodeopv_descs[] = {
     94 	&coda_vnodeop_opv_desc,
     95 	NULL,
     96 };
     97 
     98 struct vfsops coda_vfsops = {
     99     MOUNT_CODA,
    100     coda_mount,
    101     coda_start,
    102     coda_unmount,
    103     coda_root,
    104     coda_quotactl,
    105     coda_nb_statfs,
    106     coda_sync,
    107     coda_vget,
    108     (int (*) (struct mount *, struct fid *, struct vnode ** ))
    109 	eopnotsupp,
    110     (int (*) (struct vnode *, struct fid *)) eopnotsupp,
    111     coda_init,
    112     coda_sysctl,
    113     (int (*)(void)) eopnotsupp,
    114     (int (*)(struct mount *, struct mbuf *, int *, struct ucred **))
    115 	eopnotsupp,
    116     coda_vnodeopv_descs,
    117     0
    118 };
    119 
    120 int
    121 coda_vfsopstats_init(void)
    122 {
    123 	register int i;
    124 
    125 	for (i=0;i<CODA_VFSOPS_SIZE;i++) {
    126 		coda_vfsopstats[i].opcode = i;
    127 		coda_vfsopstats[i].entries = 0;
    128 		coda_vfsopstats[i].sat_intrn = 0;
    129 		coda_vfsopstats[i].unsat_intrn = 0;
    130 		coda_vfsopstats[i].gen_intrn = 0;
    131 	}
    132 
    133 	return 0;
    134 }
    135 
    136 /*
    137  * cfs mount vfsop
    138  * Set up mount info record and attach it to vfs struct.
    139  */
    140 /*ARGSUSED*/
    141 int
    142 coda_mount(vfsp, path, data, ndp, p)
    143     struct mount *vfsp;		/* Allocated and initialized by mount(2) */
    144     const char *path;		/* path covered: ignored by the fs-layer */
    145     void *data;			/* Need to define a data type for this in netbsd? */
    146     struct nameidata *ndp;	/* Clobber this to lookup the device name */
    147     struct proc *p;		/* The ever-famous proc pointer */
    148 {
    149     struct vnode *dvp;
    150     struct cnode *cp;
    151     dev_t dev;
    152     struct coda_mntinfo *mi;
    153     struct vnode *rootvp;
    154     ViceFid rootfid;
    155     ViceFid ctlfid;
    156     int error;
    157 
    158     ENTRY;
    159 
    160     coda_vfsopstats_init();
    161     coda_vnodeopstats_init();
    162 
    163     MARK_ENTRY(CODA_MOUNT_STATS);
    164     if (CODA_MOUNTED(vfsp)) {
    165 	MARK_INT_FAIL(CODA_MOUNT_STATS);
    166 	return(EBUSY);
    167     }
    168 
    169     /* Validate mount device.  Similar to getmdev(). */
    170 
    171     NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, data, p);
    172     error = namei(ndp);
    173     dvp = ndp->ni_vp;
    174 
    175     if (error) {
    176 	MARK_INT_FAIL(CODA_MOUNT_STATS);
    177 	return (error);
    178     }
    179     if (dvp->v_type != VCHR) {
    180 	MARK_INT_FAIL(CODA_MOUNT_STATS);
    181 	vrele(dvp);
    182 	return(ENXIO);
    183     }
    184     dev = dvp->v_specinfo->si_rdev;
    185     vrele(dvp);
    186     if (major(dev) >= nchrdev || major(dev) < 0) {
    187 	MARK_INT_FAIL(CODA_MOUNT_STATS);
    188 	return(ENXIO);
    189     }
    190 
    191     /*
    192      * See if the device table matches our expectations.
    193      */
    194     if (cdevsw[major(dev)].d_open != vc_nb_open)
    195     {
    196 	MARK_INT_FAIL(CODA_MOUNT_STATS);
    197 	return(ENXIO);
    198     }
    199 
    200     if (minor(dev) >= NVCODA || minor(dev) < 0) {
    201 	MARK_INT_FAIL(CODA_MOUNT_STATS);
    202 	return(ENXIO);
    203     }
    204 
    205     /*
    206      * Initialize the mount record and link it to the vfs struct
    207      */
    208     mi = &coda_mnttbl[minor(dev)];
    209 
    210     if (!VC_OPEN(&mi->mi_vcomm)) {
    211 	MARK_INT_FAIL(CODA_MOUNT_STATS);
    212 	return(ENODEV);
    213     }
    214 
    215     /* No initialization (here) of mi_vcomm! */
    216     vfsp->mnt_data = (qaddr_t)mi;
    217     vfsp->mnt_stat.f_fsid.val[0] = 0;
    218     vfsp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_CODA);
    219     mi->mi_vfsp = vfsp;
    220 
    221     /*
    222      * Make a root vnode to placate the Vnode interface, but don't
    223      * actually make the CODA_ROOT call to venus until the first call
    224      * to coda_root in case a server is down while venus is starting.
    225      */
    226     rootfid.Volume = 0;
    227     rootfid.Vnode = 0;
    228     rootfid.Unique = 0;
    229     cp = make_coda_node(&rootfid, vfsp, VDIR);
    230     rootvp = CTOV(cp);
    231     rootvp->v_flag |= VROOT;
    232 
    233     ctlfid.Volume = CTL_VOL;
    234     ctlfid.Vnode = CTL_VNO;
    235     ctlfid.Unique = CTL_UNI;
    236 /*  cp = make_coda_node(&ctlfid, vfsp, VCHR);
    237     The above code seems to cause a loop in the cnode links.
    238     I don't totally understand when it happens, it is caught
    239     when closing down the system.
    240  */
    241     cp = make_coda_node(&ctlfid, 0, VCHR);
    242 
    243     coda_ctlvp = CTOV(cp);
    244 
    245     /* Add vfs and rootvp to chain of vfs hanging off mntinfo */
    246     mi->mi_vfsp = vfsp;
    247     mi->mi_rootvp = rootvp;
    248 
    249     /* set filesystem block size */
    250     vfsp->mnt_stat.f_bsize = 8192;	    /* XXX -JJK */
    251 
    252     /* error is currently guaranteed to be zero, but in case some
    253        code changes... */
    254     CODADEBUG(1,
    255 	     myprintf(("coda_mount returned %d\n",error)););
    256     if (error)
    257 	MARK_INT_FAIL(CODA_MOUNT_STATS);
    258     else
    259 	MARK_INT_SAT(CODA_MOUNT_STATS);
    260 
    261     return(error);
    262 }
    263 
    264 int
    265 coda_start(vfsp, flags, p)
    266     struct mount *vfsp;
    267     int flags;
    268     struct proc *p;
    269 {
    270     ENTRY;
    271     return (0);
    272 }
    273 
    274 int
    275 coda_unmount(vfsp, mntflags, p)
    276     struct mount *vfsp;
    277     int mntflags;
    278     struct proc *p;
    279 {
    280     struct coda_mntinfo *mi = vftomi(vfsp);
    281     int active, error = 0;
    282 
    283     ENTRY;
    284     MARK_ENTRY(CODA_UMOUNT_STATS);
    285     if (!CODA_MOUNTED(vfsp)) {
    286 	MARK_INT_FAIL(CODA_UMOUNT_STATS);
    287 	return(EINVAL);
    288     }
    289 
    290     if (mi->mi_vfsp == vfsp) {	/* We found the victim */
    291 	if (!IS_UNMOUNTING(VTOC(mi->mi_rootvp)))
    292 	    return (EBUSY); 	/* Venus is still running */
    293 
    294 #ifdef	DEBUG
    295 	printf("coda_unmount: ROOT: vp %p, cp %p\n", mi->mi_rootvp, VTOC(mi->mi_rootvp));
    296 #endif
    297 	vrele(mi->mi_rootvp);
    298 
    299 	active = coda_kill(vfsp, NOT_DOWNCALL);
    300 	mi->mi_rootvp->v_flag &= ~VROOT;
    301 	error = vflush(mi->mi_vfsp, NULLVP, FORCECLOSE);
    302 	printf("coda_unmount: active = %d, vflush active %d\n", active, error);
    303 	error = 0;
    304 
    305 	/* I'm going to take this out to allow lookups to go through. I'm
    306 	 * not sure it's important anyway. -- DCS 2/2/94
    307 	 */
    308 	/* vfsp->VFS_DATA = NULL; */
    309 
    310 	/* No more vfsp's to hold onto */
    311 	mi->mi_vfsp = NULL;
    312 	mi->mi_rootvp = NULL;
    313 
    314 	if (error)
    315 	    MARK_INT_FAIL(CODA_UMOUNT_STATS);
    316 	else
    317 	    MARK_INT_SAT(CODA_UMOUNT_STATS);
    318 
    319 	return(error);
    320     }
    321     return (EINVAL);
    322 }
    323 
    324 /*
    325  * find root of cfs
    326  */
    327 int
    328 coda_root(vfsp, vpp)
    329 	struct mount *vfsp;
    330 	struct vnode **vpp;
    331 {
    332     struct coda_mntinfo *mi = vftomi(vfsp);
    333     struct vnode **result;
    334     int error;
    335     struct proc *p = curproc;    /* XXX - bnoble */
    336     ViceFid VFid;
    337 
    338     ENTRY;
    339     MARK_ENTRY(CODA_ROOT_STATS);
    340     result = NULL;
    341 
    342     if (vfsp == mi->mi_vfsp) {
    343 	if ((VTOC(mi->mi_rootvp)->c_fid.Volume != 0) ||
    344 	    (VTOC(mi->mi_rootvp)->c_fid.Vnode != 0) ||
    345 	    (VTOC(mi->mi_rootvp)->c_fid.Unique != 0))
    346 	    { /* Found valid root. */
    347 		*vpp = mi->mi_rootvp;
    348 		/* On Mach, this is vref.  On NetBSD, VOP_LOCK */
    349 		vref(*vpp);
    350 		vn_lock(*vpp, LK_EXCLUSIVE);
    351 		MARK_INT_SAT(CODA_ROOT_STATS);
    352 		return(0);
    353 	    }
    354     }
    355 
    356     error = venus_root(vftomi(vfsp), p->p_cred->pc_ucred, p, &VFid);
    357 
    358     if (!error) {
    359 	/*
    360 	 * Save the new rootfid in the cnode, and rehash the cnode into the
    361 	 * cnode hash with the new fid key.
    362 	 */
    363 	coda_unsave(VTOC(mi->mi_rootvp));
    364 	VTOC(mi->mi_rootvp)->c_fid = VFid;
    365 	coda_save(VTOC(mi->mi_rootvp));
    366 
    367 	*vpp = mi->mi_rootvp;
    368 	vref(*vpp);
    369 	vn_lock(*vpp, LK_EXCLUSIVE);
    370 	MARK_INT_SAT(CODA_ROOT_STATS);
    371 	goto exit;
    372     } else if (error == ENODEV || error == EINTR) {
    373 	/* Gross hack here! */
    374 	/*
    375 	 * If Venus fails to respond to the CODA_ROOT call, coda_call returns
    376 	 * ENODEV. Return the uninitialized root vnode to allow vfs
    377 	 * operations such as unmount to continue. Without this hack,
    378 	 * there is no way to do an unmount if Venus dies before a
    379 	 * successful CODA_ROOT call is done. All vnode operations
    380 	 * will fail.
    381 	 */
    382 	*vpp = mi->mi_rootvp;
    383 	vref(*vpp);
    384 	vn_lock(*vpp, LK_EXCLUSIVE);
    385 	MARK_INT_FAIL(CODA_ROOT_STATS);
    386 	error = 0;
    387 	goto exit;
    388     } else {
    389 	CODADEBUG( CODA_ROOT, myprintf(("error %d in CODA_ROOT\n", error)); );
    390 	MARK_INT_FAIL(CODA_ROOT_STATS);
    391 
    392 	goto exit;
    393     }
    394  exit:
    395     return(error);
    396 }
    397 
    398 int
    399 coda_quotactl(vfsp, cmd, uid, arg, p)
    400     struct mount *vfsp;
    401     int cmd;
    402     uid_t uid;
    403     caddr_t arg;
    404     struct proc *p;
    405 {
    406     ENTRY;
    407     return (EOPNOTSUPP);
    408 }
    409 
    410 /*
    411  * Get file system statistics.
    412  */
    413 int
    414 coda_nb_statfs(vfsp, sbp, p)
    415     register struct mount *vfsp;
    416     struct statfs *sbp;
    417     struct proc *p;
    418 {
    419     ENTRY;
    420 /*  MARK_ENTRY(CODA_STATFS_STATS); */
    421     if (!CODA_MOUNTED(vfsp)) {
    422 /*	MARK_INT_FAIL(CODA_STATFS_STATS);*/
    423 	return(EINVAL);
    424     }
    425 
    426     bzero(sbp, sizeof(struct statfs));
    427     /* XXX - what to do about f_flags, others? --bnoble */
    428     /* Below This is what AFS does
    429     	#define NB_SFS_SIZ 0x895440
    430      */
    431     /* Note: Normal fs's have a bsize of 0x400 == 1024 */
    432     sbp->f_type = 0;
    433     sbp->f_bsize = 8192; /* XXX */
    434     sbp->f_iosize = 8192; /* XXX */
    435 #define NB_SFS_SIZ 0x8AB75D
    436     sbp->f_blocks = NB_SFS_SIZ;
    437     sbp->f_bfree = NB_SFS_SIZ;
    438     sbp->f_bavail = NB_SFS_SIZ;
    439     sbp->f_files = NB_SFS_SIZ;
    440     sbp->f_ffree = NB_SFS_SIZ;
    441     bcopy((caddr_t)&(vfsp->mnt_stat.f_fsid), (caddr_t)&(sbp->f_fsid), sizeof (fsid_t));
    442     strncpy(sbp->f_fstypename, MOUNT_CODA, MFSNAMELEN-1);
    443     strcpy(sbp->f_mntonname, "/coda");
    444     strcpy(sbp->f_mntfromname, "CODA");
    445 /*  MARK_INT_SAT(CODA_STATFS_STATS); */
    446     return(0);
    447 }
    448 
    449 /*
    450  * Flush any pending I/O.
    451  */
    452 int
    453 coda_sync(vfsp, waitfor, cred, p)
    454     struct mount *vfsp;
    455     int    waitfor;
    456     struct ucred *cred;
    457     struct proc *p;
    458 {
    459     ENTRY;
    460     MARK_ENTRY(CODA_SYNC_STATS);
    461     MARK_INT_SAT(CODA_SYNC_STATS);
    462     return(0);
    463 }
    464 
    465 int
    466 coda_vget(vfsp, ino, vpp)
    467     struct mount *vfsp;
    468     ino_t ino;
    469     struct vnode **vpp;
    470 {
    471     ENTRY;
    472     return (EOPNOTSUPP);
    473 }
    474 
    475 /*
    476  * fhtovp is now what vget used to be in 4.3-derived systems.  For
    477  * some silly reason, vget is now keyed by a 32 bit ino_t, rather than
    478  * a type-specific fid.
    479  */
    480 int
    481 coda_fhtovp(vfsp, fhp, nam, vpp, exflagsp, creadanonp)
    482     register struct mount *vfsp;
    483     struct fid *fhp;
    484     struct mbuf *nam;
    485     struct vnode **vpp;
    486     int *exflagsp;
    487     struct ucred **creadanonp;
    488 {
    489     struct cfid *cfid = (struct cfid *)fhp;
    490     struct cnode *cp = 0;
    491     int error;
    492     struct proc *p = curproc; /* XXX -mach */
    493     ViceFid VFid;
    494     int vtype;
    495 
    496     ENTRY;
    497 
    498     MARK_ENTRY(CODA_VGET_STATS);
    499     /* Check for vget of control object. */
    500     if (IS_CTL_FID(&cfid->cfid_fid)) {
    501 	*vpp = coda_ctlvp;
    502 	vref(coda_ctlvp);
    503 	MARK_INT_SAT(CODA_VGET_STATS);
    504 	return(0);
    505     }
    506 
    507     error = venus_fhtovp(vftomi(vfsp), &cfid->cfid_fid, p->p_cred->pc_ucred, p, &VFid, &vtype);
    508 
    509     if (error) {
    510 	CODADEBUG(CODA_VGET, myprintf(("vget error %d\n",error));)
    511 	    *vpp = (struct vnode *)0;
    512     } else {
    513 	CODADEBUG(CODA_VGET,
    514 		 myprintf(("vget: vol %lx vno %lx uni %lx type %d result %d\n",
    515 			VFid.Volume, VFid.Vnode, VFid.Unique, vtype, error)); )
    516 
    517 	cp = make_coda_node(&VFid, vfsp, vtype);
    518 	*vpp = CTOV(cp);
    519     }
    520     return(error);
    521 }
    522 
    523 int
    524 coda_vptofh(vnp, fidp)
    525     struct vnode *vnp;
    526     struct fid   *fidp;
    527 {
    528     ENTRY;
    529     return (EOPNOTSUPP);
    530 }
    531 
    532 void
    533 coda_init(void)
    534 {
    535     ENTRY;
    536 }
    537 
    538 int
    539 coda_sysctl(name, namelen, oldp, oldlp, newp, newl, p)
    540 	int *name;
    541 	u_int namelen;
    542 	void *oldp;
    543 	size_t *oldlp;
    544 	void *newp;
    545 	size_t newl;
    546 	struct proc *p;
    547 {
    548 
    549 	/* all sysctl names at this level are terminal */
    550 	if (namelen != 1)
    551 		return (ENOTDIR);		/* overloaded */
    552 
    553 	switch (name[0]) {
    554 /*
    555 	case FFS_CLUSTERREAD:
    556 		return (sysctl_int(oldp, oldlp, newp, newl, &doclusterread));
    557  */
    558 	default:
    559 		return (EOPNOTSUPP);
    560 	}
    561 	/* NOTREACHED */
    562 }
    563 
    564 /*
    565  * To allow for greater ease of use, some vnodes may be orphaned when
    566  * Venus dies.  Certain operations should still be allowed to go
    567  * through, but without propagating ophan-ness.  So this function will
    568  * get a new vnode for the file from the current run of Venus.  */
    569 
    570 int
    571 getNewVnode(vpp)
    572      struct vnode **vpp;
    573 {
    574     struct cfid cfid;
    575     struct coda_mntinfo *mi = vftomi((*vpp)->v_mount);
    576 
    577     ENTRY;
    578 
    579     cfid.cfid_len = (short)sizeof(ViceFid);
    580     cfid.cfid_fid = VTOC(*vpp)->c_fid;	/* Structure assignment. */
    581     /* XXX ? */
    582 
    583     /* We're guessing that if set, the 1st element on the list is a
    584      * valid vnode to use. If not, return ENODEV as venus is dead.
    585      */
    586     if (mi->mi_vfsp == NULL)
    587 	return ENODEV;
    588 
    589     return coda_fhtovp(mi->mi_vfsp, (struct fid*)&cfid, NULL, vpp,
    590 		      NULL, NULL);
    591 }
    592 
    593 #include <ufs/ufs/quota.h>
    594 #include <ufs/ufs/ufsmount.h>
    595 /* get the mount structure corresponding to a given device.  Assume
    596  * device corresponds to a UFS. Return NULL if no device is found.
    597  */
    598 struct mount *devtomp(dev)
    599     dev_t dev;
    600 {
    601     struct mount *mp, *nmp;
    602 
    603     for (mp = mountlist.cqh_first; mp != (void*)&mountlist; mp = nmp) {
    604 	nmp = mp->mnt_list.cqe_next;
    605 	if ((!strcmp(mp->mnt_op->vfs_name, MOUNT_UFS)) &&
    606 	    ((VFSTOUFS(mp))->um_dev == (dev_t) dev)) {
    607 	    /* mount corresponds to UFS and the device matches one we want */
    608 	    return(mp);
    609 	}
    610     }
    611     /* mount structure wasn't found */
    612     return(NULL);
    613 }
    614