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