~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/coda/psdev.c

Version: ~ [ linux-5.7-rc7 ] ~ [ linux-5.6.14 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.42 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.124 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.181 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.224 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.224 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.84 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp