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