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 / logger.c
1 /*
2  * TUX - Integrated Application Protocols Layer and Object Cache
3  *
4  * Copyright (C) 2000, 2001, Ingo Molnar <mingo@redhat.com>
5  *
6  * Cleaned up logger output for Alpha.
7  * -- Phil Ezolt (Phillip.Ezolt@compaq.com) & Bill Carr (wcarr92@yahoo.com)
8  *
9  * logger.c: log requests finished by TUX.
10  */
11
12 #define __KERNEL_SYSCALLS__
13 #include <net/tux.h>
14
15 /****************************************************************
16  *      This program is free software; you can redistribute it and/or modify
17  *      it under the terms of the GNU General Public License as published by
18  *      the Free Software Foundation; either version 2, or (at your option)
19  *      any later version.
20  *
21  *      This program is distributed in the hope that it will be useful,
22  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
23  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  *      GNU General Public License for more details.
25  *
26  *      You should have received a copy of the GNU General Public License
27  *      along with this program; if not, write to the Free Software
28  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29  *
30  ****************************************************************/
31
32 static DEFINE_SPINLOCK(log_lock);
33 static unsigned int log_head, log_tail;
34 static char * log_buffer = NULL;
35 static DECLARE_WAIT_QUEUE_HEAD(log_wait);
36 static DECLARE_WAIT_QUEUE_HEAD(log_full);
37 static int logger_pid = 0;
38
39 /*
40  * High-speed TUX logging architecture:
41  *
42  * All fast threads share a common log-ringbuffer. (default size 1MB)
43  * Log entries are binary and are padded to be cacheline aligned, this
44  * ensures that there is no cache-pingpong between fast threads.
45  *
46  * The logger thread writes out pending log entries within 1 second
47  * (buffer-cache writes data out within 5 seconds). The logger thread
48  * gets activated once we have more than 25% of the log ringbuffer
49  * filled - or the 1 second log timeout expires. Fast threads block
50  * if if more than 95% of the ringbuffer is filled and unblock only
51  * if used logbuffer space drops below 90%.
52  *
53  * This architecture guarantees that 1) logging is reliable (no
54  * log entry is ever lost), 2) timely (touches disk within 6 seconds),
55  * 3) in the log-contention case the saturation behavior is still
56  * write-clustered, but 4) if the logger thread can keep up then
57  * the coupling is completely asynchron and parallel.
58  *
59  * The binary log format gives us about 50% saved IO/memory bandwith
60  * and 50% less on-disk used log space than the traditional W3C ASCII
61  * format.
62  *
63  * (We might switch to raw IO though to write the logfile.)
64  */
65
66 #define SOFT_LIMIT              (LOG_LEN*25/100)
67 #define HARD_LIMIT              (LOG_LEN*95/100)
68 #define HARD_RELAX_LIMIT        (LOG_LEN*90/100)
69
70 unsigned int tux_logentry_align_order = 5;
71
72 #if SMP_CACHE_BYTES == 8
73 # define TUX_LOGENTRY_ALIGN 3
74 #else
75 #if SMP_CACHE_BYTES == 16
76 # define TUX_LOGENTRY_ALIGN 4
77 #else
78 #if SMP_CACHE_BYTES == 32
79 # define TUX_LOGENTRY_ALIGN 5
80 #else
81 #if SMP_CACHE_BYTES == 64
82 # define TUX_LOGENTRY_ALIGN 6
83 #else
84 #if SMP_CACHE_BYTES == 128
85 # define TUX_LOGENTRY_ALIGN 7
86 #else
87 #if SMP_CACHE_BYTES == 256
88 # define TUX_LOGENTRY_ALIGN 8
89 #else
90 #error Add entry!
91 #endif
92 #endif
93 #endif
94 #endif
95 #endif
96 #endif
97
98 #define ROUND_UP(x) (((((x)-1) >> TUX_LOGENTRY_ALIGN) + 1) \
99                                         << TUX_LOGENTRY_ALIGN)
100
101 static void __throttle_logging (void)
102 {
103         DECLARE_WAITQUEUE(wait, current);
104         int pending;
105
106         add_wait_queue(&log_full, &wait);
107         for (;;) {
108                 static unsigned long last_warning = 0;
109
110                 if (jiffies - last_warning > 10*HZ) {
111                         last_warning = jiffies;
112                         printk(KERN_NOTICE "TUX: log buffer overflow, have to throttle TUX thread!\n");
113                 }
114
115                 current->state = TASK_INTERRUPTIBLE;
116
117                 spin_lock(&log_lock);
118                 pending = log_head-log_tail;
119                 spin_unlock(&log_lock);
120
121                 if ((pending % LOG_LEN) < HARD_LIMIT)
122                         break;
123
124                 schedule();
125         }
126         current->state = TASK_RUNNING;
127         remove_wait_queue(&log_full, &wait);
128 }
129
130 #ifdef CONFIG_TUX_DEBUG
131 #define CHECK_LOGPTR(ptr) \
132 do { \
133         if ((ptr < log_buffer) || (ptr > log_buffer + LOG_LEN)) { \
134                 printk(KERN_ERR "TUX: ouch: log ptr %p > %p + %ld!\n", \
135                         ptr, log_buffer, LOG_LEN); \
136                 TUX_BUG(); \
137         } \
138 } while (0)
139 #else
140 #define CHECK_LOGPTR(ptr) do { } while (0)
141 #endif
142
143 void __log_request (tux_req_t *req)
144 {
145         char *str, *next;
146         const char *uri_str;
147         unsigned int inc, len, uri_len, pending, next_head, def_vhost_len = 0;
148         unsigned long flags;
149
150         if (req->proto->pre_log)
151                 req->proto->pre_log(req);
152         /*
153          * Log the reply status (success, or type of failure)
154          */
155         if (!tux_log_incomplete && (!req->status || (req->bytes_sent == -1))) {
156
157                 Dprintk("not logging req %p: {%s} [%d/%d]\n", req, req->uri_str, req->status, req->bytes_sent);
158                 return;
159         }
160         Dprintk("uri: {%s} [%d]\n", req->uri_str, req->uri_len);
161
162 #define NO_URI "<none>"
163         if (req->uri_len) {
164                 uri_len = req->uri_len;
165                 uri_str = req->uri_str;
166         } else {
167                 uri_str = NO_URI;
168                 uri_len = sizeof(NO_URI)-1;
169         }
170         len = uri_len + 1;
171
172         if (req->virtual) {
173                 if (req->host_len)
174                         len += req->host_len;
175                 else {
176                         def_vhost_len = strlen(tux_default_vhost);
177                         len += def_vhost_len;
178                 }
179         }
180
181         Dprintk("method_str: {%s} [%d]\n", req->method_str, req->method_len);
182         len += req->method_len + 1;
183
184         Dprintk("version_str: {%s} [%d]\n", req->version_str, req->version_len);
185         len += req->version_len + 1;
186
187 #ifdef CONFIG_TUX_EXTENDED_LOG
188         Dprintk("user_agent_str: {%s} [%d]\n", req->user_agent_str, req->user_agent_len);
189         len += req->user_agent_len + 1;
190 #endif
191         if (tux_referer_logging) {
192                 Dprintk("referer_str: {%s} [%d]\n", req->referer_str, req->referer_len);
193                 len += req->referer_len;
194         }
195         len++;
196
197         inc = 5*sizeof(u32) + len;
198 #ifdef CONFIG_TUX_EXTENDED_LOG
199         inc += 7*sizeof(u32);
200 #endif
201
202         spin_lock_irqsave(&log_lock, flags);
203
204         next_head = ROUND_UP(log_head + inc);
205
206         if (next_head < LOG_LEN) {
207                 str = log_buffer + log_head;
208                 if (str > log_buffer + LOG_LEN)
209                         TUX_BUG();
210                 log_head = next_head;
211         } else {
212                 if (log_head < LOG_LEN)
213                         memset(log_buffer+log_head, 0, LOG_LEN-log_head);
214                 str = log_buffer;
215                 log_head = ROUND_UP(inc);
216         }
217
218         if (str < log_buffer || str+inc >= log_buffer+LOG_LEN)
219                 TUX_BUG();
220
221         /*
222          * Log record signature - this makes finding the next entry
223          * easier (since record length is variable), and makes the
224          * binary logfile more robust against potential data corruption
225          * and other damage. The signature also servers as a log format
226          * version identifier.
227          */
228 #ifdef CONFIG_TUX_EXTENDED_LOG
229         *(u32 *)str = 0x2223beef;
230 #else
231         *(u32 *)str = 0x1112beef;
232 #endif
233         str += sizeof(u32);
234         CHECK_LOGPTR(str);
235
236         *(u32 *)str = 0;
237         /*
238          * Log the client IP address:
239          */
240         if (tux_ip_logging)
241                 *(u32 *)str = req->client_addr;
242         str += sizeof(u32);
243         CHECK_LOGPTR(str);
244
245 #ifdef CONFIG_TUX_EXTENDED_LOG
246         /*
247          * Log the client port number:
248          */
249         *(u32 *)str = 0;
250         if (tux_ip_logging)
251                 *(u32 *)str = req->client_port;
252         str += sizeof(u32);
253         CHECK_LOGPTR(str);
254 #endif
255
256         /*
257          * Log the request timestamp, in units of 'seconds since 1970'.
258          */
259         *(u32 *)str = CURRENT_TIME.tv_sec;
260         str += sizeof(u32);
261         CHECK_LOGPTR(str);
262
263 #ifdef CONFIG_TUX_EXTENDED_LOG
264         *(u32 *)str = req->accept_timestamp; str += sizeof(u32);
265         *(u32 *)str = req->parse_timestamp; str += sizeof(u32);
266         *(u32 *)str = req->output_timestamp; str += sizeof(u32);
267         *(u32 *)str = req->flush_timestamp; str += sizeof(u32);
268         *(u32 *)str = req->had_cachemiss; str += sizeof(u32);
269         *(u32 *)str = req->keep_alive; str += sizeof(u32);
270 #endif
271         /*
272          * Log the requested file size (in fact, log actual bytes sent.)
273          */
274         *(u32 *)str = req->bytes_sent;
275         str += sizeof(u32);
276         CHECK_LOGPTR(str);
277
278         *(u32 *)str = req->status;
279         str += sizeof(u32);
280         CHECK_LOGPTR(str);
281
282         /*
283          * Zero-terminated method, (base) URI, query and version string.
284          */
285         if (req->method_len) {
286                 memcpy(str, req->method_str, req->method_len);
287                 str += req->method_len;
288                 CHECK_LOGPTR(str);
289         }
290         *str++ = 0;
291
292         if (req->virtual) {
293                 if (req->host_len) {
294                         memcpy(str, req->host, req->host_len);
295                         str += req->host_len;
296                 } else {
297                         memcpy(str, tux_default_vhost, def_vhost_len);
298                         str += def_vhost_len;
299                 }
300                 CHECK_LOGPTR(str);
301         }
302
303         memcpy(str, uri_str, uri_len);
304         str += uri_len;
305         *str++ = 0;
306
307         CHECK_LOGPTR(str);
308
309         if (req->version_len) {
310                 memcpy(str, req->version_str, req->version_len);
311                 str += req->version_len;
312                 CHECK_LOGPTR(str);
313         }
314         *str++ = 0;
315 #ifdef CONFIG_TUX_EXTENDED_LOG
316         if (req->user_agent_len) {
317                 memcpy(str, req->user_agent_str, req->user_agent_len);
318                 str += req->user_agent_len;
319                 CHECK_LOGPTR(str);
320         }
321         *str++ = 0;
322 #endif
323         CHECK_LOGPTR(str);
324
325         if (tux_referer_logging && req->referer_len) {
326                 memcpy(str, req->referer_str, req->referer_len);
327                 str += req->referer_len;
328                 CHECK_LOGPTR(str);
329         }
330         *str++ = 0;
331         CHECK_LOGPTR(str);
332         /*
333          * pad with spaces to next cacheline, with an ending newline.
334          * (not needed for the user-space log utility, but results in
335          * a more readable binary log file, and reduces the amount
336          * of cache pingpong.)
337          */
338         next = (char *)ROUND_UP((unsigned long)str);
339
340         CHECK_LOGPTR(next);
341         len = next-str;
342         memset(str, ' ', len);
343
344         pending = (log_head-log_tail) % LOG_LEN;
345         spin_unlock_irqrestore(&log_lock, flags);
346
347         if (pending >= SOFT_LIMIT)
348                 wake_up(&log_wait);
349
350         if (pending >= HARD_LIMIT)
351                 __throttle_logging();
352 }
353
354 void tux_push_pending (struct sock *sk)
355 {
356         struct tcp_sock *tp = tcp_sk(sk);
357         struct inet_connection_sock *icsk = inet_csk(sk);
358
359         Dprintk("pushing pending frames on sock %p.\n", sk);
360         lock_sock(sk);
361         if ((sk->sk_state == TCP_ESTABLISHED) && !sk->sk_err) {
362                 icsk->icsk_ack.pingpong = tux_ack_pingpong;
363                 tp->nonagle = 1;
364                 __tcp_push_pending_frames(sk, tp, tcp_current_mss(sk, 0), TCP_NAGLE_OFF);
365         }
366         release_sock(sk);
367 }
368
369 inline void tux_push_req (tux_req_t *req)
370 {
371         if (req->sock)
372                 tux_push_pending(req->sock->sk);
373         if (req->data_sock)
374                 tux_push_pending(req->data_sock->sk);
375 }
376
377 void __put_data_sock (tux_req_t *req)
378 {
379         unlink_tux_data_socket(req);
380         if (req->data_sock->file)
381                 fput(req->data_sock->file);
382         else
383                 sock_release(req->data_sock);
384         req->data_sock = NULL;
385 }
386
387 void flush_request (tux_req_t *req, int cachemiss)
388 {
389         struct socket *sock;
390         struct sock *sk;
391         int keep_alive;
392
393         if (cachemiss)
394                 TUX_BUG();
395         __set_task_state(current, TASK_RUNNING);
396
397         if (req->magic != TUX_MAGIC)
398                 TUX_BUG();
399         if (req->ti->thread != current)
400                 TUX_BUG();
401 #ifdef CONFIG_TUX_DEBUG
402         if (req->bytes_expected && (req->bytes_sent != req->bytes_expected)) {
403                 printk("hm, bytes_expected: %d != bytes_sent: %d!\n",
404                         req->bytes_expected, req->bytes_sent);
405                 TUX_BUG();
406         }
407 #endif
408         SET_TIMESTAMP(req->flush_timestamp);
409
410         log_request(req);
411         sock = req->sock;
412         sk = NULL;
413         if (sock)
414                 sk = sock->sk;
415         Dprintk("FLUSHING req %p <%p> (sock %p, sk %p) (keepalive: %d, status: %d)\n", req, __builtin_return_address(0), sock, sk, req->keep_alive, req->status);
416         if (req->in_file->f_pos)
417                 /*TUX_BUG()*/;
418         release_req_dentry(req);
419         req->private = 0;
420
421         if (req->docroot_dentry) {
422                 dput(req->docroot_dentry);
423                 req->docroot_dentry = NULL;
424                 if (!req->docroot_mnt)
425                         TUX_BUG();
426         }
427         if (req->docroot_mnt) {
428                 mntput(req->docroot_mnt);
429                 req->docroot_mnt = NULL;
430         }
431
432         req->offset_start = 0;
433         req->offset_end = 0;
434         req->output_len = 0;
435         req->total_file_len = 0;
436         req->lendigits = 0;
437         req->mtime = 0;
438         req->etaglen = 0;
439         req->etag[0] = 0;
440         req->ftp_command = 0;
441
442         if (req->postponed)
443                 TUX_BUG();
444         if (test_bit(0, &req->idle_input))
445                 TUX_BUG();
446         if (test_bit(0, &req->wait_output_space))
447                 TUX_BUG();
448         if (req->parsed_len)
449                 trunc_headers(req);
450         if (req->parsed_len)
451                 TUX_BUG();
452         req->attr = NULL;
453         req->usermode = 0;
454         req->usermodule_idx = 0;
455         req->atom_idx = 0;
456         if (req->module_dentry) {
457                 dput(req->module_dentry);
458                 req->module_dentry = NULL;
459         }
460         if (req->headers)
461                 kfree(req->headers);
462         req->headers = NULL;
463         req->headers_len = 0;
464
465         req->method = METHOD_NONE;
466         req->method_len = 0;
467         req->method_str = NULL;
468         req->version = 0;
469         req->version_str = NULL;
470         req->version_len = 0;
471
472         req->uri_str = NULL;
473         req->uri_len = 0;
474
475         req->objectname[0] = 0;
476         req->objectname_len = 0;
477
478         req->query_str = NULL;
479         req->query_len = 0;
480
481         req->cookies_str = NULL;
482         req->cookies_len = 0;
483         req->parse_cookies = 0;
484
485         req->contentlen_str = NULL;
486         req->contentlen_len = 0;
487         req->content_len = 0;
488
489         req->user_agent_str = NULL;
490         req->user_agent_len = 0;
491
492         req->may_send_gzip = 0;
493         req->content_gzipped = 0;
494
495         req->content_type_str = NULL;
496         req->content_type_len = 0;
497
498         req->accept_str = NULL;
499         req->accept_len = 0;
500
501         req->accept_charset_str = NULL;
502         req->accept_charset_len = 0;
503
504         req->accept_encoding_str = NULL;
505         req->accept_encoding_len = 0;
506
507         req->accept_language_str = NULL;
508         req->accept_language_len = 0;
509
510         req->cache_control_str = NULL;
511         req->cache_control_len = 0;
512
513         req->if_modified_since_str = NULL;
514         req->if_modified_since_len = 0;
515
516         req->if_none_match_str = NULL;
517         req->if_none_match_len = 0;
518
519         req->if_range_str = NULL;
520         req->if_range_len = 0;
521
522         req->negotiate_str = NULL;
523         req->negotiate_len = 0;
524
525         req->pragma_str = NULL;
526         req->pragma_len = 0;
527
528         req->referer_str = NULL;
529         req->referer_len = 0;
530
531         req->post_data_str = NULL;
532         req->post_data_len = 0;
533
534         SET_TIMESTAMP(req->accept_timestamp);
535 #ifdef CONFIG_TUX_EXTENDED_LOG
536         req->parse_timestamp = 0;
537         req->output_timestamp = 0;
538         req->flush_timestamp = 0;
539 #endif
540         req->status = 0;
541
542         req->total_bytes += req->bytes_sent;
543         req->bytes_sent = 0;
544 #ifdef CONFIG_TUX_DEBUG
545         req->bytes_expected = 0;
546 #endif
547         req->body_len = 0;
548         keep_alive = req->keep_alive;
549         clear_keepalive(req);
550         req->had_cachemiss = 0;
551         // first_timestamp and total_bytes is kept!
552         req->event = 0;
553         req->lookup_dir = 0;
554         req->lookup_404 = 0;
555
556         req->error = 0;
557         req->user_error = 0;
558
559         if (req->abuf.page)
560                 __free_page(req->abuf.page);
561         memset(&req->abuf, 0, sizeof(req->abuf));
562
563         if (sk && keep_alive) {
564                 add_tux_atom(req, parse_request);
565                 if (skb_queue_empty(&sk->sk_receive_queue)) {
566                         spin_lock_irq(&req->ti->work_lock);
567                         add_keepalive_timer(req);
568                         if (test_and_set_bit(0, &req->idle_input))
569                                 TUX_BUG();
570                         /*
571                          * Avoid the race with the event callback:
572                          */
573                         if (skb_queue_empty(&sk->sk_receive_queue) ||
574                                    !test_and_clear_bit(0, &req->idle_input)) {
575                                 INC_STAT(nr_idle_input_pending);
576                                 spin_unlock_irq(&req->ti->work_lock);
577                                 tux_push_req(req);
578                                 goto out;
579                         }
580                         del_keepalive_timer(req);
581                         spin_unlock_irq(&req->ti->work_lock);
582                 }
583                 Dprintk("KEEPALIVE PENDING req %p <%p> (sock %p, sk %p) (keepalive: %d, status: %d)\n", req, __builtin_return_address(0), req->sock, req->sock->sk, req->keep_alive, req->status);
584                 add_req_to_workqueue(req);
585                 INC_STAT(nr_keepalive_optimized);
586                 goto out;
587         }
588
589         del_timer_sync(&req->keepalive_timer);
590         del_timer_sync(&req->output_timer);
591
592         if (timer_pending(&req->keepalive_timer))
593                 TUX_BUG();
594         if (timer_pending(&req->output_timer))
595                 TUX_BUG();
596         if (!list_empty(&req->lru))
597                 TUX_BUG();
598         req->nr_keepalives = 0;
599         req->client_addr = 0;
600         req->client_port = 0;
601         req->virtual = 0;
602         req->ftp_offset_start = 0;
603
604         req->host[0] = 0;
605         req->host_len = 0;
606
607         if (req->cwd_dentry) {
608                 dput(req->cwd_dentry);
609                 req->cwd_dentry = NULL;
610                 if (!req->cwd_mnt)
611                         TUX_BUG();
612         }
613         if (req->cwd_mnt) {
614                 mntput(req->cwd_mnt);
615                 req->cwd_mnt = NULL;
616         }
617         put_data_sock(req);
618         req->prev_pos = 0;
619         req->curroff = 0;
620         req->total = 0;
621         if (req->dirp0) {
622                 kfree(req->dirp0);
623                 req->dirp0 = NULL;
624         }
625
626         if (sk)
627                 unlink_tux_socket(req);
628         req->sock = NULL;
629         /*
630          * Close potential user-space file descriptors.
631          */
632         {
633                 int fd = req->fd, ret;
634
635                 if (fd != -1) {
636                         Dprintk("closing req->fd: %d\n", fd);
637                         req->fd = -1;
638                         ret = tux_close(fd);
639                         if (ret)
640                                 TUX_BUG();
641                 } else
642                         if (sock)
643                                 sock_release(sock);
644         }
645         kfree_req(req);
646 out:
647         ;
648 }
649
650 static int warn_once = 1;
651
652 static loff_t log_filp_last_index;
653
654 static unsigned int writeout_log (void)
655 {
656         unsigned int len, pending, next_log_tail;
657         mm_segment_t oldmm = get_fs();
658         struct file *log_filp;
659         char * str;
660         unsigned int ret;
661         struct inode *inode;
662         struct address_space *mapping;
663
664         if (tux_logging)
665                 Dprintk("TUX logger: opening log file {%s}.\n", tux_logfile);
666         log_filp = tux_open_file(tux_logfile, O_CREAT|O_APPEND|O_WRONLY|O_LARGEFILE);
667         if (!log_filp) {
668                 if (warn_once) {
669                         printk(KERN_ERR "TUX: could not open log file {%s}!\n",
670                                 tux_logfile);
671                         warn_once = 0;
672                 }
673                 __set_current_state(TASK_INTERRUPTIBLE);
674                 schedule_timeout(HZ);
675                 return 0;
676         }
677         spin_lock(&log_lock);
678         str = log_buffer + log_tail;
679         if (log_head < log_tail) {
680                 len = LOG_LEN-log_tail;
681                 next_log_tail = 0;
682         } else {
683                 len = log_head-log_tail;
684                 next_log_tail = log_head;
685         }
686         if (!len)
687                 goto out;
688         spin_unlock(&log_lock);
689
690         set_fs(KERNEL_DS);
691         ret = log_filp->f_op->write(log_filp, str, len, &log_filp->f_pos);
692         set_fs(oldmm);
693
694         if (len != ret) {
695                 if (ret == -ENOSPC) {
696                         printk(KERN_ERR "TUX: trying to write TUX logfile %s, but filesystem is full! Lost %d bytes of log data.\n", tux_logfile, len);
697                 } else {
698                         printk(KERN_ERR "TUX: log write %d != %d.\n", ret, len);
699                         printk(KERN_ERR "TUX: log_filp: %p, str: %p, len: %d str[len-1]: %d.\n", log_filp, str, len, str[len-1]);
700                 }
701                 goto out_lock;
702         }
703
704         /*
705          * Sync log data to disk:
706          */
707         inode = log_filp->f_dentry->d_inode;
708         mapping = inode->i_mapping;
709         if (mapping->nrpages > 256) {   /* batch stuff up */
710                 mutex_lock(&inode->i_mutex);
711                 filemap_fdatawrite(inode->i_mapping);
712
713                 /*
714                  * Now nuke old pagecache up to the place where we just
715                  * started the I/O.   There's no point in trying to invalidate
716                  * pages after that, because they're currently in-flight.
717                  */
718                 invalidate_mapping_pages(mapping, 0, log_filp_last_index);
719                 log_filp_last_index = log_filp->f_pos >> PAGE_CACHE_SHIFT;
720                 mutex_unlock(&inode->i_mutex);
721         }
722
723 out_lock:
724         spin_lock(&log_lock);
725 out:
726         log_tail = next_log_tail;
727         pending = (log_head-log_tail) % LOG_LEN;
728         spin_unlock(&log_lock);
729
730         if (pending < HARD_LIMIT)
731                 wake_up(&log_full);
732
733         fput(log_filp);
734         return pending;
735 }
736
737 static DECLARE_WAIT_QUEUE_HEAD(stop_logger_wait);
738 static int stop_logger = 0;
739
740 static int logger_thread (void *data)
741 {
742         DECLARE_WAITQUEUE(wait, current);
743         mm_segment_t oldmm;
744
745         daemonize("TUX logger");
746
747         oldmm = get_fs();
748         set_fs(KERNEL_DS);
749         printk(KERN_NOTICE "TUX: logger thread started.\n");
750 #ifdef CONFIG_SMP
751         {
752                 cpumask_t map;
753
754                 cpus_and(map, cpu_online_map, tux_log_cpu_mask);
755                 if (!(cpus_empty(map)))
756                         set_cpus_allowed(current, map);
757
758         }
759 #endif
760
761
762         spin_lock_irq(&current->sighand->siglock);
763         siginitsetinv(&current->blocked, 0);
764         recalc_sigpending();
765         spin_unlock_irq(&current->sighand->siglock);
766
767         if (log_buffer)
768                 TUX_BUG();
769         log_buffer = vmalloc(LOG_LEN);
770         if (!log_buffer) {
771                 TUX_BUG();
772                 goto out;
773         }
774         memset(log_buffer, 0, LOG_LEN);
775         log_head = log_tail = 0;
776
777         current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
778
779         add_wait_queue(&log_wait, &wait);
780         for (;;) {
781                 if (tux_logging)
782                         Dprintk("logger does writeout - stop:%d.\n", stop_logger);
783
784                 while (writeout_log() >= SOFT_LIMIT) {
785                         if (stop_logger)
786                                 break;
787                 }
788                 if (stop_logger)
789                         break;
790                         /* nothing */;
791
792                 if (tux_logging)
793                         Dprintk("logger does sleep - stop:%d.\n", stop_logger);
794                 __set_current_state(TASK_INTERRUPTIBLE);
795                 if (log_head != log_tail) {
796                         __set_current_state(TASK_RUNNING);
797                         continue;
798                 }
799                 schedule_timeout(HZ);
800                 if (tux_logging)
801                         Dprintk("logger back from sleep - stop:%d.\n", stop_logger);
802                 if (signal_pending(current))
803                         flush_all_signals();
804         }
805         remove_wait_queue(&log_wait, &wait);
806
807         vfree(log_buffer);
808         log_buffer = NULL;
809         stop_logger = 0;
810         wake_up(&stop_logger_wait);
811 out:
812         set_fs(oldmm);
813
814         return 0;
815 }
816
817 void start_log_thread (void)
818 {
819         warn_once = 1;
820
821         logger_pid = kernel_thread(logger_thread, NULL, 0);
822         if (logger_pid < 0)
823                 TUX_BUG();
824 }
825
826 void stop_log_thread (void)
827 {
828         DECLARE_WAITQUEUE(wait, current);
829
830         Dprintk("stopping logger thread %d ...\n", logger_pid);
831
832         __set_current_state(TASK_UNINTERRUPTIBLE);
833         add_wait_queue(&stop_logger_wait, &wait);
834         stop_logger = 1;
835         wake_up(&log_wait);
836         schedule();
837         __set_current_state(TASK_RUNNING);
838         remove_wait_queue(&stop_logger_wait, &wait);
839
840         Dprintk("logger thread stopped!\n");
841 }