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