Whamcloud - gitweb
port llog fixes from b1_6 into HEAD
[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 #ifdef __KERNEL__
28 #error Kernel files should not #include <liblustre.h>
29 #else
30 /*
31  * The userspace implementations of linux/spinlock.h vary; we just
32  * include our own for all of them
33  */
34 #define __LINUX_SPINLOCK_H
35 #endif
36
37 #include <sys/mman.h>
38 #ifdef HAVE_STDINT_H
39 # include <stdint.h>
40 #endif
41 #ifdef HAVE_ASM_PAGE_H
42 # include <asm/page.h>
43 #endif
44 #ifdef HAVE_SYS_USER_H
45 # include <sys/user.h>
46 #endif
47 #ifdef HAVE_SYS_IOCTL_H
48 # include <sys/ioctl.h>
49 #endif
50 #ifndef _IOWR
51 # include "ioctl.h"
52 #endif
53
54 #include <stdio.h>
55 #include <sys/ioctl.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <errno.h>
59 #include <sys/stat.h>
60 #ifdef HAVE_SYS_VFS_H
61 # include <sys/vfs.h>
62 #endif
63 #include <unistd.h>
64 #include <fcntl.h>
65 #include <assert.h>
66
67 #include <libcfs/list.h>
68 #include <lnet/lnet.h>
69 #include <libcfs/kp30.h>
70 #include <libcfs/user-bitops.h>
71
72 /* definitions for liblustre */
73
74 #ifdef __CYGWIN__
75
76 #define CFS_PAGE_SHIFT 12
77 #define CFS_PAGE_SIZE (1UL << CFS_PAGE_SHIFT)
78 #define CFS_PAGE_MASK (~((__u64)CFS_PAGE_SIZE-1))
79 #define loff_t long long
80 #define ERESTART 2001
81 typedef unsigned short umode_t;
82
83 #endif
84
85 #ifndef CURRENT_SECONDS
86 # define CURRENT_SECONDS time(0)
87 #endif
88
89 #ifndef ARRAY_SIZE
90 #define ARRAY_SIZE(a) ((sizeof (a))/(sizeof ((a)[0])))
91 #endif
92
93 /* This is because lprocfs_status.h gets included here indirectly.  It would
94  * be much better to just avoid lprocfs being included into liblustre entirely
95  * but that requires more header surgery than I can handle right now.
96  */
97 #ifndef smp_processor_id
98 #define smp_processor_id() 0
99 #endif
100 #ifndef num_online_cpus
101 #define num_online_cpus() 1
102 #endif
103 #ifndef num_possible_cpus
104 #define num_possible_cpus() 1
105 #endif
106
107 /* always adopt 2.5 definitions */
108 #define KERNEL_VERSION(a,b,c) ((a)*100+(b)*10+c)
109 #define LINUX_VERSION_CODE KERNEL_VERSION(2,6,5)
110
111 #ifndef page_private
112 #define page_private(page) ((page)->private)
113 #define set_page_private(page, v) ((page)->private = (v))
114 #endif
115
116
117 static inline void inter_module_put(void *a)
118 {
119         return;
120 }
121
122 void *inter_module_get(char *arg);
123
124 /* cheats for now */
125
126 struct work_struct {
127         void (*ws_task)(void *arg);
128         void *ws_arg;
129 };
130
131 static inline void prepare_work(struct work_struct *q, void (*t)(void *),
132                                 void *arg)
133 {
134         q->ws_task = t;
135         q->ws_arg = arg;
136         return;
137 }
138
139 static inline void schedule_work(struct work_struct *q)
140 {
141         q->ws_task(q->ws_arg);
142 }
143
144
145 #define strnlen(a,b) strlen(a)
146 static inline void *kmalloc(int size, int prot)
147 {
148         return malloc(size);
149 }
150 #define vmalloc malloc
151 #define vfree free
152 #define kfree(a) free(a)
153 #define GFP_KERNEL 1
154 #define GFP_HIGHUSER 1
155 #define GFP_ATOMIC 1
156 #define GFP_NOFS 1
157 #define IS_ERR(a) ((unsigned long)(a) > (unsigned long)-1000L)
158 #define PTR_ERR(a) ((long)(a))
159 #define ERR_PTR(a) ((void*)((long)(a)))
160
161 typedef int (read_proc_t)(char *page, char **start, off_t off,
162                           int count, int *eof, void *data);
163
164 struct file; /* forward ref */
165 typedef int (write_proc_t)(struct file *file, const char *buffer,
166                            unsigned long count, void *data);
167
168 #define NIPQUAD(addr) \
169         ((unsigned char *)&addr)[0], \
170         ((unsigned char *)&addr)[1], \
171         ((unsigned char *)&addr)[2], \
172         ((unsigned char *)&addr)[3]
173
174 #if defined(__LITTLE_ENDIAN)
175 #define HIPQUAD(addr) \
176         ((unsigned char *)&addr)[3], \
177         ((unsigned char *)&addr)[2], \
178         ((unsigned char *)&addr)[1], \
179         ((unsigned char *)&addr)[0]
180 #elif defined(__BIG_ENDIAN)
181 #define HIPQUAD NIPQUAD
182 #else
183 #error "Undefined byteorder??"
184 #endif /* __LITTLE_ENDIAN */
185
186 /* bits ops */
187
188 /* a long can be more than 32 bits, so use BITS_PER_LONG
189  * to allow the compiler to adjust the bit shifting accordingly
190  */
191
192 static __inline__ int ext2_set_bit(int nr, void *addr)
193 {
194         return set_bit(nr, addr);
195 }
196
197 static __inline__ int ext2_clear_bit(int nr, void *addr)
198 {
199         return clear_bit(nr, addr);
200 }
201
202 static __inline__ int ext2_test_bit(int nr, void *addr)
203 {
204         return test_bit(nr, addr);
205 }
206
207 /* modules */
208
209 struct module {
210         int count;
211 };
212
213 static inline void MODULE_AUTHOR(char *name)
214 {
215         printf("%s\n", name);
216 }
217 #define MODULE_DESCRIPTION(name) MODULE_AUTHOR(name)
218 #define MODULE_LICENSE(name) MODULE_AUTHOR(name)
219
220 #define THIS_MODULE NULL
221 #define __init
222 #define __exit
223
224 /* devices */
225
226 static inline int misc_register(void *foo)
227 {
228         return 0;
229 }
230
231 static inline int misc_deregister(void *foo)
232 {
233         return 0;
234 }
235
236 static inline int request_module(char *name)
237 {
238         return (-EINVAL);
239 }
240
241 #define __MOD_INC_USE_COUNT(m)  do {} while (0)
242 #define __MOD_DEC_USE_COUNT(m)  do {} while (0)
243 #define MOD_INC_USE_COUNT       do {} while (0)
244 #define MOD_DEC_USE_COUNT       do {} while (0)
245 static inline void __module_get(struct module *module)
246 {
247 }
248
249 static inline int try_module_get(struct module *module)
250 {
251         return 1;
252 }
253
254 static inline void module_put(struct module *module)
255 {
256 }
257
258 /* module initialization */
259 extern int init_obdclass(void);
260 extern int ptlrpc_init(void);
261 extern int ldlm_init(void);
262 extern int osc_init(void);
263 extern int lov_init(void);
264 extern int mdc_init(void);
265 extern int lmv_init(void);
266 extern int mgc_init(void);
267 extern int echo_client_init(void);
268
269
270
271 /* general stuff */
272
273 #define EXPORT_SYMBOL(S)
274
275 struct rcu_head { };
276
277 typedef struct { } spinlock_t;
278 typedef __u64 kdev_t;
279
280 #define SPIN_LOCK_UNLOCKED (spinlock_t) { }
281 #define LASSERT_SPIN_LOCKED(lock) do {} while(0)
282 #define LASSERT_SEM_LOCKED(sem) do {} while(0)
283
284 static inline void spin_lock(spinlock_t *l) {return;}
285 static inline void spin_unlock(spinlock_t *l) {return;}
286 static inline void spin_lock_init(spinlock_t *l) {return;}
287 static inline void local_irq_save(unsigned long flag) {return;}
288 static inline void local_irq_restore(unsigned long flag) {return;}
289 static inline int spin_is_locked(spinlock_t *l) {return 1;}
290
291 static inline void spin_lock_bh(spinlock_t *l) {}
292 static inline void spin_unlock_bh(spinlock_t *l) {}
293 static inline void spin_lock_irqsave(spinlock_t *a, unsigned long b) {}
294 static inline void spin_unlock_irqrestore(spinlock_t *a, unsigned long b) {}
295
296 typedef spinlock_t rwlock_t;
297 #define RW_LOCK_UNLOCKED        SPIN_LOCK_UNLOCKED
298 #define read_lock(l)            spin_lock(l)
299 #define read_unlock(l)          spin_unlock(l)
300 #define write_lock(l)           spin_lock(l)
301 #define write_unlock(l)         spin_unlock(l)
302
303
304 #define min(x,y) ((x)<(y) ? (x) : (y))
305 #define max(x,y) ((x)>(y) ? (x) : (y))
306
307 #ifndef min_t
308 #define min_t(type,x,y) \
309         ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
310 #endif
311 #ifndef max_t
312 #define max_t(type,x,y) \
313         ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
314 #endif
315
316 #define simple_strtol strtol
317
318 /* registering symbols */
319 #ifndef ERESTARTSYS
320 #define ERESTARTSYS ERESTART
321 #endif
322 #define HZ 1
323
324 /* random */
325
326 void get_random_bytes(void *ptr, int size);
327
328 /* memory */
329
330 /* memory size: used for some client tunables */
331 #define num_physpages (256 * 1024) /* 1GB */
332
333 static inline int copy_from_user(void *a,void *b, int c)
334 {
335         memcpy(a,b,c);
336         return 0;
337 }
338
339 static inline int copy_to_user(void *a,void *b, int c)
340 {
341         memcpy(a,b,c);
342         return 0;
343 }
344
345
346 /* slabs */
347 typedef struct {
348          int size;
349 } kmem_cache_t;
350 #define SLAB_HWCACHE_ALIGN 0
351 static inline kmem_cache_t *
352 kmem_cache_create(const char *name, size_t objsize, size_t cdum,
353                   unsigned long d,
354                   void (*e)(void *, kmem_cache_t *, unsigned long),
355                   void (*f)(void *, kmem_cache_t *, unsigned long))
356 {
357         kmem_cache_t *c;
358         c = malloc(sizeof(*c));
359         if (!c)
360                 return NULL;
361         c->size = objsize;
362         CDEBUG(D_MALLOC, "alloc slab cache %s at %p, objsize %d\n",
363                name, c, (int)objsize);
364         return c;
365 };
366
367 static inline int kmem_cache_destroy(kmem_cache_t *a)
368 {
369         CDEBUG(D_MALLOC, "destroy slab cache %p, objsize %u\n", a, a->size);
370         free(a);
371         return 0;
372 }
373
374 /* struct page decl moved out from here into portals/include/libcfs/user-prim.h */
375
376 /* 2.4 defines */
377 #define PAGE_LIST_ENTRY list
378 #define PAGE_LIST(page) ((page)->list)
379
380 #define kmap(page) (page)->addr
381 #define kunmap(a) do {} while (0)
382
383 static inline cfs_page_t *alloc_pages(int mask, unsigned long order)
384 {
385         cfs_page_t *pg = malloc(sizeof(*pg));
386
387         if (!pg)
388                 return NULL;
389 #if 0 //#ifdef MAP_ANONYMOUS
390         pg->addr = mmap(0, PAGE_SIZE << order, PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
391 #else
392         pg->addr = malloc(CFS_PAGE_SIZE << order);
393 #endif
394
395         if (!pg->addr) {
396                 free(pg);
397                 return NULL;
398         }
399         return pg;
400 }
401 #define cfs_alloc_pages(mask, order)  alloc_pages((mask), (order))
402
403 #define alloc_page(mask)      alloc_pages((mask), 0)
404 #define cfs_alloc_page(mask)  alloc_page(mask)
405
406 static inline void __free_pages(cfs_page_t *pg, int what)
407 {
408 #if 0 //#ifdef MAP_ANONYMOUS
409         munmap(pg->addr, PAGE_SIZE);
410 #else
411         free(pg->addr);
412 #endif
413         free(pg);
414 }
415 #define __cfs_free_pages(pg, order)  __free_pages((pg), (order))
416
417 #define __free_page(page) __free_pages((page), 0)
418 #define free_page(page) __free_page(page)
419 #define __cfs_free_page(page)  __cfs_free_pages((page), 0)
420
421 static inline cfs_page_t* __grab_cache_page(unsigned long index)
422 {
423         cfs_page_t *pg = alloc_pages(0, 0);
424
425         if (pg)
426                 pg->index = index;
427         return pg;
428 }
429
430 #define grab_cache_page(index) __grab_cache_page(index)
431 #define page_cache_release(page) __free_pages(page, 0)
432
433 /* arithmetic */
434 #define do_div(a,b)                     \
435         ({                              \
436                 unsigned long remainder;\
437                 remainder = (a) % (b);  \
438                 (a) = (a) / (b);        \
439                 (remainder);            \
440         })
441
442 /* VFS stuff */
443 #define ATTR_MODE       0x0001
444 #define ATTR_UID        0x0002
445 #define ATTR_GID        0x0004
446 #define ATTR_SIZE       0x0008
447 #define ATTR_ATIME      0x0010
448 #define ATTR_MTIME      0x0020
449 #define ATTR_CTIME      0x0040
450 #define ATTR_ATIME_SET  0x0080
451 #define ATTR_MTIME_SET  0x0100
452 #define ATTR_FORCE      0x0200  /* Not a change, but a change it */
453 #define ATTR_ATTR_FLAG  0x0400
454 #define ATTR_RAW        0x0800  /* file system, not vfs will massage attrs */
455 #define ATTR_FROM_OPEN  0x1000  /* called from open path, ie O_TRUNC */
456 #define ATTR_CTIME_SET  0x2000
457 #define ATTR_BLOCKS     0x4000
458 #define ATTR_KILL_SUID  0
459 #define ATTR_KILL_SGID  0
460
461 struct iattr {
462         unsigned int    ia_valid;
463         umode_t         ia_mode;
464         uid_t           ia_uid;
465         gid_t           ia_gid;
466         loff_t          ia_size;
467         time_t          ia_atime;
468         time_t          ia_mtime;
469         time_t          ia_ctime;
470         unsigned int    ia_attr_flags;
471 };
472
473 #define ll_iattr iattr
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 struct dentry {
518         int d_count;
519 };
520
521 struct vfsmount {
522         void *pwd;
523 };
524
525 /* semaphores */
526 struct rw_semaphore {
527         int count;
528 };
529
530 /* semaphores */
531 struct semaphore {
532         int count;
533 };
534
535 /* use the macro's argument to avoid unused warnings */
536 #define down(a) do { (void)a; } while (0)
537 #define mutex_down(a)   down(a)
538 #define up(a) do { (void)a; } while (0)
539 #define mutex_up(a)     up(a)
540 #define down_read(a) do { (void)a; } while (0)
541 #define up_read(a) do { (void)a; } while (0)
542 #define down_write(a) do { (void)a; } while (0)
543 #define up_write(a) do { (void)a; } while (0)
544 #define sema_init(a,b) do { (void)a; } while (0)
545 #define init_rwsem(a) do { (void)a; } while (0)
546 #define DECLARE_MUTEX(name)     \
547         struct semaphore name = { 1 }
548 static inline void init_MUTEX (struct semaphore *sem)
549 {
550         sema_init(sem, 1);
551 }
552 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
553 {
554         sema_init(sem, 0);
555 }
556
557 #define init_mutex(s)   init_MUTEX(s)
558
559 typedef struct  {
560         struct list_head sleepers;
561 } wait_queue_head_t;
562
563 typedef struct  {
564         struct list_head sleeping;
565         void *process;
566 } wait_queue_t;
567
568 struct signal {
569         int signal;
570 };
571
572 struct task_struct {
573         int state;
574         struct signal pending;
575         char comm[32];
576         int uid;
577         int gid;
578         int pid;
579         int fsuid;
580         int fsgid;
581         int max_groups;
582         int ngroups;
583         gid_t *groups;
584         __u32 cap_effective;
585 };
586
587 typedef struct task_struct cfs_task_t;
588 #define cfs_current()           current
589 #define cfs_curproc_pid()       (current->pid)
590 #define cfs_curproc_comm()      (current->comm)
591
592 extern struct task_struct *current;
593 int in_group_p(gid_t gid);
594 static inline int capable(int cap)
595 {
596         if (current->cap_effective & (1 << cap))
597                 return 1;
598         else
599                 return 0;
600 }
601
602 #define set_current_state(foo) do { current->state = foo; } while (0)
603
604 #define init_waitqueue_entry(q,p) do { (q)->process = p; } while (0)
605 #define add_wait_queue(q,p) do {  list_add(&(q)->sleepers, &(p)->sleeping); } while (0)
606 #define del_wait_queue(p) do { list_del(&(p)->sleeping); } while (0)
607 #define remove_wait_queue(q,p) do { list_del(&(p)->sleeping); } while (0)
608
609 #define DECLARE_WAIT_QUEUE_HEAD(HEAD)                           \
610         wait_queue_head_t HEAD = {                              \
611                 .sleepers = LIST_HEAD_INIT(HEAD.sleepers)       \
612         }
613 #define init_waitqueue_head(l) INIT_LIST_HEAD(&(l)->sleepers)
614 #define wake_up(l) do { int a; a++; } while (0)
615 #define TASK_INTERRUPTIBLE 0
616 #define TASK_UNINTERRUPTIBLE 1
617 #define TASK_RUNNING 2
618
619 #define wait_event_interruptible(wq, condition)                         \
620 ({                                                                      \
621         struct l_wait_info lwi;                                         \
622         int timeout = 100000000;/* for ever */                          \
623         int ret;                                                        \
624                                                                         \
625         lwi = LWI_TIMEOUT(timeout, NULL, NULL);                         \
626         ret = l_wait_event(NULL, condition, &lwi);                      \
627                                                                         \
628         ret;                                                            \
629 })
630
631 #define in_interrupt() (0)
632
633 #define schedule() do {} while (0)
634 static inline int schedule_timeout(signed long t)
635 {
636         return 0;
637 }
638
639 #define lock_kernel() do {} while (0)
640 #define unlock_kernel() do {} while (0)
641 #define daemonize(l) do {} while (0)
642 #define sigfillset(l) do {} while (0)
643 #define recalc_sigpending(l) do {} while (0)
644 #define kernel_thread(l,m,n) LBUG()
645
646 #define USERMODEHELPER(path, argv, envp) (0)
647 #define SIGNAL_MASK_ASSERT()
648 #define KERN_INFO
649
650 #include <sys/time.h>
651 #if HZ != 1
652 #error "liblustre's jiffies currently expects HZ to be 1"
653 #endif
654 #define jiffies                                 \
655 ({                                              \
656         unsigned long _ret = 0;                 \
657         struct timeval tv;                      \
658         if (gettimeofday(&tv, NULL) == 0)       \
659                 _ret = tv.tv_sec;               \
660         _ret;                                   \
661 })
662 #define get_jiffies_64()  (__u64)jiffies
663 #define time_after(a, b) ((long)(b) - (long)(a) < 0)
664 #define time_before(a, b) time_after(b,a)
665 #define time_after_eq(a,b)      ((long)(a) - (long)(b) >= 0)
666
667 struct timer_list {
668         struct list_head tl_list;
669         void (*function)(unsigned long unused);
670         unsigned long data;
671         long expires;
672 };
673
674 static inline int timer_pending(struct timer_list *l)
675 {
676         if (time_after(l->expires, jiffies))
677                 return 1;
678         else
679                 return 0;
680 }
681
682 static inline int init_timer(struct timer_list *l)
683 {
684         INIT_LIST_HEAD(&l->tl_list);
685         return 0;
686 }
687
688 static inline void mod_timer(struct timer_list *l, int thetime)
689 {
690         l->expires = thetime;
691 }
692
693 static inline void del_timer(struct timer_list *l)
694 {
695         free(l);
696 }
697
698 typedef struct { volatile int counter; } atomic_t;
699
700 #define ATOMIC_INIT(i) { (i) }
701
702 #define atomic_read(a) ((a)->counter)
703 #define atomic_set(a,b) do {(a)->counter = b; } while (0)
704 #define atomic_dec_and_test(a) ((--((a)->counter)) == 0)
705 #define atomic_dec_and_lock(a,b) ((--((a)->counter)) == 0)
706 #define atomic_inc(a)  (((a)->counter)++)
707 #define atomic_dec(a)  do { (a)->counter--; } while (0)
708 #define atomic_add(b,a)  do {(a)->counter += b;} while (0)
709 #define atomic_add_return(n,a) ((a)->counter = n)
710 #define atomic_inc_return(a) atomic_add_return(1,a)
711 #define atomic_sub(b,a)  do {(a)->counter -= b;} while (0)
712 #define atomic_sub_return(n,a) ((a)->counter -= n)
713 #define atomic_dec_return(a)  atomic_sub_return(1,a)
714
715 #ifndef likely
716 #define likely(exp) (exp)
717 #endif
718 #ifndef unlikely
719 #define unlikely(exp) (exp)
720 #endif
721
722 #define might_sleep()
723 #define might_sleep_if(c)
724 #define smp_mb()
725
726 static inline
727 int test_and_set_bit(int nr, unsigned long *addr)
728 {
729         int oldbit;
730
731         while (nr >= sizeof(long)) {
732                 nr -= sizeof(long);
733                 addr++;
734         }
735
736         oldbit = (*addr) & (1 << nr);
737         *addr |= (1 << nr);
738         return oldbit;
739 }
740
741 static inline
742 int test_and_clear_bit(int nr, unsigned long *addr)
743 {
744         int oldbit;
745
746         while (nr >= sizeof(long)) {
747                 nr -= sizeof(long);
748                 addr++;
749         }
750
751         oldbit = (*addr) & (1 << nr);
752         *addr &= ~(1 << nr);
753         return oldbit;
754 }
755
756 /* FIXME sys/capability will finally included linux/fs.h thus
757  * cause numerous trouble on x86-64. as temporary solution for
758  * build broken at Cray, we copy definition we need from capability.h
759  * FIXME
760  */
761 struct _cap_struct;
762 typedef struct _cap_struct *cap_t;
763 typedef int cap_value_t;
764 typedef enum {
765     CAP_EFFECTIVE=0,
766     CAP_PERMITTED=1,
767     CAP_INHERITABLE=2
768 } cap_flag_t;
769 typedef enum {
770     CAP_CLEAR=0,
771     CAP_SET=1
772 } cap_flag_value_t;
773
774 #define CAP_DAC_OVERRIDE        1
775 #define CAP_DAC_READ_SEARCH     2
776 #define CAP_FOWNER              3
777 #define CAP_FSETID              4
778 #define CAP_SYS_ADMIN          21
779
780 cap_t   cap_get_proc(void);
781 int     cap_get_flag(cap_t, cap_value_t, cap_flag_t, cap_flag_value_t *);
782
783 static inline void libcfs_run_lbug_upcall(char *file, const char *fn,
784                                            const int l){}
785
786 /* completion */
787 struct completion {
788         unsigned int done;
789         cfs_waitq_t wait;
790 };
791
792 #define COMPLETION_INITIALIZER(work) \
793         { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
794
795 #define DECLARE_COMPLETION(work) \
796         struct completion work = COMPLETION_INITIALIZER(work)
797
798 #define INIT_COMPLETION(x)      ((x).done = 0)
799
800 static inline void init_completion(struct completion *x)
801 {
802         x->done = 0;
803         init_waitqueue_head(&x->wait);
804 }
805
806 struct liblustre_wait_callback {
807         struct list_head    llwc_list;
808         const char         *llwc_name;
809         int               (*llwc_fn)(void *arg);
810         void               *llwc_arg;
811 };
812
813 void *liblustre_register_wait_callback(const char *name,
814                                        int (*fn)(void *arg), void *arg);
815 void liblustre_deregister_wait_callback(void *notifier);
816 int liblustre_wait_event(int timeout);
817
818 void *liblustre_register_idle_callback(const char *name, 
819                                        int (*fn)(void *arg), void *arg);
820 void liblustre_deregister_idle_callback(void *notifier);
821 void liblustre_wait_idle(void);
822
823 /* flock related */
824 struct nfs_lock_info {
825         __u32             state;
826         __u32             flags;
827         void            *host;
828 };
829
830 typedef struct file_lock {
831         struct file_lock *fl_next;      /* singly linked list for this inode  */
832         struct list_head fl_link;       /* doubly linked list of all locks */
833         struct list_head fl_block;      /* circular list of blocked processes */
834         void *fl_owner;
835         unsigned int fl_pid;
836         cfs_waitq_t fl_wait;
837         struct file *fl_file;
838         unsigned char fl_flags;
839         unsigned char fl_type;
840         loff_t fl_start;
841         loff_t fl_end;
842
843         void (*fl_notify)(struct file_lock *);  /* unblock callback */
844         void (*fl_insert)(struct file_lock *);  /* lock insertion callback */
845         void (*fl_remove)(struct file_lock *);  /* lock removal callback */
846
847         void *fl_fasync; /* for lease break notifications */
848         unsigned long fl_break_time;    /* for nonblocking lease breaks */
849
850         union {
851                 struct nfs_lock_info    nfs_fl;
852         } fl_u;
853 } cfs_flock_t;
854
855 #define cfs_flock_type(fl)                  ((fl)->fl_type)
856 #define cfs_flock_set_type(fl, type)        do { (fl)->fl_type = (type); } while(0)
857 #define cfs_flock_pid(fl)                   ((fl)->fl_pid)
858 #define cfs_flock_set_pid(fl, pid)          do { (fl)->fl_pid = (pid); } while(0)
859 #define cfs_flock_start(fl)                 ((fl)->fl_start)
860 #define cfs_flock_set_start(fl, start)      do { (fl)->fl_start = (start); } while(0)
861 #define cfs_flock_end(fl)                   ((fl)->fl_end)
862 #define cfs_flock_set_end(fl, end)          do { (fl)->fl_end = (end); } while(0)
863
864 #ifndef OFFSET_MAX
865 #define INT_LIMIT(x)    (~((x)1 << (sizeof(x)*8 - 1)))
866 #define OFFSET_MAX      INT_LIMIT(loff_t)
867 #endif
868
869 /* XXX: defined in kernel */
870 #define FL_POSIX        1
871 #define FL_SLEEP        128
872
873 /* quota */
874 #define QUOTA_OK 0
875 #define NO_QUOTA 1
876
877 /* ACL */
878 struct posix_acl_entry {
879         short                   e_tag;
880         unsigned short          e_perm;
881         unsigned int            e_id;
882 };
883
884 struct posix_acl {
885         atomic_t                a_refcount;
886         unsigned int            a_count;
887         struct posix_acl_entry  a_entries[0];
888 };
889
890 typedef struct {
891         __u16           e_tag;
892         __u16           e_perm;
893         __u32           e_id;
894 } xattr_acl_entry;
895
896 typedef struct {
897         __u32           a_version;
898         xattr_acl_entry a_entries[0];
899 } xattr_acl_header;
900
901 static inline size_t xattr_acl_size(int count)
902 {
903         return sizeof(xattr_acl_header) + count * sizeof(xattr_acl_entry);
904 }
905
906 static inline
907 struct posix_acl * posix_acl_from_xattr(const void *value, size_t size)
908 {
909         return NULL;
910 }
911
912 static inline
913 int posix_acl_valid(const struct posix_acl *acl)
914 {
915         return 0;
916 }
917
918 static inline
919 void posix_acl_release(struct posix_acl *acl)
920 {
921 }
922
923 #ifdef LIBLUSTRE_POSIX_ACL
924 # ifndef posix_acl_xattr_entry 
925 #  define posix_acl_xattr_entry xattr_acl_entry
926 # endif
927 # ifndef posix_acl_xattr_header 
928 #  define posix_acl_xattr_header xattr_acl_header
929 # endif
930 # ifndef posix_acl_xattr_size
931 #  define posix_acl_xattr_size(entry) xattr_acl_size(entry)
932 # endif
933 # ifndef CONFIG_FS_POSIX_ACL
934 #  define CONFIG_FS_POSIX_ACL 1
935 # endif
936 #endif
937
938 #ifndef ENOTSUPP
939 #define ENOTSUPP ENOTSUP
940 #endif
941
942 typedef int mm_segment_t;
943 enum {
944         KERNEL_DS,
945         USER_DS
946 };
947 static inline mm_segment_t get_fs(void)
948 {
949         return USER_DS;
950 }
951
952 static inline void set_fs(mm_segment_t seg)
953 {
954 }
955
956 #include <obd_support.h>
957 #include <lustre/lustre_idl.h>
958 #include <lustre_lib.h>
959 #include <lustre_import.h>
960 #include <lustre_export.h>
961 #include <lustre_net.h>
962
963 /* Fast hashing routine for a long.
964    (C) 2002 William Lee Irwin III, IBM */
965
966 /*
967  * Knuth recommends primes in approximately golden ratio to the maximum
968  * integer representable by a machine word for multiplicative hashing.
969  * Chuck Lever verified the effectiveness of this technique:
970  * http://www.citi.umich.edu/techreports/reports/citi-tr-00-1.pdf
971  *
972  * These primes are chosen to be bit-sparse, that is operations on
973  * them can use shifts and additions instead of multiplications for
974  * machines where multiplications are slow.
975  */
976 #if BITS_PER_LONG == 32
977 /* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
978 #define GOLDEN_RATIO_PRIME 0x9e370001UL
979 #elif BITS_PER_LONG == 64
980 /*  2^63 + 2^61 - 2^57 + 2^54 - 2^51 - 2^18 + 1 */
981 #define GOLDEN_RATIO_PRIME 0x9e37fffffffc0001UL
982 #else
983 #error Define GOLDEN_RATIO_PRIME for your wordsize.
984 #endif
985
986 static inline unsigned long hash_long(unsigned long val, unsigned int bits)
987 {
988         unsigned long hash = val;
989
990 #if BITS_PER_LONG == 64
991         /*  Sigh, gcc can't optimise this alone like it does for 32 bits. */
992         unsigned long n = hash;
993         n <<= 18;
994         hash -= n;
995         n <<= 33;
996         hash -= n;
997         n <<= 3;
998         hash += n;
999         n <<= 3;
1000         hash -= n;
1001         n <<= 4;
1002         hash += n;
1003         n <<= 2;
1004         hash += n;
1005 #else
1006         /* On some cpus multiply is faster, on others gcc will do shifts */
1007         hash *= GOLDEN_RATIO_PRIME;
1008 #endif
1009
1010         /* High bits are more random, so use them. */
1011         return hash >> (BITS_PER_LONG - bits);
1012 }
1013         
1014 static inline unsigned long hash_ptr(void *ptr, unsigned int bits)
1015 {
1016         return hash_long((unsigned long)ptr, bits);
1017 }
1018
1019 #endif