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