Home | History | Annotate | Line # | Download | only in coda
      1  1.92    andvar /*	$NetBSD: coda_vfsops.c,v 1.92 2025/05/28 06:06:54 andvar Exp $	*/
      2   1.2       rvb 
      3   1.1       rvb /*
      4  1.42     perry  *
      5   1.2       rvb  *             Coda: an Experimental Distributed File System
      6   1.2       rvb  *                              Release 3.1
      7  1.42     perry  *
      8   1.2       rvb  *           Copyright (c) 1987-1998 Carnegie Mellon University
      9   1.2       rvb  *                          All Rights Reserved
     10  1.42     perry  *
     11   1.2       rvb  * Permission  to  use, copy, modify and distribute this software and its
     12   1.2       rvb  * documentation is hereby granted,  provided  that  both  the  copyright
     13   1.2       rvb  * notice  and  this  permission  notice  appear  in  all  copies  of the
     14   1.2       rvb  * software, derivative works or  modified  versions,  and  any  portions
     15   1.2       rvb  * thereof, and that both notices appear in supporting documentation, and
     16   1.2       rvb  * that credit is given to Carnegie Mellon University  in  all  documents
     17   1.2       rvb  * and publicity pertaining to direct or indirect use of this code or its
     18   1.2       rvb  * derivatives.
     19  1.42     perry  *
     20   1.2       rvb  * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS  KNOWN  TO  HAVE  BUGS,
     21   1.2       rvb  * SOME  OF  WHICH MAY HAVE SERIOUS CONSEQUENCES.  CARNEGIE MELLON ALLOWS
     22   1.2       rvb  * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.   CARNEGIE  MELLON
     23   1.2       rvb  * DISCLAIMS  ANY  LIABILITY  OF  ANY  KIND  FOR  ANY  DAMAGES WHATSOEVER
     24   1.2       rvb  * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE  OR  OF
     25   1.2       rvb  * ANY DERIVATIVE WORK.
     26  1.42     perry  *
     27   1.2       rvb  * Carnegie  Mellon  encourages  users  of  this  software  to return any
     28   1.2       rvb  * improvements or extensions that  they  make,  and  to  grant  Carnegie
     29   1.2       rvb  * Mellon the rights to redistribute these changes without encumbrance.
     30  1.42     perry  *
     31  1.42     perry  * 	@(#) cfs/coda_vfsops.c,v 1.1.1.1 1998/08/29 21:26:45 rvb Exp $
     32   1.2       rvb  */
     33   1.1       rvb 
     34  1.42     perry /*
     35   1.1       rvb  * Mach Operating System
     36   1.1       rvb  * Copyright (c) 1989 Carnegie-Mellon University
     37   1.1       rvb  * All rights reserved.  The CMU software License Agreement specifies
     38   1.1       rvb  * the terms and conditions for use and redistribution.
     39   1.1       rvb  */
     40   1.1       rvb 
     41   1.1       rvb /*
     42   1.1       rvb  * This code was written for the Coda file system at Carnegie Mellon
     43   1.1       rvb  * University.  Contributers include David Steere, James Kistler, and
     44  1.42     perry  * M. Satyanarayanan.
     45   1.1       rvb  */
     46  1.14     lukem 
     47  1.14     lukem #include <sys/cdefs.h>
     48  1.92    andvar __KERNEL_RCSID(0, "$NetBSD: coda_vfsops.c,v 1.92 2025/05/28 06:06:54 andvar Exp $");
     49   1.1       rvb 
     50   1.1       rvb #include <sys/param.h>
     51   1.1       rvb #include <sys/systm.h>
     52  1.31    atatat #include <sys/sysctl.h>
     53   1.1       rvb #include <sys/conf.h>
     54   1.1       rvb #include <sys/namei.h>
     55  1.35  christos #include <sys/dirent.h>
     56   1.1       rvb #include <sys/mount.h>
     57   1.1       rvb #include <sys/proc.h>
     58   1.1       rvb #include <sys/select.h>
     59  1.48      elad #include <sys/kauth.h>
     60  1.66    rumble #include <sys/module.h>
     61   1.1       rvb 
     62   1.4       rvb #include <coda/coda.h>
     63   1.4       rvb #include <coda/cnode.h>
     64   1.4       rvb #include <coda/coda_vfsops.h>
     65   1.4       rvb #include <coda/coda_venus.h>
     66   1.4       rvb #include <coda/coda_subr.h>
     67   1.4       rvb #include <coda/coda_opstats.h>
     68   1.1       rvb /* for VN_RDEV */
     69   1.1       rvb #include <miscfs/specfs/specdev.h>
     70  1.64  dholland #include <miscfs/genfs/genfs.h>
     71  1.66    rumble 
     72  1.73  christos MODULE(MODULE_CLASS_VFS, coda, "vcoda");
     73  1.21   thorpej 
     74  1.15     perry #define ENTRY if(coda_vfsop_print_entry) myprintf(("Entered %s\n",__func__))
     75   1.1       rvb 
     76  1.74  christos extern struct vnode *coda_ctlvp;
     77  1.71  christos extern struct coda_mntinfo coda_mnttbl[NVCODA]; /* indexed by minor device number */
     78   1.1       rvb 
     79   1.1       rvb /* structure to keep statistics of internally generated/satisfied calls */
     80   1.1       rvb 
     81   1.3       rvb struct coda_op_stats coda_vfsopstats[CODA_VFSOPS_SIZE];
     82   1.1       rvb 
     83   1.3       rvb #define MARK_ENTRY(op) (coda_vfsopstats[op].entries++)
     84   1.3       rvb #define MARK_INT_SAT(op) (coda_vfsopstats[op].sat_intrn++)
     85   1.3       rvb #define MARK_INT_FAIL(op) (coda_vfsopstats[op].unsat_intrn++)
     86   1.3       rvb #define MRAK_INT_GEN(op) (coda_vfsopstats[op].gen_intrn++)
     87   1.1       rvb 
     88  1.18   gehenna extern const struct cdevsw vcoda_cdevsw;
     89  1.11  jdolecek extern const struct vnodeopv_desc coda_vnodeop_opv_desc;
     90   1.1       rvb 
     91  1.11  jdolecek const struct vnodeopv_desc * const coda_vnodeopv_descs[] = {
     92   1.3       rvb 	&coda_vnodeop_opv_desc,
     93   1.1       rvb 	NULL,
     94   1.1       rvb };
     95   1.1       rvb 
     96   1.3       rvb struct vfsops coda_vfsops = {
     97  1.80   hannken 	.vfs_name = MOUNT_CODA,
     98  1.80   hannken 	.vfs_min_mount_data = 256,
     99  1.80   hannken 			/* This is the pathname, unlike every other fs */
    100  1.80   hannken 	.vfs_mount = coda_mount,
    101  1.80   hannken 	.vfs_start = coda_start,
    102  1.80   hannken 	.vfs_unmount = coda_unmount,
    103  1.80   hannken 	.vfs_root = coda_root,
    104  1.80   hannken 	.vfs_quotactl = (void *)eopnotsupp,
    105  1.80   hannken 	.vfs_statvfs = coda_nb_statvfs,
    106  1.80   hannken 	.vfs_sync = coda_sync,
    107  1.80   hannken 	.vfs_vget = coda_vget,
    108  1.84   hannken 	.vfs_loadvnode = coda_loadvnode,
    109  1.80   hannken 	.vfs_fhtovp = (void *)eopnotsupp,
    110  1.80   hannken 	.vfs_vptofh = (void *)eopnotsupp,
    111  1.80   hannken 	.vfs_init = coda_init,
    112  1.80   hannken 	.vfs_done = coda_done,
    113  1.80   hannken 	.vfs_mountroot = (void *)eopnotsupp,
    114  1.80   hannken 	.vfs_snapshot = (void *)eopnotsupp,
    115  1.80   hannken 	.vfs_extattrctl = vfs_stdextattrctl,
    116  1.85   hannken 	.vfs_suspendctl = genfs_suspendctl,
    117  1.80   hannken 	.vfs_renamelock_enter = genfs_renamelock_enter,
    118  1.80   hannken 	.vfs_renamelock_exit = genfs_renamelock_exit,
    119  1.80   hannken 	.vfs_fsync = (void *)eopnotsupp,
    120  1.80   hannken 	.vfs_opv_descs = coda_vnodeopv_descs
    121   1.1       rvb };
    122   1.1       rvb 
    123  1.66    rumble static int
    124  1.66    rumble coda_modcmd(modcmd_t cmd, void *arg)
    125  1.66    rumble {
    126  1.66    rumble 
    127  1.66    rumble 	switch (cmd) {
    128  1.66    rumble 	case MODULE_CMD_INIT:
    129  1.66    rumble 		return vfs_attach(&coda_vfsops);
    130  1.66    rumble 	case MODULE_CMD_FINI:
    131  1.66    rumble 		return vfs_detach(&coda_vfsops);
    132  1.66    rumble 	default:
    133  1.66    rumble 		return ENOTTY;
    134  1.66    rumble 	}
    135  1.66    rumble }
    136  1.29  drochner 
    137   1.1       rvb int
    138   1.3       rvb coda_vfsopstats_init(void)
    139   1.1       rvb {
    140  1.10  augustss 	int i;
    141  1.42     perry 
    142   1.3       rvb 	for (i=0;i<CODA_VFSOPS_SIZE;i++) {
    143   1.3       rvb 		coda_vfsopstats[i].opcode = i;
    144   1.3       rvb 		coda_vfsopstats[i].entries = 0;
    145   1.3       rvb 		coda_vfsopstats[i].sat_intrn = 0;
    146   1.3       rvb 		coda_vfsopstats[i].unsat_intrn = 0;
    147   1.3       rvb 		coda_vfsopstats[i].gen_intrn = 0;
    148   1.1       rvb 	}
    149  1.42     perry 
    150   1.1       rvb 	return 0;
    151   1.1       rvb }
    152   1.1       rvb 
    153   1.1       rvb /*
    154   1.1       rvb  * cfs mount vfsop
    155   1.1       rvb  * Set up mount info record and attach it to vfs struct.
    156   1.1       rvb  */
    157   1.1       rvb /*ARGSUSED*/
    158   1.1       rvb int
    159  1.45   xtraeme coda_mount(struct mount *vfsp,	/* Allocated and initialized by mount(2) */
    160  1.52  christos     const char *path,	/* path covered: ignored by the fs-layer */
    161  1.51  christos     void *data,		/* Need to define a data type for this in netbsd? */
    162  1.60     pooka     size_t *data_len)
    163   1.1       rvb {
    164  1.60     pooka     struct lwp *l = curlwp;
    165   1.1       rvb     struct vnode *dvp;
    166   1.1       rvb     struct cnode *cp;
    167   1.1       rvb     dev_t dev;
    168   1.3       rvb     struct coda_mntinfo *mi;
    169  1.43  christos     struct vnode *rtvp;
    170  1.18   gehenna     const struct cdevsw *cdev;
    171  1.29  drochner     CodaFid rootfid = INVAL_FID;
    172  1.29  drochner     CodaFid ctlfid = CTL_FID;
    173   1.1       rvb     int error;
    174   1.1       rvb 
    175  1.81      maxv     if (data == NULL)
    176  1.81      maxv 	return EINVAL;
    177  1.19  christos     if (vfsp->mnt_flag & MNT_GETARGS)
    178  1.55       dsl 	return EINVAL;
    179   1.1       rvb     ENTRY;
    180   1.1       rvb 
    181   1.3       rvb     coda_vfsopstats_init();
    182   1.3       rvb     coda_vnodeopstats_init();
    183  1.42     perry 
    184   1.3       rvb     MARK_ENTRY(CODA_MOUNT_STATS);
    185   1.3       rvb     if (CODA_MOUNTED(vfsp)) {
    186   1.3       rvb 	MARK_INT_FAIL(CODA_MOUNT_STATS);
    187   1.1       rvb 	return(EBUSY);
    188   1.1       rvb     }
    189  1.42     perry 
    190   1.1       rvb     /* Validate mount device.  Similar to getmdev(). */
    191   1.1       rvb 
    192  1.55       dsl     /*
    193  1.55       dsl      * XXX: coda passes the mount device as the entire mount args,
    194  1.92    andvar      * All other fs pass a structure containing a pointer.
    195  1.55       dsl      * In order to get sys_mount() to do the copyin() we've set a
    196  1.61       dsl      * fixed default size for the filename buffer.
    197  1.55       dsl      */
    198  1.61       dsl     /* Ensure that namei() doesn't run off the filename buffer */
    199  1.89   hannken     if (*data_len < 1 || *data_len > PATH_MAX ||
    200  1.89   hannken 	strnlen(data, *data_len) >= *data_len) {
    201  1.89   hannken 	MARK_INT_FAIL(CODA_MOUNT_STATS);
    202  1.89   hannken 	return EINVAL;
    203  1.89   hannken     }
    204  1.69  dholland     error = namei_simple_kernel((char *)data, NSM_FOLLOW_NOEMULROOT,
    205  1.69  dholland 		&dvp);
    206   1.1       rvb 
    207   1.1       rvb     if (error) {
    208   1.3       rvb 	MARK_INT_FAIL(CODA_MOUNT_STATS);
    209   1.1       rvb 	return (error);
    210   1.1       rvb     }
    211   1.1       rvb     if (dvp->v_type != VCHR) {
    212   1.3       rvb 	MARK_INT_FAIL(CODA_MOUNT_STATS);
    213   1.1       rvb 	vrele(dvp);
    214   1.1       rvb 	return(ENXIO);
    215   1.1       rvb     }
    216  1.63        ad     dev = dvp->v_rdev;
    217   1.1       rvb     vrele(dvp);
    218  1.18   gehenna     cdev = cdevsw_lookup(dev);
    219  1.18   gehenna     if (cdev == NULL) {
    220   1.3       rvb 	MARK_INT_FAIL(CODA_MOUNT_STATS);
    221   1.1       rvb 	return(ENXIO);
    222   1.1       rvb     }
    223   1.1       rvb 
    224   1.1       rvb     /*
    225   1.1       rvb      * See if the device table matches our expectations.
    226   1.1       rvb      */
    227  1.18   gehenna     if (cdev != &vcoda_cdevsw)
    228   1.1       rvb     {
    229   1.3       rvb 	MARK_INT_FAIL(CODA_MOUNT_STATS);
    230   1.1       rvb 	return(ENXIO);
    231   1.1       rvb     }
    232  1.42     perry 
    233  1.68  christos     if (minor(dev) >= NVCODA) {
    234   1.3       rvb 	MARK_INT_FAIL(CODA_MOUNT_STATS);
    235   1.1       rvb 	return(ENXIO);
    236   1.1       rvb     }
    237  1.42     perry 
    238   1.1       rvb     /*
    239   1.1       rvb      * Initialize the mount record and link it to the vfs struct
    240   1.1       rvb      */
    241   1.3       rvb     mi = &coda_mnttbl[minor(dev)];
    242  1.42     perry 
    243   1.1       rvb     if (!VC_OPEN(&mi->mi_vcomm)) {
    244   1.3       rvb 	MARK_INT_FAIL(CODA_MOUNT_STATS);
    245   1.1       rvb 	return(ENODEV);
    246   1.1       rvb     }
    247  1.42     perry 
    248   1.1       rvb     /* No initialization (here) of mi_vcomm! */
    249  1.17     soren     vfsp->mnt_data = mi;
    250  1.34  christos     vfsp->mnt_stat.f_fsidx.__fsid_val[0] = 0;
    251  1.34  christos     vfsp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_CODA);
    252  1.34  christos     vfsp->mnt_stat.f_fsid = vfsp->mnt_stat.f_fsidx.__fsid_val[0];
    253  1.70  christos     vfsp->mnt_stat.f_namemax = CODA_MAXNAMLEN;
    254   1.1       rvb     mi->mi_vfsp = vfsp;
    255  1.42     perry 
    256   1.1       rvb     /*
    257   1.1       rvb      * Make a root vnode to placate the Vnode interface, but don't
    258   1.3       rvb      * actually make the CODA_ROOT call to venus until the first call
    259   1.3       rvb      * to coda_root in case a server is down while venus is starting.
    260   1.1       rvb      */
    261   1.3       rvb     cp = make_coda_node(&rootfid, vfsp, VDIR);
    262  1.43  christos     rtvp = CTOV(cp);
    263  1.59        ad     rtvp->v_vflag |= VV_ROOT;
    264  1.42     perry 
    265  1.82   hannken     cp = make_coda_node(&ctlfid, vfsp, VCHR);
    266   1.1       rvb 
    267   1.3       rvb     coda_ctlvp = CTOV(cp);
    268   1.1       rvb 
    269   1.1       rvb     /* Add vfs and rootvp to chain of vfs hanging off mntinfo */
    270   1.1       rvb     mi->mi_vfsp = vfsp;
    271  1.43  christos     mi->mi_rootvp = rtvp;
    272  1.42     perry 
    273   1.1       rvb     /* set filesystem block size */
    274   1.1       rvb     vfsp->mnt_stat.f_bsize = 8192;	    /* XXX -JJK */
    275  1.34  christos     vfsp->mnt_stat.f_frsize = 8192;	    /* XXX -JJK */
    276   1.1       rvb 
    277   1.1       rvb     /* error is currently guaranteed to be zero, but in case some
    278   1.1       rvb        code changes... */
    279   1.3       rvb     CODADEBUG(1,
    280   1.3       rvb 	     myprintf(("coda_mount returned %d\n",error)););
    281   1.1       rvb     if (error)
    282   1.3       rvb 	MARK_INT_FAIL(CODA_MOUNT_STATS);
    283   1.1       rvb     else
    284   1.3       rvb 	MARK_INT_SAT(CODA_MOUNT_STATS);
    285  1.22  christos 
    286  1.56     pooka     return set_statvfs_info("/coda", UIO_SYSSPACE, "CODA", UIO_SYSSPACE,
    287  1.56     pooka 	vfsp->mnt_op->vfs_name, vfsp, l);
    288   1.1       rvb }
    289   1.1       rvb 
    290   1.1       rvb int
    291  1.60     pooka coda_start(struct mount *vfsp, int flags)
    292   1.1       rvb {
    293   1.1       rvb     ENTRY;
    294  1.28  drochner     vftomi(vfsp)->mi_started = 1;
    295   1.1       rvb     return (0);
    296   1.1       rvb }
    297   1.1       rvb 
    298   1.1       rvb int
    299  1.60     pooka coda_unmount(struct mount *vfsp, int mntflags)
    300   1.1       rvb {
    301   1.3       rvb     struct coda_mntinfo *mi = vftomi(vfsp);
    302   1.1       rvb     int active, error = 0;
    303  1.42     perry 
    304   1.1       rvb     ENTRY;
    305   1.3       rvb     MARK_ENTRY(CODA_UMOUNT_STATS);
    306   1.3       rvb     if (!CODA_MOUNTED(vfsp)) {
    307   1.3       rvb 	MARK_INT_FAIL(CODA_UMOUNT_STATS);
    308   1.1       rvb 	return(EINVAL);
    309   1.1       rvb     }
    310  1.42     perry 
    311   1.1       rvb     if (mi->mi_vfsp == vfsp) {	/* We found the victim */
    312   1.1       rvb 	if (!IS_UNMOUNTING(VTOC(mi->mi_rootvp)))
    313   1.1       rvb 	    return (EBUSY); 	/* Venus is still running */
    314   1.1       rvb 
    315   1.1       rvb #ifdef	DEBUG
    316   1.3       rvb 	printf("coda_unmount: ROOT: vp %p, cp %p\n", mi->mi_rootvp, VTOC(mi->mi_rootvp));
    317   1.1       rvb #endif
    318  1.28  drochner 	mi->mi_started = 0;
    319  1.28  drochner 
    320   1.1       rvb 	vrele(mi->mi_rootvp);
    321  1.82   hannken 	vrele(coda_ctlvp);
    322   1.1       rvb 
    323   1.3       rvb 	active = coda_kill(vfsp, NOT_DOWNCALL);
    324  1.59        ad 	mi->mi_rootvp->v_vflag &= ~VV_ROOT;
    325   1.1       rvb 	error = vflush(mi->mi_vfsp, NULLVP, FORCECLOSE);
    326   1.3       rvb 	printf("coda_unmount: active = %d, vflush active %d\n", active, error);
    327   1.1       rvb 	error = 0;
    328   1.2       rvb 
    329   1.1       rvb 	/* I'm going to take this out to allow lookups to go through. I'm
    330   1.1       rvb 	 * not sure it's important anyway. -- DCS 2/2/94
    331   1.1       rvb 	 */
    332   1.1       rvb 	/* vfsp->VFS_DATA = NULL; */
    333   1.1       rvb 
    334   1.1       rvb 	/* No more vfsp's to hold onto */
    335   1.1       rvb 	mi->mi_vfsp = NULL;
    336   1.1       rvb 	mi->mi_rootvp = NULL;
    337   1.1       rvb 
    338   1.1       rvb 	if (error)
    339   1.3       rvb 	    MARK_INT_FAIL(CODA_UMOUNT_STATS);
    340   1.1       rvb 	else
    341   1.3       rvb 	    MARK_INT_SAT(CODA_UMOUNT_STATS);
    342   1.1       rvb 
    343   1.1       rvb 	return(error);
    344   1.1       rvb     }
    345   1.1       rvb     return (EINVAL);
    346   1.1       rvb }
    347   1.1       rvb 
    348   1.1       rvb /*
    349   1.1       rvb  * find root of cfs
    350   1.1       rvb  */
    351   1.1       rvb int
    352  1.87        ad coda_root(struct mount *vfsp, int lktype, struct vnode **vpp)
    353   1.1       rvb {
    354   1.3       rvb     struct coda_mntinfo *mi = vftomi(vfsp);
    355   1.1       rvb     int error;
    356  1.49        ad     struct lwp *l = curlwp;    /* XXX - bnoble */
    357  1.29  drochner     CodaFid VFid;
    358  1.29  drochner     static const CodaFid invalfid = INVAL_FID;
    359   1.1       rvb 
    360   1.1       rvb     ENTRY;
    361   1.3       rvb     MARK_ENTRY(CODA_ROOT_STATS);
    362  1.42     perry 
    363   1.1       rvb     if (vfsp == mi->mi_vfsp) {
    364  1.29  drochner     	if (memcmp(&VTOC(mi->mi_rootvp)->c_fid, &invalfid, sizeof(CodaFid)))
    365   1.1       rvb 	    { /* Found valid root. */
    366   1.1       rvb 		*vpp = mi->mi_rootvp;
    367   1.1       rvb 		/* On Mach, this is vref.  On NetBSD, VOP_LOCK */
    368   1.1       rvb 		vref(*vpp);
    369  1.87        ad 		vn_lock(*vpp, lktype);
    370   1.3       rvb 		MARK_INT_SAT(CODA_ROOT_STATS);
    371   1.1       rvb 		return(0);
    372   1.1       rvb 	    }
    373   1.1       rvb     }
    374   1.1       rvb 
    375  1.49        ad     error = venus_root(vftomi(vfsp), l->l_cred, l->l_proc, &VFid);
    376   1.1       rvb 
    377   1.1       rvb     if (!error) {
    378  1.84   hannken 	struct cnode *cp = VTOC(mi->mi_rootvp);
    379  1.84   hannken 
    380   1.1       rvb 	/*
    381  1.84   hannken 	 * Save the new rootfid in the cnode, and rekey the cnode
    382  1.84   hannken 	 * with the new fid key.
    383   1.1       rvb 	 */
    384  1.84   hannken 	error = vcache_rekey_enter(vfsp, mi->mi_rootvp,
    385  1.84   hannken 	    &invalfid, sizeof(CodaFid), &VFid, sizeof(CodaFid));
    386  1.84   hannken 	if (error)
    387  1.84   hannken 	        goto exit;
    388  1.84   hannken 	cp->c_fid = VFid;
    389  1.84   hannken 	vcache_rekey_exit(vfsp, mi->mi_rootvp,
    390  1.84   hannken 	    &invalfid, sizeof(CodaFid), &cp->c_fid, sizeof(CodaFid));
    391   1.1       rvb 
    392   1.1       rvb 	*vpp = mi->mi_rootvp;
    393   1.1       rvb 	vref(*vpp);
    394  1.88   hannken 	vn_lock(*vpp, lktype);
    395   1.3       rvb 	MARK_INT_SAT(CODA_ROOT_STATS);
    396   1.1       rvb 	goto exit;
    397   1.6       rvb     } else if (error == ENODEV || error == EINTR) {
    398   1.1       rvb 	/* Gross hack here! */
    399   1.1       rvb 	/*
    400   1.3       rvb 	 * If Venus fails to respond to the CODA_ROOT call, coda_call returns
    401   1.1       rvb 	 * ENODEV. Return the uninitialized root vnode to allow vfs
    402   1.1       rvb 	 * operations such as unmount to continue. Without this hack,
    403  1.42     perry 	 * there is no way to do an unmount if Venus dies before a
    404  1.42     perry 	 * successful CODA_ROOT call is done. All vnode operations
    405   1.1       rvb 	 * will fail.
    406   1.1       rvb 	 */
    407   1.1       rvb 	*vpp = mi->mi_rootvp;
    408   1.1       rvb 	vref(*vpp);
    409  1.88   hannken 	vn_lock(*vpp, lktype);
    410   1.3       rvb 	MARK_INT_FAIL(CODA_ROOT_STATS);
    411   1.1       rvb 	error = 0;
    412   1.1       rvb 	goto exit;
    413   1.1       rvb     } else {
    414   1.3       rvb 	CODADEBUG( CODA_ROOT, myprintf(("error %d in CODA_ROOT\n", error)); );
    415   1.3       rvb 	MARK_INT_FAIL(CODA_ROOT_STATS);
    416  1.42     perry 
    417   1.1       rvb 	goto exit;
    418   1.1       rvb     }
    419   1.1       rvb  exit:
    420   1.1       rvb     return(error);
    421   1.1       rvb }
    422   1.1       rvb 
    423   1.1       rvb /*
    424   1.1       rvb  * Get file system statistics.
    425   1.1       rvb  */
    426   1.1       rvb int
    427  1.60     pooka coda_nb_statvfs(struct mount *vfsp, struct statvfs *sbp)
    428   1.1       rvb {
    429  1.60     pooka     struct lwp *l = curlwp;
    430  1.16      phil     struct coda_statfs fsstat;
    431  1.16      phil     int error;
    432  1.16      phil 
    433   1.1       rvb     ENTRY;
    434  1.16      phil     MARK_ENTRY(CODA_STATFS_STATS);
    435   1.3       rvb     if (!CODA_MOUNTED(vfsp)) {
    436  1.16      phil /*	MARK_INT_FAIL(CODA_STATFS_STATS); */
    437   1.1       rvb 	return(EINVAL);
    438   1.1       rvb     }
    439  1.42     perry 
    440   1.1       rvb     /* XXX - what to do about f_flags, others? --bnoble */
    441   1.1       rvb     /* Below This is what AFS does
    442   1.1       rvb     	#define NB_SFS_SIZ 0x895440
    443   1.1       rvb      */
    444   1.1       rvb     /* Note: Normal fs's have a bsize of 0x400 == 1024 */
    445  1.16      phil 
    446  1.49        ad     error = venus_statfs(vftomi(vfsp), l->l_cred, l, &fsstat);
    447  1.16      phil 
    448  1.16      phil     if (!error) {
    449  1.16      phil 	sbp->f_bsize = 8192; /* XXX */
    450  1.34  christos 	sbp->f_frsize = 8192; /* XXX */
    451  1.16      phil 	sbp->f_iosize = 8192; /* XXX */
    452  1.16      phil 	sbp->f_blocks = fsstat.f_blocks;
    453  1.16      phil 	sbp->f_bfree  = fsstat.f_bfree;
    454  1.16      phil 	sbp->f_bavail = fsstat.f_bavail;
    455  1.34  christos 	sbp->f_bresvd = 0;
    456  1.16      phil 	sbp->f_files  = fsstat.f_files;
    457  1.16      phil 	sbp->f_ffree  = fsstat.f_ffree;
    458  1.34  christos 	sbp->f_favail = fsstat.f_ffree;
    459  1.34  christos 	sbp->f_fresvd = 0;
    460  1.34  christos 	copy_statvfs_info(sbp, vfsp);
    461  1.16      phil     }
    462  1.16      phil 
    463  1.16      phil     MARK_INT_SAT(CODA_STATFS_STATS);
    464  1.16      phil     return(error);
    465   1.1       rvb }
    466   1.1       rvb 
    467   1.1       rvb /*
    468   1.1       rvb  * Flush any pending I/O.
    469   1.1       rvb  */
    470   1.1       rvb int
    471  1.52  christos coda_sync(struct mount *vfsp, int waitfor,
    472  1.60     pooka     kauth_cred_t cred)
    473   1.1       rvb {
    474   1.1       rvb     ENTRY;
    475   1.3       rvb     MARK_ENTRY(CODA_SYNC_STATS);
    476   1.3       rvb     MARK_INT_SAT(CODA_SYNC_STATS);
    477   1.1       rvb     return(0);
    478   1.1       rvb }
    479   1.1       rvb 
    480   1.1       rvb int
    481  1.87        ad coda_vget(struct mount *vfsp, ino_t ino, int lktype,
    482  1.52  christos     struct vnode **vpp)
    483   1.1       rvb {
    484   1.1       rvb     ENTRY;
    485   1.1       rvb     return (EOPNOTSUPP);
    486   1.1       rvb }
    487   1.1       rvb 
    488  1.84   hannken int
    489  1.84   hannken coda_loadvnode(struct mount *mp, struct vnode *vp,
    490  1.84   hannken     const void *key, size_t key_len, const void **new_key)
    491  1.84   hannken {
    492  1.84   hannken 	CodaFid fid;
    493  1.84   hannken 	struct cnode *cp;
    494  1.84   hannken 	extern int (**coda_vnodeop_p)(void *);
    495  1.84   hannken 
    496  1.84   hannken 	KASSERT(key_len == sizeof(CodaFid));
    497  1.84   hannken 	memcpy(&fid, key, key_len);
    498  1.84   hannken 
    499  1.84   hannken 	cp = kmem_zalloc(sizeof(*cp), KM_SLEEP);
    500  1.84   hannken 	mutex_init(&cp->c_lock, MUTEX_DEFAULT, IPL_NONE);
    501  1.84   hannken 	cp->c_fid = fid;
    502  1.84   hannken 	cp->c_vnode = vp;
    503  1.84   hannken 	vp->v_op = coda_vnodeop_p;
    504  1.84   hannken 	vp->v_tag = VT_CODA;
    505  1.84   hannken 	vp->v_type = VNON;
    506  1.84   hannken 	vp->v_data = cp;
    507  1.84   hannken 
    508  1.84   hannken 	*new_key = &cp->c_fid;
    509  1.84   hannken 
    510  1.84   hannken 	return 0;
    511  1.84   hannken }
    512  1.84   hannken 
    513  1.42     perry /*
    514   1.1       rvb  * fhtovp is now what vget used to be in 4.3-derived systems.  For
    515   1.1       rvb  * some silly reason, vget is now keyed by a 32 bit ino_t, rather than
    516  1.42     perry  * a type-specific fid.
    517   1.1       rvb  */
    518   1.1       rvb int
    519  1.52  christos coda_fhtovp(struct mount *vfsp, struct fid *fhp, struct mbuf *nam,
    520  1.52  christos     struct vnode **vpp, int *exflagsp,
    521  1.87        ad     kauth_cred_t *creadanonp, int lktype)
    522   1.1       rvb {
    523   1.1       rvb     struct cfid *cfid = (struct cfid *)fhp;
    524   1.1       rvb     struct cnode *cp = 0;
    525   1.1       rvb     int error;
    526  1.49        ad     struct lwp *l = curlwp; /* XXX -mach */
    527  1.29  drochner     CodaFid VFid;
    528   1.1       rvb     int vtype;
    529   1.1       rvb 
    530   1.1       rvb     ENTRY;
    531  1.42     perry 
    532   1.3       rvb     MARK_ENTRY(CODA_VGET_STATS);
    533   1.1       rvb     /* Check for vget of control object. */
    534   1.1       rvb     if (IS_CTL_FID(&cfid->cfid_fid)) {
    535   1.3       rvb 	*vpp = coda_ctlvp;
    536   1.3       rvb 	vref(coda_ctlvp);
    537   1.3       rvb 	MARK_INT_SAT(CODA_VGET_STATS);
    538   1.1       rvb 	return(0);
    539   1.1       rvb     }
    540  1.42     perry 
    541  1.49        ad     error = venus_fhtovp(vftomi(vfsp), &cfid->cfid_fid, l->l_cred, l->l_proc, &VFid, &vtype);
    542  1.42     perry 
    543   1.1       rvb     if (error) {
    544   1.3       rvb 	CODADEBUG(CODA_VGET, myprintf(("vget error %d\n",error));)
    545   1.1       rvb 	    *vpp = (struct vnode *)0;
    546   1.1       rvb     } else {
    547  1.42     perry 	CODADEBUG(CODA_VGET,
    548  1.29  drochner 		 myprintf(("vget: %s type %d result %d\n",
    549  1.29  drochner 			coda_f2s(&VFid), vtype, error)); )
    550  1.42     perry 
    551   1.3       rvb 	cp = make_coda_node(&VFid, vfsp, vtype);
    552   1.1       rvb 	*vpp = CTOV(cp);
    553   1.1       rvb     }
    554   1.1       rvb     return(error);
    555   1.1       rvb }
    556   1.1       rvb 
    557   1.1       rvb int
    558  1.52  christos coda_vptofh(struct vnode *vnp, struct fid *fidp)
    559   1.1       rvb {
    560   1.1       rvb     ENTRY;
    561   1.1       rvb     return (EOPNOTSUPP);
    562   1.1       rvb }
    563   1.1       rvb 
    564   1.1       rvb void
    565   1.3       rvb coda_init(void)
    566   1.1       rvb {
    567   1.1       rvb     ENTRY;
    568   1.1       rvb }
    569   1.9  jdolecek 
    570   1.9  jdolecek void
    571   1.9  jdolecek coda_done(void)
    572   1.9  jdolecek {
    573   1.9  jdolecek     ENTRY;
    574   1.9  jdolecek }
    575   1.1       rvb 
    576  1.31    atatat SYSCTL_SETUP(sysctl_vfs_coda_setup, "sysctl vfs.coda subtree setup")
    577   1.1       rvb {
    578  1.79     pooka 
    579  1.33    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    580  1.33    atatat 		       CTLFLAG_PERMANENT,
    581  1.37    atatat 		       CTLTYPE_NODE, "coda",
    582  1.37    atatat 		       SYSCTL_DESCR("code vfs options"),
    583  1.31    atatat 		       NULL, 0, NULL, 0,
    584  1.31    atatat 		       CTL_VFS, 18, CTL_EOL);
    585  1.31    atatat 	/*
    586  1.31    atatat 	 * XXX the "18" above could be dynamic, thereby eliminating
    587  1.31    atatat 	 * one more instance of the "number to vfs" mapping problem,
    588  1.31    atatat 	 * but "18" is the order as taken from sys/mount.h
    589  1.31    atatat 	 */
    590   1.1       rvb 
    591   1.1       rvb /*
    592  1.33    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    593  1.33    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    594  1.37    atatat 		       CTLTYPE_INT, "clusterread",
    595  1.37    atatat 		       SYSCTL_DESCR( anyone? ),
    596  1.31    atatat 		       NULL, 0, &doclusterread, 0,
    597  1.31    atatat 		       CTL_VFS, 18, FFS_CLUSTERREAD, CTL_EOL);
    598  1.31    atatat */
    599   1.1       rvb }
    600   1.1       rvb 
    601   1.1       rvb /*
    602   1.1       rvb  * To allow for greater ease of use, some vnodes may be orphaned when
    603   1.1       rvb  * Venus dies.  Certain operations should still be allowed to go
    604  1.32    atatat  * through, but without propagating orphan-ness.  So this function will
    605  1.32    atatat  * get a new vnode for the file from the current run of Venus.
    606  1.32    atatat  */
    607  1.42     perry 
    608   1.1       rvb int
    609  1.45   xtraeme getNewVnode(struct vnode **vpp)
    610   1.1       rvb {
    611   1.1       rvb     struct cfid cfid;
    612   1.3       rvb     struct coda_mntinfo *mi = vftomi((*vpp)->v_mount);
    613  1.42     perry 
    614   1.1       rvb     ENTRY;
    615   1.1       rvb 
    616  1.29  drochner     cfid.cfid_len = (short)sizeof(CodaFid);
    617   1.1       rvb     cfid.cfid_fid = VTOC(*vpp)->c_fid;	/* Structure assignment. */
    618   1.1       rvb     /* XXX ? */
    619   1.1       rvb 
    620   1.1       rvb     /* We're guessing that if set, the 1st element on the list is a
    621   1.1       rvb      * valid vnode to use. If not, return ENODEV as venus is dead.
    622   1.1       rvb      */
    623   1.1       rvb     if (mi->mi_vfsp == NULL)
    624   1.1       rvb 	return ENODEV;
    625  1.42     perry 
    626   1.3       rvb     return coda_fhtovp(mi->mi_vfsp, (struct fid*)&cfid, NULL, vpp,
    627  1.87        ad 		      NULL, NULL, LK_EXCLUSIVE);
    628   1.1       rvb }
    629   1.1       rvb 
    630  1.86   hannken /* Get the mount structure corresponding to a given device.
    631  1.86   hannken  * Return NULL if no device is found or the device is not mounted.
    632  1.42     perry  */
    633  1.45   xtraeme struct mount *devtomp(dev_t dev)
    634   1.1       rvb {
    635  1.76  christos     struct mount *mp;
    636  1.86   hannken     struct vnode *vp;
    637  1.42     perry 
    638  1.90  riastrad     if (spec_node_lookup_by_dev(VBLK, dev, VDEAD_NOWAIT, &vp) == 0) {
    639  1.86   hannken 	mp = spec_node_getmountedfs(vp);
    640  1.86   hannken 	vrele(vp);
    641  1.86   hannken     } else {
    642  1.86   hannken 	mp = NULL;
    643   1.1       rvb     }
    644  1.86   hannken 
    645  1.77  christos     return mp;
    646   1.1       rvb }
    647