Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / libcfs / include / libcfs / winnt / winnt-prim.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see [sun.com URL with a
20  * copy of GPLv2].
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/include/libcfs/winnt/winnt-prim.h
37  *
38  * Basic library routines.
39  */
40
41 #ifndef __LIBCFS_WINNT_CFS_PRIM_H__
42 #define __LIBCFS_WINNT_CFS_PRIM_H__
43
44 #ifndef __LIBCFS_LIBCFS_H__
45 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
46 #endif
47
48
49 /*
50  * libcfs proc device object
51  */
52
53
54 #define LUSTRE_PROC_DEVICE  L"\\Device\\lproc"      /* proc fs emulator device object */
55 #define LUSTRE_PROC_SYMLNK  L"\\DosDevices\\lproc"  /* proc fs user-visible device */
56
57
58 /*
59  * Device IO Control Code Definitions
60  */
61
62 #define FILE_DEVICE_LIBCFS      ('LC')
63
64 #define FILE_DEVICE_LIBCFS      ('LC')
65
66 #define FUNC_LIBCFS_VERSION     0x101  // get version of current libcfs
67 #define FUNC_LIBCFS_IOCTL       0x102  // Device i/o control to proc fs
68
69
70 #define IOCTL_LIBCFS_VERSION \
71      CTL_CODE (FILE_DEVICE_LIBCFS, FUNC_LIBCFS_VERSION, METHOD_BUFFERED, FILE_ANY_ACCESS)
72 #define IOCTL_LIBCFS_ENTRY   \
73      CTL_CODE(FILE_DEVICE_LIBCFS, FUNC_LIBCFS_IOCTL,   METHOD_BUFFERED, FILE_ANY_ACCESS)
74
75 #pragma pack(4)
76
77 typedef struct _CFS_PROC_IOCTL {
78
79     ULONG           cmd;    // ioctl command identifier
80     ULONG           len;    // length of data
81
82     // UCHAR        data[]; // content of the real ioctl
83
84 } CFS_PROC_IOCTL, *PCFS_PROC_IOCTL;
85
86 #pragma pack()
87
88 #ifdef __KERNEL__
89
90 #include <libcfs/list.h>
91
92 /*
93  * Symbol functions for libcfs
94  *
95  * OSX has no facility for use to register symbol.
96  * So we have to implement it.
97  */
98 #define CFS_SYMBOL_LEN     64
99
100 struct  cfs_symbol {
101         char    name[CFS_SYMBOL_LEN];
102         void    *value;
103         int     ref;
104         struct  list_head sym_list;
105 };
106
107 extern int      cfs_symbol_register(const char *, const void *);
108 extern void     cfs_symbol_unregister(const char *);
109 extern void *   cfs_symbol_get(const char *);
110 extern void     cfs_symbol_put(const char *);
111 extern void     cfs_symbol_clean();
112
113
114
115 typedef struct file_operations cfs_file_operations_t;
116 typedef struct file cfs_file_t;
117
118 /*
119  * Pseudo device register
120  */
121
122 typedef struct
123 {
124     int                     minor;
125     const char *            name;
126     cfs_file_operations_t * fops;
127 } cfs_psdev_t;
128
129 int cfs_psdev_register(cfs_psdev_t * psdev);
130 int cfs_psdev_deregister(cfs_psdev_t * psdev);
131
132
133 /*
134  * Proc emulator file system APIs
135  */
136
137 typedef int cfs_read_proc_t(char *page, char **start, off_t off,
138                           int count, int *eof, void *data);
139 typedef int cfs_write_proc_t(struct file *file, const char *buffer,
140                            ulong_ptr count, void *data);
141
142 #define CFS_PROC_ENTRY_MAGIC 'CPEM'
143
144 #define CFS_PROC_FLAG_DIRECTORY    0x00000001 // directory node
145 #define CFS_PROC_FLAG_ATTACHED     0x00000002 // node is attached to proc
146 #define CFS_PROC_FLAG_MISCDEV      0x00000004 // miscellaneous device
147
148 typedef struct cfs_proc_entry
149 {
150     ULONG                   magic;      // Magic
151     ULONG                   flags;      // Flags
152
153     struct _dir_entry {                 // proc directory entry
154         PRTL_SPLAY_LINKS    root;
155     };
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
165         
166     struct file_operations * proc_fops;
167         void * data;
168
169     // proc_dir_entry ended.
170
171     RTL_SPLAY_LINKS         s_link;       // splay link
172
173     //
174     // Maximum length of proc entry name is 0x20
175     //
176
177     char                    name[0x20];
178
179 } cfs_proc_entry_t, cfs_proc_dir_entry_t;
180
181 typedef cfs_proc_entry_t cfs_proc_dir_entry_t;
182
183 #define PROC_BLOCK_SIZE    PAGE_SIZE
184
185 /*
186  * Sysctl register
187  */
188
189 typedef struct ctl_table                    cfs_sysctl_table_t;
190 typedef struct ctl_table_header         cfs_sysctl_table_header_t;
191
192
193 typedef int ctl_handler (
194             cfs_sysctl_table_t *table,
195             int *name,    int nlen,
196                         void *oldval, size_t *oldlenp,
197                         void *newval, size_t newlen, 
198                         void **context );
199
200 typedef int proc_handler (
201             cfs_sysctl_table_t *ctl,
202             int write, struct file * filp,
203                         void *buffer, size_t *lenp );
204
205
206 int proc_dointvec(cfs_sysctl_table_t *table, int write, struct file *filp,
207                      void *buffer, size_t *lenp);
208
209 int proc_dostring(cfs_sysctl_table_t *table, int write, struct file *filp,
210                   void *buffer, size_t *lenp);
211
212 int sysctl_string(cfs_sysctl_table_t *table, int *name, int nlen,
213                   void *oldval, size_t *oldlenp,
214                   void *newval, size_t newlen, void **context);
215
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         struct list_head        ctl_entry;
262 };
263
264
265 cfs_proc_entry_t * create_proc_entry(char *name, mode_t mod,
266                                           cfs_proc_entry_t *parent);
267 void proc_free_entry(cfs_proc_entry_t *de);
268 void remove_proc_entry(char *name, cfs_proc_entry_t *entry);
269 cfs_proc_entry_t * search_proc_entry(char * name,
270                         cfs_proc_entry_t *  root );
271
272 #define cfs_create_proc_entry create_proc_entry
273 #define cfs_free_proc_entry   proc_free_entry
274 #define cfs_remove_proc_entry remove_proc_entry
275
276 #define register_cfs_sysctl_table(t, a) register_sysctl_table(t, a)
277 #define unregister_cfs_sysctl_table(t)  unregister_sysctl_table(t, a)
278
279
280 /*
281  *  declaration of proc kernel process routines
282  */
283
284 cfs_file_t *
285 lustre_open_file(char * filename);
286
287 int
288 lustre_close_file(cfs_file_t * fh);
289
290 int
291 lustre_do_ioctl( cfs_file_t * fh,
292                  unsigned long cmd,
293                  ulong_ptr arg );
294
295 int
296 lustre_ioctl_file( cfs_file_t * fh,
297                    PCFS_PROC_IOCTL devctl);
298
299 size_t
300 lustre_read_file( cfs_file_t *    fh,
301                   loff_t          off,
302                   size_t          size,
303                   char *          buf
304                   );
305
306 size_t
307 lustre_write_file( cfs_file_t *    fh,
308                    loff_t          off,
309                    size_t          size,
310                    char *          buf
311                    );
312
313 /*
314  * Wait Queue
315  */
316
317
318 typedef int cfs_task_state_t;
319
320 #define CFS_TASK_INTERRUPTIBLE  0x00000001
321 #define CFS_TASK_UNINT          0x00000002
322
323
324
325 #define CFS_WAITQ_MAGIC     'CWQM'
326 #define CFS_WAITLINK_MAGIC  'CWLM'
327
328 typedef struct cfs_waitq {
329
330     unsigned int        magic;
331     unsigned int        flags;
332     
333     spinlock_t          guard;
334     struct list_head    waiters;
335
336 } cfs_waitq_t;
337
338
339 typedef struct cfs_waitlink cfs_waitlink_t;
340
341 #define CFS_WAITQ_CHANNELS     (2)
342
343 #define CFS_WAITQ_CHAN_NORMAL  (0)
344 #define CFS_WAITQ_CHAN_FORWARD (1)
345
346
347
348 typedef struct cfs_waitlink_channel {
349     struct list_head        link;
350     cfs_waitq_t *           waitq;
351     cfs_waitlink_t *        waitl;
352 } cfs_waitlink_channel_t;
353
354 struct cfs_waitlink {
355
356     unsigned int            magic;
357     int                     flags;
358     event_t  *              event;
359     atomic_t *              hits;
360
361     cfs_waitlink_channel_t  waitq[CFS_WAITQ_CHANNELS];
362 };
363
364 enum {
365         CFS_WAITQ_EXCLUSIVE = 1
366 };
367
368 #define CFS_DECL_WAITQ(name) cfs_waitq_t name
369
370
371 void cfs_waitq_init(struct cfs_waitq *waitq);
372 void cfs_waitlink_init(struct cfs_waitlink *link);
373
374 void cfs_waitq_add(struct cfs_waitq *waitq, struct cfs_waitlink *link);
375 void cfs_waitq_add_exclusive(struct cfs_waitq *waitq, 
376                              struct cfs_waitlink *link);
377 void cfs_waitq_forward(struct cfs_waitlink *link, struct cfs_waitq *waitq);
378 void cfs_waitq_del(struct cfs_waitq *waitq, struct cfs_waitlink *link);
379 int  cfs_waitq_active(struct cfs_waitq *waitq);
380
381 void cfs_waitq_signal(struct cfs_waitq *waitq);
382 void cfs_waitq_signal_nr(struct cfs_waitq *waitq, int nr);
383 void cfs_waitq_broadcast(struct cfs_waitq *waitq);
384
385 void cfs_waitq_wait(struct cfs_waitlink *link, cfs_task_state_t state);
386 cfs_duration_t cfs_waitq_timedwait(struct cfs_waitlink *link, 
387                                    cfs_task_state_t state, cfs_duration_t timeout);
388
389
390
391 /* Kernel thread */
392
393 typedef int (*cfs_thread_t) (void *arg);
394
395 typedef struct _cfs_thread_context {
396     cfs_thread_t        func;
397     void *              arg;
398 } cfs_thread_context_t;
399
400 int cfs_kernel_thread(int (*func)(void *), void *arg, int flag);
401
402 /*
403  * thread creation flags from Linux, not used in winnt
404  */
405 #define CSIGNAL         0x000000ff      /* signal mask to be sent at exit */
406 #define CLONE_VM        0x00000100      /* set if VM shared between processes */
407 #define CLONE_FS        0x00000200      /* set if fs info shared between processes */
408 #define CLONE_FILES     0x00000400      /* set if open files shared between processes */
409 #define CLONE_SIGHAND   0x00000800      /* set if signal handlers and blocked signals shared */
410 #define CLONE_PID       0x00001000      /* set if pid shared */
411 #define CLONE_PTRACE    0x00002000      /* set if we want to let tracing continue on the child too */
412 #define CLONE_VFORK     0x00004000      /* set if the parent wants the child to wake it up on mm_release */
413 #define CLONE_PARENT    0x00008000      /* set if we want to have the same parent as the cloner */
414 #define CLONE_THREAD    0x00010000      /* Same thread group? */
415 #define CLONE_NEWNS     0x00020000      /* New namespace group? */
416
417 #define CLONE_SIGNAL    (CLONE_SIGHAND | CLONE_THREAD)
418
419
420 /*
421  * sigset ...
422  */
423
424 typedef sigset_t cfs_sigset_t;
425
426 /*
427  * Task struct
428  */
429
430 #define MAX_SCHEDULE_TIMEOUT    ((long_ptr)(~0UL>>12))
431
432
433 #define NGROUPS 1
434 #define CFS_CURPROC_COMM_MAX (16)
435 typedef struct task_sruct{
436     mode_t umask;
437
438         pid_t pid;
439         pid_t pgrp;
440
441         uid_t uid,euid,suid,fsuid;
442         gid_t gid,egid,sgid,fsgid;
443
444         int ngroups;
445         gid_t   groups[NGROUPS];
446         cfs_kernel_cap_t   cap_effective,
447                        cap_inheritable,
448                        cap_permitted;
449
450         char comm[CFS_CURPROC_COMM_MAX];
451     void * journal_info;
452 }  cfs_task_t;
453
454
455 /*
456  *  linux task struct emulator ...
457  */
458
459 #define TASKMAN_MAGIC  'TMAN'   /* Task Manager */
460 #define TASKSLT_MAGIC  'TSLT'   /* Task Slot */
461
462 typedef struct _TASK_MAN {
463
464     ULONG       Magic;      /* Magic and Flags */
465     ULONG       Flags;
466
467     spinlock_t  Lock;       /* Protection lock */
468
469     cfs_mem_cache_t * slab; /* Memory slab for task slot */
470
471     ULONG       NumOfTasks; /* Total tasks (threads) */
472     LIST_ENTRY  TaskList;   /* List of task slots */
473
474 } TASK_MAN, *PTASK_MAN;
475
476 typedef struct _TASK_SLOT {
477
478     ULONG       Magic;      /* Magic and Flags */
479     ULONG       Flags;
480
481     LIST_ENTRY  Link;       /* To be linked to TaskMan */
482
483     event_t     Event;      /* Schedule event */
484
485     HANDLE      Pid;        /* Process id */
486     HANDLE      Tid;        /* Thread id */
487     PETHREAD    Tet;        /* Pointer to ethread */
488
489     atomic_t    count;      /* refer count */
490     atomic_t    hits;       /* times of waken event singaled */
491
492     KIRQL       irql;       /* irql for rwlock ... */
493
494     cfs_task_t  task;       /* linux task part */
495
496 } TASK_SLOT, *PTASK_SLOT;
497
498
499 #define current                 cfs_current()
500 #define set_current_state(s)    do {;} while (0)
501
502 #define wait_event(wq, condition)                           \
503 do {                                                        \
504     cfs_waitlink_t __wait;                                      \
505                                                             \
506     cfs_waitlink_init(&__wait);                             \
507         while (TRUE) {                                          \
508                 cfs_waitq_add(&wq, &__wait);                        \
509                 if (condition)  {                                           \
510                         break;                                                  \
511         }                                                   \
512                 cfs_waitq_wait(&__wait, CFS_TASK_INTERRUPTIBLE);        \
513                 cfs_waitq_del(&wq, &__wait);                        \
514         }                                                                           \
515         cfs_waitq_del(&wq, &__wait);                                \
516 } while(0)
517
518 #define wait_event_interruptible(wq, condition, __ret)      \
519 do {                                                        \
520     cfs_waitlink_t __wait;                                      \
521                                                             \
522     __ret = 0;                                              \
523     cfs_waitlink_init(&__wait);                             \
524         while (TRUE) {                                          \
525                 cfs_waitq_add(&wq, &__wait);                        \
526                 if (condition)  {                                           \
527                         break;                                                  \
528         }                                                   \
529                 cfs_waitq_wait(&__wait, CFS_TASK_INTERRUPTIBLE);    \
530                 cfs_waitq_del(&wq, &__wait);                        \
531         }                                                                           \
532         cfs_waitq_del(&wq, &__wait);                                \
533 } while(0)
534
535
536 int     init_task_manager();
537 void    cleanup_task_manager();
538 cfs_task_t * cfs_current();
539 int     schedule_timeout(int64_t time);
540 int     schedule();
541 int     wake_up_process(cfs_task_t * task);
542 #define cfs_schedule_timeout(state, time)  schedule_timeout(time)
543 void sleep_on(cfs_waitq_t *waitq);
544
545 #define CFS_DECL_JOURNAL_DATA   
546 #define CFS_PUSH_JOURNAL            do {;} while(0)
547 #define CFS_POP_JOURNAL             do {;} while(0)
548
549
550 /* module related definitions */
551
552 #ifndef __exit
553 #define __exit
554 #endif
555 #ifndef __init
556 #define __init
557 #endif
558
559 #define request_module(x) (0)
560
561 #define EXPORT_SYMBOL(s)
562 #define MODULE_AUTHOR(s)
563 #define MODULE_DESCRIPTION(s)
564 #define MODULE_LICENSE(s)
565 #define MODULE_PARM(a, b)
566 #define MODULE_PARM_DESC(a, b)
567
568 #define module_init(X) int  __init module_##X() {return X();}
569 #define module_exit(X) void __exit module_##X() {X();}
570
571 #define DECLARE_INIT(X) extern int  __init  module_##X(void)
572 #define DECLARE_EXIT(X) extern void __exit  module_##X(void)
573
574 #define MODULE_INIT(X) do { int rc = module_##X(); \
575                             if (rc) goto errorout; \
576                           } while(0)
577
578 #define MODULE_EXIT(X) do { module_##X(); } while(0)
579
580
581 /* Module interfaces */
582 #define cfs_module(name, version, init, fini) \
583 module_init(init);                            \
584 module_exit(fini)
585
586
587 /*
588  *  Linux kernel version definition
589  */
590
591 #define KERNEL_VERSION(a,b,c) ((a)*100+(b)*10+c)
592 #define LINUX_VERSION_CODE (2*100+6*10+7)
593
594
595 /*
596  * Signal
597  */
598 #define SIGNAL_MASK_ASSERT()
599
600 /*
601  * Timer
602  */
603
604 #define CFS_TIMER_FLAG_INITED   0x00000001  // Initialized already
605 #define CFS_TIMER_FLAG_TIMERED  0x00000002  // KeSetTimer is called
606
607 typedef struct cfs_timer {
608
609     KSPIN_LOCK      Lock;
610
611     ULONG           Flags;
612
613     KDPC            Dpc;
614     KTIMER          Timer;
615
616     cfs_time_t      deadline;
617
618     void (*proc)(ulong_ptr);
619     void *          arg;
620
621 } cfs_timer_t;
622
623
624 typedef  void (*timer_func_t)(ulong_ptr);
625
626 #define cfs_init_timer(t)
627
628 void cfs_timer_init(cfs_timer_t *timer, void (*func)(ulong_ptr), void *arg);
629 void cfs_timer_done(cfs_timer_t *t);
630 void cfs_timer_arm(cfs_timer_t *t, cfs_time_t deadline);
631 void cfs_timer_disarm(cfs_timer_t *t);
632 int  cfs_timer_is_armed(cfs_timer_t *t);
633 cfs_time_t cfs_timer_deadline(cfs_timer_t *t);
634
635
636 /* deschedule for a bit... */
637 static inline void cfs_pause(cfs_duration_t ticks)
638 {
639     cfs_schedule_timeout(TASK_UNINTERRUPTIBLE, ticks);
640 }
641
642
643 static inline void cfs_enter_debugger(void)
644 {
645 #if _X86_
646     __asm int 3;
647 #else
648     KdBreakPoint();
649 #endif
650 }
651
652 /*
653  *  libcfs globals initialization/cleanup
654  */
655
656 int
657 libcfs_arch_init(void);
658
659 void
660 libcfs_arch_cleanup(void);
661
662 /*
663  * SMP ...
664  */
665
666 #define SMP_CACHE_BYTES             128
667 #define __cacheline_aligned
668 #define NR_CPUS                                     (2)
669 #define smp_processor_id()                  KeGetCurrentProcessorNumber()
670 #define smp_num_cpus                NR_CPUS
671 #define num_online_cpus() smp_num_cpus
672 #define smp_call_function(f, a, n, w)           do {} while(0)
673
674 /*
675  *  Irp related
676  */
677
678 #define NR_IRQS                             512
679 #define in_interrupt()                  (0)
680
681 /*
682  *  printk flags
683  */
684
685 #define KERN_EMERG      "<0>"   /* system is unusable                   */
686 #define KERN_ALERT      "<1>"   /* action must be taken immediately     */
687 #define KERN_CRIT       "<2>"   /* critical conditions                  */
688 #define KERN_ERR        "<3>"   /* error conditions                     */
689 #define KERN_WARNING    "<4>"   /* warning conditions                   */
690 #define KERN_NOTICE     "<5>"   /* normal but significant condition     */
691 #define KERN_INFO       "<6>"   /* informational                        */
692 #define KERN_DEBUG      "<7>"   /* debug-level messages                 */
693
694 /*
695  * Misc
696  */
697
698
699 #define inter_module_get(n)                     cfs_symbol_get(n)
700 #define inter_module_put(n)                     cfs_symbol_put(n)
701
702 #ifndef likely
703 #define likely(exp) (exp)
704 #endif
705 #ifndef unlikely
706 #define unlikely(exp) (exp)
707 #endif
708
709 #define lock_kernel()               do {} while(0)
710 #define unlock_kernel()             do {} while(0)
711
712 #define CAP_SYS_ADMIN                    0
713 #define CAP_SYS_ROOT                     1
714
715 #define capable(a)                              (TRUE)
716
717 #define USERMODEHELPER(path, argv, envp)        (0)
718
719
720 #define local_irq_save(x)
721 #define local_irq_restore(x)
722
723 #define cfs_assert                      ASSERT
724
725 #define THREAD_NAME
726
727 #else   /* !__KERNEL__ */
728
729 #define PAGE_CACHE_SIZE PAGE_SIZE
730 #define PAGE_CACHE_MASK PAGE_MASK
731
732 #define getpagesize()   (PAGE_SIZE)
733
734
735 typedef struct {
736     int foo;
737 } pthread_mutex_t;
738
739 typedef struct {
740     int foo;
741 } pthread_cond_t;
742
743 #define pthread_mutex_init(x, y)    do {} while(0)
744 #define pthread_cond_init(x, y)     do {} while(0)
745
746 #define pthread_mutex_lock(x)       do {} while(0)
747 #define pthread_mutex_unlock(x)     do {} while(0)
748
749 #define pthread_cond_wait(x,y)      do {} while(0)
750 #define pthread_cond_broadcast(x)   do {} while(0)
751
752 typedef struct file {
753     int foo;
754 } cfs_file_t;
755
756 typedef struct cfs_proc_dir_entry{
757         void            *data;
758 }cfs_proc_dir_entry_t;
759
760
761
762 #include "../user-prim.h"
763
764 #include <sys/stat.h>
765 #include <sys/types.h>
766
767 #define strcasecmp  strcmp
768 #define strncasecmp strncmp
769 #define snprintf   _snprintf
770 #define getpid()   (0)
771
772
773 #define getpwuid(x) (NULL)
774 #define getgrgid(x) (NULL)
775
776 int cfs_proc_mknod(const char *path, mode_t mode, dev_t dev);
777
778 int gethostname(char * name, int namelen);
779
780 #define setlinebuf(x) do {} while(0)
781
782
783 NTSYSAPI VOID NTAPI DebugBreak();
784
785
786 static inline void cfs_enter_debugger(void)
787 {
788 #if _X86_
789     __asm int 3;
790 #else
791     DebugBreak();
792 #endif
793 }
794
795 /* Maximum EA Information Length */
796 #define EA_MAX_LENGTH  (sizeof(FILE_FULL_EA_INFORMATION) + 15)
797
798
799 /*
800  *  proc user mode routines
801  */
802
803 HANDLE cfs_proc_open (char * filename, int oflag);
804 int cfs_proc_close(HANDLE handle);
805 int cfs_proc_read(HANDLE handle, void *buffer, unsigned int count);
806 int cfs_proc_write(HANDLE handle, void *buffer, unsigned int count);
807 int cfs_proc_ioctl(HANDLE handle, int cmd, void *buffer);
808
809
810 /*
811  * Native API definitions
812  */
813
814 //
815 //  Disk I/O Routines
816 //
817
818 NTSYSAPI
819 NTSTATUS
820 NTAPI
821 NtReadFile(HANDLE FileHandle,
822     HANDLE Event OPTIONAL,
823     PIO_APC_ROUTINE ApcRoutine OPTIONAL,
824     PVOID ApcContext OPTIONAL,
825     PIO_STATUS_BLOCK IoStatusBlock,
826     PVOID Buffer,
827     ULONG Length,
828     PLARGE_INTEGER ByteOffset OPTIONAL,
829     PULONG Key OPTIONAL);
830
831 NTSYSAPI
832 NTSTATUS
833 NTAPI
834 NtWriteFile(HANDLE FileHandle,
835     HANDLE Event OPTIONAL,
836     PIO_APC_ROUTINE ApcRoutine OPTIONAL,
837     PVOID ApcContext OPTIONAL,
838     PIO_STATUS_BLOCK IoStatusBlock,
839     PVOID Buffer,
840     ULONG Length,
841     PLARGE_INTEGER ByteOffset OPTIONAL,
842     PULONG Key OPTIONAL);
843
844 NTSYSAPI
845 NTSTATUS
846 NTAPI
847 NtClose(HANDLE Handle);
848
849 NTSYSAPI
850 NTSTATUS
851 NTAPI
852 NtCreateFile(PHANDLE FileHandle,
853     ACCESS_MASK DesiredAccess,
854     POBJECT_ATTRIBUTES ObjectAttributes,
855     PIO_STATUS_BLOCK IoStatusBlock,
856     PLARGE_INTEGER AllocationSize OPTIONAL,
857     ULONG FileAttributes,
858     ULONG ShareAccess,
859     ULONG CreateDisposition,
860     ULONG CreateOptions,
861     PVOID EaBuffer OPTIONAL,
862     ULONG EaLength);
863
864
865 NTSYSAPI
866 NTSTATUS
867 NTAPI
868 NtDeviceIoControlFile(
869     IN HANDLE  FileHandle,
870     IN HANDLE  Event,
871     IN PIO_APC_ROUTINE  ApcRoutine,
872     IN PVOID  ApcContext,
873     OUT PIO_STATUS_BLOCK  IoStatusBlock,
874     IN ULONG  IoControlCode,
875     IN PVOID  InputBuffer,
876     IN ULONG  InputBufferLength,
877     OUT PVOID  OutputBuffer,
878     OUT ULONG  OutputBufferLength
879     ); 
880
881 NTSYSAPI
882 NTSTATUS
883 NTAPI
884 NtFsControlFile(
885     IN HANDLE FileHandle,
886     IN HANDLE Event OPTIONAL,
887     IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
888     IN PVOID ApcContext OPTIONAL,
889     OUT PIO_STATUS_BLOCK IoStatusBlock,
890     IN ULONG FsControlCode,
891     IN PVOID InputBuffer OPTIONAL,
892     IN ULONG InputBufferLength,
893     OUT PVOID OutputBuffer OPTIONAL,
894     IN ULONG OutputBufferLength
895 );
896
897
898 NTSYSAPI
899 NTSTATUS
900 NTAPI
901 NtQueryInformationFile(
902     IN HANDLE  FileHandle,
903     OUT PIO_STATUS_BLOCK  IoStatusBlock,
904     OUT PVOID  FileInformation,
905     IN ULONG  Length,
906     IN FILE_INFORMATION_CLASS  FileInformationClass
907     );
908
909 //
910 // Random routines ...
911 //
912
913 NTSYSAPI
914 ULONG
915 NTAPI
916 RtlRandom(
917     IN OUT PULONG  Seed
918     ); 
919
920 #endif /* __KERNEL__ */
921
922
923 //
924 // Inode flags (Linux uses octad number, but why ? strange!!!)
925 //
926
927 #undef S_IFMT
928 #undef S_IFDIR
929 #undef S_IFCHR
930 #undef S_IFREG
931 #undef S_IREAD
932 #undef S_IWRITE
933 #undef S_IEXEC
934
935 #define S_IFMT   0x0F000            /* 017 0000 */
936 #define S_IFSOCK 0x0C000            /* 014 0000 */
937 #define S_IFLNK  0x0A000            /* 012 0000 */
938 #define S_IFREG  0x08000            /* 010 0000 */
939 #define S_IFBLK  0x06000            /* 006 0000 */
940 #define S_IFDIR  0x04000            /* 004 0000 */
941 #define S_IFCHR  0x02000            /* 002 0000 */
942 #define S_IFIFO  0x01000            /* 001 0000 */
943 #define S_ISUID  0x00800            /* 000 4000 */
944 #define S_ISGID  0x00400            /* 000 2000 */
945 #define S_ISVTX  0x00200            /* 000 1000 */
946
947 #define S_ISREG(m)      (((m) & S_IFMT) == S_IFREG)
948 #define S_ISSOCK(m)     (((m) & S_IFMT) == S_IFSOCK)
949 #define S_ISLNK(m)      (((m) & S_IFMT) == S_IFLNK)
950 #define S_ISFIL(m)      (((m) & S_IFMT) == S_IFFIL)
951 #define S_ISBLK(m)      (((m) & S_IFMT) == S_IFBLK)
952 #define S_ISDIR(m)      (((m) & S_IFMT) == S_IFDIR)
953 #define S_ISCHR(m)      (((m) & S_IFMT) == S_IFCHR)
954 #define S_ISFIFO(m)     (((m) & S_IFMT) == S_IFIFO)
955
956 #define S_IPERMISSION_MASK 0x1FF /*  */
957
958 #define S_IRWXU  0x1C0              /* 0 0700 */
959 #define S_IRUSR  0x100              /* 0 0400 */
960 #define S_IWUSR  0x080              /* 0 0200 */
961 #define S_IXUSR  0x040              /* 0 0100 */
962
963 #define S_IRWXG  0x038              /* 0 0070 */
964 #define S_IRGRP  0x020              /* 0 0040 */
965 #define S_IWGRP  0x010              /* 0 0020 */
966 #define S_IXGRP  0x008              /* 0 0010 */
967
968 #define S_IRWXO  0x007              /* 0 0007 */
969 #define S_IROTH  0x004              /* 0 0004 */
970 #define S_IWOTH  0x002              /* 0 0002 */
971 #define S_IXOTH  0x001              /* 0 0001 */
972
973 #define S_IRWXUGO   (S_IRWXU|S_IRWXG|S_IRWXO)
974 #define S_IALLUGO   (S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO)
975 #define S_IRUGO     (S_IRUSR|S_IRGRP|S_IROTH)
976 #define S_IWUGO     (S_IWUSR|S_IWGRP|S_IWOTH)
977 #define S_IXUGO     (S_IXUSR|S_IXGRP|S_IXOTH)
978
979 /*
980  *  linux ioctl coding definitions
981  */
982  
983 #define _IOC_NRBITS 8
984 #define _IOC_TYPEBITS   8
985 #define _IOC_SIZEBITS   14
986 #define _IOC_DIRBITS    2
987
988 #define _IOC_NRMASK ((1 << _IOC_NRBITS)-1)
989 #define _IOC_TYPEMASK   ((1 << _IOC_TYPEBITS)-1)
990 #define _IOC_SIZEMASK   ((1 << _IOC_SIZEBITS)-1)
991 #define _IOC_DIRMASK    ((1 << _IOC_DIRBITS)-1)
992
993 #define _IOC_NRSHIFT    0
994 #define _IOC_TYPESHIFT  (_IOC_NRSHIFT+_IOC_NRBITS)
995 #define _IOC_SIZESHIFT  (_IOC_TYPESHIFT+_IOC_TYPEBITS)
996 #define _IOC_DIRSHIFT   (_IOC_SIZESHIFT+_IOC_SIZEBITS)
997
998 /*
999  * Direction bits.
1000  */
1001 #define _IOC_NONE   0U
1002 #define _IOC_WRITE  1U
1003 #define _IOC_READ   2U
1004
1005 #define _IOC(dir,type,nr,size) \
1006     (((dir)  << _IOC_DIRSHIFT) | \
1007      ((type) << _IOC_TYPESHIFT) | \
1008      ((nr)   << _IOC_NRSHIFT) | \
1009      ((size) << _IOC_SIZESHIFT))
1010
1011 /* used to create numbers */
1012 #define _IO(type,nr)      _IOC(_IOC_NONE,(type),(nr),0)
1013 #define _IOR(type,nr,size)    _IOC(_IOC_READ,(type),(nr),sizeof(size))
1014 #define _IOW(type,nr,size)    _IOC(_IOC_WRITE,(type),(nr),sizeof(size))
1015 #define _IOWR(type,nr,size) _IOC(_IOC_READ|_IOC_WRITE,(type),(nr),sizeof(size))
1016
1017 /* used to decode ioctl numbers.. */
1018 #define _IOC_DIR(nr)        (((nr) >> _IOC_DIRSHIFT) & _IOC_DIRMASK)
1019 #define _IOC_TYPE(nr)       (((nr) >> _IOC_TYPESHIFT) & _IOC_TYPEMASK)
1020 #define _IOC_NR(nr)         (((nr) >> _IOC_NRSHIFT) & _IOC_NRMASK)
1021 #define _IOC_SIZE(nr)       (((nr) >> _IOC_SIZESHIFT) & _IOC_SIZEMASK)
1022
1023 /*
1024  * Io vector ...  
1025  */
1026
1027 struct iovec
1028 {
1029     void *iov_base;
1030     size_t iov_len;
1031 };
1032
1033
1034 #define ULONG_LONG_MAX ((__u64)(0xFFFFFFFFFFFFFFFF))
1035 /*
1036  * Convert a string to an unsigned long long integer.
1037  *
1038  * Ignores `locale' stuff.  Assumes that the upper and lower case
1039  * alphabets and digits are each contiguous.
1040  */
1041 static inline __u64
1042 strtoull(
1043         char *nptr,
1044         char **endptr,
1045         int base)
1046 {
1047         char *s = nptr;
1048         __u64 acc, cutoff;
1049         int c, neg = 0, any, cutlim;
1050
1051         /*
1052          * See strtol for comments as to the logic used.
1053          */
1054         do {
1055                 c = *s++;
1056         } while (isspace(c));
1057         if (c == '-') {
1058                 neg = 1;
1059                 c = *s++;
1060         } else if (c == '+')
1061                 c = *s++;
1062         if ((base == 0 || base == 16) &&
1063             c == '0' && (*s == 'x' || *s == 'X')) {
1064                 c = s[1];
1065                 s += 2;
1066                 base = 16;
1067         }
1068         if (base == 0)
1069                 base = c == '0' ? 8 : 10;
1070         cutoff = (__u64)ULONG_LONG_MAX / (__u64)base;
1071         cutlim = (int)((__u64)ULONG_LONG_MAX % (__u64)base);
1072         for (acc = 0, any = 0;; c = *s++) {
1073                 if (isdigit(c))
1074                         c -= '0';
1075                 else if (isalpha(c))
1076                         c -= isupper(c) ? 'A' - 10 : 'a' - 10;
1077                 else
1078                         break;
1079                 if (c >= base)
1080                         break;
1081                if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
1082                         any = -1;
1083                 else {
1084                         any = 1;
1085                         acc *= base;
1086                         acc += c;
1087                 }
1088         }
1089         if (any < 0) {
1090                 acc = ULONG_LONG_MAX;
1091         } else if (neg)
1092                 acc = 0 - acc;
1093         if (endptr != 0)
1094                 *endptr = (char *) (any ? s - 1 : nptr);
1095         return (acc);
1096 }
1097
1098 #endif