This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / fs / intermezzo / psdev.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *              An implementation of a loadable kernel mode driver providing
5  *              multiple kernel/user space bidirectional communications links.
6  *
7  *              Author:         Alan Cox <alan@cymru.net>
8  *
9  *              This program is free software; you can redistribute it and/or
10  *              modify it under the terms of the GNU General Public License
11  *              version 2 as published by the Free Software Foundation.
12  *
13  *              Adapted to become the Linux 2.0 Coda pseudo device
14  *              Peter  Braam  <braam@maths.ox.ac.uk>
15  *              Michael Callahan <mjc@emmy.smith.edu>
16  *
17  *              Changes for Linux 2.1
18  *              Copyright (c) 1997 Carnegie-Mellon University
19  *
20  *              Redone again for InterMezzo
21  *              Copyright (c) 1998 Peter J. Braam
22  *              Copyright (c) 2000 Mountain View Data, Inc.
23  *              Copyright (c) 2000 Tacitus Systems, Inc.
24  *              Copyright (c) 2001 Cluster File Systems, Inc.
25  *
26  */
27
28 #include <linux/module.h>
29 #include <linux/errno.h>
30 #include <linux/kernel.h>
31 #include <linux/major.h>
32 #include <linux/sched.h>
33 #include <linux/lp.h>
34 #include <linux/slab.h>
35 #include <linux/ioport.h>
36 #include <linux/fcntl.h>
37 #include <linux/delay.h>
38 #include <linux/skbuff.h>
39 #include <linux/proc_fs.h>
40 #include <linux/vmalloc.h>
41 #include <linux/fs.h>
42 #include <linux/file.h>
43 #include <linux/poll.h>
44 #include <linux/init.h>
45 #include <linux/list.h>
46 #include <linux/devfs_fs_kernel.h>
47 #include <asm/io.h>
48 #include <asm/segment.h>
49 #include <asm/system.h>
50 #include <asm/poll.h>
51 #include <asm/uaccess.h>
52 #include <linux/miscdevice.h>
53
54 #include "intermezzo_fs.h"
55 #include "intermezzo_psdev.h"
56
57
58 #ifdef PRESTO_DEVEL
59 int  presto_print_entry = 1;
60 int  presto_debug = 4095;
61 #else
62 int  presto_print_entry = 0;
63 int  presto_debug = 0;
64 #endif
65
66 /* Like inode.c (presto_sym_iops), the initializer is just to prevent
67    izo_channels from appearing as a COMMON symbol (and therefore
68    interfering with other modules that use the same variable name). */
69 struct upc_channel izo_channels[MAX_CHANNEL] = {{0}};
70
71 int izo_psdev_get_free_channel(void)
72 {
73         int i, result = -1;
74         
75         for (i = 0 ; i < MAX_CHANNEL ; i++ ) {
76                 if (list_empty(&(izo_channels[i].uc_cache_list))) { 
77                     result = i;
78                     break;
79                 }
80         }
81         return result;
82 }
83
84
85 int izo_psdev_setpid(int minor)
86 {
87         struct upc_channel *channel; 
88         if (minor < 0 || minor >= MAX_CHANNEL) { 
89                 return -EINVAL;
90         }
91
92         channel = &(izo_channels[minor]); 
93         /*
94          * This ioctl is performed by each Lento that starts up
95          * and wants to do further communication with presto.
96          */
97         CDEBUG(D_PSDEV, "Setting current pid to %d channel %d\n", 
98                current->pid, minor);
99         channel->uc_pid = current->pid;
100         spin_lock(&channel->uc_lock); 
101         if ( !list_empty(&channel->uc_processing) ) {
102                 struct list_head *lh;
103                 struct upc_req *req;
104                 CERROR("WARNING: setpid & processing not empty!\n");
105                 list_for_each(lh, &channel->uc_processing) {
106                         req = list_entry(lh, struct upc_req, rq_chain);
107                         /* freeing of req and data is done by the sleeper */
108                         wake_up(&req->rq_sleep);
109                 }
110         }
111         if ( !list_empty(&channel->uc_processing) ) {
112                 CERROR("BAD: FAILDED TO CLEAN PROCESSING LIST!\n");
113         }
114         spin_unlock(&channel->uc_lock); 
115         EXIT;
116         return 0;
117 }
118
119 int izo_psdev_setchannel(struct file *file, int fd)
120 {
121
122         struct file *psdev_file = fget(fd); 
123         struct presto_cache *cache = presto_get_cache(file->f_dentry->d_inode);
124
125         if (!psdev_file) { 
126                 CERROR("%s: no psdev_file!\n", __FUNCTION__);
127                 return -EINVAL;
128         }
129
130         if (!cache) { 
131                 CERROR("%s: no cache!\n", __FUNCTION__);
132                 fput(psdev_file); 
133                 return -EINVAL;
134         } 
135
136         if (psdev_file->private_data) { 
137                 CERROR("%s: channel already set!\n", __FUNCTION__);
138                 fput(psdev_file); 
139                 return -EINVAL;
140         }
141
142         psdev_file->private_data = cache->cache_psdev;
143         fput(psdev_file); 
144         EXIT; 
145         return 0; 
146 }
147
148 inline int presto_lento_up(int minor) 
149 {
150         return izo_channels[minor].uc_pid;
151 }
152
153 static unsigned int presto_psdev_poll(struct file *file, poll_table * wait)
154  {
155         struct upc_channel *channel = (struct upc_channel *)file->private_data;
156         unsigned int mask = POLLOUT | POLLWRNORM;
157
158         /* ENTRY; this will flood you */
159         if ( ! channel ) { 
160                 CERROR("%s: bad psdev file\n", __FUNCTION__);
161                 return -EBADF;
162         }
163
164         poll_wait(file, &(channel->uc_waitq), wait);
165
166         spin_lock(&channel->uc_lock);
167         if (!list_empty(&channel->uc_pending)) {
168                 CDEBUG(D_PSDEV, "Non-empty pending list.\n");
169                 mask |= POLLIN | POLLRDNORM;
170         }
171         spin_unlock(&channel->uc_lock);
172
173         /* EXIT; will flood you */
174         return mask;
175 }
176
177 /*
178  *      Receive a message written by Lento to the psdev
179  */
180 static ssize_t presto_psdev_write(struct file *file, const char *buf,
181                                   size_t count, loff_t *off)
182 {
183         struct upc_channel *channel = (struct upc_channel *)file->private_data;
184         struct upc_req *req = NULL;
185         struct upc_req *tmp;
186         struct list_head *lh;
187         struct izo_upcall_resp hdr;
188         int error;
189
190         if ( ! channel ) { 
191                 CERROR("%s: bad psdev file\n", __FUNCTION__);
192                 return -EBADF;
193         }
194
195         /* Peek at the opcode, uniquefier */
196         if ( count < sizeof(hdr) ) {
197               CERROR("presto_psdev_write: Lento didn't write full hdr.\n");
198                 return -EINVAL;
199         }
200
201         error = copy_from_user(&hdr, buf, sizeof(hdr));
202         if ( error )
203                 return -EFAULT;
204
205         CDEBUG(D_PSDEV, "(process,opc,uniq)=(%d,%d,%d)\n",
206                current->pid, hdr.opcode, hdr.unique);
207
208         spin_lock(&channel->uc_lock); 
209         /* Look for the message on the processing queue. */
210         list_for_each(lh, &channel->uc_processing) {
211                 tmp = list_entry(lh, struct upc_req , rq_chain);
212                 if (tmp->rq_unique == hdr.unique) {
213                         req = tmp;
214                         /* unlink here: keeps search length minimal */
215                         list_del_init(&req->rq_chain);
216                         CDEBUG(D_PSDEV,"Eureka opc %d uniq %d!\n",
217                                hdr.opcode, hdr.unique);
218                         break;
219                 }
220         }
221         spin_unlock(&channel->uc_lock); 
222         if (!req) {
223                 CERROR("psdev_write: msg (%d, %d) not found\n",
224                        hdr.opcode, hdr.unique);
225                 return(-ESRCH);
226         }
227
228         /* move data into response buffer. */
229         if (req->rq_bufsize < count) {
230                 CERROR("psdev_write: too much cnt: %d, cnt: %Zd, "
231                        "opc: %d, uniq: %d.\n",
232                        req->rq_bufsize, count, hdr.opcode, hdr.unique);
233                 count = req->rq_bufsize; /* don't have more space! */
234         }
235         error = copy_from_user(req->rq_data, buf, count);
236         if ( error )
237                 return -EFAULT;
238
239         /* adjust outsize: good upcalls can be aware of this */
240         req->rq_rep_size = count;
241         req->rq_flags |= REQ_WRITE;
242
243         wake_up(&req->rq_sleep);
244         return(count);
245 }
246
247 /*
248  *      Read a message from the kernel to Lento
249  */
250 static ssize_t presto_psdev_read(struct file * file, char * buf,
251                                  size_t count, loff_t *off)
252 {
253         struct upc_channel *channel = (struct upc_channel *)file->private_data;
254         struct upc_req *req;
255         int result = count;
256
257         if ( ! channel ) { 
258                 CERROR("%s: bad psdev file\n", __FUNCTION__);
259                 return -EBADF;
260         }
261
262         spin_lock(&channel->uc_lock); 
263         if (list_empty(&(channel->uc_pending))) {
264                 CDEBUG(D_UPCALL, "Empty pending list in read, not good\n");
265                 spin_unlock(&channel->uc_lock); 
266                 return -EINVAL;
267         }
268         req = list_entry((channel->uc_pending.next), struct upc_req, rq_chain);
269         list_del(&(req->rq_chain));
270         if (! (req->rq_flags & REQ_ASYNC) ) {
271                 list_add(&(req->rq_chain), channel->uc_processing.prev);
272         }
273         spin_unlock(&channel->uc_lock); 
274
275         req->rq_flags |= REQ_READ;
276
277         /* Move the input args into userspace */
278         CDEBUG(D_PSDEV, "\n");
279         if (req->rq_bufsize <= count) {
280                 result = req->rq_bufsize;
281         }
282
283         if (count < req->rq_bufsize) {
284                 CERROR ("psdev_read: buffer too small, read %Zd of %d bytes\n",
285                         count, req->rq_bufsize);
286         }
287
288         if ( copy_to_user(buf, req->rq_data, result) ) {
289                 BUG();
290                 return -EFAULT;
291         }
292
293         /* If request was asynchronous don't enqueue, but free */
294         if (req->rq_flags & REQ_ASYNC) {
295                 CDEBUG(D_PSDEV, "psdev_read: async msg (%d, %d), result %d\n",
296                        req->rq_opcode, req->rq_unique, result);
297                 PRESTO_FREE(req->rq_data, req->rq_bufsize);
298                 PRESTO_FREE(req, sizeof(*req));
299                 return result;
300         }
301
302         return result;
303 }
304
305
306 static int presto_psdev_open(struct inode * inode, struct file * file)
307 {
308         ENTRY;
309
310         file->private_data = NULL;  
311
312         CDEBUG(D_PSDEV, "Psdev_open: caller: %d, flags: %d\n", current->pid, file->f_flags);
313
314         EXIT;
315         return 0;
316 }
317
318
319
320 static int presto_psdev_release(struct inode * inode, struct file * file)
321 {
322         struct upc_channel *channel = (struct upc_channel *)file->private_data;
323         struct upc_req *req;
324         struct list_head *lh;
325         ENTRY;
326
327         if ( ! channel ) { 
328                 CERROR("%s: bad psdev file\n", __FUNCTION__);
329                 return -EBADF;
330         }
331
332         CDEBUG(D_PSDEV, "Lento: pid %d\n", current->pid);
333         channel->uc_pid = 0;
334
335         /* Wake up clients so they can return. */
336         CDEBUG(D_PSDEV, "Wake up clients sleeping for pending.\n");
337         spin_lock(&channel->uc_lock); 
338         list_for_each(lh, &channel->uc_pending) {
339                 req = list_entry(lh, struct upc_req, rq_chain);
340
341                 /* Async requests stay around for a new lento */
342                 if (req->rq_flags & REQ_ASYNC) {
343                         continue;
344                 }
345                 /* the sleeper will free the req and data */
346                 req->rq_flags |= REQ_DEAD; 
347                 wake_up(&req->rq_sleep);
348         }
349
350         CDEBUG(D_PSDEV, "Wake up clients sleeping for processing\n");
351         list_for_each(lh, &channel->uc_processing) {
352                 req = list_entry(lh, struct upc_req, rq_chain);
353                 /* freeing of req and data is done by the sleeper */
354                 req->rq_flags |= REQ_DEAD; 
355                 wake_up(&req->rq_sleep);
356         }
357         spin_unlock(&channel->uc_lock); 
358         CDEBUG(D_PSDEV, "Done.\n");
359
360         EXIT;
361         return 0;
362 }
363
364 static struct file_operations presto_psdev_fops = {
365         .owner   = THIS_MODULE,
366         .read    = presto_psdev_read,
367         .write   = presto_psdev_write,
368         .poll    = presto_psdev_poll,
369         .open    = presto_psdev_open,
370         .release = presto_psdev_release
371 };
372
373 /* modules setup */
374 static struct miscdevice intermezzo_psdev = {
375         INTERMEZZO_MINOR,
376         "intermezzo",
377         &presto_psdev_fops
378 };
379
380 int  presto_psdev_init(void)
381 {
382         int i;
383         int err; 
384
385         if ( (err = misc_register(&intermezzo_psdev)) ) { 
386                 CERROR("%s: cannot register %d err %d\n", 
387                        __FUNCTION__, INTERMEZZO_MINOR, err);
388                 return -EIO;
389         }
390
391         memset(&izo_channels, 0, sizeof(izo_channels));
392         for ( i = 0 ; i < MAX_CHANNEL ; i++ ) {
393                 struct upc_channel *channel = &(izo_channels[i]);
394                 INIT_LIST_HEAD(&channel->uc_pending);
395                 INIT_LIST_HEAD(&channel->uc_processing);
396                 INIT_LIST_HEAD(&channel->uc_cache_list);
397                 init_waitqueue_head(&channel->uc_waitq);
398                 channel->uc_lock = SPIN_LOCK_UNLOCKED;
399                 channel->uc_hard = 0;
400                 channel->uc_no_filter = 0;
401                 channel->uc_no_journal = 0;
402                 channel->uc_no_upcall = 0;
403                 channel->uc_timeout = 30;
404                 channel->uc_errorval = 0;
405                 channel->uc_minor = i;
406         }
407         return 0;
408 }
409
410 void presto_psdev_cleanup(void)
411 {
412         int i;
413
414         misc_deregister(&intermezzo_psdev);
415
416         for ( i = 0 ; i < MAX_CHANNEL ; i++ ) {
417                 struct upc_channel *channel = &(izo_channels[i]);
418                 struct list_head *lh, *next;
419
420                 spin_lock(&channel->uc_lock); 
421                 if ( ! list_empty(&channel->uc_pending)) { 
422                         CERROR("Weird, tell Peter: module cleanup and pending list not empty dev %d\n", i);
423                 }
424                 if ( ! list_empty(&channel->uc_processing)) { 
425                         CERROR("Weird, tell Peter: module cleanup and processing list not empty dev %d\n", i);
426                 }
427                 if ( ! list_empty(&channel->uc_cache_list)) { 
428                         CERROR("Weird, tell Peter: module cleanup and cache listnot empty dev %d\n", i);
429                 }
430                 list_for_each_safe(lh, next, &channel->uc_pending) {
431                         struct upc_req *req;
432
433                         req = list_entry(lh, struct upc_req, rq_chain);
434                         if ( req->rq_flags & REQ_ASYNC ) {
435                                 list_del(&(req->rq_chain));
436                                 CDEBUG(D_UPCALL, "free pending upcall type %d\n",
437                                        req->rq_opcode);
438                                 PRESTO_FREE(req->rq_data, req->rq_bufsize);
439                                 PRESTO_FREE(req, sizeof(struct upc_req));
440                         } else {
441                                 req->rq_flags |= REQ_DEAD; 
442                                 wake_up(&req->rq_sleep);
443                         }
444                 }
445                 list_for_each(lh, &channel->uc_processing) {
446                         struct upc_req *req;
447                         req = list_entry(lh, struct upc_req, rq_chain);
448                         list_del(&(req->rq_chain));
449                         req->rq_flags |= REQ_DEAD; 
450                         wake_up(&req->rq_sleep);
451                 }
452                 spin_unlock(&channel->uc_lock); 
453         }
454 }
455
456 /*
457  * lento_upcall and lento_downcall routines
458  */
459 static inline unsigned long lento_waitfor_upcall
460             (struct upc_channel *channel, struct upc_req *req, int minor)
461 {
462         DECLARE_WAITQUEUE(wait, current);
463         unsigned long posttime;
464
465         req->rq_posttime = posttime = jiffies;
466
467         add_wait_queue(&req->rq_sleep, &wait);
468         for (;;) {
469                 if ( izo_channels[minor].uc_hard == 0 )
470                         set_current_state(TASK_INTERRUPTIBLE);
471                 else
472                         set_current_state(TASK_UNINTERRUPTIBLE);
473
474                 /* got a reply */
475                 if ( req->rq_flags & (REQ_WRITE | REQ_DEAD) )
476                         break;
477
478                 /* these cases only apply when TASK_INTERRUPTIBLE */ 
479                 if ( !izo_channels[minor].uc_hard && signal_pending(current) ) {
480                         /* if this process really wants to die, let it go */
481                         if (sigismember(&(current->pending.signal), SIGKILL)||
482                             sigismember(&(current->pending.signal), SIGINT) )
483                                 break;
484                         /* signal is present: after timeout always return
485                            really smart idea, probably useless ... */
486                         if ( time_after(jiffies, req->rq_posttime +
487                              izo_channels[minor].uc_timeout * HZ) )
488                                 break;
489                 }
490                 schedule();
491         }
492
493         spin_lock(&channel->uc_lock);
494         list_del_init(&req->rq_chain); 
495         spin_unlock(&channel->uc_lock);
496         remove_wait_queue(&req->rq_sleep, &wait);
497         set_current_state(TASK_RUNNING);
498
499         CDEBUG(D_SPECIAL, "posttime: %ld, returned: %ld\n",
500                posttime, jiffies-posttime);
501         return  (jiffies - posttime);
502 }
503
504 /*
505  * lento_upcall will return an error in the case of
506  * failed communication with Lento _or_ will peek at Lento
507  * reply and return Lento's error.
508  *
509  * As lento has 2 types of errors, normal errors (positive) and internal
510  * errors (negative), normal errors are negated, while internal errors
511  * are all mapped to -EINTR, while showing a nice warning message. (jh)
512  *
513  * lento_upcall will always free buffer, either directly, when an upcall
514  * is read (in presto_psdev_read), when the filesystem is unmounted, or
515  * when the module is unloaded.
516  */
517 int izo_upc_upcall(int minor, int *size, struct izo_upcall_hdr *buffer, 
518                    int async)
519 {
520         unsigned long runtime;
521         struct upc_channel *channel;
522         struct izo_upcall_resp *out;
523         struct upc_req *req;
524         int error = 0;
525
526         ENTRY;
527         channel = &(izo_channels[minor]);
528
529         if (channel->uc_no_upcall) {
530                 EXIT;
531                 goto exit_buf;
532         }
533         if (!channel->uc_pid && !async) {
534                 EXIT;
535                 error = -ENXIO;
536                 goto exit_buf;
537         }
538
539         /* Format the request message. */
540         PRESTO_ALLOC(req, sizeof(struct upc_req));
541         if ( !req ) {
542                 EXIT;
543                 error = -ENOMEM;
544                 goto exit_buf;
545         }
546         req->rq_data = (void *)buffer;
547         req->rq_flags = 0;
548         req->rq_bufsize = *size;
549         req->rq_rep_size = 0;
550         req->rq_opcode = buffer->u_opc;
551         req->rq_unique = ++channel->uc_seq;
552         init_waitqueue_head(&req->rq_sleep);
553
554         /* Fill in the common input args. */
555         buffer->u_uniq = req->rq_unique;
556         buffer->u_async = async;
557
558         /* Remove potential datarace possibility*/
559         if ( async ) 
560                 req->rq_flags = REQ_ASYNC;
561
562         spin_lock(&channel->uc_lock); 
563         /* Append msg to pending queue and poke Lento. */
564         list_add(&req->rq_chain, channel->uc_pending.prev);
565         spin_unlock(&channel->uc_lock); 
566         CDEBUG(D_UPCALL,
567                "Proc %d waking Lento %d for(opc,uniq) =(%d,%d) msg at %p.\n",
568                current->pid, channel->uc_pid, req->rq_opcode,
569                req->rq_unique, req);
570         wake_up_interruptible(&channel->uc_waitq);
571
572         if ( async ) {
573                 /* req, rq_data are freed in presto_psdev_read for async */
574                 /* req->rq_flags = REQ_ASYNC;*/
575                 EXIT;
576                 return 0;
577         }
578
579         /* We can be interrupted while we wait for Lento to process
580          * our request.  If the interrupt occurs before Lento has read
581          * the request, we dequeue and return. If it occurs after the
582          * read but before the reply, we dequeue, send a signal
583          * message, and return. If it occurs after the reply we ignore
584          * it. In no case do we want to restart the syscall.  If it
585          * was interrupted by a lento shutdown (psdev_close), return
586          * ENODEV.  */
587
588         /* Go to sleep.  Wake up on signals only after the timeout. */
589         runtime = lento_waitfor_upcall(channel, req, minor);
590
591         CDEBUG(D_TIMING, "opc: %d time: %ld uniq: %d size: %d\n",
592                req->rq_opcode, jiffies - req->rq_posttime,
593                req->rq_unique, req->rq_rep_size);
594         CDEBUG(D_UPCALL,
595                "..process %d woken up by Lento for req at 0x%p, data at %p\n",
596                current->pid, req, req->rq_data);
597
598         if (channel->uc_pid) {      /* i.e. Lento is still alive */
599           /* Op went through, interrupt or not we go on */
600             if (req->rq_flags & REQ_WRITE) {
601                     out = (struct izo_upcall_resp *)req->rq_data;
602                     /* here we map positive Lento errors to kernel errors */
603                     if ( out->result < 0 ) {
604                             CERROR("Tell Peter: Lento returns negative error %d, for oc %d!\n",
605                                    out->result, out->opcode);
606                           out->result = EINVAL;
607                     }
608                     error = -out->result;
609                     CDEBUG(D_UPCALL, "upcall: (u,o,r) (%d, %d, %d) out at %p\n",
610                            out->unique, out->opcode, out->result, out);
611                     *size = req->rq_rep_size;
612                     EXIT;
613                     goto exit_req;
614             }
615             /* Interrupted before lento read it. */
616             if ( !(req->rq_flags & REQ_READ) && signal_pending(current)) {
617                     CDEBUG(D_UPCALL,
618                            "Interrupt before read: (op,un)=(%d,%d), flags %x\n",
619                            req->rq_opcode, req->rq_unique, req->rq_flags);
620                     /* perhaps the best way to convince the app to give up? */
621                     error = -EINTR;
622                     EXIT;
623                     goto exit_req;
624             }
625
626             /* interrupted after Lento did its read, send signal */
627             if ( (req->rq_flags & REQ_READ) && signal_pending(current) ) {
628                     CDEBUG(D_UPCALL,"Interrupt after read: op = %d.%d, flags = %x\n",
629                            req->rq_opcode, req->rq_unique, req->rq_flags);
630
631                     error = -EINTR;
632             } else {
633                   CERROR("Lento: Strange interruption - tell Peter.\n");
634                     error = -EINTR;
635             }
636         } else {        /* If lento died i.e. !UC_OPEN(channel) */
637                 CERROR("lento_upcall: Lento dead on (op,un) (%d.%d) flags %d\n",
638                        req->rq_opcode, req->rq_unique, req->rq_flags);
639                 error = -ENODEV;
640         }
641
642 exit_req:
643         PRESTO_FREE(req, sizeof(struct upc_req));
644 exit_buf:
645         PRESTO_FREE(buffer,*size);
646         return error;
647 }