Whamcloud - gitweb
Land b_smallfix onto HEAD (20040416_1638) (more 2.6 build fixes)
[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 #include <asm/byteorder.h>
29 #ifndef  __CYGWIN__
30 #include <stdint.h>
31 #include <asm/page.h>
32 #else
33 #include <sys/types.h>
34 #include "ioctl.h"
35 #endif
36 #include <stdio.h>
37 #include <sys/ioctl.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <sys/stat.h>
42 #include <sys/vfs.h>
43
44 #include <portals/list.h>
45 #include <portals/p30.h>
46 #include <linux/kp30.h>
47
48 /* definitions for liblustre */
49
50 #ifdef __CYGWIN__
51
52 #define PAGE_SHIFT 12
53 #define PAGE_SIZE (1UL << PAGE_SHIFT)
54 #define PAGE_MASK (~(PAGE_SIZE-1))
55 #define loff_t long long
56 #define ERESTART 2001
57 typedef unsigned short umode_t;
58
59 #endif
60
61 /* This is because lprocfs_status.h gets included here indirectly.  It would
62  * be much better to just avoid lprocfs being included into liblustre entirely
63  * but that requires more header surgery than I can handle right now.
64  */
65 #ifndef smp_processor_id
66 #define smp_processor_id() 0
67 #endif
68
69 /* always adopt 2.5 definitions */
70 #define KERNEL_VERSION(a,b,c) ((a)*100+(b)*10+c)
71 #define LINUX_VERSION_CODE (2*200+5*10+0)
72
73 static inline void inter_module_put(void *a)
74 {
75         return;
76 }
77
78 extern ptl_handle_ni_t         tcpnal_ni;
79
80 void *inter_module_get(char *arg);
81
82 /* cheats for now */
83
84 struct work_struct {
85         void (*ws_task)(void *arg);
86         void *ws_arg;
87 };
88
89 static inline void prepare_work(struct work_struct *q, void (*t)(void *),
90                                 void *arg)
91 {
92         q->ws_task = t;
93         q->ws_arg = arg;
94         return;
95 }
96
97 static inline void schedule_work(struct work_struct *q)
98 {
99         q->ws_task(q->ws_arg);
100 }
101
102
103 #define strnlen(a,b) strlen(a)
104 static inline void *kmalloc(int size, int prot)
105 {
106         return malloc(size);
107 }
108 #define vmalloc malloc
109 #define vfree free
110 #define kfree(a) free(a)
111 #define GFP_KERNEL 1
112 #define GFP_HIGHUSER 1
113 #define GFP_ATOMIC 1
114 #define GFP_NOFS 1
115 #define IS_ERR(a) (((a) && abs((long)(a)) < 500) ? 1 : 0)
116 #define PTR_ERR(a) ((long)(a))
117 #define ERR_PTR(a) ((void*)((long)(a)))
118
119 #define capable(foo) 1
120 #define CAP_SYS_ADMIN 1
121
122 typedef struct {
123         void *cwd;
124 }mm_segment_t;
125
126 typedef int (read_proc_t)(char *page, char **start, off_t off,
127                           int count, int *eof, void *data);
128
129 struct file; /* forward ref */
130 typedef int (write_proc_t)(struct file *file, const char *buffer,
131                            unsigned long count, void *data);
132
133 # define le16_to_cpu(x) __le16_to_cpu(x)
134 # define cpu_to_le16(x) __cpu_to_le16(x)
135 # define le32_to_cpu(x) __le32_to_cpu(x)
136 # define cpu_to_le32(x) __cpu_to_le32(x)
137 # define le64_to_cpu(x) __le64_to_cpu(x)
138 # define cpu_to_le64(x) __cpu_to_le64(x)
139
140 #define NIPQUAD(addr) \
141         ((unsigned char *)&addr)[0], \
142         ((unsigned char *)&addr)[1], \
143         ((unsigned char *)&addr)[2], \
144         ((unsigned char *)&addr)[3]
145                                                                                                                         
146 #if defined(__LITTLE_ENDIAN)
147 #define HIPQUAD(addr) \
148         ((unsigned char *)&addr)[3], \
149         ((unsigned char *)&addr)[2], \
150         ((unsigned char *)&addr)[1], \
151         ((unsigned char *)&addr)[0]
152 #elif defined(__BIG_ENDIAN)
153 #define HIPQUAD NIPQUAD
154 #else
155 #error "Undefined byteorder??"
156 #endif /* __LITTLE_ENDIAN */
157
158 /* bits ops */
159 static __inline__ int set_bit(int nr,long * addr)
160 {
161         int     mask, retval;
162
163         addr += nr >> 5;
164         mask = 1 << (nr & 0x1f);
165         retval = (mask & *addr) != 0;
166         *addr |= mask;
167         return retval;
168 }
169
170 static __inline__ int clear_bit(int nr, long * addr)
171 {
172         int     mask, retval;
173
174         addr += nr >> 5;
175         mask = 1 << (nr & 0x1f);
176         retval = (mask & *addr) != 0;
177         *addr &= ~mask;
178         return retval;
179 }
180
181 static __inline__ int test_bit(int nr, long * addr)
182 {
183         int     mask;
184
185         addr += nr >> 5;
186         mask = 1 << (nr & 0x1f);
187         return ((mask & *addr) != 0);
188 }
189
190 static __inline__ int ext2_set_bit(int nr, void *addr)
191 {
192         return set_bit(nr, (long*)addr);
193 }
194
195 static __inline__ int ext2_clear_bit(int nr, void *addr)
196 {
197         return clear_bit(nr, (long*)addr);
198 }
199
200 static __inline__ int ext2_test_bit(int nr, void *addr)
201 {
202         return test_bit(nr, (long*)addr);
203 }
204
205 /* modules */
206
207 struct module {
208         int count;
209 };
210
211 static inline void MODULE_AUTHOR(char *name)
212 {
213         printf("%s\n", name);
214 }
215 #define MODULE_DESCRIPTION(name) MODULE_AUTHOR(name)
216 #define MODULE_LICENSE(name) MODULE_AUTHOR(name)
217
218 #define THIS_MODULE NULL
219 #define __init
220 #define __exit
221
222 /* devices */
223
224 static inline int misc_register(void *foo)
225 {
226         return 0;
227 }
228
229 static inline int misc_deregister(void *foo)
230 {
231         return 0;
232 }
233
234 static inline int request_module(char *name)
235 {
236         return (-EINVAL);
237 }
238
239 #define __MOD_INC_USE_COUNT(m)  do {} while (0)
240 #define __MOD_DEC_USE_COUNT(m)  do {} while (0)
241 #define MOD_INC_USE_COUNT       do {} while (0)
242 #define MOD_DEC_USE_COUNT       do {} while (0)
243 static inline void __module_get(struct module *module)
244 {
245 }
246
247 static inline int try_module_get(struct module *module)
248 {
249         return 1;
250 }
251
252 static inline void module_put(struct module *module)
253 {
254 }
255
256 /* module initialization */
257 extern int init_obdclass(void);
258 extern int ptlrpc_init(void);
259 extern int ldlm_init(void);
260 extern int osc_init(void);
261 extern int lov_init(void);
262 extern int mdc_init(void);
263 extern int echo_client_init(void);
264
265
266
267 /* general stuff */
268
269 #define EXPORT_SYMBOL(S)
270
271 typedef struct { } spinlock_t;
272 typedef __u64 kdev_t;
273
274 #define SPIN_LOCK_UNLOCKED (spinlock_t) { }
275 #define LASSERT_SPIN_LOCKED(lock) do {} while(0)
276
277 static inline void spin_lock(spinlock_t *l) {return;}
278 static inline void spin_unlock(spinlock_t *l) {return;}
279 static inline void spin_lock_init(spinlock_t *l) {return;}
280 static inline void local_irq_save(unsigned long flag) {return;}
281 static inline void local_irq_restore(unsigned long flag) {return;}
282 static inline int spin_is_locked(spinlock_t *l) {return 1;}
283
284 static inline void spin_lock_bh(spinlock_t *l) {}
285 static inline void spin_unlock_bh(spinlock_t *l) {}
286 static inline void spin_lock_irqsave(spinlock_t *a, unsigned long b) {}
287 static inline void spin_unlock_irqrestore(spinlock_t *a, unsigned long b) {}
288
289 #define min(x,y) ((x)<(y) ? (x) : (y))
290 #define max(x,y) ((x)>(y) ? (x) : (y))
291
292 #ifndef min_t
293 #define min_t(type,x,y) \
294         ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
295 #endif
296 #ifndef max_t
297 #define max_t(type,x,y) \
298         ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
299 #endif
300
301 /* registering symbols */
302
303 #define ERESTARTSYS ERESTART
304 #define HZ 1
305
306 /* random */
307
308 static inline void get_random_bytes(void *ptr, int size)
309 {
310         int *p = (int *)ptr;
311         int i, count = size/sizeof(int);
312
313         for (i = 0; i< count; i++)
314                 *p++ = rand();
315 }
316
317 /* memory */
318
319 /* FIXME */
320 #define num_physpages (16 * 1024)
321
322 static inline int copy_from_user(void *a,void *b, int c)
323 {
324         memcpy(a,b,c);
325         return 0;
326 }
327
328 static inline int copy_to_user(void *a,void *b, int c)
329 {
330         memcpy(a,b,c);
331         return 0;
332 }
333
334
335 /* slabs */
336 typedef struct {
337          int size;
338 } kmem_cache_t;
339 #define SLAB_HWCACHE_ALIGN 0
340 static inline kmem_cache_t *
341 kmem_cache_create(const char *name, size_t objsize, size_t cdum,
342                   unsigned long d,
343                   void (*e)(void *, kmem_cache_t *, unsigned long),
344                   void (*f)(void *, kmem_cache_t *, unsigned long))
345 {
346         kmem_cache_t *c;
347         c = malloc(sizeof(*c));
348         if (!c)
349                 return NULL;
350         c->size = objsize;
351         CDEBUG(D_MALLOC, "alloc slab cache %s at %p, objsize %d\n",
352                name, c, (int)objsize);
353         return c;
354 };
355
356 static inline int kmem_cache_destroy(kmem_cache_t *a)
357 {
358         CDEBUG(D_MALLOC, "destroy slab cache %p, objsize %u\n", a, a->size);
359         free(a);
360         return 0;
361 }
362 #define kmem_cache_alloc(cache, prio) malloc(cache->size)
363 #define kmem_cache_free(cache, obj) free(obj)
364
365 #define PAGE_CACHE_SIZE PAGE_SIZE
366 #define PAGE_CACHE_SHIFT 12
367 #define PAGE_CACHE_MASK PAGE_MASK
368
369 /* XXX
370  * for this moment, liblusre will not rely OST for non-page-aligned write
371  */
372 #define LIBLUSTRE_HANDLE_UNALIGNED_PAGE
373
374 struct page {
375         void   *addr;
376         unsigned long index;
377         struct list_head list;
378         unsigned long private;
379
380         /* internally used by liblustre file i/o */
381         int     _offset;
382         int     _count;
383 #ifdef LIBLUSTRE_HANDLE_UNALIGNED_PAGE
384         int     _managed;
385 #endif
386 };
387
388 /* 2.4 defines */
389 #define PAGE_LIST_ENTRY list
390 #define PAGE_LIST(page) ((page)->list)
391
392 #define kmap(page) (page)->addr
393 #define kunmap(a) do {} while (0)
394
395 static inline struct page *alloc_pages(int mask, unsigned long order)
396 {
397         struct page *pg = malloc(sizeof(*pg));
398
399         if (!pg)
400                 return NULL;
401 #if 0 //#ifdef MAP_ANONYMOUS
402         pg->addr = mmap(0, PAGE_SIZE << order, PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
403 #else
404         pg->addr = malloc(PAGE_SIZE << order);
405 #endif
406
407         if (!pg->addr) {
408                 free(pg);
409                 return NULL;
410         }
411         return pg;
412 }
413
414 #define alloc_page(mask) alloc_pages((mask), 0)
415
416 static inline void __free_pages(struct page *pg, int what)
417 {
418 #if 0 //#ifdef MAP_ANONYMOUS
419         munmap(pg->addr, PAGE_SIZE);
420 #else
421         free(pg->addr);
422 #endif
423         free(pg);
424 }
425
426 #define __free_page(page) __free_pages((page), 0)
427 #define free_page(page) __free_page(page)
428
429 static inline struct page* __grab_cache_page(unsigned long index)
430 {
431         struct page *pg = alloc_pages(0, 0);
432
433         if (pg)
434                 pg->index = index;
435         return pg;
436 }
437
438 #define grab_cache_page(index) __grab_cache_page(index)
439 #define page_cache_release(page) __free_pages(page, 0)
440
441 /* arithmetic */
442 #define do_div(a,b)                     \
443         ({                              \
444                 unsigned long remainder;\
445                 remainder = (a) % (b);  \
446                 (a) = (a) / (b);        \
447                 (remainder);            \
448         })
449
450 /* VFS stuff */
451 #define ATTR_MODE       0x0001
452 #define ATTR_UID        0x0002
453 #define ATTR_GID        0x0004
454 #define ATTR_SIZE       0x0008
455 #define ATTR_ATIME      0x0010
456 #define ATTR_MTIME      0x0020
457 #define ATTR_CTIME      0x0040
458 #define ATTR_ATIME_SET  0x0080
459 #define ATTR_MTIME_SET  0x0100
460 #define ATTR_FORCE      0x0200  /* Not a change, but a change it */
461 #define ATTR_ATTR_FLAG  0x0400
462 #define ATTR_RAW        0x0800  /* file system, not vfs will massage attrs */
463 #define ATTR_FROM_OPEN  0x1000  /* called from open path, ie O_TRUNC */
464 #define ATTR_CTIME_SET  0x2000
465
466 struct iattr {
467         unsigned int    ia_valid;
468         umode_t         ia_mode;
469         uid_t           ia_uid;
470         gid_t           ia_gid;
471         loff_t          ia_size;
472         time_t          ia_atime;
473         time_t          ia_mtime;
474         time_t          ia_ctime;
475         unsigned int    ia_attr_flags;
476 };
477
478 #define IT_OPEN     0x0001
479 #define IT_CREAT    0x0002
480 #define IT_READDIR  0x0004
481 #define IT_GETATTR  0x0008
482 #define IT_LOOKUP   0x0010
483 #define IT_UNLINK   0x0020
484 #define IT_GETXATTR 0x0040
485 #define IT_EXEC     0x0080
486 #define IT_PIN      0x0100
487
488 #define IT_FL_LOCKED   0x0001
489 #define IT_FL_FOLLOWED 0x0002 /* set by vfs_follow_link */
490
491 #define INTENT_MAGIC 0x19620323
492
493 struct lustre_intent_data {
494         int       it_disposition;
495         int       it_status;
496         __u64     it_lock_handle;
497         void     *it_data;
498         int       it_lock_mode;
499         int it_int_flags;
500 };
501 struct lookup_intent {
502         int     it_magic;
503         void    (*it_op_release)(struct lookup_intent *);
504         int     it_op;
505         int     it_flags;
506         int     it_create_mode;
507         union {
508                 struct lustre_intent_data lustre;
509         } d;
510 };
511
512 static inline void intent_init(struct lookup_intent *it, int op, int flags)
513 {
514         memset(it, 0, sizeof(*it));
515         it->it_magic = INTENT_MAGIC;
516         it->it_op = op;
517         it->it_flags = flags;
518 }
519
520
521 struct dentry {
522         int d_count;
523 };
524
525 struct vfsmount {
526         void *pwd;
527 };
528
529 /* semaphores */
530 struct rw_semaphore {
531         int count;
532 };
533
534 /* semaphores */
535 struct semaphore {
536         int count;
537 };
538
539 /* use the macro's argument to avoid unused warnings */
540 #define down(a) do { (void)a; } while (0)
541 #define up(a) do { (void)a; } while (0)
542 #define down_read(a) do { (void)a; } while (0)
543 #define up_read(a) do { (void)a; } while (0)
544 #define down_write(a) do { (void)a; } while (0)
545 #define up_write(a) do { (void)a; } while (0)
546 #define sema_init(a,b) do { (void)a; } while (0)
547 #define init_rwsem(a) do { (void)a; } while (0)
548 #define DECLARE_MUTEX(name)     \
549         struct semaphore name = { 1 }
550 static inline void init_MUTEX (struct semaphore *sem)
551 {
552         sema_init(sem, 1);
553 }
554
555
556 typedef struct  {
557         struct list_head sleepers;
558 } wait_queue_head_t;
559
560 typedef struct  {
561         struct list_head sleeping;
562         void *process;
563 } wait_queue_t;
564
565 struct signal {
566         int signal;
567 };
568
569 struct fs_struct {
570         int umask;
571 };
572
573 struct task_struct {
574         struct fs_struct *fs;
575         int state;
576         struct signal pending;
577         char comm[32];
578         int pid;
579         int fsuid;
580         int fsgid;
581         __u32 cap_effective;
582 };
583
584 extern struct task_struct *current;
585
586 #define in_group_p(a) 0 /* FIXME */
587
588 #define set_current_state(foo) do { current->state = foo; } while (0)
589
590 #define init_waitqueue_entry(q,p) do { (q)->process = p; } while (0)
591 #define add_wait_queue(q,p) do {  list_add(&(q)->sleepers, &(p)->sleeping); } while (0)
592 #define del_wait_queue(p) do { list_del(&(p)->sleeping); } while (0)
593 #define remove_wait_queue(q,p) do { list_del(&(p)->sleeping); } while (0)
594
595 #define DECLARE_WAIT_QUEUE_HEAD(HEAD)                           \
596         wait_queue_head_t HEAD = {                              \
597                 .sleepers = LIST_HEAD_INIT(HEAD.sleepers)       \
598         }
599 #define init_waitqueue_head(l) INIT_LIST_HEAD(&(l)->sleepers)
600 #define wake_up(l) do { int a; a++; } while (0)
601 #define TASK_INTERRUPTIBLE 0
602 #define TASK_UNINTERRUPTIBLE 1
603 #define TASK_RUNNING 2
604
605 #define wait_event_interruptible(wq, condition)                         \
606 ({                                                                      \
607         struct l_wait_info lwi;                                         \
608         int timeout = 100000000;/* for ever */                          \
609         int ret;                                                        \
610                                                                         \
611         lwi = LWI_TIMEOUT(timeout, NULL, NULL);                         \
612         ret = l_wait_event(NULL, condition, &lwi);                      \
613                                                                         \
614         ret;                                                            \
615 })
616
617 #define in_interrupt() (0)
618
619 #define schedule() do {} while (0)
620 static inline int schedule_timeout(signed long t)
621 {
622         return 0;
623 }
624
625 #define lock_kernel() do {} while (0)
626 #define daemonize(l) do {} while (0)
627 #define sigfillset(l) do {} while (0)
628 #define recalc_sigpending(l) do {} while (0)
629 #define kernel_thread(l,m,n) LBUG()
630
631 #define USERMODEHELPER(path, argv, envp) (0)
632 #define SIGNAL_MASK_ASSERT()
633 #define KERN_INFO
634
635 #include <sys/time.h>
636 #if HZ != 1
637 #error "liblustre's jiffies currently expects HZ to be 1"
638 #endif
639 #define jiffies                                 \
640 ({                                              \
641         unsigned long _ret = 0;                 \
642         struct timeval tv;                      \
643         if (gettimeofday(&tv, NULL) == 0)       \
644                 _ret = tv.tv_sec;               \
645         _ret;                                   \
646 })
647 #define time_after(a, b) ((long)(b) - (long)(a) > 0)
648 #define time_before(a, b) time_after(b,a)
649
650 struct timer_list {
651         struct list_head tl_list;
652         void (*function)(unsigned long unused);
653         unsigned long data;
654         long expires;
655 };
656
657 static inline int timer_pending(struct timer_list *l)
658 {
659         if (l->expires > jiffies)
660                 return 1;
661         else
662                 return 0;
663 }
664
665 static inline int init_timer(struct timer_list *l)
666 {
667         INIT_LIST_HEAD(&l->tl_list);
668         return 0;
669 }
670
671 static inline void mod_timer(struct timer_list *l, int thetime)
672 {
673         l->expires = thetime;
674 }
675
676 static inline void del_timer(struct timer_list *l)
677 {
678         free(l);
679 }
680
681 typedef struct { volatile int counter; } atomic_t;
682
683 #define atomic_read(a) ((a)->counter)
684 #define atomic_set(a,b) do {(a)->counter = b; } while (0)
685 #define atomic_dec_and_test(a) ((--((a)->counter)) == 0)
686 #define atomic_inc(a)  (((a)->counter)++)
687 #define atomic_dec(a)  do { (a)->counter--; } while (0)
688 #define atomic_add(b,a)  do {(a)->counter += b;} while (0)
689 #define atomic_sub(b,a)  do {(a)->counter -= b;} while (0)
690
691 #ifndef likely
692 #define likely(exp) (exp)
693 #endif
694 #ifndef unlikely
695 #define unlikely(exp) (exp)
696 #endif
697
698 /* log related */
699 static inline int llog_init_commit_master(void) { return 0; }
700 static inline int llog_cleanup_commit_master(int force) { return 0; }
701 static inline void portals_run_lbug_upcall(char *file, const char *fn,
702                                            const int l){}
703
704 #define LBUG()                                                          \
705         do {                                                            \
706                 printf("!!!LBUG at %s:%d\n", __FILE__, __LINE__);       \
707                 sleep(1000000);                                         \
708         } while (0)
709
710
711
712 /* completion */
713 struct completion {
714         unsigned int done;
715         wait_queue_head_t wait;
716 };
717
718 #define COMPLETION_INITIALIZER(work) \
719         { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
720
721 #define DECLARE_COMPLETION(work) \
722         struct completion work = COMPLETION_INITIALIZER(work)
723
724 #define INIT_COMPLETION(x)      ((x).done = 0)
725
726 static inline void init_completion(struct completion *x)
727 {
728         x->done = 0;
729         init_waitqueue_head(&x->wait);
730 }
731
732 struct liblustre_wait_callback {
733         struct list_head    llwc_list;
734         int               (*llwc_fn)(void *arg);
735         void               *llwc_arg;
736 };
737
738 void *liblustre_register_wait_callback(int (*fn)(void *arg), void *arg);
739 void liblustre_deregister_wait_callback(void *notifier);
740 int liblustre_wait_event(int timeout);
741
742 #include <linux/obd_support.h>
743 #include <linux/lustre_idl.h>
744 #include <linux/lustre_lib.h>
745 #include <linux/lustre_import.h>
746 #include <linux/lustre_export.h>
747 #include <linux/lustre_net.h>
748
749
750 #endif