5 * TUX - Integrated Application Protocols Layer and Object Cache
7 * Copyright (C) 2000, 2001, Ingo Molnar <mingo@redhat.com>
9 * tux.h: main structure definitions and function prototypes
12 #define __KERNEL_SYSCALLS__
15 #include <linux/net.h>
16 #include <linux/wait.h>
17 #include <linux/namei.h>
18 #include <linux/file.h>
19 #include <linux/mman.h>
20 #include <linux/swap.h>
21 #include <linux/ctype.h>
22 #include <linux/config.h>
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/unistd.h>
26 #include <linux/sysctl.h>
27 #include <linux/proc_fs.h>
28 #include <linux/pagemap.h>
29 #include <linux/vmalloc.h>
30 #include <linux/utsname.h>
31 #include <linux/smp_lock.h>
32 #include <linux/kernel_stat.h>
33 #include <linux/kernel_stat.h>
34 #include <linux/time.h>
35 #include <asm/div64.h>
36 #include <asm/unaligned.h>
37 #include <linux/compiler.h>
38 #include <linux/mount.h>
39 #include <linux/zlib.h>
40 #include <linux/syscalls.h>
41 #include <linux/cpumask.h>
44 #include <net/tux_u.h>
46 /* Maximum number of threads: */
47 #define CONFIG_TUX_NUMTHREADS 16
49 /* Number of cachemiss/IO threads: */
50 #define NR_IO_THREADS 64
52 /* Maximum number of listen sockets per thread: */
53 #define CONFIG_TUX_NUMSOCKETS 16
55 extern spinlock_t tux_module_lock;
56 extern struct module *tux_module;
57 extern asmlinkage long (*sys_tux_ptr) (unsigned int action, user_req_t *u_info);
61 extern int tux_TDprintk;
62 extern int tux_Dprintk;
64 #ifdef CONFIG_TUX_DEBUG
65 # define TUX_BUG() BUG()
67 # define TUX_DPRINTK 1
68 # define TDprintk(x...) do { if (tux_TDprintk) { printk("<%ld:%s:%d>: ", jiffies, __FILE__, __LINE__); printk(x); } } while (0)
69 # define Dprintk(x...) do { if (tux_Dprintk == 1) TDprintk(x); } while (0)
71 # define TUX_DPRINTK 0
72 # define Dprintk(x...) do { } while (0)
73 # define TDprintk(x...) do { } while (0)
74 //# define TUX_BUG() BUG()
75 # define TUX_BUG() do { } while (0)
79 # define INC_STAT(x) do { } while (0)
80 # define DEC_STAT(x) do { } while (0)
81 # define ADD_STAT(x,y) do { } while (0)
82 # define SUB_STAT(x,y) do { } while (0)
84 # define INC_STAT(x) atomic_inc((atomic_t *)&kstat.x)
85 # define DEC_STAT(x) atomic_dec((atomic_t *)&kstat.x)
86 # define ADD_STAT(y,x) atomic_add(y,(atomic_t *)&kstat.x)
87 # define SUB_STAT(y,x) atomic_sub(y,(atomic_t *)&kstat.x)
92 # define DEBUG_DEL_LIST(x...) do { INIT_LIST_HEAD((x)); } while (0)
95 #define LOG_LEN (8*1024*1024UL)
97 struct tux_req_struct;
98 typedef struct tux_req_struct tux_req_t;
99 typedef struct tux_threadinfo threadinfo_t;
101 extern struct address_space_operations url_aops;
103 typedef struct tcapi_template_s {
105 struct list_head modules;
106 int (*query) (tux_req_t *req);
108 unsigned int userspace_id;
111 typedef struct mimetype_s {
112 struct list_head list;
115 unsigned int ext_len;
117 unsigned int type_len;
119 unsigned int expire_str_len;
121 unsigned int special;
124 typedef struct tux_attribute_s {
126 tcapi_template_t *tcapi;
129 #define MAX_TUX_ATOMS 8
131 typedef void (atom_func_t)(tux_req_t *req, int cachemiss);
133 typedef struct tux_proto_s
135 unsigned int defer_accept;
136 unsigned int can_redirect;
137 void (*got_request) (tux_req_t *req);
138 int (*parse_message) (tux_req_t *req, const int total_len);
139 atom_func_t *illegal_request;
140 atom_func_t *request_timeout;
141 void (*pre_log) (tux_req_t *req);
142 int (*check_req_err) (tux_req_t *req, int cachemiss);
143 char * (*print_dir_line) (tux_req_t *req, char *tmp, char *d_name, int d_len, int d_type, struct dentry *dentry, struct inode *inode);
145 struct nameidata main_docroot;
148 typedef struct tux_socket_s {
152 struct proc_dir_entry *entry;
155 extern tux_socket_t tux_listen [CONFIG_TUX_NUMTHREADS][CONFIG_TUX_NUMSOCKETS];
158 typedef struct abuf_s {
162 unsigned int max_len;
168 struct linux_dirent64 {
171 unsigned short d_reclen;
172 unsigned char d_type;
176 struct getdents_callback64 {
177 struct linux_dirent64 * current_dir;
178 struct linux_dirent64 * previous;
183 #define TUX_MAGIC 0x12457801
185 #define MAX_TUX_ATOMS 8
187 struct tux_req_struct
192 atom_func_t *atoms [MAX_TUX_ATOMS];
193 struct list_head work;
195 struct list_head all;
196 struct list_head free;
197 struct list_head lru;
199 unsigned long idle_input;
200 unsigned long wait_output_space;
203 struct dentry *dentry;
204 struct vfsmount *mnt;
205 struct dentry *docroot_dentry;
206 struct vfsmount *docroot_mnt;
207 struct dentry *cwd_dentry;
208 struct vfsmount *cwd_mnt;
210 struct file *in_file;
212 read_descriptor_t desc;
215 unsigned int virtual;
217 loff_t total_file_len;
218 unsigned int lendigits;
223 loff_t ftp_offset_start;
226 unsigned int etaglen;
230 unsigned int usermodule_idx;
231 struct dentry *module_dentry;
232 struct vfsmount *module_mnt;
234 unsigned int userlen;
236 tux_attribute_t *attr;
240 wait_queue_t ftp_sleep;
244 * Parsed request fields. In-line strings are zero-delimited.
247 unsigned int headers_len;
249 unsigned int parsed_len;
252 ftp_command_t ftp_command;
256 struct socket *data_sock;
257 unsigned int prev_pos;
260 struct linux_dirent64 *dirp0;
261 unsigned int curroff, total;
263 #define MAX_USERNAME_LEN 16
264 char username[MAX_USERNAME_LEN];
265 unsigned int username_len;
268 http_method_t method;
269 const char *method_str;
270 unsigned int method_len;
272 http_version_t version;
273 const char *version_str;
274 unsigned int version_len;
279 unsigned int uri_len;
281 /* Objectname (filename/scriptname) this URI refers to: */
283 #define MAX_OBJECTNAME_LEN 256
284 char objectname[MAX_OBJECTNAME_LEN + 4]; // space for .gz as well
285 unsigned int objectname_len;
287 /* Query string within the URI: */
289 const char *query_str;
290 unsigned int query_len;
294 const char *cookies_str;
295 unsigned int cookies_len;
296 unsigned int parse_cookies;
299 const char *content_type_str;
300 unsigned int content_type_len;
302 /* Content-Length: */
304 const char *contentlen_str;
305 unsigned int contentlen_len;
306 unsigned int content_len;
310 const char *user_agent_str;
311 unsigned int user_agent_len;
315 const char *accept_str;
316 unsigned int accept_len;
318 /* Accept-Charset: */
320 const char *accept_charset_str;
321 unsigned int accept_charset_len;
323 /* Accept-Language: */
325 const char *accept_language_str;
326 unsigned int accept_language_len;
330 const char *cache_control_str;
331 unsigned int cache_control_len;
333 /* If-Modified-Since: */
335 const char *if_modified_since_str;
336 unsigned int if_modified_since_len;
339 const char *if_none_match_str;
340 unsigned int if_none_match_len;
344 const char *if_range_str;
345 unsigned int if_range_len;
349 const char *negotiate_str;
350 unsigned int negotiate_len;
354 const char *pragma_str;
355 unsigned int pragma_len;
359 const char *referer_str;
360 unsigned int referer_len;
362 /* Accept-Encoding: */
364 const char *accept_encoding_str;
365 unsigned int accept_encoding_len;
366 unsigned int may_send_gzip;
367 unsigned int content_gzipped;
371 #define MAX_HOST_LEN 128
372 char host[MAX_HOST_LEN];
373 unsigned int host_len;
377 const char *post_data_str;
378 unsigned int post_data_len;
382 /* the file being sent */
384 unsigned int bytes_sent;
385 #ifdef CONFIG_TUX_DEBUG
386 unsigned int bytes_expected;
388 unsigned long first_timestamp;
389 unsigned int body_len;
391 unsigned int user_error;
401 struct timer_list keepalive_timer;
402 unsigned int total_bytes;
403 struct timer_list output_timer;
405 unsigned int nr_keepalives;
411 void (*real_data_ready)(struct sock *sk, int space);
412 void (*real_state_change)(struct sock *sk);
413 void (*real_write_space)(struct sock *sk);
414 void (*real_error_report)(struct sock *sk);
415 void (*real_destruct)(struct sock *sk);
417 void (*ftp_real_data_ready)(struct sock *sk, int space);
418 void (*ftp_real_state_change)(struct sock *sk);
419 void (*ftp_real_write_space)(struct sock *sk);
420 void (*ftp_real_error_report)(struct sock *sk);
421 void (*ftp_real_create_child)(struct sock *sk, struct sock *newsk);
422 void (*ftp_real_destruct)(struct sock *sk);
424 #ifdef CONFIG_TUX_EXTENDED_LOG
425 unsigned long accept_timestamp;
426 unsigned long parse_timestamp;
427 unsigned long output_timestamp;
428 unsigned long flush_timestamp;
429 # define SET_TIMESTAMP(x) do { (x) = jiffies; } while (0)
431 # define SET_TIMESTAMP(x) do { } while (0)
436 extern void add_tux_atom (tux_req_t *req, atom_func_t *event_done);
437 extern void del_tux_atom (tux_req_t *req);
438 extern void tux_schedule_atom (tux_req_t *req, int cachemiss);
439 extern void add_req_to_workqueue (tux_req_t *req);
442 typedef struct iothread_s
444 spinlock_t async_lock;
446 struct list_head async_queue;
447 wait_queue_head_t async_sleep;
448 unsigned int nr_async_pending;
449 unsigned int threads;
450 unsigned int shutdown;
451 wait_queue_head_t wait_shutdown;
454 typedef struct tux_listen_s
461 struct tux_threadinfo
463 tux_req_t *userspace_req;
464 unsigned int started;
465 struct task_struct *thread;
467 wait_queue_t wait_event [CONFIG_TUX_NUMSOCKETS];
471 struct page *header_cache;
472 unsigned int header_offset;
474 unsigned int nr_requests;
475 struct list_head all_requests;
477 unsigned int nr_free_requests;
478 spinlock_t free_requests_lock;
479 struct list_head free_requests;
481 spinlock_t work_lock;
482 struct list_head work_pending;
483 struct list_head lru;
486 unsigned int listen_error;
487 tux_listen_t listen[CONFIG_TUX_NUMSOCKETS];
489 struct semaphore gzip_sem;
493 unsigned int __padding[16];
496 typedef enum special_mimetypes {
501 } special_mimetypes_t;
503 #ifdef CONFIG_TUX_DEBUG
505 extern inline void url_hist_hit (int size)
507 unsigned int idx = size/1024;
509 if (idx >= URL_HIST_SIZE)
510 idx = URL_HIST_SIZE-1;
511 kstat.url_hist_hits[idx]++;
513 extern inline void url_hist_miss (int size)
515 unsigned int idx = size/1024;
517 if (idx >= URL_HIST_SIZE)
518 idx = URL_HIST_SIZE-1;
519 kstat.url_hist_misses[idx]++;
522 extern void __check_req_list (tux_req_t *req, struct list_head *list);
523 # define check_req_list __check_req_list
525 # define check_req_list(req, list) do { } while (0)
528 #define url_hist_hit(size) do { } while (0)
529 #define url_hist_miss(size) do { } while (0)
531 extern char tux_common_docroot[200];
532 extern char tux_http_subdocroot[200];
533 extern char tux_ftp_subdocroot[200];
534 extern char tux_logfile[200];
535 extern char tux_cgiroot[200];
536 extern char tux_404_page[200];
537 extern char tux_default_vhost[200];
538 extern char tux_extra_html_header[600];
539 extern unsigned int tux_extra_html_header_size;
540 extern int tux_cgi_uid;
541 extern int tux_cgi_gid;
542 extern unsigned int tux_clientport;
543 extern unsigned int tux_logging;
544 extern unsigned int tux_threads;
545 extern unsigned int tux_keepalive_timeout;
546 extern unsigned int tux_max_output_bandwidth;
547 extern unsigned int tux_max_backlog;
548 extern unsigned int tux_max_connect;
549 extern unsigned int tux_mode_forbidden;
550 extern unsigned int tux_mode_allowed;
551 extern unsigned int tux_logentry_align_order;
552 extern unsigned int tux_nonagle;
553 extern unsigned int tux_ack_pingpong;
554 extern unsigned int tux_push_all;
555 extern unsigned int tux_zerocopy_parse;
556 extern unsigned int tux_generate_etags;
557 extern unsigned int tux_generate_last_mod;
558 extern unsigned int tux_generate_cache_control;
559 extern unsigned int tux_ip_logging;
560 extern unsigned int tux_ftp_wait_close;
561 extern unsigned int tux_ftp_log_retr_only;
562 extern unsigned int tux_hide_unreadable;
564 typedef enum virtual_server {
571 extern unsigned int tux_virtual_server;
572 extern unsigned int mass_hosting_hash;
573 extern unsigned int strip_host_tail;
574 extern unsigned int tux_ftp_virtual_server;
576 extern unsigned int tux_max_object_size;
577 extern unsigned int tux_max_free_requests;
578 extern unsigned int tux_defer_accept;
580 extern struct socket * start_listening(tux_socket_t *listen, int nr);
581 extern void stop_listening(struct socket **sock);
582 extern void start_sysctl(void);
583 extern void end_sysctl(void);
584 extern void flush_request (tux_req_t *req, int cachemiss);
585 extern void unlink_tux_socket (tux_req_t *req);
586 extern void unlink_tux_data_socket (tux_req_t *req);
587 extern void unlink_tux_listen_socket (tux_req_t *req);
588 extern void link_tux_ftp_accept_socket (tux_req_t *req, struct socket *sock);
589 extern void link_tux_data_socket (tux_req_t *req, struct socket *sock);
590 extern void tux_push_req (tux_req_t *req);
591 extern int send_sync_buf (tux_req_t *req, struct socket *sock, const char *buf, const size_t length, unsigned long flags);
592 extern void __send_async_message (tux_req_t *req, const char *message, int status, unsigned int size, int push);
593 #define send_async_message(req,str,status,push) \
594 __send_async_message(req,str,status,strlen(str),push)
596 extern void send_success (tux_req_t *req, struct socket *sock);
597 extern void send_async_err_not_found (tux_req_t *req);
598 extern void send_async_timed_out (tux_req_t *req);
600 extern void kfree_req (tux_req_t *req);
601 extern int accept_requests (threadinfo_t *ti);
602 extern int process_requests (threadinfo_t *ti, tux_req_t **user_req);
603 extern int flush_freequeue (threadinfo_t * ti);
604 extern int tux_flush_workqueue (threadinfo_t *ti);
605 extern tux_req_t * pick_userspace_req (threadinfo_t *ti);
606 extern atom_func_t redirect_request;
607 extern atom_func_t parse_request;
608 extern void queue_cachemiss (tux_req_t *req);
609 extern int start_cachemiss_threads (threadinfo_t *ti);
610 extern void stop_cachemiss_threads (threadinfo_t *ti);
611 struct file * tux_open_file(char *filename, int mode);
612 extern void start_log_thread (void);
613 extern void stop_log_thread (void);
614 extern void add_mimetype (char *new_ext, char *new_type, char *new_expire);
615 extern void free_mimetypes (void);
616 extern int lookup_object (tux_req_t *req, const unsigned int flag);
617 extern int handle_gzip_req (tux_req_t *req, unsigned int flags);
618 extern struct dentry * tux_lookup (tux_req_t *req, const char *filename, const unsigned int flag, struct vfsmount **mnt);
619 extern tcapi_template_t * lookup_tuxmodule (const char *filename);
620 extern int register_tuxmodule (tcapi_template_t *tcapi);
621 extern tcapi_template_t * unregister_tuxmodule (char *vfs_name);
622 extern tcapi_template_t * get_first_usermodule (void);
623 extern int user_register_module (user_req_t *u_info);
624 extern int user_unregister_module (user_req_t *u_info);
625 extern void unregister_all_tuxmodules (void);
627 typedef struct exec_param_s {
631 unsigned int pipe_fds;
634 extern pid_t tux_exec_process (char *command, char **argv, char **envp, int pipe_fds, exec_param_t *param, int wait);
636 extern void start_external_cgi (tux_req_t *req);
637 extern tcapi_template_t extcgi_tcapi;
639 extern void queue_output_req (tux_req_t *req, threadinfo_t *ti);
640 extern void queue_userspace_req (tux_req_t *req, threadinfo_t *ti);
643 extern void __log_request (tux_req_t *req);
644 extern inline void log_request (tux_req_t *req)
650 extern int __connection_too_fast (tux_req_t *req);
652 #define connection_too_fast(req) \
655 if (unlikely(tux_max_output_bandwidth)) \
656 __ret = __connection_too_fast(req); \
660 extern void trunc_headers (tux_req_t *req);
661 extern int generic_send_file (tux_req_t *req, struct socket *sock, int cachemiss);
662 extern int tux_fetch_file (tux_req_t *req, int nonblock);
664 extern void postpone_request (tux_req_t *req);
665 extern int continue_request (int fd);
666 extern void tux_push_pending (struct sock *sk);
667 extern void zap_request (tux_req_t *req, int cachemiss);
668 extern int add_output_space_event (tux_req_t *req, struct socket *sock);
670 extern void reap_kids (void);
671 extern void unuse_frag (struct sk_buff *skb, skb_frag_t *frag);
672 extern skb_frag_t * build_dynbuf_frag (tux_req_t *req, unsigned int size);
673 extern int tux_permission (struct inode *inode);
674 extern void flush_all_signals (void);
676 #define D() Dprintk("{%s:%d}\n", __FILE__, __LINE__)
678 extern int nr_async_io_pending (void);
680 extern void __add_keepalive_timer (tux_req_t *req);
681 #define add_keepalive_timer(req) \
683 if (tux_keepalive_timeout) { \
684 Dprintk("add_keepalive_timer(%p).\n", (req)); \
685 __add_keepalive_timer(req); \
688 extern void __del_keepalive_timer (tux_req_t *req);
689 #define del_keepalive_timer(req) \
691 if (tux_keepalive_timeout) { \
692 Dprintk("del_keepalive_timer(%p).\n", (req)); \
693 __del_keepalive_timer(req); \
697 extern void del_output_timer (tux_req_t *req);
698 extern void output_timeout (tux_req_t *req);
700 extern void print_req (tux_req_t *req);
702 extern char tux_date [DATE_LEN];
705 extern int nr_async_io_pending (void);
706 extern void tux_exit (void);
707 extern char * get_abuf (tux_req_t *req, unsigned int max_size);
708 extern void send_abuf (tux_req_t *req, unsigned int size, unsigned long flags);
711 extern int idle_event (tux_req_t *req);
712 extern int output_space_event (tux_req_t *req);
713 extern cpumask_t tux_log_cpu_mask;
714 extern unsigned int tux_compression;
715 extern unsigned int tux_noid;
716 extern unsigned int tux_cgi_inherit_cpu;
717 extern unsigned int tux_zerocopy_header;
718 extern unsigned int tux_zerocopy_sendfile;
719 extern cpumask_t tux_cgi_cpu_mask;
720 extern tux_proto_t tux_proto_http;
721 extern tux_proto_t tux_proto_ftp;
722 extern unsigned int tux_all_userspace;
723 extern unsigned int tux_ignore_query;
724 extern unsigned int tux_redirect_logging;
725 extern unsigned int tux_referer_logging;
726 extern unsigned int tux_log_incomplete;
727 extern unsigned int tux_max_header_len;
728 extern unsigned int tux_cpu_offset;
729 extern unsigned int tux_ftp_login_message;
731 extern void drop_permissions (void);
732 extern int query_extcgi (tux_req_t *req);
733 extern int tux_chroot (char *dir);
735 extern void install_req_dentry (tux_req_t *req, struct dentry *dentry, struct vfsmount *mnt);
736 extern void release_req_dentry (tux_req_t *req);
737 extern void unidle_req (tux_req_t *req);
738 extern int nr_requests_used (void);
740 #define req_err(req) do { (req)->error = 1; Dprintk("request %p error at %s:%d.\n", req, __FILE__, __LINE__); } while (0)
742 #define enough_wspace(sk) (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
743 #define clear_keepalive(req) do { (req)->keep_alive = 0; Dprintk("keepalive cleared for req %p.\n", req); } while (0)
745 extern int print_all_requests (threadinfo_t *ti);
746 extern unsigned int tux_max_keepalives;
747 extern int time_unix2ls (time_t zulu, char *buf);
748 extern void last_mod_time(char * curr, const time_t t);
749 extern int mdtm_time(char * curr, const time_t t);
750 extern time_t parse_time(const char *str, const int str_len);
752 extern unsigned int nr_tux_threads;
753 extern threadinfo_t threadinfo[CONFIG_TUX_NUMTHREADS];
755 #define switch_docroot(req) do { if (((req)->docroot_dentry != current->fs->root) || ((req)->docroot_mnt != current->fs->rootmnt)) __switch_docroot(req); } while (0)
756 extern void __switch_docroot(tux_req_t *req);
757 extern void list_directory (tux_req_t *req, int cachemiss);
758 extern char * tux_print_path (tux_req_t *req, struct dentry *dentry, struct vfsmount *mnt, char *buf, unsigned int max_len);
760 extern unsigned int tux_http_dir_indexing;
762 int tux_gzip_compress (tux_req_t *req, unsigned char *data_in, unsigned char *data_out, __u32 *in_len, __u32 *out_len);
764 struct dentry * __tux_lookup (tux_req_t *req, const char *filename,
765 struct nameidata *base, struct vfsmount **mnt);
767 /* error codes for req->error */
768 #define TUX_ERROR_REDIRECT 1
769 #define TUX_ERROR_UNUSED 2
770 #define TUX_ERROR_CONN_CLOSE 3
771 #define TUX_ERROR_CONN_TIMEOUT 4
773 extern void __put_data_sock (tux_req_t *req);
775 static inline void put_data_sock (tux_req_t *req)
778 __put_data_sock(req);
781 #define socket_input(sock) \
782 (!skb_queue_empty(&(sock)->sk->sk_receive_queue) || \
783 !skb_queue_empty(&(sock)->sk->sk_error_queue))
785 #define tux_kmalloc(size) \
789 while (!(__ptr = kmalloc(size, GFP_KERNEL))) { \
790 if (net_ratelimit()) \
791 printk(KERN_WARNING "tux: OOM at %s:%d (%d bytes).\n", \
792 __FILE__, __LINE__, size); \
793 current->state = TASK_UNINTERRUPTIBLE; \
794 schedule_timeout(1); \
799 #define tux_close(fd) sys_close(fd)
801 extern int init_tux_request_slabs(void);
802 extern void free_tux_request_slabs(void);