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