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