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