Whamcloud - gitweb
6bef4c1b1e0294860bc87c6bf1ad5d501f1cb158
[fs/lustre-release.git] / libcfs / include / libcfs / winnt / winnt-prim.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/include/libcfs/winnt/winnt-prim.h
37  *
38  * Basic library routines.
39  */
40
41 #ifndef __LIBCFS_WINNT_CFS_PRIM_H__
42 #define __LIBCFS_WINNT_CFS_PRIM_H__
43
44 #ifndef __LIBCFS_LIBCFS_H__
45 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
46 #endif
47
48 /*
49  * libcfs proc device object
50  */
51
52
53 #define LUSTRE_PROC_DEVICE  L"\\Device\\LNetProcFS"      /* proc fs emulator device object */
54 #define LUSTRE_PROC_SYMLNK  L"\\DosDevices\\LNetProcFS"  /* proc fs user-visible device */
55
56
57 /*
58  * Device IO Control Code Definitions
59  */
60
61 #define FILE_DEVICE_LIBCFS      ('LC')
62
63 #define FUNC_LIBCFS_VERSION     0x101  // get version of current libcfs
64 #define FUNC_LIBCFS_IOCTL       0x102  // Device i/o control to proc fs
65
66
67 #define IOCTL_LIBCFS_VERSION \
68      CTL_CODE (FILE_DEVICE_LIBCFS, FUNC_LIBCFS_VERSION, METHOD_BUFFERED, FILE_ANY_ACCESS)
69 #define IOCTL_LIBCFS_ENTRY   \
70      CTL_CODE(FILE_DEVICE_LIBCFS, FUNC_LIBCFS_IOCTL,   METHOD_BUFFERED, FILE_ANY_ACCESS)
71
72 #pragma pack(4)
73 typedef struct _CFS_PROC_IOCTL {
74
75     ULONG           cmd;    // ioctl command identifier
76     ULONG           len;    // length of data
77     int             rc;     // return code
78     ULONG           usused; // unused 
79
80     // UCHAR        data[]; // content of the real ioctl
81
82 } CFS_PROC_IOCTL, *PCFS_PROC_IOCTL;
83 #pragma pack()
84
85 #ifdef __KERNEL__
86
87 void cfs_enter_debugger(void);
88 #define __builtin_return_address(x) (0)
89
90 /*
91  * Symbol functions for libcfs
92  *
93  * OSX has no facility for use to register symbol.
94  * So we have to implement it.
95  */
96 #define CFS_SYMBOL_LEN     64
97
98 struct  cfs_symbol {
99         char    name[CFS_SYMBOL_LEN];
100         void    *value;
101         int     ref;
102         struct  list_head sym_list;
103 };
104
105 extern int      cfs_symbol_register(const char *, const void *);
106 extern void     cfs_symbol_unregister(const char *);
107 extern void *   cfs_symbol_get(const char *);
108 extern void     cfs_symbol_put(const char *);
109 extern void     cfs_symbol_clean();
110
111 typedef struct file_operations cfs_file_operations_t;
112 typedef struct file cfs_file_t;
113
114 /*
115  * Pseudo device register
116  */
117
118 typedef struct
119 {
120     int                     minor;
121     const char *            name;
122     cfs_file_operations_t * fops;
123 } cfs_psdev_t;
124
125 int cfs_psdev_register(cfs_psdev_t * psdev);
126 int cfs_psdev_deregister(cfs_psdev_t * psdev);
127
128
129 /*
130  * Proc emulator file system APIs
131  */
132
133 typedef int cfs_read_proc_t(char *page, char **start, off_t off,
134                             int count, int *eof, void *data);
135 typedef int cfs_write_proc_t(struct file *file, const char *buffer,
136                              unsigned long count, void *data);
137
138 #define CFS_PROC_ENTRY_MAGIC 'CPEM'
139
140 #define CFS_PROC_FLAG_DIRECTORY    0x00000001 // directory node
141 #define CFS_PROC_FLAG_ATTACHED     0x00000002 // node is attached to proc
142 #define CFS_PROC_FLAG_MISCDEV      0x00000004 // miscellaneous device
143
144 typedef struct cfs_proc_entry
145 {
146     ULONG                   magic;      // Magic
147     ULONG                   flags;      // Flags
148
149     struct _dir_entry {                 // proc directory entry
150         PRTL_SPLAY_LINKS    root;
151     };
152
153     struct cfs_proc_entry  *parent;
154
155     struct _file_entry {                // proc file / leaf entry
156             cfs_read_proc_t  *  read_proc;
157             cfs_write_proc_t *  write_proc;
158     };
159
160     mode_t                  mode;
161     unsigned short          nlink;
162     BOOLEAN                 deleted;
163
164         
165     struct file_operations *proc_fops;
166     void                   *data;
167
168     // proc_dir_entry ended.
169
170     RTL_SPLAY_LINKS         s_link;       // splay link
171
172     //
173     // Maximum length of proc entry name is 0x20
174     //
175
176     char                    name[0x20];
177
178 } cfs_proc_entry_t, cfs_proc_dir_entry_t;
179
180 typedef cfs_proc_entry_t cfs_proc_dir_entry_t;
181 #define proc_dir_entry cfs_proc_entry
182
183 #define PROC_BLOCK_SIZE    PAGE_SIZE
184
185 struct proc_dir_entry *PDE(const struct inode *inode);
186
187
188 /*
189  * Sysctl register
190  */
191
192 typedef struct ctl_table                cfs_sysctl_table_t;
193 typedef struct ctl_table_header         cfs_sysctl_table_header_t;
194
195
196 typedef int ctl_handler (
197             cfs_sysctl_table_t *table,
198             int *name,  int nlen,
199             void *oldval, size_t *oldlenp,
200             void *newval, size_t newlen, 
201             void **context );
202
203 typedef int proc_handler (
204             cfs_sysctl_table_t *ctl,
205             int write, struct file * filp,
206             void *buffer, size_t *lenp );
207
208
209 int proc_dointvec(cfs_sysctl_table_t *table, int write, struct file *filp,
210                      void *buffer, size_t *lenp);
211
212 int proc_dostring(cfs_sysctl_table_t *table, int write, struct file *filp,
213                   void *buffer, size_t *lenp);
214
215 int sysctl_string(cfs_sysctl_table_t *table, int *name, int nlen,
216                   void *oldval, size_t *oldlenp,
217                   void *newval, size_t newlen, void **context);
218
219 /*
220  *  System io control definitions
221  */
222
223 #define CTL_MAXNAME 10
224
225 #define CTL_ANY     -1  /* Matches any name */
226 #define CTL_NONE    0
227
228 enum
229 {
230     CTL_KERN=1,     /* General kernel info and control */
231     CTL_VM=2,       /* VM management */
232     CTL_NET=3,      /* Networking */
233     CTL_PROC=4,     /* Process info */
234     CTL_FS=5,       /* Filesystems */
235     CTL_DEBUG=6,        /* Debugging */
236     CTL_DEV=7,      /* Devices */
237     CTL_BUS=8,      /* Busses */
238     CTL_ABI=9,      /* Binary emulation */
239     CTL_CPU=10      /* CPU stuff (speed scaling, etc) */
240 };
241
242 /* sysctl table definitons */
243 struct ctl_table 
244 {
245         int ctl_name;
246         char *procname;
247         void *data;
248         int maxlen;
249         mode_t mode;
250         cfs_sysctl_table_t *child;
251         proc_handler *proc_handler;     /* text formatting callback */
252         ctl_handler *strategy;          /* read / write callback functions */
253         cfs_proc_entry_t *de;   /* proc entry block */
254         void *extra1;
255         void *extra2;
256 };
257
258
259 /* the mantaner of the cfs_sysctl_table trees */
260 struct ctl_table_header
261 {
262         cfs_sysctl_table_t *    ctl_table;
263         struct list_head        ctl_entry;
264 };
265
266 /* proc root entries, support routines */
267 extern cfs_proc_entry_t *  cfs_proc_root;   /* / */
268 extern cfs_proc_entry_t *  cfs_proc_proc;   /* /proc */
269 extern cfs_proc_entry_t *  cfs_proc_fs;     /* /proc/fs */
270 extern cfs_proc_entry_t *  cfs_proc_sys;    /* /proc/sys */
271 extern cfs_proc_entry_t *  cfs_proc_dev;    /* /dev */
272
273 cfs_proc_entry_t * create_proc_entry(const char *name, mode_t mod,
274                                           cfs_proc_entry_t *parent);
275 void proc_free_entry(cfs_proc_entry_t *de);
276 void remove_proc_entry(const char *name, cfs_proc_entry_t *entry);
277 cfs_proc_entry_t * search_proc_entry(const char * name,
278                         cfs_proc_entry_t *  root );
279 cfs_proc_entry_t *proc_symlink(const char *name,
280                                        cfs_proc_entry_t *parent,
281                                const char *dest);
282 cfs_proc_entry_t *proc_mkdir(const char *name,
283                                      cfs_proc_entry_t *parent);
284
285 #define cfs_create_proc_entry create_proc_entry
286 #define cfs_free_proc_entry   proc_free_entry
287 #define cfs_remove_proc_entry remove_proc_entry
288
289 struct ctl_table_header *register_sysctl_table(cfs_sysctl_table_t * table,
290                                                int insert_at_head);
291 void unregister_sysctl_table(struct ctl_table_header * header);
292
293 #define cfs_register_sysctl_table(t, a)   register_sysctl_table(t, a)
294 #define cfs_unregister_sysctl_table(t)    unregister_sysctl_table(t)
295
296 /*
297  * seq device (linux/seq_file.h)
298  */
299
300
301 /*
302  * seq file definitions
303  */
304
305 struct dentry;
306 struct vfsmount;
307
308 struct path {
309         struct vfsmount *mnt;
310         struct dentry *dentry;
311 };
312
313 struct seq_operations;
314 struct file;
315 struct inode;
316
317 struct seq_file {
318         char *buf;
319         size_t size;
320         size_t from;
321         size_t count;
322         loff_t index;
323         u32    version;
324         mutex_t lock;
325         const struct seq_operations *op;
326         void *private;
327 };
328
329 struct seq_operations {
330         void * (*start) (struct seq_file *m, loff_t *pos);
331         void (*stop) (struct seq_file *m, void *v);
332         void * (*next) (struct seq_file *m, void *v, loff_t *pos);
333         int (*show) (struct seq_file *m, void *v);
334 };
335
336 int seq_open(struct file *, const struct seq_operations *);
337 ssize_t seq_read(struct file *, char __user *, size_t, loff_t *);
338 loff_t seq_lseek(struct file *, loff_t, int);
339 int seq_release(struct inode *, struct file *);
340 int seq_escape(struct seq_file *, const char *, const char *);
341 int seq_putc(struct seq_file *m, char c);
342 int seq_puts(struct seq_file *m, const char *s);
343
344 int seq_printf(struct seq_file *, const char *, ...)
345         __attribute__ ((format (printf,2,3)));
346
347 int seq_path(struct seq_file *, struct path *, char *);
348
349 int single_open(struct file *, int (*)(struct seq_file *, void *), void *);
350 int single_release(struct inode *, struct file *);
351 void *__seq_open_private(struct file *, const struct seq_operations *, int);
352 int seq_open_private(struct file *, const struct seq_operations *, int);
353 int seq_release_private(struct inode *, struct file *);
354
355 #define SEQ_START_TOKEN ((void *)1)
356
357 /*
358  * Helpers for iteration over list_head-s in seq_files
359  */
360
361 extern struct list_head *seq_list_start(struct list_head *head,
362                 loff_t pos);
363 extern struct list_head *seq_list_start_head(struct list_head *head,
364                 loff_t pos);
365 extern struct list_head *seq_list_next(void *v, struct list_head *head,
366                 loff_t *ppos);
367
368 /*
369  *  declaration of proc kernel process routines
370  */
371
372 cfs_file_t *
373 lustre_open_file(char * filename);
374
375 int
376 lustre_close_file(cfs_file_t * fh);
377
378 int
379 lustre_do_ioctl( cfs_file_t * fh,
380                  unsigned long cmd,
381                  ulong_ptr_t arg );
382
383 int
384 lustre_ioctl_file( cfs_file_t * fh,
385                    PCFS_PROC_IOCTL devctl);
386
387 size_t
388 lustre_read_file( cfs_file_t *    fh,
389                   loff_t          offl,
390                   size_t          size,
391                   char *          buf
392                   );
393
394 size_t
395 lustre_write_file( cfs_file_t *    fh,
396                    loff_t          off,
397                    size_t          size,
398                    char *          buf
399                    );
400
401 /*
402  * Wait Queue
403  */
404
405
406 typedef int cfs_task_state_t;
407
408 #define CFS_TASK_INTERRUPTIBLE  0x00000001
409 #define CFS_TASK_UNINT          0x00000002
410 #define CFS_TASK_RUNNING        0x00000003
411 #define CFS_TASK_UNINTERRUPTIBLE CFS_TASK_UNINT
412
413 #define CFS_WAITQ_MAGIC     'CWQM'
414 #define CFS_WAITLINK_MAGIC  'CWLM'
415
416 typedef struct cfs_waitq {
417
418     unsigned int        magic;
419     unsigned int        flags;
420     
421     spinlock_t          guard;
422     struct list_head    waiters;
423
424 } cfs_waitq_t;
425
426
427 typedef struct cfs_waitlink cfs_waitlink_t;
428
429 #define CFS_WAITQ_CHANNELS     (2)
430
431 #define CFS_WAITQ_CHAN_NORMAL  (0)
432 #define CFS_WAITQ_CHAN_FORWARD (1)
433
434
435
436 typedef struct cfs_waitlink_channel {
437     struct list_head        link;
438     cfs_waitq_t *           waitq;
439     cfs_waitlink_t *        waitl;
440 } cfs_waitlink_channel_t;
441
442 struct cfs_waitlink {
443
444     unsigned int            magic;
445     int                     flags;
446     event_t  *              event;
447     atomic_t *              hits;
448
449     cfs_waitlink_channel_t  waitq[CFS_WAITQ_CHANNELS];
450 };
451
452 enum {
453         CFS_WAITQ_EXCLUSIVE = 1
454 };
455
456 #define CFS_DECL_WAITQ(name) cfs_waitq_t name
457
458 /* Kernel thread */
459
460 typedef int (*cfs_thread_t) (void *arg);
461
462 typedef struct _cfs_thread_context {
463     cfs_thread_t        func;
464     void *              arg;
465 } cfs_thread_context_t;
466
467 int cfs_kernel_thread(int (*func)(void *), void *arg, int flag);
468 #define kernel_thread cfs_kernel_thread
469
470 /*
471  * thread creation flags from Linux, not used in winnt
472  */
473 #define CSIGNAL         0x000000ff      /* signal mask to be sent at exit */
474 #define CLONE_VM        0x00000100      /* set if VM shared between processes */
475 #define CLONE_FS        0x00000200      /* set if fs info shared between processes */
476 #define CLONE_FILES     0x00000400      /* set if open files shared between processes */
477 #define CLONE_SIGHAND   0x00000800      /* set if signal handlers and blocked signals shared */
478 #define CLONE_PID       0x00001000      /* set if pid shared */
479 #define CLONE_PTRACE    0x00002000      /* set if we want to let tracing continue on the child too */
480 #define CLONE_VFORK     0x00004000      /* set if the parent wants the child to wake it up on mm_release */
481 #define CLONE_PARENT    0x00008000      /* set if we want to have the same parent as the cloner */
482 #define CLONE_THREAD    0x00010000      /* Same thread group? */
483 #define CLONE_NEWNS     0x00020000      /* New namespace group? */
484
485 #define CLONE_SIGNAL    (CLONE_SIGHAND | CLONE_THREAD)
486
487
488 /*
489  * group_info: linux/sched.h
490  */
491 #define NGROUPS_SMALL           32
492 #define NGROUPS_PER_BLOCK       ((int)(PAGE_SIZE / sizeof(gid_t)))
493 struct group_info {
494         int ngroups;
495         atomic_t usage;
496         gid_t small_block[NGROUPS_SMALL];
497         int nblocks;
498         gid_t *blocks[0];
499 };
500
501 #define get_group_info(group_info) do { \
502         atomic_inc(&(group_info)->usage); \
503 } while (0)
504
505 #define put_group_info(group_info) do { \
506         if (atomic_dec_and_test(&(group_info)->usage)) \
507                 groups_free(group_info); \
508 } while (0)
509
510 static __inline struct group_info *groups_alloc(int gidsetsize)
511 {
512     struct group_info * groupinfo;
513     KdPrint(("%s(%d): %s NOT implemented.\n", __FILE__, __LINE__, __FUNCTION__));
514     groupinfo = (struct group_info *)cfs_alloc(sizeof(struct group_info), 0);
515     if (groupinfo) {
516         memset(groupinfo, 0, sizeof(struct group_info));
517     }
518     return groupinfo;
519 }
520 static __inline void groups_free(struct group_info *group_info)
521 {
522     KdPrint(("%s(%d): %s NOT implemented.\n", __FILE__, __LINE__, __FUNCTION__));
523     cfs_free(group_info);
524 }
525 static __inline int set_current_groups(struct group_info *group_info) {
526     KdPrint(("%s(%d): %s NOT implemented.\n", __FILE__, __LINE__, __FUNCTION__));
527     return 0;
528 }
529 static __inline int groups_search(struct group_info *group_info, gid_t grp) {
530     KdPrint(("%s(%d): %s NOT implemented.\n", __FILE__, __LINE__, __FUNCTION__));
531     return 0;
532 }
533
534 /*
535  *   capability issue (linux/capability.h)
536  */
537
538 /* Override resource limits. Set resource limits. */
539 /* Override quota limits. */
540 /* Override reserved space on ext2 filesystem */
541 /* Modify data journaling mode on ext3 filesystem (uses journaling
542    resources) */
543 /* NOTE: ext2 honors fsuid when checking for resource overrides, so
544    you can override using fsuid too */
545 /* Override size restrictions on IPC message queues */
546 /* Allow more than 64hz interrupts from the real-time clock */
547 /* Override max number of consoles on console allocation */
548 /* Override max number of keymaps */
549
550 #define CAP_SYS_RESOURCE     24
551
552 /*
553  *  capabilities support 
554  */
555
556 typedef __u32 cfs_kernel_cap_t;
557
558 #define cap_raise(c, flag)  do {} while(0)
559 #define cap_lower(c, flag)  do {} while(0)
560 #define cap_raised(c, flag) do {} while(0)
561
562
563 /*
564  * Task struct
565  */
566
567 #define MAX_SCHEDULE_TIMEOUT    ((long_ptr_t)(~0UL>>12))
568 #define schedule_timeout(t)     cfs_schedule_timeout(0, t)
569
570 struct vfsmount;
571
572 #define NGROUPS 1
573 #define CFS_CURPROC_COMM_MAX (16)
574 typedef struct task_sruct{
575     mode_t           umask;
576     sigset_t         blocked;
577
578     pid_t            pid;
579     pid_t            pgrp;
580
581     uid_t            uid,euid,suid,fsuid;
582     gid_t            gid,egid,sgid,fsgid;
583
584     int              ngroups;
585     int              cgroups;
586     gid_t            groups[NGROUPS];
587     struct group_info *group_info;
588     cfs_kernel_cap_t cap_effective,
589                      cap_inheritable,
590                      cap_permitted;
591
592     char             comm[CFS_CURPROC_COMM_MAX];
593     void            *journal_info;
594     struct vfsmount *fs;
595 }  cfs_task_t;
596
597 static inline void task_lock(cfs_task_t *t)
598 {
599 }
600
601 static inline void task_unlock(cfs_task_t *t)
602 {
603 }
604
605 /*
606  *  linux task struct emulator ...
607  */
608
609 #define TASKMAN_MAGIC  'TMAN'   /* Task Manager */
610 #define TASKSLT_MAGIC  'TSLT'   /* Task Slot */
611
612 typedef struct _TASK_MAN {
613
614     ULONG       Magic;      /* Magic and Flags */
615     ULONG       Flags;
616
617     spinlock_t  Lock;       /* Protection lock */
618
619     cfs_mem_cache_t * slab; /* Memory slab for task slot */
620
621     ULONG       NumOfTasks; /* Total tasks (threads) */
622     LIST_ENTRY  TaskList;   /* List of task slots */
623
624 } TASK_MAN, *PTASK_MAN;
625
626 typedef struct _TASK_SLOT {
627
628     ULONG       Magic;      /* Magic and Flags */
629     ULONG       Flags;
630
631     LIST_ENTRY  Link;       /* To be linked to TaskMan */
632
633     event_t     Event;      /* Schedule event */
634
635     HANDLE      Pid;        /* Process id */
636     HANDLE      Tid;        /* Thread id */
637     PETHREAD    Tet;        /* Pointer to ethread */
638
639     atomic_t    count;      /* refer count */
640     atomic_t    hits;       /* times of waken event singaled */
641
642     KIRQL       irql;       /* irql for rwlock ... */
643
644     cfs_task_t  task;       /* linux task part */
645
646 } TASK_SLOT, *PTASK_SLOT;
647
648
649 #define current                 cfs_current()
650 #define set_current_state(s)    do {;} while (0)
651
652 #define wait_event(wq, condition)                               \
653 do {                                                            \
654         cfs_waitlink_t __wait;                                  \
655                                                                 \
656         cfs_waitlink_init(&__wait);                             \
657         while (TRUE) {                                          \
658             cfs_waitq_add(&wq, &__wait);                        \
659             if (condition) {                                    \
660                 break;                                          \
661             }                                                   \
662             cfs_waitq_wait(&__wait, CFS_TASK_INTERRUPTIBLE);    \
663             cfs_waitq_del(&wq, &__wait);                        \
664         }                                                       \
665         cfs_waitq_del(&wq, &__wait);                            \
666 } while(0)
667
668 #define cfs_wait_event_interruptible(wq, condition, __ret)      \
669 do {                                                            \
670         cfs_waitlink_t __wait;                                  \
671                                                                 \
672         __ret = 0;                                              \
673         cfs_waitlink_init(&__wait);                             \
674         while (TRUE) {                                          \
675             cfs_waitq_add(&wq, &__wait);                        \
676             if (condition) {                                    \
677                 break;                                          \
678             }                                                   \
679             cfs_waitq_wait(&__wait, CFS_TASK_INTERRUPTIBLE);    \
680             cfs_waitq_del(&wq, &__wait);                        \
681         }                                                       \
682         cfs_waitq_del(&wq, &__wait);                            \
683 } while(0)
684
685 # define cfs_wait_event_interruptible_exclusive(wq, condition, rc)  \
686          cfs_wait_event_interruptible(wq, condition, rc)
687
688 /*
689    retval == 0; condition met; we're good.
690    retval < 0; interrupted by signal.
691    retval > 0; timed out.
692 */
693
694 #define cfs_waitq_wait_event_interruptible_timeout(             \
695                         wq, condition, timeout, rc)             \
696 do {                                                            \
697         cfs_waitlink_t __wait;                                  \
698                                                                 \
699         rc = 0;                                                 \
700         cfs_waitlink_init(&__wait);                             \
701         while (TRUE) {                                          \
702             cfs_waitq_add(&wq, &__wait);                        \
703             if (condition) {                                    \
704                 break;                                          \
705             }                                                   \
706             if (cfs_waitq_timedwait(&__wait,                    \
707                 CFS_TASK_INTERRUPTIBLE, timeout) == 0) {        \
708                 rc = TRUE;                                      \
709                 break;                                          \
710             }                                                   \
711             cfs_waitq_del(&wq, &__wait);                        \
712         }                                                       \
713         cfs_waitq_del(&wq, &__wait);                            \
714 } while(0)
715
716
717 #define cfs_waitq_wait_event_timeout                            \
718         cfs_waitq_wait_event_interruptible_timeout
719
720 int     init_task_manager();
721 void    cleanup_task_manager();
722 cfs_task_t * cfs_current();
723 int     wake_up_process(cfs_task_t * task);
724 void sleep_on(cfs_waitq_t *waitq);
725 #define might_sleep() do {} while(0)
726 #define CFS_DECL_JOURNAL_DATA   
727 #define CFS_PUSH_JOURNAL            do {;} while(0)
728 #define CFS_POP_JOURNAL             do {;} while(0)
729
730
731 /* module related definitions */
732
733 #ifndef __exit
734 #define __exit
735 #endif
736 #ifndef __init
737 #define __init
738 #endif
739
740 struct module {
741     const char *name;
742 };
743
744 extern struct module libcfs_global_module;
745 #define THIS_MODULE  &libcfs_global_module
746
747 #define request_module(x) (0)
748 #define EXPORT_SYMBOL(s)
749 #define MODULE_AUTHOR(s)
750 #define MODULE_DESCRIPTION(s)
751 #define MODULE_LICENSE(s)
752 #define MODULE_PARM(a, b)
753 #define MODULE_PARM_DESC(a, b)
754
755 #define module_init(X) int  __init module_##X() {return X();}
756 #define module_exit(X) void __exit module_##X() {X();}
757
758 #define DECLARE_INIT(X) extern int  __init  module_##X(void)
759 #define DECLARE_EXIT(X) extern void __exit  module_##X(void)
760
761 #define MODULE_INIT(X) do { int rc = module_##X(); \
762                             if (rc) goto errorout; \
763                           } while(0)
764
765 #define MODULE_EXIT(X) do { module_##X(); } while(0)
766
767
768 /* Module interfaces */
769 #define cfs_module(name, version, init, fini) \
770 module_init(init);                            \
771 module_exit(fini)
772 #define module_refcount(x) (1)
773
774 /*
775  * typecheck
776  */
777
778 #define typecheck(a, b) do {} while(0)
779
780 /*
781  * linux/crypto.h
782  */
783
784 #define CRYPTO_MAX_ALG_NAME             64
785
786 #define CRYPTO_TFM_MODE_ECB             0x00000001
787 #define CRYPTO_TFM_MODE_CBC             0x00000002
788 #define CRYPTO_TFM_MODE_CFB             0x00000004
789 #define CRYPTO_TFM_MODE_CTR             0x00000008
790 #define CRYPTO_TFM_MODE_EME             0x00000010
791
792 /*
793  * hash
794  */
795 /* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
796 #define GOLDEN_RATIO_PRIME_32 0x9e370001UL
797
798 #if 0 /* defined in libcfs/libcfs_hash.h */
799 static inline u32 hash_long(u32 val, unsigned int bits)
800 {
801         /* On some cpus multiply is faster, on others gcc will do shifts */
802         u32 hash = val * GOLDEN_RATIO_PRIME_32;
803
804         /* High bits are more random, so use them. */
805         return hash >> (32 - bits);
806 }
807 #endif
808
809 /*
810  * Timer
811  */
812
813 #define CFS_TIMER_FLAG_INITED   0x00000001  // Initialized already
814 #define CFS_TIMER_FLAG_TIMERED  0x00000002  // KeSetTimer is called
815
816 typedef struct cfs_timer {
817
818     KSPIN_LOCK      Lock;
819
820     ULONG           Flags;
821
822     KDPC            Dpc;
823     KTIMER          Timer;
824
825     cfs_time_t      deadline;
826
827     void (*proc)(ulong_ptr_t);
828     void *          arg;
829
830 } cfs_timer_t;
831
832 /*
833  *  libcfs globals initialization/cleanup
834  */
835
836 int
837 libcfs_arch_init(void);
838
839 void
840 libcfs_arch_cleanup(void);
841
842 /*
843  *  cache alignment size
844  */
845
846 #define L1_CACHE_ALIGN(x) (x)
847
848 #define __cacheline_aligned
849
850 /*
851  * SMP ...
852  */
853
854
855 #define SMP_CACHE_BYTES             128
856 #define NR_CPUS                     (32)
857 #define smp_num_cpus                ((CCHAR)KeNumberProcessors)
858 #define num_possible_cpus()         smp_num_cpus
859 #define num_online_cpus()           smp_num_cpus
860 #define smp_processor_id()                  ((USHORT)KeGetCurrentProcessorNumber())
861 #define smp_call_function(f, a, n, w)           do {} while(0)
862 #define smp_rmb()                   do {} while(0)
863
864 /*
865  *  Irp related
866  */
867
868 #define NR_IRQS                                 512
869 #define in_interrupt()                      (0)
870
871 /*
872  *  printk flags
873  */
874
875 #define KERN_EMERG      "<0>"   /* system is unusable                   */
876 #define KERN_ALERT      "<1>"   /* action must be taken immediately     */
877 #define KERN_CRIT       "<2>"   /* critical conditions                  */
878 #define KERN_ERR        "<3>"   /* error conditions                     */
879 #define KERN_WARNING    "<4>"   /* warning conditions                   */
880 #define KERN_NOTICE     "<5>"   /* normal but significant condition     */
881 #define KERN_INFO       "<6>"   /* informational                        */
882 #define KERN_DEBUG      "<7>"   /* debug-level messages                 */
883
884 /*
885  * Misc
886  */
887
888 #define inter_module_get(n)                     cfs_symbol_get(n)
889 #define inter_module_put(n)                     cfs_symbol_put(n)
890
891 #ifndef likely
892 #define likely(exp) (exp)
893 #endif
894 #ifndef unlikely
895 #define unlikely(exp) (exp)
896 #endif
897
898 #define lock_kernel()               do {} while(0)
899 #define unlock_kernel()             do {} while(0)
900
901 #define local_irq_save(x)
902 #define local_irq_restore(x)
903
904 #define THREAD_NAME
905
906 #define va_copy(_d, _s)                 (_d = _s)
907
908 char *strnchr(const char *s, size_t count, int c);
909
910 #define adler32(a,b,l) zlib_adler32(a,b,l)
911 ULONG zlib_adler32(ULONG adler, const BYTE *buf, UINT len);
912
913 typedef ssize_t (*read_actor_t)();
914
915 #if DBG
916 /*
917  *  winnt debug routines
918  */
919
920 VOID
921 KsPrintf(
922     LONG  DebugPrintLevel,
923     PCHAR DebugMessage,
924     ...
925     );
926
927 PUCHAR
928 KsNtStatusToString (IN NTSTATUS Status);
929 #endif
930
931 #else   /* !__KERNEL__ */
932
933 void cfs_enter_debugger();
934
935 /*
936  *  PAGE_SIZE ...
937  */
938
939 #ifndef PAGE_SIZE
940 #define PAGE_SIZE       (4096)
941 #endif
942
943 #define getpagesize()   (4096)
944
945 #define PAGE_CACHE_SIZE PAGE_SIZE
946 #define PAGE_CACHE_MASK PAGE_MASK
947
948 #define PTHREAD_MUTEX_INITIALIZER ((pthread_mutex_t) -1)
949 #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER ((pthread_mutex_t) -2)
950 #define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER ((pthread_mutex_t) -3)
951
952 typedef struct file {
953     int foo;
954 } cfs_file_t;
955
956 #include "../user-prim.h"
957 #include "../user-lock.h"
958 #include <sys/stat.h>
959 #include <sys/types.h>
960
961 #define strcasecmp  strcmp
962 #define strncasecmp strncmp
963 #define getpid()   (0)
964
965 #define getuid()    (0)
966 #define getgrgid(x) (NULL)
967
968 struct passwd {
969         uid_t pw_uid;
970         char  pw_name[64];
971 };
972 struct passwd * getpwuid(uid_t uid);
973
974 int cfs_proc_mknod(const char *path, mode_t mode, dev_t dev);
975
976 int gethostname(char * name, int namelen);
977
978 #define setlinebuf(x) do {} while(0)
979
980
981 /* Maximum EA Information Length */
982 #define EA_MAX_LENGTH  (sizeof(FILE_FULL_EA_INFORMATION) + 15)
983
984 /*
985  *  proc user mode routines
986  */
987
988 int cfs_proc_open (char * filename, int oflag);
989 int cfs_proc_close(int fd);
990 int cfs_proc_read(int fd, void *buffer, unsigned int count);
991 int cfs_proc_write(int fd, void *buffer, unsigned int count);
992 int cfs_proc_ioctl(int fd, int cmd, void *buffer);
993 FILE *cfs_proc_fopen(char *path, char * mode);
994 char *cfs_proc_fgets(char * buf, int len, FILE *fp);
995 int cfs_proc_fclose(FILE *fp);
996
997 /* Bits set in the FLAGS argument to `glob'.  */
998 #define GLOB_ERR        (1 << 0)/* Return on read errors.  */
999 #define GLOB_MARK       (1 << 1)/* Append a slash to each name.  */
1000 #define GLOB_NOSORT     (1 << 2)/* Don't sort the names.  */
1001 #define GLOB_DOOFFS     (1 << 3)/* Insert PGLOB->gl_offs NULLs.  */
1002 #define GLOB_NOCHECK    (1 << 4)/* If nothing matches, return the pattern.  */
1003 #define GLOB_APPEND     (1 << 5)/* Append to results of a previous call.  */
1004 #define GLOB_NOESCAPE   (1 << 6)/* Backslashes don't quote metacharacters.  */
1005 #define GLOB_PERIOD     (1 << 7)/* Leading `.' can be matched by metachars.  */
1006
1007 #if !defined __USE_POSIX2 || defined __USE_BSD || defined __USE_GNU
1008 # define GLOB_MAGCHAR    (1 << 8)/* Set in gl_flags if any metachars seen.  */
1009 # define GLOB_ALTDIRFUNC (1 << 9)/* Use gl_opendir et al functions.  */
1010 # define GLOB_BRACE      (1 << 10)/* Expand "{a,b}" to "a" "b".  */
1011 # define GLOB_NOMAGIC    (1 << 11)/* If no magic chars, return the pattern.  */
1012 # define GLOB_TILDE      (1 << 12)/* Expand ~user and ~ to home directories. */
1013 # define GLOB_ONLYDIR    (1 << 13)/* Match only directories.  */
1014 # define GLOB_TILDE_CHECK (1 << 14)/* Like GLOB_TILDE but return an error
1015                                       if the user name is not available.  */
1016 # define __GLOB_FLAGS   (GLOB_ERR|GLOB_MARK|GLOB_NOSORT|GLOB_DOOFFS| \
1017                          GLOB_NOESCAPE|GLOB_NOCHECK|GLOB_APPEND|     \
1018                          GLOB_PERIOD|GLOB_ALTDIRFUNC|GLOB_BRACE|     \
1019                          GLOB_NOMAGIC|GLOB_TILDE|GLOB_ONLYDIR|GLOB_TILDE_CHECK)
1020 #else
1021 # define __GLOB_FLAGS   (GLOB_ERR|GLOB_MARK|GLOB_NOSORT|GLOB_DOOFFS| \
1022                          GLOB_NOESCAPE|GLOB_NOCHECK|GLOB_APPEND|     \
1023                          GLOB_PERIOD)
1024 #endif
1025
1026 /* Error returns from `glob'.  */
1027 #define GLOB_NOSPACE    1       /* Ran out of memory.  */
1028 #define GLOB_ABORTED    2       /* Read error.  */
1029 #define GLOB_NOMATCH    3       /* No matches found.  */
1030 #define GLOB_NOSYS      4       /* Not implemented.  */
1031 #ifdef __USE_GNU
1032 /* Previous versions of this file defined GLOB_ABEND instead of
1033    GLOB_ABORTED.  Provide a compatibility definition here.  */
1034 # define GLOB_ABEND GLOB_ABORTED
1035 #endif
1036
1037 /* Structure describing a globbing run.  */
1038 #ifdef __USE_GNU
1039 struct stat;
1040 #endif
1041 typedef struct
1042   {
1043     size_t gl_pathc;            /* Count of paths matched by the pattern.  */
1044     char **gl_pathv;            /* List of matched pathnames.  */
1045     size_t gl_offs;             /* Slots to reserve in `gl_pathv'.  */
1046     int gl_flags;               /* Set to FLAGS, maybe | GLOB_MAGCHAR.  */
1047
1048     /* If the GLOB_ALTDIRFUNC flag is set, the following functions
1049        are used instead of the normal file access functions.  */
1050     void (*gl_closedir) (void *);
1051 #ifdef __USE_GNU
1052     struct dirent *(*gl_readdir) (void *);
1053 #else
1054     void *(*gl_readdir) (void *);
1055 #endif
1056     void *(*gl_opendir) (const char *);
1057 #ifdef __USE_GNU
1058     int (*gl_lstat) (const char *__restrict, struct stat *__restrict);
1059     int (*gl_stat) (const char *__restrict, struct stat *__restrict);
1060 #else
1061     int (*gl_lstat) (const char *__restrict, void *__restrict);
1062     int (*gl_stat) (const char *__restrict, void *__restrict);
1063 #endif
1064   } glob_t;
1065
1066 #ifdef __USE_LARGEFILE64
1067 # ifdef __USE_GNU
1068 struct stat64;
1069 # endif
1070 typedef struct
1071   {
1072     __size_t gl_pathc;
1073     char **gl_pathv;
1074     __size_t gl_offs;
1075     int gl_flags;
1076
1077     /* If the GLOB_ALTDIRFUNC flag is set, the following functions
1078        are used instead of the normal file access functions.  */
1079     void (*gl_closedir) (void *);
1080 # ifdef __USE_GNU
1081     struct dirent64 *(*gl_readdir) (void *);
1082 # else
1083     void *(*gl_readdir) (void *);
1084 # endif
1085     void *(*gl_opendir) (__const char *);
1086 # ifdef __USE_GNU
1087     int (*gl_lstat) (__const char *__restrict, struct stat64 *__restrict);
1088     int (*gl_stat) (__const char *__restrict, struct stat64 *__restrict);
1089 # else
1090     int (*gl_lstat) (__const char *__restrict, void *__restrict);
1091     int (*gl_stat) (__const char *__restrict, void *__restrict);
1092 # endif
1093   } glob64_t;
1094 #endif
1095
1096 int glob (const char * __pattern, int __flags,
1097                  int (*__errfunc) (const char *, int),
1098                  glob_t * __pglob);
1099 void globfree(glob_t *__pglog);
1100
1101 #endif /* !__KERNEL__ */
1102
1103 /*
1104  *  module routines
1105  */
1106
1107 static inline void __module_get(struct module *module)
1108 {
1109 }
1110
1111 static inline int try_module_get(struct module *module)
1112 {
1113     return 1;
1114 }
1115
1116 static inline void module_put(struct module *module)
1117 {
1118 }
1119
1120 /*
1121  *  sigset_t routines 
1122  */
1123
1124 typedef sigset_t cfs_sigset_t;
1125 #define sigaddset(what,sig) (*(what) |= (1<<(sig)), 0)
1126 #define sigdelset(what,sig) (*(what) &= ~(1<<(sig)), 0)
1127 #define sigemptyset(what)   (*(what) = 0, 0)
1128 #define sigfillset(what)    (*(what) = ~(0), 0)
1129 #define sigismember(what,sig) (((*(what)) & (1<<(sig))) != 0)
1130
1131 static __inline int
1132 sigprocmask(int sig, cfs_sigset_t *w1, cfs_sigset_t *w2) {
1133     return 0;
1134 }
1135 static __inline int
1136 sigpending(cfs_sigset_t *what) {
1137     return 0;
1138 }
1139
1140 /*
1141  * common inode flags (user & kernel)
1142  */
1143
1144 #define S_ISLNK(m)      (((m) & S_IFMT) == S_IFLNK)
1145 #define S_ISREG(m)      (((m) & S_IFMT) == S_IFREG)
1146 #define S_ISDIR(m)      (((m) & S_IFMT) == S_IFDIR)
1147 #define S_ISCHR(m)      (((m) & S_IFMT) == S_IFCHR)
1148 #define S_ISBLK(m)      (((m) & S_IFMT) == S_IFBLK)
1149 #define S_ISFIFO(m)     (((m) & S_IFMT) == S_IFIFO)
1150 #define S_ISSOCK(m)     (((m) & S_IFMT) == S_IFSOCK)
1151
1152 #define S_IRWXU 00700
1153 #define S_IRUSR 00400
1154 #define S_IWUSR 00200
1155 #define S_IXUSR 00100
1156
1157 #define S_IRWXG 00070
1158 #define S_IRGRP 00040
1159 #define S_IWGRP 00020
1160 #define S_IXGRP 00010
1161
1162 #define S_IRWXO 00007
1163 #define S_IROTH 00004
1164 #define S_IWOTH 00002
1165 #define S_IXOTH 00001
1166
1167 #define S_IRWXUGO   (S_IRWXU|S_IRWXG|S_IRWXO)
1168 #define S_IALLUGO   (S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO)
1169 #define S_IRUGO     (S_IRUSR|S_IRGRP|S_IROTH)
1170 #define S_IWUGO     (S_IWUSR|S_IWGRP|S_IWOTH)
1171 #define S_IXUGO     (S_IXUSR|S_IXGRP|S_IXOTH)
1172
1173
1174 /*
1175  *  Linux kernel version definition
1176  */
1177
1178 #define KERNEL_VERSION(a,b,c) ((a)*100+(b)*10+c)
1179
1180 /*
1181  *  linux ioctl coding definitions
1182  */
1183  
1184 #define _IOC_NRBITS 8
1185 #define _IOC_TYPEBITS   8
1186 #define _IOC_SIZEBITS   14
1187 #define _IOC_DIRBITS    2
1188
1189 #define _IOC_NRMASK     ((1 << _IOC_NRBITS)-1)
1190 #define _IOC_TYPEMASK   ((1 << _IOC_TYPEBITS)-1)
1191 #define _IOC_SIZEMASK   ((1 << _IOC_SIZEBITS)-1)
1192 #define _IOC_DIRMASK    ((1 << _IOC_DIRBITS)-1)
1193
1194 #define _IOC_NRSHIFT    0
1195 #define _IOC_TYPESHIFT  (_IOC_NRSHIFT+_IOC_NRBITS)
1196 #define _IOC_SIZESHIFT  (_IOC_TYPESHIFT+_IOC_TYPEBITS)
1197 #define _IOC_DIRSHIFT   (_IOC_SIZESHIFT+_IOC_SIZEBITS)
1198
1199 /*
1200  * Direction bits.
1201  */
1202 #define _IOC_NONE   0U
1203 #define _IOC_WRITE  1U
1204 #define _IOC_READ   2U
1205
1206 #define _IOC(dir,type,nr,size) \
1207     (((dir)  << _IOC_DIRSHIFT) | \
1208      ((type) << _IOC_TYPESHIFT) | \
1209      ((nr)   << _IOC_NRSHIFT) | \
1210      ((size) << _IOC_SIZESHIFT))
1211
1212 /* used to create numbers */
1213 #define _IO(type,nr)      _IOC(_IOC_NONE,(type),(nr),0)
1214 #define _IOR(type,nr,size)    _IOC(_IOC_READ,(type),(nr),sizeof(size))
1215 #define _IOW(type,nr,size)    _IOC(_IOC_WRITE,(type),(nr),sizeof(size))
1216 #define _IOWR(type,nr,size) _IOC(_IOC_READ|_IOC_WRITE,(type),(nr),sizeof(size))
1217
1218 /* used to decode ioctl numbers.. */
1219 #define _IOC_DIR(nr)        (((nr) >> _IOC_DIRSHIFT) & _IOC_DIRMASK)
1220 #define _IOC_TYPE(nr)       (((nr) >> _IOC_TYPESHIFT) & _IOC_TYPEMASK)
1221 #define _IOC_NR(nr)         (((nr) >> _IOC_NRSHIFT) & _IOC_NRMASK)
1222 #define _IOC_SIZE(nr)       (((nr) >> _IOC_SIZESHIFT) & _IOC_SIZEMASK)
1223
1224 /* i/o vector sgructure ... */
1225 struct iovec {
1226     void *iov_base;
1227     size_t iov_len;
1228 };
1229
1230 /* idr support routines */
1231 struct idr_context *cfs_idr_init();
1232 int cfs_idr_remove(struct idr_context *idp, int id);
1233 int cfs_idr_get_new(struct idr_context *idp, void *ptr);
1234 int cfs_idr_get_new_above(struct idr_context *idp, void *ptr, int starting_id);
1235 void *cfs_idr_find(struct idr_context *idp, int id);
1236 void cfs_idr_exit(struct idr_context *idp);
1237
1238 /* runtime time routines for both kenrel and user mode */
1239 extern int cfs_isalpha(int);
1240 extern int cfs_isspace(int);
1241 extern int cfs_isupper(int);
1242 extern int cfs_isdigit(int);
1243 extern int cfs_isxdigit(int);
1244
1245 #define ULONG_LONG_MAX ((__u64)(0xFFFFFFFFFFFFFFFF))
1246 /*
1247  * Convert a string to an unsigned long long integer.
1248  *
1249  * Ignores `locale' stuff.  Assumes that the upper and lower case
1250  * alphabets and digits are each contiguous.
1251  */
1252 __u64 strtoull(char *nptr, char **endptr,int base);
1253
1254 /*
1255  *  getopt routines
1256  */
1257
1258 /* For communication from `getopt' to the caller.
1259    When `getopt' finds an option that takes an argument,
1260    the argument value is returned here.
1261    Also, when `ordering' is RETURN_IN_ORDER,
1262    each non-option ARGV-element is returned here.  */
1263
1264 extern char *optarg;
1265
1266 /* Index in ARGV of the next element to be scanned.
1267    This is used for communication to and from the caller
1268    and for communication between successive calls to `getopt'.
1269
1270    On entry to `getopt', zero means this is the first call; initialize.
1271
1272    When `getopt' returns -1, this is the index of the first of the
1273    non-option elements that the caller should itself scan.
1274
1275    Otherwise, `optind' communicates from one call to the next
1276    how much of ARGV has been scanned so far.  */
1277
1278 extern int optind;
1279
1280 /* Callers store zero here to inhibit the error message `getopt' prints
1281    for unrecognized options.  */
1282
1283 extern int opterr;
1284
1285 /* Set to an option character which was unrecognized.  */
1286
1287 extern int optopt;
1288
1289
1290 struct option
1291 {
1292   const char *name;
1293   /* has_arg can't be an enum because some compilers complain about
1294      type mismatches in all the code that assumes it is an int.  */
1295   int has_arg;
1296   int *flag;
1297   int val;
1298 };
1299
1300 /* Names for the values of the `has_arg' field of `struct option'.  */
1301 # define no_argument            0
1302 # define required_argument      1
1303 # define optional_argument      2
1304
1305 extern int getopt(int ___argc, char *const *___argv, const char *__shortopts);
1306 extern int getopt_long (int ___argc, char *const *___argv,
1307                         const char *__shortopts,
1308                         const struct option *__longopts, int *__longind);
1309 extern int getopt_long_only (int ___argc, char *const *___argv,
1310                              const char *__shortopts,
1311                              const struct option *__longopts, int *__longind);
1312
1313 extern char *strcasestr (const char *phaystack, const char *pneedle);
1314
1315 /*
1316  * global environment runtime routine
1317  */
1318
1319 static __inline char * __cdecl cfs_getenv(const char *ENV) {return NULL;}
1320 static __inline void   __cdecl set_getenv(const char *ENV, const char *value, int overwrite) {}
1321
1322 int setenv(const char *envname, const char *envval, int overwrite);
1323
1324 struct utsname {
1325          char sysname[64];
1326          char nodename[64];
1327          char release[128];
1328          char version[128];
1329          char machine[64];
1330 };
1331
1332 int uname(struct utsname *uts);
1333
1334 #endif