Whamcloud - gitweb
- merge 0.7rc1 from b_devel to HEAD (20030612 merge point)
[fs/lustre-release.git] / lustre / include / liblustre.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2001 Cluster File Systems, Inc. <info@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  * User-space Lustre headers.
22  *
23  */
24 #ifndef LIBLUSTRE_H__
25 #define LIBLUSTRE_H__
26
27 #include <sys/mman.h>
28 #ifndef  __CYGWIN__
29 #include <stdint.h>
30 #include <asm/page.h>
31 #else
32 #include <sys/types.h>
33 #include "ioctl.h"
34 #endif
35 #include <stdio.h>
36 #include <sys/ioctl.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <sys/stat.h>
41 #include <sys/vfs.h>
42
43 #include <portals/list.h>
44 #include <portals/p30.h>
45 #include <linux/kp30.h>
46
47 /* definitions for liblustre */
48
49 #ifdef __CYGWIN__
50
51 #define PAGE_SHIFT 12
52 #define PAGE_SIZE (1UL << PAGE_SHIFT)
53 #define PAGE_MASK (~(PAGE_SIZE-1))
54 #define loff_t __u64
55 #define ERESTART 2001
56 typedef unsigned short umode_t;
57
58 #endif
59
60 /* always adopt 2.5 definitions */
61 #define KERNEL_VERSION(a,b,c) ((a)*100+(b)*10+c)
62 #define LINUX_VERSION_CODE (2*200+5*10+0)
63
64 static inline void inter_module_put(void *a)
65 {
66         return;
67 }
68
69 extern ptl_handle_ni_t         tcpnal_ni;
70
71 void *inter_module_get(char *arg);
72
73 /* cheats for now */
74
75 struct work_struct {
76         void (*ws_task)(void *arg);
77         void *ws_arg;
78 };
79
80 static inline void prepare_work(struct work_struct *q, void (*t)(void *),
81                                 void *arg)
82 {
83         q->ws_task = t;
84         q->ws_arg = arg;
85         return;
86 }
87
88 static inline void schedule_work(struct work_struct *q)
89 {
90         q->ws_task(q->ws_arg);
91 }
92
93
94 #define strnlen(a,b) strlen(a)
95 static inline void *kmalloc(int size, int prot)
96 {
97         return malloc(size);
98 }
99 #define vmalloc malloc
100 #define vfree free
101 #define kfree(a) free(a)
102 #define GFP_KERNEL 1
103 #define GFP_HIGHUSER 1
104 #define IS_ERR(a) (((a) && abs((int)(a)) < 500) ? 1 : 0)
105 #define PTR_ERR(a) ((int)(a))
106
107 #define capable(foo) 1
108 #define CAP_SYS_ADMIN 1
109
110 typedef struct {
111         void *cwd;
112
113 }mm_segment_t;
114
115 typedef void *read_proc_t;
116 typedef void *write_proc_t;
117
118
119 /* byteorder */
120 #define __swab16(x) \
121 ({ \
122         __u16 __x = (x); \
123         ((__u16)( \
124                 (((__u16)(__x) & (__u16)0x00ffU) << 8) | \
125                 (((__u16)(__x) & (__u16)0xff00U) >> 8) )); \
126 })
127
128 #define __swab32(x) \
129 ({ \
130         __u32 __x = (x); \
131         ((__u32)( \
132                 (((__u32)(__x) & (__u32)0x000000ffUL) << 24) | \
133                 (((__u32)(__x) & (__u32)0x0000ff00UL) <<  8) | \
134                 (((__u32)(__x) & (__u32)0x00ff0000UL) >>  8) | \
135                 (((__u32)(__x) & (__u32)0xff000000UL) >> 24) )); \
136 })
137
138 #define __swab64(x) \
139 ({ \
140         __u64 __x = (x); \
141         ((__u64)( \
142                 (__u64)(((__u64)(__x) & (__u64)0x00000000000000ffULL) << 56) | \
143                 (__u64)(((__u64)(__x) & (__u64)0x000000000000ff00ULL) << 40) | \
144                 (__u64)(((__u64)(__x) & (__u64)0x0000000000ff0000ULL) << 24) | \
145                 (__u64)(((__u64)(__x) & (__u64)0x00000000ff000000ULL) <<  8) | \
146                 (__u64)(((__u64)(__x) & (__u64)0x000000ff00000000ULL) >>  8) | \
147                 (__u64)(((__u64)(__x) & (__u64)0x0000ff0000000000ULL) >> 24) | \
148                 (__u64)(((__u64)(__x) & (__u64)0x00ff000000000000ULL) >> 40) | \
149                 (__u64)(((__u64)(__x) & (__u64)0xff00000000000000ULL) >> 56) )); \
150 })
151
152 #define __swab16s(x)    __swab16(*(x))
153 #define __swab32s(x)    __swab32(*(x))
154 #define __swab64s(x)    __swab64(*(x))
155
156 #define __LITTLE_ENDIAN__
157 #ifdef  __LITTLE_ENDIAN__
158 # define le16_to_cpu(x) ((__u16)(x))
159 # define cpu_to_le16(x) ((__u16)(x))
160 # define le32_to_cpu(x) ((__u32)(x))
161 # define cpu_to_le32(x) ((__u32)(x))
162 # define le64_to_cpu(x) ((__u64)(x))
163 # define cpu_to_le64(x) ((__u64)(x))
164 #else
165 # define le16_to_cpu(x) __swab16(x)
166 # define cpu_to_le16(x) __swab16(x)
167 # define le32_to_cpu(x) __swab32(x)
168 # define cpu_to_le32(x) __swab32(x)
169 # define le64_to_cpu(x) __swab64(x)
170 # define cpu_to_le64(x) __swab64(x)
171 # error "do more check here!!!"
172 #endif
173
174 /* bits ops */
175 static __inline__ int set_bit(int nr,long * addr)
176 {
177         int     mask, retval;
178
179         addr += nr >> 5;
180         mask = 1 << (nr & 0x1f);
181         retval = (mask & *addr) != 0;
182         *addr |= mask;
183         return retval;
184 }
185
186 static __inline__ int clear_bit(int nr, long * addr)
187 {
188         int     mask, retval;
189
190         addr += nr >> 5;
191         mask = 1 << (nr & 0x1f);
192         retval = (mask & *addr) != 0;
193         *addr &= ~mask;
194         return retval;
195 }
196
197 static __inline__ int test_bit(int nr, long * addr)
198 {
199         int     mask;
200
201         addr += nr >> 5;
202         mask = 1 << (nr & 0x1f);
203         return ((mask & *addr) != 0);
204 }
205
206 /* modules */
207
208 struct module {
209         int count;
210 };
211
212 static inline void MODULE_AUTHOR(char *name)
213 {
214         printf("%s\n", name);
215 }
216 #define MODULE_DESCRIPTION(name) MODULE_AUTHOR(name)
217 #define MODULE_LICENSE(name) MODULE_AUTHOR(name)
218
219 #define THIS_MODULE NULL
220 #define __init
221 #define __exit
222
223 /* devices */
224
225 static inline int misc_register(void *foo)
226 {
227         return 0;
228 }
229 #define misc_deregister misc_register
230
231 #define __MOD_INC_USE_COUNT(m)  do {int a = 1; a++; } while (0)
232 #define __MOD_DEC_USE_COUNT(m)  do {int a = 1; a++; } while (0)
233 #define MOD_INC_USE_COUNT  do {int a = 1; a++; } while (0)
234 #define MOD_DEC_USE_COUNT  do {int a = 1; a++; } while (0)
235
236 /* module initialization */
237 extern int init_obdclass(void);
238 extern int ptlrpc_init(void);
239 extern int ldlm_init(void);
240 extern int osc_init(void);
241 extern int lov_init(void);
242 extern int mdc_init(void);
243 extern int echo_client_init(void);
244
245
246
247 /* general stuff */
248 #define jiffies 0
249
250 #define EXPORT_SYMBOL(S)
251
252 typedef int spinlock_t;
253 typedef __u64 kdev_t;
254
255 #define SPIN_LOCK_UNLOCKED 0
256 #define spin_lock(l) do {int a = 1; a++; } while (0)
257 #define spin_unlock(l) do {int a= 1; a++; } while (0)
258 #define spin_lock_init(l) do {int a= 1; a++; } while (0)
259 static inline void spin_lock_bh(spinlock_t *l)
260 {
261         return;
262 }
263 static inline void spin_unlock_bh(spinlock_t *l)
264 {
265         return;
266 }
267 static inline void spin_unlock_irqrestore(spinlock_t *a, long b)
268 {
269         return;
270 }
271 static inline void spin_lock_irqsave(spinlock_t *a, long b)
272 {
273         return;
274 }
275
276 #define barrier() do {int a= 1; a++; } while (0)
277
278 #define min(x,y) ((x)<(y) ? (x) : (y))
279 #define max(x,y) ((x)>(y) ? (x) : (y))
280
281 /* registering symbols */
282
283 #define ERESTARTSYS ERESTART
284 #define HZ 1
285
286 /* random */
287
288 static inline void get_random_bytes(void *ptr, int size)
289 {
290         int *p = (int *)ptr;
291         int i, count = size/sizeof(int);
292
293         for (i = 0; i< count; i++)
294                 *p++ = rand();
295 }
296
297 /* memory */
298
299 /* FIXME */
300 #define num_physpages (16 * 1024)
301
302 static inline int copy_from_user(void *a,void *b, int c)
303 {
304         memcpy(a,b,c);
305         return 0;
306 }
307
308 static inline int copy_to_user(void *a,void *b, int c)
309 {
310         memcpy(a,b,c);
311         return 0;
312 }
313
314
315 /* slabs */
316 typedef struct {
317          int size;
318 } kmem_cache_t;
319 #define SLAB_HWCACHE_ALIGN 0
320 static inline kmem_cache_t *
321 kmem_cache_create(const char *name, size_t objsize, size_t cdum,
322                   unsigned long d,
323                   void (*e)(void *, kmem_cache_t *, unsigned long),
324                   void (*f)(void *, kmem_cache_t *, unsigned long))
325 {
326         kmem_cache_t *c;
327         c = malloc(sizeof(*c));
328         if (!c)
329                 return NULL;
330         c->size = objsize;
331         CDEBUG(D_MALLOC, "alloc slab cache %s at %p, objsize %d\n",
332                name, c, (int)objsize);
333         return c;
334 };
335
336 static inline int kmem_cache_destroy(kmem_cache_t *a)
337 {
338         CDEBUG(D_MALLOC, "destroy slab cache %p, objsize %u\n", a, a->size);
339         free(a);
340         return 0;
341 }
342 #define kmem_cache_validate(a,b) 1
343 #define kmem_cache_alloc(cache, prio) malloc(cache->size)
344 #define kmem_cache_free(cache, obj) free(obj)
345
346 #define PAGE_CACHE_SIZE PAGE_SIZE
347 #define PAGE_CACHE_SHIFT 12
348 #define PAGE_CACHE_MASK PAGE_MASK
349
350 struct page {
351         void *addr;
352         int index;
353 };
354
355 #define kmap(page) (page)->addr
356 #define kunmap(a) do { int foo = 1; foo++; } while (0)
357
358 static inline struct page *alloc_pages(int mask, unsigned long foo)
359 {
360         struct page *pg = malloc(sizeof(*pg));
361
362         if (!pg)
363                 return NULL;
364 #ifdef MAP_ANONYMOUS
365         pg->addr = mmap(0, PAGE_SIZE, PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
366 #else
367         pg->addr = malloc(PAGE_SIZE);
368 #endif
369
370         if (!pg->addr) {
371                 free(pg);
372                 return NULL;
373         }
374         return pg;
375 }
376
377 static inline void __free_pages(struct page *pg, int what)
378 {
379 #ifdef MAP_ANONYMOUS
380         munmap(pg->addr, PAGE_SIZE);
381 #else
382         free(pg->addr);
383 #endif
384         free(pg);
385 }
386
387 static inline struct page* __grab_cache_page(int index)
388 {
389         struct page *pg = alloc_pages(0, 0);
390
391         if (pg)
392                 pg->index = index;
393         return pg;
394 }
395
396 #define grab_cache_page(index) __grab_cache_page(index)
397 #define page_cache_release(page) __free_pages(page, 0)
398
399 /* arithmetic */
400 #define do_div(a,b)                     \
401         ({                              \
402                 unsigned long ret;      \
403                 ret = (a)%(b);          \
404                 (a) = (a)/(b);          \
405                 (ret);                  \
406         })
407
408 /* VFS stuff */
409 #define ATTR_MODE       1
410 #define ATTR_UID        2
411 #define ATTR_GID        4
412 #define ATTR_SIZE       8
413 #define ATTR_ATIME      16
414 #define ATTR_MTIME      32
415 #define ATTR_CTIME      64
416 #define ATTR_ATIME_SET  128
417 #define ATTR_MTIME_SET  256
418 #define ATTR_FORCE      512     /* Not a change, but a change it */
419 #define ATTR_ATTR_FLAG  1024
420 #define ATTR_RAW        2048    /* file system, not vfs will massage attrs */
421 #define ATTR_FROM_OPEN  4096    /* called from open path, ie O_TRUNC */
422
423 struct iattr {
424         unsigned int    ia_valid;
425         umode_t         ia_mode;
426         uid_t           ia_uid;
427         gid_t           ia_gid;
428         loff_t          ia_size;
429         time_t          ia_atime;
430         time_t          ia_mtime;
431         time_t          ia_ctime;
432         unsigned int    ia_attr_flags;
433 };
434
435 /* copy from kernel header */
436 #define IT_OPEN     (1)
437 #define IT_CREAT    (1<<1)
438 #define IT_READDIR  (1<<2)
439 #define IT_GETATTR  (1<<3)
440 #define IT_LOOKUP   (1<<4)
441 #define IT_UNLINK   (1<<5)
442
443 struct lookup_intent {
444         int it_op;
445         int it_mode;
446         int it_flags;
447         int it_disposition;
448         int it_status;
449         struct iattr *it_iattr;
450         __u64 it_lock_handle[2];
451         int it_lock_mode;
452         void *it_data;
453 };
454
455 struct dentry {
456         int d_count;
457 };
458
459 struct vfsmount {
460         void *pwd;
461 };
462
463 #define cpu_to_le32(x) ((__u32)(x))
464
465 /* semaphores */
466 struct semaphore {
467         int count;
468 };
469
470 #define down(a) do {(a)->count++;} while (0)
471 #define up(a) do {(a)->count--;} while (0)
472 #define sema_init(a,b) do { (a)->count = b; } while (0)
473
474 typedef struct  {
475         struct list_head sleepers;
476 } wait_queue_head_t;
477
478 typedef struct  {
479         struct list_head sleeping;
480         void *process;
481 } wait_queue_t;
482
483 struct signal {
484         int signal;
485 };
486
487 struct fs_struct {
488         int umask;
489 };
490
491 struct task_struct {
492         struct fs_struct *fs;
493         int state;
494         struct signal pending;
495         char comm[32];
496         int pid;
497         int fsuid;
498         int fsgid;
499         __u32 cap_effective;
500 };
501
502 extern struct task_struct *current;
503
504 #define in_group_p(a) 0 /* FIXME */
505
506 #define set_current_state(foo) do { current->state = foo; } while (0)
507
508 #define init_waitqueue_entry(q,p) do { (q)->process = p; } while (0)
509 #define add_wait_queue(q,p) do {  list_add(&(q)->sleepers, &(p)->sleeping); } while (0)
510 #define del_wait_queue(p) do { list_del(&(p)->sleeping); } while (0)
511 #define remove_wait_queue(q,p) do { list_del(&(p)->sleeping); } while (0)
512
513 #define init_waitqueue_head(l) INIT_LIST_HEAD(&(l)->sleepers)
514 #define wake_up(l) do { int a; a++; } while (0)
515 #define TASK_INTERRUPTIBLE 0
516 #define TASK_UNINTERRUPTIBLE 1
517 #define TASK_RUNNING 2
518
519 #define in_interrupt() (0)
520
521 #define schedule() do { int a; a++; } while (0)
522 static inline int schedule_timeout(signed long t)
523 {
524         return 0;
525 }
526
527 #define lock_kernel() do { int a; a++; } while (0)
528 #define daemonize(l) do { int a; a++; } while (0)
529 #define sigfillset(l) do { int a; a++; } while (0)
530 #define recalc_sigpending(l) do { int a; a++; } while (0)
531 #define kernel_thread(l,m,n)
532
533 static inline int call_usermodehelper(char *prog, char **argv, char **evnp, int unknown)
534 {
535         return 0;
536 }
537
538
539
540 #define KERN_INFO
541
542
543
544 struct timer_list {
545         struct list_head tl_list;
546         void (*function)(unsigned long unused);
547         void *data;
548         int expires;
549 };
550
551 static inline int timer_pending(struct timer_list *l)
552 {
553         if (l->expires > jiffies)
554                 return 1;
555         else
556                 return 0;
557 }
558
559 static inline int init_timer(struct timer_list *l)
560 {
561         INIT_LIST_HEAD(&l->tl_list);
562         return 0;
563 }
564
565 static inline void mod_timer(struct timer_list *l, int thetime)
566 {
567         l->expires = thetime;
568 }
569
570 static inline void del_timer(struct timer_list *l)
571 {
572         free(l);
573 }
574
575 typedef struct { volatile int counter; } atomic_t;
576
577 #define atomic_read(a) ((a)->counter)
578 #define atomic_set(a,b) do {(a)->counter = b; } while (0)
579 #define atomic_dec_and_test(a) ((--((a)->counter)) == 0)
580 #define atomic_inc(a)  (((a)->counter)++)
581 #define atomic_dec(a)  do { (a)->counter--; } while (0)
582 #define atomic_add(b,a)  do {(a)->counter += b;} while (0)
583 #define atomic_sub(b,a)  do {(a)->counter -= b;} while (0)
584
585 #define LBUG()                                                          \
586         do {                                                            \
587                 printf("!!!LBUG at %s:%d\n", __FILE__, __LINE__);       \
588                 sleep(1000000);                                         \
589         } while (0)
590
591 #include <linux/obd_support.h>
592 #include <linux/lustre_idl.h>
593 #include <linux/lustre_lib.h>
594 #include <linux/lustre_import.h>
595 #include <linux/lustre_export.h>
596 #include <linux/lustre_net.h>
597
598
599 #endif
600