1139745Simp/*-
238759Srvb *             Coda: an Experimental Distributed File System
338759Srvb *                              Release 3.1
4176139Srwatson *
538759Srvb *           Copyright (c) 1987-1998 Carnegie Mellon University
638759Srvb *                          All Rights Reserved
7176139Srwatson *
838759Srvb * Permission  to  use, copy, modify and distribute this software and its
938759Srvb * documentation is hereby granted,  provided  that  both  the  copyright
1038759Srvb * notice  and  this  permission  notice  appear  in  all  copies  of the
1138759Srvb * software, derivative works or  modified  versions,  and  any  portions
1238759Srvb * thereof, and that both notices appear in supporting documentation, and
1338759Srvb * that credit is given to Carnegie Mellon University  in  all  documents
1438759Srvb * and publicity pertaining to direct or indirect use of this code or its
1538759Srvb * derivatives.
16176139Srwatson *
1738759Srvb * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS  KNOWN  TO  HAVE  BUGS,
1838759Srvb * SOME  OF  WHICH MAY HAVE SERIOUS CONSEQUENCES.  CARNEGIE MELLON ALLOWS
1938759Srvb * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.   CARNEGIE  MELLON
2038759Srvb * DISCLAIMS  ANY  LIABILITY  OF  ANY  KIND  FOR  ANY  DAMAGES WHATSOEVER
2138759Srvb * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE  OR  OF
2238759Srvb * ANY DERIVATIVE WORK.
23176139Srwatson *
2438759Srvb * Carnegie  Mellon  encourages  users  of  this  software  to return any
2538759Srvb * improvements or extensions that  they  make,  and  to  grant  Carnegie
2638759Srvb * Mellon the rights to redistribute these changes without encumbrance.
27176139Srwatson *
2839126Srvb * 	@(#) src/sys/coda/coda_fbsd.cr,v 1.1.1.1 1998/08/29 21:14:52 rvb Exp $
2938759Srvb */
3038625Srvb
31116173Sobrien#include <sys/cdefs.h>
32116173Sobrien__FBSDID("$FreeBSD$");
33116173Sobrien
3438625Srvb#include <sys/param.h>
3538625Srvb#include <sys/systm.h>
3676166Smarkm#include <sys/conf.h>
3776166Smarkm#include <sys/fcntl.h>
3838625Srvb#include <sys/kernel.h>
3976166Smarkm#include <sys/lock.h>
4038759Srvb#include <sys/malloc.h>
41129880Sphk#include <sys/module.h>
4238625Srvb#include <sys/ucred.h>
4338759Srvb#include <sys/vnode.h>
4438759Srvb
4538625Srvb#include <vm/vm.h>
4638625Srvb#include <vm/vnode_pager.h>
4738625Srvb
48171416Srwatson#include <fs/coda/coda.h>
49171416Srwatson#include <fs/coda/cnode.h>
50171416Srwatson#include <fs/coda/coda_vnops.h>
51171416Srwatson#include <fs/coda/coda_psdev.h>
5238759Srvb
5347625Sphkstatic struct cdevsw codadevsw = {
54126080Sphk	.d_version =	D_VERSION,
55126080Sphk	.d_flags =	D_NEEDGIANT,
56176131Srwatson	.d_open =	vc_open,
57176131Srwatson	.d_close =	vc_close,
58176131Srwatson	.d_read =	vc_read,
59176131Srwatson	.d_write =	vc_write,
60176131Srwatson	.d_ioctl =	vc_ioctl,
61176131Srwatson	.d_poll =	vc_poll,
62176131Srwatson	.d_name =	"coda",
6338759Srvb};
6438625Srvb
65134585Sbrooksstatic eventhandler_tag clonetag;
66134585Sbrooks
67134585Sbrooksstatic LIST_HEAD(, coda_mntinfo) coda_mnttbl;
68134585Sbrooks
69176139Srwatson/*
70176139Srwatson * For DEVFS, using bpf & tun drivers as examples.
71176139Srwatson *
72176139Srwatson * XXX: Why use a cloned interface, aren't we really just interested in
73176139Srwatson * having a single /dev/cfs0?  It's not clear the coda module knows what to
74176139Srwatson * do with more than one.
75176139Srwatson */
76148868Srwatsonstatic void coda_fbsd_clone(void *arg, struct ucred *cred, char *name,
77148868Srwatson    int namelen, struct cdev **dev);
78134585Sbrooks
7940857Speterstatic int
8043311Sdilloncodadev_modevent(module_t mod, int type, void *data)
8138625Srvb{
82176139Srwatson	struct coda_mntinfo *mnt;
8338625Srvb
8440857Speter	switch (type) {
8540857Speter	case MOD_LOAD:
86134585Sbrooks		LIST_INIT(&coda_mnttbl);
87134585Sbrooks		clonetag = EVENTHANDLER_REGISTER(dev_clone, coda_fbsd_clone,
88134585Sbrooks		    0, 1000);
8940857Speter		break;
90176139Srwatson
9140857Speter	case MOD_UNLOAD:
92176139Srwatson		/*
93176139Srwatson		 * XXXRW: At the very least, a busy check should occur here
94176139Srwatson		 * to prevent untimely unload.  Much more serious collection
95176139Srwatson		 * of allocated memory needs to take place; right now we leak
96176139Srwatson		 * like a sieve.
97176139Srwatson		 */
98134585Sbrooks		EVENTHANDLER_DEREGISTER(dev_clone, clonetag);
99134585Sbrooks		while ((mnt = LIST_FIRST(&coda_mnttbl)) != NULL) {
100134585Sbrooks			LIST_REMOVE(mnt, mi_list);
101134585Sbrooks			destroy_dev(mnt->dev);
102134585Sbrooks			free(mnt, M_CODA);
103134585Sbrooks		}
104134585Sbrooks		break;
105134585Sbrooks
10640857Speter	default:
107132199Sphk		return (EOPNOTSUPP);
10839650Srvb	}
109176139Srwatson	return (0);
11038625Srvb}
111176139Srwatson
11240857Speterstatic moduledata_t codadev_mod = {
11340857Speter	"codadev",
11440857Speter	codadev_modevent,
11540857Speter	NULL
11640857Speter};
117134585SbrooksDECLARE_MODULE(codadev, codadev_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
11838625Srvb
119176139Srwatsonstatic void
120176139Srwatsoncoda_fbsd_clone(void *arg, struct ucred *cred, char *name, int namelen,
121176139Srwatson    struct cdev **dev)
12277784Sshafeeq{
123176139Srwatson	struct coda_mntinfo *mnt;
124176139Srwatson	int u;
12577784Sshafeeq
126176139Srwatson	if (*dev != NULL)
127176139Srwatson		return;
128176139Srwatson	if (dev_stdclone(name, NULL, "cfs", &u) != 1)
129176139Srwatson		return;
130183381Sed	*dev = make_dev(&codadevsw, u, UID_ROOT, GID_WHEEL, 0600,
131176139Srwatson	    "cfs%d", u);
132176139Srwatson	dev_ref(*dev);
133176139Srwatson	mnt = malloc(sizeof(struct coda_mntinfo), M_CODA, M_WAITOK|M_ZERO);
134176139Srwatson	LIST_INSERT_HEAD(&coda_mnttbl, mnt, mi_list);
135176139Srwatson	mnt->dev = *dev;
13677784Sshafeeq}
13777784Sshafeeq
138134585Sbrooksstruct coda_mntinfo *
139134585Sbrooksdev2coda_mntinfo(struct cdev *dev)
14077784Sshafeeq{
141176139Srwatson	struct coda_mntinfo *mnt;
14277784Sshafeeq
143134585Sbrooks	LIST_FOREACH(mnt, &coda_mnttbl, mi_list) {
144134585Sbrooks		if (mnt->dev == dev)
145176139Srwatson			return (mnt);
146134585Sbrooks	}
147176139Srwatson	return (NULL);
14877784Sshafeeq}
149