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