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