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