xref: /OK3568_Linux_fs/kernel/fs/coda/psdev.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *      	An implementation of a loadable kernel mode driver providing
4*4882a593Smuzhiyun  *		multiple kernel/user space bidirectional communications links.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * 		Author: 	Alan Cox <alan@lxorguk.ukuu.org.uk>
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  *              Adapted to become the Linux 2.0 Coda pseudo device
9*4882a593Smuzhiyun  *              Peter  Braam  <braam@maths.ox.ac.uk>
10*4882a593Smuzhiyun  *              Michael Callahan <mjc@emmy.smith.edu>
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  *              Changes for Linux 2.1
13*4882a593Smuzhiyun  *              Copyright (c) 1997 Carnegie-Mellon University
14*4882a593Smuzhiyun  */
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include <linux/module.h>
17*4882a593Smuzhiyun #include <linux/errno.h>
18*4882a593Smuzhiyun #include <linux/kernel.h>
19*4882a593Smuzhiyun #include <linux/major.h>
20*4882a593Smuzhiyun #include <linux/time.h>
21*4882a593Smuzhiyun #include <linux/sched/signal.h>
22*4882a593Smuzhiyun #include <linux/slab.h>
23*4882a593Smuzhiyun #include <linux/ioport.h>
24*4882a593Smuzhiyun #include <linux/fcntl.h>
25*4882a593Smuzhiyun #include <linux/delay.h>
26*4882a593Smuzhiyun #include <linux/skbuff.h>
27*4882a593Smuzhiyun #include <linux/proc_fs.h>
28*4882a593Smuzhiyun #include <linux/vmalloc.h>
29*4882a593Smuzhiyun #include <linux/fs.h>
30*4882a593Smuzhiyun #include <linux/file.h>
31*4882a593Smuzhiyun #include <linux/poll.h>
32*4882a593Smuzhiyun #include <linux/init.h>
33*4882a593Smuzhiyun #include <linux/list.h>
34*4882a593Smuzhiyun #include <linux/mutex.h>
35*4882a593Smuzhiyun #include <linux/device.h>
36*4882a593Smuzhiyun #include <linux/pid_namespace.h>
37*4882a593Smuzhiyun #include <asm/io.h>
38*4882a593Smuzhiyun #include <linux/uaccess.h>
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun #include <linux/coda.h>
41*4882a593Smuzhiyun #include "coda_psdev.h"
42*4882a593Smuzhiyun #include "coda_linux.h"
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun #include "coda_int.h"
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun /* statistics */
47*4882a593Smuzhiyun int           coda_hard;         /* allows signals during upcalls */
48*4882a593Smuzhiyun unsigned long coda_timeout = 30; /* .. secs, then signals will dequeue */
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun struct venus_comm coda_comms[MAX_CODADEVS];
52*4882a593Smuzhiyun static struct class *coda_psdev_class;
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun /*
55*4882a593Smuzhiyun  * Device operations
56*4882a593Smuzhiyun  */
57*4882a593Smuzhiyun 
coda_psdev_poll(struct file * file,poll_table * wait)58*4882a593Smuzhiyun static __poll_t coda_psdev_poll(struct file *file, poll_table * wait)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun         struct venus_comm *vcp = (struct venus_comm *) file->private_data;
61*4882a593Smuzhiyun 	__poll_t mask = EPOLLOUT | EPOLLWRNORM;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	poll_wait(file, &vcp->vc_waitq, wait);
64*4882a593Smuzhiyun 	mutex_lock(&vcp->vc_mutex);
65*4882a593Smuzhiyun 	if (!list_empty(&vcp->vc_pending))
66*4882a593Smuzhiyun                 mask |= EPOLLIN | EPOLLRDNORM;
67*4882a593Smuzhiyun 	mutex_unlock(&vcp->vc_mutex);
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	return mask;
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun 
coda_psdev_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)72*4882a593Smuzhiyun static long coda_psdev_ioctl(struct file * filp, unsigned int cmd, unsigned long arg)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun 	unsigned int data;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	switch(cmd) {
77*4882a593Smuzhiyun 	case CIOC_KERNEL_VERSION:
78*4882a593Smuzhiyun 		data = CODA_KERNEL_VERSION;
79*4882a593Smuzhiyun 		return put_user(data, (int __user *) arg);
80*4882a593Smuzhiyun 	default:
81*4882a593Smuzhiyun 		return -ENOTTY;
82*4882a593Smuzhiyun 	}
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	return 0;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun /*
88*4882a593Smuzhiyun  *	Receive a message written by Venus to the psdev
89*4882a593Smuzhiyun  */
90*4882a593Smuzhiyun 
coda_psdev_write(struct file * file,const char __user * buf,size_t nbytes,loff_t * off)91*4882a593Smuzhiyun static ssize_t coda_psdev_write(struct file *file, const char __user *buf,
92*4882a593Smuzhiyun 				size_t nbytes, loff_t *off)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun         struct venus_comm *vcp = (struct venus_comm *) file->private_data;
95*4882a593Smuzhiyun         struct upc_req *req = NULL;
96*4882a593Smuzhiyun         struct upc_req *tmp;
97*4882a593Smuzhiyun 	struct list_head *lh;
98*4882a593Smuzhiyun 	struct coda_in_hdr hdr;
99*4882a593Smuzhiyun 	ssize_t retval = 0, count = 0;
100*4882a593Smuzhiyun 	int error;
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	/* make sure there is enough to copy out the (opcode, unique) values */
103*4882a593Smuzhiyun 	if (nbytes < (2 * sizeof(u_int32_t)))
104*4882a593Smuzhiyun 		return -EINVAL;
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun         /* Peek at the opcode, uniquefier */
107*4882a593Smuzhiyun 	if (copy_from_user(&hdr, buf, 2 * sizeof(u_int32_t)))
108*4882a593Smuzhiyun 	        return -EFAULT;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun         if (DOWNCALL(hdr.opcode)) {
111*4882a593Smuzhiyun 		union outputArgs *dcbuf;
112*4882a593Smuzhiyun 		int size = sizeof(*dcbuf);
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 		if  ( nbytes < sizeof(struct coda_out_hdr) ) {
115*4882a593Smuzhiyun 			pr_warn("coda_downcall opc %d uniq %d, not enough!\n",
116*4882a593Smuzhiyun 				hdr.opcode, hdr.unique);
117*4882a593Smuzhiyun 			count = nbytes;
118*4882a593Smuzhiyun 			goto out;
119*4882a593Smuzhiyun 		}
120*4882a593Smuzhiyun 		if ( nbytes > size ) {
121*4882a593Smuzhiyun 			pr_warn("downcall opc %d, uniq %d, too much!",
122*4882a593Smuzhiyun 				hdr.opcode, hdr.unique);
123*4882a593Smuzhiyun 		        nbytes = size;
124*4882a593Smuzhiyun 		}
125*4882a593Smuzhiyun 		dcbuf = kvmalloc(nbytes, GFP_KERNEL);
126*4882a593Smuzhiyun 		if (!dcbuf) {
127*4882a593Smuzhiyun 			retval = -ENOMEM;
128*4882a593Smuzhiyun 			goto out;
129*4882a593Smuzhiyun 		}
130*4882a593Smuzhiyun 		if (copy_from_user(dcbuf, buf, nbytes)) {
131*4882a593Smuzhiyun 			kvfree(dcbuf);
132*4882a593Smuzhiyun 			retval = -EFAULT;
133*4882a593Smuzhiyun 			goto out;
134*4882a593Smuzhiyun 		}
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 		/* what downcall errors does Venus handle ? */
137*4882a593Smuzhiyun 		error = coda_downcall(vcp, hdr.opcode, dcbuf, nbytes);
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 		kvfree(dcbuf);
140*4882a593Smuzhiyun 		if (error) {
141*4882a593Smuzhiyun 			pr_warn("%s: coda_downcall error: %d\n",
142*4882a593Smuzhiyun 				__func__, error);
143*4882a593Smuzhiyun 			retval = error;
144*4882a593Smuzhiyun 			goto out;
145*4882a593Smuzhiyun 		}
146*4882a593Smuzhiyun 		count = nbytes;
147*4882a593Smuzhiyun 		goto out;
148*4882a593Smuzhiyun 	}
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	/* Look for the message on the processing queue. */
151*4882a593Smuzhiyun 	mutex_lock(&vcp->vc_mutex);
152*4882a593Smuzhiyun 	list_for_each(lh, &vcp->vc_processing) {
153*4882a593Smuzhiyun 		tmp = list_entry(lh, struct upc_req , uc_chain);
154*4882a593Smuzhiyun 		if (tmp->uc_unique == hdr.unique) {
155*4882a593Smuzhiyun 			req = tmp;
156*4882a593Smuzhiyun 			list_del(&req->uc_chain);
157*4882a593Smuzhiyun 			break;
158*4882a593Smuzhiyun 		}
159*4882a593Smuzhiyun 	}
160*4882a593Smuzhiyun 	mutex_unlock(&vcp->vc_mutex);
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	if (!req) {
163*4882a593Smuzhiyun 		pr_warn("%s: msg (%d, %d) not found\n",
164*4882a593Smuzhiyun 			__func__, hdr.opcode, hdr.unique);
165*4882a593Smuzhiyun 		retval = -ESRCH;
166*4882a593Smuzhiyun 		goto out;
167*4882a593Smuzhiyun 	}
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun         /* move data into response buffer. */
170*4882a593Smuzhiyun 	if (req->uc_outSize < nbytes) {
171*4882a593Smuzhiyun 		pr_warn("%s: too much cnt: %d, cnt: %ld, opc: %d, uniq: %d.\n",
172*4882a593Smuzhiyun 			__func__, req->uc_outSize, (long)nbytes,
173*4882a593Smuzhiyun 			hdr.opcode, hdr.unique);
174*4882a593Smuzhiyun 		nbytes = req->uc_outSize; /* don't have more space! */
175*4882a593Smuzhiyun 	}
176*4882a593Smuzhiyun         if (copy_from_user(req->uc_data, buf, nbytes)) {
177*4882a593Smuzhiyun 		req->uc_flags |= CODA_REQ_ABORT;
178*4882a593Smuzhiyun 		wake_up(&req->uc_sleep);
179*4882a593Smuzhiyun 		retval = -EFAULT;
180*4882a593Smuzhiyun 		goto out;
181*4882a593Smuzhiyun 	}
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	/* adjust outsize. is this useful ?? */
184*4882a593Smuzhiyun 	req->uc_outSize = nbytes;
185*4882a593Smuzhiyun 	req->uc_flags |= CODA_REQ_WRITE;
186*4882a593Smuzhiyun 	count = nbytes;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	/* Convert filedescriptor into a file handle */
189*4882a593Smuzhiyun 	if (req->uc_opcode == CODA_OPEN_BY_FD) {
190*4882a593Smuzhiyun 		struct coda_open_by_fd_out *outp =
191*4882a593Smuzhiyun 			(struct coda_open_by_fd_out *)req->uc_data;
192*4882a593Smuzhiyun 		if (!outp->oh.result) {
193*4882a593Smuzhiyun 			outp->fh = fget(outp->fd);
194*4882a593Smuzhiyun 			if (!outp->fh)
195*4882a593Smuzhiyun 				return -EBADF;
196*4882a593Smuzhiyun 		}
197*4882a593Smuzhiyun 	}
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun         wake_up(&req->uc_sleep);
200*4882a593Smuzhiyun out:
201*4882a593Smuzhiyun         return(count ? count : retval);
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun /*
205*4882a593Smuzhiyun  *	Read a message from the kernel to Venus
206*4882a593Smuzhiyun  */
207*4882a593Smuzhiyun 
coda_psdev_read(struct file * file,char __user * buf,size_t nbytes,loff_t * off)208*4882a593Smuzhiyun static ssize_t coda_psdev_read(struct file * file, char __user * buf,
209*4882a593Smuzhiyun 			       size_t nbytes, loff_t *off)
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun 	DECLARE_WAITQUEUE(wait, current);
212*4882a593Smuzhiyun         struct venus_comm *vcp = (struct venus_comm *) file->private_data;
213*4882a593Smuzhiyun         struct upc_req *req;
214*4882a593Smuzhiyun 	ssize_t retval = 0, count = 0;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	if (nbytes == 0)
217*4882a593Smuzhiyun 		return 0;
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	mutex_lock(&vcp->vc_mutex);
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	add_wait_queue(&vcp->vc_waitq, &wait);
222*4882a593Smuzhiyun 	set_current_state(TASK_INTERRUPTIBLE);
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	while (list_empty(&vcp->vc_pending)) {
225*4882a593Smuzhiyun 		if (file->f_flags & O_NONBLOCK) {
226*4882a593Smuzhiyun 			retval = -EAGAIN;
227*4882a593Smuzhiyun 			break;
228*4882a593Smuzhiyun 		}
229*4882a593Smuzhiyun 		if (signal_pending(current)) {
230*4882a593Smuzhiyun 			retval = -ERESTARTSYS;
231*4882a593Smuzhiyun 			break;
232*4882a593Smuzhiyun 		}
233*4882a593Smuzhiyun 		mutex_unlock(&vcp->vc_mutex);
234*4882a593Smuzhiyun 		schedule();
235*4882a593Smuzhiyun 		mutex_lock(&vcp->vc_mutex);
236*4882a593Smuzhiyun 	}
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	set_current_state(TASK_RUNNING);
239*4882a593Smuzhiyun 	remove_wait_queue(&vcp->vc_waitq, &wait);
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	if (retval)
242*4882a593Smuzhiyun 		goto out;
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	req = list_entry(vcp->vc_pending.next, struct upc_req,uc_chain);
245*4882a593Smuzhiyun 	list_del(&req->uc_chain);
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	/* Move the input args into userspace */
248*4882a593Smuzhiyun 	count = req->uc_inSize;
249*4882a593Smuzhiyun 	if (nbytes < req->uc_inSize) {
250*4882a593Smuzhiyun 		pr_warn("%s: Venus read %ld bytes of %d in message\n",
251*4882a593Smuzhiyun 			__func__, (long)nbytes, req->uc_inSize);
252*4882a593Smuzhiyun 		count = nbytes;
253*4882a593Smuzhiyun         }
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	if (copy_to_user(buf, req->uc_data, count))
256*4882a593Smuzhiyun 	        retval = -EFAULT;
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	/* If request was not a signal, enqueue and don't free */
259*4882a593Smuzhiyun 	if (!(req->uc_flags & CODA_REQ_ASYNC)) {
260*4882a593Smuzhiyun 		req->uc_flags |= CODA_REQ_READ;
261*4882a593Smuzhiyun 		list_add_tail(&(req->uc_chain), &vcp->vc_processing);
262*4882a593Smuzhiyun 		goto out;
263*4882a593Smuzhiyun 	}
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	kvfree(req->uc_data);
266*4882a593Smuzhiyun 	kfree(req);
267*4882a593Smuzhiyun out:
268*4882a593Smuzhiyun 	mutex_unlock(&vcp->vc_mutex);
269*4882a593Smuzhiyun 	return (count ? count : retval);
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun 
coda_psdev_open(struct inode * inode,struct file * file)272*4882a593Smuzhiyun static int coda_psdev_open(struct inode * inode, struct file * file)
273*4882a593Smuzhiyun {
274*4882a593Smuzhiyun 	struct venus_comm *vcp;
275*4882a593Smuzhiyun 	int idx, err;
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 	if (task_active_pid_ns(current) != &init_pid_ns)
278*4882a593Smuzhiyun 		return -EINVAL;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	if (current_user_ns() != &init_user_ns)
281*4882a593Smuzhiyun 		return -EINVAL;
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	idx = iminor(inode);
284*4882a593Smuzhiyun 	if (idx < 0 || idx >= MAX_CODADEVS)
285*4882a593Smuzhiyun 		return -ENODEV;
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	err = -EBUSY;
288*4882a593Smuzhiyun 	vcp = &coda_comms[idx];
289*4882a593Smuzhiyun 	mutex_lock(&vcp->vc_mutex);
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 	if (!vcp->vc_inuse) {
292*4882a593Smuzhiyun 		vcp->vc_inuse++;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 		INIT_LIST_HEAD(&vcp->vc_pending);
295*4882a593Smuzhiyun 		INIT_LIST_HEAD(&vcp->vc_processing);
296*4882a593Smuzhiyun 		init_waitqueue_head(&vcp->vc_waitq);
297*4882a593Smuzhiyun 		vcp->vc_sb = NULL;
298*4882a593Smuzhiyun 		vcp->vc_seq = 0;
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 		file->private_data = vcp;
301*4882a593Smuzhiyun 		err = 0;
302*4882a593Smuzhiyun 	}
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	mutex_unlock(&vcp->vc_mutex);
305*4882a593Smuzhiyun 	return err;
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 
coda_psdev_release(struct inode * inode,struct file * file)309*4882a593Smuzhiyun static int coda_psdev_release(struct inode * inode, struct file * file)
310*4882a593Smuzhiyun {
311*4882a593Smuzhiyun 	struct venus_comm *vcp = (struct venus_comm *) file->private_data;
312*4882a593Smuzhiyun 	struct upc_req *req, *tmp;
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	if (!vcp || !vcp->vc_inuse ) {
315*4882a593Smuzhiyun 		pr_warn("%s: Not open.\n", __func__);
316*4882a593Smuzhiyun 		return -1;
317*4882a593Smuzhiyun 	}
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	mutex_lock(&vcp->vc_mutex);
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	/* Wakeup clients so they can return. */
322*4882a593Smuzhiyun 	list_for_each_entry_safe(req, tmp, &vcp->vc_pending, uc_chain) {
323*4882a593Smuzhiyun 		list_del(&req->uc_chain);
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 		/* Async requests need to be freed here */
326*4882a593Smuzhiyun 		if (req->uc_flags & CODA_REQ_ASYNC) {
327*4882a593Smuzhiyun 			kvfree(req->uc_data);
328*4882a593Smuzhiyun 			kfree(req);
329*4882a593Smuzhiyun 			continue;
330*4882a593Smuzhiyun 		}
331*4882a593Smuzhiyun 		req->uc_flags |= CODA_REQ_ABORT;
332*4882a593Smuzhiyun 		wake_up(&req->uc_sleep);
333*4882a593Smuzhiyun 	}
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun 	list_for_each_entry_safe(req, tmp, &vcp->vc_processing, uc_chain) {
336*4882a593Smuzhiyun 		list_del(&req->uc_chain);
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 		req->uc_flags |= CODA_REQ_ABORT;
339*4882a593Smuzhiyun 		wake_up(&req->uc_sleep);
340*4882a593Smuzhiyun 	}
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	file->private_data = NULL;
343*4882a593Smuzhiyun 	vcp->vc_inuse--;
344*4882a593Smuzhiyun 	mutex_unlock(&vcp->vc_mutex);
345*4882a593Smuzhiyun 	return 0;
346*4882a593Smuzhiyun }
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun static const struct file_operations coda_psdev_fops = {
350*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
351*4882a593Smuzhiyun 	.read		= coda_psdev_read,
352*4882a593Smuzhiyun 	.write		= coda_psdev_write,
353*4882a593Smuzhiyun 	.poll		= coda_psdev_poll,
354*4882a593Smuzhiyun 	.unlocked_ioctl	= coda_psdev_ioctl,
355*4882a593Smuzhiyun 	.open		= coda_psdev_open,
356*4882a593Smuzhiyun 	.release	= coda_psdev_release,
357*4882a593Smuzhiyun 	.llseek		= noop_llseek,
358*4882a593Smuzhiyun };
359*4882a593Smuzhiyun 
init_coda_psdev(void)360*4882a593Smuzhiyun static int __init init_coda_psdev(void)
361*4882a593Smuzhiyun {
362*4882a593Smuzhiyun 	int i, err = 0;
363*4882a593Smuzhiyun 	if (register_chrdev(CODA_PSDEV_MAJOR, "coda", &coda_psdev_fops)) {
364*4882a593Smuzhiyun 		pr_err("%s: unable to get major %d\n",
365*4882a593Smuzhiyun 		       __func__, CODA_PSDEV_MAJOR);
366*4882a593Smuzhiyun 		return -EIO;
367*4882a593Smuzhiyun 	}
368*4882a593Smuzhiyun 	coda_psdev_class = class_create(THIS_MODULE, "coda");
369*4882a593Smuzhiyun 	if (IS_ERR(coda_psdev_class)) {
370*4882a593Smuzhiyun 		err = PTR_ERR(coda_psdev_class);
371*4882a593Smuzhiyun 		goto out_chrdev;
372*4882a593Smuzhiyun 	}
373*4882a593Smuzhiyun 	for (i = 0; i < MAX_CODADEVS; i++) {
374*4882a593Smuzhiyun 		mutex_init(&(&coda_comms[i])->vc_mutex);
375*4882a593Smuzhiyun 		device_create(coda_psdev_class, NULL,
376*4882a593Smuzhiyun 			      MKDEV(CODA_PSDEV_MAJOR, i), NULL, "cfs%d", i);
377*4882a593Smuzhiyun 	}
378*4882a593Smuzhiyun 	coda_sysctl_init();
379*4882a593Smuzhiyun 	goto out;
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun out_chrdev:
382*4882a593Smuzhiyun 	unregister_chrdev(CODA_PSDEV_MAJOR, "coda");
383*4882a593Smuzhiyun out:
384*4882a593Smuzhiyun 	return err;
385*4882a593Smuzhiyun }
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun MODULE_AUTHOR("Jan Harkes, Peter J. Braam");
388*4882a593Smuzhiyun MODULE_DESCRIPTION("Coda Distributed File System VFS interface");
389*4882a593Smuzhiyun MODULE_ALIAS_CHARDEV_MAJOR(CODA_PSDEV_MAJOR);
390*4882a593Smuzhiyun MODULE_LICENSE("GPL");
391*4882a593Smuzhiyun MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
392*4882a593Smuzhiyun MODULE_VERSION("7.0");
393*4882a593Smuzhiyun 
init_coda(void)394*4882a593Smuzhiyun static int __init init_coda(void)
395*4882a593Smuzhiyun {
396*4882a593Smuzhiyun 	int status;
397*4882a593Smuzhiyun 	int i;
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 	status = coda_init_inodecache();
400*4882a593Smuzhiyun 	if (status)
401*4882a593Smuzhiyun 		goto out2;
402*4882a593Smuzhiyun 	status = init_coda_psdev();
403*4882a593Smuzhiyun 	if ( status ) {
404*4882a593Smuzhiyun 		pr_warn("Problem (%d) in init_coda_psdev\n", status);
405*4882a593Smuzhiyun 		goto out1;
406*4882a593Smuzhiyun 	}
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	status = register_filesystem(&coda_fs_type);
409*4882a593Smuzhiyun 	if (status) {
410*4882a593Smuzhiyun 		pr_warn("failed to register filesystem!\n");
411*4882a593Smuzhiyun 		goto out;
412*4882a593Smuzhiyun 	}
413*4882a593Smuzhiyun 	return 0;
414*4882a593Smuzhiyun out:
415*4882a593Smuzhiyun 	for (i = 0; i < MAX_CODADEVS; i++)
416*4882a593Smuzhiyun 		device_destroy(coda_psdev_class, MKDEV(CODA_PSDEV_MAJOR, i));
417*4882a593Smuzhiyun 	class_destroy(coda_psdev_class);
418*4882a593Smuzhiyun 	unregister_chrdev(CODA_PSDEV_MAJOR, "coda");
419*4882a593Smuzhiyun 	coda_sysctl_clean();
420*4882a593Smuzhiyun out1:
421*4882a593Smuzhiyun 	coda_destroy_inodecache();
422*4882a593Smuzhiyun out2:
423*4882a593Smuzhiyun 	return status;
424*4882a593Smuzhiyun }
425*4882a593Smuzhiyun 
exit_coda(void)426*4882a593Smuzhiyun static void __exit exit_coda(void)
427*4882a593Smuzhiyun {
428*4882a593Smuzhiyun         int err, i;
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 	err = unregister_filesystem(&coda_fs_type);
431*4882a593Smuzhiyun 	if (err != 0)
432*4882a593Smuzhiyun 		pr_warn("failed to unregister filesystem\n");
433*4882a593Smuzhiyun 	for (i = 0; i < MAX_CODADEVS; i++)
434*4882a593Smuzhiyun 		device_destroy(coda_psdev_class, MKDEV(CODA_PSDEV_MAJOR, i));
435*4882a593Smuzhiyun 	class_destroy(coda_psdev_class);
436*4882a593Smuzhiyun 	unregister_chrdev(CODA_PSDEV_MAJOR, "coda");
437*4882a593Smuzhiyun 	coda_sysctl_clean();
438*4882a593Smuzhiyun 	coda_destroy_inodecache();
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun module_init(init_coda);
442*4882a593Smuzhiyun module_exit(exit_coda);
443*4882a593Smuzhiyun 
444