Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / net / tux / main.c
1 /*
2  * TUX - Integrated Application Protocols Layer and Object Cache
3  *
4  * Copyright (C) 2000, 2001, Ingo Molnar <mingo@redhat.com>
5  *
6  * main.c: main management and initialization routines
7  */
8
9 #define __KERNEL_SYSCALLS__
10 #define __KERNEL_SYSCALLS_NO_ERRNO__
11
12 #include <net/tux.h>
13
14 /****************************************************************
15  *      This program is free software; you can redistribute it and/or modify
16  *      it under the terms of the GNU General Public License as published by
17  *      the Free Software Foundation; either version 2, or (at your option)
18  *      any later version.
19  *
20  *      This program is distributed in the hope that it will be useful,
21  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
22  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *      GNU General Public License for more details.
24  *
25  *      You should have received a copy of the GNU General Public License
26  *      along with this program; if not, write to the Free Software
27  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28  *
29  ****************************************************************/
30
31 /*
32  * Threads information.
33  */
34 unsigned int nr_tux_threads;
35 static atomic_t nr_tux_threads_running = ATOMIC_INIT(0);
36 static int stop_threads = 0;
37
38 threadinfo_t threadinfo[CONFIG_TUX_NUMTHREADS];
39
40 static void flush_all_requests (threadinfo_t *ti);
41
42 void flush_all_signals (void)
43 {
44         flush_signals(current);
45         spin_lock_irq(&current->sighand->siglock);
46         recalc_sigpending();
47         spin_unlock_irq(&current->sighand->siglock);
48 }
49
50 int nr_requests_used (void)
51 {
52         unsigned int i, nr = 0;
53
54         for (i = 0; i < nr_tux_threads; i++) {
55                 threadinfo_t *ti = threadinfo + i;
56                 nr += ti->nr_requests - ti->nr_free_requests;
57         }
58
59         return nr;
60 }
61
62 static inline int accept_pending (threadinfo_t *ti)
63 {
64         int j;
65
66         for (j = 0; j < CONFIG_TUX_NUMSOCKETS; j++) {
67                 if (!ti->listen[j].proto)
68                         break;
69                 if (!ti->listen[j].sock)
70                         break;
71                 if (!reqsk_queue_empty(&inet_csk(ti->listen[j].sock->sk)->icsk_accept_queue))
72                         return 1;
73         }
74         return 0;
75 }
76
77 static inline int requests_pending (threadinfo_t *ti)
78 {
79         if (!list_empty(&ti->work_pending))
80                 return 1;
81         return 0;
82 }
83
84 static int event_loop (threadinfo_t *ti)
85 {
86         tux_req_t *req;
87         int work_done;
88
89 repeat_accept:
90         if (ti->thread != current)
91                 TUX_BUG();
92
93         /*
94          * Any (relevant) event on the socket will change this
95          * thread to TASK_RUNNING because we add it to both
96          * the main listening and the connection request socket
97          * waitqueues. Thus we can do 'lazy checking' of work
98          * to be done and schedule away only if the thread is
99          * still TASK_INTERRUPTIBLE. This makes TUX fully
100          * event driven.
101          */
102         set_task_state(current, TASK_INTERRUPTIBLE);
103         current->flags |= PF_MEMALLOC;
104         work_done = 0;
105         if (accept_pending(ti))
106                 work_done = accept_requests(ti);
107
108         if (requests_pending(ti)) {
109                 work_done = process_requests(ti, &req);
110                 if (req)
111                         goto handle_userspace_req;
112         }
113
114         /*
115          * Be nice to other processes:
116          */
117         if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) {
118                 __set_task_state(current, TASK_RUNNING);
119                 schedule();
120                 goto repeat_accept;
121         }
122
123         if (ti->userspace_req)
124                 TUX_BUG();
125         if (unlikely(stop_threads))
126                 goto handle_stop;
127
128         /* Any signals? */
129         if (unlikely(signal_pending(current)))
130                 goto handle_signal;
131
132         if (work_done)
133                 goto repeat_accept;
134         /*
135          * Any socket event either on the listen socket
136          * or on the request sockets will wake us up:
137          */
138         if ((current->state != TASK_RUNNING) &&
139                         !requests_pending(ti) && !accept_pending(ti)) {
140                 Dprintk("fast thread: no work to be done, sleeping.\n");
141                 schedule();
142                 Dprintk("fast thread: back from sleep!\n");
143                 goto repeat_accept;
144         }
145         goto repeat_accept;
146
147 handle_userspace_req:
148         if (req->attr)
149                 TUX_BUG();
150         switch_docroot(req);
151         ti->userspace_req = req;
152         __set_task_state(current, TASK_RUNNING);
153         return TUX_RETURN_USERSPACE_REQUEST;
154
155 handle_signal:
156         __set_task_state(current, TASK_RUNNING);
157         return TUX_RETURN_SIGNAL;
158
159 handle_stop:
160         __set_task_state(current, TASK_RUNNING);
161         return TUX_RETURN_EXIT;
162 }
163
164 static int init_queues (int nr_tux_threads)
165 {
166         int i;
167
168         for (i = 0; i < nr_tux_threads; i++) {
169                 threadinfo_t *ti = threadinfo + i;
170
171                 INIT_LIST_HEAD(&ti->all_requests);
172
173                 spin_lock_init(&ti->free_requests_lock);
174                 INIT_LIST_HEAD(&ti->free_requests);
175
176                 spin_lock_init(&ti->work_lock);
177                 INIT_LIST_HEAD(&ti->work_pending);
178                 INIT_LIST_HEAD(&ti->lru);
179
180         }
181         return 0;
182 }
183
184 int tux_chroot (char *dir)
185 {
186         kernel_cap_t saved_cap = current->cap_effective;
187         mm_segment_t oldmm;
188         int err;
189
190         /* Allow chroot dir to be in kernel space. */
191         oldmm = get_fs(); set_fs(KERNEL_DS);
192         set_fs(KERNEL_DS);
193         cap_raise (current->cap_effective, CAP_SYS_CHROOT);
194
195         err = sys_chroot(dir);
196         if (!err)
197                 sys_chdir("/");
198
199         current->cap_effective = saved_cap;
200         set_fs(oldmm);
201
202         return err;
203 }
204
205 /*
206  * Right now this is not fully SMP-safe against multiple TUX
207  * managers. It's just a rudimentary protection against typical
208  * mistakes.
209  */
210 static int initialized = 0;
211
212 #define MAX_DOCROOTLEN 500
213
214 static int lookup_docroot(struct nameidata *docroot, const char *name)
215 {
216         int err;
217
218         docroot->mnt = mntget(current->fs->rootmnt);
219         docroot->dentry = dget(current->fs->root);
220         docroot->last.len = 0;
221         docroot->flags = LOOKUP_FOLLOW;
222
223         err = path_walk(name, docroot);
224         if (err) {
225                 mntput(docroot->mnt);
226                 docroot->mnt = NULL;
227                 return err;
228         }
229         return 0;
230 }
231
232 static int user_req_startup (void)
233 {
234         char name[MAX_DOCROOTLEN];
235         struct nameidata *docroot;
236         unsigned int i;
237         int err;
238
239         if (initialized)
240                 return -EINVAL;
241         initialized = 1;
242
243         /*
244          * Look up the HTTP and FTP document root.
245          * (typically they are shared, but can be
246          * different directories.)
247          */
248         docroot = &tux_proto_http.main_docroot;
249         if (docroot->mnt)
250                 TUX_BUG();
251         strcpy(name, tux_common_docroot);
252         strcat(name, tux_http_subdocroot);
253
254         err = lookup_docroot(docroot, name);
255         if (err) {
256                 initialized = 0;
257                 printk(KERN_ERR "TUX: could not look up HTTP documentroot: \"%s\"\n", name);
258                 return err;
259         }
260
261         docroot = &tux_proto_ftp.main_docroot;
262         if (docroot->mnt)
263                 TUX_BUG();
264         strcpy(name, tux_common_docroot);
265         strcat(name, tux_ftp_subdocroot);
266
267         err = lookup_docroot(docroot, name);
268         if (err) {
269 abort:
270                 docroot = &tux_proto_http.main_docroot;
271                 path_release(docroot);
272                 memset(docroot, 0, sizeof(*docroot));
273                 initialized = 0;
274                 printk(KERN_ERR "TUX: could not look up FTP documentroot: \"%s\"\n", name);
275                 return err;
276         }
277
278         /*
279          * Start up the logger thread. (which opens the logfile)
280          */
281         start_log_thread();
282
283         nr_tux_threads = tux_threads;
284         if (nr_tux_threads < 1)
285                 nr_tux_threads = 1;
286         if (nr_tux_threads > CONFIG_TUX_NUMTHREADS)
287                 nr_tux_threads = CONFIG_TUX_NUMTHREADS;
288         tux_threads = nr_tux_threads;
289
290         /*
291          * Set up per-thread work-queues:
292          */
293         memset(threadinfo, 0, CONFIG_TUX_NUMTHREADS*sizeof(threadinfo_t));
294         init_queues(nr_tux_threads);
295
296         /*
297          * Prepare the worker thread structures.
298          */
299         for (i = 0; i < nr_tux_threads; i++) {
300                 threadinfo_t *ti = threadinfo + i;
301                 ti->cpu = i;
302                 ti->gzip_state.workspace =
303                         vmalloc(zlib_deflate_workspacesize());
304                 if (!ti->gzip_state.workspace ||
305                             (zlib_deflateInit(&ti->gzip_state, 6) != Z_OK)) {
306                         stop_log_thread();
307                         goto abort;
308                 }
309                 init_MUTEX(&ti->gzip_sem);
310         }
311
312         __module_get(tux_module);
313
314         return 0;
315 }
316
317 static DECLARE_WAIT_QUEUE_HEAD(wait_stop);
318 static DECLARE_WAIT_QUEUE_HEAD(thread_stopped);
319
320 static int user_req_shutdown (void)
321 {
322         DECLARE_WAITQUEUE(wait, current);
323         struct nameidata *docroot;
324         int i, err = -EINVAL;
325
326         lock_kernel();
327         if (!initialized) {
328                 Dprintk("TUX is not up - cannot shut down.\n");
329                 goto err;
330         }
331         initialized = 0;
332         stop_threads = 1;
333         add_wait_queue(&thread_stopped, &wait);
334
335 wait_more:
336         /*
337          * Wake up all the worker threads so they notice
338          * that we are being stopped.
339          */
340         set_task_state(current, TASK_UNINTERRUPTIBLE);
341         if (atomic_read(&nr_tux_threads_running)) {
342                 Dprintk("TUX: shutdown, %d threads still running.\n",
343                         atomic_read(&nr_tux_threads_running));
344                 wake_up(&wait_stop);
345                 schedule();
346                 goto wait_more;
347         }
348         set_task_state(current, TASK_RUNNING);
349         stop_threads = 0;
350         remove_wait_queue(&thread_stopped, &wait);
351
352         if (nr_async_io_pending())
353                 TUX_BUG();
354
355         stop_log_thread();
356
357         docroot = &tux_proto_http.main_docroot;
358         path_release(docroot);
359         memset(docroot, 0, sizeof(*docroot));
360         docroot = &tux_proto_ftp.main_docroot;
361         path_release(docroot);
362         memset(docroot, 0, sizeof(*docroot));
363         err = 0;
364
365         flush_dentry_attributes();
366         free_mimetypes();
367         unregister_all_tuxmodules();
368
369         for (i = 0; i < nr_tux_threads; i++) {
370                 threadinfo_t *ti = threadinfo + i;
371                 vfree(ti->gzip_state.workspace);
372         }
373
374         module_put(tux_module);
375
376 err:
377         unlock_kernel();
378         return err;
379 }
380
381 void drop_permissions (void)
382 {
383         /*
384          * Userspace drops privileges already, and group
385          * membership is important to keep.
386          */
387         /* Give the new process no privileges.. */
388         current->uid = current->euid =
389                 current->suid = current->fsuid = tux_cgi_uid;
390         current->gid = current->egid =
391                 current->sgid = current->fsgid = tux_cgi_gid;
392         cap_clear(current->cap_permitted);
393         cap_clear(current->cap_inheritable);
394         cap_clear(current->cap_effective);
395 }
396
397 static int wait_for_others (void)
398 {
399         threadinfo_t *ti;
400         unsigned int cpu;
401
402 repeat:
403         if (signal_pending(current))
404                 return -1;
405         set_current_state(TASK_INTERRUPTIBLE);
406         schedule_timeout(HZ/10);
407
408         for (cpu = 0; cpu < nr_tux_threads; cpu++) {
409                 ti = threadinfo + cpu;
410                 if (ti->listen_error)
411                         return -1;
412                 if (!ti->started)
413                         goto repeat;
414         }
415         /* ok, all threads have started up. */
416         return 0;
417 }
418
419 static void zap_listen_sockets (threadinfo_t *ti)
420 {
421         struct socket *sock;
422         int i;
423
424         for (i = 0; i < CONFIG_TUX_NUMSOCKETS; i++) {
425                 if (!ti->listen[i].proto)
426                         break;
427                 sock = ti->listen[i].sock;
428                 if (!ti->listen[i].cloned && sock) {
429                         while (waitqueue_active(sock->sk->sk_sleep))
430                                 yield();
431                         sock_release(sock);
432                 }
433                 ti->listen[i].sock = NULL;
434                 ti->listen[i].proto = NULL;
435                 ti->listen[i].cloned = 0;
436         }
437 }
438
439 static DECLARE_MUTEX(serialize_startup);
440
441 static int user_req_start_thread (threadinfo_t *ti)
442 {
443         unsigned int err, cpu, i, j, k;
444         struct k_sigaction *ka;
445
446         cpu = ti->cpu;
447 #ifdef CONFIG_SMP
448         {
449                 unsigned int home_cpu;
450                 cpumask_t map;
451
452                 home_cpu = (cpu + tux_cpu_offset) % num_online_cpus();
453                 map = cpumask_of_cpu(home_cpu);
454
455                 cpus_and(map, map, cpu_online_map);
456                 if (!(cpus_empty(map)))
457                         set_cpus_allowed(current, map);
458         }
459 #endif
460         ti->thread = current;
461         atomic_inc(&nr_tux_threads_running);
462
463         err = start_cachemiss_threads(ti);
464         if (err)
465                 goto out;
466
467         init_waitqueue_entry(&ti->stop, current);
468         for (j = 0; j < CONFIG_TUX_NUMSOCKETS; j++)
469                 init_waitqueue_entry(ti->wait_event + j, current);
470
471         ka = current->sighand->action + SIGCHLD-1;
472         ka->sa.sa_handler = SIG_IGN;
473
474         /* Block all signals except SIGKILL, SIGSTOP, SIGHUP and SIGCHLD */
475         spin_lock_irq(&current->sighand->siglock);
476         siginitsetinv(&current->blocked, sigmask(SIGKILL) |
477                         sigmask(SIGSTOP)| sigmask(SIGHUP) | sigmask(SIGCHLD));
478         recalc_sigpending();
479         spin_unlock_irq(&current->sighand->siglock);
480
481         if (!tux_listen[cpu][0].proto) {
482                 printk(KERN_ERR "no listen socket specified for TUX thread %d, in /proc/net/tux/%d/listen/, aborting.\n", cpu, cpu);
483                 goto error;
484         }
485
486         /*
487          * Serialize startup so that listen sockets can be
488          * created race-free.
489          */
490         down(&serialize_startup);
491
492         Dprintk("thread %d initializing sockets.\n", cpu);
493
494         for (k = 0; k < CONFIG_TUX_NUMSOCKETS; k++) {
495                 tux_socket_t *e1, *e2;
496
497                 e1 = tux_listen[cpu] + k;
498                 if (!e1->proto)
499                         break;
500                 for (i = 0; i < CONFIG_TUX_NUMTHREADS; i++) {
501                         if (i == cpu)
502                                 continue;
503                         for (j = 0; j < CONFIG_TUX_NUMSOCKETS; j++) {
504                                 e2 = tux_listen[i] + j;
505                                 if (!e2->proto)
506                                         continue;
507                                 if ((e1->ip == e2->ip) && (e1->port == e2->port) && (e1->proto == e2->proto) && threadinfo[i].listen[j].proto) {
508                                         ti->listen[k] = threadinfo[i].listen[j];
509                                         ti->listen[k].cloned = 1;
510                                         Dprintk("cloned socket %d from thread %d's socket %d.\n", k, i, j);
511                                         goto next_socket;
512                                 }
513                         }
514                 }
515
516                 ti->listen[k].sock = start_listening(tux_listen[cpu] + k, cpu);
517                 if (!ti->listen[k].sock)
518                         goto error_unlock;
519                 ti->listen[k].cloned = 0;
520                 ti->listen[k].proto = tux_listen[cpu][k].proto;
521                 Dprintk("thread %d got sock %p (%d), proto %s.\n", cpu, ti->listen[k].sock, k, ti->listen[k].proto->name);
522 next_socket:
523                 ;
524         }
525         Dprintk("thread %d done initializing sockets.\n", cpu);
526         up(&serialize_startup);
527
528         if (wait_for_others())
529                 goto error_nomsg;
530
531         if (!ti->listen[0].proto) {
532                 printk("hm, socket 0 has no protocol.\n");
533                 goto error;
534         }
535
536         add_wait_queue(&wait_stop, &ti->stop);
537         for (j = 0; j < CONFIG_TUX_NUMSOCKETS; j++)
538                 if (ti->listen[j].proto)
539                         add_wait_queue_exclusive(ti->listen[j].sock->sk->sk_sleep,
540                                 ti->wait_event + j);
541         drop_permissions();
542
543         __module_get(tux_module);
544         return 0;
545
546 error_unlock:
547         up(&serialize_startup);
548 error:
549         printk(KERN_NOTICE "TUX: could not start worker thread %d.\n", ti->cpu);
550
551 error_nomsg:
552         ti->listen_error = 1;
553         ti->started = 0;
554
555         zap_listen_sockets(ti);
556         flush_all_requests(ti);
557         stop_cachemiss_threads(ti);
558
559         err = -EINVAL;
560
561 out:
562         /*
563          * Last thread close the door:
564          */
565         if (atomic_dec_and_test(&nr_tux_threads_running))
566                 user_req_shutdown();
567
568         return -err;
569 }
570
571 static int flush_idleinput (threadinfo_t * ti)
572 {
573         struct list_head *head, *tmp;
574         tux_req_t *req;
575         int count = 0;
576
577         head = &ti->all_requests;
578         tmp = head->next;
579
580         while (tmp != head) {
581                 req = list_entry(tmp, tux_req_t, all);
582                 tmp = tmp->next;
583                 if (test_bit(0, &req->idle_input)) {
584                         idle_event(req);
585                         count++;
586                 }
587         }
588         return count;
589 }
590
591 static int flush_waitoutput (threadinfo_t * ti)
592 {
593         struct list_head *head, *tmp;
594         tux_req_t *req;
595         int count = 0;
596
597         head = &ti->all_requests;
598         tmp = head->next;
599
600         while (tmp != head) {
601                 req = list_entry(tmp, tux_req_t, all);
602                 tmp = tmp->next;
603                 if (test_bit(0, &req->wait_output_space)) {
604                         output_space_event(req);
605                         count++;
606                 }
607         }
608         return count;
609 }
610
611 static void flush_all_requests (threadinfo_t *ti)
612 {
613         for (;;) {
614                 int count;
615
616                 count = flush_idleinput(ti);
617                 count += flush_waitoutput(ti);
618                 count += tux_flush_workqueue(ti);
619                 count += flush_freequeue(ti);
620                 if (!ti->nr_requests)
621                         break;
622                 /*
623                  * Go through again if we advanced:
624                  */
625                 if (count)
626                         continue;
627                 Dprintk("flush_all_requests: %d requests still waiting.\n", ti->nr_requests);
628 #ifdef CONFIG_TUX_DEBUG
629                 count = print_all_requests(ti);
630                 Dprintk("flush_all_requests: printed %d requests.\n", count);
631 #endif
632                 current->state = TASK_UNINTERRUPTIBLE;
633                 schedule_timeout(HZ/10);
634         }
635 }
636
637 int nr_async_io_pending (void)
638 {
639         unsigned int i, sum = 0;
640
641         for (i = 0; i < nr_tux_threads; i++) {
642                 threadinfo_t *ti = threadinfo + i;
643                 if (ti->iot)
644                         sum += ti->iot->nr_async_pending;
645         }
646         return sum;
647 }
648
649 static int user_req_stop_thread (threadinfo_t *ti)
650 {
651         int j;
652
653         printk(KERN_NOTICE "TUX: thread %d stopping ...\n",
654                 (int)(ti-threadinfo));
655
656         if (!ti->started)
657                 TUX_BUG();
658         for (j = 0; j < CONFIG_TUX_NUMSOCKETS; j++)
659                 if (ti->listen[j].proto)
660                         remove_wait_queue(ti->listen[j].sock->sk->sk_sleep,
661                                 ti->wait_event + j);
662         remove_wait_queue(&wait_stop, &ti->stop);
663
664         Dprintk(KERN_NOTICE "TUX: thread %d waiting for sockets to go inactive ...\n", (int)(ti-threadinfo));
665         zap_listen_sockets(ti);
666
667         Dprintk(KERN_NOTICE "TUX: thread %d has all sockets inactive.\n", (int)(ti-threadinfo));
668
669         flush_all_requests(ti);
670         stop_cachemiss_threads(ti);
671
672         if (ti->nr_requests)
673                 TUX_BUG();
674         ti->started = 0;
675
676         printk(KERN_INFO "TUX: thread %d stopped.\n", ti->cpu);
677
678         ti->thread = NULL;
679         current->tux_info = NULL;
680         current->tux_exit = NULL;
681         atomic_dec(&nr_tux_threads_running);
682         wake_up(&thread_stopped);
683
684         module_put(tux_module);
685
686         return 0;
687 }
688
689 #define COPY_INT(u_field, k_field)                                      \
690 do {                                                                    \
691         if (__copy_to_user(&u_info->u_field, &req->k_field,             \
692                                         sizeof(req->k_field)))          \
693                 return_EFAULT;                                          \
694 } while (0)
695
696 #define GETLEN(k_field, maxlen)                                         \
697                 ((req->k_field##_len < maxlen) ?                        \
698                 req->k_field##_len : maxlen-1)
699
700 #define COPY_STR(u_field, k_field, maxlen)                              \
701 do {                                                                    \
702         if (__copy_to_user(u_info->u_field, req->k_field##_str,         \
703                 GETLEN(k_field, maxlen)))                               \
704                         return_EFAULT;                                  \
705 } while (0)
706
707 #define COPY_COND_STR(u_field,k_field,maxlen)                           \
708 do {                                                                    \
709         if (req->k_field##_len)                                         \
710                 COPY_STR(u_field, k_field, maxlen);                     \
711         if (__put_user((char)0, u_info->u_field +                       \
712                         GETLEN(k_field, maxlen)))                       \
713                 return_EFAULT;                                          \
714 } while (0)
715
716 static void finish_userspace_req (tux_req_t *req)
717 {
718         threadinfo_t *ti = req->ti;
719
720         ti->userspace_req = NULL;
721         req->usermode = 0;
722         req->private = 0;
723         req->error = 0;
724         DEC_STAT(nr_userspace_pending);
725         flush_request(req, 0);
726 }
727
728 static void zap_userspace_req (tux_req_t *req)
729 {
730         clear_keepalive(req);
731         finish_userspace_req(req);
732 }
733
734 /*
735  * Fills in the user-space request structure:
736  */
737 static int prepare_userspace_req (threadinfo_t *ti, user_req_t *u_info)
738 {
739         u64 u_req;
740         tux_req_t *req = ti->userspace_req;
741         unsigned int tmp;
742         int filelen;
743         int fd;
744
745         Dprintk("prepare_userspace_req(%p).\n", req);
746         if (!req)
747                 TUX_BUG();
748         if (req->error) {
749                 TDprintk("userspace request has error %d.\n", req->error);
750                 return -1;
751         }
752         fd = req->fd;
753         if (fd == -1) {
754                 fd = sock_map_fd(req->sock);
755                 Dprintk("sock_map_fd(%p) :%d.\n", req, fd);
756                 if (fd < 0) {
757                         Dprintk("sock_map_fd() returned %d.\n", fd);
758                         return -EMFILE;
759                 }
760                 req->fd = fd;
761         }
762
763 #define return_EFAULT do { Dprintk("-EFAULT at %d:%s.\n", __LINE__, __FILE__); return -EFAULT; } while (0)
764
765         if (!access_ok(VERIFY_WRITE, u_info, sizeof(*u_info)))
766                 return_EFAULT;
767         if (__copy_to_user(&u_info->sock, &fd, sizeof(fd)))
768                 return_EFAULT;
769         if (req->attr)
770                 TUX_BUG();
771
772         COPY_INT(module_index, usermodule_idx);
773
774         COPY_COND_STR(query, query, MAX_URI_LEN);
775
776         COPY_INT(event, event);
777         Dprintk("prepare userspace, user error: %d, event %d.\n", req->user_error, req->event);
778         COPY_INT(error, user_error);
779         req->user_error = 0;
780
781         filelen = req->total_file_len;
782         if (filelen < 0)
783                 filelen = 0;
784         if (__copy_to_user(&u_info->objectlen, &filelen, sizeof(filelen)))
785                 return_EFAULT;
786         if ((req->method == METHOD_POST) && !filelen)
787                 if (__copy_to_user(&u_info->objectlen,
788                         &req->content_len, sizeof(filelen)))
789                 return_EFAULT;
790         if (req->objectname_len) {
791                 if (req->objectname[req->objectname_len])
792                         TUX_BUG();
793                 if (__copy_to_user(u_info->objectname, req->objectname,
794                                 req->objectname_len + 1))
795                         return_EFAULT;
796         } else
797                 if (__put_user((char)0, u_info->objectname))
798                         return_EFAULT;
799
800         COPY_INT(http_version, version);
801         COPY_INT(http_method, method);
802         COPY_INT(keep_alive, keep_alive);
803
804         COPY_INT(cookies_len, cookies_len);
805         if (req->cookies_len)
806                 COPY_STR(cookies, cookies, MAX_COOKIE_LEN);
807         if (__put_user((char)0, u_info->cookies + req->cookies_len))
808                 return_EFAULT;
809
810         u_req = (u64)(unsigned long)req;
811         if (__copy_to_user(&u_info->id, &u_req, sizeof(u_req)))
812                 return_EFAULT;
813         COPY_INT(priv, private);
814         COPY_INT(bytes_sent, bytes_sent);
815
816         tmp = inet_sk(req->sock->sk)->daddr;
817         if (__copy_to_user(&u_info->client_host, &tmp, sizeof(tmp)))
818                 return_EFAULT;
819
820         COPY_COND_STR(content_type, content_type, MAX_FIELD_LEN);
821         COPY_COND_STR(user_agent, user_agent, MAX_FIELD_LEN);
822         COPY_COND_STR(accept, accept, MAX_FIELD_LEN);
823         COPY_COND_STR(accept_charset, accept_charset, MAX_FIELD_LEN);
824         COPY_COND_STR(accept_encoding, accept_encoding, MAX_FIELD_LEN);
825         COPY_COND_STR(accept_language, accept_language, MAX_FIELD_LEN);
826         COPY_COND_STR(cache_control, cache_control, MAX_FIELD_LEN);
827         COPY_COND_STR(if_modified_since, if_modified_since, MAX_FIELD_LEN);
828         COPY_COND_STR(negotiate, negotiate, MAX_FIELD_LEN);
829         COPY_COND_STR(pragma, pragma, MAX_FIELD_LEN);
830         COPY_COND_STR(referer, referer, MAX_FIELD_LEN);
831
832         return TUX_RETURN_USERSPACE_REQUEST;
833 }
834
835 #define GOTO_ERR_no_unlock do { Dprintk("sys_tux() ERR at %s:%d.\n", __FILE__, __LINE__); goto err_no_unlock; } while (0)
836 #define GOTO_ERR_unlock do { Dprintk("sys_tux() ERR at %s:%d.\n", __FILE__, __LINE__); goto err_unlock; } while (0)
837
838 static int register_mimetype(user_req_t *u_info)
839 {
840         char extension[MAX_URI_LEN], mimetype[MAX_URI_LEN], expires[MAX_URI_LEN];
841         u64 u_addr;
842         char *addr;
843         int ret;
844
845         ret = strncpy_from_user(extension, u_info->objectname, MAX_URI_LEN);
846         if (ret <= 0)
847                 GOTO_ERR_no_unlock;
848         extension[ret] = 0;
849         Dprintk("got MIME extension: %s.\n", extension);
850         ret = copy_from_user(&u_addr, &u_info->object_addr, sizeof(u_addr));
851         if (ret)
852                 GOTO_ERR_no_unlock;
853         addr = (char *)(unsigned long)u_addr;
854         ret = strncpy_from_user(mimetype, addr, MAX_URI_LEN);
855         if (ret <= 0)
856                 GOTO_ERR_no_unlock;
857         mimetype[ret] = 0;
858         Dprintk("got MIME type: %s.\n", mimetype);
859        ret = strncpy_from_user(expires, u_info->cache_control, MAX_URI_LEN);
860        if (ret >= 0)
861                 expires[ret] = 0;
862         else
863                 expires[0] = 0;
864        Dprintk("got expires header: %s.\n", expires);
865
866         add_mimetype(extension, mimetype, expires);
867         ret = 0;
868 err_no_unlock:
869         return ret;
870 }
871
872 void user_send_buffer (tux_req_t *req, int cachemiss)
873 {
874         int ret;
875
876
877         SET_TIMESTAMP(req->output_timestamp);
878
879 repeat:
880         ret = send_sync_buf(req, req->sock, req->userbuf, req->userlen, MSG_DONTWAIT | MSG_MORE);
881         switch (ret) {
882                 case -EAGAIN:
883                         add_tux_atom(req, user_send_buffer);
884                         if (add_output_space_event(req, req->sock)) {
885                                 del_tux_atom(req);
886                                 goto repeat;
887                         }
888                         INC_STAT(user_sendbuf_write_misses);
889                         break;
890                 default:
891                         if (ret <= 0) {
892                                 req_err(req);
893                                 req->usermode = 0;
894                                 req->private = 0;
895                                 add_req_to_workqueue(req);
896                                 break;
897                         }
898                         req->userbuf += ret;
899                         req->userlen -= ret;
900                         if ((int)req->userlen < 0)
901                                 TUX_BUG();
902                         if (req->userlen)
903                                 goto repeat;
904                         add_req_to_workqueue(req);
905                         break;
906         }
907 }
908
909 void user_send_object (tux_req_t *req, int cachemiss)
910 {
911         int ret;
912
913
914         SET_TIMESTAMP(req->output_timestamp);
915
916 repeat:
917         ret = generic_send_file(req, req->sock, cachemiss);
918         switch (ret) {
919                 case -5:
920                         add_tux_atom(req, user_send_object);
921                         output_timeout(req);
922                         break;
923                 case -4:
924                         add_tux_atom(req, user_send_object);
925                         if (add_output_space_event(req, req->sock)) {
926                                 del_tux_atom(req);
927                                 goto repeat;
928                         }
929                         INC_STAT(user_sendobject_write_misses);
930                         break;
931                 case -3:
932                         INC_STAT(user_sendobject_cachemisses);
933                         add_tux_atom(req, user_send_object);
934                         queue_cachemiss(req);
935                         break;
936                 case -1:
937                         break;
938                 default:
939                         req->in_file->f_pos = 0;
940                         add_req_to_workqueue(req);
941                         break;
942         }
943 }
944
945 void user_get_object (tux_req_t *req, int cachemiss)
946 {
947         int missed;
948
949         if (!req->dentry) {
950                 req->usermode = 0;
951                 missed = lookup_object(req, cachemiss ? 0 : LOOKUP_ATOMIC);
952                 if (req->usermode)
953                         TUX_BUG();
954                 req->usermode = 1;
955                 if (!missed && !req->dentry) {
956                         req->error = 0;
957                         req->user_error = -ENOENT;
958                         add_req_to_workqueue(req);
959                         return;
960                 }
961                 if (missed) {
962                         if (cachemiss)
963                                 TUX_BUG();
964                         INC_STAT(user_lookup_cachemisses);
965 fetch_missed:
966                         req->ti->userspace_req = NULL;
967                         DEC_STAT(nr_userspace_pending);
968                         add_tux_atom(req, user_get_object);
969                         queue_cachemiss(req);
970                         return;
971                 }
972         }
973         req->total_file_len = req->dentry->d_inode->i_size;
974         if (!req->output_len)
975                 req->output_len = req->total_file_len;
976         if (tux_fetch_file(req, !cachemiss)) {
977                 INC_STAT(user_fetch_cachemisses);
978                 goto fetch_missed;
979         }
980         req->in_file->f_pos = 0;
981         add_req_to_workqueue(req);
982 }
983
984 asmlinkage long __sys_tux (unsigned int action, user_req_t *u_info)
985 {
986         int ret = -1;
987         threadinfo_t *ti;
988         tux_req_t *req;
989
990         if (action != TUX_ACTION_CURRENT_DATE)
991                 Dprintk("got sys_tux(%d, %p).\n", action, u_info);
992
993         if (action >= MAX_TUX_ACTION)
994                 GOTO_ERR_no_unlock;
995
996         ti = (threadinfo_t *) current->tux_info;
997         if (ti)
998                 if (ti->thread != current)
999                         TUX_BUG();
1000
1001         if (!capable(CAP_SYS_ADMIN)
1002                         && (action != TUX_ACTION_CONTINUE_REQ) &&
1003                                 (action != TUX_ACTION_STOPTHREAD))
1004                 goto userspace_actions;
1005
1006         switch (action) {
1007                 case TUX_ACTION_CONTINUE_REQ:
1008                         ret = continue_request((int)(long)u_info);
1009                         goto out;
1010
1011                 case TUX_ACTION_STARTUP:
1012                         lock_kernel();
1013                         ret = user_req_startup();
1014                         unlock_kernel();
1015                         goto out;
1016
1017                 case TUX_ACTION_SHUTDOWN:
1018                         lock_kernel();
1019                         ret = user_req_shutdown();
1020                         unlock_kernel();
1021                         goto out;
1022
1023                 case TUX_ACTION_REGISTER_MODULE:
1024                         ret = user_register_module(u_info);
1025                         goto out;
1026
1027                 case TUX_ACTION_UNREGISTER_MODULE:
1028                         ret = user_unregister_module(u_info);
1029                         goto out;
1030
1031                 case TUX_ACTION_STARTTHREAD:
1032                 {
1033                         unsigned int nr;
1034
1035                         ret = copy_from_user(&nr, &u_info->thread_nr,
1036                                                 sizeof(int));
1037                         if (ret)
1038                                 GOTO_ERR_no_unlock;
1039                         if (nr >= nr_tux_threads)
1040                                 GOTO_ERR_no_unlock;
1041                         ti = threadinfo + nr;
1042                         if (ti->started)
1043                                 GOTO_ERR_unlock;
1044                         ti->started = 1;
1045                         current->tux_info = ti;
1046                         current->tux_exit = tux_exit;
1047                         if (ti->thread)
1048                                 TUX_BUG();
1049                         Dprintk("TUX: current open files limit for TUX%d: %ld.\n", nr, current->signal->rlim[RLIMIT_NOFILE].rlim_cur);
1050                         lock_kernel();
1051                         ret = user_req_start_thread(ti);
1052                         unlock_kernel();
1053                         if (ret) {
1054                                 current->tux_info = NULL;
1055                                 current->tux_exit = NULL;
1056                         } else {
1057                                 if (ti->thread != current)
1058                                         TUX_BUG();
1059                         }
1060                         goto out_userreq;
1061                 }
1062
1063                 case TUX_ACTION_STOPTHREAD:
1064                         if (!ti)
1065                                 GOTO_ERR_no_unlock;
1066                         if (!ti->started)
1067                                 GOTO_ERR_unlock;
1068                         req = ti->userspace_req;
1069                         if (req)
1070                                 zap_userspace_req(req);
1071
1072                         lock_kernel();
1073                         ret = user_req_stop_thread(ti);
1074                         unlock_kernel();
1075                         goto out_userreq;
1076
1077                 case TUX_ACTION_CURRENT_DATE:
1078                         ret = strncpy_from_user(tux_date, u_info->new_date,
1079                                 DATE_LEN);
1080                         if (ret <= 0)
1081                                 GOTO_ERR_no_unlock;
1082                         goto out;
1083
1084                 case TUX_ACTION_REGISTER_MIMETYPE:
1085                         ret = register_mimetype(u_info);
1086                         if (ret)
1087                                 GOTO_ERR_no_unlock;
1088                         goto out;
1089
1090                 case TUX_ACTION_QUERY_VERSION:
1091                         ret = (TUX_MAJOR_VERSION << 24) | (TUX_MINOR_VERSION << 16) | TUX_PATCHLEVEL_VERSION;
1092                         goto out;
1093                 default:
1094                         ;
1095         }
1096
1097 userspace_actions:
1098
1099         if (!ti)
1100                 GOTO_ERR_no_unlock;
1101
1102         if (!ti->started)
1103                 GOTO_ERR_unlock;
1104
1105         req = ti->userspace_req;
1106         if (!req) {
1107                 if (action == TUX_ACTION_EVENTLOOP)
1108                         goto eventloop;
1109                 GOTO_ERR_unlock;
1110         }
1111         if (!req->usermode)
1112                 TUX_BUG();
1113
1114         ret = copy_from_user(&req->event, &u_info->event, sizeof(int));
1115         if (ret)
1116                 GOTO_ERR_unlock;
1117         ret = copy_from_user(&req->status, &u_info->http_status, sizeof(int));
1118         if (ret)
1119                 GOTO_ERR_unlock;
1120         ret = copy_from_user(&req->bytes_sent, &u_info->bytes_sent, sizeof(int));
1121         if (ret)
1122                 GOTO_ERR_unlock;
1123         ret = copy_from_user(&req->private, &u_info->priv, sizeof(req->private));
1124         if (ret)
1125                 GOTO_ERR_unlock;
1126
1127         switch (action) {
1128
1129                 case TUX_ACTION_EVENTLOOP:
1130 eventloop:
1131                         req = ti->userspace_req;
1132                         if (req)
1133                                 zap_userspace_req(req);
1134                         ret = event_loop(ti);
1135                         goto out_userreq;
1136
1137                 /*
1138                  * Module forces keepalive off, server will close
1139                  * the connection.
1140                  */
1141                 case TUX_ACTION_FINISH_CLOSE_REQ:
1142                         clear_keepalive(req);
1143
1144                 case TUX_ACTION_FINISH_REQ:
1145                         finish_userspace_req(req);
1146                         goto eventloop;
1147
1148                 case TUX_ACTION_REDIRECT_REQ:
1149
1150                         ti->userspace_req = NULL;
1151                         req->usermode = 0;
1152                         req->private = 0;
1153                         req->error = TUX_ERROR_REDIRECT;
1154                         DEC_STAT(nr_userspace_pending);
1155                         add_tux_atom(req, redirect_request);
1156                         add_req_to_workqueue(req);
1157
1158                         goto eventloop;
1159
1160                 case TUX_ACTION_POSTPONE_REQ:
1161
1162                         postpone_request(req);
1163                         ti->userspace_req = NULL;
1164                         ret = TUX_RETURN_USERSPACE_REQUEST;
1165                         break;
1166
1167                 case TUX_ACTION_GET_OBJECT:
1168                         release_req_dentry(req);
1169                         ret = strncpy_from_user(req->objectname,
1170                                 u_info->objectname, MAX_URI_LEN-1);
1171                         if (ret <= 0) {
1172                                 req->objectname[0] = 0;
1173                                 req->objectname_len = 0;
1174                                 GOTO_ERR_unlock;
1175                         }
1176                         req->objectname[ret] = 0; // string delimit
1177                         req->objectname_len = ret;
1178
1179                         Dprintk("got objectname {%s} (%d) from user-space req %p (req: %p).\n", req->objectname, req->objectname_len, u_info, req);
1180                         req->ti->userspace_req = NULL;
1181                         DEC_STAT(nr_userspace_pending);
1182                         user_get_object(req, 0);
1183                         goto eventloop;
1184
1185                 case TUX_ACTION_READ_OBJECT:
1186                 {
1187                         u64 u_addr;
1188                         char *addr;
1189                         loff_t ppos = 0;
1190                         struct file *filp;
1191
1192                         if (!req->dentry)
1193                                 GOTO_ERR_unlock;
1194
1195                         ret = copy_from_user(&u_addr, &u_info->object_addr,
1196                                         sizeof(u_addr));
1197                         if (ret)
1198                                 GOTO_ERR_unlock;
1199                         addr = (char *)(unsigned long)u_addr;
1200                         filp = dentry_open(req->dentry, NULL, O_RDONLY);
1201                         dget(req->dentry);
1202                         generic_file_read(filp, addr, req->total_file_len, &ppos);
1203                         fput(filp);
1204                         ret = TUX_RETURN_USERSPACE_REQUEST;
1205                         break;
1206                 }
1207
1208                 case TUX_ACTION_SEND_OBJECT:
1209                         if (!req->dentry)
1210                                 GOTO_ERR_unlock;
1211                         req->ti->userspace_req = NULL;
1212                         DEC_STAT(nr_userspace_pending);
1213                         user_send_object(req, 0);
1214                         goto eventloop;
1215
1216                 case TUX_ACTION_SEND_BUFFER:
1217                 {
1218                         u64 u_addr;
1219                         char *addr;
1220                         unsigned int len;
1221
1222                         ret = copy_from_user(&u_addr,
1223                                         &u_info->object_addr, sizeof(u_addr));
1224                         if (ret)
1225                                 GOTO_ERR_unlock;
1226                         addr = (char *)(unsigned long)u_addr;
1227                         ret = copy_from_user(&len,
1228                                         &u_info->objectlen, sizeof(addr));
1229                         if (ret)
1230                                 GOTO_ERR_unlock;
1231                         if ((int)len <= 0)
1232                                 GOTO_ERR_unlock;
1233
1234                         ret = -EFAULT;
1235                         if (!access_ok(VERIFY_READ, addr, len))
1236                                 GOTO_ERR_unlock;
1237                         req->userbuf = addr;
1238                         req->userlen = len;
1239
1240                         req->ti->userspace_req = NULL;
1241                         DEC_STAT(nr_userspace_pending);
1242                         user_send_buffer(req, 0);
1243                         ret = 0;
1244                         goto eventloop;
1245                 }
1246
1247                 case TUX_ACTION_READ_HEADERS:
1248                 {
1249                         char *addr;
1250                         u64 u_addr;
1251
1252                         ret = copy_from_user(&u_addr, &u_info->object_addr,
1253                                         sizeof(u_addr));
1254                         if (ret)
1255                                 GOTO_ERR_unlock;
1256                         addr = (char *)(unsigned long)u_addr;
1257                         ret = copy_to_user(&u_info->objectlen,
1258                                  &req->headers_len, sizeof(req->headers_len));
1259                         if (ret)
1260                                 GOTO_ERR_unlock;
1261                         ret = copy_to_user(addr,req->headers, req->headers_len);
1262                         if (ret)
1263                                 GOTO_ERR_unlock;
1264                         break;
1265                 }
1266
1267                 case TUX_ACTION_READ_POST_DATA:
1268                 {
1269                         char *addr;
1270                         unsigned int size;
1271                         u64 u_addr;
1272
1273                         ret = copy_from_user(&u_addr, &u_info->object_addr,
1274                                         sizeof(u_addr));
1275                         if (ret)
1276                                 GOTO_ERR_unlock;
1277                         addr = (char *)(unsigned long)u_addr;
1278
1279                         ret = copy_from_user(&size, &u_info->objectlen,
1280                                         sizeof(size));
1281                         if (ret)
1282                                 GOTO_ERR_unlock;
1283                         Dprintk("READ_POST_DATA: got %p(%d).\n", addr, size);
1284                         if (req->post_data_len < size)
1285                                 size = req->post_data_len;
1286                         Dprintk("READ_POST_DATA: writing %d.\n", size);
1287                         ret = copy_to_user(&u_info->objectlen,
1288                                                 &size, sizeof(size));
1289                         if (ret)
1290                                 GOTO_ERR_unlock;
1291                         ret = copy_to_user(addr, req->post_data_str, size);
1292                         if (ret)
1293                                 GOTO_ERR_unlock;
1294                         goto out;
1295                 }
1296
1297                 case TUX_ACTION_WATCH_PROXY_SOCKET:
1298                 {
1299                         struct socket *sock;
1300                         int err;
1301                         long fd;
1302                         u64 u_addr;
1303
1304                         ret = copy_from_user(&u_addr, &u_info->object_addr,
1305                                         sizeof(u_addr));
1306                         if (ret)
1307                                 GOTO_ERR_unlock;
1308                         fd = (int)(unsigned long)u_addr;
1309
1310                         sock = sockfd_lookup(fd, &err);
1311                         if (!sock)
1312                                 GOTO_ERR_unlock;
1313                         put_data_sock(req);
1314                         link_tux_data_socket(req, sock);
1315
1316                         ret = 0;
1317                         goto out;
1318                 }
1319
1320                 case TUX_ACTION_WAIT_PROXY_SOCKET:
1321                 {
1322                         if (!req->data_sock)
1323                                 GOTO_ERR_unlock;
1324                         if (socket_input(req->data_sock)) {
1325                                 ret = TUX_RETURN_USERSPACE_REQUEST;
1326                                 goto out_userreq;
1327                         }
1328                         spin_lock_irq(&req->ti->work_lock);
1329                         add_keepalive_timer(req);
1330                         if (test_and_set_bit(0, &req->idle_input))
1331                                 TUX_BUG();
1332                         spin_unlock_irq(&req->ti->work_lock);
1333                         if (socket_input(req->data_sock)) {
1334                                 unidle_req(req);
1335                                 ret = TUX_RETURN_USERSPACE_REQUEST;
1336                                 goto out_userreq;
1337                         }
1338                         req->ti->userspace_req = NULL;
1339                         goto eventloop;
1340                 }
1341
1342                 default:
1343                         GOTO_ERR_unlock;
1344         }
1345
1346 out_userreq:
1347         req = ti->userspace_req;
1348         if (req) {
1349                 ret = prepare_userspace_req(ti, u_info);
1350                 if (ret < 0) {
1351                         TDprintk("hm, user req %p returned %d, zapping.\n",
1352                                 req, ret);
1353                         zap_userspace_req(req);
1354                         goto eventloop;
1355                 }
1356         }
1357 out:
1358         if (action != TUX_ACTION_CURRENT_DATE)
1359                 Dprintk("sys_tux(%d, %p) returning %d.\n", action, u_info, ret);
1360         while (unlikely(test_thread_flag(TIF_NEED_RESCHED))) {
1361                 __set_task_state(current, TASK_RUNNING);
1362                 schedule();
1363         }
1364         return ret;
1365 err_unlock:
1366 err_no_unlock:
1367         Dprintk("sys_tux(%d, %p) returning -EINVAL (ret:%d)!\n", action, u_info, ret);
1368         while (unlikely(test_thread_flag(TIF_NEED_RESCHED))) {
1369                 __set_task_state(current, TASK_RUNNING);
1370                 schedule();
1371         }
1372         return -EINVAL;
1373 }
1374
1375 /*
1376  * This gets called if a TUX thread does an exit().
1377  */
1378 void tux_exit (void)
1379 {
1380         __sys_tux(TUX_ACTION_STOPTHREAD, NULL);
1381 }
1382
1383 int tux_init(void)
1384 {
1385         if (init_tux_request_slabs())
1386                 return -ENOMEM;
1387
1388         start_sysctl();
1389
1390 #ifdef CONFIG_TUX_MODULE
1391         spin_lock(&tux_module_lock);
1392         sys_tux_ptr = __sys_tux;
1393         tux_module = THIS_MODULE;
1394         spin_unlock(&tux_module_lock);
1395 #endif
1396
1397         return 0;
1398 }
1399
1400 void tux_cleanup (void)
1401 {
1402 #ifdef CONFIG_TUX_MODULE
1403         spin_lock(&tux_module_lock);
1404         tux_module = NULL;
1405         sys_tux_ptr = NULL;
1406         spin_unlock(&tux_module_lock);
1407 #endif
1408         end_sysctl();
1409
1410         free_tux_request_slabs();
1411 }
1412
1413 module_init(tux_init)
1414 module_exit(tux_cleanup)
1415
1416 MODULE_LICENSE("GPL");
1417