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