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